blob: 19eedf2e630b5b34d36b3aed4fbb94a9429619c9 [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"
Lu Fengqiafce7722016-06-13 09:36:46 +080023#include "transaction.h"
Chris Masond1310b22008-01-24 16:13:08 -050024
Chris Masond1310b22008-01-24 16:13:08 -050025static struct kmem_cache *extent_state_cache;
26static struct kmem_cache *extent_buffer_cache;
Chris Mason9be33952013-05-17 18:30:14 -040027static struct bio_set *btrfs_bioset;
Chris Masond1310b22008-01-24 16:13:08 -050028
Filipe Manana27a35072014-07-06 20:09:59 +010029static inline bool extent_state_in_tree(const struct extent_state *state)
30{
31 return !RB_EMPTY_NODE(&state->rb_node);
32}
33
Eric Sandeen6d49ba12013-04-22 16:12:31 +000034#ifdef CONFIG_BTRFS_DEBUG
Chris Masond1310b22008-01-24 16:13:08 -050035static LIST_HEAD(buffers);
36static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040037
Chris Masond3977122009-01-05 21:25:51 -050038static DEFINE_SPINLOCK(leak_lock);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000039
40static inline
41void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
42{
43 unsigned long flags;
44
45 spin_lock_irqsave(&leak_lock, flags);
46 list_add(new, head);
47 spin_unlock_irqrestore(&leak_lock, flags);
48}
49
50static inline
51void btrfs_leak_debug_del(struct list_head *entry)
52{
53 unsigned long flags;
54
55 spin_lock_irqsave(&leak_lock, flags);
56 list_del(entry);
57 spin_unlock_irqrestore(&leak_lock, flags);
58}
59
60static inline
61void btrfs_leak_debug_check(void)
62{
63 struct extent_state *state;
64 struct extent_buffer *eb;
65
66 while (!list_empty(&states)) {
67 state = list_entry(states.next, struct extent_state, leak_list);
David Sterba9ee49a042015-01-14 19:52:13 +010068 pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
Filipe Manana27a35072014-07-06 20:09:59 +010069 state->start, state->end, state->state,
70 extent_state_in_tree(state),
Elena Reshetovab7ac31b2017-03-03 10:55:19 +020071 refcount_read(&state->refs));
Eric Sandeen6d49ba12013-04-22 16:12:31 +000072 list_del(&state->leak_list);
73 kmem_cache_free(extent_state_cache, state);
74 }
75
76 while (!list_empty(&buffers)) {
77 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Jeff Mahoney62e85572016-09-20 10:05:01 -040078 pr_err("BTRFS: buffer leak start %llu len %lu refs %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +020079 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]",
David Sterbaf85b7372017-01-20 14:54:07 +0100101 caller, btrfs_ino(BTRFS_I(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
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600130 /* tells the submit_bio code to use REQ_SYNC */
Chris Masonffbd5172009-04-20 15:50:09 -0400131 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;
Qu Wenruofefdc552015-10-12 15:35:38 +0800144 if (!set && (state->state & bits) == 0)
145 return;
Qu Wenruod38ed272015-10-12 14:53:37 +0800146 changeset->bytes_changed += state->end - state->start + 1;
David Sterba53d32352017-02-13 13:42:29 +0100147 ret = ulist_add(&changeset->range_changed, state->start, state->end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800148 GFP_ATOMIC);
149 /* ENOMEM */
150 BUG_ON(ret < 0);
151}
152
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400153static noinline void flush_write_bio(void *data);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400154static inline struct btrfs_fs_info *
155tree_fs_info(struct extent_io_tree *tree)
156{
Josef Bacika5dee372013-12-13 10:02:44 -0500157 if (!tree->mapping)
158 return NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400159 return btrfs_sb(tree->mapping->host->i_sb);
160}
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400161
Chris Masond1310b22008-01-24 16:13:08 -0500162int __init extent_io_init(void)
163{
David Sterba837e1972012-09-07 03:00:48 -0600164 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200165 sizeof(struct extent_state), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300166 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500167 if (!extent_state_cache)
168 return -ENOMEM;
169
David Sterba837e1972012-09-07 03:00:48 -0600170 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200171 sizeof(struct extent_buffer), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300172 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500173 if (!extent_buffer_cache)
174 goto free_state_cache;
Chris Mason9be33952013-05-17 18:30:14 -0400175
176 btrfs_bioset = bioset_create(BIO_POOL_SIZE,
NeilBrown011067b2017-06-18 14:38:57 +1000177 offsetof(struct btrfs_io_bio, bio),
178 BIOSET_NEED_BVECS);
Chris Mason9be33952013-05-17 18:30:14 -0400179 if (!btrfs_bioset)
180 goto free_buffer_cache;
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700181
182 if (bioset_integrity_create(btrfs_bioset, BIO_POOL_SIZE))
183 goto free_bioset;
184
Chris Masond1310b22008-01-24 16:13:08 -0500185 return 0;
186
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700187free_bioset:
188 bioset_free(btrfs_bioset);
189 btrfs_bioset = NULL;
190
Chris Mason9be33952013-05-17 18:30:14 -0400191free_buffer_cache:
192 kmem_cache_destroy(extent_buffer_cache);
193 extent_buffer_cache = NULL;
194
Chris Masond1310b22008-01-24 16:13:08 -0500195free_state_cache:
196 kmem_cache_destroy(extent_state_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400197 extent_state_cache = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500198 return -ENOMEM;
199}
200
201void extent_io_exit(void)
202{
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000203 btrfs_leak_debug_check();
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000204
205 /*
206 * Make sure all delayed rcu free are flushed before we
207 * destroy caches.
208 */
209 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +0800210 kmem_cache_destroy(extent_state_cache);
211 kmem_cache_destroy(extent_buffer_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400212 if (btrfs_bioset)
213 bioset_free(btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -0500214}
215
216void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200217 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500218{
Eric Paris6bef4d32010-02-23 19:43:04 +0000219 tree->state = RB_ROOT;
Chris Masond1310b22008-01-24 16:13:08 -0500220 tree->ops = NULL;
221 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500222 spin_lock_init(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500223 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500224}
Chris Masond1310b22008-01-24 16:13:08 -0500225
Christoph Hellwigb2950862008-12-02 09:54:17 -0500226static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500227{
228 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500229
Michal Hocko3ba7ab22017-01-09 15:39:02 +0100230 /*
231 * The given mask might be not appropriate for the slab allocator,
232 * drop the unsupported bits
233 */
234 mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
Chris Masond1310b22008-01-24 16:13:08 -0500235 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400236 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500237 return state;
238 state->state = 0;
David Sterba47dc1962016-02-11 13:24:13 +0100239 state->failrec = NULL;
Filipe Manana27a35072014-07-06 20:09:59 +0100240 RB_CLEAR_NODE(&state->rb_node);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000241 btrfs_leak_debug_add(&state->leak_list, &states);
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200242 refcount_set(&state->refs, 1);
Chris Masond1310b22008-01-24 16:13:08 -0500243 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100244 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500245 return state;
246}
Chris Masond1310b22008-01-24 16:13:08 -0500247
Chris Mason4845e442010-05-25 20:56:50 -0400248void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500249{
Chris Masond1310b22008-01-24 16:13:08 -0500250 if (!state)
251 return;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200252 if (refcount_dec_and_test(&state->refs)) {
Filipe Manana27a35072014-07-06 20:09:59 +0100253 WARN_ON(extent_state_in_tree(state));
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000254 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100255 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500256 kmem_cache_free(extent_state_cache, state);
257 }
258}
Chris Masond1310b22008-01-24 16:13:08 -0500259
Filipe Mananaf2071b22014-02-12 15:05:53 +0000260static struct rb_node *tree_insert(struct rb_root *root,
261 struct rb_node *search_start,
262 u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000263 struct rb_node *node,
264 struct rb_node ***p_in,
265 struct rb_node **parent_in)
Chris Masond1310b22008-01-24 16:13:08 -0500266{
Filipe Mananaf2071b22014-02-12 15:05:53 +0000267 struct rb_node **p;
Chris Masond3977122009-01-05 21:25:51 -0500268 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500269 struct tree_entry *entry;
270
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000271 if (p_in && parent_in) {
272 p = *p_in;
273 parent = *parent_in;
274 goto do_insert;
275 }
276
Filipe Mananaf2071b22014-02-12 15:05:53 +0000277 p = search_start ? &search_start : &root->rb_node;
Chris Masond3977122009-01-05 21:25:51 -0500278 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500279 parent = *p;
280 entry = rb_entry(parent, struct tree_entry, rb_node);
281
282 if (offset < entry->start)
283 p = &(*p)->rb_left;
284 else if (offset > entry->end)
285 p = &(*p)->rb_right;
286 else
287 return parent;
288 }
289
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000290do_insert:
Chris Masond1310b22008-01-24 16:13:08 -0500291 rb_link_node(node, parent, p);
292 rb_insert_color(node, root);
293 return NULL;
294}
295
Chris Mason80ea96b2008-02-01 14:51:59 -0500296static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000297 struct rb_node **prev_ret,
298 struct rb_node **next_ret,
299 struct rb_node ***p_ret,
300 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500301{
Chris Mason80ea96b2008-02-01 14:51:59 -0500302 struct rb_root *root = &tree->state;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000303 struct rb_node **n = &root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500304 struct rb_node *prev = NULL;
305 struct rb_node *orig_prev = NULL;
306 struct tree_entry *entry;
307 struct tree_entry *prev_entry = NULL;
308
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000309 while (*n) {
310 prev = *n;
311 entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500312 prev_entry = entry;
313
314 if (offset < entry->start)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000315 n = &(*n)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500316 else if (offset > entry->end)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000317 n = &(*n)->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500318 else
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000319 return *n;
Chris Masond1310b22008-01-24 16:13:08 -0500320 }
321
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000322 if (p_ret)
323 *p_ret = n;
324 if (parent_ret)
325 *parent_ret = prev;
326
Chris Masond1310b22008-01-24 16:13:08 -0500327 if (prev_ret) {
328 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500329 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500330 prev = rb_next(prev);
331 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
332 }
333 *prev_ret = prev;
334 prev = orig_prev;
335 }
336
337 if (next_ret) {
338 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500339 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500340 prev = rb_prev(prev);
341 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
342 }
343 *next_ret = prev;
344 }
345 return NULL;
346}
347
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000348static inline struct rb_node *
349tree_search_for_insert(struct extent_io_tree *tree,
350 u64 offset,
351 struct rb_node ***p_ret,
352 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500353{
Chris Mason70dec802008-01-29 09:59:12 -0500354 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500355 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500356
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000357 ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret);
Chris Masond3977122009-01-05 21:25:51 -0500358 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500359 return prev;
360 return ret;
361}
362
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000363static inline struct rb_node *tree_search(struct extent_io_tree *tree,
364 u64 offset)
365{
366 return tree_search_for_insert(tree, offset, NULL, NULL);
367}
368
Josef Bacik9ed74f22009-09-11 16:12:44 -0400369static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
370 struct extent_state *other)
371{
372 if (tree->ops && tree->ops->merge_extent_hook)
373 tree->ops->merge_extent_hook(tree->mapping->host, new,
374 other);
375}
376
Chris Masond1310b22008-01-24 16:13:08 -0500377/*
378 * utility function to look for merge candidates inside a given range.
379 * Any extents with matching state are merged together into a single
380 * extent in the tree. Extents with EXTENT_IO in their state field
381 * are not merged because the end_io handlers need to be able to do
382 * operations on them without sleeping (or doing allocations/splits).
383 *
384 * This should be called with the tree lock held.
385 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000386static void merge_state(struct extent_io_tree *tree,
387 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500388{
389 struct extent_state *other;
390 struct rb_node *other_node;
391
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400392 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000393 return;
Chris Masond1310b22008-01-24 16:13:08 -0500394
395 other_node = rb_prev(&state->rb_node);
396 if (other_node) {
397 other = rb_entry(other_node, struct extent_state, rb_node);
398 if (other->end == state->start - 1 &&
399 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400400 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500401 state->start = other->start;
Chris Masond1310b22008-01-24 16:13:08 -0500402 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100403 RB_CLEAR_NODE(&other->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500404 free_extent_state(other);
405 }
406 }
407 other_node = rb_next(&state->rb_node);
408 if (other_node) {
409 other = rb_entry(other_node, struct extent_state, rb_node);
410 if (other->start == state->end + 1 &&
411 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400412 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400413 state->end = other->end;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400414 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100415 RB_CLEAR_NODE(&other->rb_node);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400416 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500417 }
418 }
Chris Masond1310b22008-01-24 16:13:08 -0500419}
420
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000421static void set_state_cb(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +0100422 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500423{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000424 if (tree->ops && tree->ops->set_bit_hook)
425 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500426}
427
428static void clear_state_cb(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +0100429 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500430{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400431 if (tree->ops && tree->ops->clear_bit_hook)
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +0200432 tree->ops->clear_bit_hook(BTRFS_I(tree->mapping->host),
433 state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500434}
435
Xiao Guangrong3150b692011-07-14 03:19:08 +0000436static void set_state_bits(struct extent_io_tree *tree,
Qu Wenruod38ed272015-10-12 14:53:37 +0800437 struct extent_state *state, unsigned *bits,
438 struct extent_changeset *changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000439
Chris Masond1310b22008-01-24 16:13:08 -0500440/*
441 * insert an extent_state struct into the tree. 'bits' are set on the
442 * struct before it is inserted.
443 *
444 * This may return -EEXIST if the extent is already there, in which case the
445 * state struct is freed.
446 *
447 * The tree lock is not taken internally. This is a utility function and
448 * probably isn't what you want to call (see set/clear_extent_bit).
449 */
450static int insert_state(struct extent_io_tree *tree,
451 struct extent_state *state, u64 start, u64 end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000452 struct rb_node ***p,
453 struct rb_node **parent,
Qu Wenruod38ed272015-10-12 14:53:37 +0800454 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500455{
456 struct rb_node *node;
457
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000458 if (end < start)
Frank Holtonefe120a2013-12-20 11:37:06 -0500459 WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200460 end, start);
Chris Masond1310b22008-01-24 16:13:08 -0500461 state->start = start;
462 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400463
Qu Wenruod38ed272015-10-12 14:53:37 +0800464 set_state_bits(tree, state, bits, changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000465
Filipe Mananaf2071b22014-02-12 15:05:53 +0000466 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
Chris Masond1310b22008-01-24 16:13:08 -0500467 if (node) {
468 struct extent_state *found;
469 found = rb_entry(node, struct extent_state, rb_node);
Jeff Mahoney62e85572016-09-20 10:05:01 -0400470 pr_err("BTRFS: found node %llu %llu on insert of %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200471 found->start, found->end, start, end);
Chris Masond1310b22008-01-24 16:13:08 -0500472 return -EEXIST;
473 }
474 merge_state(tree, state);
475 return 0;
476}
477
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000478static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400479 u64 split)
480{
481 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000482 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400483}
484
Chris Masond1310b22008-01-24 16:13:08 -0500485/*
486 * split a given extent state struct in two, inserting the preallocated
487 * struct 'prealloc' as the newly created second half. 'split' indicates an
488 * offset inside 'orig' where it should be split.
489 *
490 * Before calling,
491 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
492 * are two extent state structs in the tree:
493 * prealloc: [orig->start, split - 1]
494 * orig: [ split, orig->end ]
495 *
496 * The tree locks are not taken by this function. They need to be held
497 * by the caller.
498 */
499static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
500 struct extent_state *prealloc, u64 split)
501{
502 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400503
504 split_cb(tree, orig, split);
505
Chris Masond1310b22008-01-24 16:13:08 -0500506 prealloc->start = orig->start;
507 prealloc->end = split - 1;
508 prealloc->state = orig->state;
509 orig->start = split;
510
Filipe Mananaf2071b22014-02-12 15:05:53 +0000511 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
512 &prealloc->rb_node, NULL, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500513 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500514 free_extent_state(prealloc);
515 return -EEXIST;
516 }
517 return 0;
518}
519
Li Zefancdc6a392012-03-12 16:39:48 +0800520static struct extent_state *next_state(struct extent_state *state)
521{
522 struct rb_node *next = rb_next(&state->rb_node);
523 if (next)
524 return rb_entry(next, struct extent_state, rb_node);
525 else
526 return NULL;
527}
528
Chris Masond1310b22008-01-24 16:13:08 -0500529/*
530 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800531 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500532 *
533 * If no bits are set on the state struct after clearing things, the
534 * struct is freed and removed from the tree
535 */
Li Zefancdc6a392012-03-12 16:39:48 +0800536static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
537 struct extent_state *state,
Qu Wenruofefdc552015-10-12 15:35:38 +0800538 unsigned *bits, int wake,
539 struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500540{
Li Zefancdc6a392012-03-12 16:39:48 +0800541 struct extent_state *next;
David Sterba9ee49a042015-01-14 19:52:13 +0100542 unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500543
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400544 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500545 u64 range = state->end - state->start + 1;
546 WARN_ON(range > tree->dirty_bytes);
547 tree->dirty_bytes -= range;
548 }
Chris Mason291d6732008-01-29 15:55:23 -0500549 clear_state_cb(tree, state, bits);
Qu Wenruofefdc552015-10-12 15:35:38 +0800550 add_extent_changeset(state, bits_to_clear, changeset, 0);
Josef Bacik32c00af2009-10-08 13:34:05 -0400551 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500552 if (wake)
553 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400554 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800555 next = next_state(state);
Filipe Manana27a35072014-07-06 20:09:59 +0100556 if (extent_state_in_tree(state)) {
Chris Masond1310b22008-01-24 16:13:08 -0500557 rb_erase(&state->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100558 RB_CLEAR_NODE(&state->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500559 free_extent_state(state);
560 } else {
561 WARN_ON(1);
562 }
563 } else {
564 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800565 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500566 }
Li Zefancdc6a392012-03-12 16:39:48 +0800567 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500568}
569
Xiao Guangrong82337672011-04-20 06:44:57 +0000570static struct extent_state *
571alloc_extent_state_atomic(struct extent_state *prealloc)
572{
573 if (!prealloc)
574 prealloc = alloc_extent_state(GFP_ATOMIC);
575
576 return prealloc;
577}
578
Eric Sandeen48a3b632013-04-25 20:41:01 +0000579static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400580{
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400581 btrfs_panic(tree_fs_info(tree), err,
582 "Locking error: Extent tree was modified by another thread while locked.");
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400583}
584
Chris Masond1310b22008-01-24 16:13:08 -0500585/*
586 * clear some bits on a range in the tree. This may require splitting
587 * or inserting elements in the tree, so the gfp mask is used to
588 * indicate which allocations or sleeping are allowed.
589 *
590 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
591 * the given range from the tree regardless of state (ie for truncate).
592 *
593 * the range [start, end] is inclusive.
594 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100595 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500596 */
Qu Wenruofefdc552015-10-12 15:35:38 +0800597static int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
598 unsigned bits, int wake, int delete,
599 struct extent_state **cached_state,
600 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500601{
602 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400603 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500604 struct extent_state *prealloc = NULL;
605 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400606 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500607 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000608 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500609
Josef Bacika5dee372013-12-13 10:02:44 -0500610 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000611
Josef Bacik7ee9e442013-06-21 16:37:03 -0400612 if (bits & EXTENT_DELALLOC)
613 bits |= EXTENT_NORESERVE;
614
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400615 if (delete)
616 bits |= ~EXTENT_CTLBITS;
617 bits |= EXTENT_FIRST_DELALLOC;
618
Josef Bacik2ac55d42010-02-03 19:33:23 +0000619 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
620 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500621again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800622 if (!prealloc && gfpflags_allow_blocking(mask)) {
Filipe Mananac7bc6312014-11-03 14:12:57 +0000623 /*
624 * Don't care for allocation failure here because we might end
625 * up not needing the pre-allocated extent state at all, which
626 * is the case if we only have in the tree extent states that
627 * cover our input range and don't cover too any other range.
628 * If we end up needing a new extent state we allocate it later.
629 */
Chris Masond1310b22008-01-24 16:13:08 -0500630 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500631 }
632
Chris Masoncad321a2008-12-17 14:51:42 -0500633 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400634 if (cached_state) {
635 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000636
637 if (clear) {
638 *cached_state = NULL;
639 cached_state = NULL;
640 }
641
Filipe Manana27a35072014-07-06 20:09:59 +0100642 if (cached && extent_state_in_tree(cached) &&
643 cached->start <= start && cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000644 if (clear)
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200645 refcount_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400646 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400647 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400648 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000649 if (clear)
650 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400651 }
Chris Masond1310b22008-01-24 16:13:08 -0500652 /*
653 * this search will find the extents that end after
654 * our range starts
655 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500656 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500657 if (!node)
658 goto out;
659 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400660hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500661 if (state->start > end)
662 goto out;
663 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400664 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500665
Liu Bo04493142012-02-16 18:34:37 +0800666 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800667 if (!(state->state & bits)) {
668 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800669 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800670 }
Liu Bo04493142012-02-16 18:34:37 +0800671
Chris Masond1310b22008-01-24 16:13:08 -0500672 /*
673 * | ---- desired range ---- |
674 * | state | or
675 * | ------------- state -------------- |
676 *
677 * We need to split the extent we found, and may flip
678 * bits on second half.
679 *
680 * If the extent we found extends past our range, we
681 * just split and search again. It'll get split again
682 * the next time though.
683 *
684 * If the extent we found is inside our range, we clear
685 * the desired bit on it.
686 */
687
688 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000689 prealloc = alloc_extent_state_atomic(prealloc);
690 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500691 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400692 if (err)
693 extent_io_tree_panic(tree, err);
694
Chris Masond1310b22008-01-24 16:13:08 -0500695 prealloc = NULL;
696 if (err)
697 goto out;
698 if (state->end <= end) {
Qu Wenruofefdc552015-10-12 15:35:38 +0800699 state = clear_state_bit(tree, state, &bits, wake,
700 changeset);
Liu Bod1ac6e42012-05-10 18:10:39 +0800701 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500702 }
703 goto search_again;
704 }
705 /*
706 * | ---- desired range ---- |
707 * | state |
708 * We need to split the extent, and clear the bit
709 * on the first half
710 */
711 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000712 prealloc = alloc_extent_state_atomic(prealloc);
713 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500714 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400715 if (err)
716 extent_io_tree_panic(tree, err);
717
Chris Masond1310b22008-01-24 16:13:08 -0500718 if (wake)
719 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400720
Qu Wenruofefdc552015-10-12 15:35:38 +0800721 clear_state_bit(tree, prealloc, &bits, wake, changeset);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400722
Chris Masond1310b22008-01-24 16:13:08 -0500723 prealloc = NULL;
724 goto out;
725 }
Chris Mason42daec22009-09-23 19:51:09 -0400726
Qu Wenruofefdc552015-10-12 15:35:38 +0800727 state = clear_state_bit(tree, state, &bits, wake, changeset);
Liu Bo04493142012-02-16 18:34:37 +0800728next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400729 if (last_end == (u64)-1)
730 goto out;
731 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800732 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800733 goto hit_next;
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);
Mel Gormand0164ad2015-11-06 16:28:21 -0800739 if (gfpflags_allow_blocking(mask))
Chris Masond1310b22008-01-24 16:13:08 -0500740 cond_resched();
741 goto again;
David Sterba7ab5cb22016-04-27 01:02:15 +0200742
743out:
744 spin_unlock(&tree->lock);
745 if (prealloc)
746 free_extent_state(prealloc);
747
748 return 0;
749
Chris Masond1310b22008-01-24 16:13:08 -0500750}
Chris Masond1310b22008-01-24 16:13:08 -0500751
Jeff Mahoney143bede2012-03-01 14:56:26 +0100752static void wait_on_state(struct extent_io_tree *tree,
753 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500754 __releases(tree->lock)
755 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500756{
757 DEFINE_WAIT(wait);
758 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500759 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500760 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500761 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500762 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500763}
764
765/*
766 * waits for one or more bits to clear on a range in the state tree.
767 * The range [start, end] is inclusive.
768 * The tree lock is taken by this function
769 */
David Sterba41074882013-04-29 13:38:46 +0000770static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
771 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500772{
773 struct extent_state *state;
774 struct rb_node *node;
775
Josef Bacika5dee372013-12-13 10:02:44 -0500776 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000777
Chris Masoncad321a2008-12-17 14:51:42 -0500778 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500779again:
780 while (1) {
781 /*
782 * this search will find all the extents that end after
783 * our range starts
784 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500785 node = tree_search(tree, start);
Filipe Mananac50d3e72014-03-31 14:53:25 +0100786process_node:
Chris Masond1310b22008-01-24 16:13:08 -0500787 if (!node)
788 break;
789
790 state = rb_entry(node, struct extent_state, rb_node);
791
792 if (state->start > end)
793 goto out;
794
795 if (state->state & bits) {
796 start = state->start;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200797 refcount_inc(&state->refs);
Chris Masond1310b22008-01-24 16:13:08 -0500798 wait_on_state(tree, state);
799 free_extent_state(state);
800 goto again;
801 }
802 start = state->end + 1;
803
804 if (start > end)
805 break;
806
Filipe Mananac50d3e72014-03-31 14:53:25 +0100807 if (!cond_resched_lock(&tree->lock)) {
808 node = rb_next(node);
809 goto process_node;
810 }
Chris Masond1310b22008-01-24 16:13:08 -0500811 }
812out:
Chris Masoncad321a2008-12-17 14:51:42 -0500813 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500814}
Chris Masond1310b22008-01-24 16:13:08 -0500815
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000816static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500817 struct extent_state *state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800818 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500819{
David Sterba9ee49a042015-01-14 19:52:13 +0100820 unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400821
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000822 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400823 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500824 u64 range = state->end - state->start + 1;
825 tree->dirty_bytes += range;
826 }
Qu Wenruod38ed272015-10-12 14:53:37 +0800827 add_extent_changeset(state, bits_to_set, changeset, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400828 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500829}
830
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100831static void cache_state_if_flags(struct extent_state *state,
832 struct extent_state **cached_ptr,
David Sterba9ee49a042015-01-14 19:52:13 +0100833 unsigned flags)
Chris Mason2c64c532009-09-02 15:04:12 -0400834{
835 if (cached_ptr && !(*cached_ptr)) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100836 if (!flags || (state->state & flags)) {
Chris Mason2c64c532009-09-02 15:04:12 -0400837 *cached_ptr = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200838 refcount_inc(&state->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400839 }
840 }
841}
842
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100843static void cache_state(struct extent_state *state,
844 struct extent_state **cached_ptr)
845{
846 return cache_state_if_flags(state, cached_ptr,
847 EXTENT_IOBITS | EXTENT_BOUNDARY);
848}
849
Chris Masond1310b22008-01-24 16:13:08 -0500850/*
Chris Mason1edbb732009-09-02 13:24:36 -0400851 * set some bits on a range in the tree. This may require allocations or
852 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500853 *
Chris Mason1edbb732009-09-02 13:24:36 -0400854 * If any of the exclusive bits are set, this will fail with -EEXIST if some
855 * part of the range already has the desired bits set. The start of the
856 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500857 *
Chris Mason1edbb732009-09-02 13:24:36 -0400858 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500859 */
Chris Mason1edbb732009-09-02 13:24:36 -0400860
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100861static int __must_check
862__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +0100863 unsigned bits, unsigned exclusive_bits,
David Sterba41074882013-04-29 13:38:46 +0000864 u64 *failed_start, struct extent_state **cached_state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800865 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500866{
867 struct extent_state *state;
868 struct extent_state *prealloc = NULL;
869 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000870 struct rb_node **p;
871 struct rb_node *parent;
Chris Masond1310b22008-01-24 16:13:08 -0500872 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500873 u64 last_start;
874 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400875
Josef Bacika5dee372013-12-13 10:02:44 -0500876 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000877
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400878 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500879again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800880 if (!prealloc && gfpflags_allow_blocking(mask)) {
David Sterba059f7912016-04-27 01:03:45 +0200881 /*
882 * Don't care for allocation failure here because we might end
883 * up not needing the pre-allocated extent state at all, which
884 * is the case if we only have in the tree extent states that
885 * cover our input range and don't cover too any other range.
886 * If we end up needing a new extent state we allocate it later.
887 */
Chris Masond1310b22008-01-24 16:13:08 -0500888 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500889 }
890
Chris Masoncad321a2008-12-17 14:51:42 -0500891 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400892 if (cached_state && *cached_state) {
893 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400894 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +0100895 extent_state_in_tree(state)) {
Chris Mason9655d292009-09-02 15:22:30 -0400896 node = &state->rb_node;
897 goto hit_next;
898 }
899 }
Chris Masond1310b22008-01-24 16:13:08 -0500900 /*
901 * this search will find all the extents that end after
902 * our range starts.
903 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000904 node = tree_search_for_insert(tree, start, &p, &parent);
Chris Masond1310b22008-01-24 16:13:08 -0500905 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000906 prealloc = alloc_extent_state_atomic(prealloc);
907 BUG_ON(!prealloc);
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000908 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800909 &p, &parent, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400910 if (err)
911 extent_io_tree_panic(tree, err);
912
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +0000913 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500914 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500915 goto out;
916 }
Chris Masond1310b22008-01-24 16:13:08 -0500917 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400918hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500919 last_start = state->start;
920 last_end = state->end;
921
922 /*
923 * | ---- desired range ---- |
924 * | state |
925 *
926 * Just lock what we found and keep going
927 */
928 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400929 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500930 *failed_start = state->start;
931 err = -EEXIST;
932 goto out;
933 }
Chris Mason42daec22009-09-23 19:51:09 -0400934
Qu Wenruod38ed272015-10-12 14:53:37 +0800935 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400936 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500937 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400938 if (last_end == (u64)-1)
939 goto out;
940 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800941 state = next_state(state);
942 if (start < end && state && state->start == start &&
943 !need_resched())
944 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500945 goto search_again;
946 }
947
948 /*
949 * | ---- desired range ---- |
950 * | state |
951 * or
952 * | ------------- state -------------- |
953 *
954 * We need to split the extent we found, and may flip bits on
955 * second half.
956 *
957 * If the extent we found extends past our
958 * range, we just split and search again. It'll get split
959 * again the next time though.
960 *
961 * If the extent we found is inside our range, we set the
962 * desired bit on it.
963 */
964 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400965 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500966 *failed_start = start;
967 err = -EEXIST;
968 goto out;
969 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000970
971 prealloc = alloc_extent_state_atomic(prealloc);
972 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500973 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400974 if (err)
975 extent_io_tree_panic(tree, err);
976
Chris Masond1310b22008-01-24 16:13:08 -0500977 prealloc = NULL;
978 if (err)
979 goto out;
980 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +0800981 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400982 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500983 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400984 if (last_end == (u64)-1)
985 goto out;
986 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800987 state = next_state(state);
988 if (start < end && state && state->start == start &&
989 !need_resched())
990 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500991 }
992 goto search_again;
993 }
994 /*
995 * | ---- desired range ---- |
996 * | state | or | state |
997 *
998 * There's a hole, we need to insert something in it and
999 * ignore the extent we found.
1000 */
1001 if (state->start > start) {
1002 u64 this_end;
1003 if (end < last_start)
1004 this_end = end;
1005 else
Chris Masond3977122009-01-05 21:25:51 -05001006 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +00001007
1008 prealloc = alloc_extent_state_atomic(prealloc);
1009 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +00001010
1011 /*
1012 * Avoid to free 'prealloc' if it can be merged with
1013 * the later extent.
1014 */
Chris Masond1310b22008-01-24 16:13:08 -05001015 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001016 NULL, NULL, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001017 if (err)
1018 extent_io_tree_panic(tree, err);
1019
Chris Mason2c64c532009-09-02 15:04:12 -04001020 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001021 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001022 start = this_end + 1;
1023 goto search_again;
1024 }
1025 /*
1026 * | ---- desired range ---- |
1027 * | state |
1028 * We need to split the extent, and set the bit
1029 * on the first half
1030 */
1031 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -04001032 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001033 *failed_start = start;
1034 err = -EEXIST;
1035 goto out;
1036 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001037
1038 prealloc = alloc_extent_state_atomic(prealloc);
1039 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001040 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001041 if (err)
1042 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -05001043
Qu Wenruod38ed272015-10-12 14:53:37 +08001044 set_state_bits(tree, prealloc, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001045 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001046 merge_state(tree, prealloc);
1047 prealloc = NULL;
1048 goto out;
1049 }
1050
David Sterbab5a4ba12016-04-27 01:02:15 +02001051search_again:
1052 if (start > end)
1053 goto out;
1054 spin_unlock(&tree->lock);
1055 if (gfpflags_allow_blocking(mask))
1056 cond_resched();
1057 goto again;
Chris Masond1310b22008-01-24 16:13:08 -05001058
1059out:
Chris Masoncad321a2008-12-17 14:51:42 -05001060 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001061 if (prealloc)
1062 free_extent_state(prealloc);
1063
1064 return err;
1065
Chris Masond1310b22008-01-24 16:13:08 -05001066}
Chris Masond1310b22008-01-24 16:13:08 -05001067
David Sterba41074882013-04-29 13:38:46 +00001068int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001069 unsigned bits, u64 * failed_start,
David Sterba41074882013-04-29 13:38:46 +00001070 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001071{
1072 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001073 cached_state, mask, NULL);
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001074}
1075
1076
Josef Bacik462d6fa2011-09-26 13:56:12 -04001077/**
Liu Bo10983f22012-07-11 15:26:19 +08001078 * convert_extent_bit - convert all bits in a given range from one bit to
1079 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -04001080 * @tree: the io tree to search
1081 * @start: the start offset in bytes
1082 * @end: the end offset in bytes (inclusive)
1083 * @bits: the bits to set in this range
1084 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001085 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001086 *
1087 * This will go through and set bits for the given range. If any states exist
1088 * already in this range they are set with the given bit and cleared of the
1089 * clear_bits. This is only meant to be used by things that are mergeable, ie
1090 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1091 * boundary bits like LOCK.
David Sterba210aa272016-04-26 23:54:39 +02001092 *
1093 * All allocations are done with GFP_NOFS.
Josef Bacik462d6fa2011-09-26 13:56:12 -04001094 */
1095int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001096 unsigned bits, unsigned clear_bits,
David Sterba210aa272016-04-26 23:54:39 +02001097 struct extent_state **cached_state)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001098{
1099 struct extent_state *state;
1100 struct extent_state *prealloc = NULL;
1101 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001102 struct rb_node **p;
1103 struct rb_node *parent;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001104 int err = 0;
1105 u64 last_start;
1106 u64 last_end;
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001107 bool first_iteration = true;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001108
Josef Bacika5dee372013-12-13 10:02:44 -05001109 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +00001110
Josef Bacik462d6fa2011-09-26 13:56:12 -04001111again:
David Sterba210aa272016-04-26 23:54:39 +02001112 if (!prealloc) {
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001113 /*
1114 * Best effort, don't worry if extent state allocation fails
1115 * here for the first iteration. We might have a cached state
1116 * that matches exactly the target range, in which case no
1117 * extent state allocations are needed. We'll only know this
1118 * after locking the tree.
1119 */
David Sterba210aa272016-04-26 23:54:39 +02001120 prealloc = alloc_extent_state(GFP_NOFS);
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001121 if (!prealloc && !first_iteration)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001122 return -ENOMEM;
1123 }
1124
1125 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001126 if (cached_state && *cached_state) {
1127 state = *cached_state;
1128 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +01001129 extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001130 node = &state->rb_node;
1131 goto hit_next;
1132 }
1133 }
1134
Josef Bacik462d6fa2011-09-26 13:56:12 -04001135 /*
1136 * this search will find all the extents that end after
1137 * our range starts.
1138 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001139 node = tree_search_for_insert(tree, start, &p, &parent);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001140 if (!node) {
1141 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001142 if (!prealloc) {
1143 err = -ENOMEM;
1144 goto out;
1145 }
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001146 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001147 &p, &parent, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001148 if (err)
1149 extent_io_tree_panic(tree, err);
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001150 cache_state(prealloc, cached_state);
1151 prealloc = NULL;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001152 goto out;
1153 }
1154 state = rb_entry(node, struct extent_state, rb_node);
1155hit_next:
1156 last_start = state->start;
1157 last_end = state->end;
1158
1159 /*
1160 * | ---- desired range ---- |
1161 * | state |
1162 *
1163 * Just lock what we found and keep going
1164 */
1165 if (state->start == start && state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001166 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001167 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001168 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001169 if (last_end == (u64)-1)
1170 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001171 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001172 if (start < end && state && state->start == start &&
1173 !need_resched())
1174 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001175 goto search_again;
1176 }
1177
1178 /*
1179 * | ---- desired range ---- |
1180 * | state |
1181 * or
1182 * | ------------- state -------------- |
1183 *
1184 * We need to split the extent we found, and may flip bits on
1185 * second half.
1186 *
1187 * If the extent we found extends past our
1188 * range, we just split and search again. It'll get split
1189 * again the next time though.
1190 *
1191 * If the extent we found is inside our range, we set the
1192 * desired bit on it.
1193 */
1194 if (state->start < start) {
1195 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001196 if (!prealloc) {
1197 err = -ENOMEM;
1198 goto out;
1199 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001200 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001201 if (err)
1202 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001203 prealloc = NULL;
1204 if (err)
1205 goto out;
1206 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001207 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001208 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001209 state = clear_state_bit(tree, state, &clear_bits, 0,
1210 NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001211 if (last_end == (u64)-1)
1212 goto out;
1213 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001214 if (start < end && state && state->start == start &&
1215 !need_resched())
1216 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001217 }
1218 goto search_again;
1219 }
1220 /*
1221 * | ---- desired range ---- |
1222 * | state | or | state |
1223 *
1224 * There's a hole, we need to insert something in it and
1225 * ignore the extent we found.
1226 */
1227 if (state->start > start) {
1228 u64 this_end;
1229 if (end < last_start)
1230 this_end = end;
1231 else
1232 this_end = last_start - 1;
1233
1234 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001235 if (!prealloc) {
1236 err = -ENOMEM;
1237 goto out;
1238 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001239
1240 /*
1241 * Avoid to free 'prealloc' if it can be merged with
1242 * the later extent.
1243 */
1244 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001245 NULL, NULL, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001246 if (err)
1247 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001248 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001249 prealloc = NULL;
1250 start = this_end + 1;
1251 goto search_again;
1252 }
1253 /*
1254 * | ---- desired range ---- |
1255 * | state |
1256 * We need to split the extent, and set the bit
1257 * on the first half
1258 */
1259 if (state->start <= end && state->end > end) {
1260 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001261 if (!prealloc) {
1262 err = -ENOMEM;
1263 goto out;
1264 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001265
1266 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001267 if (err)
1268 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001269
Qu Wenruod38ed272015-10-12 14:53:37 +08001270 set_state_bits(tree, prealloc, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001271 cache_state(prealloc, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001272 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001273 prealloc = NULL;
1274 goto out;
1275 }
1276
Josef Bacik462d6fa2011-09-26 13:56:12 -04001277search_again:
1278 if (start > end)
1279 goto out;
1280 spin_unlock(&tree->lock);
David Sterba210aa272016-04-26 23:54:39 +02001281 cond_resched();
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001282 first_iteration = false;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001283 goto again;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001284
1285out:
1286 spin_unlock(&tree->lock);
1287 if (prealloc)
1288 free_extent_state(prealloc);
1289
1290 return err;
1291}
1292
Chris Masond1310b22008-01-24 16:13:08 -05001293/* wrappers around set/clear extent bit */
Qu Wenruod38ed272015-10-12 14:53:37 +08001294int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba2c53b912016-04-26 23:54:39 +02001295 unsigned bits, struct extent_changeset *changeset)
Qu Wenruod38ed272015-10-12 14:53:37 +08001296{
1297 /*
1298 * We don't support EXTENT_LOCKED yet, as current changeset will
1299 * record any bits changed, so for EXTENT_LOCKED case, it will
1300 * either fail with -EEXIST or changeset will record the whole
1301 * range.
1302 */
1303 BUG_ON(bits & EXTENT_LOCKED);
1304
David Sterba2c53b912016-04-26 23:54:39 +02001305 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
Qu Wenruod38ed272015-10-12 14:53:37 +08001306 changeset);
1307}
1308
Qu Wenruofefdc552015-10-12 15:35:38 +08001309int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1310 unsigned bits, int wake, int delete,
1311 struct extent_state **cached, gfp_t mask)
1312{
1313 return __clear_extent_bit(tree, start, end, bits, wake, delete,
1314 cached, mask, NULL);
1315}
1316
Qu Wenruofefdc552015-10-12 15:35:38 +08001317int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaf734c442016-04-26 23:54:39 +02001318 unsigned bits, struct extent_changeset *changeset)
Qu Wenruofefdc552015-10-12 15:35:38 +08001319{
1320 /*
1321 * Don't support EXTENT_LOCKED case, same reason as
1322 * set_record_extent_bits().
1323 */
1324 BUG_ON(bits & EXTENT_LOCKED);
1325
David Sterbaf734c442016-04-26 23:54:39 +02001326 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
Qu Wenruofefdc552015-10-12 15:35:38 +08001327 changeset);
1328}
1329
Chris Masond352ac62008-09-29 15:18:18 -04001330/*
1331 * either insert or lock state struct between start and end use mask to tell
1332 * us if waiting is desired.
1333 */
Chris Mason1edbb732009-09-02 13:24:36 -04001334int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaff13db42015-12-03 14:30:40 +01001335 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001336{
1337 int err;
1338 u64 failed_start;
David Sterba9ee49a042015-01-14 19:52:13 +01001339
Chris Masond1310b22008-01-24 16:13:08 -05001340 while (1) {
David Sterbaff13db42015-12-03 14:30:40 +01001341 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001342 EXTENT_LOCKED, &failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001343 cached_state, GFP_NOFS, NULL);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001344 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001345 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1346 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001347 } else
Chris Masond1310b22008-01-24 16:13:08 -05001348 break;
Chris Masond1310b22008-01-24 16:13:08 -05001349 WARN_ON(start > end);
1350 }
1351 return err;
1352}
Chris Masond1310b22008-01-24 16:13:08 -05001353
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001354int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001355{
1356 int err;
1357 u64 failed_start;
1358
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001359 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
Qu Wenruod38ed272015-10-12 14:53:37 +08001360 &failed_start, NULL, GFP_NOFS, NULL);
Yan Zheng66435582008-10-30 14:19:50 -04001361 if (err == -EEXIST) {
1362 if (failed_start > start)
1363 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001364 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001365 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001366 }
Josef Bacik25179202008-10-29 14:49:05 -04001367 return 1;
1368}
Josef Bacik25179202008-10-29 14:49:05 -04001369
David Sterbabd1fa4f2015-12-03 13:08:59 +01001370void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001371{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001372 unsigned long index = start >> PAGE_SHIFT;
1373 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001374 struct page *page;
1375
1376 while (index <= end_index) {
1377 page = find_get_page(inode->i_mapping, index);
1378 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1379 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001380 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001381 index++;
1382 }
Chris Mason4adaa612013-03-26 13:07:00 -04001383}
1384
David Sterbaf6311572015-12-03 13:08:59 +01001385void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001386{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001387 unsigned long index = start >> PAGE_SHIFT;
1388 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001389 struct page *page;
1390
1391 while (index <= end_index) {
1392 page = find_get_page(inode->i_mapping, index);
1393 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Mason4adaa612013-03-26 13:07:00 -04001394 __set_page_dirty_nobuffers(page);
Konstantin Khebnikov8d386332015-02-11 15:26:55 -08001395 account_page_redirty(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001396 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001397 index++;
1398 }
Chris Mason4adaa612013-03-26 13:07:00 -04001399}
1400
Chris Masond1310b22008-01-24 16:13:08 -05001401/*
Chris Masond1310b22008-01-24 16:13:08 -05001402 * helper function to set both pages and extents in the tree writeback
1403 */
David Sterba35de6db2015-12-03 13:08:59 +01001404static void set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001405{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001406 unsigned long index = start >> PAGE_SHIFT;
1407 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05001408 struct page *page;
1409
1410 while (index <= end_index) {
1411 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001412 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001413 set_page_writeback(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001414 put_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05001415 index++;
1416 }
Chris Masond1310b22008-01-24 16:13:08 -05001417}
Chris Masond1310b22008-01-24 16:13:08 -05001418
Chris Masond352ac62008-09-29 15:18:18 -04001419/* find the first state struct with 'bits' set after 'start', and
1420 * return it. tree->lock must be held. NULL will returned if
1421 * nothing was found after 'start'
1422 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001423static struct extent_state *
1424find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +01001425 u64 start, unsigned bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001426{
1427 struct rb_node *node;
1428 struct extent_state *state;
1429
1430 /*
1431 * this search will find all the extents that end after
1432 * our range starts.
1433 */
1434 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001435 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001436 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001437
Chris Masond3977122009-01-05 21:25:51 -05001438 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001439 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001440 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001441 return state;
Chris Masond3977122009-01-05 21:25:51 -05001442
Chris Masond7fc6402008-02-18 12:12:38 -05001443 node = rb_next(node);
1444 if (!node)
1445 break;
1446 }
1447out:
1448 return NULL;
1449}
Chris Masond7fc6402008-02-18 12:12:38 -05001450
Chris Masond352ac62008-09-29 15:18:18 -04001451/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001452 * find the first offset in the io tree with 'bits' set. zero is
1453 * returned if we find something, and *start_ret and *end_ret are
1454 * set to reflect the state struct that was found.
1455 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001456 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001457 */
1458int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba9ee49a042015-01-14 19:52:13 +01001459 u64 *start_ret, u64 *end_ret, unsigned bits,
Josef Bacike6138872012-09-27 17:07:30 -04001460 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001461{
1462 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001463 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001464 int ret = 1;
1465
1466 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001467 if (cached_state && *cached_state) {
1468 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001469 if (state->end == start - 1 && extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001470 n = rb_next(&state->rb_node);
1471 while (n) {
1472 state = rb_entry(n, struct extent_state,
1473 rb_node);
1474 if (state->state & bits)
1475 goto got_it;
1476 n = rb_next(n);
1477 }
1478 free_extent_state(*cached_state);
1479 *cached_state = NULL;
1480 goto out;
1481 }
1482 free_extent_state(*cached_state);
1483 *cached_state = NULL;
1484 }
1485
Xiao Guangrong69261c42011-07-14 03:19:45 +00001486 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001487got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001488 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001489 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001490 *start_ret = state->start;
1491 *end_ret = state->end;
1492 ret = 0;
1493 }
Josef Bacike6138872012-09-27 17:07:30 -04001494out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001495 spin_unlock(&tree->lock);
1496 return ret;
1497}
1498
1499/*
Chris Masond352ac62008-09-29 15:18:18 -04001500 * find a contiguous range of bytes in the file marked as delalloc, not
1501 * more than 'max_bytes'. start and end are used to return the range,
1502 *
1503 * 1 is returned if we find something, 0 if nothing was in the tree
1504 */
Chris Masonc8b97812008-10-29 14:49:59 -04001505static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001506 u64 *start, u64 *end, u64 max_bytes,
1507 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001508{
1509 struct rb_node *node;
1510 struct extent_state *state;
1511 u64 cur_start = *start;
1512 u64 found = 0;
1513 u64 total_bytes = 0;
1514
Chris Masoncad321a2008-12-17 14:51:42 -05001515 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001516
Chris Masond1310b22008-01-24 16:13:08 -05001517 /*
1518 * this search will find all the extents that end after
1519 * our range starts.
1520 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001521 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001522 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001523 if (!found)
1524 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001525 goto out;
1526 }
1527
Chris Masond3977122009-01-05 21:25:51 -05001528 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001529 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001530 if (found && (state->start != cur_start ||
1531 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001532 goto out;
1533 }
1534 if (!(state->state & EXTENT_DELALLOC)) {
1535 if (!found)
1536 *end = state->end;
1537 goto out;
1538 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001539 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001540 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001541 *cached_state = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +02001542 refcount_inc(&state->refs);
Josef Bacikc2a128d2010-02-02 21:19:11 +00001543 }
Chris Masond1310b22008-01-24 16:13:08 -05001544 found++;
1545 *end = state->end;
1546 cur_start = state->end + 1;
1547 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001548 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001549 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001550 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001551 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001552 break;
1553 }
1554out:
Chris Masoncad321a2008-12-17 14:51:42 -05001555 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001556 return found;
1557}
1558
Liu Boda2c7002017-02-10 16:41:05 +01001559static int __process_pages_contig(struct address_space *mapping,
1560 struct page *locked_page,
1561 pgoff_t start_index, pgoff_t end_index,
1562 unsigned long page_ops, pgoff_t *index_ret);
1563
Jeff Mahoney143bede2012-03-01 14:56:26 +01001564static noinline void __unlock_for_delalloc(struct inode *inode,
1565 struct page *locked_page,
1566 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001567{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001568 unsigned long index = start >> PAGE_SHIFT;
1569 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001570
Liu Bo76c00212017-02-10 16:42:14 +01001571 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001572 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001573 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001574
Liu Bo76c00212017-02-10 16:42:14 +01001575 __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1576 PAGE_UNLOCK, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -04001577}
1578
1579static noinline int lock_delalloc_pages(struct inode *inode,
1580 struct page *locked_page,
1581 u64 delalloc_start,
1582 u64 delalloc_end)
1583{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001584 unsigned long index = delalloc_start >> PAGE_SHIFT;
Liu Bo76c00212017-02-10 16:42:14 +01001585 unsigned long index_ret = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001586 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001587 int ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001588
Liu Bo76c00212017-02-10 16:42:14 +01001589 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001590 if (index == locked_page->index && index == end_index)
1591 return 0;
1592
Liu Bo76c00212017-02-10 16:42:14 +01001593 ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1594 end_index, PAGE_LOCK, &index_ret);
1595 if (ret == -EAGAIN)
1596 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1597 (u64)index_ret << PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001598 return ret;
1599}
1600
1601/*
1602 * find a contiguous range of bytes in the file marked as delalloc, not
1603 * more than 'max_bytes'. start and end are used to return the range,
1604 *
1605 * 1 is returned if we find something, 0 if nothing was in the tree
1606 */
Josef Bacik294e30f2013-10-09 12:00:56 -04001607STATIC u64 find_lock_delalloc_range(struct inode *inode,
1608 struct extent_io_tree *tree,
1609 struct page *locked_page, u64 *start,
1610 u64 *end, u64 max_bytes)
Chris Masonc8b97812008-10-29 14:49:59 -04001611{
1612 u64 delalloc_start;
1613 u64 delalloc_end;
1614 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001615 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001616 int ret;
1617 int loops = 0;
1618
1619again:
1620 /* step one, find a bunch of delalloc bytes starting at start */
1621 delalloc_start = *start;
1622 delalloc_end = 0;
1623 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001624 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001625 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001626 *start = delalloc_start;
1627 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001628 free_extent_state(cached_state);
Liu Bo385fe0b2013-10-01 23:49:49 +08001629 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001630 }
1631
1632 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001633 * start comes from the offset of locked_page. We have to lock
1634 * pages in order, so we can't process delalloc bytes before
1635 * locked_page
1636 */
Chris Masond3977122009-01-05 21:25:51 -05001637 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001638 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001639
1640 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001641 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001642 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001643 if (delalloc_end + 1 - delalloc_start > max_bytes)
1644 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001645
Chris Masonc8b97812008-10-29 14:49:59 -04001646 /* step two, lock all the pages after the page that has start */
1647 ret = lock_delalloc_pages(inode, locked_page,
1648 delalloc_start, delalloc_end);
1649 if (ret == -EAGAIN) {
1650 /* some of the pages are gone, lets avoid looping by
1651 * shortening the size of the delalloc range we're searching
1652 */
Chris Mason9655d292009-09-02 15:22:30 -04001653 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001654 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001655 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001656 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001657 loops = 1;
1658 goto again;
1659 } else {
1660 found = 0;
1661 goto out_failed;
1662 }
1663 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001664 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001665
1666 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001667 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001668
1669 /* then test to make sure it is all still delalloc */
1670 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001671 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001672 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001673 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1674 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001675 __unlock_for_delalloc(inode, locked_page,
1676 delalloc_start, delalloc_end);
1677 cond_resched();
1678 goto again;
1679 }
Chris Mason9655d292009-09-02 15:22:30 -04001680 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001681 *start = delalloc_start;
1682 *end = delalloc_end;
1683out_failed:
1684 return found;
1685}
1686
Liu Boda2c7002017-02-10 16:41:05 +01001687static int __process_pages_contig(struct address_space *mapping,
1688 struct page *locked_page,
1689 pgoff_t start_index, pgoff_t end_index,
1690 unsigned long page_ops, pgoff_t *index_ret)
Chris Masonc8b97812008-10-29 14:49:59 -04001691{
Liu Bo873695b2017-02-02 17:49:22 -08001692 unsigned long nr_pages = end_index - start_index + 1;
Liu Boda2c7002017-02-10 16:41:05 +01001693 unsigned long pages_locked = 0;
Liu Bo873695b2017-02-02 17:49:22 -08001694 pgoff_t index = start_index;
Chris Masonc8b97812008-10-29 14:49:59 -04001695 struct page *pages[16];
Liu Bo873695b2017-02-02 17:49:22 -08001696 unsigned ret;
Liu Boda2c7002017-02-10 16:41:05 +01001697 int err = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001698 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001699
Liu Boda2c7002017-02-10 16:41:05 +01001700 if (page_ops & PAGE_LOCK) {
1701 ASSERT(page_ops == PAGE_LOCK);
1702 ASSERT(index_ret && *index_ret == start_index);
1703 }
1704
Filipe Manana704de492014-10-06 22:14:22 +01001705 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
Liu Bo873695b2017-02-02 17:49:22 -08001706 mapping_set_error(mapping, -EIO);
Filipe Manana704de492014-10-06 22:14:22 +01001707
Chris Masond3977122009-01-05 21:25:51 -05001708 while (nr_pages > 0) {
Liu Bo873695b2017-02-02 17:49:22 -08001709 ret = find_get_pages_contig(mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001710 min_t(unsigned long,
1711 nr_pages, ARRAY_SIZE(pages)), pages);
Liu Boda2c7002017-02-10 16:41:05 +01001712 if (ret == 0) {
1713 /*
1714 * Only if we're going to lock these pages,
1715 * can we find nothing at @index.
1716 */
1717 ASSERT(page_ops & PAGE_LOCK);
Liu Bo49d4a332017-03-06 18:20:56 -08001718 err = -EAGAIN;
1719 goto out;
Liu Boda2c7002017-02-10 16:41:05 +01001720 }
Chris Mason8b62b722009-09-02 16:53:46 -04001721
Liu Boda2c7002017-02-10 16:41:05 +01001722 for (i = 0; i < ret; i++) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001723 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001724 SetPagePrivate2(pages[i]);
1725
Chris Masonc8b97812008-10-29 14:49:59 -04001726 if (pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001727 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001728 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001729 continue;
1730 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001731 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001732 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001733 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001734 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001735 if (page_ops & PAGE_SET_ERROR)
1736 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001737 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001738 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001739 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001740 unlock_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001741 if (page_ops & PAGE_LOCK) {
1742 lock_page(pages[i]);
1743 if (!PageDirty(pages[i]) ||
1744 pages[i]->mapping != mapping) {
1745 unlock_page(pages[i]);
1746 put_page(pages[i]);
1747 err = -EAGAIN;
1748 goto out;
1749 }
1750 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001751 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001752 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001753 }
1754 nr_pages -= ret;
1755 index += ret;
1756 cond_resched();
1757 }
Liu Boda2c7002017-02-10 16:41:05 +01001758out:
1759 if (err && index_ret)
1760 *index_ret = start_index + pages_locked - 1;
1761 return err;
Chris Masonc8b97812008-10-29 14:49:59 -04001762}
Chris Masonc8b97812008-10-29 14:49:59 -04001763
Liu Bo873695b2017-02-02 17:49:22 -08001764void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1765 u64 delalloc_end, struct page *locked_page,
1766 unsigned clear_bits,
1767 unsigned long page_ops)
1768{
1769 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
1770 NULL, GFP_NOFS);
1771
1772 __process_pages_contig(inode->i_mapping, locked_page,
1773 start >> PAGE_SHIFT, end >> PAGE_SHIFT,
Liu Boda2c7002017-02-10 16:41:05 +01001774 page_ops, NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001775}
1776
Chris Masond352ac62008-09-29 15:18:18 -04001777/*
1778 * count the number of bytes in the tree that have a given bit(s)
1779 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1780 * cached. The total number found is returned.
1781 */
Chris Masond1310b22008-01-24 16:13:08 -05001782u64 count_range_bits(struct extent_io_tree *tree,
1783 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001784 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001785{
1786 struct rb_node *node;
1787 struct extent_state *state;
1788 u64 cur_start = *start;
1789 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001790 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001791 int found = 0;
1792
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301793 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001794 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001795
Chris Masoncad321a2008-12-17 14:51:42 -05001796 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001797 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1798 total_bytes = tree->dirty_bytes;
1799 goto out;
1800 }
1801 /*
1802 * this search will find all the extents that end after
1803 * our range starts.
1804 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001805 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001806 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001807 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001808
Chris Masond3977122009-01-05 21:25:51 -05001809 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001810 state = rb_entry(node, struct extent_state, rb_node);
1811 if (state->start > search_end)
1812 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001813 if (contig && found && state->start > last + 1)
1814 break;
1815 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001816 total_bytes += min(search_end, state->end) + 1 -
1817 max(cur_start, state->start);
1818 if (total_bytes >= max_bytes)
1819 break;
1820 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001821 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001822 found = 1;
1823 }
Chris Masonec29ed52011-02-23 16:23:20 -05001824 last = state->end;
1825 } else if (contig && found) {
1826 break;
Chris Masond1310b22008-01-24 16:13:08 -05001827 }
1828 node = rb_next(node);
1829 if (!node)
1830 break;
1831 }
1832out:
Chris Masoncad321a2008-12-17 14:51:42 -05001833 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001834 return total_bytes;
1835}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001836
Chris Masond352ac62008-09-29 15:18:18 -04001837/*
1838 * set the private field for a given byte offset in the tree. If there isn't
1839 * an extent_state there already, this does nothing.
1840 */
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001841static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001842 struct io_failure_record *failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001843{
1844 struct rb_node *node;
1845 struct extent_state *state;
1846 int ret = 0;
1847
Chris Masoncad321a2008-12-17 14:51:42 -05001848 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001849 /*
1850 * this search will find all the extents that end after
1851 * our range starts.
1852 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001853 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001854 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001855 ret = -ENOENT;
1856 goto out;
1857 }
1858 state = rb_entry(node, struct extent_state, rb_node);
1859 if (state->start != start) {
1860 ret = -ENOENT;
1861 goto out;
1862 }
David Sterba47dc1962016-02-11 13:24:13 +01001863 state->failrec = failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001864out:
Chris Masoncad321a2008-12-17 14:51:42 -05001865 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001866 return ret;
1867}
1868
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001869static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001870 struct io_failure_record **failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001871{
1872 struct rb_node *node;
1873 struct extent_state *state;
1874 int ret = 0;
1875
Chris Masoncad321a2008-12-17 14:51:42 -05001876 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001877 /*
1878 * this search will find all the extents that end after
1879 * our range starts.
1880 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001881 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001882 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001883 ret = -ENOENT;
1884 goto out;
1885 }
1886 state = rb_entry(node, struct extent_state, rb_node);
1887 if (state->start != start) {
1888 ret = -ENOENT;
1889 goto out;
1890 }
David Sterba47dc1962016-02-11 13:24:13 +01001891 *failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001892out:
Chris Masoncad321a2008-12-17 14:51:42 -05001893 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001894 return ret;
1895}
1896
1897/*
1898 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001899 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001900 * has the bits set. Otherwise, 1 is returned if any bit in the
1901 * range is found set.
1902 */
1903int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001904 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001905{
1906 struct extent_state *state = NULL;
1907 struct rb_node *node;
1908 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001909
Chris Masoncad321a2008-12-17 14:51:42 -05001910 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01001911 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001912 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001913 node = &cached->rb_node;
1914 else
1915 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001916 while (node && start <= end) {
1917 state = rb_entry(node, struct extent_state, rb_node);
1918
1919 if (filled && state->start > start) {
1920 bitset = 0;
1921 break;
1922 }
1923
1924 if (state->start > end)
1925 break;
1926
1927 if (state->state & bits) {
1928 bitset = 1;
1929 if (!filled)
1930 break;
1931 } else if (filled) {
1932 bitset = 0;
1933 break;
1934 }
Chris Mason46562ce2009-09-23 20:23:16 -04001935
1936 if (state->end == (u64)-1)
1937 break;
1938
Chris Masond1310b22008-01-24 16:13:08 -05001939 start = state->end + 1;
1940 if (start > end)
1941 break;
1942 node = rb_next(node);
1943 if (!node) {
1944 if (filled)
1945 bitset = 0;
1946 break;
1947 }
1948 }
Chris Masoncad321a2008-12-17 14:51:42 -05001949 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001950 return bitset;
1951}
Chris Masond1310b22008-01-24 16:13:08 -05001952
1953/*
1954 * helper function to set a given page up to date if all the
1955 * extents in the tree for that page are up to date
1956 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001957static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001958{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001959 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001960 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001961 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001962 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001963}
1964
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02001965int free_io_failure(struct btrfs_inode *inode, struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001966{
1967 int ret;
1968 int err = 0;
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02001969 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001970
David Sterba47dc1962016-02-11 13:24:13 +01001971 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001972 ret = clear_extent_bits(failure_tree, rec->start,
1973 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001974 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001975 if (ret)
1976 err = ret;
1977
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02001978 ret = clear_extent_bits(&inode->io_tree, rec->start,
David Woodhouse53b381b2013-01-29 18:40:14 -05001979 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001980 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05001981 if (ret && !err)
1982 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001983
1984 kfree(rec);
1985 return err;
1986}
1987
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001988/*
1989 * this bypasses the standard btrfs submit functions deliberately, as
1990 * the standard behavior is to write all copies in a raid setup. here we only
1991 * want to write the one bad copy. so we do the mapping for ourselves and issue
1992 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001993 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001994 * actually prevents the read that triggered the error from finishing.
1995 * currently, there can be no more than two copies of every data bit. thus,
1996 * exactly one rewrite is required.
1997 */
Nikolay Borisov9d4f7f82017-02-20 13:50:55 +02001998int repair_io_failure(struct btrfs_inode *inode, u64 start, u64 length,
1999 u64 logical, struct page *page,
2000 unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002001{
Nikolay Borisov9d4f7f82017-02-20 13:50:55 +02002002 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002003 struct bio *bio;
2004 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002005 u64 map_length = 0;
2006 u64 sector;
2007 struct btrfs_bio *bbio = NULL;
2008 int ret;
2009
Ilya Dryomov908960c2013-11-03 19:06:39 +02002010 ASSERT(!(fs_info->sb->s_flags & MS_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002011 BUG_ON(!mirror_num);
2012
Chris Mason9be33952013-05-17 18:30:14 -04002013 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002014 if (!bio)
2015 return -EIO;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002016 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002017 map_length = length;
2018
Filipe Mananab5de8d02016-05-27 22:21:27 +01002019 /*
2020 * Avoid races with device replace and make sure our bbio has devices
2021 * associated to its stripes that don't go away while we are doing the
2022 * read repair operation.
2023 */
2024 btrfs_bio_counter_inc_blocked(fs_info);
Liu Boc7253282017-03-29 10:53:58 -07002025 if (btrfs_is_parity_mirror(fs_info, logical, length, mirror_num)) {
2026 /*
2027 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
2028 * to update all raid stripes, but here we just want to correct
2029 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2030 * stripe's dev and sector.
2031 */
2032 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2033 &map_length, &bbio, 0);
2034 if (ret) {
2035 btrfs_bio_counter_dec(fs_info);
2036 bio_put(bio);
2037 return -EIO;
2038 }
2039 ASSERT(bbio->mirror_num == 1);
2040 } else {
2041 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2042 &map_length, &bbio, mirror_num);
2043 if (ret) {
2044 btrfs_bio_counter_dec(fs_info);
2045 bio_put(bio);
2046 return -EIO;
2047 }
2048 BUG_ON(mirror_num != bbio->mirror_num);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002049 }
Liu Boc7253282017-03-29 10:53:58 -07002050
2051 sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002052 bio->bi_iter.bi_sector = sector;
Liu Boc7253282017-03-29 10:53:58 -07002053 dev = bbio->stripes[bbio->mirror_num - 1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002054 btrfs_put_bbio(bbio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002055 if (!dev || !dev->bdev || !dev->writeable) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002056 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002057 bio_put(bio);
2058 return -EIO;
2059 }
2060 bio->bi_bdev = dev->bdev;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002061 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Miao Xieffdd2012014-09-12 18:44:00 +08002062 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002063
Mike Christie4e49ea42016-06-05 14:31:41 -05002064 if (btrfsic_submit_bio_wait(bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002065 /* try to remap that extent elsewhere? */
Filipe Mananab5de8d02016-05-27 22:21:27 +01002066 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002067 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002068 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002069 return -EIO;
2070 }
2071
David Sterbab14af3b2015-10-08 10:43:10 +02002072 btrfs_info_rl_in_rcu(fs_info,
2073 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Nikolay Borisov9d4f7f82017-02-20 13:50:55 +02002074 btrfs_ino(inode), start,
Miao Xie1203b682014-09-12 18:44:01 +08002075 rcu_str_deref(dev->name), sector);
Filipe Mananab5de8d02016-05-27 22:21:27 +01002076 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002077 bio_put(bio);
2078 return 0;
2079}
2080
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002081int repair_eb_io_failure(struct btrfs_fs_info *fs_info,
2082 struct extent_buffer *eb, int mirror_num)
Josef Bacikea466792012-03-26 21:57:36 -04002083{
Josef Bacikea466792012-03-26 21:57:36 -04002084 u64 start = eb->start;
2085 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04002086 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002087
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002088 if (fs_info->sb->s_flags & MS_RDONLY)
Ilya Dryomov908960c2013-11-03 19:06:39 +02002089 return -EROFS;
2090
Josef Bacikea466792012-03-26 21:57:36 -04002091 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002092 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002093
Nikolay Borisov9d4f7f82017-02-20 13:50:55 +02002094 ret = repair_io_failure(BTRFS_I(fs_info->btree_inode), start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002095 PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002096 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002097 if (ret)
2098 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002099 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002100 }
2101
2102 return ret;
2103}
2104
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002105/*
2106 * each time an IO finishes, we do a fast check in the IO failure tree
2107 * to see if we need to process or clean up an io_failure_record
2108 */
Nikolay Borisovb30cb442017-02-20 13:50:56 +02002109int clean_io_failure(struct btrfs_inode *inode, u64 start, struct page *page,
Miao Xie8b110e32014-09-12 18:44:03 +08002110 unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002111{
2112 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002113 struct io_failure_record *failrec;
Nikolay Borisovb30cb442017-02-20 13:50:56 +02002114 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002115 struct extent_state *state;
2116 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002117 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002118
2119 private = 0;
Nikolay Borisovb30cb442017-02-20 13:50:56 +02002120 ret = count_range_bits(&inode->io_failure_tree, &private,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002121 (u64)-1, 1, EXTENT_DIRTY, 0);
2122 if (!ret)
2123 return 0;
2124
Nikolay Borisovb30cb442017-02-20 13:50:56 +02002125 ret = get_state_failrec(&inode->io_failure_tree, start,
David Sterba47dc1962016-02-11 13:24:13 +01002126 &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002127 if (ret)
2128 return 0;
2129
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002130 BUG_ON(!failrec->this_mirror);
2131
2132 if (failrec->in_validation) {
2133 /* there was no real error, just free the record */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002134 btrfs_debug(fs_info,
2135 "clean_io_failure: freeing dummy error at %llu",
2136 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002137 goto out;
2138 }
Ilya Dryomov908960c2013-11-03 19:06:39 +02002139 if (fs_info->sb->s_flags & MS_RDONLY)
2140 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002141
Nikolay Borisovb30cb442017-02-20 13:50:56 +02002142 spin_lock(&inode->io_tree.lock);
2143 state = find_first_extent_bit_state(&inode->io_tree,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002144 failrec->start,
2145 EXTENT_LOCKED);
Nikolay Borisovb30cb442017-02-20 13:50:56 +02002146 spin_unlock(&inode->io_tree.lock);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002147
Miao Xie883d0de2013-07-25 19:22:35 +08002148 if (state && state->start <= failrec->start &&
2149 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002150 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2151 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002152 if (num_copies > 1) {
Nikolay Borisovb30cb442017-02-20 13:50:56 +02002153 repair_io_failure(inode, start, failrec->len,
Miao Xie454ff3d2014-09-12 18:43:58 +08002154 failrec->logical, page,
Miao Xie1203b682014-09-12 18:44:01 +08002155 pg_offset, failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002156 }
2157 }
2158
2159out:
Nikolay Borisovb30cb442017-02-20 13:50:56 +02002160 free_io_failure(inode, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002161
Miao Xie454ff3d2014-09-12 18:43:58 +08002162 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002163}
2164
Miao Xief6124962014-09-12 18:44:04 +08002165/*
2166 * Can be called when
2167 * - hold extent lock
2168 * - under ordered extent
2169 * - the inode is freeing
2170 */
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002171void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
Miao Xief6124962014-09-12 18:44:04 +08002172{
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002173 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
Miao Xief6124962014-09-12 18:44:04 +08002174 struct io_failure_record *failrec;
2175 struct extent_state *state, *next;
2176
2177 if (RB_EMPTY_ROOT(&failure_tree->state))
2178 return;
2179
2180 spin_lock(&failure_tree->lock);
2181 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2182 while (state) {
2183 if (state->start > end)
2184 break;
2185
2186 ASSERT(state->end <= end);
2187
2188 next = next_state(state);
2189
David Sterba47dc1962016-02-11 13:24:13 +01002190 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002191 free_extent_state(state);
2192 kfree(failrec);
2193
2194 state = next;
2195 }
2196 spin_unlock(&failure_tree->lock);
2197}
2198
Miao Xie2fe63032014-09-12 18:43:59 +08002199int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
David Sterba47dc1962016-02-11 13:24:13 +01002200 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002201{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002202 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002203 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002204 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002205 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2206 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2207 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002208 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002209 u64 logical;
2210
David Sterba47dc1962016-02-11 13:24:13 +01002211 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002212 if (ret) {
2213 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2214 if (!failrec)
2215 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002216
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002217 failrec->start = start;
2218 failrec->len = end - start + 1;
2219 failrec->this_mirror = 0;
2220 failrec->bio_flags = 0;
2221 failrec->in_validation = 0;
2222
2223 read_lock(&em_tree->lock);
2224 em = lookup_extent_mapping(em_tree, start, failrec->len);
2225 if (!em) {
2226 read_unlock(&em_tree->lock);
2227 kfree(failrec);
2228 return -EIO;
2229 }
2230
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002231 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002232 free_extent_map(em);
2233 em = NULL;
2234 }
2235 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002236 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002237 kfree(failrec);
2238 return -EIO;
2239 }
Miao Xie2fe63032014-09-12 18:43:59 +08002240
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002241 logical = start - em->start;
2242 logical = em->block_start + logical;
2243 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2244 logical = em->block_start;
2245 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2246 extent_set_compress_type(&failrec->bio_flags,
2247 em->compress_type);
2248 }
Miao Xie2fe63032014-09-12 18:43:59 +08002249
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002250 btrfs_debug(fs_info,
2251 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2252 logical, start, failrec->len);
Miao Xie2fe63032014-09-12 18:43:59 +08002253
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002254 failrec->logical = logical;
2255 free_extent_map(em);
2256
2257 /* set the bits in the private failure tree */
2258 ret = set_extent_bits(failure_tree, start, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002259 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002260 if (ret >= 0)
David Sterba47dc1962016-02-11 13:24:13 +01002261 ret = set_state_failrec(failure_tree, start, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002262 /* set the bits in the inode's tree */
2263 if (ret >= 0)
David Sterbaceeb0ae2016-04-26 23:54:39 +02002264 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002265 if (ret < 0) {
2266 kfree(failrec);
2267 return ret;
2268 }
2269 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002270 btrfs_debug(fs_info,
2271 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2272 failrec->logical, failrec->start, failrec->len,
2273 failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002274 /*
2275 * when data can be on disk more than twice, add to failrec here
2276 * (e.g. with a list for failed_mirror) to make
2277 * clean_io_failure() clean all those errors at once.
2278 */
2279 }
Miao Xie2fe63032014-09-12 18:43:59 +08002280
2281 *failrec_ret = failrec;
2282
2283 return 0;
2284}
2285
2286int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio,
2287 struct io_failure_record *failrec, int failed_mirror)
2288{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002289 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002290 int num_copies;
2291
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002292 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002293 if (num_copies == 1) {
2294 /*
2295 * we only have a single copy of the data, so don't bother with
2296 * all the retry and error correction code that follows. no
2297 * matter what the error is, it is very likely to persist.
2298 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002299 btrfs_debug(fs_info,
2300 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2301 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie2fe63032014-09-12 18:43:59 +08002302 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002303 }
2304
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002305 /*
2306 * there are two premises:
2307 * a) deliver good data to the caller
2308 * b) correct the bad sectors on disk
2309 */
2310 if (failed_bio->bi_vcnt > 1) {
2311 /*
2312 * to fulfill b), we need to know the exact failing sectors, as
2313 * we don't want to rewrite any more than the failed ones. thus,
2314 * we need separate read requests for the failed bio
2315 *
2316 * if the following BUG_ON triggers, our validation request got
2317 * merged. we need separate requests for our algorithm to work.
2318 */
2319 BUG_ON(failrec->in_validation);
2320 failrec->in_validation = 1;
2321 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002322 } else {
2323 /*
2324 * we're ready to fulfill a) and b) alongside. get a good copy
2325 * of the failed sector and if we succeed, we have setup
2326 * everything for repair_io_failure to do the rest for us.
2327 */
2328 if (failrec->in_validation) {
2329 BUG_ON(failrec->this_mirror != failed_mirror);
2330 failrec->in_validation = 0;
2331 failrec->this_mirror = 0;
2332 }
2333 failrec->failed_mirror = failed_mirror;
2334 failrec->this_mirror++;
2335 if (failrec->this_mirror == failed_mirror)
2336 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002337 }
2338
Miao Xiefacc8a222013-07-25 19:22:34 +08002339 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002340 btrfs_debug(fs_info,
2341 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2342 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie2fe63032014-09-12 18:43:59 +08002343 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002344 }
2345
Miao Xie2fe63032014-09-12 18:43:59 +08002346 return 1;
2347}
2348
2349
2350struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2351 struct io_failure_record *failrec,
2352 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002353 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002354{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002355 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002356 struct bio *bio;
2357 struct btrfs_io_bio *btrfs_failed_bio;
2358 struct btrfs_io_bio *btrfs_bio;
2359
Chris Mason9be33952013-05-17 18:30:14 -04002360 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Miao Xie2fe63032014-09-12 18:43:59 +08002361 if (!bio)
2362 return NULL;
2363
2364 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002365 bio->bi_iter.bi_sector = failrec->logical >> 9;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002366 bio->bi_bdev = fs_info->fs_devices->latest_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002367 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002368 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002369
Miao Xiefacc8a222013-07-25 19:22:34 +08002370 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2371 if (btrfs_failed_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002372 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2373
2374 btrfs_bio = btrfs_io_bio(bio);
2375 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002376 icsum *= csum_size;
2377 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002378 csum_size);
2379 }
2380
Miao Xie2fe63032014-09-12 18:43:59 +08002381 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002382
Miao Xie2fe63032014-09-12 18:43:59 +08002383 return bio;
2384}
2385
2386/*
2387 * this is a generic handler for readpage errors (default
2388 * readpage_io_failed_hook). if other copies exist, read those and write back
2389 * good data to the failed position. does not investigate in remapping the
2390 * failed extent elsewhere, hoping the device will be smart enough to do this as
2391 * needed
2392 */
2393
2394static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2395 struct page *page, u64 start, u64 end,
2396 int failed_mirror)
2397{
2398 struct io_failure_record *failrec;
2399 struct inode *inode = page->mapping->host;
2400 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2401 struct bio *bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002402 int read_mode = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002403 blk_status_t status;
Miao Xie2fe63032014-09-12 18:43:59 +08002404 int ret;
2405
Mike Christie1f7ad752016-06-05 14:31:51 -05002406 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002407
2408 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2409 if (ret)
2410 return ret;
2411
2412 ret = btrfs_check_repairable(inode, failed_bio, failrec, failed_mirror);
2413 if (!ret) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02002414 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie2fe63032014-09-12 18:43:59 +08002415 return -EIO;
2416 }
2417
2418 if (failed_bio->bi_vcnt > 1)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002419 read_mode |= REQ_FAILFAST_DEV;
Miao Xie2fe63032014-09-12 18:43:59 +08002420
2421 phy_offset >>= inode->i_sb->s_blocksize_bits;
2422 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2423 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002424 (int)phy_offset, failed_bio->bi_end_io,
2425 NULL);
Miao Xie2fe63032014-09-12 18:43:59 +08002426 if (!bio) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02002427 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie2fe63032014-09-12 18:43:59 +08002428 return -EIO;
2429 }
Mike Christie1f7ad752016-06-05 14:31:51 -05002430 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie2fe63032014-09-12 18:43:59 +08002431
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002432 btrfs_debug(btrfs_sb(inode->i_sb),
2433 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2434 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002435
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002436 status = tree->ops->submit_bio_hook(inode, bio, failrec->this_mirror,
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002437 failrec->bio_flags, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002438 if (status) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02002439 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002440 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002441 ret = blk_status_to_errno(status);
Miao Xie6c387ab2014-09-12 18:43:57 +08002442 }
2443
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002444 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002445}
2446
Chris Masond1310b22008-01-24 16:13:08 -05002447/* lots and lots of room for performance fixes in the end_bio funcs */
2448
David Sterbab5227c02015-12-03 13:08:59 +01002449void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002450{
2451 int uptodate = (err == 0);
2452 struct extent_io_tree *tree;
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002453 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002454
2455 tree = &BTRFS_I(page->mapping->host)->io_tree;
2456
David Sterbac3988d62017-02-17 15:18:32 +01002457 if (tree->ops && tree->ops->writepage_end_io_hook)
2458 tree->ops->writepage_end_io_hook(page, start, end, NULL,
2459 uptodate);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002460
Jeff Mahoney87826df2012-02-15 16:23:57 +01002461 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002462 ClearPageUptodate(page);
2463 SetPageError(page);
Colin Ian Kingbff5baf2017-05-09 18:14:01 +01002464 ret = err < 0 ? err : -EIO;
Liu Bo5dca6ee2014-05-12 12:47:36 +08002465 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002466 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002467}
2468
Chris Masond1310b22008-01-24 16:13:08 -05002469/*
2470 * after a writepage IO is done, we need to:
2471 * clear the uptodate bits on error
2472 * clear the writeback bits in the extent tree for this IO
2473 * end_page_writeback if the page has no more pending IO
2474 *
2475 * Scheduling is not allowed, so the extent state tree is expected
2476 * to have one and only one object corresponding to this IO.
2477 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002478static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002479{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002480 int error = blk_status_to_errno(bio->bi_status);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002481 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002482 u64 start;
2483 u64 end;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002484 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002485
Kent Overstreet2c30c712013-11-07 12:20:26 -08002486 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002487 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002488 struct inode *inode = page->mapping->host;
2489 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002490
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002491 /* We always issue full-page reads, but if some block
2492 * in a page fails to read, blk_update_request() will
2493 * advance bv_offset and adjust bv_len to compensate.
2494 * Print a warning for nonzero offsets, and an error
2495 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002496 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2497 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002498 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002499 "partial page write in btrfs with offset %u and length %u",
2500 bvec->bv_offset, bvec->bv_len);
2501 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002502 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002503 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002504 bvec->bv_offset, bvec->bv_len);
2505 }
Chris Masond1310b22008-01-24 16:13:08 -05002506
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002507 start = page_offset(page);
2508 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002509
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002510 end_extent_writepage(page, error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002511 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002512 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002513
Chris Masond1310b22008-01-24 16:13:08 -05002514 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002515}
2516
Miao Xie883d0de2013-07-25 19:22:35 +08002517static void
2518endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2519 int uptodate)
2520{
2521 struct extent_state *cached = NULL;
2522 u64 end = start + len - 1;
2523
2524 if (uptodate && tree->track_uptodate)
2525 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
2526 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2527}
2528
Chris Masond1310b22008-01-24 16:13:08 -05002529/*
2530 * after a readpage IO is done, we need to:
2531 * clear the uptodate bits on error
2532 * set the uptodate bits if things worked
2533 * set the page up to date if all extents in the tree are uptodate
2534 * clear the lock bit in the extent tree
2535 * unlock the page if there are no other extents locked for it
2536 *
2537 * Scheduling is not allowed, so the extent state tree is expected
2538 * to have one and only one object corresponding to this IO.
2539 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002540static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002541{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002542 struct bio_vec *bvec;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002543 int uptodate = !bio->bi_status;
Miao Xiefacc8a222013-07-25 19:22:34 +08002544 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
David Woodhouse902b22f2008-08-20 08:51:49 -04002545 struct extent_io_tree *tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002546 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002547 u64 start;
2548 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002549 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002550 u64 extent_start = 0;
2551 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002552 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002553 int ret;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002554 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002555
Kent Overstreet2c30c712013-11-07 12:20:26 -08002556 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002557 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002558 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002559 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen507903b2011-04-06 10:02:20 +00002560
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002561 btrfs_debug(fs_info,
2562 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002563 (u64)bio->bi_iter.bi_sector, bio->bi_status,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002564 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002565 tree = &BTRFS_I(inode)->io_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002566
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002567 /* We always issue full-page reads, but if some block
2568 * in a page fails to read, blk_update_request() will
2569 * advance bv_offset and adjust bv_len to compensate.
2570 * Print a warning for nonzero offsets, and an error
2571 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002572 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2573 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002574 btrfs_err(fs_info,
2575 "partial page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002576 bvec->bv_offset, bvec->bv_len);
2577 else
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002578 btrfs_info(fs_info,
2579 "incomplete page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002580 bvec->bv_offset, bvec->bv_len);
2581 }
Chris Masond1310b22008-01-24 16:13:08 -05002582
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002583 start = page_offset(page);
2584 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002585 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002586
Chris Mason9be33952013-05-17 18:30:14 -04002587 mirror = io_bio->mirror_num;
David Sterba20c98012017-02-17 15:59:35 +01002588 if (likely(uptodate && tree->ops)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002589 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2590 page, start, end,
2591 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002592 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002593 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002594 else
Nikolay Borisovb30cb442017-02-20 13:50:56 +02002595 clean_io_failure(BTRFS_I(inode), start,
2596 page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002597 }
Josef Bacikea466792012-03-26 21:57:36 -04002598
Miao Xief2a09da2013-07-25 19:22:33 +08002599 if (likely(uptodate))
2600 goto readpage_ok;
2601
David Sterba20a7db82017-02-17 16:24:29 +01002602 if (tree->ops) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002603 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Liu Bo9d0d1c82017-03-24 15:04:50 -07002604 if (ret == -EAGAIN) {
2605 /*
2606 * Data inode's readpage_io_failed_hook() always
2607 * returns -EAGAIN.
2608 *
2609 * The generic bio_readpage_error handles errors
2610 * the following way: If possible, new read
2611 * requests are created and submitted and will
2612 * end up in end_bio_extent_readpage as well (if
2613 * we're lucky, not in the !uptodate case). In
2614 * that case it returns 0 and we just go on with
2615 * the next page in our bio. If it can't handle
2616 * the error it will return -EIO and we remain
2617 * responsible for that page.
2618 */
2619 ret = bio_readpage_error(bio, offset, page,
2620 start, end, mirror);
2621 if (ret == 0) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002622 uptodate = !bio->bi_status;
Liu Bo9d0d1c82017-03-24 15:04:50 -07002623 offset += len;
2624 continue;
2625 }
Chris Mason7e383262008-04-09 16:28:12 -04002626 }
Liu Bo9d0d1c82017-03-24 15:04:50 -07002627
2628 /*
2629 * metadata's readpage_io_failed_hook() always returns
2630 * -EIO and fixes nothing. -EIO is also returned if
2631 * data inode error could not be fixed.
2632 */
2633 ASSERT(ret == -EIO);
Chris Mason7e383262008-04-09 16:28:12 -04002634 }
Miao Xief2a09da2013-07-25 19:22:33 +08002635readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002636 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002637 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002638 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002639 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002640
2641 /* Zero out the end if this page straddles i_size */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002642 off = i_size & (PAGE_SIZE-1);
Liu Boa583c022014-08-19 23:32:22 +08002643 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002644 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002645 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002646 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002647 ClearPageUptodate(page);
2648 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002649 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002650 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002651 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002652
2653 if (unlikely(!uptodate)) {
2654 if (extent_len) {
2655 endio_readpage_release_extent(tree,
2656 extent_start,
2657 extent_len, 1);
2658 extent_start = 0;
2659 extent_len = 0;
2660 }
2661 endio_readpage_release_extent(tree, start,
2662 end - start + 1, 0);
2663 } else if (!extent_len) {
2664 extent_start = start;
2665 extent_len = end + 1 - start;
2666 } else if (extent_start + extent_len == start) {
2667 extent_len += end + 1 - start;
2668 } else {
2669 endio_readpage_release_extent(tree, extent_start,
2670 extent_len, uptodate);
2671 extent_start = start;
2672 extent_len = end + 1 - start;
2673 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002674 }
Chris Masond1310b22008-01-24 16:13:08 -05002675
Miao Xie883d0de2013-07-25 19:22:35 +08002676 if (extent_len)
2677 endio_readpage_release_extent(tree, extent_start, extent_len,
2678 uptodate);
Miao Xiefacc8a222013-07-25 19:22:34 +08002679 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002680 io_bio->end_io(io_bio, blk_status_to_errno(bio->bi_status));
Chris Masond1310b22008-01-24 16:13:08 -05002681 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002682}
2683
Chris Mason9be33952013-05-17 18:30:14 -04002684/*
2685 * this allocates from the btrfs_bioset. We're returning a bio right now
2686 * but you can call btrfs_io_bio for the appropriate container_of magic
2687 */
Miao Xie88f794e2010-11-22 03:02:55 +00002688struct bio *
2689btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2690 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002691{
Miao Xiefacc8a222013-07-25 19:22:34 +08002692 struct btrfs_io_bio *btrfs_bio;
Chris Masond1310b22008-01-24 16:13:08 -05002693 struct bio *bio;
2694
Chris Mason9be33952013-05-17 18:30:14 -04002695 bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -05002696
2697 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
Chris Mason9be33952013-05-17 18:30:14 -04002698 while (!bio && (nr_vecs /= 2)) {
2699 bio = bio_alloc_bioset(gfp_flags,
2700 nr_vecs, btrfs_bioset);
2701 }
Chris Masond1310b22008-01-24 16:13:08 -05002702 }
2703
2704 if (bio) {
2705 bio->bi_bdev = bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002706 bio->bi_iter.bi_sector = first_sector;
Miao Xiefacc8a222013-07-25 19:22:34 +08002707 btrfs_bio = btrfs_io_bio(bio);
2708 btrfs_bio->csum = NULL;
2709 btrfs_bio->csum_allocated = NULL;
2710 btrfs_bio->end_io = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002711 }
2712 return bio;
2713}
2714
Chris Mason9be33952013-05-17 18:30:14 -04002715struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
2716{
Miao Xie23ea8e52014-09-12 18:43:54 +08002717 struct btrfs_io_bio *btrfs_bio;
2718 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002719
Miao Xie23ea8e52014-09-12 18:43:54 +08002720 new = bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
2721 if (new) {
2722 btrfs_bio = btrfs_io_bio(new);
2723 btrfs_bio->csum = NULL;
2724 btrfs_bio->csum_allocated = NULL;
2725 btrfs_bio->end_io = NULL;
2726 }
2727 return new;
2728}
Chris Mason9be33952013-05-17 18:30:14 -04002729
2730/* this also allocates from the btrfs_bioset */
2731struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
2732{
Miao Xiefacc8a222013-07-25 19:22:34 +08002733 struct btrfs_io_bio *btrfs_bio;
2734 struct bio *bio;
2735
2736 bio = bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
2737 if (bio) {
2738 btrfs_bio = btrfs_io_bio(bio);
2739 btrfs_bio->csum = NULL;
2740 btrfs_bio->csum_allocated = NULL;
2741 btrfs_bio->end_io = NULL;
2742 }
2743 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002744}
2745
2746
Mike Christie1f7ad752016-06-05 14:31:51 -05002747static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
2748 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002749{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002750 blk_status_t ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002751 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2752 struct page *page = bvec->bv_page;
2753 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002754 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002755
Miao Xie4eee4fa2012-12-21 09:17:45 +00002756 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002757
David Woodhouse902b22f2008-08-20 08:51:49 -04002758 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002759 bio_get(bio);
2760
David Sterba20c98012017-02-17 15:59:35 +01002761 if (tree->ops)
Mike Christie81a75f672016-06-05 14:31:54 -05002762 ret = tree->ops->submit_bio_hook(page->mapping->host, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002763 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002764 else
Mike Christie4e49ea42016-06-05 14:31:41 -05002765 btrfsic_submit_bio(bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002766
Chris Masond1310b22008-01-24 16:13:08 -05002767 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002768 return blk_status_to_errno(ret);
Chris Masond1310b22008-01-24 16:13:08 -05002769}
2770
Mike Christie1f7ad752016-06-05 14:31:51 -05002771static int merge_bio(struct extent_io_tree *tree, struct page *page,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002772 unsigned long offset, size_t size, struct bio *bio,
2773 unsigned long bio_flags)
2774{
2775 int ret = 0;
David Sterba20c98012017-02-17 15:59:35 +01002776 if (tree->ops)
Mike Christie81a75f672016-06-05 14:31:54 -05002777 ret = tree->ops->merge_bio_hook(page, offset, size, bio,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002778 bio_flags);
Jeff Mahoney3444a972011-10-03 23:23:13 -04002779 return ret;
2780
2781}
2782
Mike Christie1f7ad752016-06-05 14:31:51 -05002783static int submit_extent_page(int op, int op_flags, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002784 struct writeback_control *wbc,
Chris Masond1310b22008-01-24 16:13:08 -05002785 struct page *page, sector_t sector,
2786 size_t size, unsigned long offset,
2787 struct block_device *bdev,
2788 struct bio **bio_ret,
Chris Masonf1885912008-04-09 16:28:12 -04002789 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002790 int mirror_num,
2791 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002792 unsigned long bio_flags,
2793 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002794{
2795 int ret = 0;
2796 struct bio *bio;
Chris Masonc8b97812008-10-29 14:49:59 -04002797 int contig = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002798 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002799 size_t page_size = min_t(size_t, size, PAGE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002800
2801 if (bio_ret && *bio_ret) {
2802 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002803 if (old_compressed)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002804 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002805 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002806 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002807
2808 if (prev_bio_flags != bio_flags || !contig ||
Filipe Manana005efed2015-09-14 09:09:31 +01002809 force_bio_submit ||
Mike Christie1f7ad752016-06-05 14:31:51 -05002810 merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002811 bio_add_page(bio, page, page_size, offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002812 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002813 if (ret < 0) {
2814 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002815 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002816 }
Chris Masond1310b22008-01-24 16:13:08 -05002817 bio = NULL;
2818 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002819 if (wbc)
2820 wbc_account_io(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002821 return 0;
2822 }
2823 }
Chris Masonc8b97812008-10-29 14:49:59 -04002824
Kent Overstreetb54ffb72015-05-19 14:31:01 +02002825 bio = btrfs_bio_alloc(bdev, sector, BIO_MAX_PAGES,
2826 GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002827 if (!bio)
2828 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002829
Chris Masonc8b97812008-10-29 14:49:59 -04002830 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002831 bio->bi_end_io = end_io_func;
2832 bio->bi_private = tree;
Mike Christie1f7ad752016-06-05 14:31:51 -05002833 bio_set_op_attrs(bio, op, op_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07002834 if (wbc) {
2835 wbc_init_bio(wbc, bio);
2836 wbc_account_io(wbc, page, page_size);
2837 }
Chris Mason70dec802008-01-29 09:59:12 -05002838
Chris Masond3977122009-01-05 21:25:51 -05002839 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002840 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002841 else
Mike Christie1f7ad752016-06-05 14:31:51 -05002842 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002843
2844 return ret;
2845}
2846
Eric Sandeen48a3b632013-04-25 20:41:01 +00002847static void attach_extent_buffer_page(struct extent_buffer *eb,
2848 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002849{
2850 if (!PagePrivate(page)) {
2851 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002852 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002853 set_page_private(page, (unsigned long)eb);
2854 } else {
2855 WARN_ON(page->private != (unsigned long)eb);
2856 }
2857}
2858
Chris Masond1310b22008-01-24 16:13:08 -05002859void set_page_extent_mapped(struct page *page)
2860{
2861 if (!PagePrivate(page)) {
2862 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002863 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002864 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002865 }
2866}
2867
Miao Xie125bac012013-07-25 19:22:37 +08002868static struct extent_map *
2869__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2870 u64 start, u64 len, get_extent_t *get_extent,
2871 struct extent_map **em_cached)
2872{
2873 struct extent_map *em;
2874
2875 if (em_cached && *em_cached) {
2876 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002877 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002878 start < extent_map_end(em)) {
Elena Reshetova490b54d2017-03-03 10:55:12 +02002879 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002880 return em;
2881 }
2882
2883 free_extent_map(em);
2884 *em_cached = NULL;
2885 }
2886
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02002887 em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
Miao Xie125bac012013-07-25 19:22:37 +08002888 if (em_cached && !IS_ERR_OR_NULL(em)) {
2889 BUG_ON(*em_cached);
Elena Reshetova490b54d2017-03-03 10:55:12 +02002890 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002891 *em_cached = em;
2892 }
2893 return em;
2894}
Chris Masond1310b22008-01-24 16:13:08 -05002895/*
2896 * basic readpage implementation. Locked extent state structs are inserted
2897 * into the tree that are removed when the IO is done (by the end_io
2898 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002899 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07002900 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05002901 */
Miao Xie99740902013-07-25 19:22:36 +08002902static int __do_readpage(struct extent_io_tree *tree,
2903 struct page *page,
2904 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002905 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002906 struct bio **bio, int mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05002907 unsigned long *bio_flags, int read_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002908 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05002909{
2910 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002911 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002912 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002913 u64 end;
2914 u64 cur = start;
2915 u64 extent_offset;
2916 u64 last_byte = i_size_read(inode);
2917 u64 block_start;
2918 u64 cur_end;
2919 sector_t sector;
2920 struct extent_map *em;
2921 struct block_device *bdev;
Liu Bobaf863b2016-07-11 10:39:07 -07002922 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002923 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002924 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002925 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002926 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002927 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00002928 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002929
2930 set_page_extent_mapped(page);
2931
Miao Xie99740902013-07-25 19:22:36 +08002932 end = page_end;
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002933 if (!PageUptodate(page)) {
2934 if (cleancache_get_page(page) == 0) {
2935 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08002936 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002937 goto out;
2938 }
2939 }
2940
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002941 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04002942 char *userpage;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002943 size_t zero_offset = last_byte & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002944
2945 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002946 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002947 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002948 memset(userpage + zero_offset, 0, iosize);
2949 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002950 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002951 }
2952 }
Chris Masond1310b22008-01-24 16:13:08 -05002953 while (cur <= end) {
Filipe Manana005efed2015-09-14 09:09:31 +01002954 bool force_bio_submit = false;
Josef Bacikc8f2f242013-02-11 11:33:00 -05002955
Chris Masond1310b22008-01-24 16:13:08 -05002956 if (cur >= last_byte) {
2957 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002958 struct extent_state *cached = NULL;
2959
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002960 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002961 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002962 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002963 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002964 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002965 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002966 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00002967 unlock_extent_cached(tree, cur,
2968 cur + iosize - 1,
2969 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002970 break;
2971 }
Miao Xie125bac012013-07-25 19:22:37 +08002972 em = __get_extent_map(inode, page, pg_offset, cur,
2973 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02002974 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002975 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00002976 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002977 break;
2978 }
Chris Masond1310b22008-01-24 16:13:08 -05002979 extent_offset = cur - em->start;
2980 BUG_ON(extent_map_end(em) <= cur);
2981 BUG_ON(end < cur);
2982
Li Zefan261507a02010-12-17 14:21:50 +08002983 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07002984 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002985 extent_set_compress_type(&this_bio_flag,
2986 em->compress_type);
2987 }
Chris Masonc8b97812008-10-29 14:49:59 -04002988
Chris Masond1310b22008-01-24 16:13:08 -05002989 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2990 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002991 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002992 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2993 disk_io_size = em->block_len;
2994 sector = em->block_start >> 9;
2995 } else {
2996 sector = (em->block_start + extent_offset) >> 9;
2997 disk_io_size = iosize;
2998 }
Chris Masond1310b22008-01-24 16:13:08 -05002999 bdev = em->bdev;
3000 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04003001 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
3002 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01003003
3004 /*
3005 * If we have a file range that points to a compressed extent
3006 * and it's followed by a consecutive file range that points to
3007 * to the same compressed extent (possibly with a different
3008 * offset and/or length, so it either points to the whole extent
3009 * or only part of it), we must make sure we do not submit a
3010 * single bio to populate the pages for the 2 ranges because
3011 * this makes the compressed extent read zero out the pages
3012 * belonging to the 2nd range. Imagine the following scenario:
3013 *
3014 * File layout
3015 * [0 - 8K] [8K - 24K]
3016 * | |
3017 * | |
3018 * points to extent X, points to extent X,
3019 * offset 4K, length of 8K offset 0, length 16K
3020 *
3021 * [extent X, compressed length = 4K uncompressed length = 16K]
3022 *
3023 * If the bio to read the compressed extent covers both ranges,
3024 * it will decompress extent X into the pages belonging to the
3025 * first range and then it will stop, zeroing out the remaining
3026 * pages that belong to the other range that points to extent X.
3027 * So here we make sure we submit 2 bios, one for the first
3028 * range and another one for the third range. Both will target
3029 * the same physical extent from disk, but we can't currently
3030 * make the compressed bio endio callback populate the pages
3031 * for both ranges because each compressed bio is tightly
3032 * coupled with a single extent map, and each range can have
3033 * an extent map with a different offset value relative to the
3034 * uncompressed data of our extent and different lengths. This
3035 * is a corner case so we prioritize correctness over
3036 * non-optimal behavior (submitting 2 bios for the same extent).
3037 */
3038 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3039 prev_em_start && *prev_em_start != (u64)-1 &&
3040 *prev_em_start != em->orig_start)
3041 force_bio_submit = true;
3042
3043 if (prev_em_start)
3044 *prev_em_start = em->orig_start;
3045
Chris Masond1310b22008-01-24 16:13:08 -05003046 free_extent_map(em);
3047 em = NULL;
3048
3049 /* we've found a hole, just zero and go on */
3050 if (block_start == EXTENT_MAP_HOLE) {
3051 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003052 struct extent_state *cached = NULL;
3053
Cong Wang7ac687d2011-11-25 23:14:28 +08003054 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003055 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003056 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003057 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003058
3059 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003060 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003061 unlock_extent_cached(tree, cur,
3062 cur + iosize - 1,
3063 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003064 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003065 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003066 continue;
3067 }
3068 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003069 if (test_range_bit(tree, cur, cur_end,
3070 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003071 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003072 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003073 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003074 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003075 continue;
3076 }
Chris Mason70dec802008-01-29 09:59:12 -05003077 /* we have an inline extent but it didn't get marked up
3078 * to date. Error out
3079 */
3080 if (block_start == EXTENT_MAP_INLINE) {
3081 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003082 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003083 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003084 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003085 continue;
3086 }
Chris Masond1310b22008-01-24 16:13:08 -05003087
Mike Christie1f7ad752016-06-05 14:31:51 -05003088 ret = submit_extent_page(REQ_OP_READ, read_flags, tree, NULL,
3089 page, sector, disk_io_size, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003090 bdev, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003091 end_bio_extent_readpage, mirror_num,
3092 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003093 this_bio_flag,
3094 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003095 if (!ret) {
3096 nr++;
3097 *bio_flags = this_bio_flag;
3098 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003099 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003100 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003101 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003102 }
Chris Masond1310b22008-01-24 16:13:08 -05003103 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003104 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003105 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003106out:
Chris Masond1310b22008-01-24 16:13:08 -05003107 if (!nr) {
3108 if (!PageError(page))
3109 SetPageUptodate(page);
3110 unlock_page(page);
3111 }
Liu Bobaf863b2016-07-11 10:39:07 -07003112 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003113}
3114
Miao Xie99740902013-07-25 19:22:36 +08003115static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
3116 struct page *pages[], int nr_pages,
3117 u64 start, u64 end,
3118 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08003119 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003120 struct bio **bio, int mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003121 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003122 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003123{
3124 struct inode *inode;
3125 struct btrfs_ordered_extent *ordered;
3126 int index;
3127
3128 inode = pages[0]->mapping->host;
3129 while (1) {
3130 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003131 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Miao Xie99740902013-07-25 19:22:36 +08003132 end - start + 1);
3133 if (!ordered)
3134 break;
3135 unlock_extent(tree, start, end);
3136 btrfs_start_ordered_extent(inode, ordered, 1);
3137 btrfs_put_ordered_extent(ordered);
3138 }
3139
3140 for (index = 0; index < nr_pages; index++) {
Miao Xie125bac012013-07-25 19:22:37 +08003141 __do_readpage(tree, pages[index], get_extent, em_cached, bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003142 mirror_num, bio_flags, 0, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003143 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003144 }
3145}
3146
3147static void __extent_readpages(struct extent_io_tree *tree,
3148 struct page *pages[],
3149 int nr_pages, get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08003150 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003151 struct bio **bio, int mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003152 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003153 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003154{
Stefan Behrens35a36212013-08-14 18:12:25 +02003155 u64 start = 0;
Miao Xie99740902013-07-25 19:22:36 +08003156 u64 end = 0;
3157 u64 page_start;
3158 int index;
Stefan Behrens35a36212013-08-14 18:12:25 +02003159 int first_index = 0;
Miao Xie99740902013-07-25 19:22:36 +08003160
3161 for (index = 0; index < nr_pages; index++) {
3162 page_start = page_offset(pages[index]);
3163 if (!end) {
3164 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003165 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003166 first_index = index;
3167 } else if (end + 1 == page_start) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003168 end += PAGE_SIZE;
Miao Xie99740902013-07-25 19:22:36 +08003169 } else {
3170 __do_contiguous_readpages(tree, &pages[first_index],
3171 index - first_index, start,
Miao Xie125bac012013-07-25 19:22:37 +08003172 end, get_extent, em_cached,
3173 bio, mirror_num, bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05003174 prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003175 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003176 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003177 first_index = index;
3178 }
3179 }
3180
3181 if (end)
3182 __do_contiguous_readpages(tree, &pages[first_index],
3183 index - first_index, start,
Miao Xie125bac012013-07-25 19:22:37 +08003184 end, get_extent, em_cached, bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003185 mirror_num, bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003186 prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003187}
3188
3189static int __extent_read_full_page(struct extent_io_tree *tree,
3190 struct page *page,
3191 get_extent_t *get_extent,
3192 struct bio **bio, int mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003193 unsigned long *bio_flags, int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003194{
3195 struct inode *inode = page->mapping->host;
3196 struct btrfs_ordered_extent *ordered;
3197 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003198 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003199 int ret;
3200
3201 while (1) {
3202 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003203 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003204 PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003205 if (!ordered)
3206 break;
3207 unlock_extent(tree, start, end);
3208 btrfs_start_ordered_extent(inode, ordered, 1);
3209 btrfs_put_ordered_extent(ordered);
3210 }
3211
Miao Xie125bac012013-07-25 19:22:37 +08003212 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003213 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003214 return ret;
3215}
3216
Chris Masond1310b22008-01-24 16:13:08 -05003217int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003218 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003219{
3220 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003221 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003222 int ret;
3223
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003224 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003225 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003226 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003227 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003228 return ret;
3229}
Chris Masond1310b22008-01-24 16:13:08 -05003230
David Sterba3d4b9492017-02-10 19:33:41 +01003231static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003232 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003233{
3234 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003235}
3236
Chris Masond1310b22008-01-24 16:13:08 -05003237/*
Chris Mason40f76582014-05-21 13:35:51 -07003238 * helper for __extent_writepage, doing all of the delayed allocation setup.
3239 *
3240 * This returns 1 if our fill_delalloc function did all the work required
3241 * to write the page (copy into inline extent). In this case the IO has
3242 * been started and the page is already unlocked.
3243 *
3244 * This returns 0 if all went well (page still locked)
3245 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003246 */
Chris Mason40f76582014-05-21 13:35:51 -07003247static noinline_for_stack int writepage_delalloc(struct inode *inode,
3248 struct page *page, struct writeback_control *wbc,
3249 struct extent_page_data *epd,
3250 u64 delalloc_start,
3251 unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003252{
Chris Mason40f76582014-05-21 13:35:51 -07003253 struct extent_io_tree *tree = epd->tree;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003254 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003255 u64 nr_delalloc;
3256 u64 delalloc_to_write = 0;
3257 u64 delalloc_end = 0;
3258 int ret;
3259 int page_started = 0;
3260
3261 if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc)
3262 return 0;
3263
3264 while (delalloc_end < page_end) {
3265 nr_delalloc = find_lock_delalloc_range(inode, tree,
3266 page,
3267 &delalloc_start,
3268 &delalloc_end,
Josef Bacikdcab6a32015-02-11 15:08:59 -05003269 BTRFS_MAX_EXTENT_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003270 if (nr_delalloc == 0) {
3271 delalloc_start = delalloc_end + 1;
3272 continue;
3273 }
3274 ret = tree->ops->fill_delalloc(inode, page,
3275 delalloc_start,
3276 delalloc_end,
3277 &page_started,
3278 nr_written);
3279 /* File system has been set read-only */
3280 if (ret) {
3281 SetPageError(page);
3282 /* fill_delalloc should be return < 0 for error
3283 * but just in case, we use > 0 here meaning the
3284 * IO is started, so we don't want to return > 0
3285 * unless things are going well.
3286 */
3287 ret = ret < 0 ? ret : -EIO;
3288 goto done;
3289 }
3290 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003291 * delalloc_end is already one less than the total length, so
3292 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003293 */
3294 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003295 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003296 delalloc_start = delalloc_end + 1;
3297 }
3298 if (wbc->nr_to_write < delalloc_to_write) {
3299 int thresh = 8192;
3300
3301 if (delalloc_to_write < thresh * 2)
3302 thresh = delalloc_to_write;
3303 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3304 thresh);
3305 }
3306
3307 /* did the fill delalloc function already unlock and start
3308 * the IO?
3309 */
3310 if (page_started) {
3311 /*
3312 * we've unlocked the page, so we can't update
3313 * the mapping's writeback index, just update
3314 * nr_to_write.
3315 */
3316 wbc->nr_to_write -= *nr_written;
3317 return 1;
3318 }
3319
3320 ret = 0;
3321
3322done:
3323 return ret;
3324}
3325
3326/*
3327 * helper for __extent_writepage. This calls the writepage start hooks,
3328 * and does the loop to map the page into extents and bios.
3329 *
3330 * We return 1 if the IO is started and the page is unlocked,
3331 * 0 if all went well (page still locked)
3332 * < 0 if there were errors (page still locked)
3333 */
3334static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3335 struct page *page,
3336 struct writeback_control *wbc,
3337 struct extent_page_data *epd,
3338 loff_t i_size,
3339 unsigned long nr_written,
3340 int write_flags, int *nr_ret)
3341{
Chris Masond1310b22008-01-24 16:13:08 -05003342 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003343 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003344 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003345 u64 end;
3346 u64 cur = start;
3347 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003348 u64 block_start;
3349 u64 iosize;
3350 sector_t sector;
3351 struct extent_map *em;
3352 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003353 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003354 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003355 int ret = 0;
3356 int nr = 0;
3357 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003358
Chris Mason247e7432008-07-17 12:53:51 -04003359 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04003360 ret = tree->ops->writepage_start_hook(page, start,
3361 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01003362 if (ret) {
3363 /* Fixup worker will requeue */
3364 if (ret == -EBUSY)
3365 wbc->pages_skipped++;
3366 else
3367 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003368
David Sterba3d4b9492017-02-10 19:33:41 +01003369 update_nr_written(wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04003370 unlock_page(page);
Liu Bobcf93482017-01-25 17:15:54 -08003371 return 1;
Chris Mason247e7432008-07-17 12:53:51 -04003372 }
3373 }
3374
Chris Mason11c83492009-04-20 15:50:09 -04003375 /*
3376 * we don't want to touch the inode after unlocking the page,
3377 * so we update the mapping writeback index now
3378 */
David Sterba3d4b9492017-02-10 19:33:41 +01003379 update_nr_written(wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003380
Chris Masond1310b22008-01-24 16:13:08 -05003381 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003382 if (i_size <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003383 if (tree->ops && tree->ops->writepage_end_io_hook)
3384 tree->ops->writepage_end_io_hook(page, start,
3385 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003386 goto done;
3387 }
3388
Chris Masond1310b22008-01-24 16:13:08 -05003389 blocksize = inode->i_sb->s_blocksize;
3390
3391 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003392 u64 em_end;
David Sterba58409ed2016-05-04 11:46:10 +02003393
Chris Mason40f76582014-05-21 13:35:51 -07003394 if (cur >= i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003395 if (tree->ops && tree->ops->writepage_end_io_hook)
3396 tree->ops->writepage_end_io_hook(page, cur,
3397 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003398 break;
3399 }
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02003400 em = epd->get_extent(BTRFS_I(inode), page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003401 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003402 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003403 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003404 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003405 break;
3406 }
3407
3408 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003409 em_end = extent_map_end(em);
3410 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003411 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003412 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003413 iosize = ALIGN(iosize, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003414 sector = (em->block_start + extent_offset) >> 9;
3415 bdev = em->bdev;
3416 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003417 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003418 free_extent_map(em);
3419 em = NULL;
3420
Chris Masonc8b97812008-10-29 14:49:59 -04003421 /*
3422 * compressed and inline extents are written through other
3423 * paths in the FS
3424 */
3425 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003426 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003427 /*
3428 * end_io notification does not happen here for
3429 * compressed extents
3430 */
3431 if (!compressed && tree->ops &&
3432 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003433 tree->ops->writepage_end_io_hook(page, cur,
3434 cur + iosize - 1,
3435 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003436 else if (compressed) {
3437 /* we don't want to end_page_writeback on
3438 * a compressed extent. this happens
3439 * elsewhere
3440 */
3441 nr++;
3442 }
3443
3444 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003445 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003446 continue;
3447 }
Chris Masonc8b97812008-10-29 14:49:59 -04003448
David Sterba58409ed2016-05-04 11:46:10 +02003449 set_range_writeback(tree, cur, cur + iosize - 1);
3450 if (!PageWriteback(page)) {
3451 btrfs_err(BTRFS_I(inode)->root->fs_info,
3452 "page %lu not writeback, cur %llu end %llu",
3453 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003454 }
David Sterba58409ed2016-05-04 11:46:10 +02003455
Mike Christie1f7ad752016-06-05 14:31:51 -05003456 ret = submit_extent_page(REQ_OP_WRITE, write_flags, tree, wbc,
3457 page, sector, iosize, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003458 bdev, &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003459 end_bio_extent_writepage,
3460 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003461 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003462 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003463 if (PageWriteback(page))
3464 end_page_writeback(page);
3465 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003466
Chris Masond1310b22008-01-24 16:13:08 -05003467 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003468 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003469 nr++;
3470 }
3471done:
Chris Mason40f76582014-05-21 13:35:51 -07003472 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003473 return ret;
3474}
3475
3476/*
3477 * the writepage semantics are similar to regular writepage. extent
3478 * records are inserted to lock ranges in the tree, and as dirty areas
3479 * are found, they are marked writeback. Then the lock bits are removed
3480 * and the end_io handler clears the writeback ranges
3481 */
3482static int __extent_writepage(struct page *page, struct writeback_control *wbc,
3483 void *data)
3484{
3485 struct inode *inode = page->mapping->host;
3486 struct extent_page_data *epd = data;
3487 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003488 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003489 int ret;
3490 int nr = 0;
3491 size_t pg_offset = 0;
3492 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003493 unsigned long end_index = i_size >> PAGE_SHIFT;
Mike Christie1f7ad752016-06-05 14:31:51 -05003494 int write_flags = 0;
Chris Mason40f76582014-05-21 13:35:51 -07003495 unsigned long nr_written = 0;
3496
3497 if (wbc->sync_mode == WB_SYNC_ALL)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06003498 write_flags = REQ_SYNC;
Chris Mason40f76582014-05-21 13:35:51 -07003499
3500 trace___extent_writepage(page, inode, wbc);
3501
3502 WARN_ON(!PageLocked(page));
3503
3504 ClearPageError(page);
3505
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003506 pg_offset = i_size & (PAGE_SIZE - 1);
Chris Mason40f76582014-05-21 13:35:51 -07003507 if (page->index > end_index ||
3508 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003509 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003510 unlock_page(page);
3511 return 0;
3512 }
3513
3514 if (page->index == end_index) {
3515 char *userpage;
3516
3517 userpage = kmap_atomic(page);
3518 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003519 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003520 kunmap_atomic(userpage);
3521 flush_dcache_page(page);
3522 }
3523
3524 pg_offset = 0;
3525
3526 set_page_extent_mapped(page);
3527
3528 ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
3529 if (ret == 1)
3530 goto done_unlocked;
3531 if (ret)
3532 goto done;
3533
3534 ret = __extent_writepage_io(inode, page, wbc, epd,
3535 i_size, nr_written, write_flags, &nr);
3536 if (ret == 1)
3537 goto done_unlocked;
3538
3539done:
Chris Masond1310b22008-01-24 16:13:08 -05003540 if (nr == 0) {
3541 /* make sure the mapping tag for page dirty gets cleared */
3542 set_page_writeback(page);
3543 end_page_writeback(page);
3544 }
Filipe Manana61391d52014-05-09 17:17:40 +01003545 if (PageError(page)) {
3546 ret = ret < 0 ? ret : -EIO;
3547 end_extent_writepage(page, ret, start, page_end);
3548 }
Chris Masond1310b22008-01-24 16:13:08 -05003549 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003550 return ret;
Chris Mason771ed682008-11-06 22:02:51 -05003551
Chris Mason11c83492009-04-20 15:50:09 -04003552done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003553 return 0;
3554}
3555
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003556void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003557{
NeilBrown74316202014-07-07 15:16:04 +10003558 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3559 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003560}
3561
Chris Mason0e378df2014-05-19 20:55:27 -07003562static noinline_for_stack int
3563lock_extent_buffer_for_io(struct extent_buffer *eb,
3564 struct btrfs_fs_info *fs_info,
3565 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003566{
3567 unsigned long i, num_pages;
3568 int flush = 0;
3569 int ret = 0;
3570
3571 if (!btrfs_try_tree_write_lock(eb)) {
3572 flush = 1;
3573 flush_write_bio(epd);
3574 btrfs_tree_lock(eb);
3575 }
3576
3577 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3578 btrfs_tree_unlock(eb);
3579 if (!epd->sync_io)
3580 return 0;
3581 if (!flush) {
3582 flush_write_bio(epd);
3583 flush = 1;
3584 }
Chris Masona098d8e2012-03-21 12:09:56 -04003585 while (1) {
3586 wait_on_extent_buffer_writeback(eb);
3587 btrfs_tree_lock(eb);
3588 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3589 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003590 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003591 }
3592 }
3593
Josef Bacik51561ff2012-07-20 16:25:24 -04003594 /*
3595 * We need to do this to prevent races in people who check if the eb is
3596 * under IO since we can end up having no IO bits set for a short period
3597 * of time.
3598 */
3599 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003600 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3601 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003602 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003603 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Miao Xiee2d84522013-01-29 10:09:20 +00003604 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3605 -eb->len,
3606 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003607 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003608 } else {
3609 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003610 }
3611
3612 btrfs_tree_unlock(eb);
3613
3614 if (!ret)
3615 return ret;
3616
3617 num_pages = num_extent_pages(eb->start, eb->len);
3618 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003619 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003620
3621 if (!trylock_page(p)) {
3622 if (!flush) {
3623 flush_write_bio(epd);
3624 flush = 1;
3625 }
3626 lock_page(p);
3627 }
3628 }
3629
3630 return ret;
3631}
3632
3633static void end_extent_buffer_writeback(struct extent_buffer *eb)
3634{
3635 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003636 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003637 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3638}
3639
Filipe Manana656f30d2014-09-26 12:25:56 +01003640static void set_btree_ioerr(struct page *page)
3641{
3642 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Filipe Manana656f30d2014-09-26 12:25:56 +01003643
3644 SetPageError(page);
3645 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3646 return;
3647
3648 /*
3649 * If writeback for a btree extent that doesn't belong to a log tree
3650 * failed, increment the counter transaction->eb_write_errors.
3651 * We do this because while the transaction is running and before it's
3652 * committing (when we call filemap_fdata[write|wait]_range against
3653 * the btree inode), we might have
3654 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3655 * returns an error or an error happens during writeback, when we're
3656 * committing the transaction we wouldn't know about it, since the pages
3657 * can be no longer dirty nor marked anymore for writeback (if a
3658 * subsequent modification to the extent buffer didn't happen before the
3659 * transaction commit), which makes filemap_fdata[write|wait]_range not
3660 * able to find the pages tagged with SetPageError at transaction
3661 * commit time. So if this happens we must abort the transaction,
3662 * otherwise we commit a super block with btree roots that point to
3663 * btree nodes/leafs whose content on disk is invalid - either garbage
3664 * or the content of some node/leaf from a past generation that got
3665 * cowed or deleted and is no longer valid.
3666 *
3667 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3668 * not be enough - we need to distinguish between log tree extents vs
3669 * non-log tree extents, and the next filemap_fdatawait_range() call
3670 * will catch and clear such errors in the mapping - and that call might
3671 * be from a log sync and not from a transaction commit. Also, checking
3672 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3673 * not done and would not be reliable - the eb might have been released
3674 * from memory and reading it back again means that flag would not be
3675 * set (since it's a runtime flag, not persisted on disk).
3676 *
3677 * Using the flags below in the btree inode also makes us achieve the
3678 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3679 * writeback for all dirty pages and before filemap_fdatawait_range()
3680 * is called, the writeback for all dirty pages had already finished
3681 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3682 * filemap_fdatawait_range() would return success, as it could not know
3683 * that writeback errors happened (the pages were no longer tagged for
3684 * writeback).
3685 */
3686 switch (eb->log_index) {
3687 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003688 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003689 break;
3690 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003691 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003692 break;
3693 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003694 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003695 break;
3696 default:
3697 BUG(); /* unexpected, logic error */
3698 }
3699}
3700
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003701static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003702{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003703 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003704 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003705 int i, done;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003706
Kent Overstreet2c30c712013-11-07 12:20:26 -08003707 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003708 struct page *page = bvec->bv_page;
3709
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003710 eb = (struct extent_buffer *)page->private;
3711 BUG_ON(!eb);
3712 done = atomic_dec_and_test(&eb->io_pages);
3713
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003714 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003715 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003716 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003717 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003718 }
3719
3720 end_page_writeback(page);
3721
3722 if (!done)
3723 continue;
3724
3725 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003726 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003727
3728 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003729}
3730
Chris Mason0e378df2014-05-19 20:55:27 -07003731static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003732 struct btrfs_fs_info *fs_info,
3733 struct writeback_control *wbc,
3734 struct extent_page_data *epd)
3735{
3736 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003737 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003738 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003739 u32 nritems;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003740 unsigned long i, num_pages;
Josef Bacikde0022b2012-09-25 14:25:58 -04003741 unsigned long bio_flags = 0;
Liu Bo851cd172016-09-23 13:44:44 -07003742 unsigned long start, end;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06003743 int write_flags = (epd->sync_io ? REQ_SYNC : 0) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003744 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003745
Filipe Manana656f30d2014-09-26 12:25:56 +01003746 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003747 num_pages = num_extent_pages(eb->start, eb->len);
3748 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003749 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3750 bio_flags = EXTENT_BIO_TREE_LOG;
3751
Liu Bo851cd172016-09-23 13:44:44 -07003752 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3753 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003754 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003755 end = btrfs_node_key_ptr_offset(nritems);
3756
David Sterbab159fa22016-11-08 18:09:03 +01003757 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003758 } else {
3759 /*
3760 * leaf:
3761 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3762 */
3763 start = btrfs_item_nr_offset(nritems);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003764 end = btrfs_leaf_data(eb) + leaf_data_end(fs_info, eb);
David Sterbab159fa22016-11-08 18:09:03 +01003765 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003766 }
3767
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003768 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003769 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003770
3771 clear_page_dirty_for_io(p);
3772 set_page_writeback(p);
Mike Christie1f7ad752016-06-05 14:31:51 -05003773 ret = submit_extent_page(REQ_OP_WRITE, write_flags, tree, wbc,
3774 p, offset >> 9, PAGE_SIZE, 0, bdev,
David Sterbac2df8bb2017-02-10 19:29:38 +01003775 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003776 end_bio_extent_buffer_writepage,
Filipe Manana005efed2015-09-14 09:09:31 +01003777 0, epd->bio_flags, bio_flags, false);
Josef Bacikde0022b2012-09-25 14:25:58 -04003778 epd->bio_flags = bio_flags;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003779 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003780 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003781 if (PageWriteback(p))
3782 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003783 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3784 end_extent_buffer_writeback(eb);
3785 ret = -EIO;
3786 break;
3787 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003788 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003789 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003790 unlock_page(p);
3791 }
3792
3793 if (unlikely(ret)) {
3794 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003795 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003796 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003797 unlock_page(p);
3798 }
3799 }
3800
3801 return ret;
3802}
3803
3804int btree_write_cache_pages(struct address_space *mapping,
3805 struct writeback_control *wbc)
3806{
3807 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3808 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3809 struct extent_buffer *eb, *prev_eb = NULL;
3810 struct extent_page_data epd = {
3811 .bio = NULL,
3812 .tree = tree,
3813 .extent_locked = 0,
3814 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003815 .bio_flags = 0,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003816 };
3817 int ret = 0;
3818 int done = 0;
3819 int nr_to_write_done = 0;
3820 struct pagevec pvec;
3821 int nr_pages;
3822 pgoff_t index;
3823 pgoff_t end; /* Inclusive */
3824 int scanned = 0;
3825 int tag;
3826
3827 pagevec_init(&pvec, 0);
3828 if (wbc->range_cyclic) {
3829 index = mapping->writeback_index; /* Start from prev offset */
3830 end = -1;
3831 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003832 index = wbc->range_start >> PAGE_SHIFT;
3833 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003834 scanned = 1;
3835 }
3836 if (wbc->sync_mode == WB_SYNC_ALL)
3837 tag = PAGECACHE_TAG_TOWRITE;
3838 else
3839 tag = PAGECACHE_TAG_DIRTY;
3840retry:
3841 if (wbc->sync_mode == WB_SYNC_ALL)
3842 tag_pages_for_writeback(mapping, index, end);
3843 while (!done && !nr_to_write_done && (index <= end) &&
3844 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3845 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3846 unsigned i;
3847
3848 scanned = 1;
3849 for (i = 0; i < nr_pages; i++) {
3850 struct page *page = pvec.pages[i];
3851
3852 if (!PagePrivate(page))
3853 continue;
3854
3855 if (!wbc->range_cyclic && page->index > end) {
3856 done = 1;
3857 break;
3858 }
3859
Josef Bacikb5bae262012-09-14 13:43:01 -04003860 spin_lock(&mapping->private_lock);
3861 if (!PagePrivate(page)) {
3862 spin_unlock(&mapping->private_lock);
3863 continue;
3864 }
3865
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003866 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003867
3868 /*
3869 * Shouldn't happen and normally this would be a BUG_ON
3870 * but no sense in crashing the users box for something
3871 * we can survive anyway.
3872 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303873 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003874 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003875 continue;
3876 }
3877
Josef Bacikb5bae262012-09-14 13:43:01 -04003878 if (eb == prev_eb) {
3879 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003880 continue;
3881 }
3882
Josef Bacikb5bae262012-09-14 13:43:01 -04003883 ret = atomic_inc_not_zero(&eb->refs);
3884 spin_unlock(&mapping->private_lock);
3885 if (!ret)
3886 continue;
3887
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003888 prev_eb = eb;
3889 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3890 if (!ret) {
3891 free_extent_buffer(eb);
3892 continue;
3893 }
3894
3895 ret = write_one_eb(eb, fs_info, wbc, &epd);
3896 if (ret) {
3897 done = 1;
3898 free_extent_buffer(eb);
3899 break;
3900 }
3901 free_extent_buffer(eb);
3902
3903 /*
3904 * the filesystem may choose to bump up nr_to_write.
3905 * We have to make sure to honor the new nr_to_write
3906 * at any time
3907 */
3908 nr_to_write_done = wbc->nr_to_write <= 0;
3909 }
3910 pagevec_release(&pvec);
3911 cond_resched();
3912 }
3913 if (!scanned && !done) {
3914 /*
3915 * We hit the last page and there is more work to be done: wrap
3916 * back to the start of the file
3917 */
3918 scanned = 1;
3919 index = 0;
3920 goto retry;
3921 }
3922 flush_write_bio(&epd);
3923 return ret;
3924}
3925
Chris Masond1310b22008-01-24 16:13:08 -05003926/**
Chris Mason4bef0842008-09-08 11:18:08 -04003927 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
Chris Masond1310b22008-01-24 16:13:08 -05003928 * @mapping: address space structure to write
3929 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3930 * @writepage: function called for each page
3931 * @data: data passed to writepage function
3932 *
3933 * If a page is already under I/O, write_cache_pages() skips it, even
3934 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3935 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3936 * and msync() need to guarantee that all the data which was dirty at the time
3937 * the call was made get new I/O started against them. If wbc->sync_mode is
3938 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3939 * existing IO to complete.
3940 */
David Sterba4242b642017-02-10 19:38:24 +01003941static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04003942 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003943 writepage_t writepage, void *data,
3944 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003945{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003946 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003947 int ret = 0;
3948 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003949 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003950 struct pagevec pvec;
3951 int nr_pages;
3952 pgoff_t index;
3953 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08003954 pgoff_t done_index;
3955 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003956 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003957 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003958
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003959 /*
3960 * We have to hold onto the inode so that ordered extents can do their
3961 * work when the IO finishes. The alternative to this is failing to add
3962 * an ordered extent if the igrab() fails there and that is a huge pain
3963 * to deal with, so instead just hold onto the inode throughout the
3964 * writepages operation. If it fails here we are freeing up the inode
3965 * anyway and we'd rather not waste our time writing out stuff that is
3966 * going to be truncated anyway.
3967 */
3968 if (!igrab(inode))
3969 return 0;
3970
Chris Masond1310b22008-01-24 16:13:08 -05003971 pagevec_init(&pvec, 0);
3972 if (wbc->range_cyclic) {
3973 index = mapping->writeback_index; /* Start from prev offset */
3974 end = -1;
3975 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003976 index = wbc->range_start >> PAGE_SHIFT;
3977 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08003978 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3979 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05003980 scanned = 1;
3981 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003982 if (wbc->sync_mode == WB_SYNC_ALL)
3983 tag = PAGECACHE_TAG_TOWRITE;
3984 else
3985 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003986retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003987 if (wbc->sync_mode == WB_SYNC_ALL)
3988 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08003989 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003990 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003991 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3992 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003993 unsigned i;
3994
3995 scanned = 1;
3996 for (i = 0; i < nr_pages; i++) {
3997 struct page *page = pvec.pages[i];
3998
Liu Boa91326672016-03-07 16:56:21 -08003999 done_index = page->index;
Chris Masond1310b22008-01-24 16:13:08 -05004000 /*
4001 * At this point we hold neither mapping->tree_lock nor
4002 * lock on the page itself: the page may be truncated or
4003 * invalidated (changing page->mapping to NULL), or even
4004 * swizzled back from swapper_space to tmpfs file
4005 * mapping
4006 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05004007 if (!trylock_page(page)) {
4008 flush_fn(data);
4009 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04004010 }
Chris Masond1310b22008-01-24 16:13:08 -05004011
4012 if (unlikely(page->mapping != mapping)) {
4013 unlock_page(page);
4014 continue;
4015 }
4016
4017 if (!wbc->range_cyclic && page->index > end) {
4018 done = 1;
4019 unlock_page(page);
4020 continue;
4021 }
4022
Chris Masond2c3f4f2008-11-19 12:44:22 -05004023 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05004024 if (PageWriteback(page))
4025 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05004026 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004027 }
Chris Masond1310b22008-01-24 16:13:08 -05004028
4029 if (PageWriteback(page) ||
4030 !clear_page_dirty_for_io(page)) {
4031 unlock_page(page);
4032 continue;
4033 }
4034
4035 ret = (*writepage)(page, wbc, data);
4036
4037 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
4038 unlock_page(page);
4039 ret = 0;
4040 }
Liu Boa91326672016-03-07 16:56:21 -08004041 if (ret < 0) {
4042 /*
4043 * done_index is set past this page,
4044 * so media errors will not choke
4045 * background writeout for the entire
4046 * file. This has consequences for
4047 * range_cyclic semantics (ie. it may
4048 * not be suitable for data integrity
4049 * writeout).
4050 */
4051 done_index = page->index + 1;
4052 done = 1;
4053 break;
4054 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004055
4056 /*
4057 * the filesystem may choose to bump up nr_to_write.
4058 * We have to make sure to honor the new nr_to_write
4059 * at any time
4060 */
4061 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004062 }
4063 pagevec_release(&pvec);
4064 cond_resched();
4065 }
Liu Bo894b36e2016-03-07 16:56:22 -08004066 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004067 /*
4068 * We hit the last page and there is more work to be done: wrap
4069 * back to the start of the file
4070 */
4071 scanned = 1;
4072 index = 0;
4073 goto retry;
4074 }
Liu Boa91326672016-03-07 16:56:21 -08004075
4076 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4077 mapping->writeback_index = done_index;
4078
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004079 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004080 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004081}
Chris Masond1310b22008-01-24 16:13:08 -05004082
Chris Masonffbd5172009-04-20 15:50:09 -04004083static void flush_epd_write_bio(struct extent_page_data *epd)
4084{
4085 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04004086 int ret;
4087
Mike Christie1f7ad752016-06-05 14:31:51 -05004088 bio_set_op_attrs(epd->bio, REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -06004089 epd->sync_io ? REQ_SYNC : 0);
Jeff Mahoney355808c2011-10-03 23:23:14 -04004090
Mike Christie1f7ad752016-06-05 14:31:51 -05004091 ret = submit_one_bio(epd->bio, 0, epd->bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004092 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04004093 epd->bio = NULL;
4094 }
4095}
4096
Chris Masond2c3f4f2008-11-19 12:44:22 -05004097static noinline void flush_write_bio(void *data)
4098{
4099 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04004100 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004101}
4102
Chris Masond1310b22008-01-24 16:13:08 -05004103int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
4104 get_extent_t *get_extent,
4105 struct writeback_control *wbc)
4106{
4107 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004108 struct extent_page_data epd = {
4109 .bio = NULL,
4110 .tree = tree,
4111 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05004112 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004113 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04004114 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05004115 };
Chris Masond1310b22008-01-24 16:13:08 -05004116
Chris Masond1310b22008-01-24 16:13:08 -05004117 ret = __extent_writepage(page, wbc, &epd);
4118
Chris Masonffbd5172009-04-20 15:50:09 -04004119 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004120 return ret;
4121}
Chris Masond1310b22008-01-24 16:13:08 -05004122
Chris Mason771ed682008-11-06 22:02:51 -05004123int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
4124 u64 start, u64 end, get_extent_t *get_extent,
4125 int mode)
4126{
4127 int ret = 0;
4128 struct address_space *mapping = inode->i_mapping;
4129 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004130 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4131 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004132
4133 struct extent_page_data epd = {
4134 .bio = NULL,
4135 .tree = tree,
4136 .get_extent = get_extent,
4137 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004138 .sync_io = mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04004139 .bio_flags = 0,
Chris Mason771ed682008-11-06 22:02:51 -05004140 };
4141 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004142 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004143 .nr_to_write = nr_pages * 2,
4144 .range_start = start,
4145 .range_end = end + 1,
4146 };
4147
Chris Masond3977122009-01-05 21:25:51 -05004148 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004149 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004150 if (clear_page_dirty_for_io(page))
4151 ret = __extent_writepage(page, &wbc_writepages, &epd);
4152 else {
4153 if (tree->ops && tree->ops->writepage_end_io_hook)
4154 tree->ops->writepage_end_io_hook(page, start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004155 start + PAGE_SIZE - 1,
Chris Mason771ed682008-11-06 22:02:51 -05004156 NULL, 1);
4157 unlock_page(page);
4158 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004159 put_page(page);
4160 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004161 }
4162
Chris Masonffbd5172009-04-20 15:50:09 -04004163 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004164 return ret;
4165}
Chris Masond1310b22008-01-24 16:13:08 -05004166
4167int extent_writepages(struct extent_io_tree *tree,
4168 struct address_space *mapping,
4169 get_extent_t *get_extent,
4170 struct writeback_control *wbc)
4171{
4172 int ret = 0;
4173 struct extent_page_data epd = {
4174 .bio = NULL,
4175 .tree = tree,
4176 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05004177 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004178 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04004179 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05004180 };
4181
David Sterba4242b642017-02-10 19:38:24 +01004182 ret = extent_write_cache_pages(mapping, wbc, __extent_writepage, &epd,
Chris Masond2c3f4f2008-11-19 12:44:22 -05004183 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04004184 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004185 return ret;
4186}
Chris Masond1310b22008-01-24 16:13:08 -05004187
4188int extent_readpages(struct extent_io_tree *tree,
4189 struct address_space *mapping,
4190 struct list_head *pages, unsigned nr_pages,
4191 get_extent_t get_extent)
4192{
4193 struct bio *bio = NULL;
4194 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04004195 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004196 struct page *pagepool[16];
4197 struct page *page;
Miao Xie125bac012013-07-25 19:22:37 +08004198 struct extent_map *em_cached = NULL;
Liu Bo67c96842012-07-20 21:43:09 -06004199 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004200 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004201
Chris Masond1310b22008-01-24 16:13:08 -05004202 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06004203 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05004204
4205 prefetchw(&page->flags);
4206 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06004207 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07004208 page->index,
4209 readahead_gfp_mask(mapping))) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004210 put_page(page);
Liu Bo67c96842012-07-20 21:43:09 -06004211 continue;
Chris Masond1310b22008-01-24 16:13:08 -05004212 }
Liu Bo67c96842012-07-20 21:43:09 -06004213
4214 pagepool[nr++] = page;
4215 if (nr < ARRAY_SIZE(pagepool))
4216 continue;
Miao Xie125bac012013-07-25 19:22:37 +08004217 __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
Mike Christie1f7ad752016-06-05 14:31:51 -05004218 &bio, 0, &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004219 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004220 }
Miao Xie99740902013-07-25 19:22:36 +08004221 if (nr)
Miao Xie125bac012013-07-25 19:22:37 +08004222 __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
Mike Christie1f7ad752016-06-05 14:31:51 -05004223 &bio, 0, &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004224
Miao Xie125bac012013-07-25 19:22:37 +08004225 if (em_cached)
4226 free_extent_map(em_cached);
4227
Chris Masond1310b22008-01-24 16:13:08 -05004228 BUG_ON(!list_empty(pages));
4229 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004230 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004231 return 0;
4232}
Chris Masond1310b22008-01-24 16:13:08 -05004233
4234/*
4235 * basic invalidatepage code, this waits on any locked or writeback
4236 * ranges corresponding to the page, and then deletes any extent state
4237 * records from the tree
4238 */
4239int extent_invalidatepage(struct extent_io_tree *tree,
4240 struct page *page, unsigned long offset)
4241{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004242 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004243 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004244 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004245 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4246
Qu Wenruofda28322013-02-26 08:10:22 +00004247 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004248 if (start > end)
4249 return 0;
4250
David Sterbaff13db42015-12-03 14:30:40 +01004251 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004252 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004253 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004254 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4255 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004256 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004257 return 0;
4258}
Chris Masond1310b22008-01-24 16:13:08 -05004259
4260/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004261 * a helper for releasepage, this tests for areas of the page that
4262 * are locked or under IO and drops the related state bits if it is safe
4263 * to drop the page.
4264 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00004265static int try_release_extent_state(struct extent_map_tree *map,
4266 struct extent_io_tree *tree,
4267 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004268{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004269 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004270 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004271 int ret = 1;
4272
Chris Mason211f90e2008-07-18 11:56:15 -04004273 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04004274 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04004275 ret = 0;
4276 else {
Chris Mason11ef1602009-09-23 20:28:46 -04004277 /*
4278 * at this point we can safely clear everything except the
4279 * locked bit and the nodatasum bit
4280 */
Chris Masone3f24cc2011-02-14 12:52:08 -05004281 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004282 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
4283 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05004284
4285 /* if clear_extent_bit failed for enomem reasons,
4286 * we can't allow the release to continue.
4287 */
4288 if (ret < 0)
4289 ret = 0;
4290 else
4291 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004292 }
4293 return ret;
4294}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004295
4296/*
Chris Masond1310b22008-01-24 16:13:08 -05004297 * a helper for releasepage. As long as there are no locked extents
4298 * in the range corresponding to the page, both state records and extent
4299 * map records are removed
4300 */
4301int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05004302 struct extent_io_tree *tree, struct page *page,
4303 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004304{
4305 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004306 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004307 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004308
Mel Gormand0164ad2015-11-06 16:28:21 -08004309 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004310 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004311 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004312 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004313 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004314 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004315 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004316 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004317 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004318 break;
4319 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004320 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4321 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004322 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004323 free_extent_map(em);
4324 break;
4325 }
4326 if (!test_range_bit(tree, em->start,
4327 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04004328 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04004329 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05004330 remove_extent_mapping(map, em);
4331 /* once for the rb tree */
4332 free_extent_map(em);
4333 }
4334 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004335 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004336
4337 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004338 free_extent_map(em);
4339 }
Chris Masond1310b22008-01-24 16:13:08 -05004340 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04004341 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004342}
Chris Masond1310b22008-01-24 16:13:08 -05004343
Chris Masonec29ed52011-02-23 16:23:20 -05004344/*
4345 * helper function for fiemap, which doesn't want to see any holes.
4346 * This maps until we find something past 'last'
4347 */
4348static struct extent_map *get_extent_skip_holes(struct inode *inode,
4349 u64 offset,
4350 u64 last,
4351 get_extent_t *get_extent)
4352{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004353 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004354 struct extent_map *em;
4355 u64 len;
4356
4357 if (offset >= last)
4358 return NULL;
4359
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304360 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004361 len = last - offset;
4362 if (len == 0)
4363 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004364 len = ALIGN(len, sectorsize);
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004365 em = get_extent(BTRFS_I(inode), NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02004366 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004367 return em;
4368
4369 /* if this isn't a hole return it */
4370 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
4371 em->block_start != EXTENT_MAP_HOLE) {
4372 return em;
4373 }
4374
4375 /* this is a hole, advance to the next extent */
4376 offset = extent_map_end(em);
4377 free_extent_map(em);
4378 if (offset >= last)
4379 break;
4380 }
4381 return NULL;
4382}
4383
Qu Wenruo47518322017-04-07 10:43:15 +08004384/*
4385 * To cache previous fiemap extent
4386 *
4387 * Will be used for merging fiemap extent
4388 */
4389struct fiemap_cache {
4390 u64 offset;
4391 u64 phys;
4392 u64 len;
4393 u32 flags;
4394 bool cached;
4395};
4396
4397/*
4398 * Helper to submit fiemap extent.
4399 *
4400 * Will try to merge current fiemap extent specified by @offset, @phys,
4401 * @len and @flags with cached one.
4402 * And only when we fails to merge, cached one will be submitted as
4403 * fiemap extent.
4404 *
4405 * Return value is the same as fiemap_fill_next_extent().
4406 */
4407static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4408 struct fiemap_cache *cache,
4409 u64 offset, u64 phys, u64 len, u32 flags)
4410{
4411 int ret = 0;
4412
4413 if (!cache->cached)
4414 goto assign;
4415
4416 /*
4417 * Sanity check, extent_fiemap() should have ensured that new
4418 * fiemap extent won't overlap with cahced one.
4419 * Not recoverable.
4420 *
4421 * NOTE: Physical address can overlap, due to compression
4422 */
4423 if (cache->offset + cache->len > offset) {
4424 WARN_ON(1);
4425 return -EINVAL;
4426 }
4427
4428 /*
4429 * Only merges fiemap extents if
4430 * 1) Their logical addresses are continuous
4431 *
4432 * 2) Their physical addresses are continuous
4433 * So truly compressed (physical size smaller than logical size)
4434 * extents won't get merged with each other
4435 *
4436 * 3) Share same flags except FIEMAP_EXTENT_LAST
4437 * So regular extent won't get merged with prealloc extent
4438 */
4439 if (cache->offset + cache->len == offset &&
4440 cache->phys + cache->len == phys &&
4441 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4442 (flags & ~FIEMAP_EXTENT_LAST)) {
4443 cache->len += len;
4444 cache->flags |= flags;
4445 goto try_submit_last;
4446 }
4447
4448 /* Not mergeable, need to submit cached one */
4449 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4450 cache->len, cache->flags);
4451 cache->cached = false;
4452 if (ret)
4453 return ret;
4454assign:
4455 cache->cached = true;
4456 cache->offset = offset;
4457 cache->phys = phys;
4458 cache->len = len;
4459 cache->flags = flags;
4460try_submit_last:
4461 if (cache->flags & FIEMAP_EXTENT_LAST) {
4462 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4463 cache->phys, cache->len, cache->flags);
4464 cache->cached = false;
4465 }
4466 return ret;
4467}
4468
4469/*
4470 * Sanity check for fiemap cache
4471 *
4472 * All fiemap cache should be submitted by emit_fiemap_extent()
4473 * Iteration should be terminated either by last fiemap extent or
4474 * fieinfo->fi_extents_max.
4475 * So no cached fiemap should exist.
4476 */
4477static int check_fiemap_cache(struct btrfs_fs_info *fs_info,
4478 struct fiemap_extent_info *fieinfo,
4479 struct fiemap_cache *cache)
4480{
4481 int ret;
4482
4483 if (!cache->cached)
4484 return 0;
4485
4486 /* Small and recoverbale problem, only to info developer */
4487#ifdef CONFIG_BTRFS_DEBUG
4488 WARN_ON(1);
4489#endif
4490 btrfs_warn(fs_info,
4491 "unhandled fiemap cache detected: offset=%llu phys=%llu len=%llu flags=0x%x",
4492 cache->offset, cache->phys, cache->len, cache->flags);
4493 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4494 cache->len, cache->flags);
4495 cache->cached = false;
4496 if (ret > 0)
4497 ret = 0;
4498 return ret;
4499}
4500
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004501int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4502 __u64 start, __u64 len, get_extent_t *get_extent)
4503{
Josef Bacik975f84f2010-11-23 19:36:57 +00004504 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004505 u64 off = start;
4506 u64 max = start + len;
4507 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004508 u32 found_type;
4509 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004510 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004511 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004512 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004513 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004514 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004515 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004516 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004517 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004518 struct fiemap_cache cache = { 0 };
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004519 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004520 u64 em_start = 0;
4521 u64 em_len = 0;
4522 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004523
4524 if (len == 0)
4525 return -EINVAL;
4526
Josef Bacik975f84f2010-11-23 19:36:57 +00004527 path = btrfs_alloc_path();
4528 if (!path)
4529 return -ENOMEM;
4530 path->leave_spinning = 1;
4531
Jeff Mahoneyda170662016-06-15 09:22:56 -04004532 start = round_down(start, btrfs_inode_sectorsize(inode));
4533 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004534
Chris Masonec29ed52011-02-23 16:23:20 -05004535 /*
4536 * lookup the last file extent. We're not using i_size here
4537 * because there might be preallocation past i_size
4538 */
David Sterbaf85b7372017-01-20 14:54:07 +01004539 ret = btrfs_lookup_file_extent(NULL, root, path,
4540 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004541 if (ret < 0) {
4542 btrfs_free_path(path);
4543 return ret;
Liu Bo2d324f52016-05-17 17:21:48 -07004544 } else {
4545 WARN_ON(!ret);
4546 if (ret == 1)
4547 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004548 }
Liu Bo2d324f52016-05-17 17:21:48 -07004549
Josef Bacik975f84f2010-11-23 19:36:57 +00004550 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004551 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004552 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004553
Chris Masonec29ed52011-02-23 16:23:20 -05004554 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004555 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004556 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004557 /* have to trust i_size as the end */
4558 last = (u64)-1;
4559 last_for_get_extent = isize;
4560 } else {
4561 /*
4562 * remember the start of the last extent. There are a
4563 * bunch of different factors that go into the length of the
4564 * extent, so its much less complex to remember where it started
4565 */
4566 last = found_key.offset;
4567 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004568 }
Liu Bofe09e162013-09-22 12:54:23 +08004569 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004570
Chris Masonec29ed52011-02-23 16:23:20 -05004571 /*
4572 * we might have some extents allocated but more delalloc past those
4573 * extents. so, we trust isize unless the start of the last extent is
4574 * beyond isize
4575 */
4576 if (last < isize) {
4577 last = (u64)-1;
4578 last_for_get_extent = isize;
4579 }
4580
David Sterbaff13db42015-12-03 14:30:40 +01004581 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004582 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004583
Josef Bacik4d479cf2011-11-17 11:34:31 -05004584 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05004585 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004586 if (!em)
4587 goto out;
4588 if (IS_ERR(em)) {
4589 ret = PTR_ERR(em);
4590 goto out;
4591 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004592
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004593 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004594 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004595
Chris Masonea8efc72011-03-08 11:54:40 -05004596 /* break if the extent we found is outside the range */
4597 if (em->start >= max || extent_map_end(em) < off)
4598 break;
4599
4600 /*
4601 * get_extent may return an extent that starts before our
4602 * requested range. We have to make sure the ranges
4603 * we return to fiemap always move forward and don't
4604 * overlap, so adjust the offsets here
4605 */
4606 em_start = max(em->start, off);
4607
4608 /*
4609 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004610 * for adjusting the disk offset below. Only do this if the
4611 * extent isn't compressed since our in ram offset may be past
4612 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004613 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004614 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4615 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004616 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004617 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004618 disko = 0;
4619 flags = 0;
4620
Chris Masonea8efc72011-03-08 11:54:40 -05004621 /*
4622 * bump off for our next call to get_extent
4623 */
4624 off = extent_map_end(em);
4625 if (off >= max)
4626 end = 1;
4627
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004628 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004629 end = 1;
4630 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004631 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004632 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4633 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004634 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004635 flags |= (FIEMAP_EXTENT_DELALLOC |
4636 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004637 } else if (fieinfo->fi_extents_max) {
Lu Fengqiafce7722016-06-13 09:36:46 +08004638 struct btrfs_trans_handle *trans;
4639
Josef Bacikdc046b12014-09-10 16:20:45 -04004640 u64 bytenr = em->block_start -
4641 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004642
Chris Masonea8efc72011-03-08 11:54:40 -05004643 disko = em->block_start + offset_in_extent;
Liu Bofe09e162013-09-22 12:54:23 +08004644
4645 /*
Lu Fengqiafce7722016-06-13 09:36:46 +08004646 * We need a trans handle to get delayed refs
4647 */
4648 trans = btrfs_join_transaction(root);
4649 /*
4650 * It's OK if we can't start a trans we can still check
4651 * from commit_root
4652 */
4653 if (IS_ERR(trans))
4654 trans = NULL;
4655
4656 /*
Liu Bofe09e162013-09-22 12:54:23 +08004657 * As btrfs supports shared space, this information
4658 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004659 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4660 * then we're just getting a count and we can skip the
4661 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004662 */
Lu Fengqiafce7722016-06-13 09:36:46 +08004663 ret = btrfs_check_shared(trans, root->fs_info,
David Sterbaf85b7372017-01-20 14:54:07 +01004664 root->objectid,
4665 btrfs_ino(BTRFS_I(inode)), bytenr);
Lu Fengqiafce7722016-06-13 09:36:46 +08004666 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004667 btrfs_end_transaction(trans);
Josef Bacikdc046b12014-09-10 16:20:45 -04004668 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004669 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004670 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004671 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004672 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004673 }
4674 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4675 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004676 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4677 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004678
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004679 free_extent_map(em);
4680 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004681 if ((em_start >= last) || em_len == (u64)-1 ||
4682 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004683 flags |= FIEMAP_EXTENT_LAST;
4684 end = 1;
4685 }
4686
Chris Masonec29ed52011-02-23 16:23:20 -05004687 /* now scan forward to see if this is really the last extent. */
4688 em = get_extent_skip_holes(inode, off, last_for_get_extent,
4689 get_extent);
4690 if (IS_ERR(em)) {
4691 ret = PTR_ERR(em);
4692 goto out;
4693 }
4694 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004695 flags |= FIEMAP_EXTENT_LAST;
4696 end = 1;
4697 }
Qu Wenruo47518322017-04-07 10:43:15 +08004698 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4699 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004700 if (ret) {
4701 if (ret == 1)
4702 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004703 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004704 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004705 }
4706out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004707 if (!ret)
4708 ret = check_fiemap_cache(root->fs_info, fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004709 free_extent_map(em);
4710out:
Liu Bofe09e162013-09-22 12:54:23 +08004711 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004712 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004713 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004714 return ret;
4715}
4716
Chris Mason727011e2010-08-06 13:21:20 -04004717static void __free_extent_buffer(struct extent_buffer *eb)
4718{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004719 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004720 kmem_cache_free(extent_buffer_cache, eb);
4721}
4722
Josef Bacika26e8c92014-03-28 17:07:27 -04004723int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004724{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004725 return (atomic_read(&eb->io_pages) ||
4726 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4727 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004728}
4729
Miao Xie897ca6e92010-10-26 20:57:29 -04004730/*
4731 * Helper for releasing extent buffer page.
4732 */
David Sterbaa50924e2014-07-31 00:51:36 +02004733static void btrfs_release_extent_buffer_page(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004734{
4735 unsigned long index;
4736 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004737 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004738
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004739 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004740
David Sterbaa50924e2014-07-31 00:51:36 +02004741 index = num_extent_pages(eb->start, eb->len);
4742 if (index == 0)
Miao Xie897ca6e92010-10-26 20:57:29 -04004743 return;
4744
4745 do {
4746 index--;
David Sterbafb85fc92014-07-31 01:03:53 +02004747 page = eb->pages[index];
Forrest Liu5d2361d2015-02-09 17:31:45 +08004748 if (!page)
4749 continue;
4750 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004751 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004752 /*
4753 * We do this since we'll remove the pages after we've
4754 * removed the eb from the radix tree, so we could race
4755 * and have this page now attached to the new eb. So
4756 * only clear page_private if it's still connected to
4757 * this eb.
4758 */
4759 if (PagePrivate(page) &&
4760 page->private == (unsigned long)eb) {
4761 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4762 BUG_ON(PageDirty(page));
4763 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004764 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004765 * We need to make sure we haven't be attached
4766 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004767 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004768 ClearPagePrivate(page);
4769 set_page_private(page, 0);
4770 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004771 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004772 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004773
4774 if (mapped)
4775 spin_unlock(&page->mapping->private_lock);
4776
Nicholas D Steeves01327612016-05-19 21:18:45 -04004777 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004778 put_page(page);
David Sterbaa50924e2014-07-31 00:51:36 +02004779 } while (index != 0);
Miao Xie897ca6e92010-10-26 20:57:29 -04004780}
4781
4782/*
4783 * Helper for releasing the extent buffer.
4784 */
4785static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4786{
David Sterbaa50924e2014-07-31 00:51:36 +02004787 btrfs_release_extent_buffer_page(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004788 __free_extent_buffer(eb);
4789}
4790
Josef Bacikf28491e2013-12-16 13:24:27 -05004791static struct extent_buffer *
4792__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004793 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004794{
4795 struct extent_buffer *eb = NULL;
4796
Michal Hockod1b5c562015-08-19 14:17:40 +02004797 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004798 eb->start = start;
4799 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004800 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004801 eb->bflags = 0;
4802 rwlock_init(&eb->lock);
4803 atomic_set(&eb->write_locks, 0);
4804 atomic_set(&eb->read_locks, 0);
4805 atomic_set(&eb->blocking_readers, 0);
4806 atomic_set(&eb->blocking_writers, 0);
4807 atomic_set(&eb->spinning_readers, 0);
4808 atomic_set(&eb->spinning_writers, 0);
4809 eb->lock_nested = 0;
4810 init_waitqueue_head(&eb->write_lock_wq);
4811 init_waitqueue_head(&eb->read_lock_wq);
4812
4813 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4814
4815 spin_lock_init(&eb->refs_lock);
4816 atomic_set(&eb->refs, 1);
4817 atomic_set(&eb->io_pages, 0);
4818
4819 /*
4820 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4821 */
4822 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4823 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4824 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4825
4826 return eb;
4827}
4828
4829struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4830{
4831 unsigned long i;
4832 struct page *p;
4833 struct extent_buffer *new;
4834 unsigned long num_pages = num_extent_pages(src->start, src->len);
4835
David Sterba3f556f72014-06-15 03:20:26 +02004836 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004837 if (new == NULL)
4838 return NULL;
4839
4840 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004841 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004842 if (!p) {
4843 btrfs_release_extent_buffer(new);
4844 return NULL;
4845 }
4846 attach_extent_buffer_page(new, p);
4847 WARN_ON(PageDirty(p));
4848 SetPageUptodate(p);
4849 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004850 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004851 }
4852
Josef Bacikdb7f3432013-08-07 14:54:37 -04004853 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4854 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4855
4856 return new;
4857}
4858
Omar Sandoval0f331222015-09-29 20:50:31 -07004859struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4860 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004861{
4862 struct extent_buffer *eb;
David Sterba3f556f72014-06-15 03:20:26 +02004863 unsigned long num_pages;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004864 unsigned long i;
4865
Omar Sandoval0f331222015-09-29 20:50:31 -07004866 num_pages = num_extent_pages(start, len);
David Sterba3f556f72014-06-15 03:20:26 +02004867
4868 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004869 if (!eb)
4870 return NULL;
4871
4872 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004873 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004874 if (!eb->pages[i])
4875 goto err;
4876 }
4877 set_extent_buffer_uptodate(eb);
4878 btrfs_set_header_nritems(eb, 0);
4879 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4880
4881 return eb;
4882err:
4883 for (; i > 0; i--)
4884 __free_page(eb->pages[i - 1]);
4885 __free_extent_buffer(eb);
4886 return NULL;
4887}
4888
Omar Sandoval0f331222015-09-29 20:50:31 -07004889struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004890 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004891{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004892 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004893}
4894
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004895static void check_buffer_tree_ref(struct extent_buffer *eb)
4896{
Chris Mason242e18c2013-01-29 17:49:37 -05004897 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004898 /* the ref bit is tricky. We have to make sure it is set
4899 * if we have the buffer dirty. Otherwise the
4900 * code to free a buffer can end up dropping a dirty
4901 * page
4902 *
4903 * Once the ref bit is set, it won't go away while the
4904 * buffer is dirty or in writeback, and it also won't
4905 * go away while we have the reference count on the
4906 * eb bumped.
4907 *
4908 * We can't just set the ref bit without bumping the
4909 * ref on the eb because free_extent_buffer might
4910 * see the ref bit and try to clear it. If this happens
4911 * free_extent_buffer might end up dropping our original
4912 * ref by mistake and freeing the page before we are able
4913 * to add one more ref.
4914 *
4915 * So bump the ref count first, then set the bit. If someone
4916 * beat us to it, drop the ref we added.
4917 */
Chris Mason242e18c2013-01-29 17:49:37 -05004918 refs = atomic_read(&eb->refs);
4919 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4920 return;
4921
Josef Bacik594831c2012-07-20 16:11:08 -04004922 spin_lock(&eb->refs_lock);
4923 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004924 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004925 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004926}
4927
Mel Gorman2457aec2014-06-04 16:10:31 -07004928static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4929 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004930{
4931 unsigned long num_pages, i;
4932
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004933 check_buffer_tree_ref(eb);
4934
Josef Bacik5df42352012-03-15 18:24:42 -04004935 num_pages = num_extent_pages(eb->start, eb->len);
4936 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004937 struct page *p = eb->pages[i];
4938
Mel Gorman2457aec2014-06-04 16:10:31 -07004939 if (p != accessed)
4940 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004941 }
4942}
4943
Josef Bacikf28491e2013-12-16 13:24:27 -05004944struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4945 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004946{
4947 struct extent_buffer *eb;
4948
4949 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004950 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004951 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004952 if (eb && atomic_inc_not_zero(&eb->refs)) {
4953 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004954 /*
4955 * Lock our eb's refs_lock to avoid races with
4956 * free_extent_buffer. When we get our eb it might be flagged
4957 * with EXTENT_BUFFER_STALE and another task running
4958 * free_extent_buffer might have seen that flag set,
4959 * eb->refs == 2, that the buffer isn't under IO (dirty and
4960 * writeback flags not set) and it's still in the tree (flag
4961 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4962 * of decrementing the extent buffer's reference count twice.
4963 * So here we could race and increment the eb's reference count,
4964 * clear its stale flag, mark it as dirty and drop our reference
4965 * before the other task finishes executing free_extent_buffer,
4966 * which would later result in an attempt to free an extent
4967 * buffer that is dirty.
4968 */
4969 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4970 spin_lock(&eb->refs_lock);
4971 spin_unlock(&eb->refs_lock);
4972 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004973 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004974 return eb;
4975 }
4976 rcu_read_unlock();
4977
4978 return NULL;
4979}
4980
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004981#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4982struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004983 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004984{
4985 struct extent_buffer *eb, *exists = NULL;
4986 int ret;
4987
4988 eb = find_extent_buffer(fs_info, start);
4989 if (eb)
4990 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04004991 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004992 if (!eb)
4993 return NULL;
4994 eb->fs_info = fs_info;
4995again:
David Sterbae1860a72016-05-09 14:11:38 +02004996 ret = radix_tree_preload(GFP_NOFS);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004997 if (ret)
4998 goto free_eb;
4999 spin_lock(&fs_info->buffer_lock);
5000 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005001 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005002 spin_unlock(&fs_info->buffer_lock);
5003 radix_tree_preload_end();
5004 if (ret == -EEXIST) {
5005 exists = find_extent_buffer(fs_info, start);
5006 if (exists)
5007 goto free_eb;
5008 else
5009 goto again;
5010 }
5011 check_buffer_tree_ref(eb);
5012 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
5013
5014 /*
5015 * We will free dummy extent buffer's if they come into
5016 * free_extent_buffer with a ref count of 2, but if we are using this we
5017 * want the buffers to stay in memory until we're done with them, so
5018 * bump the ref count again.
5019 */
5020 atomic_inc(&eb->refs);
5021 return eb;
5022free_eb:
5023 btrfs_release_extent_buffer(eb);
5024 return exists;
5025}
5026#endif
5027
Josef Bacikf28491e2013-12-16 13:24:27 -05005028struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02005029 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05005030{
Jeff Mahoneyda170662016-06-15 09:22:56 -04005031 unsigned long len = fs_info->nodesize;
Chris Masond1310b22008-01-24 16:13:08 -05005032 unsigned long num_pages = num_extent_pages(start, len);
5033 unsigned long i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005034 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005035 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04005036 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05005037 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05005038 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05005039 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005040 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05005041
Jeff Mahoneyda170662016-06-15 09:22:56 -04005042 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07005043 btrfs_err(fs_info, "bad tree block start %llu", start);
5044 return ERR_PTR(-EINVAL);
5045 }
5046
Josef Bacikf28491e2013-12-16 13:24:27 -05005047 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005048 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04005049 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04005050
David Sterba23d79d82014-06-15 02:55:29 +02005051 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04005052 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07005053 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05005054
Chris Mason727011e2010-08-06 13:21:20 -04005055 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02005056 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07005057 if (!p) {
5058 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04005059 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005060 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005061
5062 spin_lock(&mapping->private_lock);
5063 if (PagePrivate(p)) {
5064 /*
5065 * We could have already allocated an eb for this page
5066 * and attached one so lets see if we can get a ref on
5067 * the existing eb, and if we can we know it's good and
5068 * we can just return that one, else we know we can just
5069 * overwrite page->private.
5070 */
5071 exists = (struct extent_buffer *)p->private;
5072 if (atomic_inc_not_zero(&exists->refs)) {
5073 spin_unlock(&mapping->private_lock);
5074 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005075 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07005076 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005077 goto free_eb;
5078 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005079 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005080
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005081 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005082 * Do this so attach doesn't complain and we need to
5083 * drop the ref the old guy had.
5084 */
5085 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005086 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005087 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05005088 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005089 attach_extent_buffer_page(eb, p);
5090 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005091 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04005092 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05005093 if (!PageUptodate(p))
5094 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05005095
5096 /*
5097 * see below about how we avoid a nasty race with release page
5098 * and why we unlock later
5099 */
Chris Masond1310b22008-01-24 16:13:08 -05005100 }
5101 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05005102 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05005103again:
David Sterbae1860a72016-05-09 14:11:38 +02005104 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07005105 if (ret) {
5106 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04005107 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005108 }
Miao Xie19fe0a82010-10-26 20:57:29 -04005109
Josef Bacikf28491e2013-12-16 13:24:27 -05005110 spin_lock(&fs_info->buffer_lock);
5111 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005112 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05005113 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005114 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04005115 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005116 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005117 if (exists)
5118 goto free_eb;
5119 else
Josef Bacik115391d2012-03-09 09:51:43 -05005120 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04005121 }
Chris Mason6af118ce2008-07-22 11:18:07 -04005122 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005123 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005124 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005125
5126 /*
5127 * there is a race where release page may have
5128 * tried to find this extent buffer in the radix
5129 * but failed. It will tell the VM it is safe to
5130 * reclaim the, and it will clear the page private bit.
5131 * We must make sure to set the page private bit properly
5132 * after the extent buffer is in the radix tree so
5133 * it doesn't get lost
5134 */
Chris Mason727011e2010-08-06 13:21:20 -04005135 SetPageChecked(eb->pages[0]);
5136 for (i = 1; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005137 p = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005138 ClearPageChecked(p);
5139 unlock_page(p);
5140 }
5141 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05005142 return eb;
5143
Chris Mason6af118ce2008-07-22 11:18:07 -04005144free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005145 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005146 for (i = 0; i < num_pages; i++) {
5147 if (eb->pages[i])
5148 unlock_page(eb->pages[i]);
5149 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005150
Miao Xie897ca6e92010-10-26 20:57:29 -04005151 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005152 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005153}
Chris Masond1310b22008-01-24 16:13:08 -05005154
Josef Bacik3083ee22012-03-09 16:01:49 -05005155static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5156{
5157 struct extent_buffer *eb =
5158 container_of(head, struct extent_buffer, rcu_head);
5159
5160 __free_extent_buffer(eb);
5161}
5162
Josef Bacik3083ee22012-03-09 16:01:49 -05005163/* Expects to have eb->eb_lock already held */
David Sterbaf7a52a42013-04-26 14:56:29 +00005164static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005165{
5166 WARN_ON(atomic_read(&eb->refs) == 0);
5167 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005168 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005169 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005170
Jan Schmidt815a51c2012-05-16 17:00:02 +02005171 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005172
Josef Bacikf28491e2013-12-16 13:24:27 -05005173 spin_lock(&fs_info->buffer_lock);
5174 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005175 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005176 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005177 } else {
5178 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005179 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005180
5181 /* Should be safe to release our pages at this point */
David Sterbaa50924e2014-07-31 00:51:36 +02005182 btrfs_release_extent_buffer_page(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005183#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
5184 if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) {
5185 __free_extent_buffer(eb);
5186 return 1;
5187 }
5188#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005189 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005190 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005191 }
5192 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005193
5194 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005195}
5196
Chris Masond1310b22008-01-24 16:13:08 -05005197void free_extent_buffer(struct extent_buffer *eb)
5198{
Chris Mason242e18c2013-01-29 17:49:37 -05005199 int refs;
5200 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005201 if (!eb)
5202 return;
5203
Chris Mason242e18c2013-01-29 17:49:37 -05005204 while (1) {
5205 refs = atomic_read(&eb->refs);
5206 if (refs <= 3)
5207 break;
5208 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5209 if (old == refs)
5210 return;
5211 }
5212
Josef Bacik3083ee22012-03-09 16:01:49 -05005213 spin_lock(&eb->refs_lock);
5214 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02005215 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
5216 atomic_dec(&eb->refs);
5217
5218 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005219 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005220 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005221 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5222 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005223
Josef Bacik3083ee22012-03-09 16:01:49 -05005224 /*
5225 * I know this is terrible, but it's temporary until we stop tracking
5226 * the uptodate bits and such for the extent buffers.
5227 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005228 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005229}
Chris Masond1310b22008-01-24 16:13:08 -05005230
Josef Bacik3083ee22012-03-09 16:01:49 -05005231void free_extent_buffer_stale(struct extent_buffer *eb)
5232{
5233 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005234 return;
5235
Josef Bacik3083ee22012-03-09 16:01:49 -05005236 spin_lock(&eb->refs_lock);
5237 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5238
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005239 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005240 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5241 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005242 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005243}
5244
Chris Mason1d4284b2012-03-28 20:31:37 -04005245void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005246{
Chris Masond1310b22008-01-24 16:13:08 -05005247 unsigned long i;
5248 unsigned long num_pages;
5249 struct page *page;
5250
Chris Masond1310b22008-01-24 16:13:08 -05005251 num_pages = num_extent_pages(eb->start, eb->len);
5252
5253 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005254 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005255 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005256 continue;
5257
Chris Masona61e6f22008-07-22 11:18:08 -04005258 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005259 WARN_ON(!PagePrivate(page));
5260
Chris Masond1310b22008-01-24 16:13:08 -05005261 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005262 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05005263 if (!PageDirty(page)) {
5264 radix_tree_tag_clear(&page->mapping->page_tree,
5265 page_index(page),
5266 PAGECACHE_TAG_DIRTY);
5267 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005268 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005269 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005270 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005271 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005272 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005273}
Chris Masond1310b22008-01-24 16:13:08 -05005274
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005275int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005276{
5277 unsigned long i;
5278 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04005279 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005280
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005281 check_buffer_tree_ref(eb);
5282
Chris Masonb9473432009-03-13 11:00:37 -04005283 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005284
Chris Masond1310b22008-01-24 16:13:08 -05005285 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05005286 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005287 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5288
Chris Masonb9473432009-03-13 11:00:37 -04005289 for (i = 0; i < num_pages; i++)
David Sterbafb85fc92014-07-31 01:03:53 +02005290 set_page_dirty(eb->pages[i]);
Chris Masonb9473432009-03-13 11:00:37 -04005291 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005292}
Chris Masond1310b22008-01-24 16:13:08 -05005293
David Sterba69ba3922015-12-03 13:08:59 +01005294void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005295{
5296 unsigned long i;
5297 struct page *page;
5298 unsigned long num_pages;
5299
Chris Masonb4ce94d2009-02-04 09:25:08 -05005300 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005301 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04005302 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005303 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005304 if (page)
5305 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005306 }
Chris Mason1259ab72008-05-12 13:39:03 -04005307}
5308
David Sterba09c25a82015-12-03 13:08:59 +01005309void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005310{
5311 unsigned long i;
5312 struct page *page;
5313 unsigned long num_pages;
5314
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005315 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05005316 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05005317 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005318 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005319 SetPageUptodate(page);
5320 }
Chris Masond1310b22008-01-24 16:13:08 -05005321}
Chris Masond1310b22008-01-24 16:13:08 -05005322
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005323int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005324{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005325 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05005326}
Chris Masond1310b22008-01-24 16:13:08 -05005327
5328int read_extent_buffer_pages(struct extent_io_tree *tree,
Josef Bacik8436ea912016-09-02 15:40:03 -04005329 struct extent_buffer *eb, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04005330 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005331{
5332 unsigned long i;
Chris Masond1310b22008-01-24 16:13:08 -05005333 struct page *page;
5334 int err;
5335 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005336 int locked_pages = 0;
5337 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05005338 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005339 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005340 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005341 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005342
Chris Masonb4ce94d2009-02-04 09:25:08 -05005343 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005344 return 0;
5345
Chris Masond1310b22008-01-24 16:13:08 -05005346 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik8436ea912016-09-02 15:40:03 -04005347 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005348 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005349 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005350 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005351 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005352 } else {
5353 lock_page(page);
5354 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005355 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005356 }
5357 /*
5358 * We need to firstly lock all pages to make sure that
5359 * the uptodate bit of our pages won't be affected by
5360 * clear_extent_buffer_uptodate().
5361 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005362 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005363 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005364 if (!PageUptodate(page)) {
5365 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005366 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005367 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005368 }
Liu Bo2571e732016-08-03 12:33:01 -07005369
Chris Masonce9adaa2008-04-09 16:28:12 -04005370 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005371 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005372 goto unlock_exit;
5373 }
5374
Filipe Manana656f30d2014-09-26 12:25:56 +01005375 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005376 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005377 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005378 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005379 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005380
Chris Masonce9adaa2008-04-09 16:28:12 -04005381 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005382 if (ret) {
5383 atomic_dec(&eb->io_pages);
5384 unlock_page(page);
5385 continue;
5386 }
5387
Chris Masonf1885912008-04-09 16:28:12 -04005388 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005389 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04005390 get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005391 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005392 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005393 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005394 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005395 /*
5396 * We use &bio in above __extent_read_full_page,
5397 * so we ensure that if it returns error, the
5398 * current page fails to add itself to bio and
5399 * it's been unlocked.
5400 *
5401 * We must dec io_pages by ourselves.
5402 */
5403 atomic_dec(&eb->io_pages);
5404 }
Chris Masond1310b22008-01-24 16:13:08 -05005405 } else {
5406 unlock_page(page);
5407 }
5408 }
5409
Jeff Mahoney355808c2011-10-03 23:23:14 -04005410 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005411 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005412 if (err)
5413 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005414 }
Chris Masona86c12c2008-02-07 10:50:54 -05005415
Arne Jansenbb82ab82011-06-10 14:06:53 +02005416 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005417 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005418
Josef Bacik8436ea912016-09-02 15:40:03 -04005419 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005420 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005421 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005422 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005423 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005424 }
Chris Masond3977122009-01-05 21:25:51 -05005425
Chris Masond1310b22008-01-24 16:13:08 -05005426 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005427
5428unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005429 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005430 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005431 page = eb->pages[locked_pages];
5432 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005433 }
5434 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005435}
Chris Masond1310b22008-01-24 16:13:08 -05005436
5437void read_extent_buffer(struct extent_buffer *eb, void *dstv,
5438 unsigned long start,
5439 unsigned long len)
5440{
5441 size_t cur;
5442 size_t offset;
5443 struct page *page;
5444 char *kaddr;
5445 char *dst = (char *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005446 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5447 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005448
5449 WARN_ON(start > eb->len);
5450 WARN_ON(start + len > eb->start + eb->len);
5451
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005452 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005453
Chris Masond3977122009-01-05 21:25:51 -05005454 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005455 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005456
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005457 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005458 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005459 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005460
5461 dst += cur;
5462 len -= cur;
5463 offset = 0;
5464 i++;
5465 }
5466}
Chris Masond1310b22008-01-24 16:13:08 -05005467
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005468int read_extent_buffer_to_user(struct extent_buffer *eb, void __user *dstv,
5469 unsigned long start,
5470 unsigned long len)
5471{
5472 size_t cur;
5473 size_t offset;
5474 struct page *page;
5475 char *kaddr;
5476 char __user *dst = (char __user *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005477 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5478 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005479 int ret = 0;
5480
5481 WARN_ON(start > eb->len);
5482 WARN_ON(start + len > eb->start + eb->len);
5483
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005484 offset = (start_offset + start) & (PAGE_SIZE - 1);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005485
5486 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005487 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005488
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005489 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005490 kaddr = page_address(page);
5491 if (copy_to_user(dst, kaddr + offset, cur)) {
5492 ret = -EFAULT;
5493 break;
5494 }
5495
5496 dst += cur;
5497 len -= cur;
5498 offset = 0;
5499 i++;
5500 }
5501
5502 return ret;
5503}
5504
Liu Bo415b35a2016-06-17 19:16:21 -07005505/*
5506 * return 0 if the item is found within a page.
5507 * return 1 if the item spans two pages.
5508 * return -EINVAL otherwise.
5509 */
Chris Masond1310b22008-01-24 16:13:08 -05005510int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04005511 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05005512 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04005513 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005514{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005515 size_t offset = start & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005516 char *kaddr;
5517 struct page *p;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005518 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5519 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005520 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005521 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005522
5523 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005524 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005525
5526 if (i == 0) {
5527 offset = start_offset;
5528 *map_start = 0;
5529 } else {
5530 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005531 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005532 }
Chris Masond3977122009-01-05 21:25:51 -05005533
Chris Masond1310b22008-01-24 16:13:08 -05005534 if (start + min_len > eb->len) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005535 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005536 eb->start, eb->len, start, min_len);
Josef Bacik850265332011-03-15 14:52:12 -04005537 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05005538 }
5539
David Sterbafb85fc92014-07-31 01:03:53 +02005540 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005541 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005542 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005543 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005544 return 0;
5545}
Chris Masond1310b22008-01-24 16:13:08 -05005546
Chris Masond1310b22008-01-24 16:13:08 -05005547int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
5548 unsigned long start,
5549 unsigned long len)
5550{
5551 size_t cur;
5552 size_t offset;
5553 struct page *page;
5554 char *kaddr;
5555 char *ptr = (char *)ptrv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005556 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5557 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005558 int ret = 0;
5559
5560 WARN_ON(start > eb->len);
5561 WARN_ON(start + len > eb->start + eb->len);
5562
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005563 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005564
Chris Masond3977122009-01-05 21:25:51 -05005565 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005566 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005567
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005568 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005569
Chris Masona6591712011-07-19 12:04:14 -04005570 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005571 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005572 if (ret)
5573 break;
5574
5575 ptr += cur;
5576 len -= cur;
5577 offset = 0;
5578 i++;
5579 }
5580 return ret;
5581}
Chris Masond1310b22008-01-24 16:13:08 -05005582
David Sterbaf157bf72016-11-09 17:43:38 +01005583void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5584 const void *srcv)
5585{
5586 char *kaddr;
5587
5588 WARN_ON(!PageUptodate(eb->pages[0]));
5589 kaddr = page_address(eb->pages[0]);
5590 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5591 BTRFS_FSID_SIZE);
5592}
5593
5594void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5595{
5596 char *kaddr;
5597
5598 WARN_ON(!PageUptodate(eb->pages[0]));
5599 kaddr = page_address(eb->pages[0]);
5600 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5601 BTRFS_FSID_SIZE);
5602}
5603
Chris Masond1310b22008-01-24 16:13:08 -05005604void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5605 unsigned long start, unsigned long len)
5606{
5607 size_t cur;
5608 size_t offset;
5609 struct page *page;
5610 char *kaddr;
5611 char *src = (char *)srcv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005612 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5613 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005614
5615 WARN_ON(start > eb->len);
5616 WARN_ON(start + len > eb->start + eb->len);
5617
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005618 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005619
Chris Masond3977122009-01-05 21:25:51 -05005620 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005621 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005622 WARN_ON(!PageUptodate(page));
5623
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005624 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005625 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005626 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005627
5628 src += cur;
5629 len -= cur;
5630 offset = 0;
5631 i++;
5632 }
5633}
Chris Masond1310b22008-01-24 16:13:08 -05005634
David Sterbab159fa22016-11-08 18:09:03 +01005635void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5636 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005637{
5638 size_t cur;
5639 size_t offset;
5640 struct page *page;
5641 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005642 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5643 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005644
5645 WARN_ON(start > eb->len);
5646 WARN_ON(start + len > eb->start + eb->len);
5647
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005648 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005649
Chris Masond3977122009-01-05 21:25:51 -05005650 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005651 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005652 WARN_ON(!PageUptodate(page));
5653
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005654 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005655 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005656 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005657
5658 len -= cur;
5659 offset = 0;
5660 i++;
5661 }
5662}
Chris Masond1310b22008-01-24 16:13:08 -05005663
David Sterba58e80122016-11-08 18:30:31 +01005664void copy_extent_buffer_full(struct extent_buffer *dst,
5665 struct extent_buffer *src)
5666{
5667 int i;
5668 unsigned num_pages;
5669
5670 ASSERT(dst->len == src->len);
5671
5672 num_pages = num_extent_pages(dst->start, dst->len);
5673 for (i = 0; i < num_pages; i++)
5674 copy_page(page_address(dst->pages[i]),
5675 page_address(src->pages[i]));
5676}
5677
Chris Masond1310b22008-01-24 16:13:08 -05005678void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5679 unsigned long dst_offset, unsigned long src_offset,
5680 unsigned long len)
5681{
5682 u64 dst_len = dst->len;
5683 size_t cur;
5684 size_t offset;
5685 struct page *page;
5686 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005687 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
5688 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005689
5690 WARN_ON(src->len != dst_len);
5691
5692 offset = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005693 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005694
Chris Masond3977122009-01-05 21:25:51 -05005695 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005696 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005697 WARN_ON(!PageUptodate(page));
5698
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005699 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005700
Chris Masona6591712011-07-19 12:04:14 -04005701 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005702 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005703
5704 src_offset += cur;
5705 len -= cur;
5706 offset = 0;
5707 i++;
5708 }
5709}
Chris Masond1310b22008-01-24 16:13:08 -05005710
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005711void le_bitmap_set(u8 *map, unsigned int start, int len)
5712{
5713 u8 *p = map + BIT_BYTE(start);
5714 const unsigned int size = start + len;
5715 int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
5716 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
5717
5718 while (len - bits_to_set >= 0) {
5719 *p |= mask_to_set;
5720 len -= bits_to_set;
5721 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005722 mask_to_set = ~0;
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005723 p++;
5724 }
5725 if (len) {
5726 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5727 *p |= mask_to_set;
5728 }
5729}
5730
5731void le_bitmap_clear(u8 *map, unsigned int start, int len)
5732{
5733 u8 *p = map + BIT_BYTE(start);
5734 const unsigned int size = start + len;
5735 int bits_to_clear = BITS_PER_BYTE - (start % BITS_PER_BYTE);
5736 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(start);
5737
5738 while (len - bits_to_clear >= 0) {
5739 *p &= ~mask_to_clear;
5740 len -= bits_to_clear;
5741 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005742 mask_to_clear = ~0;
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005743 p++;
5744 }
5745 if (len) {
5746 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5747 *p &= ~mask_to_clear;
5748 }
5749}
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005750
5751/*
5752 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5753 * given bit number
5754 * @eb: the extent buffer
5755 * @start: offset of the bitmap item in the extent buffer
5756 * @nr: bit number
5757 * @page_index: return index of the page in the extent buffer that contains the
5758 * given bit number
5759 * @page_offset: return offset into the page given by page_index
5760 *
5761 * This helper hides the ugliness of finding the byte in an extent buffer which
5762 * contains a given bit.
5763 */
5764static inline void eb_bitmap_offset(struct extent_buffer *eb,
5765 unsigned long start, unsigned long nr,
5766 unsigned long *page_index,
5767 size_t *page_offset)
5768{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005769 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005770 size_t byte_offset = BIT_BYTE(nr);
5771 size_t offset;
5772
5773 /*
5774 * The byte we want is the offset of the extent buffer + the offset of
5775 * the bitmap item in the extent buffer + the offset of the byte in the
5776 * bitmap item.
5777 */
5778 offset = start_offset + start + byte_offset;
5779
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005780 *page_index = offset >> PAGE_SHIFT;
5781 *page_offset = offset & (PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005782}
5783
5784/**
5785 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5786 * @eb: the extent buffer
5787 * @start: offset of the bitmap item in the extent buffer
5788 * @nr: bit number to test
5789 */
5790int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5791 unsigned long nr)
5792{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005793 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005794 struct page *page;
5795 unsigned long i;
5796 size_t offset;
5797
5798 eb_bitmap_offset(eb, start, nr, &i, &offset);
5799 page = eb->pages[i];
5800 WARN_ON(!PageUptodate(page));
5801 kaddr = page_address(page);
5802 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5803}
5804
5805/**
5806 * extent_buffer_bitmap_set - set an area of a bitmap
5807 * @eb: the extent buffer
5808 * @start: offset of the bitmap item in the extent buffer
5809 * @pos: bit number of the first bit
5810 * @len: number of bits to set
5811 */
5812void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5813 unsigned long pos, unsigned long len)
5814{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005815 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005816 struct page *page;
5817 unsigned long i;
5818 size_t offset;
5819 const unsigned int size = pos + len;
5820 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005821 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005822
5823 eb_bitmap_offset(eb, start, pos, &i, &offset);
5824 page = eb->pages[i];
5825 WARN_ON(!PageUptodate(page));
5826 kaddr = page_address(page);
5827
5828 while (len >= bits_to_set) {
5829 kaddr[offset] |= mask_to_set;
5830 len -= bits_to_set;
5831 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005832 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005833 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005834 offset = 0;
5835 page = eb->pages[++i];
5836 WARN_ON(!PageUptodate(page));
5837 kaddr = page_address(page);
5838 }
5839 }
5840 if (len) {
5841 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5842 kaddr[offset] |= mask_to_set;
5843 }
5844}
5845
5846
5847/**
5848 * extent_buffer_bitmap_clear - clear an area of a bitmap
5849 * @eb: the extent buffer
5850 * @start: offset of the bitmap item in the extent buffer
5851 * @pos: bit number of the first bit
5852 * @len: number of bits to clear
5853 */
5854void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5855 unsigned long pos, unsigned long len)
5856{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005857 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005858 struct page *page;
5859 unsigned long i;
5860 size_t offset;
5861 const unsigned int size = pos + len;
5862 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005863 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005864
5865 eb_bitmap_offset(eb, start, pos, &i, &offset);
5866 page = eb->pages[i];
5867 WARN_ON(!PageUptodate(page));
5868 kaddr = page_address(page);
5869
5870 while (len >= bits_to_clear) {
5871 kaddr[offset] &= ~mask_to_clear;
5872 len -= bits_to_clear;
5873 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005874 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005875 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005876 offset = 0;
5877 page = eb->pages[++i];
5878 WARN_ON(!PageUptodate(page));
5879 kaddr = page_address(page);
5880 }
5881 }
5882 if (len) {
5883 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5884 kaddr[offset] &= ~mask_to_clear;
5885 }
5886}
5887
Sergei Trofimovich33872062011-04-11 21:52:52 +00005888static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5889{
5890 unsigned long distance = (src > dst) ? src - dst : dst - src;
5891 return distance < len;
5892}
5893
Chris Masond1310b22008-01-24 16:13:08 -05005894static void copy_pages(struct page *dst_page, struct page *src_page,
5895 unsigned long dst_off, unsigned long src_off,
5896 unsigned long len)
5897{
Chris Masona6591712011-07-19 12:04:14 -04005898 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005899 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005900 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005901
Sergei Trofimovich33872062011-04-11 21:52:52 +00005902 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005903 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005904 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005905 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005906 if (areas_overlap(src_off, dst_off, len))
5907 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005908 }
Chris Masond1310b22008-01-24 16:13:08 -05005909
Chris Mason727011e2010-08-06 13:21:20 -04005910 if (must_memmove)
5911 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5912 else
5913 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005914}
5915
5916void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5917 unsigned long src_offset, unsigned long len)
5918{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005919 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005920 size_t cur;
5921 size_t dst_off_in_page;
5922 size_t src_off_in_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005923 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005924 unsigned long dst_i;
5925 unsigned long src_i;
5926
5927 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005928 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005929 "memmove bogus src_offset %lu move len %lu dst len %lu",
5930 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005931 BUG_ON(1);
5932 }
5933 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005934 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005935 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5936 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005937 BUG_ON(1);
5938 }
5939
Chris Masond3977122009-01-05 21:25:51 -05005940 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005941 dst_off_in_page = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005942 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005943 src_off_in_page = (start_offset + src_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005944 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005945
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005946 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5947 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005948
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005949 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005950 src_off_in_page));
5951 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005952 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005953
David Sterbafb85fc92014-07-31 01:03:53 +02005954 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005955 dst_off_in_page, src_off_in_page, cur);
5956
5957 src_offset += cur;
5958 dst_offset += cur;
5959 len -= cur;
5960 }
5961}
Chris Masond1310b22008-01-24 16:13:08 -05005962
5963void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5964 unsigned long src_offset, unsigned long len)
5965{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005966 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005967 size_t cur;
5968 size_t dst_off_in_page;
5969 size_t src_off_in_page;
5970 unsigned long dst_end = dst_offset + len - 1;
5971 unsigned long src_end = src_offset + len - 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005972 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005973 unsigned long dst_i;
5974 unsigned long src_i;
5975
5976 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005977 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005978 "memmove bogus src_offset %lu move len %lu len %lu",
5979 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005980 BUG_ON(1);
5981 }
5982 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005983 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005984 "memmove bogus dst_offset %lu move len %lu len %lu",
5985 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005986 BUG_ON(1);
5987 }
Chris Mason727011e2010-08-06 13:21:20 -04005988 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005989 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5990 return;
5991 }
Chris Masond3977122009-01-05 21:25:51 -05005992 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005993 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
5994 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005995
5996 dst_off_in_page = (start_offset + dst_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005997 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005998 src_off_in_page = (start_offset + src_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005999 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05006000
6001 cur = min_t(unsigned long, len, src_off_in_page + 1);
6002 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02006003 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05006004 dst_off_in_page - cur + 1,
6005 src_off_in_page - cur + 1, cur);
6006
6007 dst_end -= cur;
6008 src_end -= cur;
6009 len -= cur;
6010 }
6011}
Chris Mason6af118ce2008-07-22 11:18:07 -04006012
David Sterbaf7a52a42013-04-26 14:56:29 +00006013int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04006014{
Chris Mason6af118ce2008-07-22 11:18:07 -04006015 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04006016
Miao Xie19fe0a82010-10-26 20:57:29 -04006017 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006018 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05006019 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04006020 */
Josef Bacik3083ee22012-03-09 16:01:49 -05006021 spin_lock(&page->mapping->private_lock);
6022 if (!PagePrivate(page)) {
6023 spin_unlock(&page->mapping->private_lock);
6024 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04006025 }
6026
Josef Bacik3083ee22012-03-09 16:01:49 -05006027 eb = (struct extent_buffer *)page->private;
6028 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04006029
Josef Bacik0b32f4b2012-03-13 09:38:00 -04006030 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05006031 * This is a little awful but should be ok, we need to make sure that
6032 * the eb doesn't disappear out from under us while we're looking at
6033 * this page.
6034 */
6035 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04006036 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05006037 spin_unlock(&eb->refs_lock);
6038 spin_unlock(&page->mapping->private_lock);
6039 return 0;
6040 }
6041 spin_unlock(&page->mapping->private_lock);
6042
Josef Bacik3083ee22012-03-09 16:01:49 -05006043 /*
6044 * If tree ref isn't set then we know the ref on this eb is a real ref,
6045 * so just return, this page will likely be freed soon anyway.
6046 */
6047 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
6048 spin_unlock(&eb->refs_lock);
6049 return 0;
6050 }
Josef Bacik3083ee22012-03-09 16:01:49 -05006051
David Sterbaf7a52a42013-04-26 14:56:29 +00006052 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04006053}