blob: 43091b29b8d7f081eb3be8c19f12c161dd7fb96b [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
David Sterbac1d7c512018-04-03 19:23:33 +02002
Chris Masond1310b22008-01-24 16:13:08 -05003#include <linux/bitops.h>
4#include <linux/slab.h>
5#include <linux/bio.h>
6#include <linux/mm.h>
Chris Masond1310b22008-01-24 16:13:08 -05007#include <linux/pagemap.h>
8#include <linux/page-flags.h>
Chris Masond1310b22008-01-24 16:13:08 -05009#include <linux/spinlock.h>
10#include <linux/blkdev.h>
11#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050012#include <linux/writeback.h>
13#include <linux/pagevec.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070014#include <linux/prefetch.h>
Dan Magenheimer90a887c2011-05-26 10:01:56 -060015#include <linux/cleancache.h>
Chris Masond1310b22008-01-24 16:13:08 -050016#include "extent_io.h"
17#include "extent_map.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040018#include "ctree.h"
19#include "btrfs_inode.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020020#include "volumes.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010021#include "check-integrity.h"
Josef Bacik0b32f4b2012-03-13 09:38:00 -040022#include "locking.h"
Josef Bacik606686e2012-06-04 14:03:51 -040023#include "rcu-string.h"
Liu Bofe09e162013-09-22 12:54:23 +080024#include "backref.h"
David Sterba6af49db2017-06-23 04:09:57 +020025#include "disk-io.h"
Chris Masond1310b22008-01-24 16:13:08 -050026
Chris Masond1310b22008-01-24 16:13:08 -050027static struct kmem_cache *extent_state_cache;
28static struct kmem_cache *extent_buffer_cache;
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -040029static struct bio_set btrfs_bioset;
Chris Masond1310b22008-01-24 16:13:08 -050030
Filipe Manana27a35072014-07-06 20:09:59 +010031static inline bool extent_state_in_tree(const struct extent_state *state)
32{
33 return !RB_EMPTY_NODE(&state->rb_node);
34}
35
Eric Sandeen6d49ba12013-04-22 16:12:31 +000036#ifdef CONFIG_BTRFS_DEBUG
Chris Masond1310b22008-01-24 16:13:08 -050037static LIST_HEAD(buffers);
38static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040039
Chris Masond3977122009-01-05 21:25:51 -050040static DEFINE_SPINLOCK(leak_lock);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000041
42static inline
43void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
44{
45 unsigned long flags;
46
47 spin_lock_irqsave(&leak_lock, flags);
48 list_add(new, head);
49 spin_unlock_irqrestore(&leak_lock, flags);
50}
51
52static inline
53void btrfs_leak_debug_del(struct list_head *entry)
54{
55 unsigned long flags;
56
57 spin_lock_irqsave(&leak_lock, flags);
58 list_del(entry);
59 spin_unlock_irqrestore(&leak_lock, flags);
60}
61
62static inline
63void btrfs_leak_debug_check(void)
64{
65 struct extent_state *state;
66 struct extent_buffer *eb;
67
68 while (!list_empty(&states)) {
69 state = list_entry(states.next, struct extent_state, leak_list);
David Sterba9ee49a042015-01-14 19:52:13 +010070 pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
Filipe Manana27a35072014-07-06 20:09:59 +010071 state->start, state->end, state->state,
72 extent_state_in_tree(state),
Elena Reshetovab7ac31b2017-03-03 10:55:19 +020073 refcount_read(&state->refs));
Eric Sandeen6d49ba12013-04-22 16:12:31 +000074 list_del(&state->leak_list);
75 kmem_cache_free(extent_state_cache, state);
76 }
77
78 while (!list_empty(&buffers)) {
79 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Liu Boaf2679e2018-01-25 11:02:48 -070080 pr_err("BTRFS: buffer leak start %llu len %lu refs %d bflags %lu\n",
81 eb->start, eb->len, atomic_read(&eb->refs), eb->bflags);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000082 list_del(&eb->leak_list);
83 kmem_cache_free(extent_buffer_cache, eb);
84 }
85}
David Sterba8d599ae2013-04-30 15:22:23 +000086
Josef Bacika5dee372013-12-13 10:02:44 -050087#define btrfs_debug_check_extent_io_range(tree, start, end) \
88 __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
David Sterba8d599ae2013-04-30 15:22:23 +000089static inline void __btrfs_debug_check_extent_io_range(const char *caller,
Josef Bacika5dee372013-12-13 10:02:44 -050090 struct extent_io_tree *tree, u64 start, u64 end)
David Sterba8d599ae2013-04-30 15:22:23 +000091{
Nikolay Borisov65a680f2018-11-01 14:09:49 +020092 struct inode *inode = tree->private_data;
93 u64 isize;
94
95 if (!inode || !is_data_inode(inode))
96 return;
97
98 isize = i_size_read(inode);
99 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
100 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
101 "%s: ino %llu isize %llu odd range [%llu,%llu]",
102 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
103 }
David Sterba8d599ae2013-04-30 15:22:23 +0000104}
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000105#else
106#define btrfs_leak_debug_add(new, head) do {} while (0)
107#define btrfs_leak_debug_del(entry) do {} while (0)
108#define btrfs_leak_debug_check() do {} while (0)
David Sterba8d599ae2013-04-30 15:22:23 +0000109#define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
Chris Mason4bef0842008-09-08 11:18:08 -0400110#endif
Chris Masond1310b22008-01-24 16:13:08 -0500111
Chris Masond1310b22008-01-24 16:13:08 -0500112#define BUFFER_LRU_MAX 64
113
114struct tree_entry {
115 u64 start;
116 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -0500117 struct rb_node rb_node;
118};
119
120struct extent_page_data {
121 struct bio *bio;
122 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -0500123 /* tells writepage not to lock the state bits for this range
124 * it still does the unlocking
125 */
Chris Masonffbd5172009-04-20 15:50:09 -0400126 unsigned int extent_locked:1;
127
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600128 /* tells the submit_bio code to use REQ_SYNC */
Chris Masonffbd5172009-04-20 15:50:09 -0400129 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -0500130};
131
David Sterba57599c72018-03-01 17:56:34 +0100132static int add_extent_changeset(struct extent_state *state, unsigned bits,
Qu Wenruod38ed272015-10-12 14:53:37 +0800133 struct extent_changeset *changeset,
134 int set)
135{
136 int ret;
137
138 if (!changeset)
David Sterba57599c72018-03-01 17:56:34 +0100139 return 0;
Qu Wenruod38ed272015-10-12 14:53:37 +0800140 if (set && (state->state & bits) == bits)
David Sterba57599c72018-03-01 17:56:34 +0100141 return 0;
Qu Wenruofefdc552015-10-12 15:35:38 +0800142 if (!set && (state->state & bits) == 0)
David Sterba57599c72018-03-01 17:56:34 +0100143 return 0;
Qu Wenruod38ed272015-10-12 14:53:37 +0800144 changeset->bytes_changed += state->end - state->start + 1;
David Sterba53d32352017-02-13 13:42:29 +0100145 ret = ulist_add(&changeset->range_changed, state->start, state->end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800146 GFP_ATOMIC);
David Sterba57599c72018-03-01 17:56:34 +0100147 return ret;
Qu Wenruod38ed272015-10-12 14:53:37 +0800148}
149
Qu Wenruobb58eb92019-01-25 13:09:15 +0800150static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
151 unsigned long bio_flags)
152{
153 blk_status_t ret = 0;
154 struct bio_vec *bvec = bio_last_bvec_all(bio);
Linus Torvalds80201fe2019-03-08 14:12:17 -0800155 struct bio_vec bv;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800156 struct extent_io_tree *tree = bio->bi_private;
157 u64 start;
158
Linus Torvalds80201fe2019-03-08 14:12:17 -0800159 mp_bvec_last_segment(bvec, &bv);
160 start = page_offset(bv.bv_page) + bv.bv_offset;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800161
162 bio->bi_private = NULL;
163
164 if (tree->ops)
165 ret = tree->ops->submit_bio_hook(tree->private_data, bio,
166 mirror_num, bio_flags, start);
167 else
168 btrfsic_submit_bio(bio);
169
170 return blk_status_to_errno(ret);
171}
172
Qu Wenruof4340622019-03-20 14:27:41 +0800173/*
174 * Submit bio from extent page data via submit_one_bio
175 *
176 * Return 0 if everything is OK.
177 * Return <0 for error.
178 */
179static int __must_check flush_write_bio(struct extent_page_data *epd)
Qu Wenruobb58eb92019-01-25 13:09:15 +0800180{
Qu Wenruof4340622019-03-20 14:27:41 +0800181 int ret = 0;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800182
Qu Wenruof4340622019-03-20 14:27:41 +0800183 if (epd->bio) {
Qu Wenruobb58eb92019-01-25 13:09:15 +0800184 ret = submit_one_bio(epd->bio, 0, 0);
Qu Wenruof4340622019-03-20 14:27:41 +0800185 /*
186 * Clean up of epd->bio is handled by its endio function.
187 * And endio is either triggered by successful bio execution
188 * or the error handler of submit bio hook.
189 * So at this point, no matter what happened, we don't need
190 * to clean up epd->bio.
191 */
Qu Wenruobb58eb92019-01-25 13:09:15 +0800192 epd->bio = NULL;
193 }
Qu Wenruof4340622019-03-20 14:27:41 +0800194 return ret;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800195}
David Sterbae2932ee2017-06-23 04:16:17 +0200196
Chris Masond1310b22008-01-24 16:13:08 -0500197int __init extent_io_init(void)
198{
David Sterba837e1972012-09-07 03:00:48 -0600199 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200200 sizeof(struct extent_state), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300201 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500202 if (!extent_state_cache)
203 return -ENOMEM;
204
David Sterba837e1972012-09-07 03:00:48 -0600205 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200206 sizeof(struct extent_buffer), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300207 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500208 if (!extent_buffer_cache)
209 goto free_state_cache;
Chris Mason9be33952013-05-17 18:30:14 -0400210
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400211 if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
212 offsetof(struct btrfs_io_bio, bio),
213 BIOSET_NEED_BVECS))
Chris Mason9be33952013-05-17 18:30:14 -0400214 goto free_buffer_cache;
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700215
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400216 if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE))
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700217 goto free_bioset;
218
Chris Masond1310b22008-01-24 16:13:08 -0500219 return 0;
220
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700221free_bioset:
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400222 bioset_exit(&btrfs_bioset);
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700223
Chris Mason9be33952013-05-17 18:30:14 -0400224free_buffer_cache:
225 kmem_cache_destroy(extent_buffer_cache);
226 extent_buffer_cache = NULL;
227
Chris Masond1310b22008-01-24 16:13:08 -0500228free_state_cache:
229 kmem_cache_destroy(extent_state_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400230 extent_state_cache = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500231 return -ENOMEM;
232}
233
David Sterbae67c7182018-02-19 17:24:18 +0100234void __cold extent_io_exit(void)
Chris Masond1310b22008-01-24 16:13:08 -0500235{
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000236 btrfs_leak_debug_check();
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000237
238 /*
239 * Make sure all delayed rcu free are flushed before we
240 * destroy caches.
241 */
242 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +0800243 kmem_cache_destroy(extent_state_cache);
244 kmem_cache_destroy(extent_buffer_cache);
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400245 bioset_exit(&btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -0500246}
247
Qu Wenruoc258d6e2019-03-01 10:47:58 +0800248void extent_io_tree_init(struct btrfs_fs_info *fs_info,
Qu Wenruo43eb5f22019-03-01 10:47:59 +0800249 struct extent_io_tree *tree, unsigned int owner,
250 void *private_data)
Chris Masond1310b22008-01-24 16:13:08 -0500251{
Qu Wenruoc258d6e2019-03-01 10:47:58 +0800252 tree->fs_info = fs_info;
Eric Paris6bef4d32010-02-23 19:43:04 +0000253 tree->state = RB_ROOT;
Chris Masond1310b22008-01-24 16:13:08 -0500254 tree->ops = NULL;
255 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500256 spin_lock_init(&tree->lock);
Josef Bacikc6100a42017-05-05 11:57:13 -0400257 tree->private_data = private_data;
Qu Wenruo43eb5f22019-03-01 10:47:59 +0800258 tree->owner = owner;
Chris Masond1310b22008-01-24 16:13:08 -0500259}
Chris Masond1310b22008-01-24 16:13:08 -0500260
Christoph Hellwigb2950862008-12-02 09:54:17 -0500261static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500262{
263 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500264
Michal Hocko3ba7ab22017-01-09 15:39:02 +0100265 /*
266 * The given mask might be not appropriate for the slab allocator,
267 * drop the unsupported bits
268 */
269 mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
Chris Masond1310b22008-01-24 16:13:08 -0500270 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400271 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500272 return state;
273 state->state = 0;
David Sterba47dc1962016-02-11 13:24:13 +0100274 state->failrec = NULL;
Filipe Manana27a35072014-07-06 20:09:59 +0100275 RB_CLEAR_NODE(&state->rb_node);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000276 btrfs_leak_debug_add(&state->leak_list, &states);
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200277 refcount_set(&state->refs, 1);
Chris Masond1310b22008-01-24 16:13:08 -0500278 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100279 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500280 return state;
281}
Chris Masond1310b22008-01-24 16:13:08 -0500282
Chris Mason4845e442010-05-25 20:56:50 -0400283void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500284{
Chris Masond1310b22008-01-24 16:13:08 -0500285 if (!state)
286 return;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200287 if (refcount_dec_and_test(&state->refs)) {
Filipe Manana27a35072014-07-06 20:09:59 +0100288 WARN_ON(extent_state_in_tree(state));
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000289 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100290 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500291 kmem_cache_free(extent_state_cache, state);
292 }
293}
Chris Masond1310b22008-01-24 16:13:08 -0500294
Filipe Mananaf2071b22014-02-12 15:05:53 +0000295static struct rb_node *tree_insert(struct rb_root *root,
296 struct rb_node *search_start,
297 u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000298 struct rb_node *node,
299 struct rb_node ***p_in,
300 struct rb_node **parent_in)
Chris Masond1310b22008-01-24 16:13:08 -0500301{
Filipe Mananaf2071b22014-02-12 15:05:53 +0000302 struct rb_node **p;
Chris Masond3977122009-01-05 21:25:51 -0500303 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500304 struct tree_entry *entry;
305
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000306 if (p_in && parent_in) {
307 p = *p_in;
308 parent = *parent_in;
309 goto do_insert;
310 }
311
Filipe Mananaf2071b22014-02-12 15:05:53 +0000312 p = search_start ? &search_start : &root->rb_node;
Chris Masond3977122009-01-05 21:25:51 -0500313 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500314 parent = *p;
315 entry = rb_entry(parent, struct tree_entry, rb_node);
316
317 if (offset < entry->start)
318 p = &(*p)->rb_left;
319 else if (offset > entry->end)
320 p = &(*p)->rb_right;
321 else
322 return parent;
323 }
324
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000325do_insert:
Chris Masond1310b22008-01-24 16:13:08 -0500326 rb_link_node(node, parent, p);
327 rb_insert_color(node, root);
328 return NULL;
329}
330
Chris Mason80ea96b2008-02-01 14:51:59 -0500331static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000332 struct rb_node **next_ret,
Nikolay Borisov352646c2019-01-30 16:51:00 +0200333 struct rb_node **prev_ret,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000334 struct rb_node ***p_ret,
335 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500336{
Chris Mason80ea96b2008-02-01 14:51:59 -0500337 struct rb_root *root = &tree->state;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000338 struct rb_node **n = &root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500339 struct rb_node *prev = NULL;
340 struct rb_node *orig_prev = NULL;
341 struct tree_entry *entry;
342 struct tree_entry *prev_entry = NULL;
343
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000344 while (*n) {
345 prev = *n;
346 entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500347 prev_entry = entry;
348
349 if (offset < entry->start)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000350 n = &(*n)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500351 else if (offset > entry->end)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000352 n = &(*n)->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500353 else
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000354 return *n;
Chris Masond1310b22008-01-24 16:13:08 -0500355 }
356
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000357 if (p_ret)
358 *p_ret = n;
359 if (parent_ret)
360 *parent_ret = prev;
361
Nikolay Borisov352646c2019-01-30 16:51:00 +0200362 if (next_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500363 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500364 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500365 prev = rb_next(prev);
366 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
367 }
Nikolay Borisov352646c2019-01-30 16:51:00 +0200368 *next_ret = prev;
Chris Masond1310b22008-01-24 16:13:08 -0500369 prev = orig_prev;
370 }
371
Nikolay Borisov352646c2019-01-30 16:51:00 +0200372 if (prev_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500373 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500374 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500375 prev = rb_prev(prev);
376 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
377 }
Nikolay Borisov352646c2019-01-30 16:51:00 +0200378 *prev_ret = prev;
Chris Masond1310b22008-01-24 16:13:08 -0500379 }
380 return NULL;
381}
382
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000383static inline struct rb_node *
384tree_search_for_insert(struct extent_io_tree *tree,
385 u64 offset,
386 struct rb_node ***p_ret,
387 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500388{
Nikolay Borisov352646c2019-01-30 16:51:00 +0200389 struct rb_node *next= NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500390 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500391
Nikolay Borisov352646c2019-01-30 16:51:00 +0200392 ret = __etree_search(tree, offset, &next, NULL, p_ret, parent_ret);
Chris Masond3977122009-01-05 21:25:51 -0500393 if (!ret)
Nikolay Borisov352646c2019-01-30 16:51:00 +0200394 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500395 return ret;
396}
397
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000398static inline struct rb_node *tree_search(struct extent_io_tree *tree,
399 u64 offset)
400{
401 return tree_search_for_insert(tree, offset, NULL, NULL);
402}
403
Chris Masond1310b22008-01-24 16:13:08 -0500404/*
405 * utility function to look for merge candidates inside a given range.
406 * Any extents with matching state are merged together into a single
407 * extent in the tree. Extents with EXTENT_IO in their state field
408 * are not merged because the end_io handlers need to be able to do
409 * operations on them without sleeping (or doing allocations/splits).
410 *
411 * This should be called with the tree lock held.
412 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000413static void merge_state(struct extent_io_tree *tree,
414 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500415{
416 struct extent_state *other;
417 struct rb_node *other_node;
418
Nikolay Borisov88826792019-03-14 15:28:31 +0200419 if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000420 return;
Chris Masond1310b22008-01-24 16:13:08 -0500421
422 other_node = rb_prev(&state->rb_node);
423 if (other_node) {
424 other = rb_entry(other_node, struct extent_state, rb_node);
425 if (other->end == state->start - 1 &&
426 other->state == state->state) {
Nikolay Borisov5c848192018-11-01 14:09:52 +0200427 if (tree->private_data &&
428 is_data_inode(tree->private_data))
429 btrfs_merge_delalloc_extent(tree->private_data,
430 state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500431 state->start = other->start;
Chris Masond1310b22008-01-24 16:13:08 -0500432 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100433 RB_CLEAR_NODE(&other->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500434 free_extent_state(other);
435 }
436 }
437 other_node = rb_next(&state->rb_node);
438 if (other_node) {
439 other = rb_entry(other_node, struct extent_state, rb_node);
440 if (other->start == state->end + 1 &&
441 other->state == state->state) {
Nikolay Borisov5c848192018-11-01 14:09:52 +0200442 if (tree->private_data &&
443 is_data_inode(tree->private_data))
444 btrfs_merge_delalloc_extent(tree->private_data,
445 state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400446 state->end = other->end;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400447 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100448 RB_CLEAR_NODE(&other->rb_node);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400449 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500450 }
451 }
Chris Masond1310b22008-01-24 16:13:08 -0500452}
453
Xiao Guangrong3150b692011-07-14 03:19:08 +0000454static void set_state_bits(struct extent_io_tree *tree,
Qu Wenruod38ed272015-10-12 14:53:37 +0800455 struct extent_state *state, unsigned *bits,
456 struct extent_changeset *changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000457
Chris Masond1310b22008-01-24 16:13:08 -0500458/*
459 * insert an extent_state struct into the tree. 'bits' are set on the
460 * struct before it is inserted.
461 *
462 * This may return -EEXIST if the extent is already there, in which case the
463 * state struct is freed.
464 *
465 * The tree lock is not taken internally. This is a utility function and
466 * probably isn't what you want to call (see set/clear_extent_bit).
467 */
468static int insert_state(struct extent_io_tree *tree,
469 struct extent_state *state, u64 start, u64 end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000470 struct rb_node ***p,
471 struct rb_node **parent,
Qu Wenruod38ed272015-10-12 14:53:37 +0800472 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500473{
474 struct rb_node *node;
475
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000476 if (end < start)
Frank Holtonefe120a2013-12-20 11:37:06 -0500477 WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200478 end, start);
Chris Masond1310b22008-01-24 16:13:08 -0500479 state->start = start;
480 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400481
Qu Wenruod38ed272015-10-12 14:53:37 +0800482 set_state_bits(tree, state, bits, changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000483
Filipe Mananaf2071b22014-02-12 15:05:53 +0000484 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
Chris Masond1310b22008-01-24 16:13:08 -0500485 if (node) {
486 struct extent_state *found;
487 found = rb_entry(node, struct extent_state, rb_node);
Jeff Mahoney62e85572016-09-20 10:05:01 -0400488 pr_err("BTRFS: found node %llu %llu on insert of %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200489 found->start, found->end, start, end);
Chris Masond1310b22008-01-24 16:13:08 -0500490 return -EEXIST;
491 }
492 merge_state(tree, state);
493 return 0;
494}
495
496/*
497 * split a given extent state struct in two, inserting the preallocated
498 * struct 'prealloc' as the newly created second half. 'split' indicates an
499 * offset inside 'orig' where it should be split.
500 *
501 * Before calling,
502 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
503 * are two extent state structs in the tree:
504 * prealloc: [orig->start, split - 1]
505 * orig: [ split, orig->end ]
506 *
507 * The tree locks are not taken by this function. They need to be held
508 * by the caller.
509 */
510static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
511 struct extent_state *prealloc, u64 split)
512{
513 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400514
Nikolay Borisovabbb55f2018-11-01 14:09:53 +0200515 if (tree->private_data && is_data_inode(tree->private_data))
516 btrfs_split_delalloc_extent(tree->private_data, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400517
Chris Masond1310b22008-01-24 16:13:08 -0500518 prealloc->start = orig->start;
519 prealloc->end = split - 1;
520 prealloc->state = orig->state;
521 orig->start = split;
522
Filipe Mananaf2071b22014-02-12 15:05:53 +0000523 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
524 &prealloc->rb_node, NULL, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500525 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500526 free_extent_state(prealloc);
527 return -EEXIST;
528 }
529 return 0;
530}
531
Li Zefancdc6a392012-03-12 16:39:48 +0800532static struct extent_state *next_state(struct extent_state *state)
533{
534 struct rb_node *next = rb_next(&state->rb_node);
535 if (next)
536 return rb_entry(next, struct extent_state, rb_node);
537 else
538 return NULL;
539}
540
Chris Masond1310b22008-01-24 16:13:08 -0500541/*
542 * utility function to clear some bits in an extent state struct.
Andrea Gelmini52042d82018-11-28 12:05:13 +0100543 * it will optionally wake up anyone waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500544 *
545 * If no bits are set on the state struct after clearing things, the
546 * struct is freed and removed from the tree
547 */
Li Zefancdc6a392012-03-12 16:39:48 +0800548static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
549 struct extent_state *state,
Qu Wenruofefdc552015-10-12 15:35:38 +0800550 unsigned *bits, int wake,
551 struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500552{
Li Zefancdc6a392012-03-12 16:39:48 +0800553 struct extent_state *next;
David Sterba9ee49a042015-01-14 19:52:13 +0100554 unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100555 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500556
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400557 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500558 u64 range = state->end - state->start + 1;
559 WARN_ON(range > tree->dirty_bytes);
560 tree->dirty_bytes -= range;
561 }
Nikolay Borisova36bb5f2018-11-01 14:09:51 +0200562
563 if (tree->private_data && is_data_inode(tree->private_data))
564 btrfs_clear_delalloc_extent(tree->private_data, state, bits);
565
David Sterba57599c72018-03-01 17:56:34 +0100566 ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
567 BUG_ON(ret < 0);
Josef Bacik32c00af2009-10-08 13:34:05 -0400568 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500569 if (wake)
570 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400571 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800572 next = next_state(state);
Filipe Manana27a35072014-07-06 20:09:59 +0100573 if (extent_state_in_tree(state)) {
Chris Masond1310b22008-01-24 16:13:08 -0500574 rb_erase(&state->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100575 RB_CLEAR_NODE(&state->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500576 free_extent_state(state);
577 } else {
578 WARN_ON(1);
579 }
580 } else {
581 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800582 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500583 }
Li Zefancdc6a392012-03-12 16:39:48 +0800584 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500585}
586
Xiao Guangrong82337672011-04-20 06:44:57 +0000587static struct extent_state *
588alloc_extent_state_atomic(struct extent_state *prealloc)
589{
590 if (!prealloc)
591 prealloc = alloc_extent_state(GFP_ATOMIC);
592
593 return prealloc;
594}
595
Eric Sandeen48a3b632013-04-25 20:41:01 +0000596static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400597{
David Sterba05912a32018-07-18 19:23:45 +0200598 struct inode *inode = tree->private_data;
599
600 btrfs_panic(btrfs_sb(inode->i_sb), err,
601 "locking error: extent tree was modified by another thread while locked");
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400602}
603
Chris Masond1310b22008-01-24 16:13:08 -0500604/*
605 * clear some bits on a range in the tree. This may require splitting
606 * or inserting elements in the tree, so the gfp mask is used to
607 * indicate which allocations or sleeping are allowed.
608 *
609 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
610 * the given range from the tree regardless of state (ie for truncate).
611 *
612 * the range [start, end] is inclusive.
613 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100614 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500615 */
David Sterba66b0c882017-10-31 16:30:47 +0100616int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Qu Wenruofefdc552015-10-12 15:35:38 +0800617 unsigned bits, int wake, int delete,
618 struct extent_state **cached_state,
619 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500620{
621 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400622 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500623 struct extent_state *prealloc = NULL;
624 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400625 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500626 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000627 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500628
Josef Bacika5dee372013-12-13 10:02:44 -0500629 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +0800630 trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits);
David Sterba8d599ae2013-04-30 15:22:23 +0000631
Josef Bacik7ee9e442013-06-21 16:37:03 -0400632 if (bits & EXTENT_DELALLOC)
633 bits |= EXTENT_NORESERVE;
634
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400635 if (delete)
636 bits |= ~EXTENT_CTLBITS;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400637
Nikolay Borisov88826792019-03-14 15:28:31 +0200638 if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY))
Josef Bacik2ac55d42010-02-03 19:33:23 +0000639 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500640again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800641 if (!prealloc && gfpflags_allow_blocking(mask)) {
Filipe Mananac7bc6312014-11-03 14:12:57 +0000642 /*
643 * Don't care for allocation failure here because we might end
644 * up not needing the pre-allocated extent state at all, which
645 * is the case if we only have in the tree extent states that
646 * cover our input range and don't cover too any other range.
647 * If we end up needing a new extent state we allocate it later.
648 */
Chris Masond1310b22008-01-24 16:13:08 -0500649 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500650 }
651
Chris Masoncad321a2008-12-17 14:51:42 -0500652 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400653 if (cached_state) {
654 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000655
656 if (clear) {
657 *cached_state = NULL;
658 cached_state = NULL;
659 }
660
Filipe Manana27a35072014-07-06 20:09:59 +0100661 if (cached && extent_state_in_tree(cached) &&
662 cached->start <= start && cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000663 if (clear)
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200664 refcount_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400665 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400666 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400667 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000668 if (clear)
669 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400670 }
Chris Masond1310b22008-01-24 16:13:08 -0500671 /*
672 * this search will find the extents that end after
673 * our range starts
674 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500675 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500676 if (!node)
677 goto out;
678 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400679hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500680 if (state->start > end)
681 goto out;
682 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400683 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500684
Liu Bo04493142012-02-16 18:34:37 +0800685 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800686 if (!(state->state & bits)) {
687 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800688 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800689 }
Liu Bo04493142012-02-16 18:34:37 +0800690
Chris Masond1310b22008-01-24 16:13:08 -0500691 /*
692 * | ---- desired range ---- |
693 * | state | or
694 * | ------------- state -------------- |
695 *
696 * We need to split the extent we found, and may flip
697 * bits on second half.
698 *
699 * If the extent we found extends past our range, we
700 * just split and search again. It'll get split again
701 * the next time though.
702 *
703 * If the extent we found is inside our range, we clear
704 * the desired bit on it.
705 */
706
707 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000708 prealloc = alloc_extent_state_atomic(prealloc);
709 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500710 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400711 if (err)
712 extent_io_tree_panic(tree, err);
713
Chris Masond1310b22008-01-24 16:13:08 -0500714 prealloc = NULL;
715 if (err)
716 goto out;
717 if (state->end <= end) {
Qu Wenruofefdc552015-10-12 15:35:38 +0800718 state = clear_state_bit(tree, state, &bits, wake,
719 changeset);
Liu Bod1ac6e42012-05-10 18:10:39 +0800720 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500721 }
722 goto search_again;
723 }
724 /*
725 * | ---- desired range ---- |
726 * | state |
727 * We need to split the extent, and clear the bit
728 * on the first half
729 */
730 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000731 prealloc = alloc_extent_state_atomic(prealloc);
732 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500733 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400734 if (err)
735 extent_io_tree_panic(tree, err);
736
Chris Masond1310b22008-01-24 16:13:08 -0500737 if (wake)
738 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400739
Qu Wenruofefdc552015-10-12 15:35:38 +0800740 clear_state_bit(tree, prealloc, &bits, wake, changeset);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400741
Chris Masond1310b22008-01-24 16:13:08 -0500742 prealloc = NULL;
743 goto out;
744 }
Chris Mason42daec22009-09-23 19:51:09 -0400745
Qu Wenruofefdc552015-10-12 15:35:38 +0800746 state = clear_state_bit(tree, state, &bits, wake, changeset);
Liu Bo04493142012-02-16 18:34:37 +0800747next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400748 if (last_end == (u64)-1)
749 goto out;
750 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800751 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800752 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500753
754search_again:
755 if (start > end)
756 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500757 spin_unlock(&tree->lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800758 if (gfpflags_allow_blocking(mask))
Chris Masond1310b22008-01-24 16:13:08 -0500759 cond_resched();
760 goto again;
David Sterba7ab5cb22016-04-27 01:02:15 +0200761
762out:
763 spin_unlock(&tree->lock);
764 if (prealloc)
765 free_extent_state(prealloc);
766
767 return 0;
768
Chris Masond1310b22008-01-24 16:13:08 -0500769}
Chris Masond1310b22008-01-24 16:13:08 -0500770
Jeff Mahoney143bede2012-03-01 14:56:26 +0100771static void wait_on_state(struct extent_io_tree *tree,
772 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500773 __releases(tree->lock)
774 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500775{
776 DEFINE_WAIT(wait);
777 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500778 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500779 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500780 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500781 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500782}
783
784/*
785 * waits for one or more bits to clear on a range in the state tree.
786 * The range [start, end] is inclusive.
787 * The tree lock is taken by this function
788 */
David Sterba41074882013-04-29 13:38:46 +0000789static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
790 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500791{
792 struct extent_state *state;
793 struct rb_node *node;
794
Josef Bacika5dee372013-12-13 10:02:44 -0500795 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000796
Chris Masoncad321a2008-12-17 14:51:42 -0500797 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500798again:
799 while (1) {
800 /*
801 * this search will find all the extents that end after
802 * our range starts
803 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500804 node = tree_search(tree, start);
Filipe Mananac50d3e72014-03-31 14:53:25 +0100805process_node:
Chris Masond1310b22008-01-24 16:13:08 -0500806 if (!node)
807 break;
808
809 state = rb_entry(node, struct extent_state, rb_node);
810
811 if (state->start > end)
812 goto out;
813
814 if (state->state & bits) {
815 start = state->start;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200816 refcount_inc(&state->refs);
Chris Masond1310b22008-01-24 16:13:08 -0500817 wait_on_state(tree, state);
818 free_extent_state(state);
819 goto again;
820 }
821 start = state->end + 1;
822
823 if (start > end)
824 break;
825
Filipe Mananac50d3e72014-03-31 14:53:25 +0100826 if (!cond_resched_lock(&tree->lock)) {
827 node = rb_next(node);
828 goto process_node;
829 }
Chris Masond1310b22008-01-24 16:13:08 -0500830 }
831out:
Chris Masoncad321a2008-12-17 14:51:42 -0500832 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500833}
Chris Masond1310b22008-01-24 16:13:08 -0500834
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000835static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500836 struct extent_state *state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800837 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500838{
David Sterba9ee49a042015-01-14 19:52:13 +0100839 unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100840 int ret;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400841
Nikolay Borisove06a1fc2018-11-01 14:09:50 +0200842 if (tree->private_data && is_data_inode(tree->private_data))
843 btrfs_set_delalloc_extent(tree->private_data, state, bits);
844
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400845 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500846 u64 range = state->end - state->start + 1;
847 tree->dirty_bytes += range;
848 }
David Sterba57599c72018-03-01 17:56:34 +0100849 ret = add_extent_changeset(state, bits_to_set, changeset, 1);
850 BUG_ON(ret < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400851 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500852}
853
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100854static void cache_state_if_flags(struct extent_state *state,
855 struct extent_state **cached_ptr,
David Sterba9ee49a042015-01-14 19:52:13 +0100856 unsigned flags)
Chris Mason2c64c532009-09-02 15:04:12 -0400857{
858 if (cached_ptr && !(*cached_ptr)) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100859 if (!flags || (state->state & flags)) {
Chris Mason2c64c532009-09-02 15:04:12 -0400860 *cached_ptr = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200861 refcount_inc(&state->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400862 }
863 }
864}
865
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100866static void cache_state(struct extent_state *state,
867 struct extent_state **cached_ptr)
868{
869 return cache_state_if_flags(state, cached_ptr,
Nikolay Borisov88826792019-03-14 15:28:31 +0200870 EXTENT_LOCKED | EXTENT_BOUNDARY);
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100871}
872
Chris Masond1310b22008-01-24 16:13:08 -0500873/*
Chris Mason1edbb732009-09-02 13:24:36 -0400874 * set some bits on a range in the tree. This may require allocations or
875 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500876 *
Chris Mason1edbb732009-09-02 13:24:36 -0400877 * If any of the exclusive bits are set, this will fail with -EEXIST if some
878 * part of the range already has the desired bits set. The start of the
879 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500880 *
Chris Mason1edbb732009-09-02 13:24:36 -0400881 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500882 */
Chris Mason1edbb732009-09-02 13:24:36 -0400883
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100884static int __must_check
885__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +0100886 unsigned bits, unsigned exclusive_bits,
David Sterba41074882013-04-29 13:38:46 +0000887 u64 *failed_start, struct extent_state **cached_state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800888 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500889{
890 struct extent_state *state;
891 struct extent_state *prealloc = NULL;
892 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000893 struct rb_node **p;
894 struct rb_node *parent;
Chris Masond1310b22008-01-24 16:13:08 -0500895 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500896 u64 last_start;
897 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400898
Josef Bacika5dee372013-12-13 10:02:44 -0500899 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +0800900 trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits);
David Sterba8d599ae2013-04-30 15:22:23 +0000901
Chris Masond1310b22008-01-24 16:13:08 -0500902again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800903 if (!prealloc && gfpflags_allow_blocking(mask)) {
David Sterba059f7912016-04-27 01:03:45 +0200904 /*
905 * Don't care for allocation failure here because we might end
906 * up not needing the pre-allocated extent state at all, which
907 * is the case if we only have in the tree extent states that
908 * cover our input range and don't cover too any other range.
909 * If we end up needing a new extent state we allocate it later.
910 */
Chris Masond1310b22008-01-24 16:13:08 -0500911 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500912 }
913
Chris Masoncad321a2008-12-17 14:51:42 -0500914 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400915 if (cached_state && *cached_state) {
916 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400917 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +0100918 extent_state_in_tree(state)) {
Chris Mason9655d292009-09-02 15:22:30 -0400919 node = &state->rb_node;
920 goto hit_next;
921 }
922 }
Chris Masond1310b22008-01-24 16:13:08 -0500923 /*
924 * this search will find all the extents that end after
925 * our range starts.
926 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000927 node = tree_search_for_insert(tree, start, &p, &parent);
Chris Masond1310b22008-01-24 16:13:08 -0500928 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000929 prealloc = alloc_extent_state_atomic(prealloc);
930 BUG_ON(!prealloc);
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000931 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800932 &p, &parent, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400933 if (err)
934 extent_io_tree_panic(tree, err);
935
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +0000936 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500937 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500938 goto out;
939 }
Chris Masond1310b22008-01-24 16:13:08 -0500940 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400941hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500942 last_start = state->start;
943 last_end = state->end;
944
945 /*
946 * | ---- desired range ---- |
947 * | state |
948 *
949 * Just lock what we found and keep going
950 */
951 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400952 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500953 *failed_start = state->start;
954 err = -EEXIST;
955 goto out;
956 }
Chris Mason42daec22009-09-23 19:51:09 -0400957
Qu Wenruod38ed272015-10-12 14:53:37 +0800958 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400959 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500960 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400961 if (last_end == (u64)-1)
962 goto out;
963 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800964 state = next_state(state);
965 if (start < end && state && state->start == start &&
966 !need_resched())
967 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500968 goto search_again;
969 }
970
971 /*
972 * | ---- desired range ---- |
973 * | state |
974 * or
975 * | ------------- state -------------- |
976 *
977 * We need to split the extent we found, and may flip bits on
978 * second half.
979 *
980 * If the extent we found extends past our
981 * range, we just split and search again. It'll get split
982 * again the next time though.
983 *
984 * If the extent we found is inside our range, we set the
985 * desired bit on it.
986 */
987 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400988 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500989 *failed_start = start;
990 err = -EEXIST;
991 goto out;
992 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000993
994 prealloc = alloc_extent_state_atomic(prealloc);
995 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500996 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400997 if (err)
998 extent_io_tree_panic(tree, err);
999
Chris Masond1310b22008-01-24 16:13:08 -05001000 prealloc = NULL;
1001 if (err)
1002 goto out;
1003 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001004 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001005 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001006 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -04001007 if (last_end == (u64)-1)
1008 goto out;
1009 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001010 state = next_state(state);
1011 if (start < end && state && state->start == start &&
1012 !need_resched())
1013 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -05001014 }
1015 goto search_again;
1016 }
1017 /*
1018 * | ---- desired range ---- |
1019 * | state | or | state |
1020 *
1021 * There's a hole, we need to insert something in it and
1022 * ignore the extent we found.
1023 */
1024 if (state->start > start) {
1025 u64 this_end;
1026 if (end < last_start)
1027 this_end = end;
1028 else
Chris Masond3977122009-01-05 21:25:51 -05001029 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +00001030
1031 prealloc = alloc_extent_state_atomic(prealloc);
1032 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +00001033
1034 /*
1035 * Avoid to free 'prealloc' if it can be merged with
1036 * the later extent.
1037 */
Chris Masond1310b22008-01-24 16:13:08 -05001038 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001039 NULL, NULL, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001040 if (err)
1041 extent_io_tree_panic(tree, err);
1042
Chris Mason2c64c532009-09-02 15:04:12 -04001043 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001044 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001045 start = this_end + 1;
1046 goto search_again;
1047 }
1048 /*
1049 * | ---- desired range ---- |
1050 * | state |
1051 * We need to split the extent, and set the bit
1052 * on the first half
1053 */
1054 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -04001055 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001056 *failed_start = start;
1057 err = -EEXIST;
1058 goto out;
1059 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001060
1061 prealloc = alloc_extent_state_atomic(prealloc);
1062 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001063 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001064 if (err)
1065 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -05001066
Qu Wenruod38ed272015-10-12 14:53:37 +08001067 set_state_bits(tree, prealloc, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001068 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001069 merge_state(tree, prealloc);
1070 prealloc = NULL;
1071 goto out;
1072 }
1073
David Sterbab5a4ba142016-04-27 01:02:15 +02001074search_again:
1075 if (start > end)
1076 goto out;
1077 spin_unlock(&tree->lock);
1078 if (gfpflags_allow_blocking(mask))
1079 cond_resched();
1080 goto again;
Chris Masond1310b22008-01-24 16:13:08 -05001081
1082out:
Chris Masoncad321a2008-12-17 14:51:42 -05001083 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001084 if (prealloc)
1085 free_extent_state(prealloc);
1086
1087 return err;
1088
Chris Masond1310b22008-01-24 16:13:08 -05001089}
Chris Masond1310b22008-01-24 16:13:08 -05001090
David Sterba41074882013-04-29 13:38:46 +00001091int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001092 unsigned bits, u64 * failed_start,
David Sterba41074882013-04-29 13:38:46 +00001093 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001094{
1095 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001096 cached_state, mask, NULL);
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001097}
1098
1099
Josef Bacik462d6fa2011-09-26 13:56:12 -04001100/**
Liu Bo10983f22012-07-11 15:26:19 +08001101 * convert_extent_bit - convert all bits in a given range from one bit to
1102 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -04001103 * @tree: the io tree to search
1104 * @start: the start offset in bytes
1105 * @end: the end offset in bytes (inclusive)
1106 * @bits: the bits to set in this range
1107 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001108 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001109 *
1110 * This will go through and set bits for the given range. If any states exist
1111 * already in this range they are set with the given bit and cleared of the
1112 * clear_bits. This is only meant to be used by things that are mergeable, ie
1113 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1114 * boundary bits like LOCK.
David Sterba210aa272016-04-26 23:54:39 +02001115 *
1116 * All allocations are done with GFP_NOFS.
Josef Bacik462d6fa2011-09-26 13:56:12 -04001117 */
1118int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001119 unsigned bits, unsigned clear_bits,
David Sterba210aa272016-04-26 23:54:39 +02001120 struct extent_state **cached_state)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001121{
1122 struct extent_state *state;
1123 struct extent_state *prealloc = NULL;
1124 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001125 struct rb_node **p;
1126 struct rb_node *parent;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001127 int err = 0;
1128 u64 last_start;
1129 u64 last_end;
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001130 bool first_iteration = true;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001131
Josef Bacika5dee372013-12-13 10:02:44 -05001132 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +08001133 trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits,
1134 clear_bits);
David Sterba8d599ae2013-04-30 15:22:23 +00001135
Josef Bacik462d6fa2011-09-26 13:56:12 -04001136again:
David Sterba210aa272016-04-26 23:54:39 +02001137 if (!prealloc) {
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001138 /*
1139 * Best effort, don't worry if extent state allocation fails
1140 * here for the first iteration. We might have a cached state
1141 * that matches exactly the target range, in which case no
1142 * extent state allocations are needed. We'll only know this
1143 * after locking the tree.
1144 */
David Sterba210aa272016-04-26 23:54:39 +02001145 prealloc = alloc_extent_state(GFP_NOFS);
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001146 if (!prealloc && !first_iteration)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001147 return -ENOMEM;
1148 }
1149
1150 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001151 if (cached_state && *cached_state) {
1152 state = *cached_state;
1153 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +01001154 extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001155 node = &state->rb_node;
1156 goto hit_next;
1157 }
1158 }
1159
Josef Bacik462d6fa2011-09-26 13:56:12 -04001160 /*
1161 * this search will find all the extents that end after
1162 * our range starts.
1163 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001164 node = tree_search_for_insert(tree, start, &p, &parent);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001165 if (!node) {
1166 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001167 if (!prealloc) {
1168 err = -ENOMEM;
1169 goto out;
1170 }
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001171 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001172 &p, &parent, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001173 if (err)
1174 extent_io_tree_panic(tree, err);
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001175 cache_state(prealloc, cached_state);
1176 prealloc = NULL;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001177 goto out;
1178 }
1179 state = rb_entry(node, struct extent_state, rb_node);
1180hit_next:
1181 last_start = state->start;
1182 last_end = state->end;
1183
1184 /*
1185 * | ---- desired range ---- |
1186 * | state |
1187 *
1188 * Just lock what we found and keep going
1189 */
1190 if (state->start == start && state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001191 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001192 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001193 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001194 if (last_end == (u64)-1)
1195 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001196 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001197 if (start < end && state && state->start == start &&
1198 !need_resched())
1199 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001200 goto search_again;
1201 }
1202
1203 /*
1204 * | ---- desired range ---- |
1205 * | state |
1206 * or
1207 * | ------------- state -------------- |
1208 *
1209 * We need to split the extent we found, and may flip bits on
1210 * second half.
1211 *
1212 * If the extent we found extends past our
1213 * range, we just split and search again. It'll get split
1214 * again the next time though.
1215 *
1216 * If the extent we found is inside our range, we set the
1217 * desired bit on it.
1218 */
1219 if (state->start < start) {
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 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001226 if (err)
1227 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001228 prealloc = NULL;
1229 if (err)
1230 goto out;
1231 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001232 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001233 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001234 state = clear_state_bit(tree, state, &clear_bits, 0,
1235 NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001236 if (last_end == (u64)-1)
1237 goto out;
1238 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001239 if (start < end && state && state->start == start &&
1240 !need_resched())
1241 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001242 }
1243 goto search_again;
1244 }
1245 /*
1246 * | ---- desired range ---- |
1247 * | state | or | state |
1248 *
1249 * There's a hole, we need to insert something in it and
1250 * ignore the extent we found.
1251 */
1252 if (state->start > start) {
1253 u64 this_end;
1254 if (end < last_start)
1255 this_end = end;
1256 else
1257 this_end = last_start - 1;
1258
1259 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001260 if (!prealloc) {
1261 err = -ENOMEM;
1262 goto out;
1263 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001264
1265 /*
1266 * Avoid to free 'prealloc' if it can be merged with
1267 * the later extent.
1268 */
1269 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001270 NULL, NULL, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001271 if (err)
1272 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001273 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001274 prealloc = NULL;
1275 start = this_end + 1;
1276 goto search_again;
1277 }
1278 /*
1279 * | ---- desired range ---- |
1280 * | state |
1281 * We need to split the extent, and set the bit
1282 * on the first half
1283 */
1284 if (state->start <= end && state->end > end) {
1285 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001286 if (!prealloc) {
1287 err = -ENOMEM;
1288 goto out;
1289 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001290
1291 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001292 if (err)
1293 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001294
Qu Wenruod38ed272015-10-12 14:53:37 +08001295 set_state_bits(tree, prealloc, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001296 cache_state(prealloc, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001297 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001298 prealloc = NULL;
1299 goto out;
1300 }
1301
Josef Bacik462d6fa2011-09-26 13:56:12 -04001302search_again:
1303 if (start > end)
1304 goto out;
1305 spin_unlock(&tree->lock);
David Sterba210aa272016-04-26 23:54:39 +02001306 cond_resched();
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001307 first_iteration = false;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001308 goto again;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001309
1310out:
1311 spin_unlock(&tree->lock);
1312 if (prealloc)
1313 free_extent_state(prealloc);
1314
1315 return err;
1316}
1317
Chris Masond1310b22008-01-24 16:13:08 -05001318/* wrappers around set/clear extent bit */
Qu Wenruod38ed272015-10-12 14:53:37 +08001319int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba2c53b912016-04-26 23:54:39 +02001320 unsigned bits, struct extent_changeset *changeset)
Qu Wenruod38ed272015-10-12 14:53:37 +08001321{
1322 /*
1323 * We don't support EXTENT_LOCKED yet, as current changeset will
1324 * record any bits changed, so for EXTENT_LOCKED case, it will
1325 * either fail with -EEXIST or changeset will record the whole
1326 * range.
1327 */
1328 BUG_ON(bits & EXTENT_LOCKED);
1329
David Sterba2c53b912016-04-26 23:54:39 +02001330 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
Qu Wenruod38ed272015-10-12 14:53:37 +08001331 changeset);
1332}
1333
Qu Wenruofefdc552015-10-12 15:35:38 +08001334int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1335 unsigned bits, int wake, int delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001336 struct extent_state **cached)
Qu Wenruofefdc552015-10-12 15:35:38 +08001337{
1338 return __clear_extent_bit(tree, start, end, bits, wake, delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001339 cached, GFP_NOFS, NULL);
Qu Wenruofefdc552015-10-12 15:35:38 +08001340}
1341
Qu Wenruofefdc552015-10-12 15:35:38 +08001342int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaf734c442016-04-26 23:54:39 +02001343 unsigned bits, struct extent_changeset *changeset)
Qu Wenruofefdc552015-10-12 15:35:38 +08001344{
1345 /*
1346 * Don't support EXTENT_LOCKED case, same reason as
1347 * set_record_extent_bits().
1348 */
1349 BUG_ON(bits & EXTENT_LOCKED);
1350
David Sterbaf734c442016-04-26 23:54:39 +02001351 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
Qu Wenruofefdc552015-10-12 15:35:38 +08001352 changeset);
1353}
1354
Chris Masond352ac62008-09-29 15:18:18 -04001355/*
1356 * either insert or lock state struct between start and end use mask to tell
1357 * us if waiting is desired.
1358 */
Chris Mason1edbb732009-09-02 13:24:36 -04001359int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaff13db42015-12-03 14:30:40 +01001360 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001361{
1362 int err;
1363 u64 failed_start;
David Sterba9ee49a042015-01-14 19:52:13 +01001364
Chris Masond1310b22008-01-24 16:13:08 -05001365 while (1) {
David Sterbaff13db42015-12-03 14:30:40 +01001366 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001367 EXTENT_LOCKED, &failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001368 cached_state, GFP_NOFS, NULL);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001369 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001370 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1371 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001372 } else
Chris Masond1310b22008-01-24 16:13:08 -05001373 break;
Chris Masond1310b22008-01-24 16:13:08 -05001374 WARN_ON(start > end);
1375 }
1376 return err;
1377}
Chris Masond1310b22008-01-24 16:13:08 -05001378
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001379int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001380{
1381 int err;
1382 u64 failed_start;
1383
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001384 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
Qu Wenruod38ed272015-10-12 14:53:37 +08001385 &failed_start, NULL, GFP_NOFS, NULL);
Yan Zheng66435582008-10-30 14:19:50 -04001386 if (err == -EEXIST) {
1387 if (failed_start > start)
1388 clear_extent_bit(tree, start, failed_start - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001389 EXTENT_LOCKED, 1, 0, NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001390 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001391 }
Josef Bacik25179202008-10-29 14:49:05 -04001392 return 1;
1393}
Josef Bacik25179202008-10-29 14:49:05 -04001394
David Sterbabd1fa4f2015-12-03 13:08:59 +01001395void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001396{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001397 unsigned long index = start >> PAGE_SHIFT;
1398 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001399 struct page *page;
1400
1401 while (index <= end_index) {
1402 page = find_get_page(inode->i_mapping, index);
1403 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1404 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001405 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001406 index++;
1407 }
Chris Mason4adaa612013-03-26 13:07:00 -04001408}
1409
David Sterbaf6311572015-12-03 13:08:59 +01001410void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001411{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001412 unsigned long index = start >> PAGE_SHIFT;
1413 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001414 struct page *page;
1415
1416 while (index <= end_index) {
1417 page = find_get_page(inode->i_mapping, index);
1418 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Mason4adaa612013-03-26 13:07:00 -04001419 __set_page_dirty_nobuffers(page);
Konstantin Khebnikov8d386332015-02-11 15:26:55 -08001420 account_page_redirty(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001421 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001422 index++;
1423 }
Chris Mason4adaa612013-03-26 13:07:00 -04001424}
1425
Chris Masond352ac62008-09-29 15:18:18 -04001426/* find the first state struct with 'bits' set after 'start', and
1427 * return it. tree->lock must be held. NULL will returned if
1428 * nothing was found after 'start'
1429 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001430static struct extent_state *
1431find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +01001432 u64 start, unsigned bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001433{
1434 struct rb_node *node;
1435 struct extent_state *state;
1436
1437 /*
1438 * this search will find all the extents that end after
1439 * our range starts.
1440 */
1441 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001442 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001443 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001444
Chris Masond3977122009-01-05 21:25:51 -05001445 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001446 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001447 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001448 return state;
Chris Masond3977122009-01-05 21:25:51 -05001449
Chris Masond7fc6402008-02-18 12:12:38 -05001450 node = rb_next(node);
1451 if (!node)
1452 break;
1453 }
1454out:
1455 return NULL;
1456}
Chris Masond7fc6402008-02-18 12:12:38 -05001457
Chris Masond352ac62008-09-29 15:18:18 -04001458/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001459 * find the first offset in the io tree with 'bits' set. zero is
1460 * returned if we find something, and *start_ret and *end_ret are
1461 * set to reflect the state struct that was found.
1462 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001463 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001464 */
1465int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba9ee49a042015-01-14 19:52:13 +01001466 u64 *start_ret, u64 *end_ret, unsigned bits,
Josef Bacike6138872012-09-27 17:07:30 -04001467 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001468{
1469 struct extent_state *state;
1470 int ret = 1;
1471
1472 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001473 if (cached_state && *cached_state) {
1474 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001475 if (state->end == start - 1 && extent_state_in_tree(state)) {
Liu Bo9688e9a2018-08-23 03:14:53 +08001476 while ((state = next_state(state)) != NULL) {
Josef Bacike6138872012-09-27 17:07:30 -04001477 if (state->state & bits)
1478 goto got_it;
Josef Bacike6138872012-09-27 17:07:30 -04001479 }
1480 free_extent_state(*cached_state);
1481 *cached_state = NULL;
1482 goto out;
1483 }
1484 free_extent_state(*cached_state);
1485 *cached_state = NULL;
1486 }
1487
Xiao Guangrong69261c42011-07-14 03:19:45 +00001488 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001489got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001490 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001491 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001492 *start_ret = state->start;
1493 *end_ret = state->end;
1494 ret = 0;
1495 }
Josef Bacike6138872012-09-27 17:07:30 -04001496out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001497 spin_unlock(&tree->lock);
1498 return ret;
1499}
1500
1501/*
Chris Masond352ac62008-09-29 15:18:18 -04001502 * find a contiguous range of bytes in the file marked as delalloc, not
1503 * more than 'max_bytes'. start and end are used to return the range,
1504 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001505 * true is returned if we find something, false if nothing was in the tree
Chris Masond352ac62008-09-29 15:18:18 -04001506 */
Lu Fengqi3522e902018-11-29 11:33:38 +08001507static noinline bool find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001508 u64 *start, u64 *end, u64 max_bytes,
1509 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001510{
1511 struct rb_node *node;
1512 struct extent_state *state;
1513 u64 cur_start = *start;
Lu Fengqi3522e902018-11-29 11:33:38 +08001514 bool found = false;
Chris Masond1310b22008-01-24 16:13:08 -05001515 u64 total_bytes = 0;
1516
Chris Masoncad321a2008-12-17 14:51:42 -05001517 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001518
Chris Masond1310b22008-01-24 16:13:08 -05001519 /*
1520 * this search will find all the extents that end after
1521 * our range starts.
1522 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001523 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001524 if (!node) {
Lu Fengqi3522e902018-11-29 11:33:38 +08001525 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001526 goto out;
1527 }
1528
Chris Masond3977122009-01-05 21:25:51 -05001529 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001530 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001531 if (found && (state->start != cur_start ||
1532 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001533 goto out;
1534 }
1535 if (!(state->state & EXTENT_DELALLOC)) {
1536 if (!found)
1537 *end = state->end;
1538 goto out;
1539 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001540 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001541 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001542 *cached_state = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +02001543 refcount_inc(&state->refs);
Josef Bacikc2a128d2010-02-02 21:19:11 +00001544 }
Lu Fengqi3522e902018-11-29 11:33:38 +08001545 found = true;
Chris Masond1310b22008-01-24 16:13:08 -05001546 *end = state->end;
1547 cur_start = state->end + 1;
1548 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001549 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001550 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001551 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001552 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001553 break;
1554 }
1555out:
Chris Masoncad321a2008-12-17 14:51:42 -05001556 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001557 return found;
1558}
1559
Liu Boda2c7002017-02-10 16:41:05 +01001560static int __process_pages_contig(struct address_space *mapping,
1561 struct page *locked_page,
1562 pgoff_t start_index, pgoff_t end_index,
1563 unsigned long page_ops, pgoff_t *index_ret);
1564
Jeff Mahoney143bede2012-03-01 14:56:26 +01001565static noinline void __unlock_for_delalloc(struct inode *inode,
1566 struct page *locked_page,
1567 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001568{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001569 unsigned long index = start >> PAGE_SHIFT;
1570 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001571
Liu Bo76c00212017-02-10 16:42:14 +01001572 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001573 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001574 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001575
Liu Bo76c00212017-02-10 16:42:14 +01001576 __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1577 PAGE_UNLOCK, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -04001578}
1579
1580static noinline int lock_delalloc_pages(struct inode *inode,
1581 struct page *locked_page,
1582 u64 delalloc_start,
1583 u64 delalloc_end)
1584{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001585 unsigned long index = delalloc_start >> PAGE_SHIFT;
Liu Bo76c00212017-02-10 16:42:14 +01001586 unsigned long index_ret = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001587 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001588 int ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001589
Liu Bo76c00212017-02-10 16:42:14 +01001590 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001591 if (index == locked_page->index && index == end_index)
1592 return 0;
1593
Liu Bo76c00212017-02-10 16:42:14 +01001594 ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1595 end_index, PAGE_LOCK, &index_ret);
1596 if (ret == -EAGAIN)
1597 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1598 (u64)index_ret << PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001599 return ret;
1600}
1601
1602/*
Lu Fengqi3522e902018-11-29 11:33:38 +08001603 * Find and lock a contiguous range of bytes in the file marked as delalloc, no
1604 * more than @max_bytes. @Start and @end are used to return the range,
Chris Masonc8b97812008-10-29 14:49:59 -04001605 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001606 * Return: true if we find something
1607 * false if nothing was in the tree
Chris Masonc8b97812008-10-29 14:49:59 -04001608 */
Johannes Thumshirnce9f9672018-11-19 10:38:17 +01001609EXPORT_FOR_TESTS
Lu Fengqi3522e902018-11-29 11:33:38 +08001610noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
Josef Bacik294e30f2013-10-09 12:00:56 -04001611 struct extent_io_tree *tree,
1612 struct page *locked_page, u64 *start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03001613 u64 *end)
Chris Masonc8b97812008-10-29 14:49:59 -04001614{
Nikolay Borisov917aace2018-10-26 14:43:20 +03001615 u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001616 u64 delalloc_start;
1617 u64 delalloc_end;
Lu Fengqi3522e902018-11-29 11:33:38 +08001618 bool found;
Chris Mason9655d292009-09-02 15:22:30 -04001619 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001620 int ret;
1621 int loops = 0;
1622
1623again:
1624 /* step one, find a bunch of delalloc bytes starting at start */
1625 delalloc_start = *start;
1626 delalloc_end = 0;
1627 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001628 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001629 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001630 *start = delalloc_start;
1631 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001632 free_extent_state(cached_state);
Lu Fengqi3522e902018-11-29 11:33:38 +08001633 return false;
Chris Masonc8b97812008-10-29 14:49:59 -04001634 }
1635
1636 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001637 * start comes from the offset of locked_page. We have to lock
1638 * pages in order, so we can't process delalloc bytes before
1639 * locked_page
1640 */
Chris Masond3977122009-01-05 21:25:51 -05001641 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001642 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001643
1644 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001645 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001646 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001647 if (delalloc_end + 1 - delalloc_start > max_bytes)
1648 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001649
Chris Masonc8b97812008-10-29 14:49:59 -04001650 /* step two, lock all the pages after the page that has start */
1651 ret = lock_delalloc_pages(inode, locked_page,
1652 delalloc_start, delalloc_end);
Nikolay Borisov9bfd61d2018-10-26 14:43:21 +03001653 ASSERT(!ret || ret == -EAGAIN);
Chris Masonc8b97812008-10-29 14:49:59 -04001654 if (ret == -EAGAIN) {
1655 /* some of the pages are gone, lets avoid looping by
1656 * shortening the size of the delalloc range we're searching
1657 */
Chris Mason9655d292009-09-02 15:22:30 -04001658 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001659 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001660 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001661 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001662 loops = 1;
1663 goto again;
1664 } else {
Lu Fengqi3522e902018-11-29 11:33:38 +08001665 found = false;
Chris Masonc8b97812008-10-29 14:49:59 -04001666 goto out_failed;
1667 }
1668 }
Chris Masonc8b97812008-10-29 14:49:59 -04001669
1670 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001671 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001672
1673 /* then test to make sure it is all still delalloc */
1674 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001675 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001676 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001677 unlock_extent_cached(tree, delalloc_start, delalloc_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001678 &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001679 __unlock_for_delalloc(inode, locked_page,
1680 delalloc_start, delalloc_end);
1681 cond_resched();
1682 goto again;
1683 }
Chris Mason9655d292009-09-02 15:22:30 -04001684 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001685 *start = delalloc_start;
1686 *end = delalloc_end;
1687out_failed:
1688 return found;
1689}
1690
Liu Boda2c7002017-02-10 16:41:05 +01001691static int __process_pages_contig(struct address_space *mapping,
1692 struct page *locked_page,
1693 pgoff_t start_index, pgoff_t end_index,
1694 unsigned long page_ops, pgoff_t *index_ret)
Chris Masonc8b97812008-10-29 14:49:59 -04001695{
Liu Bo873695b2017-02-02 17:49:22 -08001696 unsigned long nr_pages = end_index - start_index + 1;
Liu Boda2c7002017-02-10 16:41:05 +01001697 unsigned long pages_locked = 0;
Liu Bo873695b2017-02-02 17:49:22 -08001698 pgoff_t index = start_index;
Chris Masonc8b97812008-10-29 14:49:59 -04001699 struct page *pages[16];
Liu Bo873695b2017-02-02 17:49:22 -08001700 unsigned ret;
Liu Boda2c7002017-02-10 16:41:05 +01001701 int err = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001702 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001703
Liu Boda2c7002017-02-10 16:41:05 +01001704 if (page_ops & PAGE_LOCK) {
1705 ASSERT(page_ops == PAGE_LOCK);
1706 ASSERT(index_ret && *index_ret == start_index);
1707 }
1708
Filipe Manana704de492014-10-06 22:14:22 +01001709 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
Liu Bo873695b2017-02-02 17:49:22 -08001710 mapping_set_error(mapping, -EIO);
Filipe Manana704de492014-10-06 22:14:22 +01001711
Chris Masond3977122009-01-05 21:25:51 -05001712 while (nr_pages > 0) {
Liu Bo873695b2017-02-02 17:49:22 -08001713 ret = find_get_pages_contig(mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001714 min_t(unsigned long,
1715 nr_pages, ARRAY_SIZE(pages)), pages);
Liu Boda2c7002017-02-10 16:41:05 +01001716 if (ret == 0) {
1717 /*
1718 * Only if we're going to lock these pages,
1719 * can we find nothing at @index.
1720 */
1721 ASSERT(page_ops & PAGE_LOCK);
Liu Bo49d4a332017-03-06 18:20:56 -08001722 err = -EAGAIN;
1723 goto out;
Liu Boda2c7002017-02-10 16:41:05 +01001724 }
Chris Mason8b62b722009-09-02 16:53:46 -04001725
Liu Boda2c7002017-02-10 16:41:05 +01001726 for (i = 0; i < ret; i++) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001727 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001728 SetPagePrivate2(pages[i]);
1729
Chris Masonc8b97812008-10-29 14:49:59 -04001730 if (pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001731 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001732 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001733 continue;
1734 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001735 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001736 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001737 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001738 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001739 if (page_ops & PAGE_SET_ERROR)
1740 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001741 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001742 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001743 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001744 unlock_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001745 if (page_ops & PAGE_LOCK) {
1746 lock_page(pages[i]);
1747 if (!PageDirty(pages[i]) ||
1748 pages[i]->mapping != mapping) {
1749 unlock_page(pages[i]);
1750 put_page(pages[i]);
1751 err = -EAGAIN;
1752 goto out;
1753 }
1754 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001755 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001756 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001757 }
1758 nr_pages -= ret;
1759 index += ret;
1760 cond_resched();
1761 }
Liu Boda2c7002017-02-10 16:41:05 +01001762out:
1763 if (err && index_ret)
1764 *index_ret = start_index + pages_locked - 1;
1765 return err;
Chris Masonc8b97812008-10-29 14:49:59 -04001766}
Chris Masonc8b97812008-10-29 14:49:59 -04001767
Liu Bo873695b2017-02-02 17:49:22 -08001768void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1769 u64 delalloc_end, struct page *locked_page,
1770 unsigned clear_bits,
1771 unsigned long page_ops)
1772{
1773 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001774 NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001775
1776 __process_pages_contig(inode->i_mapping, locked_page,
1777 start >> PAGE_SHIFT, end >> PAGE_SHIFT,
Liu Boda2c7002017-02-10 16:41:05 +01001778 page_ops, NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001779}
1780
Chris Masond352ac62008-09-29 15:18:18 -04001781/*
1782 * count the number of bytes in the tree that have a given bit(s)
1783 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1784 * cached. The total number found is returned.
1785 */
Chris Masond1310b22008-01-24 16:13:08 -05001786u64 count_range_bits(struct extent_io_tree *tree,
1787 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001788 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001789{
1790 struct rb_node *node;
1791 struct extent_state *state;
1792 u64 cur_start = *start;
1793 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001794 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001795 int found = 0;
1796
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301797 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001798 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001799
Chris Masoncad321a2008-12-17 14:51:42 -05001800 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001801 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1802 total_bytes = tree->dirty_bytes;
1803 goto out;
1804 }
1805 /*
1806 * this search will find all the extents that end after
1807 * our range starts.
1808 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001809 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001810 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001811 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001812
Chris Masond3977122009-01-05 21:25:51 -05001813 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001814 state = rb_entry(node, struct extent_state, rb_node);
1815 if (state->start > search_end)
1816 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001817 if (contig && found && state->start > last + 1)
1818 break;
1819 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001820 total_bytes += min(search_end, state->end) + 1 -
1821 max(cur_start, state->start);
1822 if (total_bytes >= max_bytes)
1823 break;
1824 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001825 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001826 found = 1;
1827 }
Chris Masonec29ed52011-02-23 16:23:20 -05001828 last = state->end;
1829 } else if (contig && found) {
1830 break;
Chris Masond1310b22008-01-24 16:13:08 -05001831 }
1832 node = rb_next(node);
1833 if (!node)
1834 break;
1835 }
1836out:
Chris Masoncad321a2008-12-17 14:51:42 -05001837 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001838 return total_bytes;
1839}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001840
Chris Masond352ac62008-09-29 15:18:18 -04001841/*
1842 * set the private field for a given byte offset in the tree. If there isn't
1843 * an extent_state there already, this does nothing.
1844 */
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001845static noinline int set_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 state->failrec = 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
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001873static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001874 struct io_failure_record **failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001875{
1876 struct rb_node *node;
1877 struct extent_state *state;
1878 int ret = 0;
1879
Chris Masoncad321a2008-12-17 14:51:42 -05001880 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001881 /*
1882 * this search will find all the extents that end after
1883 * our range starts.
1884 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001885 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001886 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001887 ret = -ENOENT;
1888 goto out;
1889 }
1890 state = rb_entry(node, struct extent_state, rb_node);
1891 if (state->start != start) {
1892 ret = -ENOENT;
1893 goto out;
1894 }
David Sterba47dc1962016-02-11 13:24:13 +01001895 *failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001896out:
Chris Masoncad321a2008-12-17 14:51:42 -05001897 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001898 return ret;
1899}
1900
1901/*
1902 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001903 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001904 * has the bits set. Otherwise, 1 is returned if any bit in the
1905 * range is found set.
1906 */
1907int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001908 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001909{
1910 struct extent_state *state = NULL;
1911 struct rb_node *node;
1912 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001913
Chris Masoncad321a2008-12-17 14:51:42 -05001914 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01001915 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001916 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001917 node = &cached->rb_node;
1918 else
1919 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001920 while (node && start <= end) {
1921 state = rb_entry(node, struct extent_state, rb_node);
1922
1923 if (filled && state->start > start) {
1924 bitset = 0;
1925 break;
1926 }
1927
1928 if (state->start > end)
1929 break;
1930
1931 if (state->state & bits) {
1932 bitset = 1;
1933 if (!filled)
1934 break;
1935 } else if (filled) {
1936 bitset = 0;
1937 break;
1938 }
Chris Mason46562ce2009-09-23 20:23:16 -04001939
1940 if (state->end == (u64)-1)
1941 break;
1942
Chris Masond1310b22008-01-24 16:13:08 -05001943 start = state->end + 1;
1944 if (start > end)
1945 break;
1946 node = rb_next(node);
1947 if (!node) {
1948 if (filled)
1949 bitset = 0;
1950 break;
1951 }
1952 }
Chris Masoncad321a2008-12-17 14:51:42 -05001953 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001954 return bitset;
1955}
Chris Masond1310b22008-01-24 16:13:08 -05001956
1957/*
1958 * helper function to set a given page up to date if all the
1959 * extents in the tree for that page are up to date
1960 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001961static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001962{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001963 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001964 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001965 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001966 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001967}
1968
Josef Bacik7870d082017-05-05 11:57:15 -04001969int free_io_failure(struct extent_io_tree *failure_tree,
1970 struct extent_io_tree *io_tree,
1971 struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001972{
1973 int ret;
1974 int err = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001975
David Sterba47dc1962016-02-11 13:24:13 +01001976 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001977 ret = clear_extent_bits(failure_tree, rec->start,
1978 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001979 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001980 if (ret)
1981 err = ret;
1982
Josef Bacik7870d082017-05-05 11:57:15 -04001983 ret = clear_extent_bits(io_tree, rec->start,
David Woodhouse53b381b2013-01-29 18:40:14 -05001984 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001985 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05001986 if (ret && !err)
1987 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001988
1989 kfree(rec);
1990 return err;
1991}
1992
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001993/*
1994 * this bypasses the standard btrfs submit functions deliberately, as
1995 * the standard behavior is to write all copies in a raid setup. here we only
1996 * want to write the one bad copy. so we do the mapping for ourselves and issue
1997 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001998 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001999 * actually prevents the read that triggered the error from finishing.
2000 * currently, there can be no more than two copies of every data bit. thus,
2001 * exactly one rewrite is required.
2002 */
Josef Bacik6ec656b2017-05-05 11:57:14 -04002003int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
2004 u64 length, u64 logical, struct page *page,
2005 unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002006{
2007 struct bio *bio;
2008 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002009 u64 map_length = 0;
2010 u64 sector;
2011 struct btrfs_bio *bbio = NULL;
2012 int ret;
2013
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002014 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002015 BUG_ON(!mirror_num);
2016
David Sterbac5e4c3d2017-06-12 17:29:41 +02002017 bio = btrfs_io_bio_alloc(1);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002018 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002019 map_length = length;
2020
Filipe Mananab5de8d02016-05-27 22:21:27 +01002021 /*
2022 * Avoid races with device replace and make sure our bbio has devices
2023 * associated to its stripes that don't go away while we are doing the
2024 * read repair operation.
2025 */
2026 btrfs_bio_counter_inc_blocked(fs_info);
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03002027 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
Liu Boc7253282017-03-29 10:53:58 -07002028 /*
2029 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
2030 * to update all raid stripes, but here we just want to correct
2031 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2032 * stripe's dev and sector.
2033 */
2034 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2035 &map_length, &bbio, 0);
2036 if (ret) {
2037 btrfs_bio_counter_dec(fs_info);
2038 bio_put(bio);
2039 return -EIO;
2040 }
2041 ASSERT(bbio->mirror_num == 1);
2042 } else {
2043 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2044 &map_length, &bbio, mirror_num);
2045 if (ret) {
2046 btrfs_bio_counter_dec(fs_info);
2047 bio_put(bio);
2048 return -EIO;
2049 }
2050 BUG_ON(mirror_num != bbio->mirror_num);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002051 }
Liu Boc7253282017-03-29 10:53:58 -07002052
2053 sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002054 bio->bi_iter.bi_sector = sector;
Liu Boc7253282017-03-29 10:53:58 -07002055 dev = bbio->stripes[bbio->mirror_num - 1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002056 btrfs_put_bbio(bbio);
Anand Jainebbede42017-12-04 12:54:52 +08002057 if (!dev || !dev->bdev ||
2058 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002059 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002060 bio_put(bio);
2061 return -EIO;
2062 }
Christoph Hellwig74d46992017-08-23 19:10:32 +02002063 bio_set_dev(bio, dev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002064 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Miao Xieffdd2012014-09-12 18:44:00 +08002065 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002066
Mike Christie4e49ea42016-06-05 14:31:41 -05002067 if (btrfsic_submit_bio_wait(bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002068 /* try to remap that extent elsewhere? */
Filipe Mananab5de8d02016-05-27 22:21:27 +01002069 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002070 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002071 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002072 return -EIO;
2073 }
2074
David Sterbab14af3b2015-10-08 10:43:10 +02002075 btrfs_info_rl_in_rcu(fs_info,
2076 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Josef Bacik6ec656b2017-05-05 11:57:14 -04002077 ino, start,
Miao Xie1203b682014-09-12 18:44:01 +08002078 rcu_str_deref(dev->name), sector);
Filipe Mananab5de8d02016-05-27 22:21:27 +01002079 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002080 bio_put(bio);
2081 return 0;
2082}
2083
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002084int repair_eb_io_failure(struct btrfs_fs_info *fs_info,
2085 struct extent_buffer *eb, int mirror_num)
Josef Bacikea466792012-03-26 21:57:36 -04002086{
Josef Bacikea466792012-03-26 21:57:36 -04002087 u64 start = eb->start;
David Sterbacc5e31a2018-03-01 18:20:27 +01002088 int i, num_pages = num_extent_pages(eb);
Chris Masond95603b2012-04-12 15:55:15 -04002089 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002090
David Howellsbc98a422017-07-17 08:45:34 +01002091 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002092 return -EROFS;
2093
Josef Bacikea466792012-03-26 21:57:36 -04002094 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002095 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002096
Josef Bacik6ec656b2017-05-05 11:57:14 -04002097 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002098 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002099 if (ret)
2100 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002101 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002102 }
2103
2104 return ret;
2105}
2106
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002107/*
2108 * each time an IO finishes, we do a fast check in the IO failure tree
2109 * to see if we need to process or clean up an io_failure_record
2110 */
Josef Bacik7870d082017-05-05 11:57:15 -04002111int clean_io_failure(struct btrfs_fs_info *fs_info,
2112 struct extent_io_tree *failure_tree,
2113 struct extent_io_tree *io_tree, u64 start,
2114 struct page *page, u64 ino, unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002115{
2116 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002117 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002118 struct extent_state *state;
2119 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002120 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002121
2122 private = 0;
Josef Bacik7870d082017-05-05 11:57:15 -04002123 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2124 EXTENT_DIRTY, 0);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002125 if (!ret)
2126 return 0;
2127
Josef Bacik7870d082017-05-05 11:57:15 -04002128 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002129 if (ret)
2130 return 0;
2131
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002132 BUG_ON(!failrec->this_mirror);
2133
2134 if (failrec->in_validation) {
2135 /* there was no real error, just free the record */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002136 btrfs_debug(fs_info,
2137 "clean_io_failure: freeing dummy error at %llu",
2138 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002139 goto out;
2140 }
David Howellsbc98a422017-07-17 08:45:34 +01002141 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002142 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002143
Josef Bacik7870d082017-05-05 11:57:15 -04002144 spin_lock(&io_tree->lock);
2145 state = find_first_extent_bit_state(io_tree,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002146 failrec->start,
2147 EXTENT_LOCKED);
Josef Bacik7870d082017-05-05 11:57:15 -04002148 spin_unlock(&io_tree->lock);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002149
Miao Xie883d0de2013-07-25 19:22:35 +08002150 if (state && state->start <= failrec->start &&
2151 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002152 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2153 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002154 if (num_copies > 1) {
Josef Bacik7870d082017-05-05 11:57:15 -04002155 repair_io_failure(fs_info, ino, start, failrec->len,
2156 failrec->logical, page, pg_offset,
2157 failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002158 }
2159 }
2160
2161out:
Josef Bacik7870d082017-05-05 11:57:15 -04002162 free_io_failure(failure_tree, io_tree, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002163
Miao Xie454ff3d2014-09-12 18:43:58 +08002164 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002165}
2166
Miao Xief6124962014-09-12 18:44:04 +08002167/*
2168 * Can be called when
2169 * - hold extent lock
2170 * - under ordered extent
2171 * - the inode is freeing
2172 */
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002173void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
Miao Xief6124962014-09-12 18:44:04 +08002174{
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002175 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
Miao Xief6124962014-09-12 18:44:04 +08002176 struct io_failure_record *failrec;
2177 struct extent_state *state, *next;
2178
2179 if (RB_EMPTY_ROOT(&failure_tree->state))
2180 return;
2181
2182 spin_lock(&failure_tree->lock);
2183 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2184 while (state) {
2185 if (state->start > end)
2186 break;
2187
2188 ASSERT(state->end <= end);
2189
2190 next = next_state(state);
2191
David Sterba47dc1962016-02-11 13:24:13 +01002192 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002193 free_extent_state(state);
2194 kfree(failrec);
2195
2196 state = next;
2197 }
2198 spin_unlock(&failure_tree->lock);
2199}
2200
Miao Xie2fe63032014-09-12 18:43:59 +08002201int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
David Sterba47dc1962016-02-11 13:24:13 +01002202 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002203{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002204 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002205 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002206 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002207 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2208 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2209 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002210 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002211 u64 logical;
2212
David Sterba47dc1962016-02-11 13:24:13 +01002213 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002214 if (ret) {
2215 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2216 if (!failrec)
2217 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002218
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002219 failrec->start = start;
2220 failrec->len = end - start + 1;
2221 failrec->this_mirror = 0;
2222 failrec->bio_flags = 0;
2223 failrec->in_validation = 0;
2224
2225 read_lock(&em_tree->lock);
2226 em = lookup_extent_mapping(em_tree, start, failrec->len);
2227 if (!em) {
2228 read_unlock(&em_tree->lock);
2229 kfree(failrec);
2230 return -EIO;
2231 }
2232
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002233 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002234 free_extent_map(em);
2235 em = NULL;
2236 }
2237 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002238 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002239 kfree(failrec);
2240 return -EIO;
2241 }
Miao Xie2fe63032014-09-12 18:43:59 +08002242
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002243 logical = start - em->start;
2244 logical = em->block_start + logical;
2245 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2246 logical = em->block_start;
2247 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2248 extent_set_compress_type(&failrec->bio_flags,
2249 em->compress_type);
2250 }
Miao Xie2fe63032014-09-12 18:43:59 +08002251
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002252 btrfs_debug(fs_info,
2253 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2254 logical, start, failrec->len);
Miao Xie2fe63032014-09-12 18:43:59 +08002255
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002256 failrec->logical = logical;
2257 free_extent_map(em);
2258
2259 /* set the bits in the private failure tree */
2260 ret = set_extent_bits(failure_tree, start, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002261 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002262 if (ret >= 0)
David Sterba47dc1962016-02-11 13:24:13 +01002263 ret = set_state_failrec(failure_tree, start, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002264 /* set the bits in the inode's tree */
2265 if (ret >= 0)
David Sterbaceeb0ae2016-04-26 23:54:39 +02002266 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002267 if (ret < 0) {
2268 kfree(failrec);
2269 return ret;
2270 }
2271 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002272 btrfs_debug(fs_info,
2273 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2274 failrec->logical, failrec->start, failrec->len,
2275 failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002276 /*
2277 * when data can be on disk more than twice, add to failrec here
2278 * (e.g. with a list for failed_mirror) to make
2279 * clean_io_failure() clean all those errors at once.
2280 */
2281 }
Miao Xie2fe63032014-09-12 18:43:59 +08002282
2283 *failrec_ret = failrec;
2284
2285 return 0;
2286}
2287
Ming Leia0b60d72017-12-18 20:22:11 +08002288bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages,
Miao Xie2fe63032014-09-12 18:43:59 +08002289 struct io_failure_record *failrec, int failed_mirror)
2290{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002291 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002292 int num_copies;
2293
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002294 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002295 if (num_copies == 1) {
2296 /*
2297 * we only have a single copy of the data, so don't bother with
2298 * all the retry and error correction code that follows. no
2299 * matter what the error is, it is very likely to persist.
2300 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002301 btrfs_debug(fs_info,
2302 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2303 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002304 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002305 }
2306
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002307 /*
2308 * there are two premises:
2309 * a) deliver good data to the caller
2310 * b) correct the bad sectors on disk
2311 */
Ming Leia0b60d72017-12-18 20:22:11 +08002312 if (failed_bio_pages > 1) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002313 /*
2314 * to fulfill b), we need to know the exact failing sectors, as
2315 * we don't want to rewrite any more than the failed ones. thus,
2316 * we need separate read requests for the failed bio
2317 *
2318 * if the following BUG_ON triggers, our validation request got
2319 * merged. we need separate requests for our algorithm to work.
2320 */
2321 BUG_ON(failrec->in_validation);
2322 failrec->in_validation = 1;
2323 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002324 } else {
2325 /*
2326 * we're ready to fulfill a) and b) alongside. get a good copy
2327 * of the failed sector and if we succeed, we have setup
2328 * everything for repair_io_failure to do the rest for us.
2329 */
2330 if (failrec->in_validation) {
2331 BUG_ON(failrec->this_mirror != failed_mirror);
2332 failrec->in_validation = 0;
2333 failrec->this_mirror = 0;
2334 }
2335 failrec->failed_mirror = failed_mirror;
2336 failrec->this_mirror++;
2337 if (failrec->this_mirror == failed_mirror)
2338 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002339 }
2340
Miao Xiefacc8a222013-07-25 19:22:34 +08002341 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002342 btrfs_debug(fs_info,
2343 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2344 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002345 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002346 }
2347
Liu Boc3cfb652017-07-13 15:00:50 -07002348 return true;
Miao Xie2fe63032014-09-12 18:43:59 +08002349}
2350
2351
2352struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2353 struct io_failure_record *failrec,
2354 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002355 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002356{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002357 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002358 struct bio *bio;
2359 struct btrfs_io_bio *btrfs_failed_bio;
2360 struct btrfs_io_bio *btrfs_bio;
2361
David Sterbac5e4c3d2017-06-12 17:29:41 +02002362 bio = btrfs_io_bio_alloc(1);
Miao Xie2fe63032014-09-12 18:43:59 +08002363 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002364 bio->bi_iter.bi_sector = failrec->logical >> 9;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002365 bio_set_dev(bio, fs_info->fs_devices->latest_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002366 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002367 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002368
Miao Xiefacc8a222013-07-25 19:22:34 +08002369 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2370 if (btrfs_failed_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002371 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2372
2373 btrfs_bio = btrfs_io_bio(bio);
2374 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002375 icsum *= csum_size;
2376 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002377 csum_size);
2378 }
2379
Miao Xie2fe63032014-09-12 18:43:59 +08002380 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002381
Miao Xie2fe63032014-09-12 18:43:59 +08002382 return bio;
2383}
2384
2385/*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002386 * This is a generic handler for readpage errors. If other copies exist, read
2387 * those and write back good data to the failed position. Does not investigate
2388 * in remapping the failed extent elsewhere, hoping the device will be smart
2389 * enough to do this as needed
Miao Xie2fe63032014-09-12 18:43:59 +08002390 */
Miao Xie2fe63032014-09-12 18:43:59 +08002391static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2392 struct page *page, u64 start, u64 end,
2393 int failed_mirror)
2394{
2395 struct io_failure_record *failrec;
2396 struct inode *inode = page->mapping->host;
2397 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002398 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie2fe63032014-09-12 18:43:59 +08002399 struct bio *bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002400 int read_mode = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002401 blk_status_t status;
Miao Xie2fe63032014-09-12 18:43:59 +08002402 int ret;
Christoph Hellwig8a2ee442019-02-15 19:13:07 +08002403 unsigned failed_bio_pages = failed_bio->bi_iter.bi_size >> PAGE_SHIFT;
Miao Xie2fe63032014-09-12 18:43:59 +08002404
Mike Christie1f7ad752016-06-05 14:31:51 -05002405 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002406
2407 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2408 if (ret)
2409 return ret;
2410
Ming Leia0b60d72017-12-18 20:22:11 +08002411 if (!btrfs_check_repairable(inode, failed_bio_pages, failrec,
Liu Boc3cfb652017-07-13 15:00:50 -07002412 failed_mirror)) {
Josef Bacik7870d082017-05-05 11:57:15 -04002413 free_io_failure(failure_tree, tree, failrec);
Miao Xie2fe63032014-09-12 18:43:59 +08002414 return -EIO;
2415 }
2416
Ming Leia0b60d72017-12-18 20:22:11 +08002417 if (failed_bio_pages > 1)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002418 read_mode |= REQ_FAILFAST_DEV;
Miao Xie2fe63032014-09-12 18:43:59 +08002419
2420 phy_offset >>= inode->i_sb->s_blocksize_bits;
2421 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2422 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002423 (int)phy_offset, failed_bio->bi_end_io,
2424 NULL);
David Sterbaebcc3262018-06-29 10:56:53 +02002425 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie2fe63032014-09-12 18:43:59 +08002426
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002427 btrfs_debug(btrfs_sb(inode->i_sb),
2428 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2429 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002430
Linus Torvalds8c27cb32017-07-05 16:41:23 -07002431 status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002432 failrec->bio_flags, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002433 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04002434 free_io_failure(failure_tree, tree, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002435 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002436 ret = blk_status_to_errno(status);
Miao Xie6c387ab2014-09-12 18:43:57 +08002437 }
2438
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002439 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002440}
2441
Chris Masond1310b22008-01-24 16:13:08 -05002442/* lots and lots of room for performance fixes in the end_bio funcs */
2443
David Sterbab5227c02015-12-03 13:08:59 +01002444void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002445{
2446 int uptodate = (err == 0);
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002447 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002448
Nikolay Borisovc6297322018-11-08 10:18:08 +02002449 btrfs_writepage_endio_finish_ordered(page, start, end, uptodate);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002450
Jeff Mahoney87826df2012-02-15 16:23:57 +01002451 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002452 ClearPageUptodate(page);
2453 SetPageError(page);
Colin Ian Kingbff5baf2017-05-09 18:14:01 +01002454 ret = err < 0 ? err : -EIO;
Liu Bo5dca6ee2014-05-12 12:47:36 +08002455 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002456 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002457}
2458
Chris Masond1310b22008-01-24 16:13:08 -05002459/*
2460 * after a writepage IO is done, we need to:
2461 * clear the uptodate bits on error
2462 * clear the writeback bits in the extent tree for this IO
2463 * end_page_writeback if the page has no more pending IO
2464 *
2465 * Scheduling is not allowed, so the extent state tree is expected
2466 * to have one and only one object corresponding to this IO.
2467 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002468static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002469{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002470 int error = blk_status_to_errno(bio->bi_status);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002471 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002472 u64 start;
2473 u64 end;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002474 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08002475 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002476
David Sterbac09abff2017-07-13 18:10:07 +02002477 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08002478 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002479 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002480 struct inode *inode = page->mapping->host;
2481 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002482
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002483 /* We always issue full-page reads, but if some block
2484 * in a page fails to read, blk_update_request() will
2485 * advance bv_offset and adjust bv_len to compensate.
2486 * Print a warning for nonzero offsets, and an error
2487 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002488 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2489 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002490 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002491 "partial page write in btrfs with offset %u and length %u",
2492 bvec->bv_offset, bvec->bv_len);
2493 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002494 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002495 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002496 bvec->bv_offset, bvec->bv_len);
2497 }
Chris Masond1310b22008-01-24 16:13:08 -05002498
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002499 start = page_offset(page);
2500 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002501
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002502 end_extent_writepage(page, error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002503 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002504 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002505
Chris Masond1310b22008-01-24 16:13:08 -05002506 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002507}
2508
Miao Xie883d0de2013-07-25 19:22:35 +08002509static void
2510endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2511 int uptodate)
2512{
2513 struct extent_state *cached = NULL;
2514 u64 end = start + len - 1;
2515
2516 if (uptodate && tree->track_uptodate)
2517 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
David Sterbad810a4b2017-12-07 18:52:54 +01002518 unlock_extent_cached_atomic(tree, start, end, &cached);
Miao Xie883d0de2013-07-25 19:22:35 +08002519}
2520
Chris Masond1310b22008-01-24 16:13:08 -05002521/*
2522 * after a readpage IO is done, we need to:
2523 * clear the uptodate bits on error
2524 * set the uptodate bits if things worked
2525 * set the page up to date if all extents in the tree are uptodate
2526 * clear the lock bit in the extent tree
2527 * unlock the page if there are no other extents locked for it
2528 *
2529 * Scheduling is not allowed, so the extent state tree is expected
2530 * to have one and only one object corresponding to this IO.
2531 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002532static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002533{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002534 struct bio_vec *bvec;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002535 int uptodate = !bio->bi_status;
Miao Xiefacc8a222013-07-25 19:22:34 +08002536 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04002537 struct extent_io_tree *tree, *failure_tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002538 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002539 u64 start;
2540 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002541 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002542 u64 extent_start = 0;
2543 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002544 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002545 int ret;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002546 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08002547 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002548
David Sterbac09abff2017-07-13 18:10:07 +02002549 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08002550 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002551 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002552 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002553 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002554 bool data_inode = btrfs_ino(BTRFS_I(inode))
2555 != BTRFS_BTREE_INODE_OBJECTID;
Arne Jansen507903b2011-04-06 10:02:20 +00002556
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002557 btrfs_debug(fs_info,
2558 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002559 (u64)bio->bi_iter.bi_sector, bio->bi_status,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002560 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002561 tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002562 failure_tree = &BTRFS_I(inode)->io_failure_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002563
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002564 /* We always issue full-page reads, but if some block
2565 * in a page fails to read, blk_update_request() will
2566 * advance bv_offset and adjust bv_len to compensate.
2567 * Print a warning for nonzero offsets, and an error
2568 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002569 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2570 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002571 btrfs_err(fs_info,
2572 "partial page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002573 bvec->bv_offset, bvec->bv_len);
2574 else
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002575 btrfs_info(fs_info,
2576 "incomplete page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002577 bvec->bv_offset, bvec->bv_len);
2578 }
Chris Masond1310b22008-01-24 16:13:08 -05002579
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002580 start = page_offset(page);
2581 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002582 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002583
Chris Mason9be33952013-05-17 18:30:14 -04002584 mirror = io_bio->mirror_num;
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002585 if (likely(uptodate)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002586 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2587 page, start, end,
2588 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002589 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002590 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002591 else
Josef Bacik7870d082017-05-05 11:57:15 -04002592 clean_io_failure(BTRFS_I(inode)->root->fs_info,
2593 failure_tree, tree, start,
2594 page,
2595 btrfs_ino(BTRFS_I(inode)), 0);
Chris Masond1310b22008-01-24 16:13:08 -05002596 }
Josef Bacikea466792012-03-26 21:57:36 -04002597
Miao Xief2a09da2013-07-25 19:22:33 +08002598 if (likely(uptodate))
2599 goto readpage_ok;
2600
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002601 if (data_inode) {
Liu Bo9d0d1c82017-03-24 15:04:50 -07002602
2603 /*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002604 * The generic bio_readpage_error handles errors the
2605 * following way: If possible, new read requests are
2606 * created and submitted and will end up in
2607 * end_bio_extent_readpage as well (if we're lucky,
2608 * not in the !uptodate case). In that case it returns
2609 * 0 and we just go on with the next page in our bio.
2610 * If it can't handle the error it will return -EIO and
2611 * we remain responsible for that page.
Liu Bo9d0d1c82017-03-24 15:04:50 -07002612 */
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002613 ret = bio_readpage_error(bio, offset, page, start, end,
2614 mirror);
2615 if (ret == 0) {
2616 uptodate = !bio->bi_status;
2617 offset += len;
2618 continue;
2619 }
2620 } else {
2621 struct extent_buffer *eb;
2622
2623 eb = (struct extent_buffer *)page->private;
2624 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
2625 eb->read_mirror = mirror;
2626 atomic_dec(&eb->io_pages);
2627 if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD,
2628 &eb->bflags))
2629 btree_readahead_hook(eb, -EIO);
Chris Mason7e383262008-04-09 16:28:12 -04002630 }
Miao Xief2a09da2013-07-25 19:22:33 +08002631readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002632 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002633 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002634 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002635 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002636
2637 /* Zero out the end if this page straddles i_size */
Johannes Thumshirn70730172018-12-05 15:23:03 +01002638 off = offset_in_page(i_size);
Liu Boa583c022014-08-19 23:32:22 +08002639 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002640 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002641 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002642 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002643 ClearPageUptodate(page);
2644 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002645 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002646 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002647 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002648
2649 if (unlikely(!uptodate)) {
2650 if (extent_len) {
2651 endio_readpage_release_extent(tree,
2652 extent_start,
2653 extent_len, 1);
2654 extent_start = 0;
2655 extent_len = 0;
2656 }
2657 endio_readpage_release_extent(tree, start,
2658 end - start + 1, 0);
2659 } else if (!extent_len) {
2660 extent_start = start;
2661 extent_len = end + 1 - start;
2662 } else if (extent_start + extent_len == start) {
2663 extent_len += end + 1 - start;
2664 } else {
2665 endio_readpage_release_extent(tree, extent_start,
2666 extent_len, uptodate);
2667 extent_start = start;
2668 extent_len = end + 1 - start;
2669 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002670 }
Chris Masond1310b22008-01-24 16:13:08 -05002671
Miao Xie883d0de2013-07-25 19:22:35 +08002672 if (extent_len)
2673 endio_readpage_release_extent(tree, extent_start, extent_len,
2674 uptodate);
David Sterbab3a0dd52018-11-22 17:16:49 +01002675 btrfs_io_bio_free_csum(io_bio);
Chris Masond1310b22008-01-24 16:13:08 -05002676 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002677}
2678
Chris Mason9be33952013-05-17 18:30:14 -04002679/*
David Sterba184f9992017-06-12 17:29:39 +02002680 * Initialize the members up to but not including 'bio'. Use after allocating a
2681 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
2682 * 'bio' because use of __GFP_ZERO is not supported.
Chris Mason9be33952013-05-17 18:30:14 -04002683 */
David Sterba184f9992017-06-12 17:29:39 +02002684static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
Chris Masond1310b22008-01-24 16:13:08 -05002685{
David Sterba184f9992017-06-12 17:29:39 +02002686 memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
2687}
2688
2689/*
David Sterba6e707bc2017-06-02 17:26:26 +02002690 * The following helpers allocate a bio. As it's backed by a bioset, it'll
2691 * never fail. We're returning a bio right now but you can call btrfs_io_bio
2692 * for the appropriate container_of magic
Chris Masond1310b22008-01-24 16:13:08 -05002693 */
David Sterbac821e7f32017-06-02 18:35:36 +02002694struct bio *btrfs_bio_alloc(struct block_device *bdev, u64 first_byte)
Chris Masond1310b22008-01-24 16:13:08 -05002695{
2696 struct bio *bio;
2697
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002698 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset);
Christoph Hellwig74d46992017-08-23 19:10:32 +02002699 bio_set_dev(bio, bdev);
David Sterbac821e7f32017-06-02 18:35:36 +02002700 bio->bi_iter.bi_sector = first_byte >> 9;
David Sterba184f9992017-06-12 17:29:39 +02002701 btrfs_io_bio_init(btrfs_io_bio(bio));
Chris Masond1310b22008-01-24 16:13:08 -05002702 return bio;
2703}
2704
David Sterba8b6c1d52017-06-02 17:48:13 +02002705struct bio *btrfs_bio_clone(struct bio *bio)
Chris Mason9be33952013-05-17 18:30:14 -04002706{
Miao Xie23ea8e52014-09-12 18:43:54 +08002707 struct btrfs_io_bio *btrfs_bio;
2708 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002709
David Sterba6e707bc2017-06-02 17:26:26 +02002710 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002711 new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset);
David Sterba6e707bc2017-06-02 17:26:26 +02002712 btrfs_bio = btrfs_io_bio(new);
David Sterba184f9992017-06-12 17:29:39 +02002713 btrfs_io_bio_init(btrfs_bio);
David Sterba6e707bc2017-06-02 17:26:26 +02002714 btrfs_bio->iter = bio->bi_iter;
Miao Xie23ea8e52014-09-12 18:43:54 +08002715 return new;
2716}
Chris Mason9be33952013-05-17 18:30:14 -04002717
David Sterbac5e4c3d2017-06-12 17:29:41 +02002718struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
Chris Mason9be33952013-05-17 18:30:14 -04002719{
Miao Xiefacc8a222013-07-25 19:22:34 +08002720 struct bio *bio;
2721
David Sterba6e707bc2017-06-02 17:26:26 +02002722 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002723 bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
David Sterba184f9992017-06-12 17:29:39 +02002724 btrfs_io_bio_init(btrfs_io_bio(bio));
Miao Xiefacc8a222013-07-25 19:22:34 +08002725 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002726}
2727
Liu Boe4770942017-05-16 10:57:14 -07002728struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
Liu Bo2f8e9142017-05-15 17:43:31 -07002729{
2730 struct bio *bio;
2731 struct btrfs_io_bio *btrfs_bio;
2732
2733 /* this will never fail when it's backed by a bioset */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002734 bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset);
Liu Bo2f8e9142017-05-15 17:43:31 -07002735 ASSERT(bio);
2736
2737 btrfs_bio = btrfs_io_bio(bio);
David Sterba184f9992017-06-12 17:29:39 +02002738 btrfs_io_bio_init(btrfs_bio);
Liu Bo2f8e9142017-05-15 17:43:31 -07002739
2740 bio_trim(bio, offset >> 9, size >> 9);
Liu Bo17347ce2017-05-15 15:33:27 -07002741 btrfs_bio->iter = bio->bi_iter;
Liu Bo2f8e9142017-05-15 17:43:31 -07002742 return bio;
2743}
Chris Mason9be33952013-05-17 18:30:14 -04002744
David Sterba4b81ba42017-06-06 19:14:26 +02002745/*
2746 * @opf: bio REQ_OP_* and REQ_* flags as one value
David Sterbab8b3d622017-06-12 19:50:41 +02002747 * @tree: tree so we can call our merge_bio hook
2748 * @wbc: optional writeback control for io accounting
2749 * @page: page to add to the bio
2750 * @pg_offset: offset of the new bio or to check whether we are adding
2751 * a contiguous page to the previous one
2752 * @size: portion of page that we want to write
2753 * @offset: starting offset in the page
2754 * @bdev: attach newly created bios to this bdev
David Sterba5c2b1fd2017-06-06 19:22:55 +02002755 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
David Sterbab8b3d622017-06-12 19:50:41 +02002756 * @end_io_func: end_io callback for new bio
2757 * @mirror_num: desired mirror to read/write
2758 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
2759 * @bio_flags: flags of the current bio to see if we can merge them
David Sterba4b81ba42017-06-06 19:14:26 +02002760 */
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;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002775 size_t page_size = min_t(size_t, size, PAGE_SIZE);
David Sterba6273b7f2017-10-04 17:30:11 +02002776 sector_t sector = offset >> 9;
Chris Masond1310b22008-01-24 16:13:08 -05002777
David Sterba5c2b1fd2017-06-06 19:22:55 +02002778 ASSERT(bio_ret);
2779
2780 if (*bio_ret) {
David Sterba0c8508a2017-06-12 20:00:43 +02002781 bool contig;
2782 bool can_merge = true;
2783
Chris Masond1310b22008-01-24 16:13:08 -05002784 bio = *bio_ret;
David Sterba0c8508a2017-06-12 20:00:43 +02002785 if (prev_bio_flags & EXTENT_BIO_COMPRESSED)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002786 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002787 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002788 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002789
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002790 ASSERT(tree->ops);
2791 if (btrfs_bio_fits_in_stripe(page, page_size, bio, bio_flags))
David Sterba0c8508a2017-06-12 20:00:43 +02002792 can_merge = false;
2793
2794 if (prev_bio_flags != bio_flags || !contig || !can_merge ||
Filipe Manana005efed2015-09-14 09:09:31 +01002795 force_bio_submit ||
David Sterba6c5a4e22017-10-04 17:10:34 +02002796 bio_add_page(bio, page, page_size, pg_offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002797 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002798 if (ret < 0) {
2799 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002800 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002801 }
Chris Masond1310b22008-01-24 16:13:08 -05002802 bio = NULL;
2803 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002804 if (wbc)
2805 wbc_account_io(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002806 return 0;
2807 }
2808 }
Chris Masonc8b97812008-10-29 14:49:59 -04002809
David Sterba6273b7f2017-10-04 17:30:11 +02002810 bio = btrfs_bio_alloc(bdev, offset);
David Sterba6c5a4e22017-10-04 17:10:34 +02002811 bio_add_page(bio, page, page_size, pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002812 bio->bi_end_io = end_io_func;
2813 bio->bi_private = tree;
Jens Axboee6959b92017-06-27 11:51:28 -06002814 bio->bi_write_hint = page->mapping->host->i_write_hint;
David Sterba4b81ba42017-06-06 19:14:26 +02002815 bio->bi_opf = opf;
Chris Masonda2f0f72015-07-02 13:57:22 -07002816 if (wbc) {
2817 wbc_init_bio(wbc, bio);
2818 wbc_account_io(wbc, page, page_size);
2819 }
Chris Mason70dec802008-01-29 09:59:12 -05002820
David Sterba5c2b1fd2017-06-06 19:22:55 +02002821 *bio_ret = bio;
Chris Masond1310b22008-01-24 16:13:08 -05002822
2823 return ret;
2824}
2825
Eric Sandeen48a3b632013-04-25 20:41:01 +00002826static void attach_extent_buffer_page(struct extent_buffer *eb,
2827 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002828{
2829 if (!PagePrivate(page)) {
2830 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002831 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002832 set_page_private(page, (unsigned long)eb);
2833 } else {
2834 WARN_ON(page->private != (unsigned long)eb);
2835 }
2836}
2837
Chris Masond1310b22008-01-24 16:13:08 -05002838void set_page_extent_mapped(struct page *page)
2839{
2840 if (!PagePrivate(page)) {
2841 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002842 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002843 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002844 }
2845}
2846
Miao Xie125bac012013-07-25 19:22:37 +08002847static struct extent_map *
2848__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2849 u64 start, u64 len, get_extent_t *get_extent,
2850 struct extent_map **em_cached)
2851{
2852 struct extent_map *em;
2853
2854 if (em_cached && *em_cached) {
2855 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002856 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002857 start < extent_map_end(em)) {
Elena Reshetova490b54d2017-03-03 10:55:12 +02002858 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002859 return em;
2860 }
2861
2862 free_extent_map(em);
2863 *em_cached = NULL;
2864 }
2865
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02002866 em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
Miao Xie125bac012013-07-25 19:22:37 +08002867 if (em_cached && !IS_ERR_OR_NULL(em)) {
2868 BUG_ON(*em_cached);
Elena Reshetova490b54d2017-03-03 10:55:12 +02002869 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002870 *em_cached = em;
2871 }
2872 return em;
2873}
Chris Masond1310b22008-01-24 16:13:08 -05002874/*
2875 * basic readpage implementation. Locked extent state structs are inserted
2876 * into the tree that are removed when the IO is done (by the end_io
2877 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002878 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07002879 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05002880 */
Miao Xie99740902013-07-25 19:22:36 +08002881static int __do_readpage(struct extent_io_tree *tree,
2882 struct page *page,
2883 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002884 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002885 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02002886 unsigned long *bio_flags, unsigned int read_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002887 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05002888{
2889 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002890 u64 start = page_offset(page);
David Sterba8eec8292017-06-06 19:50:13 +02002891 const u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002892 u64 cur = start;
2893 u64 extent_offset;
2894 u64 last_byte = i_size_read(inode);
2895 u64 block_start;
2896 u64 cur_end;
Chris Masond1310b22008-01-24 16:13:08 -05002897 struct extent_map *em;
2898 struct block_device *bdev;
Liu Bobaf863b2016-07-11 10:39:07 -07002899 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002900 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002901 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002902 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002903 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002904 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00002905 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002906
2907 set_page_extent_mapped(page);
2908
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002909 if (!PageUptodate(page)) {
2910 if (cleancache_get_page(page) == 0) {
2911 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08002912 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002913 goto out;
2914 }
2915 }
2916
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002917 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04002918 char *userpage;
Johannes Thumshirn70730172018-12-05 15:23:03 +01002919 size_t zero_offset = offset_in_page(last_byte);
Chris Masonc8b97812008-10-29 14:49:59 -04002920
2921 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002922 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002923 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002924 memset(userpage + zero_offset, 0, iosize);
2925 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002926 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002927 }
2928 }
Chris Masond1310b22008-01-24 16:13:08 -05002929 while (cur <= end) {
Filipe Manana005efed2015-09-14 09:09:31 +01002930 bool force_bio_submit = false;
David Sterba6273b7f2017-10-04 17:30:11 +02002931 u64 offset;
Josef Bacikc8f2f242013-02-11 11:33:00 -05002932
Chris Masond1310b22008-01-24 16:13:08 -05002933 if (cur >= last_byte) {
2934 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002935 struct extent_state *cached = NULL;
2936
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002937 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002938 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002939 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002940 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002941 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002942 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002943 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00002944 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01002945 cur + iosize - 1, &cached);
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 &&
Filipe Manana8e928212019-02-14 15:17:20 +00003016 *prev_em_start != em->start)
Filipe Manana005efed2015-09-14 09:09:31 +01003017 force_bio_submit = true;
3018
3019 if (prev_em_start)
Filipe Manana8e928212019-02-14 15:17:20 +00003020 *prev_em_start = em->start;
Filipe Manana005efed2015-09-14 09:09:31 +01003021
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,
David Sterbae43bbe52017-12-12 21:43:52 +01003038 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003039 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003040 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003041 continue;
3042 }
3043 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003044 if (test_range_bit(tree, cur, cur_end,
3045 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003046 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003047 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003048 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003049 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003050 continue;
3051 }
Chris Mason70dec802008-01-29 09:59:12 -05003052 /* we have an inline extent but it didn't get marked up
3053 * to date. Error out
3054 */
3055 if (block_start == EXTENT_MAP_INLINE) {
3056 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003057 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003058 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003059 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003060 continue;
3061 }
Chris Masond1310b22008-01-24 16:13:08 -05003062
David Sterba4b81ba42017-06-06 19:14:26 +02003063 ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL,
David Sterba6273b7f2017-10-04 17:30:11 +02003064 page, offset, disk_io_size,
3065 pg_offset, bdev, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003066 end_bio_extent_readpage, mirror_num,
3067 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003068 this_bio_flag,
3069 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003070 if (!ret) {
3071 nr++;
3072 *bio_flags = this_bio_flag;
3073 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003074 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003075 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003076 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003077 }
Chris Masond1310b22008-01-24 16:13:08 -05003078 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003079 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003080 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003081out:
Chris Masond1310b22008-01-24 16:13:08 -05003082 if (!nr) {
3083 if (!PageError(page))
3084 SetPageUptodate(page);
3085 unlock_page(page);
3086 }
Liu Bobaf863b2016-07-11 10:39:07 -07003087 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003088}
3089
Nikolay Borisove65ef212019-03-11 09:55:38 +02003090static inline void contiguous_readpages(struct extent_io_tree *tree,
Miao Xie99740902013-07-25 19:22:36 +08003091 struct page *pages[], int nr_pages,
3092 u64 start, u64 end,
Miao Xie125bac012013-07-25 19:22:37 +08003093 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003094 struct bio **bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003095 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003096 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003097{
3098 struct inode *inode;
3099 struct btrfs_ordered_extent *ordered;
3100 int index;
3101
3102 inode = pages[0]->mapping->host;
3103 while (1) {
3104 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003105 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Miao Xie99740902013-07-25 19:22:36 +08003106 end - start + 1);
3107 if (!ordered)
3108 break;
3109 unlock_extent(tree, start, end);
3110 btrfs_start_ordered_extent(inode, ordered, 1);
3111 btrfs_put_ordered_extent(ordered);
3112 }
3113
3114 for (index = 0; index < nr_pages; index++) {
David Sterba4ef77692017-06-23 04:09:57 +02003115 __do_readpage(tree, pages[index], btrfs_get_extent, em_cached,
Jens Axboe5e9d3982018-08-17 15:45:39 -07003116 bio, 0, bio_flags, REQ_RAHEAD, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003117 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003118 }
3119}
3120
Miao Xie99740902013-07-25 19:22:36 +08003121static int __extent_read_full_page(struct extent_io_tree *tree,
3122 struct page *page,
3123 get_extent_t *get_extent,
3124 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003125 unsigned long *bio_flags,
3126 unsigned int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003127{
3128 struct inode *inode = page->mapping->host;
3129 struct btrfs_ordered_extent *ordered;
3130 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003131 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003132 int ret;
3133
3134 while (1) {
3135 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003136 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003137 PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003138 if (!ordered)
3139 break;
3140 unlock_extent(tree, start, end);
3141 btrfs_start_ordered_extent(inode, ordered, 1);
3142 btrfs_put_ordered_extent(ordered);
3143 }
3144
Miao Xie125bac012013-07-25 19:22:37 +08003145 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003146 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003147 return ret;
3148}
3149
Chris Masond1310b22008-01-24 16:13:08 -05003150int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003151 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003152{
3153 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003154 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003155 int ret;
3156
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003157 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003158 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003159 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003160 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003161 return ret;
3162}
Chris Masond1310b22008-01-24 16:13:08 -05003163
David Sterba3d4b9492017-02-10 19:33:41 +01003164static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003165 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003166{
3167 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003168}
3169
Chris Masond1310b22008-01-24 16:13:08 -05003170/*
Chris Mason40f76582014-05-21 13:35:51 -07003171 * helper for __extent_writepage, doing all of the delayed allocation setup.
3172 *
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003173 * This returns 1 if btrfs_run_delalloc_range function did all the work required
Chris Mason40f76582014-05-21 13:35:51 -07003174 * to write the page (copy into inline extent). In this case the IO has
3175 * been started and the page is already unlocked.
3176 *
3177 * This returns 0 if all went well (page still locked)
3178 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003179 */
Chris Mason40f76582014-05-21 13:35:51 -07003180static noinline_for_stack int writepage_delalloc(struct inode *inode,
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003181 struct page *page, struct writeback_control *wbc,
3182 u64 delalloc_start, unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003183{
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003184 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003185 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Lu Fengqi3522e902018-11-29 11:33:38 +08003186 bool found;
Chris Mason40f76582014-05-21 13:35:51 -07003187 u64 delalloc_to_write = 0;
3188 u64 delalloc_end = 0;
3189 int ret;
3190 int page_started = 0;
3191
Chris Mason40f76582014-05-21 13:35:51 -07003192
3193 while (delalloc_end < page_end) {
Lu Fengqi3522e902018-11-29 11:33:38 +08003194 found = find_lock_delalloc_range(inode, tree,
Chris Mason40f76582014-05-21 13:35:51 -07003195 page,
3196 &delalloc_start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03003197 &delalloc_end);
Lu Fengqi3522e902018-11-29 11:33:38 +08003198 if (!found) {
Chris Mason40f76582014-05-21 13:35:51 -07003199 delalloc_start = delalloc_end + 1;
3200 continue;
3201 }
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003202 ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
3203 delalloc_end, &page_started, nr_written, wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003204 /* File system has been set read-only */
3205 if (ret) {
3206 SetPageError(page);
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003207 /*
3208 * btrfs_run_delalloc_range should return < 0 for error
3209 * but just in case, we use > 0 here meaning the IO is
3210 * started, so we don't want to return > 0 unless
3211 * things are going well.
Chris Mason40f76582014-05-21 13:35:51 -07003212 */
3213 ret = ret < 0 ? ret : -EIO;
3214 goto done;
3215 }
3216 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003217 * delalloc_end is already one less than the total length, so
3218 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003219 */
3220 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003221 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003222 delalloc_start = delalloc_end + 1;
3223 }
3224 if (wbc->nr_to_write < delalloc_to_write) {
3225 int thresh = 8192;
3226
3227 if (delalloc_to_write < thresh * 2)
3228 thresh = delalloc_to_write;
3229 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3230 thresh);
3231 }
3232
3233 /* did the fill delalloc function already unlock and start
3234 * the IO?
3235 */
3236 if (page_started) {
3237 /*
3238 * we've unlocked the page, so we can't update
3239 * the mapping's writeback index, just update
3240 * nr_to_write.
3241 */
3242 wbc->nr_to_write -= *nr_written;
3243 return 1;
3244 }
3245
3246 ret = 0;
3247
3248done:
3249 return ret;
3250}
3251
3252/*
3253 * helper for __extent_writepage. This calls the writepage start hooks,
3254 * and does the loop to map the page into extents and bios.
3255 *
3256 * We return 1 if the IO is started and the page is unlocked,
3257 * 0 if all went well (page still locked)
3258 * < 0 if there were errors (page still locked)
3259 */
3260static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3261 struct page *page,
3262 struct writeback_control *wbc,
3263 struct extent_page_data *epd,
3264 loff_t i_size,
3265 unsigned long nr_written,
David Sterbaf1c77c52017-06-06 19:03:49 +02003266 unsigned int write_flags, int *nr_ret)
Chris Mason40f76582014-05-21 13:35:51 -07003267{
Chris Masond1310b22008-01-24 16:13:08 -05003268 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003269 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003270 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003271 u64 end;
3272 u64 cur = start;
3273 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003274 u64 block_start;
3275 u64 iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003276 struct extent_map *em;
3277 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003278 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003279 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003280 int ret = 0;
3281 int nr = 0;
3282 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003283
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003284 ret = btrfs_writepage_cow_fixup(page, start, page_end);
3285 if (ret) {
3286 /* Fixup worker will requeue */
3287 if (ret == -EBUSY)
3288 wbc->pages_skipped++;
3289 else
3290 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003291
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003292 update_nr_written(wbc, nr_written);
3293 unlock_page(page);
3294 return 1;
Chris Mason247e7432008-07-17 12:53:51 -04003295 }
3296
Chris Mason11c83492009-04-20 15:50:09 -04003297 /*
3298 * we don't want to touch the inode after unlocking the page,
3299 * so we update the mapping writeback index now
3300 */
David Sterba3d4b9492017-02-10 19:33:41 +01003301 update_nr_written(wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003302
Chris Masond1310b22008-01-24 16:13:08 -05003303 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003304 if (i_size <= start) {
Nikolay Borisovc6297322018-11-08 10:18:08 +02003305 btrfs_writepage_endio_finish_ordered(page, start, page_end, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003306 goto done;
3307 }
3308
Chris Masond1310b22008-01-24 16:13:08 -05003309 blocksize = inode->i_sb->s_blocksize;
3310
3311 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003312 u64 em_end;
David Sterba6273b7f2017-10-04 17:30:11 +02003313 u64 offset;
David Sterba58409ed2016-05-04 11:46:10 +02003314
Chris Mason40f76582014-05-21 13:35:51 -07003315 if (cur >= i_size) {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003316 btrfs_writepage_endio_finish_ordered(page, cur,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003317 page_end, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003318 break;
3319 }
David Sterba3c98c622017-06-23 04:01:08 +02003320 em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003321 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003322 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003323 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003324 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003325 break;
3326 }
3327
3328 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003329 em_end = extent_map_end(em);
3330 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003331 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003332 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003333 iosize = ALIGN(iosize, blocksize);
David Sterba6273b7f2017-10-04 17:30:11 +02003334 offset = em->block_start + extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003335 bdev = em->bdev;
3336 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003337 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003338 free_extent_map(em);
3339 em = NULL;
3340
Chris Masonc8b97812008-10-29 14:49:59 -04003341 /*
3342 * compressed and inline extents are written through other
3343 * paths in the FS
3344 */
3345 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003346 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003347 /*
3348 * end_io notification does not happen here for
3349 * compressed extents
3350 */
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003351 if (!compressed)
3352 btrfs_writepage_endio_finish_ordered(page, cur,
3353 cur + iosize - 1,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003354 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003355 else if (compressed) {
3356 /* we don't want to end_page_writeback on
3357 * a compressed extent. this happens
3358 * elsewhere
3359 */
3360 nr++;
3361 }
3362
3363 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003364 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003365 continue;
3366 }
Chris Masonc8b97812008-10-29 14:49:59 -04003367
David Sterba5cdc84b2018-07-18 20:32:52 +02003368 btrfs_set_range_writeback(tree, cur, cur + iosize - 1);
David Sterba58409ed2016-05-04 11:46:10 +02003369 if (!PageWriteback(page)) {
3370 btrfs_err(BTRFS_I(inode)->root->fs_info,
3371 "page %lu not writeback, cur %llu end %llu",
3372 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003373 }
David Sterba58409ed2016-05-04 11:46:10 +02003374
David Sterba4b81ba42017-06-06 19:14:26 +02003375 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003376 page, offset, iosize, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003377 bdev, &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003378 end_bio_extent_writepage,
3379 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003380 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003381 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003382 if (PageWriteback(page))
3383 end_page_writeback(page);
3384 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003385
Chris Masond1310b22008-01-24 16:13:08 -05003386 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003387 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003388 nr++;
3389 }
3390done:
Chris Mason40f76582014-05-21 13:35:51 -07003391 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003392 return ret;
3393}
3394
3395/*
3396 * the writepage semantics are similar to regular writepage. extent
3397 * records are inserted to lock ranges in the tree, and as dirty areas
3398 * are found, they are marked writeback. Then the lock bits are removed
3399 * and the end_io handler clears the writeback ranges
3400 */
3401static int __extent_writepage(struct page *page, struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003402 struct extent_page_data *epd)
Chris Mason40f76582014-05-21 13:35:51 -07003403{
3404 struct inode *inode = page->mapping->host;
Chris Mason40f76582014-05-21 13:35:51 -07003405 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003406 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003407 int ret;
3408 int nr = 0;
3409 size_t pg_offset = 0;
3410 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003411 unsigned long end_index = i_size >> PAGE_SHIFT;
David Sterbaf1c77c52017-06-06 19:03:49 +02003412 unsigned int write_flags = 0;
Chris Mason40f76582014-05-21 13:35:51 -07003413 unsigned long nr_written = 0;
3414
Liu Boff40adf2017-08-24 18:19:48 -06003415 write_flags = wbc_to_write_flags(wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003416
3417 trace___extent_writepage(page, inode, wbc);
3418
3419 WARN_ON(!PageLocked(page));
3420
3421 ClearPageError(page);
3422
Johannes Thumshirn70730172018-12-05 15:23:03 +01003423 pg_offset = offset_in_page(i_size);
Chris Mason40f76582014-05-21 13:35:51 -07003424 if (page->index > end_index ||
3425 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003426 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003427 unlock_page(page);
3428 return 0;
3429 }
3430
3431 if (page->index == end_index) {
3432 char *userpage;
3433
3434 userpage = kmap_atomic(page);
3435 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003436 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003437 kunmap_atomic(userpage);
3438 flush_dcache_page(page);
3439 }
3440
3441 pg_offset = 0;
3442
3443 set_page_extent_mapped(page);
3444
Nikolay Borisov7789a552018-11-08 10:18:06 +02003445 if (!epd->extent_locked) {
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003446 ret = writepage_delalloc(inode, page, wbc, start, &nr_written);
Nikolay Borisov7789a552018-11-08 10:18:06 +02003447 if (ret == 1)
3448 goto done_unlocked;
3449 if (ret)
3450 goto done;
3451 }
Chris Mason40f76582014-05-21 13:35:51 -07003452
3453 ret = __extent_writepage_io(inode, page, wbc, epd,
3454 i_size, nr_written, write_flags, &nr);
3455 if (ret == 1)
3456 goto done_unlocked;
3457
3458done:
Chris Masond1310b22008-01-24 16:13:08 -05003459 if (nr == 0) {
3460 /* make sure the mapping tag for page dirty gets cleared */
3461 set_page_writeback(page);
3462 end_page_writeback(page);
3463 }
Filipe Manana61391d52014-05-09 17:17:40 +01003464 if (PageError(page)) {
3465 ret = ret < 0 ? ret : -EIO;
3466 end_extent_writepage(page, ret, start, page_end);
3467 }
Chris Masond1310b22008-01-24 16:13:08 -05003468 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003469 return ret;
Chris Mason771ed682008-11-06 22:02:51 -05003470
Chris Mason11c83492009-04-20 15:50:09 -04003471done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003472 return 0;
3473}
3474
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003475void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003476{
NeilBrown74316202014-07-07 15:16:04 +10003477 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3478 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003479}
3480
Chris Mason0e378df2014-05-19 20:55:27 -07003481static noinline_for_stack int
3482lock_extent_buffer_for_io(struct extent_buffer *eb,
3483 struct btrfs_fs_info *fs_info,
3484 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003485{
David Sterbacc5e31a2018-03-01 18:20:27 +01003486 int i, num_pages;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003487 int flush = 0;
3488 int ret = 0;
3489
3490 if (!btrfs_try_tree_write_lock(eb)) {
3491 flush = 1;
Qu Wenruof4340622019-03-20 14:27:41 +08003492 ret = flush_write_bio(epd);
3493 BUG_ON(ret < 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003494 btrfs_tree_lock(eb);
3495 }
3496
3497 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3498 btrfs_tree_unlock(eb);
3499 if (!epd->sync_io)
3500 return 0;
3501 if (!flush) {
Qu Wenruof4340622019-03-20 14:27:41 +08003502 ret = flush_write_bio(epd);
3503 BUG_ON(ret < 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003504 flush = 1;
3505 }
Chris Masona098d8e82012-03-21 12:09:56 -04003506 while (1) {
3507 wait_on_extent_buffer_writeback(eb);
3508 btrfs_tree_lock(eb);
3509 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3510 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003511 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003512 }
3513 }
3514
Josef Bacik51561ff2012-07-20 16:25:24 -04003515 /*
3516 * We need to do this to prevent races in people who check if the eb is
3517 * under IO since we can end up having no IO bits set for a short period
3518 * of time.
3519 */
3520 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003521 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3522 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003523 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003524 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Nikolay Borisov104b4e52017-06-20 21:01:20 +03003525 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3526 -eb->len,
3527 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003528 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003529 } else {
3530 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003531 }
3532
3533 btrfs_tree_unlock(eb);
3534
3535 if (!ret)
3536 return ret;
3537
David Sterba65ad0102018-06-29 10:56:49 +02003538 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003539 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003540 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003541
3542 if (!trylock_page(p)) {
3543 if (!flush) {
Qu Wenruof4340622019-03-20 14:27:41 +08003544 ret = flush_write_bio(epd);
3545 BUG_ON(ret < 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003546 flush = 1;
3547 }
3548 lock_page(p);
3549 }
3550 }
3551
3552 return ret;
3553}
3554
3555static void end_extent_buffer_writeback(struct extent_buffer *eb)
3556{
3557 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003558 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003559 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3560}
3561
Filipe Manana656f30d2014-09-26 12:25:56 +01003562static void set_btree_ioerr(struct page *page)
3563{
3564 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Filipe Manana656f30d2014-09-26 12:25:56 +01003565
3566 SetPageError(page);
3567 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3568 return;
3569
3570 /*
3571 * If writeback for a btree extent that doesn't belong to a log tree
3572 * failed, increment the counter transaction->eb_write_errors.
3573 * We do this because while the transaction is running and before it's
3574 * committing (when we call filemap_fdata[write|wait]_range against
3575 * the btree inode), we might have
3576 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3577 * returns an error or an error happens during writeback, when we're
3578 * committing the transaction we wouldn't know about it, since the pages
3579 * can be no longer dirty nor marked anymore for writeback (if a
3580 * subsequent modification to the extent buffer didn't happen before the
3581 * transaction commit), which makes filemap_fdata[write|wait]_range not
3582 * able to find the pages tagged with SetPageError at transaction
3583 * commit time. So if this happens we must abort the transaction,
3584 * otherwise we commit a super block with btree roots that point to
3585 * btree nodes/leafs whose content on disk is invalid - either garbage
3586 * or the content of some node/leaf from a past generation that got
3587 * cowed or deleted and is no longer valid.
3588 *
3589 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3590 * not be enough - we need to distinguish between log tree extents vs
3591 * non-log tree extents, and the next filemap_fdatawait_range() call
3592 * will catch and clear such errors in the mapping - and that call might
3593 * be from a log sync and not from a transaction commit. Also, checking
3594 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3595 * not done and would not be reliable - the eb might have been released
3596 * from memory and reading it back again means that flag would not be
3597 * set (since it's a runtime flag, not persisted on disk).
3598 *
3599 * Using the flags below in the btree inode also makes us achieve the
3600 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3601 * writeback for all dirty pages and before filemap_fdatawait_range()
3602 * is called, the writeback for all dirty pages had already finished
3603 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3604 * filemap_fdatawait_range() would return success, as it could not know
3605 * that writeback errors happened (the pages were no longer tagged for
3606 * writeback).
3607 */
3608 switch (eb->log_index) {
3609 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003610 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003611 break;
3612 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003613 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003614 break;
3615 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003616 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003617 break;
3618 default:
3619 BUG(); /* unexpected, logic error */
3620 }
3621}
3622
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003623static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003624{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003625 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003626 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003627 int i, done;
Ming Lei6dc4f102019-02-15 19:13:19 +08003628 struct bvec_iter_all iter_all;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003629
David Sterbac09abff2017-07-13 18:10:07 +02003630 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08003631 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003632 struct page *page = bvec->bv_page;
3633
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003634 eb = (struct extent_buffer *)page->private;
3635 BUG_ON(!eb);
3636 done = atomic_dec_and_test(&eb->io_pages);
3637
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003638 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003639 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003640 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003641 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003642 }
3643
3644 end_page_writeback(page);
3645
3646 if (!done)
3647 continue;
3648
3649 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003650 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003651
3652 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003653}
3654
Chris Mason0e378df2014-05-19 20:55:27 -07003655static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003656 struct btrfs_fs_info *fs_info,
3657 struct writeback_control *wbc,
3658 struct extent_page_data *epd)
3659{
3660 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003661 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003662 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003663 u32 nritems;
David Sterbacc5e31a2018-03-01 18:20:27 +01003664 int i, num_pages;
Liu Bo851cd172016-09-23 13:44:44 -07003665 unsigned long start, end;
Liu Boff40adf2017-08-24 18:19:48 -06003666 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003667 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003668
Filipe Manana656f30d2014-09-26 12:25:56 +01003669 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02003670 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003671 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003672
Liu Bo851cd172016-09-23 13:44:44 -07003673 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3674 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003675 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003676 end = btrfs_node_key_ptr_offset(nritems);
3677
David Sterbab159fa22016-11-08 18:09:03 +01003678 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003679 } else {
3680 /*
3681 * leaf:
3682 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3683 */
3684 start = btrfs_item_nr_offset(nritems);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003685 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, eb);
David Sterbab159fa22016-11-08 18:09:03 +01003686 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003687 }
3688
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003689 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003690 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003691
3692 clear_page_dirty_for_io(p);
3693 set_page_writeback(p);
David Sterba4b81ba42017-06-06 19:14:26 +02003694 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003695 p, offset, PAGE_SIZE, 0, bdev,
David Sterbac2df8bb2017-02-10 19:29:38 +01003696 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003697 end_bio_extent_buffer_writepage,
Liu Bo18fdc672017-09-13 12:18:22 -06003698 0, 0, 0, false);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003699 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003700 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003701 if (PageWriteback(p))
3702 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003703 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3704 end_extent_buffer_writeback(eb);
3705 ret = -EIO;
3706 break;
3707 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003708 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003709 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003710 unlock_page(p);
3711 }
3712
3713 if (unlikely(ret)) {
3714 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003715 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003716 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003717 unlock_page(p);
3718 }
3719 }
3720
3721 return ret;
3722}
3723
3724int btree_write_cache_pages(struct address_space *mapping,
3725 struct writeback_control *wbc)
3726{
3727 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3728 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3729 struct extent_buffer *eb, *prev_eb = NULL;
3730 struct extent_page_data epd = {
3731 .bio = NULL,
3732 .tree = tree,
3733 .extent_locked = 0,
3734 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3735 };
3736 int ret = 0;
Qu Wenruof4340622019-03-20 14:27:41 +08003737 int flush_ret;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003738 int done = 0;
3739 int nr_to_write_done = 0;
3740 struct pagevec pvec;
3741 int nr_pages;
3742 pgoff_t index;
3743 pgoff_t end; /* Inclusive */
3744 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003745 xa_mark_t tag;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003746
Mel Gorman86679822017-11-15 17:37:52 -08003747 pagevec_init(&pvec);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003748 if (wbc->range_cyclic) {
3749 index = mapping->writeback_index; /* Start from prev offset */
3750 end = -1;
3751 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003752 index = wbc->range_start >> PAGE_SHIFT;
3753 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003754 scanned = 1;
3755 }
3756 if (wbc->sync_mode == WB_SYNC_ALL)
3757 tag = PAGECACHE_TAG_TOWRITE;
3758 else
3759 tag = PAGECACHE_TAG_DIRTY;
3760retry:
3761 if (wbc->sync_mode == WB_SYNC_ALL)
3762 tag_pages_for_writeback(mapping, index, end);
3763 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara4006f432017-11-15 17:34:37 -08003764 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08003765 tag))) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003766 unsigned i;
3767
3768 scanned = 1;
3769 for (i = 0; i < nr_pages; i++) {
3770 struct page *page = pvec.pages[i];
3771
3772 if (!PagePrivate(page))
3773 continue;
3774
Josef Bacikb5bae262012-09-14 13:43:01 -04003775 spin_lock(&mapping->private_lock);
3776 if (!PagePrivate(page)) {
3777 spin_unlock(&mapping->private_lock);
3778 continue;
3779 }
3780
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003781 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003782
3783 /*
3784 * Shouldn't happen and normally this would be a BUG_ON
3785 * but no sense in crashing the users box for something
3786 * we can survive anyway.
3787 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303788 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003789 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003790 continue;
3791 }
3792
Josef Bacikb5bae262012-09-14 13:43:01 -04003793 if (eb == prev_eb) {
3794 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003795 continue;
3796 }
3797
Josef Bacikb5bae262012-09-14 13:43:01 -04003798 ret = atomic_inc_not_zero(&eb->refs);
3799 spin_unlock(&mapping->private_lock);
3800 if (!ret)
3801 continue;
3802
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003803 prev_eb = eb;
3804 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3805 if (!ret) {
3806 free_extent_buffer(eb);
3807 continue;
3808 }
3809
3810 ret = write_one_eb(eb, fs_info, wbc, &epd);
3811 if (ret) {
3812 done = 1;
3813 free_extent_buffer(eb);
3814 break;
3815 }
3816 free_extent_buffer(eb);
3817
3818 /*
3819 * the filesystem may choose to bump up nr_to_write.
3820 * We have to make sure to honor the new nr_to_write
3821 * at any time
3822 */
3823 nr_to_write_done = wbc->nr_to_write <= 0;
3824 }
3825 pagevec_release(&pvec);
3826 cond_resched();
3827 }
3828 if (!scanned && !done) {
3829 /*
3830 * We hit the last page and there is more work to be done: wrap
3831 * back to the start of the file
3832 */
3833 scanned = 1;
3834 index = 0;
3835 goto retry;
3836 }
Qu Wenruof4340622019-03-20 14:27:41 +08003837 flush_ret = flush_write_bio(&epd);
3838 BUG_ON(flush_ret < 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003839 return ret;
3840}
3841
Chris Masond1310b22008-01-24 16:13:08 -05003842/**
Chris Mason4bef0842008-09-08 11:18:08 -04003843 * 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 -05003844 * @mapping: address space structure to write
3845 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
David Sterba935db852017-06-23 04:30:28 +02003846 * @data: data passed to __extent_writepage function
Chris Masond1310b22008-01-24 16:13:08 -05003847 *
3848 * If a page is already under I/O, write_cache_pages() skips it, even
3849 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3850 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3851 * and msync() need to guarantee that all the data which was dirty at the time
3852 * the call was made get new I/O started against them. If wbc->sync_mode is
3853 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3854 * existing IO to complete.
3855 */
David Sterba4242b642017-02-10 19:38:24 +01003856static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04003857 struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003858 struct extent_page_data *epd)
Chris Masond1310b22008-01-24 16:13:08 -05003859{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003860 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003861 int ret = 0;
3862 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003863 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003864 struct pagevec pvec;
3865 int nr_pages;
3866 pgoff_t index;
3867 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08003868 pgoff_t done_index;
3869 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003870 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003871 xa_mark_t tag;
Chris Masond1310b22008-01-24 16:13:08 -05003872
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003873 /*
3874 * We have to hold onto the inode so that ordered extents can do their
3875 * work when the IO finishes. The alternative to this is failing to add
3876 * an ordered extent if the igrab() fails there and that is a huge pain
3877 * to deal with, so instead just hold onto the inode throughout the
3878 * writepages operation. If it fails here we are freeing up the inode
3879 * anyway and we'd rather not waste our time writing out stuff that is
3880 * going to be truncated anyway.
3881 */
3882 if (!igrab(inode))
3883 return 0;
3884
Mel Gorman86679822017-11-15 17:37:52 -08003885 pagevec_init(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05003886 if (wbc->range_cyclic) {
3887 index = mapping->writeback_index; /* Start from prev offset */
3888 end = -1;
3889 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003890 index = wbc->range_start >> PAGE_SHIFT;
3891 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08003892 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3893 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05003894 scanned = 1;
3895 }
Ethan Lien3cd24c62018-11-01 14:49:03 +08003896
3897 /*
3898 * We do the tagged writepage as long as the snapshot flush bit is set
3899 * and we are the first one who do the filemap_flush() on this inode.
3900 *
3901 * The nr_to_write == LONG_MAX is needed to make sure other flushers do
3902 * not race in and drop the bit.
3903 */
3904 if (range_whole && wbc->nr_to_write == LONG_MAX &&
3905 test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
3906 &BTRFS_I(inode)->runtime_flags))
3907 wbc->tagged_writepages = 1;
3908
3909 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00003910 tag = PAGECACHE_TAG_TOWRITE;
3911 else
3912 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003913retry:
Ethan Lien3cd24c62018-11-01 14:49:03 +08003914 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00003915 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08003916 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003917 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara67fd7072017-11-15 17:35:19 -08003918 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
3919 &index, end, tag))) {
Chris Masond1310b22008-01-24 16:13:08 -05003920 unsigned i;
3921
3922 scanned = 1;
3923 for (i = 0; i < nr_pages; i++) {
3924 struct page *page = pvec.pages[i];
3925
Liu Boa91326672016-03-07 16:56:21 -08003926 done_index = page->index;
Chris Masond1310b22008-01-24 16:13:08 -05003927 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07003928 * At this point we hold neither the i_pages lock nor
3929 * the page lock: the page may be truncated or
3930 * invalidated (changing page->mapping to NULL),
3931 * or even swizzled back from swapper_space to
3932 * tmpfs file mapping
Chris Masond1310b22008-01-24 16:13:08 -05003933 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003934 if (!trylock_page(page)) {
Qu Wenruof4340622019-03-20 14:27:41 +08003935 ret = flush_write_bio(epd);
3936 BUG_ON(ret < 0);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003937 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003938 }
Chris Masond1310b22008-01-24 16:13:08 -05003939
3940 if (unlikely(page->mapping != mapping)) {
3941 unlock_page(page);
3942 continue;
3943 }
3944
Chris Masond2c3f4f2008-11-19 12:44:22 -05003945 if (wbc->sync_mode != WB_SYNC_NONE) {
Qu Wenruof4340622019-03-20 14:27:41 +08003946 if (PageWriteback(page)) {
3947 ret = flush_write_bio(epd);
3948 BUG_ON(ret < 0);
3949 }
Chris Masond1310b22008-01-24 16:13:08 -05003950 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003951 }
Chris Masond1310b22008-01-24 16:13:08 -05003952
3953 if (PageWriteback(page) ||
3954 !clear_page_dirty_for_io(page)) {
3955 unlock_page(page);
3956 continue;
3957 }
3958
David Sterbaaab6e9e2017-11-30 18:00:02 +01003959 ret = __extent_writepage(page, wbc, epd);
Chris Masond1310b22008-01-24 16:13:08 -05003960
3961 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3962 unlock_page(page);
3963 ret = 0;
3964 }
Liu Boa91326672016-03-07 16:56:21 -08003965 if (ret < 0) {
3966 /*
3967 * done_index is set past this page,
3968 * so media errors will not choke
3969 * background writeout for the entire
3970 * file. This has consequences for
3971 * range_cyclic semantics (ie. it may
3972 * not be suitable for data integrity
3973 * writeout).
3974 */
3975 done_index = page->index + 1;
3976 done = 1;
3977 break;
3978 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003979
3980 /*
3981 * the filesystem may choose to bump up nr_to_write.
3982 * We have to make sure to honor the new nr_to_write
3983 * at any time
3984 */
3985 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003986 }
3987 pagevec_release(&pvec);
3988 cond_resched();
3989 }
Liu Bo894b36e2016-03-07 16:56:22 -08003990 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05003991 /*
3992 * We hit the last page and there is more work to be done: wrap
3993 * back to the start of the file
3994 */
3995 scanned = 1;
3996 index = 0;
3997 goto retry;
3998 }
Liu Boa91326672016-03-07 16:56:21 -08003999
4000 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4001 mapping->writeback_index = done_index;
4002
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004003 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004004 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004005}
Chris Masond1310b22008-01-24 16:13:08 -05004006
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004007int extent_write_full_page(struct page *page, struct writeback_control *wbc)
Chris Masond1310b22008-01-24 16:13:08 -05004008{
4009 int ret;
Qu Wenruof4340622019-03-20 14:27:41 +08004010 int flush_ret;
Chris Masond1310b22008-01-24 16:13:08 -05004011 struct extent_page_data epd = {
4012 .bio = NULL,
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004013 .tree = &BTRFS_I(page->mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004014 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004015 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004016 };
Chris Masond1310b22008-01-24 16:13:08 -05004017
Chris Masond1310b22008-01-24 16:13:08 -05004018 ret = __extent_writepage(page, wbc, &epd);
4019
Qu Wenruof4340622019-03-20 14:27:41 +08004020 flush_ret = flush_write_bio(&epd);
4021 BUG_ON(flush_ret < 0);
Chris Masond1310b22008-01-24 16:13:08 -05004022 return ret;
4023}
Chris Masond1310b22008-01-24 16:13:08 -05004024
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004025int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
Chris Mason771ed682008-11-06 22:02:51 -05004026 int mode)
4027{
4028 int ret = 0;
Qu Wenruof4340622019-03-20 14:27:41 +08004029 int flush_ret;
Chris Mason771ed682008-11-06 22:02:51 -05004030 struct address_space *mapping = inode->i_mapping;
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004031 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Mason771ed682008-11-06 22:02:51 -05004032 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004033 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4034 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004035
4036 struct extent_page_data epd = {
4037 .bio = NULL,
4038 .tree = tree,
Chris Mason771ed682008-11-06 22:02:51 -05004039 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004040 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05004041 };
4042 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004043 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004044 .nr_to_write = nr_pages * 2,
4045 .range_start = start,
4046 .range_end = end + 1,
4047 };
4048
Chris Masond3977122009-01-05 21:25:51 -05004049 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004050 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004051 if (clear_page_dirty_for_io(page))
4052 ret = __extent_writepage(page, &wbc_writepages, &epd);
4053 else {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02004054 btrfs_writepage_endio_finish_ordered(page, start,
Nikolay Borisovc6297322018-11-08 10:18:08 +02004055 start + PAGE_SIZE - 1, 1);
Chris Mason771ed682008-11-06 22:02:51 -05004056 unlock_page(page);
4057 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004058 put_page(page);
4059 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004060 }
4061
Qu Wenruof4340622019-03-20 14:27:41 +08004062 flush_ret = flush_write_bio(&epd);
4063 BUG_ON(flush_ret < 0);
Chris Mason771ed682008-11-06 22:02:51 -05004064 return ret;
4065}
Chris Masond1310b22008-01-24 16:13:08 -05004066
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004067int extent_writepages(struct address_space *mapping,
Chris Masond1310b22008-01-24 16:13:08 -05004068 struct writeback_control *wbc)
4069{
4070 int ret = 0;
Qu Wenruof4340622019-03-20 14:27:41 +08004071 int flush_ret;
Chris Masond1310b22008-01-24 16:13:08 -05004072 struct extent_page_data epd = {
4073 .bio = NULL,
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004074 .tree = &BTRFS_I(mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004075 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004076 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004077 };
4078
David Sterba935db852017-06-23 04:30:28 +02004079 ret = extent_write_cache_pages(mapping, wbc, &epd);
Qu Wenruof4340622019-03-20 14:27:41 +08004080 flush_ret = flush_write_bio(&epd);
4081 BUG_ON(flush_ret < 0);
Chris Masond1310b22008-01-24 16:13:08 -05004082 return ret;
4083}
Chris Masond1310b22008-01-24 16:13:08 -05004084
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004085int extent_readpages(struct address_space *mapping, struct list_head *pages,
4086 unsigned nr_pages)
Chris Masond1310b22008-01-24 16:13:08 -05004087{
4088 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004089 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004090 struct page *pagepool[16];
Miao Xie125bac012013-07-25 19:22:37 +08004091 struct extent_map *em_cached = NULL;
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004092 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Liu Bo67c96842012-07-20 21:43:09 -06004093 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004094 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004095
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004096 while (!list_empty(pages)) {
Nikolay Borisove65ef212019-03-11 09:55:38 +02004097 u64 contig_end = 0;
4098
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004099 for (nr = 0; nr < ARRAY_SIZE(pagepool) && !list_empty(pages);) {
Nikolay Borisovf86196e2019-01-03 15:29:02 -08004100 struct page *page = lru_to_page(pages);
Chris Masond1310b22008-01-24 16:13:08 -05004101
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004102 prefetchw(&page->flags);
4103 list_del(&page->lru);
4104 if (add_to_page_cache_lru(page, mapping, page->index,
4105 readahead_gfp_mask(mapping))) {
4106 put_page(page);
Nikolay Borisove65ef212019-03-11 09:55:38 +02004107 break;
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004108 }
4109
4110 pagepool[nr++] = page;
Nikolay Borisove65ef212019-03-11 09:55:38 +02004111 contig_end = page_offset(page) + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004112 }
Liu Bo67c96842012-07-20 21:43:09 -06004113
Nikolay Borisove65ef212019-03-11 09:55:38 +02004114 if (nr) {
4115 u64 contig_start = page_offset(pagepool[0]);
4116
4117 ASSERT(contig_start + nr * PAGE_SIZE - 1 == contig_end);
4118
4119 contiguous_readpages(tree, pagepool, nr, contig_start,
4120 contig_end, &em_cached, &bio, &bio_flags,
4121 &prev_em_start);
4122 }
Chris Masond1310b22008-01-24 16:13:08 -05004123 }
Liu Bo67c96842012-07-20 21:43:09 -06004124
Miao Xie125bac012013-07-25 19:22:37 +08004125 if (em_cached)
4126 free_extent_map(em_cached);
4127
Chris Masond1310b22008-01-24 16:13:08 -05004128 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004129 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004130 return 0;
4131}
Chris Masond1310b22008-01-24 16:13:08 -05004132
4133/*
4134 * basic invalidatepage code, this waits on any locked or writeback
4135 * ranges corresponding to the page, and then deletes any extent state
4136 * records from the tree
4137 */
4138int extent_invalidatepage(struct extent_io_tree *tree,
4139 struct page *page, unsigned long offset)
4140{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004141 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004142 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004143 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004144 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4145
Qu Wenruofda28322013-02-26 08:10:22 +00004146 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004147 if (start > end)
4148 return 0;
4149
David Sterbaff13db42015-12-03 14:30:40 +01004150 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004151 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004152 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004153 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4154 EXTENT_DO_ACCOUNTING,
David Sterbaae0f1622017-10-31 16:37:52 +01004155 1, 1, &cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05004156 return 0;
4157}
Chris Masond1310b22008-01-24 16:13:08 -05004158
4159/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004160 * a helper for releasepage, this tests for areas of the page that
4161 * are locked or under IO and drops the related state bits if it is safe
4162 * to drop the page.
4163 */
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004164static int try_release_extent_state(struct extent_io_tree *tree,
Eric Sandeen48a3b632013-04-25 20:41:01 +00004165 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004166{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004167 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004168 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004169 int ret = 1;
4170
Nikolay Borisov88826792019-03-14 15:28:31 +02004171 if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) {
Chris Mason7b13b7b2008-04-18 10:29:50 -04004172 ret = 0;
Nikolay Borisov88826792019-03-14 15:28:31 +02004173 } else {
Chris Mason11ef1602009-09-23 20:28:46 -04004174 /*
4175 * at this point we can safely clear everything except the
4176 * locked bit and the nodatasum bit
4177 */
David Sterba66b0c882017-10-31 16:30:47 +01004178 ret = __clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004179 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
David Sterba66b0c882017-10-31 16:30:47 +01004180 0, 0, NULL, mask, NULL);
Chris Masone3f24cc2011-02-14 12:52:08 -05004181
4182 /* if clear_extent_bit failed for enomem reasons,
4183 * we can't allow the release to continue.
4184 */
4185 if (ret < 0)
4186 ret = 0;
4187 else
4188 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004189 }
4190 return ret;
4191}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004192
4193/*
Chris Masond1310b22008-01-24 16:13:08 -05004194 * a helper for releasepage. As long as there are no locked extents
4195 * in the range corresponding to the page, both state records and extent
4196 * map records are removed
4197 */
Nikolay Borisov477a30b2018-04-19 10:46:34 +03004198int try_release_extent_mapping(struct page *page, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004199{
4200 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004201 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004202 u64 end = start + PAGE_SIZE - 1;
Filipe Mananabd3599a2018-07-12 01:36:43 +01004203 struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
4204 struct extent_io_tree *tree = &btrfs_inode->io_tree;
4205 struct extent_map_tree *map = &btrfs_inode->extent_tree;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004206
Mel Gormand0164ad2015-11-06 16:28:21 -08004207 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004208 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004209 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004210 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004211 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004212 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004213 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004214 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004215 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004216 break;
4217 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004218 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4219 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004220 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004221 free_extent_map(em);
4222 break;
4223 }
4224 if (!test_range_bit(tree, em->start,
4225 extent_map_end(em) - 1,
Nikolay Borisov4e586ca2019-03-14 15:28:30 +02004226 EXTENT_LOCKED, 0, NULL)) {
Filipe Mananabd3599a2018-07-12 01:36:43 +01004227 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4228 &btrfs_inode->runtime_flags);
Chris Mason70dec802008-01-29 09:59:12 -05004229 remove_extent_mapping(map, em);
4230 /* once for the rb tree */
4231 free_extent_map(em);
4232 }
4233 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004234 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004235
4236 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004237 free_extent_map(em);
4238 }
Chris Masond1310b22008-01-24 16:13:08 -05004239 }
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004240 return try_release_extent_state(tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004241}
Chris Masond1310b22008-01-24 16:13:08 -05004242
Chris Masonec29ed52011-02-23 16:23:20 -05004243/*
4244 * helper function for fiemap, which doesn't want to see any holes.
4245 * This maps until we find something past 'last'
4246 */
4247static struct extent_map *get_extent_skip_holes(struct inode *inode,
David Sterbae3350e12017-06-23 04:09:57 +02004248 u64 offset, u64 last)
Chris Masonec29ed52011-02-23 16:23:20 -05004249{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004250 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004251 struct extent_map *em;
4252 u64 len;
4253
4254 if (offset >= last)
4255 return NULL;
4256
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304257 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004258 len = last - offset;
4259 if (len == 0)
4260 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004261 len = ALIGN(len, sectorsize);
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02004262 em = btrfs_get_extent_fiemap(BTRFS_I(inode), offset, len);
David Sterbac7040052011-04-19 18:00:01 +02004263 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004264 return em;
4265
4266 /* if this isn't a hole return it */
Nikolay Borisov4a2d25c2017-11-23 10:51:43 +02004267 if (em->block_start != EXTENT_MAP_HOLE)
Chris Masonec29ed52011-02-23 16:23:20 -05004268 return em;
Chris Masonec29ed52011-02-23 16:23:20 -05004269
4270 /* this is a hole, advance to the next extent */
4271 offset = extent_map_end(em);
4272 free_extent_map(em);
4273 if (offset >= last)
4274 break;
4275 }
4276 return NULL;
4277}
4278
Qu Wenruo47518322017-04-07 10:43:15 +08004279/*
4280 * To cache previous fiemap extent
4281 *
4282 * Will be used for merging fiemap extent
4283 */
4284struct fiemap_cache {
4285 u64 offset;
4286 u64 phys;
4287 u64 len;
4288 u32 flags;
4289 bool cached;
4290};
4291
4292/*
4293 * Helper to submit fiemap extent.
4294 *
4295 * Will try to merge current fiemap extent specified by @offset, @phys,
4296 * @len and @flags with cached one.
4297 * And only when we fails to merge, cached one will be submitted as
4298 * fiemap extent.
4299 *
4300 * Return value is the same as fiemap_fill_next_extent().
4301 */
4302static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4303 struct fiemap_cache *cache,
4304 u64 offset, u64 phys, u64 len, u32 flags)
4305{
4306 int ret = 0;
4307
4308 if (!cache->cached)
4309 goto assign;
4310
4311 /*
4312 * Sanity check, extent_fiemap() should have ensured that new
Andrea Gelmini52042d82018-11-28 12:05:13 +01004313 * fiemap extent won't overlap with cached one.
Qu Wenruo47518322017-04-07 10:43:15 +08004314 * Not recoverable.
4315 *
4316 * NOTE: Physical address can overlap, due to compression
4317 */
4318 if (cache->offset + cache->len > offset) {
4319 WARN_ON(1);
4320 return -EINVAL;
4321 }
4322
4323 /*
4324 * Only merges fiemap extents if
4325 * 1) Their logical addresses are continuous
4326 *
4327 * 2) Their physical addresses are continuous
4328 * So truly compressed (physical size smaller than logical size)
4329 * extents won't get merged with each other
4330 *
4331 * 3) Share same flags except FIEMAP_EXTENT_LAST
4332 * So regular extent won't get merged with prealloc extent
4333 */
4334 if (cache->offset + cache->len == offset &&
4335 cache->phys + cache->len == phys &&
4336 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4337 (flags & ~FIEMAP_EXTENT_LAST)) {
4338 cache->len += len;
4339 cache->flags |= flags;
4340 goto try_submit_last;
4341 }
4342
4343 /* Not mergeable, need to submit cached one */
4344 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4345 cache->len, cache->flags);
4346 cache->cached = false;
4347 if (ret)
4348 return ret;
4349assign:
4350 cache->cached = true;
4351 cache->offset = offset;
4352 cache->phys = phys;
4353 cache->len = len;
4354 cache->flags = flags;
4355try_submit_last:
4356 if (cache->flags & FIEMAP_EXTENT_LAST) {
4357 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4358 cache->phys, cache->len, cache->flags);
4359 cache->cached = false;
4360 }
4361 return ret;
4362}
4363
4364/*
Qu Wenruo848c23b2017-06-22 10:01:21 +08004365 * Emit last fiemap cache
Qu Wenruo47518322017-04-07 10:43:15 +08004366 *
Qu Wenruo848c23b2017-06-22 10:01:21 +08004367 * The last fiemap cache may still be cached in the following case:
4368 * 0 4k 8k
4369 * |<- Fiemap range ->|
4370 * |<------------ First extent ----------->|
4371 *
4372 * In this case, the first extent range will be cached but not emitted.
4373 * So we must emit it before ending extent_fiemap().
Qu Wenruo47518322017-04-07 10:43:15 +08004374 */
Qu Wenruo848c23b2017-06-22 10:01:21 +08004375static int emit_last_fiemap_cache(struct btrfs_fs_info *fs_info,
4376 struct fiemap_extent_info *fieinfo,
4377 struct fiemap_cache *cache)
Qu Wenruo47518322017-04-07 10:43:15 +08004378{
4379 int ret;
4380
4381 if (!cache->cached)
4382 return 0;
4383
Qu Wenruo47518322017-04-07 10:43:15 +08004384 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4385 cache->len, cache->flags);
4386 cache->cached = false;
4387 if (ret > 0)
4388 ret = 0;
4389 return ret;
4390}
4391
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004392int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterba2135fb92017-06-23 04:09:57 +02004393 __u64 start, __u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004394{
Josef Bacik975f84f2010-11-23 19:36:57 +00004395 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004396 u64 off = start;
4397 u64 max = start + len;
4398 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004399 u32 found_type;
4400 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004401 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004402 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004403 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004404 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004405 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004406 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004407 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004408 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004409 struct fiemap_cache cache = { 0 };
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004410 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004411 u64 em_start = 0;
4412 u64 em_len = 0;
4413 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004414
4415 if (len == 0)
4416 return -EINVAL;
4417
Josef Bacik975f84f2010-11-23 19:36:57 +00004418 path = btrfs_alloc_path();
4419 if (!path)
4420 return -ENOMEM;
4421 path->leave_spinning = 1;
4422
Jeff Mahoneyda170662016-06-15 09:22:56 -04004423 start = round_down(start, btrfs_inode_sectorsize(inode));
4424 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004425
Chris Masonec29ed52011-02-23 16:23:20 -05004426 /*
4427 * lookup the last file extent. We're not using i_size here
4428 * because there might be preallocation past i_size
4429 */
David Sterbaf85b7372017-01-20 14:54:07 +01004430 ret = btrfs_lookup_file_extent(NULL, root, path,
4431 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004432 if (ret < 0) {
4433 btrfs_free_path(path);
4434 return ret;
Liu Bo2d324f52016-05-17 17:21:48 -07004435 } else {
4436 WARN_ON(!ret);
4437 if (ret == 1)
4438 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004439 }
Liu Bo2d324f52016-05-17 17:21:48 -07004440
Josef Bacik975f84f2010-11-23 19:36:57 +00004441 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004442 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004443 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004444
Chris Masonec29ed52011-02-23 16:23:20 -05004445 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004446 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004447 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004448 /* have to trust i_size as the end */
4449 last = (u64)-1;
4450 last_for_get_extent = isize;
4451 } else {
4452 /*
4453 * remember the start of the last extent. There are a
4454 * bunch of different factors that go into the length of the
4455 * extent, so its much less complex to remember where it started
4456 */
4457 last = found_key.offset;
4458 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004459 }
Liu Bofe09e162013-09-22 12:54:23 +08004460 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004461
Chris Masonec29ed52011-02-23 16:23:20 -05004462 /*
4463 * we might have some extents allocated but more delalloc past those
4464 * extents. so, we trust isize unless the start of the last extent is
4465 * beyond isize
4466 */
4467 if (last < isize) {
4468 last = (u64)-1;
4469 last_for_get_extent = isize;
4470 }
4471
David Sterbaff13db42015-12-03 14:30:40 +01004472 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004473 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004474
David Sterbae3350e12017-06-23 04:09:57 +02004475 em = get_extent_skip_holes(inode, start, last_for_get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004476 if (!em)
4477 goto out;
4478 if (IS_ERR(em)) {
4479 ret = PTR_ERR(em);
4480 goto out;
4481 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004482
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004483 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004484 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004485
Chris Masonea8efc72011-03-08 11:54:40 -05004486 /* break if the extent we found is outside the range */
4487 if (em->start >= max || extent_map_end(em) < off)
4488 break;
4489
4490 /*
4491 * get_extent may return an extent that starts before our
4492 * requested range. We have to make sure the ranges
4493 * we return to fiemap always move forward and don't
4494 * overlap, so adjust the offsets here
4495 */
4496 em_start = max(em->start, off);
4497
4498 /*
4499 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004500 * for adjusting the disk offset below. Only do this if the
4501 * extent isn't compressed since our in ram offset may be past
4502 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004503 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004504 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4505 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004506 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004507 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004508 flags = 0;
Filipe Mananaf0986312018-06-20 10:02:30 +01004509 if (em->block_start < EXTENT_MAP_LAST_BYTE)
4510 disko = em->block_start + offset_in_extent;
4511 else
4512 disko = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004513
Chris Masonea8efc72011-03-08 11:54:40 -05004514 /*
4515 * bump off for our next call to get_extent
4516 */
4517 off = extent_map_end(em);
4518 if (off >= max)
4519 end = 1;
4520
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004521 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004522 end = 1;
4523 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004524 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004525 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4526 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004527 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004528 flags |= (FIEMAP_EXTENT_DELALLOC |
4529 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004530 } else if (fieinfo->fi_extents_max) {
4531 u64 bytenr = em->block_start -
4532 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004533
Liu Bofe09e162013-09-22 12:54:23 +08004534 /*
4535 * As btrfs supports shared space, this information
4536 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004537 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4538 * then we're just getting a count and we can skip the
4539 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004540 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06004541 ret = btrfs_check_shared(root,
4542 btrfs_ino(BTRFS_I(inode)),
4543 bytenr);
Josef Bacikdc046b12014-09-10 16:20:45 -04004544 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004545 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004546 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004547 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004548 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004549 }
4550 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4551 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004552 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4553 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004554
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004555 free_extent_map(em);
4556 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004557 if ((em_start >= last) || em_len == (u64)-1 ||
4558 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004559 flags |= FIEMAP_EXTENT_LAST;
4560 end = 1;
4561 }
4562
Chris Masonec29ed52011-02-23 16:23:20 -05004563 /* now scan forward to see if this is really the last extent. */
David Sterbae3350e12017-06-23 04:09:57 +02004564 em = get_extent_skip_holes(inode, off, last_for_get_extent);
Chris Masonec29ed52011-02-23 16:23:20 -05004565 if (IS_ERR(em)) {
4566 ret = PTR_ERR(em);
4567 goto out;
4568 }
4569 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004570 flags |= FIEMAP_EXTENT_LAST;
4571 end = 1;
4572 }
Qu Wenruo47518322017-04-07 10:43:15 +08004573 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4574 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004575 if (ret) {
4576 if (ret == 1)
4577 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004578 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004579 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004580 }
4581out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004582 if (!ret)
Qu Wenruo848c23b2017-06-22 10:01:21 +08004583 ret = emit_last_fiemap_cache(root->fs_info, fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004584 free_extent_map(em);
4585out:
Liu Bofe09e162013-09-22 12:54:23 +08004586 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004587 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004588 &cached_state);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004589 return ret;
4590}
4591
Chris Mason727011e2010-08-06 13:21:20 -04004592static void __free_extent_buffer(struct extent_buffer *eb)
4593{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004594 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004595 kmem_cache_free(extent_buffer_cache, eb);
4596}
4597
Josef Bacika26e8c92014-03-28 17:07:27 -04004598int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004599{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004600 return (atomic_read(&eb->io_pages) ||
4601 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4602 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004603}
4604
Miao Xie897ca6e92010-10-26 20:57:29 -04004605/*
David Sterba55ac0132018-07-19 17:24:32 +02004606 * Release all pages attached to the extent buffer.
Miao Xie897ca6e92010-10-26 20:57:29 -04004607 */
David Sterba55ac0132018-07-19 17:24:32 +02004608static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004609{
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004610 int i;
4611 int num_pages;
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004612 int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004613
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004614 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004615
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004616 num_pages = num_extent_pages(eb);
4617 for (i = 0; i < num_pages; i++) {
4618 struct page *page = eb->pages[i];
Miao Xie897ca6e92010-10-26 20:57:29 -04004619
Forrest Liu5d2361d2015-02-09 17:31:45 +08004620 if (!page)
4621 continue;
4622 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004623 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004624 /*
4625 * We do this since we'll remove the pages after we've
4626 * removed the eb from the radix tree, so we could race
4627 * and have this page now attached to the new eb. So
4628 * only clear page_private if it's still connected to
4629 * this eb.
4630 */
4631 if (PagePrivate(page) &&
4632 page->private == (unsigned long)eb) {
4633 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4634 BUG_ON(PageDirty(page));
4635 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004636 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004637 * We need to make sure we haven't be attached
4638 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004639 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004640 ClearPagePrivate(page);
4641 set_page_private(page, 0);
4642 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004643 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004644 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004645
4646 if (mapped)
4647 spin_unlock(&page->mapping->private_lock);
4648
Nicholas D Steeves01327612016-05-19 21:18:45 -04004649 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004650 put_page(page);
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004651 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004652}
4653
4654/*
4655 * Helper for releasing the extent buffer.
4656 */
4657static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4658{
David Sterba55ac0132018-07-19 17:24:32 +02004659 btrfs_release_extent_buffer_pages(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004660 __free_extent_buffer(eb);
4661}
4662
Josef Bacikf28491e2013-12-16 13:24:27 -05004663static struct extent_buffer *
4664__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004665 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004666{
4667 struct extent_buffer *eb = NULL;
4668
Michal Hockod1b5c562015-08-19 14:17:40 +02004669 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004670 eb->start = start;
4671 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004672 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004673 eb->bflags = 0;
4674 rwlock_init(&eb->lock);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004675 atomic_set(&eb->blocking_readers, 0);
4676 atomic_set(&eb->blocking_writers, 0);
David Sterbaed1b4ed2018-08-24 16:31:17 +02004677 eb->lock_nested = false;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004678 init_waitqueue_head(&eb->write_lock_wq);
4679 init_waitqueue_head(&eb->read_lock_wq);
4680
4681 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4682
4683 spin_lock_init(&eb->refs_lock);
4684 atomic_set(&eb->refs, 1);
4685 atomic_set(&eb->io_pages, 0);
4686
4687 /*
4688 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4689 */
4690 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4691 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4692 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4693
David Sterba843ccf92018-08-24 14:56:28 +02004694#ifdef CONFIG_BTRFS_DEBUG
4695 atomic_set(&eb->spinning_writers, 0);
David Sterbaafd495a2018-08-24 15:57:38 +02004696 atomic_set(&eb->spinning_readers, 0);
David Sterba5c9c7992018-08-24 16:15:51 +02004697 atomic_set(&eb->read_locks, 0);
David Sterbac79adfc2018-08-24 16:24:26 +02004698 atomic_set(&eb->write_locks, 0);
David Sterba843ccf92018-08-24 14:56:28 +02004699#endif
4700
Josef Bacikdb7f3432013-08-07 14:54:37 -04004701 return eb;
4702}
4703
4704struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4705{
David Sterbacc5e31a2018-03-01 18:20:27 +01004706 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004707 struct page *p;
4708 struct extent_buffer *new;
David Sterbacc5e31a2018-03-01 18:20:27 +01004709 int num_pages = num_extent_pages(src);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004710
David Sterba3f556f72014-06-15 03:20:26 +02004711 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004712 if (new == NULL)
4713 return NULL;
4714
4715 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004716 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004717 if (!p) {
4718 btrfs_release_extent_buffer(new);
4719 return NULL;
4720 }
4721 attach_extent_buffer_page(new, p);
4722 WARN_ON(PageDirty(p));
4723 SetPageUptodate(p);
4724 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004725 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004726 }
4727
Josef Bacikdb7f3432013-08-07 14:54:37 -04004728 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004729 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004730
4731 return new;
4732}
4733
Omar Sandoval0f331222015-09-29 20:50:31 -07004734struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4735 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004736{
4737 struct extent_buffer *eb;
David Sterbacc5e31a2018-03-01 18:20:27 +01004738 int num_pages;
4739 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004740
David Sterba3f556f72014-06-15 03:20:26 +02004741 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004742 if (!eb)
4743 return NULL;
4744
David Sterba65ad0102018-06-29 10:56:49 +02004745 num_pages = num_extent_pages(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004746 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004747 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004748 if (!eb->pages[i])
4749 goto err;
4750 }
4751 set_extent_buffer_uptodate(eb);
4752 btrfs_set_header_nritems(eb, 0);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004753 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004754
4755 return eb;
4756err:
4757 for (; i > 0; i--)
4758 __free_page(eb->pages[i - 1]);
4759 __free_extent_buffer(eb);
4760 return NULL;
4761}
4762
Omar Sandoval0f331222015-09-29 20:50:31 -07004763struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004764 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004765{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004766 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004767}
4768
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004769static void check_buffer_tree_ref(struct extent_buffer *eb)
4770{
Chris Mason242e18c2013-01-29 17:49:37 -05004771 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004772 /* the ref bit is tricky. We have to make sure it is set
4773 * if we have the buffer dirty. Otherwise the
4774 * code to free a buffer can end up dropping a dirty
4775 * page
4776 *
4777 * Once the ref bit is set, it won't go away while the
4778 * buffer is dirty or in writeback, and it also won't
4779 * go away while we have the reference count on the
4780 * eb bumped.
4781 *
4782 * We can't just set the ref bit without bumping the
4783 * ref on the eb because free_extent_buffer might
4784 * see the ref bit and try to clear it. If this happens
4785 * free_extent_buffer might end up dropping our original
4786 * ref by mistake and freeing the page before we are able
4787 * to add one more ref.
4788 *
4789 * So bump the ref count first, then set the bit. If someone
4790 * beat us to it, drop the ref we added.
4791 */
Chris Mason242e18c2013-01-29 17:49:37 -05004792 refs = atomic_read(&eb->refs);
4793 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4794 return;
4795
Josef Bacik594831c2012-07-20 16:11:08 -04004796 spin_lock(&eb->refs_lock);
4797 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004798 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004799 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004800}
4801
Mel Gorman2457aec2014-06-04 16:10:31 -07004802static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4803 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004804{
David Sterbacc5e31a2018-03-01 18:20:27 +01004805 int num_pages, i;
Josef Bacik5df42352012-03-15 18:24:42 -04004806
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004807 check_buffer_tree_ref(eb);
4808
David Sterba65ad0102018-06-29 10:56:49 +02004809 num_pages = num_extent_pages(eb);
Josef Bacik5df42352012-03-15 18:24:42 -04004810 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004811 struct page *p = eb->pages[i];
4812
Mel Gorman2457aec2014-06-04 16:10:31 -07004813 if (p != accessed)
4814 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004815 }
4816}
4817
Josef Bacikf28491e2013-12-16 13:24:27 -05004818struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4819 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004820{
4821 struct extent_buffer *eb;
4822
4823 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004824 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004825 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004826 if (eb && atomic_inc_not_zero(&eb->refs)) {
4827 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004828 /*
4829 * Lock our eb's refs_lock to avoid races with
4830 * free_extent_buffer. When we get our eb it might be flagged
4831 * with EXTENT_BUFFER_STALE and another task running
4832 * free_extent_buffer might have seen that flag set,
4833 * eb->refs == 2, that the buffer isn't under IO (dirty and
4834 * writeback flags not set) and it's still in the tree (flag
4835 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4836 * of decrementing the extent buffer's reference count twice.
4837 * So here we could race and increment the eb's reference count,
4838 * clear its stale flag, mark it as dirty and drop our reference
4839 * before the other task finishes executing free_extent_buffer,
4840 * which would later result in an attempt to free an extent
4841 * buffer that is dirty.
4842 */
4843 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4844 spin_lock(&eb->refs_lock);
4845 spin_unlock(&eb->refs_lock);
4846 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004847 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004848 return eb;
4849 }
4850 rcu_read_unlock();
4851
4852 return NULL;
4853}
4854
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004855#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4856struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004857 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004858{
4859 struct extent_buffer *eb, *exists = NULL;
4860 int ret;
4861
4862 eb = find_extent_buffer(fs_info, start);
4863 if (eb)
4864 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04004865 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004866 if (!eb)
4867 return NULL;
4868 eb->fs_info = fs_info;
4869again:
David Sterbae1860a72016-05-09 14:11:38 +02004870 ret = radix_tree_preload(GFP_NOFS);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004871 if (ret)
4872 goto free_eb;
4873 spin_lock(&fs_info->buffer_lock);
4874 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004875 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004876 spin_unlock(&fs_info->buffer_lock);
4877 radix_tree_preload_end();
4878 if (ret == -EEXIST) {
4879 exists = find_extent_buffer(fs_info, start);
4880 if (exists)
4881 goto free_eb;
4882 else
4883 goto again;
4884 }
4885 check_buffer_tree_ref(eb);
4886 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
4887
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004888 return eb;
4889free_eb:
4890 btrfs_release_extent_buffer(eb);
4891 return exists;
4892}
4893#endif
4894
Josef Bacikf28491e2013-12-16 13:24:27 -05004895struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004896 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05004897{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004898 unsigned long len = fs_info->nodesize;
David Sterbacc5e31a2018-03-01 18:20:27 +01004899 int num_pages;
4900 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004901 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004902 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004903 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004904 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05004905 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05004906 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004907 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004908
Jeff Mahoneyda170662016-06-15 09:22:56 -04004909 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07004910 btrfs_err(fs_info, "bad tree block start %llu", start);
4911 return ERR_PTR(-EINVAL);
4912 }
4913
Josef Bacikf28491e2013-12-16 13:24:27 -05004914 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004915 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004916 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004917
David Sterba23d79d82014-06-15 02:55:29 +02004918 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04004919 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07004920 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05004921
David Sterba65ad0102018-06-29 10:56:49 +02004922 num_pages = num_extent_pages(eb);
Chris Mason727011e2010-08-06 13:21:20 -04004923 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02004924 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07004925 if (!p) {
4926 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04004927 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004928 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004929
4930 spin_lock(&mapping->private_lock);
4931 if (PagePrivate(p)) {
4932 /*
4933 * We could have already allocated an eb for this page
4934 * and attached one so lets see if we can get a ref on
4935 * the existing eb, and if we can we know it's good and
4936 * we can just return that one, else we know we can just
4937 * overwrite page->private.
4938 */
4939 exists = (struct extent_buffer *)p->private;
4940 if (atomic_inc_not_zero(&exists->refs)) {
4941 spin_unlock(&mapping->private_lock);
4942 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004943 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07004944 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004945 goto free_eb;
4946 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08004947 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004948
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004949 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004950 * Do this so attach doesn't complain and we need to
4951 * drop the ref the old guy had.
4952 */
4953 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004954 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004955 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05004956 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004957 attach_extent_buffer_page(eb, p);
4958 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004959 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04004960 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004961 if (!PageUptodate(p))
4962 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004963
4964 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03004965 * We can't unlock the pages just yet since the extent buffer
4966 * hasn't been properly inserted in the radix tree, this
4967 * opens a race with btree_releasepage which can free a page
4968 * while we are still filling in all pages for the buffer and
4969 * we could crash.
Chris Masoneb14ab82011-02-10 12:35:00 -05004970 */
Chris Masond1310b22008-01-24 16:13:08 -05004971 }
4972 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004973 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004974again:
David Sterbae1860a72016-05-09 14:11:38 +02004975 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07004976 if (ret) {
4977 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04004978 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004979 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004980
Josef Bacikf28491e2013-12-16 13:24:27 -05004981 spin_lock(&fs_info->buffer_lock);
4982 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004983 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05004984 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004985 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04004986 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05004987 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004988 if (exists)
4989 goto free_eb;
4990 else
Josef Bacik115391d2012-03-09 09:51:43 -05004991 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04004992 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004993 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004994 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05004995 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05004996
4997 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03004998 * Now it's safe to unlock the pages because any calls to
4999 * btree_releasepage will correctly detect that a page belongs to a
5000 * live buffer and won't free them prematurely.
Chris Masoneb14ab82011-02-10 12:35:00 -05005001 */
Nikolay Borisov28187ae2018-07-04 10:24:51 +03005002 for (i = 0; i < num_pages; i++)
5003 unlock_page(eb->pages[i]);
Chris Masond1310b22008-01-24 16:13:08 -05005004 return eb;
5005
Chris Mason6af118ce2008-07-22 11:18:07 -04005006free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005007 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005008 for (i = 0; i < num_pages; i++) {
5009 if (eb->pages[i])
5010 unlock_page(eb->pages[i]);
5011 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005012
Miao Xie897ca6e92010-10-26 20:57:29 -04005013 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005014 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005015}
Chris Masond1310b22008-01-24 16:13:08 -05005016
Josef Bacik3083ee22012-03-09 16:01:49 -05005017static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5018{
5019 struct extent_buffer *eb =
5020 container_of(head, struct extent_buffer, rcu_head);
5021
5022 __free_extent_buffer(eb);
5023}
5024
David Sterbaf7a52a42013-04-26 14:56:29 +00005025static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005026{
Nikolay Borisov07e21c42018-06-27 16:38:23 +03005027 lockdep_assert_held(&eb->refs_lock);
5028
Josef Bacik3083ee22012-03-09 16:01:49 -05005029 WARN_ON(atomic_read(&eb->refs) == 0);
5030 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005031 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005032 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005033
Jan Schmidt815a51c2012-05-16 17:00:02 +02005034 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005035
Josef Bacikf28491e2013-12-16 13:24:27 -05005036 spin_lock(&fs_info->buffer_lock);
5037 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005038 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005039 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005040 } else {
5041 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005042 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005043
5044 /* Should be safe to release our pages at this point */
David Sterba55ac0132018-07-19 17:24:32 +02005045 btrfs_release_extent_buffer_pages(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005046#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005047 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
Josef Bacikbcb7e442015-03-16 17:38:02 -04005048 __free_extent_buffer(eb);
5049 return 1;
5050 }
5051#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005052 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005053 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005054 }
5055 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005056
5057 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005058}
5059
Chris Masond1310b22008-01-24 16:13:08 -05005060void free_extent_buffer(struct extent_buffer *eb)
5061{
Chris Mason242e18c2013-01-29 17:49:37 -05005062 int refs;
5063 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005064 if (!eb)
5065 return;
5066
Chris Mason242e18c2013-01-29 17:49:37 -05005067 while (1) {
5068 refs = atomic_read(&eb->refs);
Nikolay Borisov46cc7752018-10-15 17:04:01 +03005069 if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
5070 || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
5071 refs == 1))
Chris Mason242e18c2013-01-29 17:49:37 -05005072 break;
5073 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5074 if (old == refs)
5075 return;
5076 }
5077
Josef Bacik3083ee22012-03-09 16:01:49 -05005078 spin_lock(&eb->refs_lock);
5079 if (atomic_read(&eb->refs) == 2 &&
5080 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005081 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005082 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5083 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005084
Josef Bacik3083ee22012-03-09 16:01:49 -05005085 /*
5086 * I know this is terrible, but it's temporary until we stop tracking
5087 * the uptodate bits and such for the extent buffers.
5088 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005089 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005090}
Chris Masond1310b22008-01-24 16:13:08 -05005091
Josef Bacik3083ee22012-03-09 16:01:49 -05005092void free_extent_buffer_stale(struct extent_buffer *eb)
5093{
5094 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005095 return;
5096
Josef Bacik3083ee22012-03-09 16:01:49 -05005097 spin_lock(&eb->refs_lock);
5098 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5099
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005100 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005101 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5102 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005103 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005104}
5105
Chris Mason1d4284b2012-03-28 20:31:37 -04005106void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005107{
David Sterbacc5e31a2018-03-01 18:20:27 +01005108 int i;
5109 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005110 struct page *page;
5111
David Sterba65ad0102018-06-29 10:56:49 +02005112 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005113
5114 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005115 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005116 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005117 continue;
5118
Chris Masona61e6f22008-07-22 11:18:08 -04005119 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005120 WARN_ON(!PagePrivate(page));
5121
Chris Masond1310b22008-01-24 16:13:08 -05005122 clear_page_dirty_for_io(page);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005123 xa_lock_irq(&page->mapping->i_pages);
Matthew Wilcox0a943c62017-12-04 10:37:22 -05005124 if (!PageDirty(page))
5125 __xa_clear_mark(&page->mapping->i_pages,
5126 page_index(page), PAGECACHE_TAG_DIRTY);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005127 xa_unlock_irq(&page->mapping->i_pages);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005128 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005129 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005130 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005131 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005132}
Chris Masond1310b22008-01-24 16:13:08 -05005133
Liu Boabb57ef2018-09-14 01:44:42 +08005134bool set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005135{
David Sterbacc5e31a2018-03-01 18:20:27 +01005136 int i;
5137 int num_pages;
Liu Boabb57ef2018-09-14 01:44:42 +08005138 bool was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005139
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005140 check_buffer_tree_ref(eb);
5141
Chris Masonb9473432009-03-13 11:00:37 -04005142 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005143
David Sterba65ad0102018-06-29 10:56:49 +02005144 num_pages = num_extent_pages(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05005145 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005146 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5147
Liu Boabb57ef2018-09-14 01:44:42 +08005148 if (!was_dirty)
5149 for (i = 0; i < num_pages; i++)
5150 set_page_dirty(eb->pages[i]);
Liu Bo51995c32018-09-14 01:46:08 +08005151
5152#ifdef CONFIG_BTRFS_DEBUG
5153 for (i = 0; i < num_pages; i++)
5154 ASSERT(PageDirty(eb->pages[i]));
5155#endif
5156
Chris Masonb9473432009-03-13 11:00:37 -04005157 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005158}
Chris Masond1310b22008-01-24 16:13:08 -05005159
David Sterba69ba3922015-12-03 13:08:59 +01005160void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005161{
David Sterbacc5e31a2018-03-01 18:20:27 +01005162 int i;
Chris Mason1259ab72008-05-12 13:39:03 -04005163 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005164 int num_pages;
Chris Mason1259ab72008-05-12 13:39:03 -04005165
Chris Masonb4ce94d2009-02-04 09:25:08 -05005166 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005167 num_pages = num_extent_pages(eb);
Chris Mason1259ab72008-05-12 13:39:03 -04005168 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005169 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005170 if (page)
5171 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005172 }
Chris Mason1259ab72008-05-12 13:39:03 -04005173}
5174
David Sterba09c25a82015-12-03 13:08:59 +01005175void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005176{
David Sterbacc5e31a2018-03-01 18:20:27 +01005177 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005178 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005179 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005180
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005181 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005182 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005183 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005184 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005185 SetPageUptodate(page);
5186 }
Chris Masond1310b22008-01-24 16:13:08 -05005187}
Chris Masond1310b22008-01-24 16:13:08 -05005188
Chris Masond1310b22008-01-24 16:13:08 -05005189int read_extent_buffer_pages(struct extent_io_tree *tree,
David Sterba6af49db2017-06-23 04:09:57 +02005190 struct extent_buffer *eb, int wait, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005191{
David Sterbacc5e31a2018-03-01 18:20:27 +01005192 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005193 struct page *page;
5194 int err;
5195 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005196 int locked_pages = 0;
5197 int all_uptodate = 1;
David Sterbacc5e31a2018-03-01 18:20:27 +01005198 int num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005199 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005200 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005201 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005202
Chris Masonb4ce94d2009-02-04 09:25:08 -05005203 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005204 return 0;
5205
David Sterba65ad0102018-06-29 10:56:49 +02005206 num_pages = num_extent_pages(eb);
Josef Bacik8436ea912016-09-02 15:40:03 -04005207 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005208 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005209 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005210 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005211 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005212 } else {
5213 lock_page(page);
5214 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005215 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005216 }
5217 /*
5218 * We need to firstly lock all pages to make sure that
5219 * the uptodate bit of our pages won't be affected by
5220 * clear_extent_buffer_uptodate().
5221 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005222 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005223 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005224 if (!PageUptodate(page)) {
5225 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005226 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005227 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005228 }
Liu Bo2571e732016-08-03 12:33:01 -07005229
Chris Masonce9adaa2008-04-09 16:28:12 -04005230 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005231 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005232 goto unlock_exit;
5233 }
5234
Filipe Manana656f30d2014-09-26 12:25:56 +01005235 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005236 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005237 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005238 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005239 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005240
Chris Masonce9adaa2008-04-09 16:28:12 -04005241 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005242 if (ret) {
5243 atomic_dec(&eb->io_pages);
5244 unlock_page(page);
5245 continue;
5246 }
5247
Chris Masonf1885912008-04-09 16:28:12 -04005248 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005249 err = __extent_read_full_page(tree, page,
David Sterba6af49db2017-06-23 04:09:57 +02005250 btree_get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005251 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005252 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005253 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005254 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005255 /*
5256 * We use &bio in above __extent_read_full_page,
5257 * so we ensure that if it returns error, the
5258 * current page fails to add itself to bio and
5259 * it's been unlocked.
5260 *
5261 * We must dec io_pages by ourselves.
5262 */
5263 atomic_dec(&eb->io_pages);
5264 }
Chris Masond1310b22008-01-24 16:13:08 -05005265 } else {
5266 unlock_page(page);
5267 }
5268 }
5269
Jeff Mahoney355808c2011-10-03 23:23:14 -04005270 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005271 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005272 if (err)
5273 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005274 }
Chris Masona86c12c2008-02-07 10:50:54 -05005275
Arne Jansenbb82ab82011-06-10 14:06:53 +02005276 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005277 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005278
Josef Bacik8436ea912016-09-02 15:40:03 -04005279 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005280 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005281 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005282 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005283 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005284 }
Chris Masond3977122009-01-05 21:25:51 -05005285
Chris Masond1310b22008-01-24 16:13:08 -05005286 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005287
5288unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005289 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005290 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005291 page = eb->pages[locked_pages];
5292 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005293 }
5294 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005295}
Chris Masond1310b22008-01-24 16:13:08 -05005296
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005297void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5298 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005299{
5300 size_t cur;
5301 size_t offset;
5302 struct page *page;
5303 char *kaddr;
5304 char *dst = (char *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005305 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005306 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005307
Liu Bof716abd2017-08-09 11:10:16 -06005308 if (start + len > eb->len) {
5309 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5310 eb->start, eb->len, start, len);
5311 memset(dst, 0, len);
5312 return;
5313 }
Chris Masond1310b22008-01-24 16:13:08 -05005314
Johannes Thumshirn70730172018-12-05 15:23:03 +01005315 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005316
Chris Masond3977122009-01-05 21:25:51 -05005317 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005318 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005319
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005320 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005321 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005322 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005323
5324 dst += cur;
5325 len -= cur;
5326 offset = 0;
5327 i++;
5328 }
5329}
Chris Masond1310b22008-01-24 16:13:08 -05005330
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005331int read_extent_buffer_to_user(const struct extent_buffer *eb,
5332 void __user *dstv,
5333 unsigned long start, unsigned long len)
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005334{
5335 size_t cur;
5336 size_t offset;
5337 struct page *page;
5338 char *kaddr;
5339 char __user *dst = (char __user *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005340 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005341 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005342 int ret = 0;
5343
5344 WARN_ON(start > eb->len);
5345 WARN_ON(start + len > eb->start + eb->len);
5346
Johannes Thumshirn70730172018-12-05 15:23:03 +01005347 offset = offset_in_page(start_offset + start);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005348
5349 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005350 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005351
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005352 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005353 kaddr = page_address(page);
5354 if (copy_to_user(dst, kaddr + offset, cur)) {
5355 ret = -EFAULT;
5356 break;
5357 }
5358
5359 dst += cur;
5360 len -= cur;
5361 offset = 0;
5362 i++;
5363 }
5364
5365 return ret;
5366}
5367
Liu Bo415b35a2016-06-17 19:16:21 -07005368/*
5369 * return 0 if the item is found within a page.
5370 * return 1 if the item spans two pages.
5371 * return -EINVAL otherwise.
5372 */
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005373int map_private_extent_buffer(const struct extent_buffer *eb,
5374 unsigned long start, unsigned long min_len,
5375 char **map, unsigned long *map_start,
5376 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005377{
Johannes Thumshirncc2c39d2018-11-28 09:54:54 +01005378 size_t offset;
Chris Masond1310b22008-01-24 16:13:08 -05005379 char *kaddr;
5380 struct page *p;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005381 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005382 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005383 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005384 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005385
Liu Bof716abd2017-08-09 11:10:16 -06005386 if (start + min_len > eb->len) {
5387 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5388 eb->start, eb->len, start, min_len);
5389 return -EINVAL;
5390 }
5391
Chris Masond1310b22008-01-24 16:13:08 -05005392 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005393 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005394
5395 if (i == 0) {
5396 offset = start_offset;
5397 *map_start = 0;
5398 } else {
5399 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005400 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005401 }
Chris Masond3977122009-01-05 21:25:51 -05005402
David Sterbafb85fc92014-07-31 01:03:53 +02005403 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005404 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005405 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005406 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005407 return 0;
5408}
Chris Masond1310b22008-01-24 16:13:08 -05005409
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005410int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5411 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005412{
5413 size_t cur;
5414 size_t offset;
5415 struct page *page;
5416 char *kaddr;
5417 char *ptr = (char *)ptrv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005418 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005419 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005420 int ret = 0;
5421
5422 WARN_ON(start > eb->len);
5423 WARN_ON(start + len > eb->start + eb->len);
5424
Johannes Thumshirn70730172018-12-05 15:23:03 +01005425 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005426
Chris Masond3977122009-01-05 21:25:51 -05005427 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005428 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005429
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005430 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005431
Chris Masona6591712011-07-19 12:04:14 -04005432 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005433 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005434 if (ret)
5435 break;
5436
5437 ptr += cur;
5438 len -= cur;
5439 offset = 0;
5440 i++;
5441 }
5442 return ret;
5443}
Chris Masond1310b22008-01-24 16:13:08 -05005444
David Sterbaf157bf72016-11-09 17:43:38 +01005445void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5446 const void *srcv)
5447{
5448 char *kaddr;
5449
5450 WARN_ON(!PageUptodate(eb->pages[0]));
5451 kaddr = page_address(eb->pages[0]);
5452 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5453 BTRFS_FSID_SIZE);
5454}
5455
5456void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5457{
5458 char *kaddr;
5459
5460 WARN_ON(!PageUptodate(eb->pages[0]));
5461 kaddr = page_address(eb->pages[0]);
5462 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5463 BTRFS_FSID_SIZE);
5464}
5465
Chris Masond1310b22008-01-24 16:13:08 -05005466void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5467 unsigned long start, unsigned long len)
5468{
5469 size_t cur;
5470 size_t offset;
5471 struct page *page;
5472 char *kaddr;
5473 char *src = (char *)srcv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005474 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005475 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005476
5477 WARN_ON(start > eb->len);
5478 WARN_ON(start + len > eb->start + eb->len);
5479
Johannes Thumshirn70730172018-12-05 15:23:03 +01005480 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005481
Chris Masond3977122009-01-05 21:25:51 -05005482 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005483 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005484 WARN_ON(!PageUptodate(page));
5485
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005486 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005487 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005488 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005489
5490 src += cur;
5491 len -= cur;
5492 offset = 0;
5493 i++;
5494 }
5495}
Chris Masond1310b22008-01-24 16:13:08 -05005496
David Sterbab159fa22016-11-08 18:09:03 +01005497void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5498 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;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005504 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005505 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005506
5507 WARN_ON(start > eb->len);
5508 WARN_ON(start + len > eb->start + eb->len);
5509
Johannes Thumshirn70730172018-12-05 15:23:03 +01005510 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005511
Chris Masond3977122009-01-05 21:25:51 -05005512 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005513 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005514 WARN_ON(!PageUptodate(page));
5515
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005516 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005517 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005518 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005519
5520 len -= cur;
5521 offset = 0;
5522 i++;
5523 }
5524}
Chris Masond1310b22008-01-24 16:13:08 -05005525
David Sterba58e80122016-11-08 18:30:31 +01005526void copy_extent_buffer_full(struct extent_buffer *dst,
5527 struct extent_buffer *src)
5528{
5529 int i;
David Sterbacc5e31a2018-03-01 18:20:27 +01005530 int num_pages;
David Sterba58e80122016-11-08 18:30:31 +01005531
5532 ASSERT(dst->len == src->len);
5533
David Sterba65ad0102018-06-29 10:56:49 +02005534 num_pages = num_extent_pages(dst);
David Sterba58e80122016-11-08 18:30:31 +01005535 for (i = 0; i < num_pages; i++)
5536 copy_page(page_address(dst->pages[i]),
5537 page_address(src->pages[i]));
5538}
5539
Chris Masond1310b22008-01-24 16:13:08 -05005540void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5541 unsigned long dst_offset, unsigned long src_offset,
5542 unsigned long len)
5543{
5544 u64 dst_len = dst->len;
5545 size_t cur;
5546 size_t offset;
5547 struct page *page;
5548 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005549 size_t start_offset = offset_in_page(dst->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005550 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005551
5552 WARN_ON(src->len != dst_len);
5553
Johannes Thumshirn70730172018-12-05 15:23:03 +01005554 offset = offset_in_page(start_offset + dst_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005555
Chris Masond3977122009-01-05 21:25:51 -05005556 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005557 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005558 WARN_ON(!PageUptodate(page));
5559
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005560 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005561
Chris Masona6591712011-07-19 12:04:14 -04005562 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005563 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005564
5565 src_offset += cur;
5566 len -= cur;
5567 offset = 0;
5568 i++;
5569 }
5570}
Chris Masond1310b22008-01-24 16:13:08 -05005571
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005572/*
5573 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5574 * given bit number
5575 * @eb: the extent buffer
5576 * @start: offset of the bitmap item in the extent buffer
5577 * @nr: bit number
5578 * @page_index: return index of the page in the extent buffer that contains the
5579 * given bit number
5580 * @page_offset: return offset into the page given by page_index
5581 *
5582 * This helper hides the ugliness of finding the byte in an extent buffer which
5583 * contains a given bit.
5584 */
5585static inline void eb_bitmap_offset(struct extent_buffer *eb,
5586 unsigned long start, unsigned long nr,
5587 unsigned long *page_index,
5588 size_t *page_offset)
5589{
Johannes Thumshirn70730172018-12-05 15:23:03 +01005590 size_t start_offset = offset_in_page(eb->start);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005591 size_t byte_offset = BIT_BYTE(nr);
5592 size_t offset;
5593
5594 /*
5595 * The byte we want is the offset of the extent buffer + the offset of
5596 * the bitmap item in the extent buffer + the offset of the byte in the
5597 * bitmap item.
5598 */
5599 offset = start_offset + start + byte_offset;
5600
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005601 *page_index = offset >> PAGE_SHIFT;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005602 *page_offset = offset_in_page(offset);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005603}
5604
5605/**
5606 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5607 * @eb: the extent buffer
5608 * @start: offset of the bitmap item in the extent buffer
5609 * @nr: bit number to test
5610 */
5611int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5612 unsigned long nr)
5613{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005614 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005615 struct page *page;
5616 unsigned long i;
5617 size_t offset;
5618
5619 eb_bitmap_offset(eb, start, nr, &i, &offset);
5620 page = eb->pages[i];
5621 WARN_ON(!PageUptodate(page));
5622 kaddr = page_address(page);
5623 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5624}
5625
5626/**
5627 * extent_buffer_bitmap_set - set an area of a bitmap
5628 * @eb: the extent buffer
5629 * @start: offset of the bitmap item in the extent buffer
5630 * @pos: bit number of the first bit
5631 * @len: number of bits to set
5632 */
5633void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5634 unsigned long pos, unsigned long len)
5635{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005636 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005637 struct page *page;
5638 unsigned long i;
5639 size_t offset;
5640 const unsigned int size = pos + len;
5641 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005642 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005643
5644 eb_bitmap_offset(eb, start, pos, &i, &offset);
5645 page = eb->pages[i];
5646 WARN_ON(!PageUptodate(page));
5647 kaddr = page_address(page);
5648
5649 while (len >= bits_to_set) {
5650 kaddr[offset] |= mask_to_set;
5651 len -= bits_to_set;
5652 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005653 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005654 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005655 offset = 0;
5656 page = eb->pages[++i];
5657 WARN_ON(!PageUptodate(page));
5658 kaddr = page_address(page);
5659 }
5660 }
5661 if (len) {
5662 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5663 kaddr[offset] |= mask_to_set;
5664 }
5665}
5666
5667
5668/**
5669 * extent_buffer_bitmap_clear - clear an area of a bitmap
5670 * @eb: the extent buffer
5671 * @start: offset of the bitmap item in the extent buffer
5672 * @pos: bit number of the first bit
5673 * @len: number of bits to clear
5674 */
5675void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5676 unsigned long pos, unsigned long len)
5677{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005678 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005679 struct page *page;
5680 unsigned long i;
5681 size_t offset;
5682 const unsigned int size = pos + len;
5683 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005684 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005685
5686 eb_bitmap_offset(eb, start, pos, &i, &offset);
5687 page = eb->pages[i];
5688 WARN_ON(!PageUptodate(page));
5689 kaddr = page_address(page);
5690
5691 while (len >= bits_to_clear) {
5692 kaddr[offset] &= ~mask_to_clear;
5693 len -= bits_to_clear;
5694 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005695 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005696 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005697 offset = 0;
5698 page = eb->pages[++i];
5699 WARN_ON(!PageUptodate(page));
5700 kaddr = page_address(page);
5701 }
5702 }
5703 if (len) {
5704 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5705 kaddr[offset] &= ~mask_to_clear;
5706 }
5707}
5708
Sergei Trofimovich33872062011-04-11 21:52:52 +00005709static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5710{
5711 unsigned long distance = (src > dst) ? src - dst : dst - src;
5712 return distance < len;
5713}
5714
Chris Masond1310b22008-01-24 16:13:08 -05005715static void copy_pages(struct page *dst_page, struct page *src_page,
5716 unsigned long dst_off, unsigned long src_off,
5717 unsigned long len)
5718{
Chris Masona6591712011-07-19 12:04:14 -04005719 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005720 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005721 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005722
Sergei Trofimovich33872062011-04-11 21:52:52 +00005723 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005724 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005725 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005726 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005727 if (areas_overlap(src_off, dst_off, len))
5728 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005729 }
Chris Masond1310b22008-01-24 16:13:08 -05005730
Chris Mason727011e2010-08-06 13:21:20 -04005731 if (must_memmove)
5732 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5733 else
5734 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005735}
5736
5737void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5738 unsigned long src_offset, unsigned long len)
5739{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005740 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005741 size_t cur;
5742 size_t dst_off_in_page;
5743 size_t src_off_in_page;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005744 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005745 unsigned long dst_i;
5746 unsigned long src_i;
5747
5748 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005749 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005750 "memmove bogus src_offset %lu move len %lu dst len %lu",
5751 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005752 BUG_ON(1);
5753 }
5754 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005755 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005756 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5757 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005758 BUG_ON(1);
5759 }
5760
Chris Masond3977122009-01-05 21:25:51 -05005761 while (len > 0) {
Johannes Thumshirn70730172018-12-05 15:23:03 +01005762 dst_off_in_page = offset_in_page(start_offset + dst_offset);
5763 src_off_in_page = offset_in_page(start_offset + src_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005764
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005765 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5766 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005767
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005768 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005769 src_off_in_page));
5770 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005771 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005772
David Sterbafb85fc92014-07-31 01:03:53 +02005773 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005774 dst_off_in_page, src_off_in_page, cur);
5775
5776 src_offset += cur;
5777 dst_offset += cur;
5778 len -= cur;
5779 }
5780}
Chris Masond1310b22008-01-24 16:13:08 -05005781
5782void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5783 unsigned long src_offset, unsigned long len)
5784{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005785 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005786 size_t cur;
5787 size_t dst_off_in_page;
5788 size_t src_off_in_page;
5789 unsigned long dst_end = dst_offset + len - 1;
5790 unsigned long src_end = src_offset + len - 1;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005791 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005792 unsigned long dst_i;
5793 unsigned long src_i;
5794
5795 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005796 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005797 "memmove bogus src_offset %lu move len %lu len %lu",
5798 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005799 BUG_ON(1);
5800 }
5801 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005802 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005803 "memmove bogus dst_offset %lu move len %lu len %lu",
5804 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005805 BUG_ON(1);
5806 }
Chris Mason727011e2010-08-06 13:21:20 -04005807 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005808 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5809 return;
5810 }
Chris Masond3977122009-01-05 21:25:51 -05005811 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005812 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
5813 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005814
Johannes Thumshirn70730172018-12-05 15:23:03 +01005815 dst_off_in_page = offset_in_page(start_offset + dst_end);
5816 src_off_in_page = offset_in_page(start_offset + src_end);
Chris Masond1310b22008-01-24 16:13:08 -05005817
5818 cur = min_t(unsigned long, len, src_off_in_page + 1);
5819 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005820 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005821 dst_off_in_page - cur + 1,
5822 src_off_in_page - cur + 1, cur);
5823
5824 dst_end -= cur;
5825 src_end -= cur;
5826 len -= cur;
5827 }
5828}
Chris Mason6af118ce2008-07-22 11:18:07 -04005829
David Sterbaf7a52a42013-04-26 14:56:29 +00005830int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005831{
Chris Mason6af118ce2008-07-22 11:18:07 -04005832 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005833
Miao Xie19fe0a82010-10-26 20:57:29 -04005834 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005835 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05005836 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005837 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005838 spin_lock(&page->mapping->private_lock);
5839 if (!PagePrivate(page)) {
5840 spin_unlock(&page->mapping->private_lock);
5841 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005842 }
5843
Josef Bacik3083ee22012-03-09 16:01:49 -05005844 eb = (struct extent_buffer *)page->private;
5845 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005846
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005847 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005848 * This is a little awful but should be ok, we need to make sure that
5849 * the eb doesn't disappear out from under us while we're looking at
5850 * this page.
5851 */
5852 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005853 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005854 spin_unlock(&eb->refs_lock);
5855 spin_unlock(&page->mapping->private_lock);
5856 return 0;
5857 }
5858 spin_unlock(&page->mapping->private_lock);
5859
Josef Bacik3083ee22012-03-09 16:01:49 -05005860 /*
5861 * If tree ref isn't set then we know the ref on this eb is a real ref,
5862 * so just return, this page will likely be freed soon anyway.
5863 */
5864 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5865 spin_unlock(&eb->refs_lock);
5866 return 0;
5867 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005868
David Sterbaf7a52a42013-04-26 14:56:29 +00005869 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005870}