blob: 1dfd14678db86daa915b992e846e4180509dfb54 [file] [log] [blame]
Chris Masond1310b22008-01-24 16:13:08 -05001#include <linux/bitops.h>
2#include <linux/slab.h>
3#include <linux/bio.h>
4#include <linux/mm.h>
Chris Masond1310b22008-01-24 16:13:08 -05005#include <linux/pagemap.h>
6#include <linux/page-flags.h>
Chris Masond1310b22008-01-24 16:13:08 -05007#include <linux/spinlock.h>
8#include <linux/blkdev.h>
9#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050010#include <linux/writeback.h>
11#include <linux/pagevec.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070012#include <linux/prefetch.h>
Dan Magenheimer90a887c2011-05-26 10:01:56 -060013#include <linux/cleancache.h>
Chris Masond1310b22008-01-24 16:13:08 -050014#include "extent_io.h"
15#include "extent_map.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040016#include "ctree.h"
17#include "btrfs_inode.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020018#include "volumes.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010019#include "check-integrity.h"
Josef Bacik0b32f4b2012-03-13 09:38:00 -040020#include "locking.h"
Josef Bacik606686e2012-06-04 14:03:51 -040021#include "rcu-string.h"
Liu Bofe09e162013-09-22 12:54:23 +080022#include "backref.h"
Chris Masond1310b22008-01-24 16:13:08 -050023
Chris Masond1310b22008-01-24 16:13:08 -050024static struct kmem_cache *extent_state_cache;
25static struct kmem_cache *extent_buffer_cache;
Chris Mason9be33952013-05-17 18:30:14 -040026static struct bio_set *btrfs_bioset;
Chris Masond1310b22008-01-24 16:13:08 -050027
Filipe Manana27a35072014-07-06 20:09:59 +010028static inline bool extent_state_in_tree(const struct extent_state *state)
29{
30 return !RB_EMPTY_NODE(&state->rb_node);
31}
32
Eric Sandeen6d49ba12013-04-22 16:12:31 +000033#ifdef CONFIG_BTRFS_DEBUG
Chris Masond1310b22008-01-24 16:13:08 -050034static LIST_HEAD(buffers);
35static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040036
Chris Masond3977122009-01-05 21:25:51 -050037static DEFINE_SPINLOCK(leak_lock);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000038
39static inline
40void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
41{
42 unsigned long flags;
43
44 spin_lock_irqsave(&leak_lock, flags);
45 list_add(new, head);
46 spin_unlock_irqrestore(&leak_lock, flags);
47}
48
49static inline
50void btrfs_leak_debug_del(struct list_head *entry)
51{
52 unsigned long flags;
53
54 spin_lock_irqsave(&leak_lock, flags);
55 list_del(entry);
56 spin_unlock_irqrestore(&leak_lock, flags);
57}
58
59static inline
60void btrfs_leak_debug_check(void)
61{
62 struct extent_state *state;
63 struct extent_buffer *eb;
64
65 while (!list_empty(&states)) {
66 state = list_entry(states.next, struct extent_state, leak_list);
David Sterba9ee49a042015-01-14 19:52:13 +010067 pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
Filipe Manana27a35072014-07-06 20:09:59 +010068 state->start, state->end, state->state,
69 extent_state_in_tree(state),
Elena Reshetovab7ac31b2017-03-03 10:55:19 +020070 refcount_read(&state->refs));
Eric Sandeen6d49ba12013-04-22 16:12:31 +000071 list_del(&state->leak_list);
72 kmem_cache_free(extent_state_cache, state);
73 }
74
75 while (!list_empty(&buffers)) {
76 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Jeff Mahoney62e85572016-09-20 10:05:01 -040077 pr_err("BTRFS: buffer leak start %llu len %lu refs %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +020078 eb->start, eb->len, atomic_read(&eb->refs));
Eric Sandeen6d49ba12013-04-22 16:12:31 +000079 list_del(&eb->leak_list);
80 kmem_cache_free(extent_buffer_cache, eb);
81 }
82}
David Sterba8d599ae2013-04-30 15:22:23 +000083
Josef Bacika5dee372013-12-13 10:02:44 -050084#define btrfs_debug_check_extent_io_range(tree, start, end) \
85 __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
David Sterba8d599ae2013-04-30 15:22:23 +000086static inline void __btrfs_debug_check_extent_io_range(const char *caller,
Josef Bacika5dee372013-12-13 10:02:44 -050087 struct extent_io_tree *tree, u64 start, u64 end)
David Sterba8d599ae2013-04-30 15:22:23 +000088{
Josef Bacikc6100a42017-05-05 11:57:13 -040089 if (tree->ops && tree->ops->check_extent_io_range)
90 tree->ops->check_extent_io_range(tree->private_data, caller,
91 start, end);
David Sterba8d599ae2013-04-30 15:22:23 +000092}
Eric Sandeen6d49ba12013-04-22 16:12:31 +000093#else
94#define btrfs_leak_debug_add(new, head) do {} while (0)
95#define btrfs_leak_debug_del(entry) do {} while (0)
96#define btrfs_leak_debug_check() do {} while (0)
David Sterba8d599ae2013-04-30 15:22:23 +000097#define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
Chris Mason4bef0842008-09-08 11:18:08 -040098#endif
Chris Masond1310b22008-01-24 16:13:08 -050099
Chris Masond1310b22008-01-24 16:13:08 -0500100#define BUFFER_LRU_MAX 64
101
102struct tree_entry {
103 u64 start;
104 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -0500105 struct rb_node rb_node;
106};
107
108struct extent_page_data {
109 struct bio *bio;
110 struct extent_io_tree *tree;
111 get_extent_t *get_extent;
Chris Mason771ed682008-11-06 22:02:51 -0500112
113 /* tells writepage not to lock the state bits for this range
114 * it still does the unlocking
115 */
Chris Masonffbd5172009-04-20 15:50:09 -0400116 unsigned int extent_locked:1;
117
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600118 /* tells the submit_bio code to use REQ_SYNC */
Chris Masonffbd5172009-04-20 15:50:09 -0400119 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -0500120};
121
Qu Wenruod38ed272015-10-12 14:53:37 +0800122static void add_extent_changeset(struct extent_state *state, unsigned bits,
123 struct extent_changeset *changeset,
124 int set)
125{
126 int ret;
127
128 if (!changeset)
129 return;
130 if (set && (state->state & bits) == bits)
131 return;
Qu Wenruofefdc552015-10-12 15:35:38 +0800132 if (!set && (state->state & bits) == 0)
133 return;
Qu Wenruod38ed272015-10-12 14:53:37 +0800134 changeset->bytes_changed += state->end - state->start + 1;
David Sterba53d32352017-02-13 13:42:29 +0100135 ret = ulist_add(&changeset->range_changed, state->start, state->end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800136 GFP_ATOMIC);
137 /* ENOMEM */
138 BUG_ON(ret < 0);
139}
140
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400141static noinline void flush_write_bio(void *data);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400142static inline struct btrfs_fs_info *
143tree_fs_info(struct extent_io_tree *tree)
144{
Josef Bacikc6100a42017-05-05 11:57:13 -0400145 if (tree->ops)
146 return tree->ops->tree_fs_info(tree->private_data);
147 return NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400148}
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400149
Chris Masond1310b22008-01-24 16:13:08 -0500150int __init extent_io_init(void)
151{
David Sterba837e1972012-09-07 03:00:48 -0600152 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200153 sizeof(struct extent_state), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300154 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500155 if (!extent_state_cache)
156 return -ENOMEM;
157
David Sterba837e1972012-09-07 03:00:48 -0600158 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200159 sizeof(struct extent_buffer), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300160 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500161 if (!extent_buffer_cache)
162 goto free_state_cache;
Chris Mason9be33952013-05-17 18:30:14 -0400163
164 btrfs_bioset = bioset_create(BIO_POOL_SIZE,
NeilBrown011067b2017-06-18 14:38:57 +1000165 offsetof(struct btrfs_io_bio, bio),
166 BIOSET_NEED_BVECS);
Chris Mason9be33952013-05-17 18:30:14 -0400167 if (!btrfs_bioset)
168 goto free_buffer_cache;
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700169
170 if (bioset_integrity_create(btrfs_bioset, BIO_POOL_SIZE))
171 goto free_bioset;
172
Chris Masond1310b22008-01-24 16:13:08 -0500173 return 0;
174
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700175free_bioset:
176 bioset_free(btrfs_bioset);
177 btrfs_bioset = NULL;
178
Chris Mason9be33952013-05-17 18:30:14 -0400179free_buffer_cache:
180 kmem_cache_destroy(extent_buffer_cache);
181 extent_buffer_cache = NULL;
182
Chris Masond1310b22008-01-24 16:13:08 -0500183free_state_cache:
184 kmem_cache_destroy(extent_state_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400185 extent_state_cache = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500186 return -ENOMEM;
187}
188
189void extent_io_exit(void)
190{
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000191 btrfs_leak_debug_check();
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000192
193 /*
194 * Make sure all delayed rcu free are flushed before we
195 * destroy caches.
196 */
197 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +0800198 kmem_cache_destroy(extent_state_cache);
199 kmem_cache_destroy(extent_buffer_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400200 if (btrfs_bioset)
201 bioset_free(btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -0500202}
203
204void extent_io_tree_init(struct extent_io_tree *tree,
Josef Bacikc6100a42017-05-05 11:57:13 -0400205 void *private_data)
Chris Masond1310b22008-01-24 16:13:08 -0500206{
Eric Paris6bef4d32010-02-23 19:43:04 +0000207 tree->state = RB_ROOT;
Chris Masond1310b22008-01-24 16:13:08 -0500208 tree->ops = NULL;
209 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500210 spin_lock_init(&tree->lock);
Josef Bacikc6100a42017-05-05 11:57:13 -0400211 tree->private_data = private_data;
Chris Masond1310b22008-01-24 16:13:08 -0500212}
Chris Masond1310b22008-01-24 16:13:08 -0500213
Christoph Hellwigb2950862008-12-02 09:54:17 -0500214static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500215{
216 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500217
Michal Hocko3ba7ab22017-01-09 15:39:02 +0100218 /*
219 * The given mask might be not appropriate for the slab allocator,
220 * drop the unsupported bits
221 */
222 mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
Chris Masond1310b22008-01-24 16:13:08 -0500223 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400224 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500225 return state;
226 state->state = 0;
David Sterba47dc1962016-02-11 13:24:13 +0100227 state->failrec = NULL;
Filipe Manana27a35072014-07-06 20:09:59 +0100228 RB_CLEAR_NODE(&state->rb_node);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000229 btrfs_leak_debug_add(&state->leak_list, &states);
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200230 refcount_set(&state->refs, 1);
Chris Masond1310b22008-01-24 16:13:08 -0500231 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100232 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500233 return state;
234}
Chris Masond1310b22008-01-24 16:13:08 -0500235
Chris Mason4845e442010-05-25 20:56:50 -0400236void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500237{
Chris Masond1310b22008-01-24 16:13:08 -0500238 if (!state)
239 return;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200240 if (refcount_dec_and_test(&state->refs)) {
Filipe Manana27a35072014-07-06 20:09:59 +0100241 WARN_ON(extent_state_in_tree(state));
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000242 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100243 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500244 kmem_cache_free(extent_state_cache, state);
245 }
246}
Chris Masond1310b22008-01-24 16:13:08 -0500247
Filipe Mananaf2071b22014-02-12 15:05:53 +0000248static struct rb_node *tree_insert(struct rb_root *root,
249 struct rb_node *search_start,
250 u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000251 struct rb_node *node,
252 struct rb_node ***p_in,
253 struct rb_node **parent_in)
Chris Masond1310b22008-01-24 16:13:08 -0500254{
Filipe Mananaf2071b22014-02-12 15:05:53 +0000255 struct rb_node **p;
Chris Masond3977122009-01-05 21:25:51 -0500256 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500257 struct tree_entry *entry;
258
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000259 if (p_in && parent_in) {
260 p = *p_in;
261 parent = *parent_in;
262 goto do_insert;
263 }
264
Filipe Mananaf2071b22014-02-12 15:05:53 +0000265 p = search_start ? &search_start : &root->rb_node;
Chris Masond3977122009-01-05 21:25:51 -0500266 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500267 parent = *p;
268 entry = rb_entry(parent, struct tree_entry, rb_node);
269
270 if (offset < entry->start)
271 p = &(*p)->rb_left;
272 else if (offset > entry->end)
273 p = &(*p)->rb_right;
274 else
275 return parent;
276 }
277
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000278do_insert:
Chris Masond1310b22008-01-24 16:13:08 -0500279 rb_link_node(node, parent, p);
280 rb_insert_color(node, root);
281 return NULL;
282}
283
Chris Mason80ea96b2008-02-01 14:51:59 -0500284static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000285 struct rb_node **prev_ret,
286 struct rb_node **next_ret,
287 struct rb_node ***p_ret,
288 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500289{
Chris Mason80ea96b2008-02-01 14:51:59 -0500290 struct rb_root *root = &tree->state;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000291 struct rb_node **n = &root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500292 struct rb_node *prev = NULL;
293 struct rb_node *orig_prev = NULL;
294 struct tree_entry *entry;
295 struct tree_entry *prev_entry = NULL;
296
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000297 while (*n) {
298 prev = *n;
299 entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500300 prev_entry = entry;
301
302 if (offset < entry->start)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000303 n = &(*n)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500304 else if (offset > entry->end)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000305 n = &(*n)->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500306 else
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000307 return *n;
Chris Masond1310b22008-01-24 16:13:08 -0500308 }
309
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000310 if (p_ret)
311 *p_ret = n;
312 if (parent_ret)
313 *parent_ret = prev;
314
Chris Masond1310b22008-01-24 16:13:08 -0500315 if (prev_ret) {
316 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500317 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500318 prev = rb_next(prev);
319 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
320 }
321 *prev_ret = prev;
322 prev = orig_prev;
323 }
324
325 if (next_ret) {
326 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500327 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500328 prev = rb_prev(prev);
329 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
330 }
331 *next_ret = prev;
332 }
333 return NULL;
334}
335
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000336static inline struct rb_node *
337tree_search_for_insert(struct extent_io_tree *tree,
338 u64 offset,
339 struct rb_node ***p_ret,
340 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500341{
Chris Mason70dec802008-01-29 09:59:12 -0500342 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500343 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500344
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000345 ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret);
Chris Masond3977122009-01-05 21:25:51 -0500346 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500347 return prev;
348 return ret;
349}
350
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000351static inline struct rb_node *tree_search(struct extent_io_tree *tree,
352 u64 offset)
353{
354 return tree_search_for_insert(tree, offset, NULL, NULL);
355}
356
Josef Bacik9ed74f22009-09-11 16:12:44 -0400357static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
358 struct extent_state *other)
359{
360 if (tree->ops && tree->ops->merge_extent_hook)
Josef Bacikc6100a42017-05-05 11:57:13 -0400361 tree->ops->merge_extent_hook(tree->private_data, new, other);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400362}
363
Chris Masond1310b22008-01-24 16:13:08 -0500364/*
365 * utility function to look for merge candidates inside a given range.
366 * Any extents with matching state are merged together into a single
367 * extent in the tree. Extents with EXTENT_IO in their state field
368 * are not merged because the end_io handlers need to be able to do
369 * operations on them without sleeping (or doing allocations/splits).
370 *
371 * This should be called with the tree lock held.
372 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000373static void merge_state(struct extent_io_tree *tree,
374 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500375{
376 struct extent_state *other;
377 struct rb_node *other_node;
378
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400379 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000380 return;
Chris Masond1310b22008-01-24 16:13:08 -0500381
382 other_node = rb_prev(&state->rb_node);
383 if (other_node) {
384 other = rb_entry(other_node, struct extent_state, rb_node);
385 if (other->end == state->start - 1 &&
386 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400387 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500388 state->start = other->start;
Chris Masond1310b22008-01-24 16:13:08 -0500389 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100390 RB_CLEAR_NODE(&other->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500391 free_extent_state(other);
392 }
393 }
394 other_node = rb_next(&state->rb_node);
395 if (other_node) {
396 other = rb_entry(other_node, struct extent_state, rb_node);
397 if (other->start == state->end + 1 &&
398 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400399 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400400 state->end = other->end;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400401 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100402 RB_CLEAR_NODE(&other->rb_node);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400403 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500404 }
405 }
Chris Masond1310b22008-01-24 16:13:08 -0500406}
407
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000408static void set_state_cb(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +0100409 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500410{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000411 if (tree->ops && tree->ops->set_bit_hook)
Josef Bacikc6100a42017-05-05 11:57:13 -0400412 tree->ops->set_bit_hook(tree->private_data, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500413}
414
415static void clear_state_cb(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +0100416 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500417{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400418 if (tree->ops && tree->ops->clear_bit_hook)
Josef Bacikc6100a42017-05-05 11:57:13 -0400419 tree->ops->clear_bit_hook(tree->private_data, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500420}
421
Xiao Guangrong3150b692011-07-14 03:19:08 +0000422static void set_state_bits(struct extent_io_tree *tree,
Qu Wenruod38ed272015-10-12 14:53:37 +0800423 struct extent_state *state, unsigned *bits,
424 struct extent_changeset *changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000425
Chris Masond1310b22008-01-24 16:13:08 -0500426/*
427 * insert an extent_state struct into the tree. 'bits' are set on the
428 * struct before it is inserted.
429 *
430 * This may return -EEXIST if the extent is already there, in which case the
431 * state struct is freed.
432 *
433 * The tree lock is not taken internally. This is a utility function and
434 * probably isn't what you want to call (see set/clear_extent_bit).
435 */
436static int insert_state(struct extent_io_tree *tree,
437 struct extent_state *state, u64 start, u64 end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000438 struct rb_node ***p,
439 struct rb_node **parent,
Qu Wenruod38ed272015-10-12 14:53:37 +0800440 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500441{
442 struct rb_node *node;
443
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000444 if (end < start)
Frank Holtonefe120a2013-12-20 11:37:06 -0500445 WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200446 end, start);
Chris Masond1310b22008-01-24 16:13:08 -0500447 state->start = start;
448 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400449
Qu Wenruod38ed272015-10-12 14:53:37 +0800450 set_state_bits(tree, state, bits, changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000451
Filipe Mananaf2071b22014-02-12 15:05:53 +0000452 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
Chris Masond1310b22008-01-24 16:13:08 -0500453 if (node) {
454 struct extent_state *found;
455 found = rb_entry(node, struct extent_state, rb_node);
Jeff Mahoney62e85572016-09-20 10:05:01 -0400456 pr_err("BTRFS: found node %llu %llu on insert of %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200457 found->start, found->end, start, end);
Chris Masond1310b22008-01-24 16:13:08 -0500458 return -EEXIST;
459 }
460 merge_state(tree, state);
461 return 0;
462}
463
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000464static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400465 u64 split)
466{
467 if (tree->ops && tree->ops->split_extent_hook)
Josef Bacikc6100a42017-05-05 11:57:13 -0400468 tree->ops->split_extent_hook(tree->private_data, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400469}
470
Chris Masond1310b22008-01-24 16:13:08 -0500471/*
472 * split a given extent state struct in two, inserting the preallocated
473 * struct 'prealloc' as the newly created second half. 'split' indicates an
474 * offset inside 'orig' where it should be split.
475 *
476 * Before calling,
477 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
478 * are two extent state structs in the tree:
479 * prealloc: [orig->start, split - 1]
480 * orig: [ split, orig->end ]
481 *
482 * The tree locks are not taken by this function. They need to be held
483 * by the caller.
484 */
485static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
486 struct extent_state *prealloc, u64 split)
487{
488 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400489
490 split_cb(tree, orig, split);
491
Chris Masond1310b22008-01-24 16:13:08 -0500492 prealloc->start = orig->start;
493 prealloc->end = split - 1;
494 prealloc->state = orig->state;
495 orig->start = split;
496
Filipe Mananaf2071b22014-02-12 15:05:53 +0000497 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
498 &prealloc->rb_node, NULL, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500499 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500500 free_extent_state(prealloc);
501 return -EEXIST;
502 }
503 return 0;
504}
505
Li Zefancdc6a392012-03-12 16:39:48 +0800506static struct extent_state *next_state(struct extent_state *state)
507{
508 struct rb_node *next = rb_next(&state->rb_node);
509 if (next)
510 return rb_entry(next, struct extent_state, rb_node);
511 else
512 return NULL;
513}
514
Chris Masond1310b22008-01-24 16:13:08 -0500515/*
516 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800517 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500518 *
519 * If no bits are set on the state struct after clearing things, the
520 * struct is freed and removed from the tree
521 */
Li Zefancdc6a392012-03-12 16:39:48 +0800522static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
523 struct extent_state *state,
Qu Wenruofefdc552015-10-12 15:35:38 +0800524 unsigned *bits, int wake,
525 struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500526{
Li Zefancdc6a392012-03-12 16:39:48 +0800527 struct extent_state *next;
David Sterba9ee49a042015-01-14 19:52:13 +0100528 unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500529
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400530 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500531 u64 range = state->end - state->start + 1;
532 WARN_ON(range > tree->dirty_bytes);
533 tree->dirty_bytes -= range;
534 }
Chris Mason291d6732008-01-29 15:55:23 -0500535 clear_state_cb(tree, state, bits);
Qu Wenruofefdc552015-10-12 15:35:38 +0800536 add_extent_changeset(state, bits_to_clear, changeset, 0);
Josef Bacik32c00af2009-10-08 13:34:05 -0400537 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500538 if (wake)
539 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400540 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800541 next = next_state(state);
Filipe Manana27a35072014-07-06 20:09:59 +0100542 if (extent_state_in_tree(state)) {
Chris Masond1310b22008-01-24 16:13:08 -0500543 rb_erase(&state->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100544 RB_CLEAR_NODE(&state->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500545 free_extent_state(state);
546 } else {
547 WARN_ON(1);
548 }
549 } else {
550 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800551 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500552 }
Li Zefancdc6a392012-03-12 16:39:48 +0800553 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500554}
555
Xiao Guangrong82337672011-04-20 06:44:57 +0000556static struct extent_state *
557alloc_extent_state_atomic(struct extent_state *prealloc)
558{
559 if (!prealloc)
560 prealloc = alloc_extent_state(GFP_ATOMIC);
561
562 return prealloc;
563}
564
Eric Sandeen48a3b632013-04-25 20:41:01 +0000565static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400566{
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400567 btrfs_panic(tree_fs_info(tree), err,
568 "Locking error: Extent tree was modified by another thread while locked.");
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400569}
570
Chris Masond1310b22008-01-24 16:13:08 -0500571/*
572 * clear some bits on a range in the tree. This may require splitting
573 * or inserting elements in the tree, so the gfp mask is used to
574 * indicate which allocations or sleeping are allowed.
575 *
576 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
577 * the given range from the tree regardless of state (ie for truncate).
578 *
579 * the range [start, end] is inclusive.
580 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100581 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500582 */
Qu Wenruofefdc552015-10-12 15:35:38 +0800583static int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
584 unsigned bits, int wake, int delete,
585 struct extent_state **cached_state,
586 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500587{
588 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400589 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500590 struct extent_state *prealloc = NULL;
591 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400592 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500593 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000594 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500595
Josef Bacika5dee372013-12-13 10:02:44 -0500596 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000597
Josef Bacik7ee9e442013-06-21 16:37:03 -0400598 if (bits & EXTENT_DELALLOC)
599 bits |= EXTENT_NORESERVE;
600
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400601 if (delete)
602 bits |= ~EXTENT_CTLBITS;
603 bits |= EXTENT_FIRST_DELALLOC;
604
Josef Bacik2ac55d42010-02-03 19:33:23 +0000605 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
606 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500607again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800608 if (!prealloc && gfpflags_allow_blocking(mask)) {
Filipe Mananac7bc6312014-11-03 14:12:57 +0000609 /*
610 * Don't care for allocation failure here because we might end
611 * up not needing the pre-allocated extent state at all, which
612 * is the case if we only have in the tree extent states that
613 * cover our input range and don't cover too any other range.
614 * If we end up needing a new extent state we allocate it later.
615 */
Chris Masond1310b22008-01-24 16:13:08 -0500616 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500617 }
618
Chris Masoncad321a2008-12-17 14:51:42 -0500619 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400620 if (cached_state) {
621 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000622
623 if (clear) {
624 *cached_state = NULL;
625 cached_state = NULL;
626 }
627
Filipe Manana27a35072014-07-06 20:09:59 +0100628 if (cached && extent_state_in_tree(cached) &&
629 cached->start <= start && cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000630 if (clear)
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200631 refcount_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400632 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400633 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400634 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000635 if (clear)
636 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400637 }
Chris Masond1310b22008-01-24 16:13:08 -0500638 /*
639 * this search will find the extents that end after
640 * our range starts
641 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500642 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500643 if (!node)
644 goto out;
645 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400646hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500647 if (state->start > end)
648 goto out;
649 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400650 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500651
Liu Bo04493142012-02-16 18:34:37 +0800652 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800653 if (!(state->state & bits)) {
654 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800655 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800656 }
Liu Bo04493142012-02-16 18:34:37 +0800657
Chris Masond1310b22008-01-24 16:13:08 -0500658 /*
659 * | ---- desired range ---- |
660 * | state | or
661 * | ------------- state -------------- |
662 *
663 * We need to split the extent we found, and may flip
664 * bits on second half.
665 *
666 * If the extent we found extends past our range, we
667 * just split and search again. It'll get split again
668 * the next time though.
669 *
670 * If the extent we found is inside our range, we clear
671 * the desired bit on it.
672 */
673
674 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000675 prealloc = alloc_extent_state_atomic(prealloc);
676 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500677 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400678 if (err)
679 extent_io_tree_panic(tree, err);
680
Chris Masond1310b22008-01-24 16:13:08 -0500681 prealloc = NULL;
682 if (err)
683 goto out;
684 if (state->end <= end) {
Qu Wenruofefdc552015-10-12 15:35:38 +0800685 state = clear_state_bit(tree, state, &bits, wake,
686 changeset);
Liu Bod1ac6e42012-05-10 18:10:39 +0800687 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500688 }
689 goto search_again;
690 }
691 /*
692 * | ---- desired range ---- |
693 * | state |
694 * We need to split the extent, and clear the bit
695 * on the first half
696 */
697 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000698 prealloc = alloc_extent_state_atomic(prealloc);
699 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500700 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400701 if (err)
702 extent_io_tree_panic(tree, err);
703
Chris Masond1310b22008-01-24 16:13:08 -0500704 if (wake)
705 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400706
Qu Wenruofefdc552015-10-12 15:35:38 +0800707 clear_state_bit(tree, prealloc, &bits, wake, changeset);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400708
Chris Masond1310b22008-01-24 16:13:08 -0500709 prealloc = NULL;
710 goto out;
711 }
Chris Mason42daec22009-09-23 19:51:09 -0400712
Qu Wenruofefdc552015-10-12 15:35:38 +0800713 state = clear_state_bit(tree, state, &bits, wake, changeset);
Liu Bo04493142012-02-16 18:34:37 +0800714next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400715 if (last_end == (u64)-1)
716 goto out;
717 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800718 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800719 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500720
721search_again:
722 if (start > end)
723 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500724 spin_unlock(&tree->lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800725 if (gfpflags_allow_blocking(mask))
Chris Masond1310b22008-01-24 16:13:08 -0500726 cond_resched();
727 goto again;
David Sterba7ab5cb22016-04-27 01:02:15 +0200728
729out:
730 spin_unlock(&tree->lock);
731 if (prealloc)
732 free_extent_state(prealloc);
733
734 return 0;
735
Chris Masond1310b22008-01-24 16:13:08 -0500736}
Chris Masond1310b22008-01-24 16:13:08 -0500737
Jeff Mahoney143bede2012-03-01 14:56:26 +0100738static void wait_on_state(struct extent_io_tree *tree,
739 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500740 __releases(tree->lock)
741 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500742{
743 DEFINE_WAIT(wait);
744 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500745 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500746 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500747 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500748 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500749}
750
751/*
752 * waits for one or more bits to clear on a range in the state tree.
753 * The range [start, end] is inclusive.
754 * The tree lock is taken by this function
755 */
David Sterba41074882013-04-29 13:38:46 +0000756static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
757 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500758{
759 struct extent_state *state;
760 struct rb_node *node;
761
Josef Bacika5dee372013-12-13 10:02:44 -0500762 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000763
Chris Masoncad321a2008-12-17 14:51:42 -0500764 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500765again:
766 while (1) {
767 /*
768 * this search will find all the extents that end after
769 * our range starts
770 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500771 node = tree_search(tree, start);
Filipe Mananac50d3e72014-03-31 14:53:25 +0100772process_node:
Chris Masond1310b22008-01-24 16:13:08 -0500773 if (!node)
774 break;
775
776 state = rb_entry(node, struct extent_state, rb_node);
777
778 if (state->start > end)
779 goto out;
780
781 if (state->state & bits) {
782 start = state->start;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200783 refcount_inc(&state->refs);
Chris Masond1310b22008-01-24 16:13:08 -0500784 wait_on_state(tree, state);
785 free_extent_state(state);
786 goto again;
787 }
788 start = state->end + 1;
789
790 if (start > end)
791 break;
792
Filipe Mananac50d3e72014-03-31 14:53:25 +0100793 if (!cond_resched_lock(&tree->lock)) {
794 node = rb_next(node);
795 goto process_node;
796 }
Chris Masond1310b22008-01-24 16:13:08 -0500797 }
798out:
Chris Masoncad321a2008-12-17 14:51:42 -0500799 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500800}
Chris Masond1310b22008-01-24 16:13:08 -0500801
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000802static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500803 struct extent_state *state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800804 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500805{
David Sterba9ee49a042015-01-14 19:52:13 +0100806 unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400807
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000808 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400809 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500810 u64 range = state->end - state->start + 1;
811 tree->dirty_bytes += range;
812 }
Qu Wenruod38ed272015-10-12 14:53:37 +0800813 add_extent_changeset(state, bits_to_set, changeset, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400814 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500815}
816
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100817static void cache_state_if_flags(struct extent_state *state,
818 struct extent_state **cached_ptr,
David Sterba9ee49a042015-01-14 19:52:13 +0100819 unsigned flags)
Chris Mason2c64c532009-09-02 15:04:12 -0400820{
821 if (cached_ptr && !(*cached_ptr)) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100822 if (!flags || (state->state & flags)) {
Chris Mason2c64c532009-09-02 15:04:12 -0400823 *cached_ptr = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200824 refcount_inc(&state->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400825 }
826 }
827}
828
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100829static void cache_state(struct extent_state *state,
830 struct extent_state **cached_ptr)
831{
832 return cache_state_if_flags(state, cached_ptr,
833 EXTENT_IOBITS | EXTENT_BOUNDARY);
834}
835
Chris Masond1310b22008-01-24 16:13:08 -0500836/*
Chris Mason1edbb732009-09-02 13:24:36 -0400837 * set some bits on a range in the tree. This may require allocations or
838 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500839 *
Chris Mason1edbb732009-09-02 13:24:36 -0400840 * If any of the exclusive bits are set, this will fail with -EEXIST if some
841 * part of the range already has the desired bits set. The start of the
842 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500843 *
Chris Mason1edbb732009-09-02 13:24:36 -0400844 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500845 */
Chris Mason1edbb732009-09-02 13:24:36 -0400846
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100847static int __must_check
848__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +0100849 unsigned bits, unsigned exclusive_bits,
David Sterba41074882013-04-29 13:38:46 +0000850 u64 *failed_start, struct extent_state **cached_state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800851 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500852{
853 struct extent_state *state;
854 struct extent_state *prealloc = NULL;
855 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000856 struct rb_node **p;
857 struct rb_node *parent;
Chris Masond1310b22008-01-24 16:13:08 -0500858 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500859 u64 last_start;
860 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400861
Josef Bacika5dee372013-12-13 10:02:44 -0500862 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000863
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400864 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500865again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800866 if (!prealloc && gfpflags_allow_blocking(mask)) {
David Sterba059f7912016-04-27 01:03:45 +0200867 /*
868 * Don't care for allocation failure here because we might end
869 * up not needing the pre-allocated extent state at all, which
870 * is the case if we only have in the tree extent states that
871 * cover our input range and don't cover too any other range.
872 * If we end up needing a new extent state we allocate it later.
873 */
Chris Masond1310b22008-01-24 16:13:08 -0500874 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500875 }
876
Chris Masoncad321a2008-12-17 14:51:42 -0500877 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400878 if (cached_state && *cached_state) {
879 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400880 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +0100881 extent_state_in_tree(state)) {
Chris Mason9655d292009-09-02 15:22:30 -0400882 node = &state->rb_node;
883 goto hit_next;
884 }
885 }
Chris Masond1310b22008-01-24 16:13:08 -0500886 /*
887 * this search will find all the extents that end after
888 * our range starts.
889 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000890 node = tree_search_for_insert(tree, start, &p, &parent);
Chris Masond1310b22008-01-24 16:13:08 -0500891 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000892 prealloc = alloc_extent_state_atomic(prealloc);
893 BUG_ON(!prealloc);
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000894 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800895 &p, &parent, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400896 if (err)
897 extent_io_tree_panic(tree, err);
898
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +0000899 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500900 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500901 goto out;
902 }
Chris Masond1310b22008-01-24 16:13:08 -0500903 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400904hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500905 last_start = state->start;
906 last_end = state->end;
907
908 /*
909 * | ---- desired range ---- |
910 * | state |
911 *
912 * Just lock what we found and keep going
913 */
914 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400915 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500916 *failed_start = state->start;
917 err = -EEXIST;
918 goto out;
919 }
Chris Mason42daec22009-09-23 19:51:09 -0400920
Qu Wenruod38ed272015-10-12 14:53:37 +0800921 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400922 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500923 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400924 if (last_end == (u64)-1)
925 goto out;
926 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800927 state = next_state(state);
928 if (start < end && state && state->start == start &&
929 !need_resched())
930 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500931 goto search_again;
932 }
933
934 /*
935 * | ---- desired range ---- |
936 * | state |
937 * or
938 * | ------------- state -------------- |
939 *
940 * We need to split the extent we found, and may flip bits on
941 * second half.
942 *
943 * If the extent we found extends past our
944 * range, we just split and search again. It'll get split
945 * again the next time though.
946 *
947 * If the extent we found is inside our range, we set the
948 * desired bit on it.
949 */
950 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400951 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500952 *failed_start = start;
953 err = -EEXIST;
954 goto out;
955 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000956
957 prealloc = alloc_extent_state_atomic(prealloc);
958 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500959 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400960 if (err)
961 extent_io_tree_panic(tree, err);
962
Chris Masond1310b22008-01-24 16:13:08 -0500963 prealloc = NULL;
964 if (err)
965 goto out;
966 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +0800967 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400968 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500969 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400970 if (last_end == (u64)-1)
971 goto out;
972 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800973 state = next_state(state);
974 if (start < end && state && state->start == start &&
975 !need_resched())
976 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500977 }
978 goto search_again;
979 }
980 /*
981 * | ---- desired range ---- |
982 * | state | or | state |
983 *
984 * There's a hole, we need to insert something in it and
985 * ignore the extent we found.
986 */
987 if (state->start > start) {
988 u64 this_end;
989 if (end < last_start)
990 this_end = end;
991 else
Chris Masond3977122009-01-05 21:25:51 -0500992 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000993
994 prealloc = alloc_extent_state_atomic(prealloc);
995 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000996
997 /*
998 * Avoid to free 'prealloc' if it can be merged with
999 * the later extent.
1000 */
Chris Masond1310b22008-01-24 16:13:08 -05001001 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001002 NULL, NULL, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001003 if (err)
1004 extent_io_tree_panic(tree, err);
1005
Chris Mason2c64c532009-09-02 15:04:12 -04001006 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001007 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001008 start = this_end + 1;
1009 goto search_again;
1010 }
1011 /*
1012 * | ---- desired range ---- |
1013 * | state |
1014 * We need to split the extent, and set the bit
1015 * on the first half
1016 */
1017 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -04001018 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001019 *failed_start = start;
1020 err = -EEXIST;
1021 goto out;
1022 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001023
1024 prealloc = alloc_extent_state_atomic(prealloc);
1025 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001026 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001027 if (err)
1028 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -05001029
Qu Wenruod38ed272015-10-12 14:53:37 +08001030 set_state_bits(tree, prealloc, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001031 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001032 merge_state(tree, prealloc);
1033 prealloc = NULL;
1034 goto out;
1035 }
1036
David Sterbab5a4ba142016-04-27 01:02:15 +02001037search_again:
1038 if (start > end)
1039 goto out;
1040 spin_unlock(&tree->lock);
1041 if (gfpflags_allow_blocking(mask))
1042 cond_resched();
1043 goto again;
Chris Masond1310b22008-01-24 16:13:08 -05001044
1045out:
Chris Masoncad321a2008-12-17 14:51:42 -05001046 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001047 if (prealloc)
1048 free_extent_state(prealloc);
1049
1050 return err;
1051
Chris Masond1310b22008-01-24 16:13:08 -05001052}
Chris Masond1310b22008-01-24 16:13:08 -05001053
David Sterba41074882013-04-29 13:38:46 +00001054int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001055 unsigned bits, u64 * failed_start,
David Sterba41074882013-04-29 13:38:46 +00001056 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001057{
1058 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001059 cached_state, mask, NULL);
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001060}
1061
1062
Josef Bacik462d6fa2011-09-26 13:56:12 -04001063/**
Liu Bo10983f22012-07-11 15:26:19 +08001064 * convert_extent_bit - convert all bits in a given range from one bit to
1065 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -04001066 * @tree: the io tree to search
1067 * @start: the start offset in bytes
1068 * @end: the end offset in bytes (inclusive)
1069 * @bits: the bits to set in this range
1070 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001071 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001072 *
1073 * This will go through and set bits for the given range. If any states exist
1074 * already in this range they are set with the given bit and cleared of the
1075 * clear_bits. This is only meant to be used by things that are mergeable, ie
1076 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1077 * boundary bits like LOCK.
David Sterba210aa272016-04-26 23:54:39 +02001078 *
1079 * All allocations are done with GFP_NOFS.
Josef Bacik462d6fa2011-09-26 13:56:12 -04001080 */
1081int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001082 unsigned bits, unsigned clear_bits,
David Sterba210aa272016-04-26 23:54:39 +02001083 struct extent_state **cached_state)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001084{
1085 struct extent_state *state;
1086 struct extent_state *prealloc = NULL;
1087 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001088 struct rb_node **p;
1089 struct rb_node *parent;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001090 int err = 0;
1091 u64 last_start;
1092 u64 last_end;
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001093 bool first_iteration = true;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001094
Josef Bacika5dee372013-12-13 10:02:44 -05001095 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +00001096
Josef Bacik462d6fa2011-09-26 13:56:12 -04001097again:
David Sterba210aa272016-04-26 23:54:39 +02001098 if (!prealloc) {
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001099 /*
1100 * Best effort, don't worry if extent state allocation fails
1101 * here for the first iteration. We might have a cached state
1102 * that matches exactly the target range, in which case no
1103 * extent state allocations are needed. We'll only know this
1104 * after locking the tree.
1105 */
David Sterba210aa272016-04-26 23:54:39 +02001106 prealloc = alloc_extent_state(GFP_NOFS);
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001107 if (!prealloc && !first_iteration)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001108 return -ENOMEM;
1109 }
1110
1111 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001112 if (cached_state && *cached_state) {
1113 state = *cached_state;
1114 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +01001115 extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001116 node = &state->rb_node;
1117 goto hit_next;
1118 }
1119 }
1120
Josef Bacik462d6fa2011-09-26 13:56:12 -04001121 /*
1122 * this search will find all the extents that end after
1123 * our range starts.
1124 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001125 node = tree_search_for_insert(tree, start, &p, &parent);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001126 if (!node) {
1127 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001128 if (!prealloc) {
1129 err = -ENOMEM;
1130 goto out;
1131 }
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001132 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001133 &p, &parent, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001134 if (err)
1135 extent_io_tree_panic(tree, err);
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001136 cache_state(prealloc, cached_state);
1137 prealloc = NULL;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001138 goto out;
1139 }
1140 state = rb_entry(node, struct extent_state, rb_node);
1141hit_next:
1142 last_start = state->start;
1143 last_end = state->end;
1144
1145 /*
1146 * | ---- desired range ---- |
1147 * | state |
1148 *
1149 * Just lock what we found and keep going
1150 */
1151 if (state->start == start && state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001152 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001153 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001154 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001155 if (last_end == (u64)-1)
1156 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001157 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001158 if (start < end && state && state->start == start &&
1159 !need_resched())
1160 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001161 goto search_again;
1162 }
1163
1164 /*
1165 * | ---- desired range ---- |
1166 * | state |
1167 * or
1168 * | ------------- state -------------- |
1169 *
1170 * We need to split the extent we found, and may flip bits on
1171 * second half.
1172 *
1173 * If the extent we found extends past our
1174 * range, we just split and search again. It'll get split
1175 * again the next time though.
1176 *
1177 * If the extent we found is inside our range, we set the
1178 * desired bit on it.
1179 */
1180 if (state->start < start) {
1181 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001182 if (!prealloc) {
1183 err = -ENOMEM;
1184 goto out;
1185 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001186 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001187 if (err)
1188 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001189 prealloc = NULL;
1190 if (err)
1191 goto out;
1192 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001193 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001194 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001195 state = clear_state_bit(tree, state, &clear_bits, 0,
1196 NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001197 if (last_end == (u64)-1)
1198 goto out;
1199 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001200 if (start < end && state && state->start == start &&
1201 !need_resched())
1202 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001203 }
1204 goto search_again;
1205 }
1206 /*
1207 * | ---- desired range ---- |
1208 * | state | or | state |
1209 *
1210 * There's a hole, we need to insert something in it and
1211 * ignore the extent we found.
1212 */
1213 if (state->start > start) {
1214 u64 this_end;
1215 if (end < last_start)
1216 this_end = end;
1217 else
1218 this_end = last_start - 1;
1219
1220 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001221 if (!prealloc) {
1222 err = -ENOMEM;
1223 goto out;
1224 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001225
1226 /*
1227 * Avoid to free 'prealloc' if it can be merged with
1228 * the later extent.
1229 */
1230 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001231 NULL, NULL, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001232 if (err)
1233 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001234 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001235 prealloc = NULL;
1236 start = this_end + 1;
1237 goto search_again;
1238 }
1239 /*
1240 * | ---- desired range ---- |
1241 * | state |
1242 * We need to split the extent, and set the bit
1243 * on the first half
1244 */
1245 if (state->start <= end && state->end > end) {
1246 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001247 if (!prealloc) {
1248 err = -ENOMEM;
1249 goto out;
1250 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001251
1252 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001253 if (err)
1254 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001255
Qu Wenruod38ed272015-10-12 14:53:37 +08001256 set_state_bits(tree, prealloc, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001257 cache_state(prealloc, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001258 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001259 prealloc = NULL;
1260 goto out;
1261 }
1262
Josef Bacik462d6fa2011-09-26 13:56:12 -04001263search_again:
1264 if (start > end)
1265 goto out;
1266 spin_unlock(&tree->lock);
David Sterba210aa272016-04-26 23:54:39 +02001267 cond_resched();
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001268 first_iteration = false;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001269 goto again;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001270
1271out:
1272 spin_unlock(&tree->lock);
1273 if (prealloc)
1274 free_extent_state(prealloc);
1275
1276 return err;
1277}
1278
Chris Masond1310b22008-01-24 16:13:08 -05001279/* wrappers around set/clear extent bit */
Qu Wenruod38ed272015-10-12 14:53:37 +08001280int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba2c53b912016-04-26 23:54:39 +02001281 unsigned bits, struct extent_changeset *changeset)
Qu Wenruod38ed272015-10-12 14:53:37 +08001282{
1283 /*
1284 * We don't support EXTENT_LOCKED yet, as current changeset will
1285 * record any bits changed, so for EXTENT_LOCKED case, it will
1286 * either fail with -EEXIST or changeset will record the whole
1287 * range.
1288 */
1289 BUG_ON(bits & EXTENT_LOCKED);
1290
David Sterba2c53b912016-04-26 23:54:39 +02001291 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
Qu Wenruod38ed272015-10-12 14:53:37 +08001292 changeset);
1293}
1294
Qu Wenruofefdc552015-10-12 15:35:38 +08001295int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1296 unsigned bits, int wake, int delete,
1297 struct extent_state **cached, gfp_t mask)
1298{
1299 return __clear_extent_bit(tree, start, end, bits, wake, delete,
1300 cached, mask, NULL);
1301}
1302
Qu Wenruofefdc552015-10-12 15:35:38 +08001303int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaf734c442016-04-26 23:54:39 +02001304 unsigned bits, struct extent_changeset *changeset)
Qu Wenruofefdc552015-10-12 15:35:38 +08001305{
1306 /*
1307 * Don't support EXTENT_LOCKED case, same reason as
1308 * set_record_extent_bits().
1309 */
1310 BUG_ON(bits & EXTENT_LOCKED);
1311
David Sterbaf734c442016-04-26 23:54:39 +02001312 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
Qu Wenruofefdc552015-10-12 15:35:38 +08001313 changeset);
1314}
1315
Chris Masond352ac62008-09-29 15:18:18 -04001316/*
1317 * either insert or lock state struct between start and end use mask to tell
1318 * us if waiting is desired.
1319 */
Chris Mason1edbb732009-09-02 13:24:36 -04001320int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaff13db42015-12-03 14:30:40 +01001321 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001322{
1323 int err;
1324 u64 failed_start;
David Sterba9ee49a042015-01-14 19:52:13 +01001325
Chris Masond1310b22008-01-24 16:13:08 -05001326 while (1) {
David Sterbaff13db42015-12-03 14:30:40 +01001327 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001328 EXTENT_LOCKED, &failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001329 cached_state, GFP_NOFS, NULL);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001330 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001331 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1332 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001333 } else
Chris Masond1310b22008-01-24 16:13:08 -05001334 break;
Chris Masond1310b22008-01-24 16:13:08 -05001335 WARN_ON(start > end);
1336 }
1337 return err;
1338}
Chris Masond1310b22008-01-24 16:13:08 -05001339
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001340int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik251792012008-10-29 14:49:05 -04001341{
1342 int err;
1343 u64 failed_start;
1344
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001345 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
Qu Wenruod38ed272015-10-12 14:53:37 +08001346 &failed_start, NULL, GFP_NOFS, NULL);
Yan Zheng66435582008-10-30 14:19:50 -04001347 if (err == -EEXIST) {
1348 if (failed_start > start)
1349 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001350 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik251792012008-10-29 14:49:05 -04001351 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001352 }
Josef Bacik251792012008-10-29 14:49:05 -04001353 return 1;
1354}
Josef Bacik251792012008-10-29 14:49:05 -04001355
David Sterbabd1fa4f2015-12-03 13:08:59 +01001356void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001357{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001358 unsigned long index = start >> PAGE_SHIFT;
1359 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001360 struct page *page;
1361
1362 while (index <= end_index) {
1363 page = find_get_page(inode->i_mapping, index);
1364 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1365 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001366 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001367 index++;
1368 }
Chris Mason4adaa612013-03-26 13:07:00 -04001369}
1370
David Sterbaf6311572015-12-03 13:08:59 +01001371void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001372{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001373 unsigned long index = start >> PAGE_SHIFT;
1374 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001375 struct page *page;
1376
1377 while (index <= end_index) {
1378 page = find_get_page(inode->i_mapping, index);
1379 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Mason4adaa612013-03-26 13:07:00 -04001380 __set_page_dirty_nobuffers(page);
Konstantin Khebnikov8d386332015-02-11 15:26:55 -08001381 account_page_redirty(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001382 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001383 index++;
1384 }
Chris Mason4adaa612013-03-26 13:07:00 -04001385}
1386
Chris Masond1310b22008-01-24 16:13:08 -05001387/*
Chris Masond1310b22008-01-24 16:13:08 -05001388 * helper function to set both pages and extents in the tree writeback
1389 */
David Sterba35de6db2015-12-03 13:08:59 +01001390static void set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001391{
Josef Bacikc6100a42017-05-05 11:57:13 -04001392 tree->ops->set_range_writeback(tree->private_data, start, end);
Chris Masond1310b22008-01-24 16:13:08 -05001393}
Chris Masond1310b22008-01-24 16:13:08 -05001394
Chris Masond352ac62008-09-29 15:18:18 -04001395/* find the first state struct with 'bits' set after 'start', and
1396 * return it. tree->lock must be held. NULL will returned if
1397 * nothing was found after 'start'
1398 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001399static struct extent_state *
1400find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +01001401 u64 start, unsigned bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001402{
1403 struct rb_node *node;
1404 struct extent_state *state;
1405
1406 /*
1407 * this search will find all the extents that end after
1408 * our range starts.
1409 */
1410 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001411 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001412 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001413
Chris Masond3977122009-01-05 21:25:51 -05001414 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001415 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001416 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001417 return state;
Chris Masond3977122009-01-05 21:25:51 -05001418
Chris Masond7fc6402008-02-18 12:12:38 -05001419 node = rb_next(node);
1420 if (!node)
1421 break;
1422 }
1423out:
1424 return NULL;
1425}
Chris Masond7fc6402008-02-18 12:12:38 -05001426
Chris Masond352ac62008-09-29 15:18:18 -04001427/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001428 * find the first offset in the io tree with 'bits' set. zero is
1429 * returned if we find something, and *start_ret and *end_ret are
1430 * set to reflect the state struct that was found.
1431 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001432 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001433 */
1434int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba9ee49a042015-01-14 19:52:13 +01001435 u64 *start_ret, u64 *end_ret, unsigned bits,
Josef Bacike6138872012-09-27 17:07:30 -04001436 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001437{
1438 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001439 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001440 int ret = 1;
1441
1442 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001443 if (cached_state && *cached_state) {
1444 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001445 if (state->end == start - 1 && extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001446 n = rb_next(&state->rb_node);
1447 while (n) {
1448 state = rb_entry(n, struct extent_state,
1449 rb_node);
1450 if (state->state & bits)
1451 goto got_it;
1452 n = rb_next(n);
1453 }
1454 free_extent_state(*cached_state);
1455 *cached_state = NULL;
1456 goto out;
1457 }
1458 free_extent_state(*cached_state);
1459 *cached_state = NULL;
1460 }
1461
Xiao Guangrong69261c42011-07-14 03:19:45 +00001462 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001463got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001464 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001465 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001466 *start_ret = state->start;
1467 *end_ret = state->end;
1468 ret = 0;
1469 }
Josef Bacike6138872012-09-27 17:07:30 -04001470out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001471 spin_unlock(&tree->lock);
1472 return ret;
1473}
1474
1475/*
Chris Masond352ac62008-09-29 15:18:18 -04001476 * find a contiguous range of bytes in the file marked as delalloc, not
1477 * more than 'max_bytes'. start and end are used to return the range,
1478 *
1479 * 1 is returned if we find something, 0 if nothing was in the tree
1480 */
Chris Masonc8b97812008-10-29 14:49:59 -04001481static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001482 u64 *start, u64 *end, u64 max_bytes,
1483 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001484{
1485 struct rb_node *node;
1486 struct extent_state *state;
1487 u64 cur_start = *start;
1488 u64 found = 0;
1489 u64 total_bytes = 0;
1490
Chris Masoncad321a2008-12-17 14:51:42 -05001491 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001492
Chris Masond1310b22008-01-24 16:13:08 -05001493 /*
1494 * this search will find all the extents that end after
1495 * our range starts.
1496 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001497 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001498 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001499 if (!found)
1500 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001501 goto out;
1502 }
1503
Chris Masond3977122009-01-05 21:25:51 -05001504 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001505 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001506 if (found && (state->start != cur_start ||
1507 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001508 goto out;
1509 }
1510 if (!(state->state & EXTENT_DELALLOC)) {
1511 if (!found)
1512 *end = state->end;
1513 goto out;
1514 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001515 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001516 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001517 *cached_state = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +02001518 refcount_inc(&state->refs);
Josef Bacikc2a128d2010-02-02 21:19:11 +00001519 }
Chris Masond1310b22008-01-24 16:13:08 -05001520 found++;
1521 *end = state->end;
1522 cur_start = state->end + 1;
1523 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001524 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001525 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001526 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001527 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001528 break;
1529 }
1530out:
Chris Masoncad321a2008-12-17 14:51:42 -05001531 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001532 return found;
1533}
1534
Liu Boda2c7002017-02-10 16:41:05 +01001535static int __process_pages_contig(struct address_space *mapping,
1536 struct page *locked_page,
1537 pgoff_t start_index, pgoff_t end_index,
1538 unsigned long page_ops, pgoff_t *index_ret);
1539
Jeff Mahoney143bede2012-03-01 14:56:26 +01001540static noinline void __unlock_for_delalloc(struct inode *inode,
1541 struct page *locked_page,
1542 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001543{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001544 unsigned long index = start >> PAGE_SHIFT;
1545 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001546
Liu Bo76c00212017-02-10 16:42:14 +01001547 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001548 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001549 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001550
Liu Bo76c00212017-02-10 16:42:14 +01001551 __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1552 PAGE_UNLOCK, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -04001553}
1554
1555static noinline int lock_delalloc_pages(struct inode *inode,
1556 struct page *locked_page,
1557 u64 delalloc_start,
1558 u64 delalloc_end)
1559{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001560 unsigned long index = delalloc_start >> PAGE_SHIFT;
Liu Bo76c00212017-02-10 16:42:14 +01001561 unsigned long index_ret = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001562 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001563 int ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001564
Liu Bo76c00212017-02-10 16:42:14 +01001565 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001566 if (index == locked_page->index && index == end_index)
1567 return 0;
1568
Liu Bo76c00212017-02-10 16:42:14 +01001569 ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1570 end_index, PAGE_LOCK, &index_ret);
1571 if (ret == -EAGAIN)
1572 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1573 (u64)index_ret << PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001574 return ret;
1575}
1576
1577/*
1578 * find a contiguous range of bytes in the file marked as delalloc, not
1579 * more than 'max_bytes'. start and end are used to return the range,
1580 *
1581 * 1 is returned if we find something, 0 if nothing was in the tree
1582 */
Josef Bacik294e30f2013-10-09 12:00:56 -04001583STATIC u64 find_lock_delalloc_range(struct inode *inode,
1584 struct extent_io_tree *tree,
1585 struct page *locked_page, u64 *start,
1586 u64 *end, u64 max_bytes)
Chris Masonc8b97812008-10-29 14:49:59 -04001587{
1588 u64 delalloc_start;
1589 u64 delalloc_end;
1590 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001591 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001592 int ret;
1593 int loops = 0;
1594
1595again:
1596 /* step one, find a bunch of delalloc bytes starting at start */
1597 delalloc_start = *start;
1598 delalloc_end = 0;
1599 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001600 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001601 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001602 *start = delalloc_start;
1603 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001604 free_extent_state(cached_state);
Liu Bo385fe0b2013-10-01 23:49:49 +08001605 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001606 }
1607
1608 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001609 * start comes from the offset of locked_page. We have to lock
1610 * pages in order, so we can't process delalloc bytes before
1611 * locked_page
1612 */
Chris Masond3977122009-01-05 21:25:51 -05001613 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001614 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001615
1616 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001617 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001618 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001619 if (delalloc_end + 1 - delalloc_start > max_bytes)
1620 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001621
Chris Masonc8b97812008-10-29 14:49:59 -04001622 /* step two, lock all the pages after the page that has start */
1623 ret = lock_delalloc_pages(inode, locked_page,
1624 delalloc_start, delalloc_end);
1625 if (ret == -EAGAIN) {
1626 /* some of the pages are gone, lets avoid looping by
1627 * shortening the size of the delalloc range we're searching
1628 */
Chris Mason9655d292009-09-02 15:22:30 -04001629 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001630 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001631 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001632 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001633 loops = 1;
1634 goto again;
1635 } else {
1636 found = 0;
1637 goto out_failed;
1638 }
1639 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001640 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001641
1642 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001643 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001644
1645 /* then test to make sure it is all still delalloc */
1646 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001647 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001648 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001649 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1650 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001651 __unlock_for_delalloc(inode, locked_page,
1652 delalloc_start, delalloc_end);
1653 cond_resched();
1654 goto again;
1655 }
Chris Mason9655d292009-09-02 15:22:30 -04001656 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001657 *start = delalloc_start;
1658 *end = delalloc_end;
1659out_failed:
1660 return found;
1661}
1662
Liu Boda2c7002017-02-10 16:41:05 +01001663static int __process_pages_contig(struct address_space *mapping,
1664 struct page *locked_page,
1665 pgoff_t start_index, pgoff_t end_index,
1666 unsigned long page_ops, pgoff_t *index_ret)
Chris Masonc8b97812008-10-29 14:49:59 -04001667{
Liu Bo873695b2017-02-02 17:49:22 -08001668 unsigned long nr_pages = end_index - start_index + 1;
Liu Boda2c7002017-02-10 16:41:05 +01001669 unsigned long pages_locked = 0;
Liu Bo873695b2017-02-02 17:49:22 -08001670 pgoff_t index = start_index;
Chris Masonc8b97812008-10-29 14:49:59 -04001671 struct page *pages[16];
Liu Bo873695b2017-02-02 17:49:22 -08001672 unsigned ret;
Liu Boda2c7002017-02-10 16:41:05 +01001673 int err = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001674 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001675
Liu Boda2c7002017-02-10 16:41:05 +01001676 if (page_ops & PAGE_LOCK) {
1677 ASSERT(page_ops == PAGE_LOCK);
1678 ASSERT(index_ret && *index_ret == start_index);
1679 }
1680
Filipe Manana704de492014-10-06 22:14:22 +01001681 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
Liu Bo873695b2017-02-02 17:49:22 -08001682 mapping_set_error(mapping, -EIO);
Filipe Manana704de492014-10-06 22:14:22 +01001683
Chris Masond3977122009-01-05 21:25:51 -05001684 while (nr_pages > 0) {
Liu Bo873695b2017-02-02 17:49:22 -08001685 ret = find_get_pages_contig(mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001686 min_t(unsigned long,
1687 nr_pages, ARRAY_SIZE(pages)), pages);
Liu Boda2c7002017-02-10 16:41:05 +01001688 if (ret == 0) {
1689 /*
1690 * Only if we're going to lock these pages,
1691 * can we find nothing at @index.
1692 */
1693 ASSERT(page_ops & PAGE_LOCK);
Liu Bo49d4a332017-03-06 18:20:56 -08001694 err = -EAGAIN;
1695 goto out;
Liu Boda2c7002017-02-10 16:41:05 +01001696 }
Chris Mason8b62b722009-09-02 16:53:46 -04001697
Liu Boda2c7002017-02-10 16:41:05 +01001698 for (i = 0; i < ret; i++) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001699 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001700 SetPagePrivate2(pages[i]);
1701
Chris Masonc8b97812008-10-29 14:49:59 -04001702 if (pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001703 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001704 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001705 continue;
1706 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001707 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001708 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001709 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001710 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001711 if (page_ops & PAGE_SET_ERROR)
1712 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001713 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001714 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001715 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001716 unlock_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001717 if (page_ops & PAGE_LOCK) {
1718 lock_page(pages[i]);
1719 if (!PageDirty(pages[i]) ||
1720 pages[i]->mapping != mapping) {
1721 unlock_page(pages[i]);
1722 put_page(pages[i]);
1723 err = -EAGAIN;
1724 goto out;
1725 }
1726 }
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 }
1730 nr_pages -= ret;
1731 index += ret;
1732 cond_resched();
1733 }
Liu Boda2c7002017-02-10 16:41:05 +01001734out:
1735 if (err && index_ret)
1736 *index_ret = start_index + pages_locked - 1;
1737 return err;
Chris Masonc8b97812008-10-29 14:49:59 -04001738}
Chris Masonc8b97812008-10-29 14:49:59 -04001739
Liu Bo873695b2017-02-02 17:49:22 -08001740void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1741 u64 delalloc_end, struct page *locked_page,
1742 unsigned clear_bits,
1743 unsigned long page_ops)
1744{
1745 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
1746 NULL, GFP_NOFS);
1747
1748 __process_pages_contig(inode->i_mapping, locked_page,
1749 start >> PAGE_SHIFT, end >> PAGE_SHIFT,
Liu Boda2c7002017-02-10 16:41:05 +01001750 page_ops, NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001751}
1752
Chris Masond352ac62008-09-29 15:18:18 -04001753/*
1754 * count the number of bytes in the tree that have a given bit(s)
1755 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1756 * cached. The total number found is returned.
1757 */
Chris Masond1310b22008-01-24 16:13:08 -05001758u64 count_range_bits(struct extent_io_tree *tree,
1759 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001760 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001761{
1762 struct rb_node *node;
1763 struct extent_state *state;
1764 u64 cur_start = *start;
1765 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001766 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001767 int found = 0;
1768
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301769 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001770 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001771
Chris Masoncad321a2008-12-17 14:51:42 -05001772 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001773 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1774 total_bytes = tree->dirty_bytes;
1775 goto out;
1776 }
1777 /*
1778 * this search will find all the extents that end after
1779 * our range starts.
1780 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001781 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001782 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001783 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001784
Chris Masond3977122009-01-05 21:25:51 -05001785 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001786 state = rb_entry(node, struct extent_state, rb_node);
1787 if (state->start > search_end)
1788 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001789 if (contig && found && state->start > last + 1)
1790 break;
1791 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001792 total_bytes += min(search_end, state->end) + 1 -
1793 max(cur_start, state->start);
1794 if (total_bytes >= max_bytes)
1795 break;
1796 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001797 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001798 found = 1;
1799 }
Chris Masonec29ed52011-02-23 16:23:20 -05001800 last = state->end;
1801 } else if (contig && found) {
1802 break;
Chris Masond1310b22008-01-24 16:13:08 -05001803 }
1804 node = rb_next(node);
1805 if (!node)
1806 break;
1807 }
1808out:
Chris Masoncad321a2008-12-17 14:51:42 -05001809 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001810 return total_bytes;
1811}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001812
Chris Masond352ac62008-09-29 15:18:18 -04001813/*
1814 * set the private field for a given byte offset in the tree. If there isn't
1815 * an extent_state there already, this does nothing.
1816 */
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001817static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001818 struct io_failure_record *failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001819{
1820 struct rb_node *node;
1821 struct extent_state *state;
1822 int ret = 0;
1823
Chris Masoncad321a2008-12-17 14:51:42 -05001824 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001825 /*
1826 * this search will find all the extents that end after
1827 * our range starts.
1828 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001829 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001830 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001831 ret = -ENOENT;
1832 goto out;
1833 }
1834 state = rb_entry(node, struct extent_state, rb_node);
1835 if (state->start != start) {
1836 ret = -ENOENT;
1837 goto out;
1838 }
David Sterba47dc1962016-02-11 13:24:13 +01001839 state->failrec = failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001840out:
Chris Masoncad321a2008-12-17 14:51:42 -05001841 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001842 return ret;
1843}
1844
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001845static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001846 struct io_failure_record **failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001847{
1848 struct rb_node *node;
1849 struct extent_state *state;
1850 int ret = 0;
1851
Chris Masoncad321a2008-12-17 14:51:42 -05001852 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001853 /*
1854 * this search will find all the extents that end after
1855 * our range starts.
1856 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001857 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001858 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001859 ret = -ENOENT;
1860 goto out;
1861 }
1862 state = rb_entry(node, struct extent_state, rb_node);
1863 if (state->start != start) {
1864 ret = -ENOENT;
1865 goto out;
1866 }
David Sterba47dc1962016-02-11 13:24:13 +01001867 *failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001868out:
Chris Masoncad321a2008-12-17 14:51:42 -05001869 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001870 return ret;
1871}
1872
1873/*
1874 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001875 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001876 * has the bits set. Otherwise, 1 is returned if any bit in the
1877 * range is found set.
1878 */
1879int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001880 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001881{
1882 struct extent_state *state = NULL;
1883 struct rb_node *node;
1884 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001885
Chris Masoncad321a2008-12-17 14:51:42 -05001886 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01001887 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001888 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001889 node = &cached->rb_node;
1890 else
1891 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001892 while (node && start <= end) {
1893 state = rb_entry(node, struct extent_state, rb_node);
1894
1895 if (filled && state->start > start) {
1896 bitset = 0;
1897 break;
1898 }
1899
1900 if (state->start > end)
1901 break;
1902
1903 if (state->state & bits) {
1904 bitset = 1;
1905 if (!filled)
1906 break;
1907 } else if (filled) {
1908 bitset = 0;
1909 break;
1910 }
Chris Mason46562cec2009-09-23 20:23:16 -04001911
1912 if (state->end == (u64)-1)
1913 break;
1914
Chris Masond1310b22008-01-24 16:13:08 -05001915 start = state->end + 1;
1916 if (start > end)
1917 break;
1918 node = rb_next(node);
1919 if (!node) {
1920 if (filled)
1921 bitset = 0;
1922 break;
1923 }
1924 }
Chris Masoncad321a2008-12-17 14:51:42 -05001925 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001926 return bitset;
1927}
Chris Masond1310b22008-01-24 16:13:08 -05001928
1929/*
1930 * helper function to set a given page up to date if all the
1931 * extents in the tree for that page are up to date
1932 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001933static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001934{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001935 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001936 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001937 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001938 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001939}
1940
Josef Bacik7870d082017-05-05 11:57:15 -04001941int free_io_failure(struct extent_io_tree *failure_tree,
1942 struct extent_io_tree *io_tree,
1943 struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001944{
1945 int ret;
1946 int err = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001947
David Sterba47dc1962016-02-11 13:24:13 +01001948 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001949 ret = clear_extent_bits(failure_tree, rec->start,
1950 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001951 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001952 if (ret)
1953 err = ret;
1954
Josef Bacik7870d082017-05-05 11:57:15 -04001955 ret = clear_extent_bits(io_tree, rec->start,
David Woodhouse53b381b2013-01-29 18:40:14 -05001956 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001957 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05001958 if (ret && !err)
1959 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001960
1961 kfree(rec);
1962 return err;
1963}
1964
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001965/*
1966 * this bypasses the standard btrfs submit functions deliberately, as
1967 * the standard behavior is to write all copies in a raid setup. here we only
1968 * want to write the one bad copy. so we do the mapping for ourselves and issue
1969 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001970 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001971 * actually prevents the read that triggered the error from finishing.
1972 * currently, there can be no more than two copies of every data bit. thus,
1973 * exactly one rewrite is required.
1974 */
Josef Bacik6ec656b2017-05-05 11:57:14 -04001975int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
1976 u64 length, u64 logical, struct page *page,
1977 unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001978{
1979 struct bio *bio;
1980 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001981 u64 map_length = 0;
1982 u64 sector;
1983 struct btrfs_bio *bbio = NULL;
1984 int ret;
1985
Ilya Dryomov908960c2013-11-03 19:06:39 +02001986 ASSERT(!(fs_info->sb->s_flags & MS_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001987 BUG_ON(!mirror_num);
1988
David Sterbac5e4c3d2017-06-12 17:29:41 +02001989 bio = btrfs_io_bio_alloc(1);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001990 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001991 map_length = length;
1992
Filipe Mananab5de8d02016-05-27 22:21:27 +01001993 /*
1994 * Avoid races with device replace and make sure our bbio has devices
1995 * associated to its stripes that don't go away while we are doing the
1996 * read repair operation.
1997 */
1998 btrfs_bio_counter_inc_blocked(fs_info);
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03001999 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
Liu Boc7253282017-03-29 10:53:58 -07002000 /*
2001 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
2002 * to update all raid stripes, but here we just want to correct
2003 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2004 * stripe's dev and sector.
2005 */
2006 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2007 &map_length, &bbio, 0);
2008 if (ret) {
2009 btrfs_bio_counter_dec(fs_info);
2010 bio_put(bio);
2011 return -EIO;
2012 }
2013 ASSERT(bbio->mirror_num == 1);
2014 } else {
2015 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2016 &map_length, &bbio, mirror_num);
2017 if (ret) {
2018 btrfs_bio_counter_dec(fs_info);
2019 bio_put(bio);
2020 return -EIO;
2021 }
2022 BUG_ON(mirror_num != bbio->mirror_num);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002023 }
Liu Boc7253282017-03-29 10:53:58 -07002024
2025 sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002026 bio->bi_iter.bi_sector = sector;
Liu Boc7253282017-03-29 10:53:58 -07002027 dev = bbio->stripes[bbio->mirror_num - 1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002028 btrfs_put_bbio(bbio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002029 if (!dev || !dev->bdev || !dev->writeable) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002030 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002031 bio_put(bio);
2032 return -EIO;
2033 }
Christoph Hellwig74d46992017-08-23 19:10:32 +02002034 bio_set_dev(bio, dev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002035 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Miao Xieffdd2012014-09-12 18:44:00 +08002036 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002037
Mike Christie4e49ea42016-06-05 14:31:41 -05002038 if (btrfsic_submit_bio_wait(bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002039 /* try to remap that extent elsewhere? */
Filipe Mananab5de8d02016-05-27 22:21:27 +01002040 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002041 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002042 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002043 return -EIO;
2044 }
2045
David Sterbab14af3b2015-10-08 10:43:10 +02002046 btrfs_info_rl_in_rcu(fs_info,
2047 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Josef Bacik6ec656b2017-05-05 11:57:14 -04002048 ino, start,
Miao Xie1203b682014-09-12 18:44:01 +08002049 rcu_str_deref(dev->name), sector);
Filipe Mananab5de8d02016-05-27 22:21:27 +01002050 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002051 bio_put(bio);
2052 return 0;
2053}
2054
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002055int repair_eb_io_failure(struct btrfs_fs_info *fs_info,
2056 struct extent_buffer *eb, int mirror_num)
Josef Bacikea466792012-03-26 21:57:36 -04002057{
Josef Bacikea466792012-03-26 21:57:36 -04002058 u64 start = eb->start;
2059 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04002060 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002061
David Howellsbc98a422017-07-17 08:45:34 +01002062 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002063 return -EROFS;
2064
Josef Bacikea466792012-03-26 21:57:36 -04002065 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002066 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002067
Josef Bacik6ec656b2017-05-05 11:57:14 -04002068 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002069 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002070 if (ret)
2071 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002072 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002073 }
2074
2075 return ret;
2076}
2077
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002078/*
2079 * each time an IO finishes, we do a fast check in the IO failure tree
2080 * to see if we need to process or clean up an io_failure_record
2081 */
Josef Bacik7870d082017-05-05 11:57:15 -04002082int clean_io_failure(struct btrfs_fs_info *fs_info,
2083 struct extent_io_tree *failure_tree,
2084 struct extent_io_tree *io_tree, u64 start,
2085 struct page *page, u64 ino, unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002086{
2087 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002088 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002089 struct extent_state *state;
2090 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002091 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002092
2093 private = 0;
Josef Bacik7870d082017-05-05 11:57:15 -04002094 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2095 EXTENT_DIRTY, 0);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002096 if (!ret)
2097 return 0;
2098
Josef Bacik7870d082017-05-05 11:57:15 -04002099 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002100 if (ret)
2101 return 0;
2102
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002103 BUG_ON(!failrec->this_mirror);
2104
2105 if (failrec->in_validation) {
2106 /* there was no real error, just free the record */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002107 btrfs_debug(fs_info,
2108 "clean_io_failure: freeing dummy error at %llu",
2109 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002110 goto out;
2111 }
David Howellsbc98a422017-07-17 08:45:34 +01002112 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002113 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002114
Josef Bacik7870d082017-05-05 11:57:15 -04002115 spin_lock(&io_tree->lock);
2116 state = find_first_extent_bit_state(io_tree,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002117 failrec->start,
2118 EXTENT_LOCKED);
Josef Bacik7870d082017-05-05 11:57:15 -04002119 spin_unlock(&io_tree->lock);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002120
Miao Xie883d0de2013-07-25 19:22:35 +08002121 if (state && state->start <= failrec->start &&
2122 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002123 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2124 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002125 if (num_copies > 1) {
Josef Bacik7870d082017-05-05 11:57:15 -04002126 repair_io_failure(fs_info, ino, start, failrec->len,
2127 failrec->logical, page, pg_offset,
2128 failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002129 }
2130 }
2131
2132out:
Josef Bacik7870d082017-05-05 11:57:15 -04002133 free_io_failure(failure_tree, io_tree, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002134
Miao Xie454ff3d2014-09-12 18:43:58 +08002135 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002136}
2137
Miao Xief6124962014-09-12 18:44:04 +08002138/*
2139 * Can be called when
2140 * - hold extent lock
2141 * - under ordered extent
2142 * - the inode is freeing
2143 */
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002144void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
Miao Xief6124962014-09-12 18:44:04 +08002145{
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002146 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
Miao Xief6124962014-09-12 18:44:04 +08002147 struct io_failure_record *failrec;
2148 struct extent_state *state, *next;
2149
2150 if (RB_EMPTY_ROOT(&failure_tree->state))
2151 return;
2152
2153 spin_lock(&failure_tree->lock);
2154 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2155 while (state) {
2156 if (state->start > end)
2157 break;
2158
2159 ASSERT(state->end <= end);
2160
2161 next = next_state(state);
2162
David Sterba47dc1962016-02-11 13:24:13 +01002163 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002164 free_extent_state(state);
2165 kfree(failrec);
2166
2167 state = next;
2168 }
2169 spin_unlock(&failure_tree->lock);
2170}
2171
Miao Xie2fe63032014-09-12 18:43:59 +08002172int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
David Sterba47dc1962016-02-11 13:24:13 +01002173 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002174{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002175 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002176 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002177 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002178 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2179 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2180 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002181 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002182 u64 logical;
2183
David Sterba47dc1962016-02-11 13:24:13 +01002184 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002185 if (ret) {
2186 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2187 if (!failrec)
2188 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002189
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002190 failrec->start = start;
2191 failrec->len = end - start + 1;
2192 failrec->this_mirror = 0;
2193 failrec->bio_flags = 0;
2194 failrec->in_validation = 0;
2195
2196 read_lock(&em_tree->lock);
2197 em = lookup_extent_mapping(em_tree, start, failrec->len);
2198 if (!em) {
2199 read_unlock(&em_tree->lock);
2200 kfree(failrec);
2201 return -EIO;
2202 }
2203
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002204 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002205 free_extent_map(em);
2206 em = NULL;
2207 }
2208 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002209 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002210 kfree(failrec);
2211 return -EIO;
2212 }
Miao Xie2fe63032014-09-12 18:43:59 +08002213
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002214 logical = start - em->start;
2215 logical = em->block_start + logical;
2216 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2217 logical = em->block_start;
2218 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2219 extent_set_compress_type(&failrec->bio_flags,
2220 em->compress_type);
2221 }
Miao Xie2fe63032014-09-12 18:43:59 +08002222
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002223 btrfs_debug(fs_info,
2224 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2225 logical, start, failrec->len);
Miao Xie2fe63032014-09-12 18:43:59 +08002226
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002227 failrec->logical = logical;
2228 free_extent_map(em);
2229
2230 /* set the bits in the private failure tree */
2231 ret = set_extent_bits(failure_tree, start, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002232 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002233 if (ret >= 0)
David Sterba47dc1962016-02-11 13:24:13 +01002234 ret = set_state_failrec(failure_tree, start, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002235 /* set the bits in the inode's tree */
2236 if (ret >= 0)
David Sterbaceeb0ae2016-04-26 23:54:39 +02002237 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002238 if (ret < 0) {
2239 kfree(failrec);
2240 return ret;
2241 }
2242 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002243 btrfs_debug(fs_info,
2244 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2245 failrec->logical, failrec->start, failrec->len,
2246 failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002247 /*
2248 * when data can be on disk more than twice, add to failrec here
2249 * (e.g. with a list for failed_mirror) to make
2250 * clean_io_failure() clean all those errors at once.
2251 */
2252 }
Miao Xie2fe63032014-09-12 18:43:59 +08002253
2254 *failrec_ret = failrec;
2255
2256 return 0;
2257}
2258
Liu Boc3cfb652017-07-13 15:00:50 -07002259bool btrfs_check_repairable(struct inode *inode, struct bio *failed_bio,
Miao Xie2fe63032014-09-12 18:43:59 +08002260 struct io_failure_record *failrec, int failed_mirror)
2261{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002262 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002263 int num_copies;
2264
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002265 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002266 if (num_copies == 1) {
2267 /*
2268 * we only have a single copy of the data, so don't bother with
2269 * all the retry and error correction code that follows. no
2270 * matter what the error is, it is very likely to persist.
2271 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002272 btrfs_debug(fs_info,
2273 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2274 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002275 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002276 }
2277
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002278 /*
2279 * there are two premises:
2280 * a) deliver good data to the caller
2281 * b) correct the bad sectors on disk
2282 */
2283 if (failed_bio->bi_vcnt > 1) {
2284 /*
2285 * to fulfill b), we need to know the exact failing sectors, as
2286 * we don't want to rewrite any more than the failed ones. thus,
2287 * we need separate read requests for the failed bio
2288 *
2289 * if the following BUG_ON triggers, our validation request got
2290 * merged. we need separate requests for our algorithm to work.
2291 */
2292 BUG_ON(failrec->in_validation);
2293 failrec->in_validation = 1;
2294 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002295 } else {
2296 /*
2297 * we're ready to fulfill a) and b) alongside. get a good copy
2298 * of the failed sector and if we succeed, we have setup
2299 * everything for repair_io_failure to do the rest for us.
2300 */
2301 if (failrec->in_validation) {
2302 BUG_ON(failrec->this_mirror != failed_mirror);
2303 failrec->in_validation = 0;
2304 failrec->this_mirror = 0;
2305 }
2306 failrec->failed_mirror = failed_mirror;
2307 failrec->this_mirror++;
2308 if (failrec->this_mirror == failed_mirror)
2309 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002310 }
2311
Miao Xiefacc8a222013-07-25 19:22:34 +08002312 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002313 btrfs_debug(fs_info,
2314 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2315 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002316 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002317 }
2318
Liu Boc3cfb652017-07-13 15:00:50 -07002319 return true;
Miao Xie2fe63032014-09-12 18:43:59 +08002320}
2321
2322
2323struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2324 struct io_failure_record *failrec,
2325 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002326 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002327{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002328 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002329 struct bio *bio;
2330 struct btrfs_io_bio *btrfs_failed_bio;
2331 struct btrfs_io_bio *btrfs_bio;
2332
David Sterbac5e4c3d2017-06-12 17:29:41 +02002333 bio = btrfs_io_bio_alloc(1);
Miao Xie2fe63032014-09-12 18:43:59 +08002334 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002335 bio->bi_iter.bi_sector = failrec->logical >> 9;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002336 bio_set_dev(bio, fs_info->fs_devices->latest_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002337 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002338 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002339
Miao Xiefacc8a222013-07-25 19:22:34 +08002340 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2341 if (btrfs_failed_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002342 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2343
2344 btrfs_bio = btrfs_io_bio(bio);
2345 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002346 icsum *= csum_size;
2347 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002348 csum_size);
2349 }
2350
Miao Xie2fe63032014-09-12 18:43:59 +08002351 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002352
Miao Xie2fe63032014-09-12 18:43:59 +08002353 return bio;
2354}
2355
2356/*
2357 * this is a generic handler for readpage errors (default
2358 * readpage_io_failed_hook). if other copies exist, read those and write back
2359 * good data to the failed position. does not investigate in remapping the
2360 * failed extent elsewhere, hoping the device will be smart enough to do this as
2361 * needed
2362 */
2363
2364static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2365 struct page *page, u64 start, u64 end,
2366 int failed_mirror)
2367{
2368 struct io_failure_record *failrec;
2369 struct inode *inode = page->mapping->host;
2370 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002371 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie2fe63032014-09-12 18:43:59 +08002372 struct bio *bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002373 int read_mode = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002374 blk_status_t status;
Miao Xie2fe63032014-09-12 18:43:59 +08002375 int ret;
2376
Mike Christie1f7ad752016-06-05 14:31:51 -05002377 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002378
2379 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2380 if (ret)
2381 return ret;
2382
Liu Boc3cfb652017-07-13 15:00:50 -07002383 if (!btrfs_check_repairable(inode, failed_bio, failrec,
2384 failed_mirror)) {
Josef Bacik7870d082017-05-05 11:57:15 -04002385 free_io_failure(failure_tree, tree, failrec);
Miao Xie2fe63032014-09-12 18:43:59 +08002386 return -EIO;
2387 }
2388
2389 if (failed_bio->bi_vcnt > 1)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002390 read_mode |= REQ_FAILFAST_DEV;
Miao Xie2fe63032014-09-12 18:43:59 +08002391
2392 phy_offset >>= inode->i_sb->s_blocksize_bits;
2393 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2394 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002395 (int)phy_offset, failed_bio->bi_end_io,
2396 NULL);
Mike Christie1f7ad752016-06-05 14:31:51 -05002397 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie2fe63032014-09-12 18:43:59 +08002398
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002399 btrfs_debug(btrfs_sb(inode->i_sb),
2400 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2401 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002402
Linus Torvalds8c27cb32017-07-05 16:41:23 -07002403 status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002404 failrec->bio_flags, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002405 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04002406 free_io_failure(failure_tree, tree, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002407 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002408 ret = blk_status_to_errno(status);
Miao Xie6c387ab2014-09-12 18:43:57 +08002409 }
2410
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002411 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002412}
2413
Chris Masond1310b22008-01-24 16:13:08 -05002414/* lots and lots of room for performance fixes in the end_bio funcs */
2415
David Sterbab5227c02015-12-03 13:08:59 +01002416void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002417{
2418 int uptodate = (err == 0);
2419 struct extent_io_tree *tree;
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002420 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002421
2422 tree = &BTRFS_I(page->mapping->host)->io_tree;
2423
David Sterbac3988d62017-02-17 15:18:32 +01002424 if (tree->ops && tree->ops->writepage_end_io_hook)
2425 tree->ops->writepage_end_io_hook(page, start, end, NULL,
2426 uptodate);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002427
Jeff Mahoney87826df2012-02-15 16:23:57 +01002428 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002429 ClearPageUptodate(page);
2430 SetPageError(page);
Colin Ian Kingbff5baf2017-05-09 18:14:01 +01002431 ret = err < 0 ? err : -EIO;
Liu Bo5dca6ee2014-05-12 12:47:36 +08002432 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002433 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002434}
2435
Chris Masond1310b22008-01-24 16:13:08 -05002436/*
2437 * after a writepage IO is done, we need to:
2438 * clear the uptodate bits on error
2439 * clear the writeback bits in the extent tree for this IO
2440 * end_page_writeback if the page has no more pending IO
2441 *
2442 * Scheduling is not allowed, so the extent state tree is expected
2443 * to have one and only one object corresponding to this IO.
2444 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002445static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002446{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002447 int error = blk_status_to_errno(bio->bi_status);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002448 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002449 u64 start;
2450 u64 end;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002451 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002452
David Sterbac09abff2017-07-13 18:10:07 +02002453 ASSERT(!bio_flagged(bio, BIO_CLONED));
Kent Overstreet2c30c712013-11-07 12:20:26 -08002454 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002455 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002456 struct inode *inode = page->mapping->host;
2457 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002458
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002459 /* We always issue full-page reads, but if some block
2460 * in a page fails to read, blk_update_request() will
2461 * advance bv_offset and adjust bv_len to compensate.
2462 * Print a warning for nonzero offsets, and an error
2463 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002464 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2465 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002466 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002467 "partial page write in btrfs with offset %u and length %u",
2468 bvec->bv_offset, bvec->bv_len);
2469 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002470 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002471 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002472 bvec->bv_offset, bvec->bv_len);
2473 }
Chris Masond1310b22008-01-24 16:13:08 -05002474
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002475 start = page_offset(page);
2476 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002477
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002478 end_extent_writepage(page, error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002479 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002480 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002481
Chris Masond1310b22008-01-24 16:13:08 -05002482 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002483}
2484
Miao Xie883d0de2013-07-25 19:22:35 +08002485static void
2486endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2487 int uptodate)
2488{
2489 struct extent_state *cached = NULL;
2490 u64 end = start + len - 1;
2491
2492 if (uptodate && tree->track_uptodate)
2493 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
2494 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2495}
2496
Chris Masond1310b22008-01-24 16:13:08 -05002497/*
2498 * after a readpage IO is done, we need to:
2499 * clear the uptodate bits on error
2500 * set the uptodate bits if things worked
2501 * set the page up to date if all extents in the tree are uptodate
2502 * clear the lock bit in the extent tree
2503 * unlock the page if there are no other extents locked for it
2504 *
2505 * Scheduling is not allowed, so the extent state tree is expected
2506 * to have one and only one object corresponding to this IO.
2507 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002508static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002509{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002510 struct bio_vec *bvec;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002511 int uptodate = !bio->bi_status;
Miao Xiefacc8a222013-07-25 19:22:34 +08002512 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04002513 struct extent_io_tree *tree, *failure_tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002514 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002515 u64 start;
2516 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002517 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002518 u64 extent_start = 0;
2519 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002520 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002521 int ret;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002522 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002523
David Sterbac09abff2017-07-13 18:10:07 +02002524 ASSERT(!bio_flagged(bio, BIO_CLONED));
Kent Overstreet2c30c712013-11-07 12:20:26 -08002525 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002526 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002527 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002528 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen507903b2011-04-06 10:02:20 +00002529
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002530 btrfs_debug(fs_info,
2531 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002532 (u64)bio->bi_iter.bi_sector, bio->bi_status,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002533 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002534 tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002535 failure_tree = &BTRFS_I(inode)->io_failure_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002536
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002537 /* We always issue full-page reads, but if some block
2538 * in a page fails to read, blk_update_request() will
2539 * advance bv_offset and adjust bv_len to compensate.
2540 * Print a warning for nonzero offsets, and an error
2541 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002542 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2543 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002544 btrfs_err(fs_info,
2545 "partial page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002546 bvec->bv_offset, bvec->bv_len);
2547 else
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002548 btrfs_info(fs_info,
2549 "incomplete page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002550 bvec->bv_offset, bvec->bv_len);
2551 }
Chris Masond1310b22008-01-24 16:13:08 -05002552
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002553 start = page_offset(page);
2554 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002555 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002556
Chris Mason9be33952013-05-17 18:30:14 -04002557 mirror = io_bio->mirror_num;
David Sterba20c98012017-02-17 15:59:35 +01002558 if (likely(uptodate && tree->ops)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002559 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2560 page, start, end,
2561 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002562 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002563 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002564 else
Josef Bacik7870d082017-05-05 11:57:15 -04002565 clean_io_failure(BTRFS_I(inode)->root->fs_info,
2566 failure_tree, tree, start,
2567 page,
2568 btrfs_ino(BTRFS_I(inode)), 0);
Chris Masond1310b22008-01-24 16:13:08 -05002569 }
Josef Bacikea466792012-03-26 21:57:36 -04002570
Miao Xief2a09da2013-07-25 19:22:33 +08002571 if (likely(uptodate))
2572 goto readpage_ok;
2573
David Sterba20a7db82017-02-17 16:24:29 +01002574 if (tree->ops) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002575 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Liu Bo9d0d1c82017-03-24 15:04:50 -07002576 if (ret == -EAGAIN) {
2577 /*
2578 * Data inode's readpage_io_failed_hook() always
2579 * returns -EAGAIN.
2580 *
2581 * The generic bio_readpage_error handles errors
2582 * the following way: If possible, new read
2583 * requests are created and submitted and will
2584 * end up in end_bio_extent_readpage as well (if
2585 * we're lucky, not in the !uptodate case). In
2586 * that case it returns 0 and we just go on with
2587 * the next page in our bio. If it can't handle
2588 * the error it will return -EIO and we remain
2589 * responsible for that page.
2590 */
2591 ret = bio_readpage_error(bio, offset, page,
2592 start, end, mirror);
2593 if (ret == 0) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002594 uptodate = !bio->bi_status;
Liu Bo9d0d1c82017-03-24 15:04:50 -07002595 offset += len;
2596 continue;
2597 }
Chris Mason7e383262008-04-09 16:28:12 -04002598 }
Liu Bo9d0d1c82017-03-24 15:04:50 -07002599
2600 /*
2601 * metadata's readpage_io_failed_hook() always returns
2602 * -EIO and fixes nothing. -EIO is also returned if
2603 * data inode error could not be fixed.
2604 */
2605 ASSERT(ret == -EIO);
Chris Mason7e383262008-04-09 16:28:12 -04002606 }
Miao Xief2a09da2013-07-25 19:22:33 +08002607readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002608 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002609 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002610 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002611 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002612
2613 /* Zero out the end if this page straddles i_size */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002614 off = i_size & (PAGE_SIZE-1);
Liu Boa583c022014-08-19 23:32:22 +08002615 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002616 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002617 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002618 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002619 ClearPageUptodate(page);
2620 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002621 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002622 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002623 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002624
2625 if (unlikely(!uptodate)) {
2626 if (extent_len) {
2627 endio_readpage_release_extent(tree,
2628 extent_start,
2629 extent_len, 1);
2630 extent_start = 0;
2631 extent_len = 0;
2632 }
2633 endio_readpage_release_extent(tree, start,
2634 end - start + 1, 0);
2635 } else if (!extent_len) {
2636 extent_start = start;
2637 extent_len = end + 1 - start;
2638 } else if (extent_start + extent_len == start) {
2639 extent_len += end + 1 - start;
2640 } else {
2641 endio_readpage_release_extent(tree, extent_start,
2642 extent_len, uptodate);
2643 extent_start = start;
2644 extent_len = end + 1 - start;
2645 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002646 }
Chris Masond1310b22008-01-24 16:13:08 -05002647
Miao Xie883d0de2013-07-25 19:22:35 +08002648 if (extent_len)
2649 endio_readpage_release_extent(tree, extent_start, extent_len,
2650 uptodate);
Miao Xiefacc8a222013-07-25 19:22:34 +08002651 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002652 io_bio->end_io(io_bio, blk_status_to_errno(bio->bi_status));
Chris Masond1310b22008-01-24 16:13:08 -05002653 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002654}
2655
Chris Mason9be33952013-05-17 18:30:14 -04002656/*
David Sterba184f9992017-06-12 17:29:39 +02002657 * Initialize the members up to but not including 'bio'. Use after allocating a
2658 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
2659 * 'bio' because use of __GFP_ZERO is not supported.
Chris Mason9be33952013-05-17 18:30:14 -04002660 */
David Sterba184f9992017-06-12 17:29:39 +02002661static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
Chris Masond1310b22008-01-24 16:13:08 -05002662{
David Sterba184f9992017-06-12 17:29:39 +02002663 memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
2664}
2665
2666/*
David Sterba6e707bc2017-06-02 17:26:26 +02002667 * The following helpers allocate a bio. As it's backed by a bioset, it'll
2668 * never fail. We're returning a bio right now but you can call btrfs_io_bio
2669 * for the appropriate container_of magic
Chris Masond1310b22008-01-24 16:13:08 -05002670 */
David Sterbac821e7f32017-06-02 18:35:36 +02002671struct bio *btrfs_bio_alloc(struct block_device *bdev, u64 first_byte)
Chris Masond1310b22008-01-24 16:13:08 -05002672{
2673 struct bio *bio;
2674
David Sterba9f2179a2017-06-02 17:55:44 +02002675 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, btrfs_bioset);
Christoph Hellwig74d46992017-08-23 19:10:32 +02002676 bio_set_dev(bio, bdev);
David Sterbac821e7f32017-06-02 18:35:36 +02002677 bio->bi_iter.bi_sector = first_byte >> 9;
David Sterba184f9992017-06-12 17:29:39 +02002678 btrfs_io_bio_init(btrfs_io_bio(bio));
Chris Masond1310b22008-01-24 16:13:08 -05002679 return bio;
2680}
2681
David Sterba8b6c1d52017-06-02 17:48:13 +02002682struct bio *btrfs_bio_clone(struct bio *bio)
Chris Mason9be33952013-05-17 18:30:14 -04002683{
Miao Xie23ea8e52014-09-12 18:43:54 +08002684 struct btrfs_io_bio *btrfs_bio;
2685 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002686
David Sterba6e707bc2017-06-02 17:26:26 +02002687 /* Bio allocation backed by a bioset does not fail */
David Sterba8b6c1d52017-06-02 17:48:13 +02002688 new = bio_clone_fast(bio, GFP_NOFS, btrfs_bioset);
David Sterba6e707bc2017-06-02 17:26:26 +02002689 btrfs_bio = btrfs_io_bio(new);
David Sterba184f9992017-06-12 17:29:39 +02002690 btrfs_io_bio_init(btrfs_bio);
David Sterba6e707bc2017-06-02 17:26:26 +02002691 btrfs_bio->iter = bio->bi_iter;
Miao Xie23ea8e52014-09-12 18:43:54 +08002692 return new;
2693}
Chris Mason9be33952013-05-17 18:30:14 -04002694
David Sterbac5e4c3d2017-06-12 17:29:41 +02002695struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
Chris Mason9be33952013-05-17 18:30:14 -04002696{
Miao Xiefacc8a222013-07-25 19:22:34 +08002697 struct bio *bio;
2698
David Sterba6e707bc2017-06-02 17:26:26 +02002699 /* Bio allocation backed by a bioset does not fail */
David Sterbac5e4c3d2017-06-12 17:29:41 +02002700 bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, btrfs_bioset);
David Sterba184f9992017-06-12 17:29:39 +02002701 btrfs_io_bio_init(btrfs_io_bio(bio));
Miao Xiefacc8a222013-07-25 19:22:34 +08002702 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002703}
2704
Liu Boe4770942017-05-16 10:57:14 -07002705struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
Liu Bo2f8e9142017-05-15 17:43:31 -07002706{
2707 struct bio *bio;
2708 struct btrfs_io_bio *btrfs_bio;
2709
2710 /* this will never fail when it's backed by a bioset */
Liu Boe4770942017-05-16 10:57:14 -07002711 bio = bio_clone_fast(orig, GFP_NOFS, btrfs_bioset);
Liu Bo2f8e9142017-05-15 17:43:31 -07002712 ASSERT(bio);
2713
2714 btrfs_bio = btrfs_io_bio(bio);
David Sterba184f9992017-06-12 17:29:39 +02002715 btrfs_io_bio_init(btrfs_bio);
Liu Bo2f8e9142017-05-15 17:43:31 -07002716
2717 bio_trim(bio, offset >> 9, size >> 9);
Liu Bo17347ce2017-05-15 15:33:27 -07002718 btrfs_bio->iter = bio->bi_iter;
Liu Bo2f8e9142017-05-15 17:43:31 -07002719 return bio;
2720}
Chris Mason9be33952013-05-17 18:30:14 -04002721
Mike Christie1f7ad752016-06-05 14:31:51 -05002722static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
2723 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002724{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002725 blk_status_t ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002726 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2727 struct page *page = bvec->bv_page;
2728 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002729 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002730
Miao Xie4eee4fa2012-12-21 09:17:45 +00002731 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002732
David Woodhouse902b22f2008-08-20 08:51:49 -04002733 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002734 bio_get(bio);
2735
David Sterba20c98012017-02-17 15:59:35 +01002736 if (tree->ops)
Josef Bacikc6100a42017-05-05 11:57:13 -04002737 ret = tree->ops->submit_bio_hook(tree->private_data, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002738 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002739 else
Mike Christie4e49ea42016-06-05 14:31:41 -05002740 btrfsic_submit_bio(bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002741
Chris Masond1310b22008-01-24 16:13:08 -05002742 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002743 return blk_status_to_errno(ret);
Chris Masond1310b22008-01-24 16:13:08 -05002744}
2745
Mike Christie1f7ad752016-06-05 14:31:51 -05002746static int merge_bio(struct extent_io_tree *tree, struct page *page,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002747 unsigned long offset, size_t size, struct bio *bio,
2748 unsigned long bio_flags)
2749{
2750 int ret = 0;
David Sterba20c98012017-02-17 15:59:35 +01002751 if (tree->ops)
Mike Christie81a75f672016-06-05 14:31:54 -05002752 ret = tree->ops->merge_bio_hook(page, offset, size, bio,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002753 bio_flags);
Jeff Mahoney3444a972011-10-03 23:23:13 -04002754 return ret;
2755
2756}
2757
David Sterba4b81ba42017-06-06 19:14:26 +02002758/*
2759 * @opf: bio REQ_OP_* and REQ_* flags as one value
2760 */
2761static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002762 struct writeback_control *wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02002763 struct page *page, u64 offset,
David Sterba6c5a4e22017-10-04 17:10:34 +02002764 size_t size, unsigned long pg_offset,
Chris Masond1310b22008-01-24 16:13:08 -05002765 struct block_device *bdev,
2766 struct bio **bio_ret,
Chris Masonf1885912008-04-09 16:28:12 -04002767 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002768 int mirror_num,
2769 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002770 unsigned long bio_flags,
2771 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002772{
2773 int ret = 0;
2774 struct bio *bio;
Chris Masonc8b97812008-10-29 14:49:59 -04002775 int contig = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002776 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002777 size_t page_size = min_t(size_t, size, PAGE_SIZE);
David Sterba6273b7f2017-10-04 17:30:11 +02002778 sector_t sector = offset >> 9;
Chris Masond1310b22008-01-24 16:13:08 -05002779
2780 if (bio_ret && *bio_ret) {
2781 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002782 if (old_compressed)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002783 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002784 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002785 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002786
2787 if (prev_bio_flags != bio_flags || !contig ||
Filipe Manana005efed2015-09-14 09:09:31 +01002788 force_bio_submit ||
David Sterba6c5a4e22017-10-04 17:10:34 +02002789 merge_bio(tree, page, pg_offset, page_size, bio, bio_flags) ||
2790 bio_add_page(bio, page, page_size, pg_offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002791 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002792 if (ret < 0) {
2793 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002794 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002795 }
Chris Masond1310b22008-01-24 16:13:08 -05002796 bio = NULL;
2797 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002798 if (wbc)
2799 wbc_account_io(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002800 return 0;
2801 }
2802 }
Chris Masonc8b97812008-10-29 14:49:59 -04002803
David Sterba6273b7f2017-10-04 17:30:11 +02002804 bio = btrfs_bio_alloc(bdev, offset);
David Sterba6c5a4e22017-10-04 17:10:34 +02002805 bio_add_page(bio, page, page_size, pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002806 bio->bi_end_io = end_io_func;
2807 bio->bi_private = tree;
Jens Axboee6959b92017-06-27 11:51:28 -06002808 bio->bi_write_hint = page->mapping->host->i_write_hint;
David Sterba4b81ba42017-06-06 19:14:26 +02002809 bio->bi_opf = opf;
Chris Masonda2f0f72015-07-02 13:57:22 -07002810 if (wbc) {
2811 wbc_init_bio(wbc, bio);
2812 wbc_account_io(wbc, page, page_size);
2813 }
Chris Mason70dec802008-01-29 09:59:12 -05002814
Chris Masond3977122009-01-05 21:25:51 -05002815 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002816 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002817 else
Mike Christie1f7ad752016-06-05 14:31:51 -05002818 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002819
2820 return ret;
2821}
2822
Eric Sandeen48a3b632013-04-25 20:41:01 +00002823static void attach_extent_buffer_page(struct extent_buffer *eb,
2824 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002825{
2826 if (!PagePrivate(page)) {
2827 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002828 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002829 set_page_private(page, (unsigned long)eb);
2830 } else {
2831 WARN_ON(page->private != (unsigned long)eb);
2832 }
2833}
2834
Chris Masond1310b22008-01-24 16:13:08 -05002835void set_page_extent_mapped(struct page *page)
2836{
2837 if (!PagePrivate(page)) {
2838 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002839 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002840 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002841 }
2842}
2843
Miao Xie125bac012013-07-25 19:22:37 +08002844static struct extent_map *
2845__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2846 u64 start, u64 len, get_extent_t *get_extent,
2847 struct extent_map **em_cached)
2848{
2849 struct extent_map *em;
2850
2851 if (em_cached && *em_cached) {
2852 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002853 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002854 start < extent_map_end(em)) {
Elena Reshetova490b54d2017-03-03 10:55:12 +02002855 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002856 return em;
2857 }
2858
2859 free_extent_map(em);
2860 *em_cached = NULL;
2861 }
2862
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02002863 em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
Miao Xie125bac012013-07-25 19:22:37 +08002864 if (em_cached && !IS_ERR_OR_NULL(em)) {
2865 BUG_ON(*em_cached);
Elena Reshetova490b54d2017-03-03 10:55:12 +02002866 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002867 *em_cached = em;
2868 }
2869 return em;
2870}
Chris Masond1310b22008-01-24 16:13:08 -05002871/*
2872 * basic readpage implementation. Locked extent state structs are inserted
2873 * into the tree that are removed when the IO is done (by the end_io
2874 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002875 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07002876 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05002877 */
Miao Xie99740902013-07-25 19:22:36 +08002878static int __do_readpage(struct extent_io_tree *tree,
2879 struct page *page,
2880 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002881 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002882 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02002883 unsigned long *bio_flags, unsigned int read_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002884 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05002885{
2886 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002887 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002888 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002889 u64 end;
2890 u64 cur = start;
2891 u64 extent_offset;
2892 u64 last_byte = i_size_read(inode);
2893 u64 block_start;
2894 u64 cur_end;
Chris Masond1310b22008-01-24 16:13:08 -05002895 struct extent_map *em;
2896 struct block_device *bdev;
Liu Bobaf863b2016-07-11 10:39:07 -07002897 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002898 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002899 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002900 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002901 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002902 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00002903 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002904
2905 set_page_extent_mapped(page);
2906
Miao Xie99740902013-07-25 19:22:36 +08002907 end = page_end;
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002908 if (!PageUptodate(page)) {
2909 if (cleancache_get_page(page) == 0) {
2910 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08002911 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002912 goto out;
2913 }
2914 }
2915
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002916 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04002917 char *userpage;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002918 size_t zero_offset = last_byte & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002919
2920 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002921 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002922 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002923 memset(userpage + zero_offset, 0, iosize);
2924 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002925 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002926 }
2927 }
Chris Masond1310b22008-01-24 16:13:08 -05002928 while (cur <= end) {
Filipe Manana005efed2015-09-14 09:09:31 +01002929 bool force_bio_submit = false;
David Sterba6273b7f2017-10-04 17:30:11 +02002930 u64 offset;
Josef Bacikc8f2f242013-02-11 11:33:00 -05002931
Chris Masond1310b22008-01-24 16:13:08 -05002932 if (cur >= last_byte) {
2933 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002934 struct extent_state *cached = NULL;
2935
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002936 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002937 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002938 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002939 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002940 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002941 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002942 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00002943 unlock_extent_cached(tree, cur,
2944 cur + iosize - 1,
2945 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002946 break;
2947 }
Miao Xie125bac012013-07-25 19:22:37 +08002948 em = __get_extent_map(inode, page, pg_offset, cur,
2949 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02002950 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002951 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00002952 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002953 break;
2954 }
Chris Masond1310b22008-01-24 16:13:08 -05002955 extent_offset = cur - em->start;
2956 BUG_ON(extent_map_end(em) <= cur);
2957 BUG_ON(end < cur);
2958
Li Zefan261507a02010-12-17 14:21:50 +08002959 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07002960 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002961 extent_set_compress_type(&this_bio_flag,
2962 em->compress_type);
2963 }
Chris Masonc8b97812008-10-29 14:49:59 -04002964
Chris Masond1310b22008-01-24 16:13:08 -05002965 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2966 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002967 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002968 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2969 disk_io_size = em->block_len;
David Sterba6273b7f2017-10-04 17:30:11 +02002970 offset = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002971 } else {
David Sterba6273b7f2017-10-04 17:30:11 +02002972 offset = em->block_start + extent_offset;
Chris Masonc8b97812008-10-29 14:49:59 -04002973 disk_io_size = iosize;
2974 }
Chris Masond1310b22008-01-24 16:13:08 -05002975 bdev = em->bdev;
2976 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002977 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2978 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01002979
2980 /*
2981 * If we have a file range that points to a compressed extent
2982 * and it's followed by a consecutive file range that points to
2983 * to the same compressed extent (possibly with a different
2984 * offset and/or length, so it either points to the whole extent
2985 * or only part of it), we must make sure we do not submit a
2986 * single bio to populate the pages for the 2 ranges because
2987 * this makes the compressed extent read zero out the pages
2988 * belonging to the 2nd range. Imagine the following scenario:
2989 *
2990 * File layout
2991 * [0 - 8K] [8K - 24K]
2992 * | |
2993 * | |
2994 * points to extent X, points to extent X,
2995 * offset 4K, length of 8K offset 0, length 16K
2996 *
2997 * [extent X, compressed length = 4K uncompressed length = 16K]
2998 *
2999 * If the bio to read the compressed extent covers both ranges,
3000 * it will decompress extent X into the pages belonging to the
3001 * first range and then it will stop, zeroing out the remaining
3002 * pages that belong to the other range that points to extent X.
3003 * So here we make sure we submit 2 bios, one for the first
3004 * range and another one for the third range. Both will target
3005 * the same physical extent from disk, but we can't currently
3006 * make the compressed bio endio callback populate the pages
3007 * for both ranges because each compressed bio is tightly
3008 * coupled with a single extent map, and each range can have
3009 * an extent map with a different offset value relative to the
3010 * uncompressed data of our extent and different lengths. This
3011 * is a corner case so we prioritize correctness over
3012 * non-optimal behavior (submitting 2 bios for the same extent).
3013 */
3014 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3015 prev_em_start && *prev_em_start != (u64)-1 &&
3016 *prev_em_start != em->orig_start)
3017 force_bio_submit = true;
3018
3019 if (prev_em_start)
3020 *prev_em_start = em->orig_start;
3021
Chris Masond1310b22008-01-24 16:13:08 -05003022 free_extent_map(em);
3023 em = NULL;
3024
3025 /* we've found a hole, just zero and go on */
3026 if (block_start == EXTENT_MAP_HOLE) {
3027 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003028 struct extent_state *cached = NULL;
3029
Cong Wang7ac687d2011-11-25 23:14:28 +08003030 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003031 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003032 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003033 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003034
3035 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003036 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003037 unlock_extent_cached(tree, cur,
3038 cur + iosize - 1,
3039 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003040 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003041 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003042 continue;
3043 }
3044 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003045 if (test_range_bit(tree, cur, cur_end,
3046 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003047 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003048 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003049 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003050 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003051 continue;
3052 }
Chris Mason70dec802008-01-29 09:59:12 -05003053 /* we have an inline extent but it didn't get marked up
3054 * to date. Error out
3055 */
3056 if (block_start == EXTENT_MAP_INLINE) {
3057 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003058 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003059 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003060 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003061 continue;
3062 }
Chris Masond1310b22008-01-24 16:13:08 -05003063
David Sterba4b81ba42017-06-06 19:14:26 +02003064 ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL,
David Sterba6273b7f2017-10-04 17:30:11 +02003065 page, offset, disk_io_size,
3066 pg_offset, bdev, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003067 end_bio_extent_readpage, mirror_num,
3068 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003069 this_bio_flag,
3070 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003071 if (!ret) {
3072 nr++;
3073 *bio_flags = this_bio_flag;
3074 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003075 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003076 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003077 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003078 }
Chris Masond1310b22008-01-24 16:13:08 -05003079 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003080 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003081 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003082out:
Chris Masond1310b22008-01-24 16:13:08 -05003083 if (!nr) {
3084 if (!PageError(page))
3085 SetPageUptodate(page);
3086 unlock_page(page);
3087 }
Liu Bobaf863b2016-07-11 10:39:07 -07003088 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003089}
3090
Miao Xie99740902013-07-25 19:22:36 +08003091static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
3092 struct page *pages[], int nr_pages,
3093 u64 start, u64 end,
3094 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08003095 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003096 struct bio **bio, int mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003097 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003098 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003099{
3100 struct inode *inode;
3101 struct btrfs_ordered_extent *ordered;
3102 int index;
3103
3104 inode = pages[0]->mapping->host;
3105 while (1) {
3106 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003107 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Miao Xie99740902013-07-25 19:22:36 +08003108 end - start + 1);
3109 if (!ordered)
3110 break;
3111 unlock_extent(tree, start, end);
3112 btrfs_start_ordered_extent(inode, ordered, 1);
3113 btrfs_put_ordered_extent(ordered);
3114 }
3115
3116 for (index = 0; index < nr_pages; index++) {
Miao Xie125bac012013-07-25 19:22:37 +08003117 __do_readpage(tree, pages[index], get_extent, em_cached, bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003118 mirror_num, bio_flags, 0, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003119 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003120 }
3121}
3122
3123static void __extent_readpages(struct extent_io_tree *tree,
3124 struct page *pages[],
3125 int nr_pages, get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08003126 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003127 struct bio **bio, int mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003128 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003129 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003130{
Stefan Behrens35a36212013-08-14 18:12:25 +02003131 u64 start = 0;
Miao Xie99740902013-07-25 19:22:36 +08003132 u64 end = 0;
3133 u64 page_start;
3134 int index;
Stefan Behrens35a36212013-08-14 18:12:25 +02003135 int first_index = 0;
Miao Xie99740902013-07-25 19:22:36 +08003136
3137 for (index = 0; index < nr_pages; index++) {
3138 page_start = page_offset(pages[index]);
3139 if (!end) {
3140 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003141 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003142 first_index = index;
3143 } else if (end + 1 == page_start) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003144 end += PAGE_SIZE;
Miao Xie99740902013-07-25 19:22:36 +08003145 } else {
3146 __do_contiguous_readpages(tree, &pages[first_index],
3147 index - first_index, start,
Miao Xie125bac012013-07-25 19:22:37 +08003148 end, get_extent, em_cached,
3149 bio, mirror_num, bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05003150 prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003151 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003152 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003153 first_index = index;
3154 }
3155 }
3156
3157 if (end)
3158 __do_contiguous_readpages(tree, &pages[first_index],
3159 index - first_index, start,
Miao Xie125bac012013-07-25 19:22:37 +08003160 end, get_extent, em_cached, bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003161 mirror_num, bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003162 prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003163}
3164
3165static int __extent_read_full_page(struct extent_io_tree *tree,
3166 struct page *page,
3167 get_extent_t *get_extent,
3168 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003169 unsigned long *bio_flags,
3170 unsigned int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003171{
3172 struct inode *inode = page->mapping->host;
3173 struct btrfs_ordered_extent *ordered;
3174 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003175 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003176 int ret;
3177
3178 while (1) {
3179 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003180 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003181 PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003182 if (!ordered)
3183 break;
3184 unlock_extent(tree, start, end);
3185 btrfs_start_ordered_extent(inode, ordered, 1);
3186 btrfs_put_ordered_extent(ordered);
3187 }
3188
Miao Xie125bac012013-07-25 19:22:37 +08003189 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003190 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003191 return ret;
3192}
3193
Chris Masond1310b22008-01-24 16:13:08 -05003194int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003195 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003196{
3197 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003198 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003199 int ret;
3200
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003201 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003202 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003203 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003204 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003205 return ret;
3206}
Chris Masond1310b22008-01-24 16:13:08 -05003207
David Sterba3d4b9492017-02-10 19:33:41 +01003208static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003209 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003210{
3211 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003212}
3213
Chris Masond1310b22008-01-24 16:13:08 -05003214/*
Chris Mason40f76582014-05-21 13:35:51 -07003215 * helper for __extent_writepage, doing all of the delayed allocation setup.
3216 *
3217 * This returns 1 if our fill_delalloc function did all the work required
3218 * to write the page (copy into inline extent). In this case the IO has
3219 * been started and the page is already unlocked.
3220 *
3221 * This returns 0 if all went well (page still locked)
3222 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003223 */
Chris Mason40f76582014-05-21 13:35:51 -07003224static noinline_for_stack int writepage_delalloc(struct inode *inode,
3225 struct page *page, struct writeback_control *wbc,
3226 struct extent_page_data *epd,
3227 u64 delalloc_start,
3228 unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003229{
Chris Mason40f76582014-05-21 13:35:51 -07003230 struct extent_io_tree *tree = epd->tree;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003231 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003232 u64 nr_delalloc;
3233 u64 delalloc_to_write = 0;
3234 u64 delalloc_end = 0;
3235 int ret;
3236 int page_started = 0;
3237
3238 if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc)
3239 return 0;
3240
3241 while (delalloc_end < page_end) {
3242 nr_delalloc = find_lock_delalloc_range(inode, tree,
3243 page,
3244 &delalloc_start,
3245 &delalloc_end,
Josef Bacikdcab6a32015-02-11 15:08:59 -05003246 BTRFS_MAX_EXTENT_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003247 if (nr_delalloc == 0) {
3248 delalloc_start = delalloc_end + 1;
3249 continue;
3250 }
3251 ret = tree->ops->fill_delalloc(inode, page,
3252 delalloc_start,
3253 delalloc_end,
3254 &page_started,
Liu Bof82b7352017-10-23 23:18:16 -06003255 nr_written, wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003256 /* File system has been set read-only */
3257 if (ret) {
3258 SetPageError(page);
3259 /* fill_delalloc should be return < 0 for error
3260 * but just in case, we use > 0 here meaning the
3261 * IO is started, so we don't want to return > 0
3262 * unless things are going well.
3263 */
3264 ret = ret < 0 ? ret : -EIO;
3265 goto done;
3266 }
3267 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003268 * delalloc_end is already one less than the total length, so
3269 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003270 */
3271 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003272 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003273 delalloc_start = delalloc_end + 1;
3274 }
3275 if (wbc->nr_to_write < delalloc_to_write) {
3276 int thresh = 8192;
3277
3278 if (delalloc_to_write < thresh * 2)
3279 thresh = delalloc_to_write;
3280 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3281 thresh);
3282 }
3283
3284 /* did the fill delalloc function already unlock and start
3285 * the IO?
3286 */
3287 if (page_started) {
3288 /*
3289 * we've unlocked the page, so we can't update
3290 * the mapping's writeback index, just update
3291 * nr_to_write.
3292 */
3293 wbc->nr_to_write -= *nr_written;
3294 return 1;
3295 }
3296
3297 ret = 0;
3298
3299done:
3300 return ret;
3301}
3302
3303/*
3304 * helper for __extent_writepage. This calls the writepage start hooks,
3305 * and does the loop to map the page into extents and bios.
3306 *
3307 * We return 1 if the IO is started and the page is unlocked,
3308 * 0 if all went well (page still locked)
3309 * < 0 if there were errors (page still locked)
3310 */
3311static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3312 struct page *page,
3313 struct writeback_control *wbc,
3314 struct extent_page_data *epd,
3315 loff_t i_size,
3316 unsigned long nr_written,
David Sterbaf1c77c52017-06-06 19:03:49 +02003317 unsigned int write_flags, int *nr_ret)
Chris Mason40f76582014-05-21 13:35:51 -07003318{
Chris Masond1310b22008-01-24 16:13:08 -05003319 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003320 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003321 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003322 u64 end;
3323 u64 cur = start;
3324 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003325 u64 block_start;
3326 u64 iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003327 struct extent_map *em;
3328 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003329 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003330 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003331 int ret = 0;
3332 int nr = 0;
3333 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003334
Chris Mason247e7432008-07-17 12:53:51 -04003335 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04003336 ret = tree->ops->writepage_start_hook(page, start,
3337 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01003338 if (ret) {
3339 /* Fixup worker will requeue */
3340 if (ret == -EBUSY)
3341 wbc->pages_skipped++;
3342 else
3343 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003344
David Sterba3d4b9492017-02-10 19:33:41 +01003345 update_nr_written(wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04003346 unlock_page(page);
Liu Bobcf93482017-01-25 17:15:54 -08003347 return 1;
Chris Mason247e7432008-07-17 12:53:51 -04003348 }
3349 }
3350
Chris Mason11c83492009-04-20 15:50:09 -04003351 /*
3352 * we don't want to touch the inode after unlocking the page,
3353 * so we update the mapping writeback index now
3354 */
David Sterba3d4b9492017-02-10 19:33:41 +01003355 update_nr_written(wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003356
Chris Masond1310b22008-01-24 16:13:08 -05003357 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003358 if (i_size <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003359 if (tree->ops && tree->ops->writepage_end_io_hook)
3360 tree->ops->writepage_end_io_hook(page, start,
3361 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003362 goto done;
3363 }
3364
Chris Masond1310b22008-01-24 16:13:08 -05003365 blocksize = inode->i_sb->s_blocksize;
3366
3367 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003368 u64 em_end;
David Sterba6273b7f2017-10-04 17:30:11 +02003369 u64 offset;
David Sterba58409ed2016-05-04 11:46:10 +02003370
Chris Mason40f76582014-05-21 13:35:51 -07003371 if (cur >= i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003372 if (tree->ops && tree->ops->writepage_end_io_hook)
3373 tree->ops->writepage_end_io_hook(page, cur,
3374 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003375 break;
3376 }
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02003377 em = epd->get_extent(BTRFS_I(inode), page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003378 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003379 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003380 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003381 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003382 break;
3383 }
3384
3385 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003386 em_end = extent_map_end(em);
3387 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003388 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003389 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003390 iosize = ALIGN(iosize, blocksize);
David Sterba6273b7f2017-10-04 17:30:11 +02003391 offset = em->block_start + extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003392 bdev = em->bdev;
3393 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003394 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003395 free_extent_map(em);
3396 em = NULL;
3397
Chris Masonc8b97812008-10-29 14:49:59 -04003398 /*
3399 * compressed and inline extents are written through other
3400 * paths in the FS
3401 */
3402 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003403 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003404 /*
3405 * end_io notification does not happen here for
3406 * compressed extents
3407 */
3408 if (!compressed && tree->ops &&
3409 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003410 tree->ops->writepage_end_io_hook(page, cur,
3411 cur + iosize - 1,
3412 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003413 else if (compressed) {
3414 /* we don't want to end_page_writeback on
3415 * a compressed extent. this happens
3416 * elsewhere
3417 */
3418 nr++;
3419 }
3420
3421 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003422 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003423 continue;
3424 }
Chris Masonc8b97812008-10-29 14:49:59 -04003425
David Sterba58409ed2016-05-04 11:46:10 +02003426 set_range_writeback(tree, cur, cur + iosize - 1);
3427 if (!PageWriteback(page)) {
3428 btrfs_err(BTRFS_I(inode)->root->fs_info,
3429 "page %lu not writeback, cur %llu end %llu",
3430 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003431 }
David Sterba58409ed2016-05-04 11:46:10 +02003432
David Sterba4b81ba42017-06-06 19:14:26 +02003433 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003434 page, offset, iosize, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003435 bdev, &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003436 end_bio_extent_writepage,
3437 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003438 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003439 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003440 if (PageWriteback(page))
3441 end_page_writeback(page);
3442 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003443
Chris Masond1310b22008-01-24 16:13:08 -05003444 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003445 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003446 nr++;
3447 }
3448done:
Chris Mason40f76582014-05-21 13:35:51 -07003449 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003450 return ret;
3451}
3452
3453/*
3454 * the writepage semantics are similar to regular writepage. extent
3455 * records are inserted to lock ranges in the tree, and as dirty areas
3456 * are found, they are marked writeback. Then the lock bits are removed
3457 * and the end_io handler clears the writeback ranges
3458 */
3459static int __extent_writepage(struct page *page, struct writeback_control *wbc,
3460 void *data)
3461{
3462 struct inode *inode = page->mapping->host;
3463 struct extent_page_data *epd = data;
3464 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003465 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003466 int ret;
3467 int nr = 0;
3468 size_t pg_offset = 0;
3469 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003470 unsigned long end_index = i_size >> PAGE_SHIFT;
David Sterbaf1c77c52017-06-06 19:03:49 +02003471 unsigned int write_flags = 0;
Chris Mason40f76582014-05-21 13:35:51 -07003472 unsigned long nr_written = 0;
3473
Liu Boff40adf2017-08-24 18:19:48 -06003474 write_flags = wbc_to_write_flags(wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003475
3476 trace___extent_writepage(page, inode, wbc);
3477
3478 WARN_ON(!PageLocked(page));
3479
3480 ClearPageError(page);
3481
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003482 pg_offset = i_size & (PAGE_SIZE - 1);
Chris Mason40f76582014-05-21 13:35:51 -07003483 if (page->index > end_index ||
3484 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003485 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003486 unlock_page(page);
3487 return 0;
3488 }
3489
3490 if (page->index == end_index) {
3491 char *userpage;
3492
3493 userpage = kmap_atomic(page);
3494 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003495 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003496 kunmap_atomic(userpage);
3497 flush_dcache_page(page);
3498 }
3499
3500 pg_offset = 0;
3501
3502 set_page_extent_mapped(page);
3503
3504 ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
3505 if (ret == 1)
3506 goto done_unlocked;
3507 if (ret)
3508 goto done;
3509
3510 ret = __extent_writepage_io(inode, page, wbc, epd,
3511 i_size, nr_written, write_flags, &nr);
3512 if (ret == 1)
3513 goto done_unlocked;
3514
3515done:
Chris Masond1310b22008-01-24 16:13:08 -05003516 if (nr == 0) {
3517 /* make sure the mapping tag for page dirty gets cleared */
3518 set_page_writeback(page);
3519 end_page_writeback(page);
3520 }
Filipe Manana61391d52014-05-09 17:17:40 +01003521 if (PageError(page)) {
3522 ret = ret < 0 ? ret : -EIO;
3523 end_extent_writepage(page, ret, start, page_end);
3524 }
Chris Masond1310b22008-01-24 16:13:08 -05003525 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003526 return ret;
Chris Mason771ed682008-11-06 22:02:51 -05003527
Chris Mason11c83492009-04-20 15:50:09 -04003528done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003529 return 0;
3530}
3531
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003532void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003533{
NeilBrown74316202014-07-07 15:16:04 +10003534 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3535 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003536}
3537
Chris Mason0e378df2014-05-19 20:55:27 -07003538static noinline_for_stack int
3539lock_extent_buffer_for_io(struct extent_buffer *eb,
3540 struct btrfs_fs_info *fs_info,
3541 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003542{
3543 unsigned long i, num_pages;
3544 int flush = 0;
3545 int ret = 0;
3546
3547 if (!btrfs_try_tree_write_lock(eb)) {
3548 flush = 1;
3549 flush_write_bio(epd);
3550 btrfs_tree_lock(eb);
3551 }
3552
3553 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3554 btrfs_tree_unlock(eb);
3555 if (!epd->sync_io)
3556 return 0;
3557 if (!flush) {
3558 flush_write_bio(epd);
3559 flush = 1;
3560 }
Chris Masona098d8e82012-03-21 12:09:56 -04003561 while (1) {
3562 wait_on_extent_buffer_writeback(eb);
3563 btrfs_tree_lock(eb);
3564 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3565 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003566 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003567 }
3568 }
3569
Josef Bacik51561ff2012-07-20 16:25:24 -04003570 /*
3571 * We need to do this to prevent races in people who check if the eb is
3572 * under IO since we can end up having no IO bits set for a short period
3573 * of time.
3574 */
3575 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003576 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3577 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003578 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003579 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Nikolay Borisov104b4e52017-06-20 21:01:20 +03003580 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3581 -eb->len,
3582 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003583 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003584 } else {
3585 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003586 }
3587
3588 btrfs_tree_unlock(eb);
3589
3590 if (!ret)
3591 return ret;
3592
3593 num_pages = num_extent_pages(eb->start, eb->len);
3594 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003595 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003596
3597 if (!trylock_page(p)) {
3598 if (!flush) {
3599 flush_write_bio(epd);
3600 flush = 1;
3601 }
3602 lock_page(p);
3603 }
3604 }
3605
3606 return ret;
3607}
3608
3609static void end_extent_buffer_writeback(struct extent_buffer *eb)
3610{
3611 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003612 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003613 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3614}
3615
Filipe Manana656f30d2014-09-26 12:25:56 +01003616static void set_btree_ioerr(struct page *page)
3617{
3618 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Filipe Manana656f30d2014-09-26 12:25:56 +01003619
3620 SetPageError(page);
3621 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3622 return;
3623
3624 /*
3625 * If writeback for a btree extent that doesn't belong to a log tree
3626 * failed, increment the counter transaction->eb_write_errors.
3627 * We do this because while the transaction is running and before it's
3628 * committing (when we call filemap_fdata[write|wait]_range against
3629 * the btree inode), we might have
3630 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3631 * returns an error or an error happens during writeback, when we're
3632 * committing the transaction we wouldn't know about it, since the pages
3633 * can be no longer dirty nor marked anymore for writeback (if a
3634 * subsequent modification to the extent buffer didn't happen before the
3635 * transaction commit), which makes filemap_fdata[write|wait]_range not
3636 * able to find the pages tagged with SetPageError at transaction
3637 * commit time. So if this happens we must abort the transaction,
3638 * otherwise we commit a super block with btree roots that point to
3639 * btree nodes/leafs whose content on disk is invalid - either garbage
3640 * or the content of some node/leaf from a past generation that got
3641 * cowed or deleted and is no longer valid.
3642 *
3643 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3644 * not be enough - we need to distinguish between log tree extents vs
3645 * non-log tree extents, and the next filemap_fdatawait_range() call
3646 * will catch and clear such errors in the mapping - and that call might
3647 * be from a log sync and not from a transaction commit. Also, checking
3648 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3649 * not done and would not be reliable - the eb might have been released
3650 * from memory and reading it back again means that flag would not be
3651 * set (since it's a runtime flag, not persisted on disk).
3652 *
3653 * Using the flags below in the btree inode also makes us achieve the
3654 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3655 * writeback for all dirty pages and before filemap_fdatawait_range()
3656 * is called, the writeback for all dirty pages had already finished
3657 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3658 * filemap_fdatawait_range() would return success, as it could not know
3659 * that writeback errors happened (the pages were no longer tagged for
3660 * writeback).
3661 */
3662 switch (eb->log_index) {
3663 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003664 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003665 break;
3666 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003667 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003668 break;
3669 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003670 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003671 break;
3672 default:
3673 BUG(); /* unexpected, logic error */
3674 }
3675}
3676
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003677static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003678{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003679 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003680 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003681 int i, done;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003682
David Sterbac09abff2017-07-13 18:10:07 +02003683 ASSERT(!bio_flagged(bio, BIO_CLONED));
Kent Overstreet2c30c712013-11-07 12:20:26 -08003684 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003685 struct page *page = bvec->bv_page;
3686
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003687 eb = (struct extent_buffer *)page->private;
3688 BUG_ON(!eb);
3689 done = atomic_dec_and_test(&eb->io_pages);
3690
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003691 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003692 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003693 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003694 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003695 }
3696
3697 end_page_writeback(page);
3698
3699 if (!done)
3700 continue;
3701
3702 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003703 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003704
3705 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003706}
3707
Chris Mason0e378df2014-05-19 20:55:27 -07003708static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003709 struct btrfs_fs_info *fs_info,
3710 struct writeback_control *wbc,
3711 struct extent_page_data *epd)
3712{
3713 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003714 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003715 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003716 u32 nritems;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003717 unsigned long i, num_pages;
Liu Bo851cd172016-09-23 13:44:44 -07003718 unsigned long start, end;
Liu Boff40adf2017-08-24 18:19:48 -06003719 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003720 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003721
Filipe Manana656f30d2014-09-26 12:25:56 +01003722 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003723 num_pages = num_extent_pages(eb->start, eb->len);
3724 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003725
Liu Bo851cd172016-09-23 13:44:44 -07003726 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3727 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003728 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003729 end = btrfs_node_key_ptr_offset(nritems);
3730
David Sterbab159fa22016-11-08 18:09:03 +01003731 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003732 } else {
3733 /*
3734 * leaf:
3735 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3736 */
3737 start = btrfs_item_nr_offset(nritems);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003738 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, eb);
David Sterbab159fa22016-11-08 18:09:03 +01003739 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003740 }
3741
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003742 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003743 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003744
3745 clear_page_dirty_for_io(p);
3746 set_page_writeback(p);
David Sterba4b81ba42017-06-06 19:14:26 +02003747 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003748 p, offset, PAGE_SIZE, 0, bdev,
David Sterbac2df8bb2017-02-10 19:29:38 +01003749 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003750 end_bio_extent_buffer_writepage,
Liu Bo18fdc672017-09-13 12:18:22 -06003751 0, 0, 0, false);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003752 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003753 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003754 if (PageWriteback(p))
3755 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003756 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3757 end_extent_buffer_writeback(eb);
3758 ret = -EIO;
3759 break;
3760 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003761 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003762 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003763 unlock_page(p);
3764 }
3765
3766 if (unlikely(ret)) {
3767 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003768 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003769 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003770 unlock_page(p);
3771 }
3772 }
3773
3774 return ret;
3775}
3776
3777int btree_write_cache_pages(struct address_space *mapping,
3778 struct writeback_control *wbc)
3779{
3780 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3781 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3782 struct extent_buffer *eb, *prev_eb = NULL;
3783 struct extent_page_data epd = {
3784 .bio = NULL,
3785 .tree = tree,
3786 .extent_locked = 0,
3787 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3788 };
3789 int ret = 0;
3790 int done = 0;
3791 int nr_to_write_done = 0;
3792 struct pagevec pvec;
3793 int nr_pages;
3794 pgoff_t index;
3795 pgoff_t end; /* Inclusive */
3796 int scanned = 0;
3797 int tag;
3798
3799 pagevec_init(&pvec, 0);
3800 if (wbc->range_cyclic) {
3801 index = mapping->writeback_index; /* Start from prev offset */
3802 end = -1;
3803 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003804 index = wbc->range_start >> PAGE_SHIFT;
3805 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003806 scanned = 1;
3807 }
3808 if (wbc->sync_mode == WB_SYNC_ALL)
3809 tag = PAGECACHE_TAG_TOWRITE;
3810 else
3811 tag = PAGECACHE_TAG_DIRTY;
3812retry:
3813 if (wbc->sync_mode == WB_SYNC_ALL)
3814 tag_pages_for_writeback(mapping, index, end);
3815 while (!done && !nr_to_write_done && (index <= end) &&
3816 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3817 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3818 unsigned i;
3819
3820 scanned = 1;
3821 for (i = 0; i < nr_pages; i++) {
3822 struct page *page = pvec.pages[i];
3823
3824 if (!PagePrivate(page))
3825 continue;
3826
3827 if (!wbc->range_cyclic && page->index > end) {
3828 done = 1;
3829 break;
3830 }
3831
Josef Bacikb5bae262012-09-14 13:43:01 -04003832 spin_lock(&mapping->private_lock);
3833 if (!PagePrivate(page)) {
3834 spin_unlock(&mapping->private_lock);
3835 continue;
3836 }
3837
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003838 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003839
3840 /*
3841 * Shouldn't happen and normally this would be a BUG_ON
3842 * but no sense in crashing the users box for something
3843 * we can survive anyway.
3844 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303845 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003846 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003847 continue;
3848 }
3849
Josef Bacikb5bae262012-09-14 13:43:01 -04003850 if (eb == prev_eb) {
3851 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003852 continue;
3853 }
3854
Josef Bacikb5bae262012-09-14 13:43:01 -04003855 ret = atomic_inc_not_zero(&eb->refs);
3856 spin_unlock(&mapping->private_lock);
3857 if (!ret)
3858 continue;
3859
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003860 prev_eb = eb;
3861 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3862 if (!ret) {
3863 free_extent_buffer(eb);
3864 continue;
3865 }
3866
3867 ret = write_one_eb(eb, fs_info, wbc, &epd);
3868 if (ret) {
3869 done = 1;
3870 free_extent_buffer(eb);
3871 break;
3872 }
3873 free_extent_buffer(eb);
3874
3875 /*
3876 * the filesystem may choose to bump up nr_to_write.
3877 * We have to make sure to honor the new nr_to_write
3878 * at any time
3879 */
3880 nr_to_write_done = wbc->nr_to_write <= 0;
3881 }
3882 pagevec_release(&pvec);
3883 cond_resched();
3884 }
3885 if (!scanned && !done) {
3886 /*
3887 * We hit the last page and there is more work to be done: wrap
3888 * back to the start of the file
3889 */
3890 scanned = 1;
3891 index = 0;
3892 goto retry;
3893 }
3894 flush_write_bio(&epd);
3895 return ret;
3896}
3897
Chris Masond1310b22008-01-24 16:13:08 -05003898/**
Chris Mason4bef0842008-09-08 11:18:08 -04003899 * 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 -05003900 * @mapping: address space structure to write
3901 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3902 * @writepage: function called for each page
3903 * @data: data passed to writepage function
3904 *
3905 * If a page is already under I/O, write_cache_pages() skips it, even
3906 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3907 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3908 * and msync() need to guarantee that all the data which was dirty at the time
3909 * the call was made get new I/O started against them. If wbc->sync_mode is
3910 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3911 * existing IO to complete.
3912 */
David Sterba4242b642017-02-10 19:38:24 +01003913static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04003914 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003915 writepage_t writepage, void *data,
3916 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003917{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003918 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003919 int ret = 0;
3920 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003921 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003922 struct pagevec pvec;
3923 int nr_pages;
3924 pgoff_t index;
3925 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08003926 pgoff_t done_index;
3927 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003928 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003929 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003930
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003931 /*
3932 * We have to hold onto the inode so that ordered extents can do their
3933 * work when the IO finishes. The alternative to this is failing to add
3934 * an ordered extent if the igrab() fails there and that is a huge pain
3935 * to deal with, so instead just hold onto the inode throughout the
3936 * writepages operation. If it fails here we are freeing up the inode
3937 * anyway and we'd rather not waste our time writing out stuff that is
3938 * going to be truncated anyway.
3939 */
3940 if (!igrab(inode))
3941 return 0;
3942
Chris Masond1310b22008-01-24 16:13:08 -05003943 pagevec_init(&pvec, 0);
3944 if (wbc->range_cyclic) {
3945 index = mapping->writeback_index; /* Start from prev offset */
3946 end = -1;
3947 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003948 index = wbc->range_start >> PAGE_SHIFT;
3949 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08003950 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3951 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05003952 scanned = 1;
3953 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003954 if (wbc->sync_mode == WB_SYNC_ALL)
3955 tag = PAGECACHE_TAG_TOWRITE;
3956 else
3957 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003958retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003959 if (wbc->sync_mode == WB_SYNC_ALL)
3960 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08003961 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003962 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003963 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3964 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003965 unsigned i;
3966
3967 scanned = 1;
3968 for (i = 0; i < nr_pages; i++) {
3969 struct page *page = pvec.pages[i];
3970
Liu Boa91326672016-03-07 16:56:21 -08003971 done_index = page->index;
Chris Masond1310b22008-01-24 16:13:08 -05003972 /*
3973 * At this point we hold neither mapping->tree_lock nor
3974 * lock on the page itself: the page may be truncated or
3975 * invalidated (changing page->mapping to NULL), or even
3976 * swizzled back from swapper_space to tmpfs file
3977 * mapping
3978 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003979 if (!trylock_page(page)) {
3980 flush_fn(data);
3981 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003982 }
Chris Masond1310b22008-01-24 16:13:08 -05003983
3984 if (unlikely(page->mapping != mapping)) {
3985 unlock_page(page);
3986 continue;
3987 }
3988
3989 if (!wbc->range_cyclic && page->index > end) {
3990 done = 1;
3991 unlock_page(page);
3992 continue;
3993 }
3994
Chris Masond2c3f4f2008-11-19 12:44:22 -05003995 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003996 if (PageWriteback(page))
3997 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003998 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003999 }
Chris Masond1310b22008-01-24 16:13:08 -05004000
4001 if (PageWriteback(page) ||
4002 !clear_page_dirty_for_io(page)) {
4003 unlock_page(page);
4004 continue;
4005 }
4006
4007 ret = (*writepage)(page, wbc, data);
4008
4009 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
4010 unlock_page(page);
4011 ret = 0;
4012 }
Liu Boa91326672016-03-07 16:56:21 -08004013 if (ret < 0) {
4014 /*
4015 * done_index is set past this page,
4016 * so media errors will not choke
4017 * background writeout for the entire
4018 * file. This has consequences for
4019 * range_cyclic semantics (ie. it may
4020 * not be suitable for data integrity
4021 * writeout).
4022 */
4023 done_index = page->index + 1;
4024 done = 1;
4025 break;
4026 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004027
4028 /*
4029 * the filesystem may choose to bump up nr_to_write.
4030 * We have to make sure to honor the new nr_to_write
4031 * at any time
4032 */
4033 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004034 }
4035 pagevec_release(&pvec);
4036 cond_resched();
4037 }
Liu Bo894b36e2016-03-07 16:56:22 -08004038 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004039 /*
4040 * We hit the last page and there is more work to be done: wrap
4041 * back to the start of the file
4042 */
4043 scanned = 1;
4044 index = 0;
4045 goto retry;
4046 }
Liu Boa91326672016-03-07 16:56:21 -08004047
4048 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4049 mapping->writeback_index = done_index;
4050
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004051 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004052 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004053}
Chris Masond1310b22008-01-24 16:13:08 -05004054
Chris Masonffbd5172009-04-20 15:50:09 -04004055static void flush_epd_write_bio(struct extent_page_data *epd)
4056{
4057 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04004058 int ret;
4059
Liu Bo18fdc672017-09-13 12:18:22 -06004060 ret = submit_one_bio(epd->bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004061 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04004062 epd->bio = NULL;
4063 }
4064}
4065
Chris Masond2c3f4f2008-11-19 12:44:22 -05004066static noinline void flush_write_bio(void *data)
4067{
4068 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04004069 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004070}
4071
Chris Masond1310b22008-01-24 16:13:08 -05004072int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
4073 get_extent_t *get_extent,
4074 struct writeback_control *wbc)
4075{
4076 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004077 struct extent_page_data epd = {
4078 .bio = NULL,
4079 .tree = tree,
4080 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05004081 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004082 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004083 };
Chris Masond1310b22008-01-24 16:13:08 -05004084
Chris Masond1310b22008-01-24 16:13:08 -05004085 ret = __extent_writepage(page, wbc, &epd);
4086
Chris Masonffbd5172009-04-20 15:50:09 -04004087 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004088 return ret;
4089}
Chris Masond1310b22008-01-24 16:13:08 -05004090
Chris Mason771ed682008-11-06 22:02:51 -05004091int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
4092 u64 start, u64 end, get_extent_t *get_extent,
4093 int mode)
4094{
4095 int ret = 0;
4096 struct address_space *mapping = inode->i_mapping;
4097 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004098 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4099 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004100
4101 struct extent_page_data epd = {
4102 .bio = NULL,
4103 .tree = tree,
4104 .get_extent = get_extent,
4105 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004106 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05004107 };
4108 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004109 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004110 .nr_to_write = nr_pages * 2,
4111 .range_start = start,
4112 .range_end = end + 1,
4113 };
4114
Chris Masond3977122009-01-05 21:25:51 -05004115 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004116 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004117 if (clear_page_dirty_for_io(page))
4118 ret = __extent_writepage(page, &wbc_writepages, &epd);
4119 else {
4120 if (tree->ops && tree->ops->writepage_end_io_hook)
4121 tree->ops->writepage_end_io_hook(page, start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004122 start + PAGE_SIZE - 1,
Chris Mason771ed682008-11-06 22:02:51 -05004123 NULL, 1);
4124 unlock_page(page);
4125 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004126 put_page(page);
4127 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004128 }
4129
Chris Masonffbd5172009-04-20 15:50:09 -04004130 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004131 return ret;
4132}
Chris Masond1310b22008-01-24 16:13:08 -05004133
4134int extent_writepages(struct extent_io_tree *tree,
4135 struct address_space *mapping,
4136 get_extent_t *get_extent,
4137 struct writeback_control *wbc)
4138{
4139 int ret = 0;
4140 struct extent_page_data epd = {
4141 .bio = NULL,
4142 .tree = tree,
4143 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05004144 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004145 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004146 };
4147
David Sterba4242b642017-02-10 19:38:24 +01004148 ret = extent_write_cache_pages(mapping, wbc, __extent_writepage, &epd,
Chris Masond2c3f4f2008-11-19 12:44:22 -05004149 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04004150 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004151 return ret;
4152}
Chris Masond1310b22008-01-24 16:13:08 -05004153
4154int extent_readpages(struct extent_io_tree *tree,
4155 struct address_space *mapping,
4156 struct list_head *pages, unsigned nr_pages,
4157 get_extent_t get_extent)
4158{
4159 struct bio *bio = NULL;
4160 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04004161 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004162 struct page *pagepool[16];
4163 struct page *page;
Miao Xie125bac012013-07-25 19:22:37 +08004164 struct extent_map *em_cached = NULL;
Liu Bo67c96842012-07-20 21:43:09 -06004165 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004166 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004167
Chris Masond1310b22008-01-24 16:13:08 -05004168 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06004169 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05004170
4171 prefetchw(&page->flags);
4172 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06004173 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07004174 page->index,
4175 readahead_gfp_mask(mapping))) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004176 put_page(page);
Liu Bo67c96842012-07-20 21:43:09 -06004177 continue;
Chris Masond1310b22008-01-24 16:13:08 -05004178 }
Liu Bo67c96842012-07-20 21:43:09 -06004179
4180 pagepool[nr++] = page;
4181 if (nr < ARRAY_SIZE(pagepool))
4182 continue;
Miao Xie125bac012013-07-25 19:22:37 +08004183 __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
Mike Christie1f7ad752016-06-05 14:31:51 -05004184 &bio, 0, &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004185 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004186 }
Miao Xie99740902013-07-25 19:22:36 +08004187 if (nr)
Miao Xie125bac012013-07-25 19:22:37 +08004188 __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
Mike Christie1f7ad752016-06-05 14:31:51 -05004189 &bio, 0, &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004190
Miao Xie125bac012013-07-25 19:22:37 +08004191 if (em_cached)
4192 free_extent_map(em_cached);
4193
Chris Masond1310b22008-01-24 16:13:08 -05004194 BUG_ON(!list_empty(pages));
4195 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004196 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004197 return 0;
4198}
Chris Masond1310b22008-01-24 16:13:08 -05004199
4200/*
4201 * basic invalidatepage code, this waits on any locked or writeback
4202 * ranges corresponding to the page, and then deletes any extent state
4203 * records from the tree
4204 */
4205int extent_invalidatepage(struct extent_io_tree *tree,
4206 struct page *page, unsigned long offset)
4207{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004208 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004209 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004210 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004211 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4212
Qu Wenruofda28322013-02-26 08:10:22 +00004213 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004214 if (start > end)
4215 return 0;
4216
David Sterbaff13db42015-12-03 14:30:40 +01004217 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004218 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004219 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004220 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4221 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004222 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004223 return 0;
4224}
Chris Masond1310b22008-01-24 16:13:08 -05004225
4226/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004227 * a helper for releasepage, this tests for areas of the page that
4228 * are locked or under IO and drops the related state bits if it is safe
4229 * to drop the page.
4230 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00004231static int try_release_extent_state(struct extent_map_tree *map,
4232 struct extent_io_tree *tree,
4233 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004234{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004235 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004236 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004237 int ret = 1;
4238
Chris Mason211f90e2008-07-18 11:56:15 -04004239 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04004240 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04004241 ret = 0;
4242 else {
Chris Mason11ef1602009-09-23 20:28:46 -04004243 /*
4244 * at this point we can safely clear everything except the
4245 * locked bit and the nodatasum bit
4246 */
Chris Masone3f24cc2011-02-14 12:52:08 -05004247 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004248 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
4249 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05004250
4251 /* if clear_extent_bit failed for enomem reasons,
4252 * we can't allow the release to continue.
4253 */
4254 if (ret < 0)
4255 ret = 0;
4256 else
4257 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004258 }
4259 return ret;
4260}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004261
4262/*
Chris Masond1310b22008-01-24 16:13:08 -05004263 * a helper for releasepage. As long as there are no locked extents
4264 * in the range corresponding to the page, both state records and extent
4265 * map records are removed
4266 */
4267int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05004268 struct extent_io_tree *tree, struct page *page,
4269 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004270{
4271 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004272 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004273 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004274
Mel Gormand0164ad2015-11-06 16:28:21 -08004275 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004276 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004277 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004278 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004279 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004280 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004281 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004282 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004283 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004284 break;
4285 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004286 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4287 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004288 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004289 free_extent_map(em);
4290 break;
4291 }
4292 if (!test_range_bit(tree, em->start,
4293 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04004294 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04004295 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05004296 remove_extent_mapping(map, em);
4297 /* once for the rb tree */
4298 free_extent_map(em);
4299 }
4300 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004301 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004302
4303 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004304 free_extent_map(em);
4305 }
Chris Masond1310b22008-01-24 16:13:08 -05004306 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04004307 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004308}
Chris Masond1310b22008-01-24 16:13:08 -05004309
Chris Masonec29ed52011-02-23 16:23:20 -05004310/*
4311 * helper function for fiemap, which doesn't want to see any holes.
4312 * This maps until we find something past 'last'
4313 */
4314static struct extent_map *get_extent_skip_holes(struct inode *inode,
4315 u64 offset,
4316 u64 last,
4317 get_extent_t *get_extent)
4318{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004319 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004320 struct extent_map *em;
4321 u64 len;
4322
4323 if (offset >= last)
4324 return NULL;
4325
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304326 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004327 len = last - offset;
4328 if (len == 0)
4329 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004330 len = ALIGN(len, sectorsize);
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004331 em = get_extent(BTRFS_I(inode), NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02004332 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004333 return em;
4334
4335 /* if this isn't a hole return it */
4336 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
4337 em->block_start != EXTENT_MAP_HOLE) {
4338 return em;
4339 }
4340
4341 /* this is a hole, advance to the next extent */
4342 offset = extent_map_end(em);
4343 free_extent_map(em);
4344 if (offset >= last)
4345 break;
4346 }
4347 return NULL;
4348}
4349
Qu Wenruo47518322017-04-07 10:43:15 +08004350/*
4351 * To cache previous fiemap extent
4352 *
4353 * Will be used for merging fiemap extent
4354 */
4355struct fiemap_cache {
4356 u64 offset;
4357 u64 phys;
4358 u64 len;
4359 u32 flags;
4360 bool cached;
4361};
4362
4363/*
4364 * Helper to submit fiemap extent.
4365 *
4366 * Will try to merge current fiemap extent specified by @offset, @phys,
4367 * @len and @flags with cached one.
4368 * And only when we fails to merge, cached one will be submitted as
4369 * fiemap extent.
4370 *
4371 * Return value is the same as fiemap_fill_next_extent().
4372 */
4373static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4374 struct fiemap_cache *cache,
4375 u64 offset, u64 phys, u64 len, u32 flags)
4376{
4377 int ret = 0;
4378
4379 if (!cache->cached)
4380 goto assign;
4381
4382 /*
4383 * Sanity check, extent_fiemap() should have ensured that new
4384 * fiemap extent won't overlap with cahced one.
4385 * Not recoverable.
4386 *
4387 * NOTE: Physical address can overlap, due to compression
4388 */
4389 if (cache->offset + cache->len > offset) {
4390 WARN_ON(1);
4391 return -EINVAL;
4392 }
4393
4394 /*
4395 * Only merges fiemap extents if
4396 * 1) Their logical addresses are continuous
4397 *
4398 * 2) Their physical addresses are continuous
4399 * So truly compressed (physical size smaller than logical size)
4400 * extents won't get merged with each other
4401 *
4402 * 3) Share same flags except FIEMAP_EXTENT_LAST
4403 * So regular extent won't get merged with prealloc extent
4404 */
4405 if (cache->offset + cache->len == offset &&
4406 cache->phys + cache->len == phys &&
4407 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4408 (flags & ~FIEMAP_EXTENT_LAST)) {
4409 cache->len += len;
4410 cache->flags |= flags;
4411 goto try_submit_last;
4412 }
4413
4414 /* Not mergeable, need to submit cached one */
4415 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4416 cache->len, cache->flags);
4417 cache->cached = false;
4418 if (ret)
4419 return ret;
4420assign:
4421 cache->cached = true;
4422 cache->offset = offset;
4423 cache->phys = phys;
4424 cache->len = len;
4425 cache->flags = flags;
4426try_submit_last:
4427 if (cache->flags & FIEMAP_EXTENT_LAST) {
4428 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4429 cache->phys, cache->len, cache->flags);
4430 cache->cached = false;
4431 }
4432 return ret;
4433}
4434
4435/*
Qu Wenruo848c23b2017-06-22 10:01:21 +08004436 * Emit last fiemap cache
Qu Wenruo47518322017-04-07 10:43:15 +08004437 *
Qu Wenruo848c23b2017-06-22 10:01:21 +08004438 * The last fiemap cache may still be cached in the following case:
4439 * 0 4k 8k
4440 * |<- Fiemap range ->|
4441 * |<------------ First extent ----------->|
4442 *
4443 * In this case, the first extent range will be cached but not emitted.
4444 * So we must emit it before ending extent_fiemap().
Qu Wenruo47518322017-04-07 10:43:15 +08004445 */
Qu Wenruo848c23b2017-06-22 10:01:21 +08004446static int emit_last_fiemap_cache(struct btrfs_fs_info *fs_info,
4447 struct fiemap_extent_info *fieinfo,
4448 struct fiemap_cache *cache)
Qu Wenruo47518322017-04-07 10:43:15 +08004449{
4450 int ret;
4451
4452 if (!cache->cached)
4453 return 0;
4454
Qu Wenruo47518322017-04-07 10:43:15 +08004455 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4456 cache->len, cache->flags);
4457 cache->cached = false;
4458 if (ret > 0)
4459 ret = 0;
4460 return ret;
4461}
4462
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004463int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4464 __u64 start, __u64 len, get_extent_t *get_extent)
4465{
Josef Bacik975f84f2010-11-23 19:36:57 +00004466 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004467 u64 off = start;
4468 u64 max = start + len;
4469 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004470 u32 found_type;
4471 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004472 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004473 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004474 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004475 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004476 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004477 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004478 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004479 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004480 struct fiemap_cache cache = { 0 };
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004481 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004482 u64 em_start = 0;
4483 u64 em_len = 0;
4484 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004485
4486 if (len == 0)
4487 return -EINVAL;
4488
Josef Bacik975f84f2010-11-23 19:36:57 +00004489 path = btrfs_alloc_path();
4490 if (!path)
4491 return -ENOMEM;
4492 path->leave_spinning = 1;
4493
Jeff Mahoneyda170662016-06-15 09:22:56 -04004494 start = round_down(start, btrfs_inode_sectorsize(inode));
4495 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004496
Chris Masonec29ed52011-02-23 16:23:20 -05004497 /*
4498 * lookup the last file extent. We're not using i_size here
4499 * because there might be preallocation past i_size
4500 */
David Sterbaf85b7372017-01-20 14:54:07 +01004501 ret = btrfs_lookup_file_extent(NULL, root, path,
4502 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004503 if (ret < 0) {
4504 btrfs_free_path(path);
4505 return ret;
Liu Bo2d324f52016-05-17 17:21:48 -07004506 } else {
4507 WARN_ON(!ret);
4508 if (ret == 1)
4509 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004510 }
Liu Bo2d324f52016-05-17 17:21:48 -07004511
Josef Bacik975f84f2010-11-23 19:36:57 +00004512 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004513 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004514 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004515
Chris Masonec29ed52011-02-23 16:23:20 -05004516 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004517 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004518 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004519 /* have to trust i_size as the end */
4520 last = (u64)-1;
4521 last_for_get_extent = isize;
4522 } else {
4523 /*
4524 * remember the start of the last extent. There are a
4525 * bunch of different factors that go into the length of the
4526 * extent, so its much less complex to remember where it started
4527 */
4528 last = found_key.offset;
4529 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004530 }
Liu Bofe09e162013-09-22 12:54:23 +08004531 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004532
Chris Masonec29ed52011-02-23 16:23:20 -05004533 /*
4534 * we might have some extents allocated but more delalloc past those
4535 * extents. so, we trust isize unless the start of the last extent is
4536 * beyond isize
4537 */
4538 if (last < isize) {
4539 last = (u64)-1;
4540 last_for_get_extent = isize;
4541 }
4542
David Sterbaff13db42015-12-03 14:30:40 +01004543 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004544 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004545
Josef Bacik4d479cf2011-11-17 11:34:31 -05004546 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05004547 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004548 if (!em)
4549 goto out;
4550 if (IS_ERR(em)) {
4551 ret = PTR_ERR(em);
4552 goto out;
4553 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004554
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004555 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004556 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004557
Chris Masonea8efc72011-03-08 11:54:40 -05004558 /* break if the extent we found is outside the range */
4559 if (em->start >= max || extent_map_end(em) < off)
4560 break;
4561
4562 /*
4563 * get_extent may return an extent that starts before our
4564 * requested range. We have to make sure the ranges
4565 * we return to fiemap always move forward and don't
4566 * overlap, so adjust the offsets here
4567 */
4568 em_start = max(em->start, off);
4569
4570 /*
4571 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004572 * for adjusting the disk offset below. Only do this if the
4573 * extent isn't compressed since our in ram offset may be past
4574 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004575 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004576 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4577 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004578 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004579 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004580 disko = 0;
4581 flags = 0;
4582
Chris Masonea8efc72011-03-08 11:54:40 -05004583 /*
4584 * bump off for our next call to get_extent
4585 */
4586 off = extent_map_end(em);
4587 if (off >= max)
4588 end = 1;
4589
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004590 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004591 end = 1;
4592 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004593 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004594 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4595 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004596 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004597 flags |= (FIEMAP_EXTENT_DELALLOC |
4598 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004599 } else if (fieinfo->fi_extents_max) {
4600 u64 bytenr = em->block_start -
4601 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004602
Chris Masonea8efc72011-03-08 11:54:40 -05004603 disko = em->block_start + offset_in_extent;
Liu Bofe09e162013-09-22 12:54:23 +08004604
4605 /*
4606 * As btrfs supports shared space, this information
4607 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004608 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4609 * then we're just getting a count and we can skip the
4610 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004611 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06004612 ret = btrfs_check_shared(root,
4613 btrfs_ino(BTRFS_I(inode)),
4614 bytenr);
Josef Bacikdc046b12014-09-10 16:20:45 -04004615 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004616 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004617 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004618 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004619 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004620 }
4621 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4622 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004623 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4624 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004625
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004626 free_extent_map(em);
4627 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004628 if ((em_start >= last) || em_len == (u64)-1 ||
4629 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004630 flags |= FIEMAP_EXTENT_LAST;
4631 end = 1;
4632 }
4633
Chris Masonec29ed52011-02-23 16:23:20 -05004634 /* now scan forward to see if this is really the last extent. */
4635 em = get_extent_skip_holes(inode, off, last_for_get_extent,
4636 get_extent);
4637 if (IS_ERR(em)) {
4638 ret = PTR_ERR(em);
4639 goto out;
4640 }
4641 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004642 flags |= FIEMAP_EXTENT_LAST;
4643 end = 1;
4644 }
Qu Wenruo47518322017-04-07 10:43:15 +08004645 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4646 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004647 if (ret) {
4648 if (ret == 1)
4649 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004650 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004651 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004652 }
4653out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004654 if (!ret)
Qu Wenruo848c23b2017-06-22 10:01:21 +08004655 ret = emit_last_fiemap_cache(root->fs_info, fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004656 free_extent_map(em);
4657out:
Liu Bofe09e162013-09-22 12:54:23 +08004658 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004659 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004660 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004661 return ret;
4662}
4663
Chris Mason727011e2010-08-06 13:21:20 -04004664static void __free_extent_buffer(struct extent_buffer *eb)
4665{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004666 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004667 kmem_cache_free(extent_buffer_cache, eb);
4668}
4669
Josef Bacika26e8c92014-03-28 17:07:27 -04004670int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004671{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004672 return (atomic_read(&eb->io_pages) ||
4673 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4674 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004675}
4676
Miao Xie897ca6e92010-10-26 20:57:29 -04004677/*
4678 * Helper for releasing extent buffer page.
4679 */
David Sterbaa50924e2014-07-31 00:51:36 +02004680static void btrfs_release_extent_buffer_page(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004681{
4682 unsigned long index;
4683 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004684 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004685
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004686 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004687
David Sterbaa50924e2014-07-31 00:51:36 +02004688 index = num_extent_pages(eb->start, eb->len);
4689 if (index == 0)
Miao Xie897ca6e92010-10-26 20:57:29 -04004690 return;
4691
4692 do {
4693 index--;
David Sterbafb85fc92014-07-31 01:03:53 +02004694 page = eb->pages[index];
Forrest Liu5d2361d2015-02-09 17:31:45 +08004695 if (!page)
4696 continue;
4697 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004698 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004699 /*
4700 * We do this since we'll remove the pages after we've
4701 * removed the eb from the radix tree, so we could race
4702 * and have this page now attached to the new eb. So
4703 * only clear page_private if it's still connected to
4704 * this eb.
4705 */
4706 if (PagePrivate(page) &&
4707 page->private == (unsigned long)eb) {
4708 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4709 BUG_ON(PageDirty(page));
4710 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004711 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004712 * We need to make sure we haven't be attached
4713 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004714 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004715 ClearPagePrivate(page);
4716 set_page_private(page, 0);
4717 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004718 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004719 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004720
4721 if (mapped)
4722 spin_unlock(&page->mapping->private_lock);
4723
Nicholas D Steeves01327612016-05-19 21:18:45 -04004724 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004725 put_page(page);
David Sterbaa50924e2014-07-31 00:51:36 +02004726 } while (index != 0);
Miao Xie897ca6e92010-10-26 20:57:29 -04004727}
4728
4729/*
4730 * Helper for releasing the extent buffer.
4731 */
4732static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4733{
David Sterbaa50924e2014-07-31 00:51:36 +02004734 btrfs_release_extent_buffer_page(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004735 __free_extent_buffer(eb);
4736}
4737
Josef Bacikf28491e2013-12-16 13:24:27 -05004738static struct extent_buffer *
4739__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004740 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004741{
4742 struct extent_buffer *eb = NULL;
4743
Michal Hockod1b5c562015-08-19 14:17:40 +02004744 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004745 eb->start = start;
4746 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004747 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004748 eb->bflags = 0;
4749 rwlock_init(&eb->lock);
4750 atomic_set(&eb->write_locks, 0);
4751 atomic_set(&eb->read_locks, 0);
4752 atomic_set(&eb->blocking_readers, 0);
4753 atomic_set(&eb->blocking_writers, 0);
4754 atomic_set(&eb->spinning_readers, 0);
4755 atomic_set(&eb->spinning_writers, 0);
4756 eb->lock_nested = 0;
4757 init_waitqueue_head(&eb->write_lock_wq);
4758 init_waitqueue_head(&eb->read_lock_wq);
4759
4760 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4761
4762 spin_lock_init(&eb->refs_lock);
4763 atomic_set(&eb->refs, 1);
4764 atomic_set(&eb->io_pages, 0);
4765
4766 /*
4767 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4768 */
4769 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4770 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4771 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4772
4773 return eb;
4774}
4775
4776struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4777{
4778 unsigned long i;
4779 struct page *p;
4780 struct extent_buffer *new;
4781 unsigned long num_pages = num_extent_pages(src->start, src->len);
4782
David Sterba3f556f72014-06-15 03:20:26 +02004783 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004784 if (new == NULL)
4785 return NULL;
4786
4787 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004788 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004789 if (!p) {
4790 btrfs_release_extent_buffer(new);
4791 return NULL;
4792 }
4793 attach_extent_buffer_page(new, p);
4794 WARN_ON(PageDirty(p));
4795 SetPageUptodate(p);
4796 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004797 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004798 }
4799
Josef Bacikdb7f3432013-08-07 14:54:37 -04004800 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4801 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4802
4803 return new;
4804}
4805
Omar Sandoval0f331222015-09-29 20:50:31 -07004806struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4807 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004808{
4809 struct extent_buffer *eb;
David Sterba3f556f72014-06-15 03:20:26 +02004810 unsigned long num_pages;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004811 unsigned long i;
4812
Omar Sandoval0f331222015-09-29 20:50:31 -07004813 num_pages = num_extent_pages(start, len);
David Sterba3f556f72014-06-15 03:20:26 +02004814
4815 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004816 if (!eb)
4817 return NULL;
4818
4819 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004820 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004821 if (!eb->pages[i])
4822 goto err;
4823 }
4824 set_extent_buffer_uptodate(eb);
4825 btrfs_set_header_nritems(eb, 0);
4826 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4827
4828 return eb;
4829err:
4830 for (; i > 0; i--)
4831 __free_page(eb->pages[i - 1]);
4832 __free_extent_buffer(eb);
4833 return NULL;
4834}
4835
Omar Sandoval0f331222015-09-29 20:50:31 -07004836struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004837 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004838{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004839 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004840}
4841
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004842static void check_buffer_tree_ref(struct extent_buffer *eb)
4843{
Chris Mason242e18c2013-01-29 17:49:37 -05004844 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004845 /* the ref bit is tricky. We have to make sure it is set
4846 * if we have the buffer dirty. Otherwise the
4847 * code to free a buffer can end up dropping a dirty
4848 * page
4849 *
4850 * Once the ref bit is set, it won't go away while the
4851 * buffer is dirty or in writeback, and it also won't
4852 * go away while we have the reference count on the
4853 * eb bumped.
4854 *
4855 * We can't just set the ref bit without bumping the
4856 * ref on the eb because free_extent_buffer might
4857 * see the ref bit and try to clear it. If this happens
4858 * free_extent_buffer might end up dropping our original
4859 * ref by mistake and freeing the page before we are able
4860 * to add one more ref.
4861 *
4862 * So bump the ref count first, then set the bit. If someone
4863 * beat us to it, drop the ref we added.
4864 */
Chris Mason242e18c2013-01-29 17:49:37 -05004865 refs = atomic_read(&eb->refs);
4866 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4867 return;
4868
Josef Bacik594831c2012-07-20 16:11:08 -04004869 spin_lock(&eb->refs_lock);
4870 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004871 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004872 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004873}
4874
Mel Gorman2457aec2014-06-04 16:10:31 -07004875static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4876 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004877{
4878 unsigned long num_pages, i;
4879
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004880 check_buffer_tree_ref(eb);
4881
Josef Bacik5df42352012-03-15 18:24:42 -04004882 num_pages = num_extent_pages(eb->start, eb->len);
4883 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004884 struct page *p = eb->pages[i];
4885
Mel Gorman2457aec2014-06-04 16:10:31 -07004886 if (p != accessed)
4887 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004888 }
4889}
4890
Josef Bacikf28491e2013-12-16 13:24:27 -05004891struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4892 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004893{
4894 struct extent_buffer *eb;
4895
4896 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004897 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004898 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004899 if (eb && atomic_inc_not_zero(&eb->refs)) {
4900 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004901 /*
4902 * Lock our eb's refs_lock to avoid races with
4903 * free_extent_buffer. When we get our eb it might be flagged
4904 * with EXTENT_BUFFER_STALE and another task running
4905 * free_extent_buffer might have seen that flag set,
4906 * eb->refs == 2, that the buffer isn't under IO (dirty and
4907 * writeback flags not set) and it's still in the tree (flag
4908 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4909 * of decrementing the extent buffer's reference count twice.
4910 * So here we could race and increment the eb's reference count,
4911 * clear its stale flag, mark it as dirty and drop our reference
4912 * before the other task finishes executing free_extent_buffer,
4913 * which would later result in an attempt to free an extent
4914 * buffer that is dirty.
4915 */
4916 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4917 spin_lock(&eb->refs_lock);
4918 spin_unlock(&eb->refs_lock);
4919 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004920 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004921 return eb;
4922 }
4923 rcu_read_unlock();
4924
4925 return NULL;
4926}
4927
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004928#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4929struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004930 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004931{
4932 struct extent_buffer *eb, *exists = NULL;
4933 int ret;
4934
4935 eb = find_extent_buffer(fs_info, start);
4936 if (eb)
4937 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04004938 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004939 if (!eb)
4940 return NULL;
4941 eb->fs_info = fs_info;
4942again:
David Sterbae1860a72016-05-09 14:11:38 +02004943 ret = radix_tree_preload(GFP_NOFS);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004944 if (ret)
4945 goto free_eb;
4946 spin_lock(&fs_info->buffer_lock);
4947 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004948 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004949 spin_unlock(&fs_info->buffer_lock);
4950 radix_tree_preload_end();
4951 if (ret == -EEXIST) {
4952 exists = find_extent_buffer(fs_info, start);
4953 if (exists)
4954 goto free_eb;
4955 else
4956 goto again;
4957 }
4958 check_buffer_tree_ref(eb);
4959 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
4960
4961 /*
4962 * We will free dummy extent buffer's if they come into
4963 * free_extent_buffer with a ref count of 2, but if we are using this we
4964 * want the buffers to stay in memory until we're done with them, so
4965 * bump the ref count again.
4966 */
4967 atomic_inc(&eb->refs);
4968 return eb;
4969free_eb:
4970 btrfs_release_extent_buffer(eb);
4971 return exists;
4972}
4973#endif
4974
Josef Bacikf28491e2013-12-16 13:24:27 -05004975struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004976 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05004977{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004978 unsigned long len = fs_info->nodesize;
Chris Masond1310b22008-01-24 16:13:08 -05004979 unsigned long num_pages = num_extent_pages(start, len);
4980 unsigned long i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004981 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004982 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004983 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004984 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05004985 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05004986 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004987 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004988
Jeff Mahoneyda170662016-06-15 09:22:56 -04004989 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07004990 btrfs_err(fs_info, "bad tree block start %llu", start);
4991 return ERR_PTR(-EINVAL);
4992 }
4993
Josef Bacikf28491e2013-12-16 13:24:27 -05004994 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004995 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004996 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004997
David Sterba23d79d82014-06-15 02:55:29 +02004998 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04004999 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07005000 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05005001
Chris Mason727011e2010-08-06 13:21:20 -04005002 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02005003 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07005004 if (!p) {
5005 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04005006 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005007 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005008
5009 spin_lock(&mapping->private_lock);
5010 if (PagePrivate(p)) {
5011 /*
5012 * We could have already allocated an eb for this page
5013 * and attached one so lets see if we can get a ref on
5014 * the existing eb, and if we can we know it's good and
5015 * we can just return that one, else we know we can just
5016 * overwrite page->private.
5017 */
5018 exists = (struct extent_buffer *)p->private;
5019 if (atomic_inc_not_zero(&exists->refs)) {
5020 spin_unlock(&mapping->private_lock);
5021 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005022 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07005023 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005024 goto free_eb;
5025 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005026 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005027
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005028 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005029 * Do this so attach doesn't complain and we need to
5030 * drop the ref the old guy had.
5031 */
5032 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005033 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005034 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05005035 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005036 attach_extent_buffer_page(eb, p);
5037 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005038 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04005039 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05005040 if (!PageUptodate(p))
5041 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05005042
5043 /*
5044 * see below about how we avoid a nasty race with release page
5045 * and why we unlock later
5046 */
Chris Masond1310b22008-01-24 16:13:08 -05005047 }
5048 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05005049 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05005050again:
David Sterbae1860a72016-05-09 14:11:38 +02005051 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07005052 if (ret) {
5053 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04005054 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005055 }
Miao Xie19fe0a82010-10-26 20:57:29 -04005056
Josef Bacikf28491e2013-12-16 13:24:27 -05005057 spin_lock(&fs_info->buffer_lock);
5058 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005059 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05005060 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005061 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04005062 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005063 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005064 if (exists)
5065 goto free_eb;
5066 else
Josef Bacik115391d2012-03-09 09:51:43 -05005067 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04005068 }
Chris Mason6af118ce2008-07-22 11:18:07 -04005069 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005070 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005071 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005072
5073 /*
5074 * there is a race where release page may have
5075 * tried to find this extent buffer in the radix
5076 * but failed. It will tell the VM it is safe to
5077 * reclaim the, and it will clear the page private bit.
5078 * We must make sure to set the page private bit properly
5079 * after the extent buffer is in the radix tree so
5080 * it doesn't get lost
5081 */
Chris Mason727011e2010-08-06 13:21:20 -04005082 SetPageChecked(eb->pages[0]);
5083 for (i = 1; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005084 p = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005085 ClearPageChecked(p);
5086 unlock_page(p);
5087 }
5088 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05005089 return eb;
5090
Chris Mason6af118ce2008-07-22 11:18:07 -04005091free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005092 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005093 for (i = 0; i < num_pages; i++) {
5094 if (eb->pages[i])
5095 unlock_page(eb->pages[i]);
5096 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005097
Miao Xie897ca6e92010-10-26 20:57:29 -04005098 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005099 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005100}
Chris Masond1310b22008-01-24 16:13:08 -05005101
Josef Bacik3083ee22012-03-09 16:01:49 -05005102static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5103{
5104 struct extent_buffer *eb =
5105 container_of(head, struct extent_buffer, rcu_head);
5106
5107 __free_extent_buffer(eb);
5108}
5109
Josef Bacik3083ee22012-03-09 16:01:49 -05005110/* Expects to have eb->eb_lock already held */
David Sterbaf7a52a42013-04-26 14:56:29 +00005111static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005112{
5113 WARN_ON(atomic_read(&eb->refs) == 0);
5114 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005115 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005116 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005117
Jan Schmidt815a51c2012-05-16 17:00:02 +02005118 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005119
Josef Bacikf28491e2013-12-16 13:24:27 -05005120 spin_lock(&fs_info->buffer_lock);
5121 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005122 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005123 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005124 } else {
5125 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005126 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005127
5128 /* Should be safe to release our pages at this point */
David Sterbaa50924e2014-07-31 00:51:36 +02005129 btrfs_release_extent_buffer_page(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005130#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
5131 if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) {
5132 __free_extent_buffer(eb);
5133 return 1;
5134 }
5135#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005136 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005137 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005138 }
5139 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005140
5141 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005142}
5143
Chris Masond1310b22008-01-24 16:13:08 -05005144void free_extent_buffer(struct extent_buffer *eb)
5145{
Chris Mason242e18c2013-01-29 17:49:37 -05005146 int refs;
5147 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005148 if (!eb)
5149 return;
5150
Chris Mason242e18c2013-01-29 17:49:37 -05005151 while (1) {
5152 refs = atomic_read(&eb->refs);
5153 if (refs <= 3)
5154 break;
5155 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5156 if (old == refs)
5157 return;
5158 }
5159
Josef Bacik3083ee22012-03-09 16:01:49 -05005160 spin_lock(&eb->refs_lock);
5161 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02005162 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
5163 atomic_dec(&eb->refs);
5164
5165 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005166 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005167 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005168 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5169 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005170
Josef Bacik3083ee22012-03-09 16:01:49 -05005171 /*
5172 * I know this is terrible, but it's temporary until we stop tracking
5173 * the uptodate bits and such for the extent buffers.
5174 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005175 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005176}
Chris Masond1310b22008-01-24 16:13:08 -05005177
Josef Bacik3083ee22012-03-09 16:01:49 -05005178void free_extent_buffer_stale(struct extent_buffer *eb)
5179{
5180 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005181 return;
5182
Josef Bacik3083ee22012-03-09 16:01:49 -05005183 spin_lock(&eb->refs_lock);
5184 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5185
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005186 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005187 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5188 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005189 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005190}
5191
Chris Mason1d4284b2012-03-28 20:31:37 -04005192void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005193{
Chris Masond1310b22008-01-24 16:13:08 -05005194 unsigned long i;
5195 unsigned long num_pages;
5196 struct page *page;
5197
Chris Masond1310b22008-01-24 16:13:08 -05005198 num_pages = num_extent_pages(eb->start, eb->len);
5199
5200 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005201 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005202 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005203 continue;
5204
Chris Masona61e6f22008-07-22 11:18:08 -04005205 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005206 WARN_ON(!PagePrivate(page));
5207
Chris Masond1310b22008-01-24 16:13:08 -05005208 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005209 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05005210 if (!PageDirty(page)) {
5211 radix_tree_tag_clear(&page->mapping->page_tree,
5212 page_index(page),
5213 PAGECACHE_TAG_DIRTY);
5214 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005215 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005216 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005217 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005218 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005219 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005220}
Chris Masond1310b22008-01-24 16:13:08 -05005221
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005222int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005223{
5224 unsigned long i;
5225 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04005226 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005227
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005228 check_buffer_tree_ref(eb);
5229
Chris Masonb9473432009-03-13 11:00:37 -04005230 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005231
Chris Masond1310b22008-01-24 16:13:08 -05005232 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05005233 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005234 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5235
Chris Masonb9473432009-03-13 11:00:37 -04005236 for (i = 0; i < num_pages; i++)
David Sterbafb85fc92014-07-31 01:03:53 +02005237 set_page_dirty(eb->pages[i]);
Chris Masonb9473432009-03-13 11:00:37 -04005238 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005239}
Chris Masond1310b22008-01-24 16:13:08 -05005240
David Sterba69ba3922015-12-03 13:08:59 +01005241void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005242{
5243 unsigned long i;
5244 struct page *page;
5245 unsigned long num_pages;
5246
Chris Masonb4ce94d2009-02-04 09:25:08 -05005247 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005248 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04005249 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005250 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005251 if (page)
5252 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005253 }
Chris Mason1259ab72008-05-12 13:39:03 -04005254}
5255
David Sterba09c25a82015-12-03 13:08:59 +01005256void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005257{
5258 unsigned long i;
5259 struct page *page;
5260 unsigned long num_pages;
5261
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005262 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05005263 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05005264 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005265 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005266 SetPageUptodate(page);
5267 }
Chris Masond1310b22008-01-24 16:13:08 -05005268}
Chris Masond1310b22008-01-24 16:13:08 -05005269
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005270int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005271{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005272 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05005273}
Chris Masond1310b22008-01-24 16:13:08 -05005274
5275int read_extent_buffer_pages(struct extent_io_tree *tree,
Josef Bacik8436ea912016-09-02 15:40:03 -04005276 struct extent_buffer *eb, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04005277 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005278{
5279 unsigned long i;
Chris Masond1310b22008-01-24 16:13:08 -05005280 struct page *page;
5281 int err;
5282 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005283 int locked_pages = 0;
5284 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05005285 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005286 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005287 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005288 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005289
Chris Masonb4ce94d2009-02-04 09:25:08 -05005290 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005291 return 0;
5292
Chris Masond1310b22008-01-24 16:13:08 -05005293 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik8436ea912016-09-02 15:40:03 -04005294 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005295 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005296 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005297 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005298 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005299 } else {
5300 lock_page(page);
5301 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005302 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005303 }
5304 /*
5305 * We need to firstly lock all pages to make sure that
5306 * the uptodate bit of our pages won't be affected by
5307 * clear_extent_buffer_uptodate().
5308 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005309 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005310 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005311 if (!PageUptodate(page)) {
5312 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005313 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005314 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005315 }
Liu Bo2571e732016-08-03 12:33:01 -07005316
Chris Masonce9adaa2008-04-09 16:28:12 -04005317 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005318 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005319 goto unlock_exit;
5320 }
5321
Filipe Manana656f30d2014-09-26 12:25:56 +01005322 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005323 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005324 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005325 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005326 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005327
Chris Masonce9adaa2008-04-09 16:28:12 -04005328 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005329 if (ret) {
5330 atomic_dec(&eb->io_pages);
5331 unlock_page(page);
5332 continue;
5333 }
5334
Chris Masonf1885912008-04-09 16:28:12 -04005335 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005336 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04005337 get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005338 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005339 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005340 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005341 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005342 /*
5343 * We use &bio in above __extent_read_full_page,
5344 * so we ensure that if it returns error, the
5345 * current page fails to add itself to bio and
5346 * it's been unlocked.
5347 *
5348 * We must dec io_pages by ourselves.
5349 */
5350 atomic_dec(&eb->io_pages);
5351 }
Chris Masond1310b22008-01-24 16:13:08 -05005352 } else {
5353 unlock_page(page);
5354 }
5355 }
5356
Jeff Mahoney355808c2011-10-03 23:23:14 -04005357 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005358 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005359 if (err)
5360 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005361 }
Chris Masona86c12c2008-02-07 10:50:54 -05005362
Arne Jansenbb82ab82011-06-10 14:06:53 +02005363 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005364 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005365
Josef Bacik8436ea912016-09-02 15:40:03 -04005366 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005367 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005368 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005369 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005370 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005371 }
Chris Masond3977122009-01-05 21:25:51 -05005372
Chris Masond1310b22008-01-24 16:13:08 -05005373 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005374
5375unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005376 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005377 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005378 page = eb->pages[locked_pages];
5379 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005380 }
5381 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005382}
Chris Masond1310b22008-01-24 16:13:08 -05005383
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005384void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5385 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005386{
5387 size_t cur;
5388 size_t offset;
5389 struct page *page;
5390 char *kaddr;
5391 char *dst = (char *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005392 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5393 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005394
Liu Bof716abd2017-08-09 11:10:16 -06005395 if (start + len > eb->len) {
5396 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5397 eb->start, eb->len, start, len);
5398 memset(dst, 0, len);
5399 return;
5400 }
Chris Masond1310b22008-01-24 16:13:08 -05005401
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005402 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005403
Chris Masond3977122009-01-05 21:25:51 -05005404 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005405 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005406
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005407 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005408 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005409 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005410
5411 dst += cur;
5412 len -= cur;
5413 offset = 0;
5414 i++;
5415 }
5416}
Chris Masond1310b22008-01-24 16:13:08 -05005417
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005418int read_extent_buffer_to_user(const struct extent_buffer *eb,
5419 void __user *dstv,
5420 unsigned long start, unsigned long len)
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005421{
5422 size_t cur;
5423 size_t offset;
5424 struct page *page;
5425 char *kaddr;
5426 char __user *dst = (char __user *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005427 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5428 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005429 int ret = 0;
5430
5431 WARN_ON(start > eb->len);
5432 WARN_ON(start + len > eb->start + eb->len);
5433
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005434 offset = (start_offset + start) & (PAGE_SIZE - 1);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005435
5436 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005437 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005438
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005439 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005440 kaddr = page_address(page);
5441 if (copy_to_user(dst, kaddr + offset, cur)) {
5442 ret = -EFAULT;
5443 break;
5444 }
5445
5446 dst += cur;
5447 len -= cur;
5448 offset = 0;
5449 i++;
5450 }
5451
5452 return ret;
5453}
5454
Liu Bo415b35a2016-06-17 19:16:21 -07005455/*
5456 * return 0 if the item is found within a page.
5457 * return 1 if the item spans two pages.
5458 * return -EINVAL otherwise.
5459 */
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005460int map_private_extent_buffer(const struct extent_buffer *eb,
5461 unsigned long start, unsigned long min_len,
5462 char **map, unsigned long *map_start,
5463 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005464{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005465 size_t offset = start & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005466 char *kaddr;
5467 struct page *p;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005468 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5469 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005470 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005471 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005472
Liu Bof716abd2017-08-09 11:10:16 -06005473 if (start + min_len > eb->len) {
5474 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5475 eb->start, eb->len, start, min_len);
5476 return -EINVAL;
5477 }
5478
Chris Masond1310b22008-01-24 16:13:08 -05005479 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005480 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005481
5482 if (i == 0) {
5483 offset = start_offset;
5484 *map_start = 0;
5485 } else {
5486 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005487 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005488 }
Chris Masond3977122009-01-05 21:25:51 -05005489
David Sterbafb85fc92014-07-31 01:03:53 +02005490 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005491 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005492 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005493 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005494 return 0;
5495}
Chris Masond1310b22008-01-24 16:13:08 -05005496
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005497int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5498 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005499{
5500 size_t cur;
5501 size_t offset;
5502 struct page *page;
5503 char *kaddr;
5504 char *ptr = (char *)ptrv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005505 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5506 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005507 int ret = 0;
5508
5509 WARN_ON(start > eb->len);
5510 WARN_ON(start + len > eb->start + eb->len);
5511
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005512 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005513
Chris Masond3977122009-01-05 21:25:51 -05005514 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005515 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005516
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005517 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005518
Chris Masona6591712011-07-19 12:04:14 -04005519 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005520 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005521 if (ret)
5522 break;
5523
5524 ptr += cur;
5525 len -= cur;
5526 offset = 0;
5527 i++;
5528 }
5529 return ret;
5530}
Chris Masond1310b22008-01-24 16:13:08 -05005531
David Sterbaf157bf72016-11-09 17:43:38 +01005532void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5533 const void *srcv)
5534{
5535 char *kaddr;
5536
5537 WARN_ON(!PageUptodate(eb->pages[0]));
5538 kaddr = page_address(eb->pages[0]);
5539 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5540 BTRFS_FSID_SIZE);
5541}
5542
5543void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5544{
5545 char *kaddr;
5546
5547 WARN_ON(!PageUptodate(eb->pages[0]));
5548 kaddr = page_address(eb->pages[0]);
5549 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5550 BTRFS_FSID_SIZE);
5551}
5552
Chris Masond1310b22008-01-24 16:13:08 -05005553void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5554 unsigned long start, unsigned long len)
5555{
5556 size_t cur;
5557 size_t offset;
5558 struct page *page;
5559 char *kaddr;
5560 char *src = (char *)srcv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005561 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5562 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005563
5564 WARN_ON(start > eb->len);
5565 WARN_ON(start + len > eb->start + eb->len);
5566
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005567 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005568
Chris Masond3977122009-01-05 21:25:51 -05005569 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005570 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005571 WARN_ON(!PageUptodate(page));
5572
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005573 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005574 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005575 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005576
5577 src += cur;
5578 len -= cur;
5579 offset = 0;
5580 i++;
5581 }
5582}
Chris Masond1310b22008-01-24 16:13:08 -05005583
David Sterbab159fa22016-11-08 18:09:03 +01005584void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5585 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005586{
5587 size_t cur;
5588 size_t offset;
5589 struct page *page;
5590 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005591 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5592 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005593
5594 WARN_ON(start > eb->len);
5595 WARN_ON(start + len > eb->start + eb->len);
5596
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005597 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005598
Chris Masond3977122009-01-05 21:25:51 -05005599 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005600 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005601 WARN_ON(!PageUptodate(page));
5602
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005603 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005604 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005605 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005606
5607 len -= cur;
5608 offset = 0;
5609 i++;
5610 }
5611}
Chris Masond1310b22008-01-24 16:13:08 -05005612
David Sterba58e80122016-11-08 18:30:31 +01005613void copy_extent_buffer_full(struct extent_buffer *dst,
5614 struct extent_buffer *src)
5615{
5616 int i;
5617 unsigned num_pages;
5618
5619 ASSERT(dst->len == src->len);
5620
5621 num_pages = num_extent_pages(dst->start, dst->len);
5622 for (i = 0; i < num_pages; i++)
5623 copy_page(page_address(dst->pages[i]),
5624 page_address(src->pages[i]));
5625}
5626
Chris Masond1310b22008-01-24 16:13:08 -05005627void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5628 unsigned long dst_offset, unsigned long src_offset,
5629 unsigned long len)
5630{
5631 u64 dst_len = dst->len;
5632 size_t cur;
5633 size_t offset;
5634 struct page *page;
5635 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005636 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
5637 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005638
5639 WARN_ON(src->len != dst_len);
5640
5641 offset = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005642 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005643
Chris Masond3977122009-01-05 21:25:51 -05005644 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005645 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005646 WARN_ON(!PageUptodate(page));
5647
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005648 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005649
Chris Masona6591712011-07-19 12:04:14 -04005650 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005651 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005652
5653 src_offset += cur;
5654 len -= cur;
5655 offset = 0;
5656 i++;
5657 }
5658}
Chris Masond1310b22008-01-24 16:13:08 -05005659
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005660void le_bitmap_set(u8 *map, unsigned int start, int len)
5661{
5662 u8 *p = map + BIT_BYTE(start);
5663 const unsigned int size = start + len;
5664 int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
5665 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
5666
5667 while (len - bits_to_set >= 0) {
5668 *p |= mask_to_set;
5669 len -= bits_to_set;
5670 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005671 mask_to_set = ~0;
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005672 p++;
5673 }
5674 if (len) {
5675 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5676 *p |= mask_to_set;
5677 }
5678}
5679
5680void le_bitmap_clear(u8 *map, unsigned int start, int len)
5681{
5682 u8 *p = map + BIT_BYTE(start);
5683 const unsigned int size = start + len;
5684 int bits_to_clear = BITS_PER_BYTE - (start % BITS_PER_BYTE);
5685 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(start);
5686
5687 while (len - bits_to_clear >= 0) {
5688 *p &= ~mask_to_clear;
5689 len -= bits_to_clear;
5690 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005691 mask_to_clear = ~0;
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005692 p++;
5693 }
5694 if (len) {
5695 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5696 *p &= ~mask_to_clear;
5697 }
5698}
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005699
5700/*
5701 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5702 * given bit number
5703 * @eb: the extent buffer
5704 * @start: offset of the bitmap item in the extent buffer
5705 * @nr: bit number
5706 * @page_index: return index of the page in the extent buffer that contains the
5707 * given bit number
5708 * @page_offset: return offset into the page given by page_index
5709 *
5710 * This helper hides the ugliness of finding the byte in an extent buffer which
5711 * contains a given bit.
5712 */
5713static inline void eb_bitmap_offset(struct extent_buffer *eb,
5714 unsigned long start, unsigned long nr,
5715 unsigned long *page_index,
5716 size_t *page_offset)
5717{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005718 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005719 size_t byte_offset = BIT_BYTE(nr);
5720 size_t offset;
5721
5722 /*
5723 * The byte we want is the offset of the extent buffer + the offset of
5724 * the bitmap item in the extent buffer + the offset of the byte in the
5725 * bitmap item.
5726 */
5727 offset = start_offset + start + byte_offset;
5728
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005729 *page_index = offset >> PAGE_SHIFT;
5730 *page_offset = offset & (PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005731}
5732
5733/**
5734 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5735 * @eb: the extent buffer
5736 * @start: offset of the bitmap item in the extent buffer
5737 * @nr: bit number to test
5738 */
5739int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5740 unsigned long nr)
5741{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005742 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005743 struct page *page;
5744 unsigned long i;
5745 size_t offset;
5746
5747 eb_bitmap_offset(eb, start, nr, &i, &offset);
5748 page = eb->pages[i];
5749 WARN_ON(!PageUptodate(page));
5750 kaddr = page_address(page);
5751 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5752}
5753
5754/**
5755 * extent_buffer_bitmap_set - set an area of a bitmap
5756 * @eb: the extent buffer
5757 * @start: offset of the bitmap item in the extent buffer
5758 * @pos: bit number of the first bit
5759 * @len: number of bits to set
5760 */
5761void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5762 unsigned long pos, unsigned long len)
5763{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005764 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005765 struct page *page;
5766 unsigned long i;
5767 size_t offset;
5768 const unsigned int size = pos + len;
5769 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005770 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005771
5772 eb_bitmap_offset(eb, start, pos, &i, &offset);
5773 page = eb->pages[i];
5774 WARN_ON(!PageUptodate(page));
5775 kaddr = page_address(page);
5776
5777 while (len >= bits_to_set) {
5778 kaddr[offset] |= mask_to_set;
5779 len -= bits_to_set;
5780 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005781 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005782 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005783 offset = 0;
5784 page = eb->pages[++i];
5785 WARN_ON(!PageUptodate(page));
5786 kaddr = page_address(page);
5787 }
5788 }
5789 if (len) {
5790 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5791 kaddr[offset] |= mask_to_set;
5792 }
5793}
5794
5795
5796/**
5797 * extent_buffer_bitmap_clear - clear an area of a bitmap
5798 * @eb: the extent buffer
5799 * @start: offset of the bitmap item in the extent buffer
5800 * @pos: bit number of the first bit
5801 * @len: number of bits to clear
5802 */
5803void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5804 unsigned long pos, unsigned long len)
5805{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005806 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005807 struct page *page;
5808 unsigned long i;
5809 size_t offset;
5810 const unsigned int size = pos + len;
5811 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005812 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005813
5814 eb_bitmap_offset(eb, start, pos, &i, &offset);
5815 page = eb->pages[i];
5816 WARN_ON(!PageUptodate(page));
5817 kaddr = page_address(page);
5818
5819 while (len >= bits_to_clear) {
5820 kaddr[offset] &= ~mask_to_clear;
5821 len -= bits_to_clear;
5822 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005823 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005824 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005825 offset = 0;
5826 page = eb->pages[++i];
5827 WARN_ON(!PageUptodate(page));
5828 kaddr = page_address(page);
5829 }
5830 }
5831 if (len) {
5832 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5833 kaddr[offset] &= ~mask_to_clear;
5834 }
5835}
5836
Sergei Trofimovich33872062011-04-11 21:52:52 +00005837static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5838{
5839 unsigned long distance = (src > dst) ? src - dst : dst - src;
5840 return distance < len;
5841}
5842
Chris Masond1310b22008-01-24 16:13:08 -05005843static void copy_pages(struct page *dst_page, struct page *src_page,
5844 unsigned long dst_off, unsigned long src_off,
5845 unsigned long len)
5846{
Chris Masona6591712011-07-19 12:04:14 -04005847 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005848 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005849 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005850
Sergei Trofimovich33872062011-04-11 21:52:52 +00005851 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005852 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005853 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005854 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005855 if (areas_overlap(src_off, dst_off, len))
5856 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005857 }
Chris Masond1310b22008-01-24 16:13:08 -05005858
Chris Mason727011e2010-08-06 13:21:20 -04005859 if (must_memmove)
5860 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5861 else
5862 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005863}
5864
5865void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5866 unsigned long src_offset, unsigned long len)
5867{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005868 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005869 size_t cur;
5870 size_t dst_off_in_page;
5871 size_t src_off_in_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005872 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005873 unsigned long dst_i;
5874 unsigned long src_i;
5875
5876 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005877 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005878 "memmove bogus src_offset %lu move len %lu dst len %lu",
5879 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005880 BUG_ON(1);
5881 }
5882 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005883 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005884 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5885 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005886 BUG_ON(1);
5887 }
5888
Chris Masond3977122009-01-05 21:25:51 -05005889 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005890 dst_off_in_page = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005891 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005892 src_off_in_page = (start_offset + src_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005893 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005894
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005895 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5896 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005897
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005898 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005899 src_off_in_page));
5900 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005901 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005902
David Sterbafb85fc92014-07-31 01:03:53 +02005903 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005904 dst_off_in_page, src_off_in_page, cur);
5905
5906 src_offset += cur;
5907 dst_offset += cur;
5908 len -= cur;
5909 }
5910}
Chris Masond1310b22008-01-24 16:13:08 -05005911
5912void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5913 unsigned long src_offset, unsigned long len)
5914{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005915 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005916 size_t cur;
5917 size_t dst_off_in_page;
5918 size_t src_off_in_page;
5919 unsigned long dst_end = dst_offset + len - 1;
5920 unsigned long src_end = src_offset + len - 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005921 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005922 unsigned long dst_i;
5923 unsigned long src_i;
5924
5925 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005926 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005927 "memmove bogus src_offset %lu move len %lu len %lu",
5928 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005929 BUG_ON(1);
5930 }
5931 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005932 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005933 "memmove bogus dst_offset %lu move len %lu len %lu",
5934 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005935 BUG_ON(1);
5936 }
Chris Mason727011e2010-08-06 13:21:20 -04005937 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005938 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5939 return;
5940 }
Chris Masond3977122009-01-05 21:25:51 -05005941 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005942 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
5943 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005944
5945 dst_off_in_page = (start_offset + dst_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005946 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005947 src_off_in_page = (start_offset + src_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005948 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005949
5950 cur = min_t(unsigned long, len, src_off_in_page + 1);
5951 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005952 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005953 dst_off_in_page - cur + 1,
5954 src_off_in_page - cur + 1, cur);
5955
5956 dst_end -= cur;
5957 src_end -= cur;
5958 len -= cur;
5959 }
5960}
Chris Mason6af118ce2008-07-22 11:18:07 -04005961
David Sterbaf7a52a42013-04-26 14:56:29 +00005962int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005963{
Chris Mason6af118ce2008-07-22 11:18:07 -04005964 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005965
Miao Xie19fe0a82010-10-26 20:57:29 -04005966 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005967 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05005968 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005969 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005970 spin_lock(&page->mapping->private_lock);
5971 if (!PagePrivate(page)) {
5972 spin_unlock(&page->mapping->private_lock);
5973 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005974 }
5975
Josef Bacik3083ee22012-03-09 16:01:49 -05005976 eb = (struct extent_buffer *)page->private;
5977 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005978
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005979 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005980 * This is a little awful but should be ok, we need to make sure that
5981 * the eb doesn't disappear out from under us while we're looking at
5982 * this page.
5983 */
5984 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005985 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005986 spin_unlock(&eb->refs_lock);
5987 spin_unlock(&page->mapping->private_lock);
5988 return 0;
5989 }
5990 spin_unlock(&page->mapping->private_lock);
5991
Josef Bacik3083ee22012-03-09 16:01:49 -05005992 /*
5993 * If tree ref isn't set then we know the ref on this eb is a real ref,
5994 * so just return, this page will likely be freed soon anyway.
5995 */
5996 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5997 spin_unlock(&eb->refs_lock);
5998 return 0;
5999 }
Josef Bacik3083ee22012-03-09 16:01:49 -05006000
David Sterbaf7a52a42013-04-26 14:56:29 +00006001 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04006002}