blob: ff1f7b4ac02c0a594e6b626851d03db809759356 [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 -0500112struct tree_entry {
113 u64 start;
114 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -0500115 struct rb_node rb_node;
116};
117
118struct extent_page_data {
119 struct bio *bio;
120 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -0500121 /* tells writepage not to lock the state bits for this range
122 * it still does the unlocking
123 */
Chris Masonffbd5172009-04-20 15:50:09 -0400124 unsigned int extent_locked:1;
125
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600126 /* tells the submit_bio code to use REQ_SYNC */
Chris Masonffbd5172009-04-20 15:50:09 -0400127 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -0500128};
129
David Sterba57599c72018-03-01 17:56:34 +0100130static int add_extent_changeset(struct extent_state *state, unsigned bits,
Qu Wenruod38ed272015-10-12 14:53:37 +0800131 struct extent_changeset *changeset,
132 int set)
133{
134 int ret;
135
136 if (!changeset)
David Sterba57599c72018-03-01 17:56:34 +0100137 return 0;
Qu Wenruod38ed272015-10-12 14:53:37 +0800138 if (set && (state->state & bits) == bits)
David Sterba57599c72018-03-01 17:56:34 +0100139 return 0;
Qu Wenruofefdc552015-10-12 15:35:38 +0800140 if (!set && (state->state & bits) == 0)
David Sterba57599c72018-03-01 17:56:34 +0100141 return 0;
Qu Wenruod38ed272015-10-12 14:53:37 +0800142 changeset->bytes_changed += state->end - state->start + 1;
David Sterba53d32352017-02-13 13:42:29 +0100143 ret = ulist_add(&changeset->range_changed, state->start, state->end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800144 GFP_ATOMIC);
David Sterba57599c72018-03-01 17:56:34 +0100145 return ret;
Qu Wenruod38ed272015-10-12 14:53:37 +0800146}
147
Qu Wenruobb58eb92019-01-25 13:09:15 +0800148static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
149 unsigned long bio_flags)
150{
151 blk_status_t ret = 0;
152 struct bio_vec *bvec = bio_last_bvec_all(bio);
Linus Torvalds80201fe2019-03-08 14:12:17 -0800153 struct bio_vec bv;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800154 struct extent_io_tree *tree = bio->bi_private;
155 u64 start;
156
Linus Torvalds80201fe2019-03-08 14:12:17 -0800157 mp_bvec_last_segment(bvec, &bv);
158 start = page_offset(bv.bv_page) + bv.bv_offset;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800159
160 bio->bi_private = NULL;
161
162 if (tree->ops)
163 ret = tree->ops->submit_bio_hook(tree->private_data, bio,
164 mirror_num, bio_flags, start);
165 else
166 btrfsic_submit_bio(bio);
167
168 return blk_status_to_errno(ret);
169}
170
Qu Wenruo30659762019-03-20 14:27:42 +0800171/* Cleanup unsubmitted bios */
172static void end_write_bio(struct extent_page_data *epd, int ret)
173{
174 if (epd->bio) {
175 epd->bio->bi_status = errno_to_blk_status(ret);
176 bio_endio(epd->bio);
177 epd->bio = NULL;
178 }
179}
180
Qu Wenruof4340622019-03-20 14:27:41 +0800181/*
182 * Submit bio from extent page data via submit_one_bio
183 *
184 * Return 0 if everything is OK.
185 * Return <0 for error.
186 */
187static int __must_check flush_write_bio(struct extent_page_data *epd)
Qu Wenruobb58eb92019-01-25 13:09:15 +0800188{
Qu Wenruof4340622019-03-20 14:27:41 +0800189 int ret = 0;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800190
Qu Wenruof4340622019-03-20 14:27:41 +0800191 if (epd->bio) {
Qu Wenruobb58eb92019-01-25 13:09:15 +0800192 ret = submit_one_bio(epd->bio, 0, 0);
Qu Wenruof4340622019-03-20 14:27:41 +0800193 /*
194 * Clean up of epd->bio is handled by its endio function.
195 * And endio is either triggered by successful bio execution
196 * or the error handler of submit bio hook.
197 * So at this point, no matter what happened, we don't need
198 * to clean up epd->bio.
199 */
Qu Wenruobb58eb92019-01-25 13:09:15 +0800200 epd->bio = NULL;
201 }
Qu Wenruof4340622019-03-20 14:27:41 +0800202 return ret;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800203}
David Sterbae2932ee2017-06-23 04:16:17 +0200204
Chris Masond1310b22008-01-24 16:13:08 -0500205int __init extent_io_init(void)
206{
David Sterba837e1972012-09-07 03:00:48 -0600207 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200208 sizeof(struct extent_state), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300209 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500210 if (!extent_state_cache)
211 return -ENOMEM;
212
David Sterba837e1972012-09-07 03:00:48 -0600213 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200214 sizeof(struct extent_buffer), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300215 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500216 if (!extent_buffer_cache)
217 goto free_state_cache;
Chris Mason9be33952013-05-17 18:30:14 -0400218
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400219 if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
220 offsetof(struct btrfs_io_bio, bio),
221 BIOSET_NEED_BVECS))
Chris Mason9be33952013-05-17 18:30:14 -0400222 goto free_buffer_cache;
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700223
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400224 if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE))
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700225 goto free_bioset;
226
Chris Masond1310b22008-01-24 16:13:08 -0500227 return 0;
228
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700229free_bioset:
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400230 bioset_exit(&btrfs_bioset);
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700231
Chris Mason9be33952013-05-17 18:30:14 -0400232free_buffer_cache:
233 kmem_cache_destroy(extent_buffer_cache);
234 extent_buffer_cache = NULL;
235
Chris Masond1310b22008-01-24 16:13:08 -0500236free_state_cache:
237 kmem_cache_destroy(extent_state_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400238 extent_state_cache = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500239 return -ENOMEM;
240}
241
David Sterbae67c7182018-02-19 17:24:18 +0100242void __cold extent_io_exit(void)
Chris Masond1310b22008-01-24 16:13:08 -0500243{
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000244 btrfs_leak_debug_check();
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000245
246 /*
247 * Make sure all delayed rcu free are flushed before we
248 * destroy caches.
249 */
250 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +0800251 kmem_cache_destroy(extent_state_cache);
252 kmem_cache_destroy(extent_buffer_cache);
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400253 bioset_exit(&btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -0500254}
255
Qu Wenruoc258d6e2019-03-01 10:47:58 +0800256void extent_io_tree_init(struct btrfs_fs_info *fs_info,
Qu Wenruo43eb5f22019-03-01 10:47:59 +0800257 struct extent_io_tree *tree, unsigned int owner,
258 void *private_data)
Chris Masond1310b22008-01-24 16:13:08 -0500259{
Qu Wenruoc258d6e2019-03-01 10:47:58 +0800260 tree->fs_info = fs_info;
Eric Paris6bef4d32010-02-23 19:43:04 +0000261 tree->state = RB_ROOT;
Chris Masond1310b22008-01-24 16:13:08 -0500262 tree->ops = NULL;
263 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500264 spin_lock_init(&tree->lock);
Josef Bacikc6100a42017-05-05 11:57:13 -0400265 tree->private_data = private_data;
Qu Wenruo43eb5f22019-03-01 10:47:59 +0800266 tree->owner = owner;
Chris Masond1310b22008-01-24 16:13:08 -0500267}
Chris Masond1310b22008-01-24 16:13:08 -0500268
Nikolay Borisov41e7acd2019-03-25 14:31:24 +0200269void extent_io_tree_release(struct extent_io_tree *tree)
270{
271 spin_lock(&tree->lock);
272 /*
273 * Do a single barrier for the waitqueue_active check here, the state
274 * of the waitqueue should not change once extent_io_tree_release is
275 * called.
276 */
277 smp_mb();
278 while (!RB_EMPTY_ROOT(&tree->state)) {
279 struct rb_node *node;
280 struct extent_state *state;
281
282 node = rb_first(&tree->state);
283 state = rb_entry(node, struct extent_state, rb_node);
284 rb_erase(&state->rb_node, &tree->state);
285 RB_CLEAR_NODE(&state->rb_node);
286 /*
287 * btree io trees aren't supposed to have tasks waiting for
288 * changes in the flags of extent states ever.
289 */
290 ASSERT(!waitqueue_active(&state->wq));
291 free_extent_state(state);
292
293 cond_resched_lock(&tree->lock);
294 }
295 spin_unlock(&tree->lock);
296}
297
Christoph Hellwigb2950862008-12-02 09:54:17 -0500298static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500299{
300 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500301
Michal Hocko3ba7ab22017-01-09 15:39:02 +0100302 /*
303 * The given mask might be not appropriate for the slab allocator,
304 * drop the unsupported bits
305 */
306 mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
Chris Masond1310b22008-01-24 16:13:08 -0500307 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400308 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500309 return state;
310 state->state = 0;
David Sterba47dc1962016-02-11 13:24:13 +0100311 state->failrec = NULL;
Filipe Manana27a35072014-07-06 20:09:59 +0100312 RB_CLEAR_NODE(&state->rb_node);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000313 btrfs_leak_debug_add(&state->leak_list, &states);
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200314 refcount_set(&state->refs, 1);
Chris Masond1310b22008-01-24 16:13:08 -0500315 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100316 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500317 return state;
318}
Chris Masond1310b22008-01-24 16:13:08 -0500319
Chris Mason4845e442010-05-25 20:56:50 -0400320void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500321{
Chris Masond1310b22008-01-24 16:13:08 -0500322 if (!state)
323 return;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200324 if (refcount_dec_and_test(&state->refs)) {
Filipe Manana27a35072014-07-06 20:09:59 +0100325 WARN_ON(extent_state_in_tree(state));
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000326 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100327 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500328 kmem_cache_free(extent_state_cache, state);
329 }
330}
Chris Masond1310b22008-01-24 16:13:08 -0500331
Filipe Mananaf2071b22014-02-12 15:05:53 +0000332static struct rb_node *tree_insert(struct rb_root *root,
333 struct rb_node *search_start,
334 u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000335 struct rb_node *node,
336 struct rb_node ***p_in,
337 struct rb_node **parent_in)
Chris Masond1310b22008-01-24 16:13:08 -0500338{
Filipe Mananaf2071b22014-02-12 15:05:53 +0000339 struct rb_node **p;
Chris Masond3977122009-01-05 21:25:51 -0500340 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500341 struct tree_entry *entry;
342
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000343 if (p_in && parent_in) {
344 p = *p_in;
345 parent = *parent_in;
346 goto do_insert;
347 }
348
Filipe Mananaf2071b22014-02-12 15:05:53 +0000349 p = search_start ? &search_start : &root->rb_node;
Chris Masond3977122009-01-05 21:25:51 -0500350 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500351 parent = *p;
352 entry = rb_entry(parent, struct tree_entry, rb_node);
353
354 if (offset < entry->start)
355 p = &(*p)->rb_left;
356 else if (offset > entry->end)
357 p = &(*p)->rb_right;
358 else
359 return parent;
360 }
361
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000362do_insert:
Chris Masond1310b22008-01-24 16:13:08 -0500363 rb_link_node(node, parent, p);
364 rb_insert_color(node, root);
365 return NULL;
366}
367
Chris Mason80ea96b2008-02-01 14:51:59 -0500368static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000369 struct rb_node **next_ret,
Nikolay Borisov352646c2019-01-30 16:51:00 +0200370 struct rb_node **prev_ret,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000371 struct rb_node ***p_ret,
372 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500373{
Chris Mason80ea96b2008-02-01 14:51:59 -0500374 struct rb_root *root = &tree->state;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000375 struct rb_node **n = &root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500376 struct rb_node *prev = NULL;
377 struct rb_node *orig_prev = NULL;
378 struct tree_entry *entry;
379 struct tree_entry *prev_entry = NULL;
380
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000381 while (*n) {
382 prev = *n;
383 entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500384 prev_entry = entry;
385
386 if (offset < entry->start)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000387 n = &(*n)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500388 else if (offset > entry->end)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000389 n = &(*n)->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500390 else
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000391 return *n;
Chris Masond1310b22008-01-24 16:13:08 -0500392 }
393
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000394 if (p_ret)
395 *p_ret = n;
396 if (parent_ret)
397 *parent_ret = prev;
398
Nikolay Borisov352646c2019-01-30 16:51:00 +0200399 if (next_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500400 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500401 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500402 prev = rb_next(prev);
403 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
404 }
Nikolay Borisov352646c2019-01-30 16:51:00 +0200405 *next_ret = prev;
Chris Masond1310b22008-01-24 16:13:08 -0500406 prev = orig_prev;
407 }
408
Nikolay Borisov352646c2019-01-30 16:51:00 +0200409 if (prev_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500410 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500411 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500412 prev = rb_prev(prev);
413 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
414 }
Nikolay Borisov352646c2019-01-30 16:51:00 +0200415 *prev_ret = prev;
Chris Masond1310b22008-01-24 16:13:08 -0500416 }
417 return NULL;
418}
419
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000420static inline struct rb_node *
421tree_search_for_insert(struct extent_io_tree *tree,
422 u64 offset,
423 struct rb_node ***p_ret,
424 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500425{
Nikolay Borisov352646c2019-01-30 16:51:00 +0200426 struct rb_node *next= NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500427 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500428
Nikolay Borisov352646c2019-01-30 16:51:00 +0200429 ret = __etree_search(tree, offset, &next, NULL, p_ret, parent_ret);
Chris Masond3977122009-01-05 21:25:51 -0500430 if (!ret)
Nikolay Borisov352646c2019-01-30 16:51:00 +0200431 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500432 return ret;
433}
434
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000435static inline struct rb_node *tree_search(struct extent_io_tree *tree,
436 u64 offset)
437{
438 return tree_search_for_insert(tree, offset, NULL, NULL);
439}
440
Chris Masond1310b22008-01-24 16:13:08 -0500441/*
442 * utility function to look for merge candidates inside a given range.
443 * Any extents with matching state are merged together into a single
444 * extent in the tree. Extents with EXTENT_IO in their state field
445 * are not merged because the end_io handlers need to be able to do
446 * operations on them without sleeping (or doing allocations/splits).
447 *
448 * This should be called with the tree lock held.
449 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000450static void merge_state(struct extent_io_tree *tree,
451 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500452{
453 struct extent_state *other;
454 struct rb_node *other_node;
455
Nikolay Borisov88826792019-03-14 15:28:31 +0200456 if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000457 return;
Chris Masond1310b22008-01-24 16:13:08 -0500458
459 other_node = rb_prev(&state->rb_node);
460 if (other_node) {
461 other = rb_entry(other_node, struct extent_state, rb_node);
462 if (other->end == state->start - 1 &&
463 other->state == state->state) {
Nikolay Borisov5c848192018-11-01 14:09:52 +0200464 if (tree->private_data &&
465 is_data_inode(tree->private_data))
466 btrfs_merge_delalloc_extent(tree->private_data,
467 state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500468 state->start = other->start;
Chris Masond1310b22008-01-24 16:13:08 -0500469 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100470 RB_CLEAR_NODE(&other->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500471 free_extent_state(other);
472 }
473 }
474 other_node = rb_next(&state->rb_node);
475 if (other_node) {
476 other = rb_entry(other_node, struct extent_state, rb_node);
477 if (other->start == state->end + 1 &&
478 other->state == state->state) {
Nikolay Borisov5c848192018-11-01 14:09:52 +0200479 if (tree->private_data &&
480 is_data_inode(tree->private_data))
481 btrfs_merge_delalloc_extent(tree->private_data,
482 state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400483 state->end = other->end;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400484 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100485 RB_CLEAR_NODE(&other->rb_node);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400486 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500487 }
488 }
Chris Masond1310b22008-01-24 16:13:08 -0500489}
490
Xiao Guangrong3150b692011-07-14 03:19:08 +0000491static void set_state_bits(struct extent_io_tree *tree,
Qu Wenruod38ed272015-10-12 14:53:37 +0800492 struct extent_state *state, unsigned *bits,
493 struct extent_changeset *changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000494
Chris Masond1310b22008-01-24 16:13:08 -0500495/*
496 * insert an extent_state struct into the tree. 'bits' are set on the
497 * struct before it is inserted.
498 *
499 * This may return -EEXIST if the extent is already there, in which case the
500 * state struct is freed.
501 *
502 * The tree lock is not taken internally. This is a utility function and
503 * probably isn't what you want to call (see set/clear_extent_bit).
504 */
505static int insert_state(struct extent_io_tree *tree,
506 struct extent_state *state, u64 start, u64 end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000507 struct rb_node ***p,
508 struct rb_node **parent,
Qu Wenruod38ed272015-10-12 14:53:37 +0800509 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500510{
511 struct rb_node *node;
512
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000513 if (end < start)
Frank Holtonefe120a2013-12-20 11:37:06 -0500514 WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200515 end, start);
Chris Masond1310b22008-01-24 16:13:08 -0500516 state->start = start;
517 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400518
Qu Wenruod38ed272015-10-12 14:53:37 +0800519 set_state_bits(tree, state, bits, changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000520
Filipe Mananaf2071b22014-02-12 15:05:53 +0000521 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
Chris Masond1310b22008-01-24 16:13:08 -0500522 if (node) {
523 struct extent_state *found;
524 found = rb_entry(node, struct extent_state, rb_node);
Jeff Mahoney62e85572016-09-20 10:05:01 -0400525 pr_err("BTRFS: found node %llu %llu on insert of %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200526 found->start, found->end, start, end);
Chris Masond1310b22008-01-24 16:13:08 -0500527 return -EEXIST;
528 }
529 merge_state(tree, state);
530 return 0;
531}
532
533/*
534 * split a given extent state struct in two, inserting the preallocated
535 * struct 'prealloc' as the newly created second half. 'split' indicates an
536 * offset inside 'orig' where it should be split.
537 *
538 * Before calling,
539 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
540 * are two extent state structs in the tree:
541 * prealloc: [orig->start, split - 1]
542 * orig: [ split, orig->end ]
543 *
544 * The tree locks are not taken by this function. They need to be held
545 * by the caller.
546 */
547static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
548 struct extent_state *prealloc, u64 split)
549{
550 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400551
Nikolay Borisovabbb55f2018-11-01 14:09:53 +0200552 if (tree->private_data && is_data_inode(tree->private_data))
553 btrfs_split_delalloc_extent(tree->private_data, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400554
Chris Masond1310b22008-01-24 16:13:08 -0500555 prealloc->start = orig->start;
556 prealloc->end = split - 1;
557 prealloc->state = orig->state;
558 orig->start = split;
559
Filipe Mananaf2071b22014-02-12 15:05:53 +0000560 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
561 &prealloc->rb_node, NULL, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500562 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500563 free_extent_state(prealloc);
564 return -EEXIST;
565 }
566 return 0;
567}
568
Li Zefancdc6a392012-03-12 16:39:48 +0800569static struct extent_state *next_state(struct extent_state *state)
570{
571 struct rb_node *next = rb_next(&state->rb_node);
572 if (next)
573 return rb_entry(next, struct extent_state, rb_node);
574 else
575 return NULL;
576}
577
Chris Masond1310b22008-01-24 16:13:08 -0500578/*
579 * utility function to clear some bits in an extent state struct.
Andrea Gelmini52042d82018-11-28 12:05:13 +0100580 * it will optionally wake up anyone waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500581 *
582 * If no bits are set on the state struct after clearing things, the
583 * struct is freed and removed from the tree
584 */
Li Zefancdc6a392012-03-12 16:39:48 +0800585static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
586 struct extent_state *state,
Qu Wenruofefdc552015-10-12 15:35:38 +0800587 unsigned *bits, int wake,
588 struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500589{
Li Zefancdc6a392012-03-12 16:39:48 +0800590 struct extent_state *next;
David Sterba9ee49a042015-01-14 19:52:13 +0100591 unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100592 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500593
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400594 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500595 u64 range = state->end - state->start + 1;
596 WARN_ON(range > tree->dirty_bytes);
597 tree->dirty_bytes -= range;
598 }
Nikolay Borisova36bb5f2018-11-01 14:09:51 +0200599
600 if (tree->private_data && is_data_inode(tree->private_data))
601 btrfs_clear_delalloc_extent(tree->private_data, state, bits);
602
David Sterba57599c72018-03-01 17:56:34 +0100603 ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
604 BUG_ON(ret < 0);
Josef Bacik32c00af2009-10-08 13:34:05 -0400605 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500606 if (wake)
607 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400608 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800609 next = next_state(state);
Filipe Manana27a35072014-07-06 20:09:59 +0100610 if (extent_state_in_tree(state)) {
Chris Masond1310b22008-01-24 16:13:08 -0500611 rb_erase(&state->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100612 RB_CLEAR_NODE(&state->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500613 free_extent_state(state);
614 } else {
615 WARN_ON(1);
616 }
617 } else {
618 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800619 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500620 }
Li Zefancdc6a392012-03-12 16:39:48 +0800621 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500622}
623
Xiao Guangrong82337672011-04-20 06:44:57 +0000624static struct extent_state *
625alloc_extent_state_atomic(struct extent_state *prealloc)
626{
627 if (!prealloc)
628 prealloc = alloc_extent_state(GFP_ATOMIC);
629
630 return prealloc;
631}
632
Eric Sandeen48a3b632013-04-25 20:41:01 +0000633static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400634{
David Sterba05912a32018-07-18 19:23:45 +0200635 struct inode *inode = tree->private_data;
636
637 btrfs_panic(btrfs_sb(inode->i_sb), err,
638 "locking error: extent tree was modified by another thread while locked");
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400639}
640
Chris Masond1310b22008-01-24 16:13:08 -0500641/*
642 * clear some bits on a range in the tree. This may require splitting
643 * or inserting elements in the tree, so the gfp mask is used to
644 * indicate which allocations or sleeping are allowed.
645 *
646 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
647 * the given range from the tree regardless of state (ie for truncate).
648 *
649 * the range [start, end] is inclusive.
650 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100651 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500652 */
David Sterba66b0c882017-10-31 16:30:47 +0100653int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Qu Wenruofefdc552015-10-12 15:35:38 +0800654 unsigned bits, int wake, int delete,
655 struct extent_state **cached_state,
656 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500657{
658 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400659 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500660 struct extent_state *prealloc = NULL;
661 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400662 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500663 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000664 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500665
Josef Bacika5dee372013-12-13 10:02:44 -0500666 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +0800667 trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits);
David Sterba8d599ae2013-04-30 15:22:23 +0000668
Josef Bacik7ee9e442013-06-21 16:37:03 -0400669 if (bits & EXTENT_DELALLOC)
670 bits |= EXTENT_NORESERVE;
671
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400672 if (delete)
673 bits |= ~EXTENT_CTLBITS;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400674
Nikolay Borisov88826792019-03-14 15:28:31 +0200675 if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY))
Josef Bacik2ac55d42010-02-03 19:33:23 +0000676 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500677again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800678 if (!prealloc && gfpflags_allow_blocking(mask)) {
Filipe Mananac7bc6312014-11-03 14:12:57 +0000679 /*
680 * Don't care for allocation failure here because we might end
681 * up not needing the pre-allocated extent state at all, which
682 * is the case if we only have in the tree extent states that
683 * cover our input range and don't cover too any other range.
684 * If we end up needing a new extent state we allocate it later.
685 */
Chris Masond1310b22008-01-24 16:13:08 -0500686 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500687 }
688
Chris Masoncad321a2008-12-17 14:51:42 -0500689 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400690 if (cached_state) {
691 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000692
693 if (clear) {
694 *cached_state = NULL;
695 cached_state = NULL;
696 }
697
Filipe Manana27a35072014-07-06 20:09:59 +0100698 if (cached && extent_state_in_tree(cached) &&
699 cached->start <= start && cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000700 if (clear)
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200701 refcount_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400702 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400703 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400704 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000705 if (clear)
706 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400707 }
Chris Masond1310b22008-01-24 16:13:08 -0500708 /*
709 * this search will find the extents that end after
710 * our range starts
711 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500712 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500713 if (!node)
714 goto out;
715 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400716hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500717 if (state->start > end)
718 goto out;
719 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400720 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500721
Liu Bo04493142012-02-16 18:34:37 +0800722 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800723 if (!(state->state & bits)) {
724 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800725 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800726 }
Liu Bo04493142012-02-16 18:34:37 +0800727
Chris Masond1310b22008-01-24 16:13:08 -0500728 /*
729 * | ---- desired range ---- |
730 * | state | or
731 * | ------------- state -------------- |
732 *
733 * We need to split the extent we found, and may flip
734 * bits on second half.
735 *
736 * If the extent we found extends past our range, we
737 * just split and search again. It'll get split again
738 * the next time though.
739 *
740 * If the extent we found is inside our range, we clear
741 * the desired bit on it.
742 */
743
744 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000745 prealloc = alloc_extent_state_atomic(prealloc);
746 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500747 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400748 if (err)
749 extent_io_tree_panic(tree, err);
750
Chris Masond1310b22008-01-24 16:13:08 -0500751 prealloc = NULL;
752 if (err)
753 goto out;
754 if (state->end <= end) {
Qu Wenruofefdc552015-10-12 15:35:38 +0800755 state = clear_state_bit(tree, state, &bits, wake,
756 changeset);
Liu Bod1ac6e42012-05-10 18:10:39 +0800757 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500758 }
759 goto search_again;
760 }
761 /*
762 * | ---- desired range ---- |
763 * | state |
764 * We need to split the extent, and clear the bit
765 * on the first half
766 */
767 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000768 prealloc = alloc_extent_state_atomic(prealloc);
769 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500770 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400771 if (err)
772 extent_io_tree_panic(tree, err);
773
Chris Masond1310b22008-01-24 16:13:08 -0500774 if (wake)
775 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400776
Qu Wenruofefdc552015-10-12 15:35:38 +0800777 clear_state_bit(tree, prealloc, &bits, wake, changeset);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400778
Chris Masond1310b22008-01-24 16:13:08 -0500779 prealloc = NULL;
780 goto out;
781 }
Chris Mason42daec22009-09-23 19:51:09 -0400782
Qu Wenruofefdc552015-10-12 15:35:38 +0800783 state = clear_state_bit(tree, state, &bits, wake, changeset);
Liu Bo04493142012-02-16 18:34:37 +0800784next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400785 if (last_end == (u64)-1)
786 goto out;
787 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800788 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800789 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500790
791search_again:
792 if (start > end)
793 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500794 spin_unlock(&tree->lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800795 if (gfpflags_allow_blocking(mask))
Chris Masond1310b22008-01-24 16:13:08 -0500796 cond_resched();
797 goto again;
David Sterba7ab5cb22016-04-27 01:02:15 +0200798
799out:
800 spin_unlock(&tree->lock);
801 if (prealloc)
802 free_extent_state(prealloc);
803
804 return 0;
805
Chris Masond1310b22008-01-24 16:13:08 -0500806}
Chris Masond1310b22008-01-24 16:13:08 -0500807
Jeff Mahoney143bede2012-03-01 14:56:26 +0100808static void wait_on_state(struct extent_io_tree *tree,
809 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500810 __releases(tree->lock)
811 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500812{
813 DEFINE_WAIT(wait);
814 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500815 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500816 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500817 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500818 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500819}
820
821/*
822 * waits for one or more bits to clear on a range in the state tree.
823 * The range [start, end] is inclusive.
824 * The tree lock is taken by this function
825 */
David Sterba41074882013-04-29 13:38:46 +0000826static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
827 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500828{
829 struct extent_state *state;
830 struct rb_node *node;
831
Josef Bacika5dee372013-12-13 10:02:44 -0500832 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000833
Chris Masoncad321a2008-12-17 14:51:42 -0500834 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500835again:
836 while (1) {
837 /*
838 * this search will find all the extents that end after
839 * our range starts
840 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500841 node = tree_search(tree, start);
Filipe Mananac50d3e72014-03-31 14:53:25 +0100842process_node:
Chris Masond1310b22008-01-24 16:13:08 -0500843 if (!node)
844 break;
845
846 state = rb_entry(node, struct extent_state, rb_node);
847
848 if (state->start > end)
849 goto out;
850
851 if (state->state & bits) {
852 start = state->start;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200853 refcount_inc(&state->refs);
Chris Masond1310b22008-01-24 16:13:08 -0500854 wait_on_state(tree, state);
855 free_extent_state(state);
856 goto again;
857 }
858 start = state->end + 1;
859
860 if (start > end)
861 break;
862
Filipe Mananac50d3e72014-03-31 14:53:25 +0100863 if (!cond_resched_lock(&tree->lock)) {
864 node = rb_next(node);
865 goto process_node;
866 }
Chris Masond1310b22008-01-24 16:13:08 -0500867 }
868out:
Chris Masoncad321a2008-12-17 14:51:42 -0500869 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500870}
Chris Masond1310b22008-01-24 16:13:08 -0500871
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000872static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500873 struct extent_state *state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800874 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500875{
David Sterba9ee49a042015-01-14 19:52:13 +0100876 unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100877 int ret;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400878
Nikolay Borisove06a1fc2018-11-01 14:09:50 +0200879 if (tree->private_data && is_data_inode(tree->private_data))
880 btrfs_set_delalloc_extent(tree->private_data, state, bits);
881
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400882 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500883 u64 range = state->end - state->start + 1;
884 tree->dirty_bytes += range;
885 }
David Sterba57599c72018-03-01 17:56:34 +0100886 ret = add_extent_changeset(state, bits_to_set, changeset, 1);
887 BUG_ON(ret < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400888 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500889}
890
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100891static void cache_state_if_flags(struct extent_state *state,
892 struct extent_state **cached_ptr,
David Sterba9ee49a042015-01-14 19:52:13 +0100893 unsigned flags)
Chris Mason2c64c532009-09-02 15:04:12 -0400894{
895 if (cached_ptr && !(*cached_ptr)) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100896 if (!flags || (state->state & flags)) {
Chris Mason2c64c532009-09-02 15:04:12 -0400897 *cached_ptr = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200898 refcount_inc(&state->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400899 }
900 }
901}
902
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100903static void cache_state(struct extent_state *state,
904 struct extent_state **cached_ptr)
905{
906 return cache_state_if_flags(state, cached_ptr,
Nikolay Borisov88826792019-03-14 15:28:31 +0200907 EXTENT_LOCKED | EXTENT_BOUNDARY);
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100908}
909
Chris Masond1310b22008-01-24 16:13:08 -0500910/*
Chris Mason1edbb732009-09-02 13:24:36 -0400911 * set some bits on a range in the tree. This may require allocations or
912 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500913 *
Chris Mason1edbb732009-09-02 13:24:36 -0400914 * If any of the exclusive bits are set, this will fail with -EEXIST if some
915 * part of the range already has the desired bits set. The start of the
916 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500917 *
Chris Mason1edbb732009-09-02 13:24:36 -0400918 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500919 */
Chris Mason1edbb732009-09-02 13:24:36 -0400920
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100921static int __must_check
922__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +0100923 unsigned bits, unsigned exclusive_bits,
David Sterba41074882013-04-29 13:38:46 +0000924 u64 *failed_start, struct extent_state **cached_state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800925 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500926{
927 struct extent_state *state;
928 struct extent_state *prealloc = NULL;
929 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000930 struct rb_node **p;
931 struct rb_node *parent;
Chris Masond1310b22008-01-24 16:13:08 -0500932 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500933 u64 last_start;
934 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400935
Josef Bacika5dee372013-12-13 10:02:44 -0500936 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +0800937 trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits);
David Sterba8d599ae2013-04-30 15:22:23 +0000938
Chris Masond1310b22008-01-24 16:13:08 -0500939again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800940 if (!prealloc && gfpflags_allow_blocking(mask)) {
David Sterba059f7912016-04-27 01:03:45 +0200941 /*
942 * Don't care for allocation failure here because we might end
943 * up not needing the pre-allocated extent state at all, which
944 * is the case if we only have in the tree extent states that
945 * cover our input range and don't cover too any other range.
946 * If we end up needing a new extent state we allocate it later.
947 */
Chris Masond1310b22008-01-24 16:13:08 -0500948 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500949 }
950
Chris Masoncad321a2008-12-17 14:51:42 -0500951 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400952 if (cached_state && *cached_state) {
953 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400954 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +0100955 extent_state_in_tree(state)) {
Chris Mason9655d292009-09-02 15:22:30 -0400956 node = &state->rb_node;
957 goto hit_next;
958 }
959 }
Chris Masond1310b22008-01-24 16:13:08 -0500960 /*
961 * this search will find all the extents that end after
962 * our range starts.
963 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000964 node = tree_search_for_insert(tree, start, &p, &parent);
Chris Masond1310b22008-01-24 16:13:08 -0500965 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000966 prealloc = alloc_extent_state_atomic(prealloc);
967 BUG_ON(!prealloc);
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000968 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800969 &p, &parent, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400970 if (err)
971 extent_io_tree_panic(tree, err);
972
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +0000973 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500974 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500975 goto out;
976 }
Chris Masond1310b22008-01-24 16:13:08 -0500977 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400978hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500979 last_start = state->start;
980 last_end = state->end;
981
982 /*
983 * | ---- desired range ---- |
984 * | state |
985 *
986 * Just lock what we found and keep going
987 */
988 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400989 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500990 *failed_start = state->start;
991 err = -EEXIST;
992 goto out;
993 }
Chris Mason42daec22009-09-23 19:51:09 -0400994
Qu Wenruod38ed272015-10-12 14:53:37 +0800995 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400996 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500997 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400998 if (last_end == (u64)-1)
999 goto out;
1000 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001001 state = next_state(state);
1002 if (start < end && state && state->start == start &&
1003 !need_resched())
1004 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -05001005 goto search_again;
1006 }
1007
1008 /*
1009 * | ---- desired range ---- |
1010 * | state |
1011 * or
1012 * | ------------- state -------------- |
1013 *
1014 * We need to split the extent we found, and may flip bits on
1015 * second half.
1016 *
1017 * If the extent we found extends past our
1018 * range, we just split and search again. It'll get split
1019 * again the next time though.
1020 *
1021 * If the extent we found is inside our range, we set the
1022 * desired bit on it.
1023 */
1024 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -04001025 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001026 *failed_start = start;
1027 err = -EEXIST;
1028 goto out;
1029 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001030
1031 prealloc = alloc_extent_state_atomic(prealloc);
1032 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001033 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001034 if (err)
1035 extent_io_tree_panic(tree, err);
1036
Chris Masond1310b22008-01-24 16:13:08 -05001037 prealloc = NULL;
1038 if (err)
1039 goto out;
1040 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001041 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001042 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001043 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -04001044 if (last_end == (u64)-1)
1045 goto out;
1046 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001047 state = next_state(state);
1048 if (start < end && state && state->start == start &&
1049 !need_resched())
1050 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -05001051 }
1052 goto search_again;
1053 }
1054 /*
1055 * | ---- desired range ---- |
1056 * | state | or | state |
1057 *
1058 * There's a hole, we need to insert something in it and
1059 * ignore the extent we found.
1060 */
1061 if (state->start > start) {
1062 u64 this_end;
1063 if (end < last_start)
1064 this_end = end;
1065 else
Chris Masond3977122009-01-05 21:25:51 -05001066 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +00001067
1068 prealloc = alloc_extent_state_atomic(prealloc);
1069 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +00001070
1071 /*
1072 * Avoid to free 'prealloc' if it can be merged with
1073 * the later extent.
1074 */
Chris Masond1310b22008-01-24 16:13:08 -05001075 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001076 NULL, NULL, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001077 if (err)
1078 extent_io_tree_panic(tree, err);
1079
Chris Mason2c64c532009-09-02 15:04:12 -04001080 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001081 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001082 start = this_end + 1;
1083 goto search_again;
1084 }
1085 /*
1086 * | ---- desired range ---- |
1087 * | state |
1088 * We need to split the extent, and set the bit
1089 * on the first half
1090 */
1091 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -04001092 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001093 *failed_start = start;
1094 err = -EEXIST;
1095 goto out;
1096 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001097
1098 prealloc = alloc_extent_state_atomic(prealloc);
1099 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001100 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001101 if (err)
1102 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -05001103
Qu Wenruod38ed272015-10-12 14:53:37 +08001104 set_state_bits(tree, prealloc, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001105 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001106 merge_state(tree, prealloc);
1107 prealloc = NULL;
1108 goto out;
1109 }
1110
David Sterbab5a4ba142016-04-27 01:02:15 +02001111search_again:
1112 if (start > end)
1113 goto out;
1114 spin_unlock(&tree->lock);
1115 if (gfpflags_allow_blocking(mask))
1116 cond_resched();
1117 goto again;
Chris Masond1310b22008-01-24 16:13:08 -05001118
1119out:
Chris Masoncad321a2008-12-17 14:51:42 -05001120 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001121 if (prealloc)
1122 free_extent_state(prealloc);
1123
1124 return err;
1125
Chris Masond1310b22008-01-24 16:13:08 -05001126}
Chris Masond1310b22008-01-24 16:13:08 -05001127
David Sterba41074882013-04-29 13:38:46 +00001128int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001129 unsigned bits, u64 * failed_start,
David Sterba41074882013-04-29 13:38:46 +00001130 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001131{
1132 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001133 cached_state, mask, NULL);
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001134}
1135
1136
Josef Bacik462d6fa2011-09-26 13:56:12 -04001137/**
Liu Bo10983f22012-07-11 15:26:19 +08001138 * convert_extent_bit - convert all bits in a given range from one bit to
1139 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -04001140 * @tree: the io tree to search
1141 * @start: the start offset in bytes
1142 * @end: the end offset in bytes (inclusive)
1143 * @bits: the bits to set in this range
1144 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001145 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001146 *
1147 * This will go through and set bits for the given range. If any states exist
1148 * already in this range they are set with the given bit and cleared of the
1149 * clear_bits. This is only meant to be used by things that are mergeable, ie
1150 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1151 * boundary bits like LOCK.
David Sterba210aa272016-04-26 23:54:39 +02001152 *
1153 * All allocations are done with GFP_NOFS.
Josef Bacik462d6fa2011-09-26 13:56:12 -04001154 */
1155int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001156 unsigned bits, unsigned clear_bits,
David Sterba210aa272016-04-26 23:54:39 +02001157 struct extent_state **cached_state)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001158{
1159 struct extent_state *state;
1160 struct extent_state *prealloc = NULL;
1161 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001162 struct rb_node **p;
1163 struct rb_node *parent;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001164 int err = 0;
1165 u64 last_start;
1166 u64 last_end;
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001167 bool first_iteration = true;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001168
Josef Bacika5dee372013-12-13 10:02:44 -05001169 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +08001170 trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits,
1171 clear_bits);
David Sterba8d599ae2013-04-30 15:22:23 +00001172
Josef Bacik462d6fa2011-09-26 13:56:12 -04001173again:
David Sterba210aa272016-04-26 23:54:39 +02001174 if (!prealloc) {
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001175 /*
1176 * Best effort, don't worry if extent state allocation fails
1177 * here for the first iteration. We might have a cached state
1178 * that matches exactly the target range, in which case no
1179 * extent state allocations are needed. We'll only know this
1180 * after locking the tree.
1181 */
David Sterba210aa272016-04-26 23:54:39 +02001182 prealloc = alloc_extent_state(GFP_NOFS);
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001183 if (!prealloc && !first_iteration)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001184 return -ENOMEM;
1185 }
1186
1187 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001188 if (cached_state && *cached_state) {
1189 state = *cached_state;
1190 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +01001191 extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001192 node = &state->rb_node;
1193 goto hit_next;
1194 }
1195 }
1196
Josef Bacik462d6fa2011-09-26 13:56:12 -04001197 /*
1198 * this search will find all the extents that end after
1199 * our range starts.
1200 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001201 node = tree_search_for_insert(tree, start, &p, &parent);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001202 if (!node) {
1203 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001204 if (!prealloc) {
1205 err = -ENOMEM;
1206 goto out;
1207 }
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001208 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001209 &p, &parent, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001210 if (err)
1211 extent_io_tree_panic(tree, err);
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001212 cache_state(prealloc, cached_state);
1213 prealloc = NULL;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001214 goto out;
1215 }
1216 state = rb_entry(node, struct extent_state, rb_node);
1217hit_next:
1218 last_start = state->start;
1219 last_end = state->end;
1220
1221 /*
1222 * | ---- desired range ---- |
1223 * | state |
1224 *
1225 * Just lock what we found and keep going
1226 */
1227 if (state->start == start && state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001228 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001229 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001230 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001231 if (last_end == (u64)-1)
1232 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001233 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001234 if (start < end && state && state->start == start &&
1235 !need_resched())
1236 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001237 goto search_again;
1238 }
1239
1240 /*
1241 * | ---- desired range ---- |
1242 * | state |
1243 * or
1244 * | ------------- state -------------- |
1245 *
1246 * We need to split the extent we found, and may flip bits on
1247 * second half.
1248 *
1249 * If the extent we found extends past our
1250 * range, we just split and search again. It'll get split
1251 * again the next time though.
1252 *
1253 * If the extent we found is inside our range, we set the
1254 * desired bit on it.
1255 */
1256 if (state->start < start) {
1257 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001258 if (!prealloc) {
1259 err = -ENOMEM;
1260 goto out;
1261 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001262 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001263 if (err)
1264 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001265 prealloc = NULL;
1266 if (err)
1267 goto out;
1268 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001269 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001270 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001271 state = clear_state_bit(tree, state, &clear_bits, 0,
1272 NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001273 if (last_end == (u64)-1)
1274 goto out;
1275 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001276 if (start < end && state && state->start == start &&
1277 !need_resched())
1278 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001279 }
1280 goto search_again;
1281 }
1282 /*
1283 * | ---- desired range ---- |
1284 * | state | or | state |
1285 *
1286 * There's a hole, we need to insert something in it and
1287 * ignore the extent we found.
1288 */
1289 if (state->start > start) {
1290 u64 this_end;
1291 if (end < last_start)
1292 this_end = end;
1293 else
1294 this_end = last_start - 1;
1295
1296 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001297 if (!prealloc) {
1298 err = -ENOMEM;
1299 goto out;
1300 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001301
1302 /*
1303 * Avoid to free 'prealloc' if it can be merged with
1304 * the later extent.
1305 */
1306 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001307 NULL, NULL, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001308 if (err)
1309 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001310 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001311 prealloc = NULL;
1312 start = this_end + 1;
1313 goto search_again;
1314 }
1315 /*
1316 * | ---- desired range ---- |
1317 * | state |
1318 * We need to split the extent, and set the bit
1319 * on the first half
1320 */
1321 if (state->start <= end && state->end > end) {
1322 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001323 if (!prealloc) {
1324 err = -ENOMEM;
1325 goto out;
1326 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001327
1328 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001329 if (err)
1330 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001331
Qu Wenruod38ed272015-10-12 14:53:37 +08001332 set_state_bits(tree, prealloc, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001333 cache_state(prealloc, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001334 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001335 prealloc = NULL;
1336 goto out;
1337 }
1338
Josef Bacik462d6fa2011-09-26 13:56:12 -04001339search_again:
1340 if (start > end)
1341 goto out;
1342 spin_unlock(&tree->lock);
David Sterba210aa272016-04-26 23:54:39 +02001343 cond_resched();
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001344 first_iteration = false;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001345 goto again;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001346
1347out:
1348 spin_unlock(&tree->lock);
1349 if (prealloc)
1350 free_extent_state(prealloc);
1351
1352 return err;
1353}
1354
Chris Masond1310b22008-01-24 16:13:08 -05001355/* wrappers around set/clear extent bit */
Qu Wenruod38ed272015-10-12 14:53:37 +08001356int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba2c53b912016-04-26 23:54:39 +02001357 unsigned bits, struct extent_changeset *changeset)
Qu Wenruod38ed272015-10-12 14:53:37 +08001358{
1359 /*
1360 * We don't support EXTENT_LOCKED yet, as current changeset will
1361 * record any bits changed, so for EXTENT_LOCKED case, it will
1362 * either fail with -EEXIST or changeset will record the whole
1363 * range.
1364 */
1365 BUG_ON(bits & EXTENT_LOCKED);
1366
David Sterba2c53b912016-04-26 23:54:39 +02001367 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
Qu Wenruod38ed272015-10-12 14:53:37 +08001368 changeset);
1369}
1370
Nikolay Borisov4ca73652019-03-27 14:24:10 +02001371int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start, u64 end,
1372 unsigned bits)
1373{
1374 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL,
1375 GFP_NOWAIT, NULL);
1376}
1377
Qu Wenruofefdc552015-10-12 15:35:38 +08001378int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1379 unsigned bits, int wake, int delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001380 struct extent_state **cached)
Qu Wenruofefdc552015-10-12 15:35:38 +08001381{
1382 return __clear_extent_bit(tree, start, end, bits, wake, delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001383 cached, GFP_NOFS, NULL);
Qu Wenruofefdc552015-10-12 15:35:38 +08001384}
1385
Qu Wenruofefdc552015-10-12 15:35:38 +08001386int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaf734c442016-04-26 23:54:39 +02001387 unsigned bits, struct extent_changeset *changeset)
Qu Wenruofefdc552015-10-12 15:35:38 +08001388{
1389 /*
1390 * Don't support EXTENT_LOCKED case, same reason as
1391 * set_record_extent_bits().
1392 */
1393 BUG_ON(bits & EXTENT_LOCKED);
1394
David Sterbaf734c442016-04-26 23:54:39 +02001395 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
Qu Wenruofefdc552015-10-12 15:35:38 +08001396 changeset);
1397}
1398
Chris Masond352ac62008-09-29 15:18:18 -04001399/*
1400 * either insert or lock state struct between start and end use mask to tell
1401 * us if waiting is desired.
1402 */
Chris Mason1edbb732009-09-02 13:24:36 -04001403int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaff13db42015-12-03 14:30:40 +01001404 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001405{
1406 int err;
1407 u64 failed_start;
David Sterba9ee49a042015-01-14 19:52:13 +01001408
Chris Masond1310b22008-01-24 16:13:08 -05001409 while (1) {
David Sterbaff13db42015-12-03 14:30:40 +01001410 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001411 EXTENT_LOCKED, &failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001412 cached_state, GFP_NOFS, NULL);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001413 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001414 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1415 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001416 } else
Chris Masond1310b22008-01-24 16:13:08 -05001417 break;
Chris Masond1310b22008-01-24 16:13:08 -05001418 WARN_ON(start > end);
1419 }
1420 return err;
1421}
Chris Masond1310b22008-01-24 16:13:08 -05001422
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001423int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001424{
1425 int err;
1426 u64 failed_start;
1427
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001428 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
Qu Wenruod38ed272015-10-12 14:53:37 +08001429 &failed_start, NULL, GFP_NOFS, NULL);
Yan Zheng66435582008-10-30 14:19:50 -04001430 if (err == -EEXIST) {
1431 if (failed_start > start)
1432 clear_extent_bit(tree, start, failed_start - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001433 EXTENT_LOCKED, 1, 0, NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001434 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001435 }
Josef Bacik25179202008-10-29 14:49:05 -04001436 return 1;
1437}
Josef Bacik25179202008-10-29 14:49:05 -04001438
David Sterbabd1fa4f2015-12-03 13:08:59 +01001439void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001440{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001441 unsigned long index = start >> PAGE_SHIFT;
1442 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001443 struct page *page;
1444
1445 while (index <= end_index) {
1446 page = find_get_page(inode->i_mapping, index);
1447 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1448 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001449 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001450 index++;
1451 }
Chris Mason4adaa612013-03-26 13:07:00 -04001452}
1453
David Sterbaf6311572015-12-03 13:08:59 +01001454void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001455{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001456 unsigned long index = start >> PAGE_SHIFT;
1457 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001458 struct page *page;
1459
1460 while (index <= end_index) {
1461 page = find_get_page(inode->i_mapping, index);
1462 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Mason4adaa612013-03-26 13:07:00 -04001463 __set_page_dirty_nobuffers(page);
Konstantin Khebnikov8d386332015-02-11 15:26:55 -08001464 account_page_redirty(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001465 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001466 index++;
1467 }
Chris Mason4adaa612013-03-26 13:07:00 -04001468}
1469
Chris Masond352ac62008-09-29 15:18:18 -04001470/* find the first state struct with 'bits' set after 'start', and
1471 * return it. tree->lock must be held. NULL will returned if
1472 * nothing was found after 'start'
1473 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001474static struct extent_state *
1475find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +01001476 u64 start, unsigned bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001477{
1478 struct rb_node *node;
1479 struct extent_state *state;
1480
1481 /*
1482 * this search will find all the extents that end after
1483 * our range starts.
1484 */
1485 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001486 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001487 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001488
Chris Masond3977122009-01-05 21:25:51 -05001489 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001490 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001491 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001492 return state;
Chris Masond3977122009-01-05 21:25:51 -05001493
Chris Masond7fc6402008-02-18 12:12:38 -05001494 node = rb_next(node);
1495 if (!node)
1496 break;
1497 }
1498out:
1499 return NULL;
1500}
Chris Masond7fc6402008-02-18 12:12:38 -05001501
Chris Masond352ac62008-09-29 15:18:18 -04001502/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001503 * find the first offset in the io tree with 'bits' set. zero is
1504 * returned if we find something, and *start_ret and *end_ret are
1505 * set to reflect the state struct that was found.
1506 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001507 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001508 */
1509int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba9ee49a042015-01-14 19:52:13 +01001510 u64 *start_ret, u64 *end_ret, unsigned bits,
Josef Bacike6138872012-09-27 17:07:30 -04001511 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001512{
1513 struct extent_state *state;
1514 int ret = 1;
1515
1516 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001517 if (cached_state && *cached_state) {
1518 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001519 if (state->end == start - 1 && extent_state_in_tree(state)) {
Liu Bo9688e9a2018-08-23 03:14:53 +08001520 while ((state = next_state(state)) != NULL) {
Josef Bacike6138872012-09-27 17:07:30 -04001521 if (state->state & bits)
1522 goto got_it;
Josef Bacike6138872012-09-27 17:07:30 -04001523 }
1524 free_extent_state(*cached_state);
1525 *cached_state = NULL;
1526 goto out;
1527 }
1528 free_extent_state(*cached_state);
1529 *cached_state = NULL;
1530 }
1531
Xiao Guangrong69261c42011-07-14 03:19:45 +00001532 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001533got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001534 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001535 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001536 *start_ret = state->start;
1537 *end_ret = state->end;
1538 ret = 0;
1539 }
Josef Bacike6138872012-09-27 17:07:30 -04001540out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001541 spin_unlock(&tree->lock);
1542 return ret;
1543}
1544
1545/*
Chris Masond352ac62008-09-29 15:18:18 -04001546 * find a contiguous range of bytes in the file marked as delalloc, not
1547 * more than 'max_bytes'. start and end are used to return the range,
1548 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001549 * true is returned if we find something, false if nothing was in the tree
Chris Masond352ac62008-09-29 15:18:18 -04001550 */
Lu Fengqi3522e902018-11-29 11:33:38 +08001551static noinline bool find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001552 u64 *start, u64 *end, u64 max_bytes,
1553 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001554{
1555 struct rb_node *node;
1556 struct extent_state *state;
1557 u64 cur_start = *start;
Lu Fengqi3522e902018-11-29 11:33:38 +08001558 bool found = false;
Chris Masond1310b22008-01-24 16:13:08 -05001559 u64 total_bytes = 0;
1560
Chris Masoncad321a2008-12-17 14:51:42 -05001561 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001562
Chris Masond1310b22008-01-24 16:13:08 -05001563 /*
1564 * this search will find all the extents that end after
1565 * our range starts.
1566 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001567 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001568 if (!node) {
Lu Fengqi3522e902018-11-29 11:33:38 +08001569 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001570 goto out;
1571 }
1572
Chris Masond3977122009-01-05 21:25:51 -05001573 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001574 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001575 if (found && (state->start != cur_start ||
1576 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001577 goto out;
1578 }
1579 if (!(state->state & EXTENT_DELALLOC)) {
1580 if (!found)
1581 *end = state->end;
1582 goto out;
1583 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001584 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001585 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001586 *cached_state = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +02001587 refcount_inc(&state->refs);
Josef Bacikc2a128d2010-02-02 21:19:11 +00001588 }
Lu Fengqi3522e902018-11-29 11:33:38 +08001589 found = true;
Chris Masond1310b22008-01-24 16:13:08 -05001590 *end = state->end;
1591 cur_start = state->end + 1;
1592 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001593 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001594 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001595 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001596 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001597 break;
1598 }
1599out:
Chris Masoncad321a2008-12-17 14:51:42 -05001600 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001601 return found;
1602}
1603
Liu Boda2c7002017-02-10 16:41:05 +01001604static int __process_pages_contig(struct address_space *mapping,
1605 struct page *locked_page,
1606 pgoff_t start_index, pgoff_t end_index,
1607 unsigned long page_ops, pgoff_t *index_ret);
1608
Jeff Mahoney143bede2012-03-01 14:56:26 +01001609static noinline void __unlock_for_delalloc(struct inode *inode,
1610 struct page *locked_page,
1611 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001612{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001613 unsigned long index = start >> PAGE_SHIFT;
1614 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001615
Liu Bo76c00212017-02-10 16:42:14 +01001616 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001617 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001618 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001619
Liu Bo76c00212017-02-10 16:42:14 +01001620 __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1621 PAGE_UNLOCK, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -04001622}
1623
1624static noinline int lock_delalloc_pages(struct inode *inode,
1625 struct page *locked_page,
1626 u64 delalloc_start,
1627 u64 delalloc_end)
1628{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001629 unsigned long index = delalloc_start >> PAGE_SHIFT;
Liu Bo76c00212017-02-10 16:42:14 +01001630 unsigned long index_ret = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001631 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001632 int ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001633
Liu Bo76c00212017-02-10 16:42:14 +01001634 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001635 if (index == locked_page->index && index == end_index)
1636 return 0;
1637
Liu Bo76c00212017-02-10 16:42:14 +01001638 ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1639 end_index, PAGE_LOCK, &index_ret);
1640 if (ret == -EAGAIN)
1641 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1642 (u64)index_ret << PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001643 return ret;
1644}
1645
1646/*
Lu Fengqi3522e902018-11-29 11:33:38 +08001647 * Find and lock a contiguous range of bytes in the file marked as delalloc, no
1648 * more than @max_bytes. @Start and @end are used to return the range,
Chris Masonc8b97812008-10-29 14:49:59 -04001649 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001650 * Return: true if we find something
1651 * false if nothing was in the tree
Chris Masonc8b97812008-10-29 14:49:59 -04001652 */
Johannes Thumshirnce9f9672018-11-19 10:38:17 +01001653EXPORT_FOR_TESTS
Lu Fengqi3522e902018-11-29 11:33:38 +08001654noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
Josef Bacik294e30f2013-10-09 12:00:56 -04001655 struct extent_io_tree *tree,
1656 struct page *locked_page, u64 *start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03001657 u64 *end)
Chris Masonc8b97812008-10-29 14:49:59 -04001658{
Nikolay Borisov917aace2018-10-26 14:43:20 +03001659 u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001660 u64 delalloc_start;
1661 u64 delalloc_end;
Lu Fengqi3522e902018-11-29 11:33:38 +08001662 bool found;
Chris Mason9655d292009-09-02 15:22:30 -04001663 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001664 int ret;
1665 int loops = 0;
1666
1667again:
1668 /* step one, find a bunch of delalloc bytes starting at start */
1669 delalloc_start = *start;
1670 delalloc_end = 0;
1671 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001672 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001673 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001674 *start = delalloc_start;
1675 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001676 free_extent_state(cached_state);
Lu Fengqi3522e902018-11-29 11:33:38 +08001677 return false;
Chris Masonc8b97812008-10-29 14:49:59 -04001678 }
1679
1680 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001681 * start comes from the offset of locked_page. We have to lock
1682 * pages in order, so we can't process delalloc bytes before
1683 * locked_page
1684 */
Chris Masond3977122009-01-05 21:25:51 -05001685 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001686 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001687
1688 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001689 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001690 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001691 if (delalloc_end + 1 - delalloc_start > max_bytes)
1692 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001693
Chris Masonc8b97812008-10-29 14:49:59 -04001694 /* step two, lock all the pages after the page that has start */
1695 ret = lock_delalloc_pages(inode, locked_page,
1696 delalloc_start, delalloc_end);
Nikolay Borisov9bfd61d2018-10-26 14:43:21 +03001697 ASSERT(!ret || ret == -EAGAIN);
Chris Masonc8b97812008-10-29 14:49:59 -04001698 if (ret == -EAGAIN) {
1699 /* some of the pages are gone, lets avoid looping by
1700 * shortening the size of the delalloc range we're searching
1701 */
Chris Mason9655d292009-09-02 15:22:30 -04001702 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001703 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001704 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001705 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001706 loops = 1;
1707 goto again;
1708 } else {
Lu Fengqi3522e902018-11-29 11:33:38 +08001709 found = false;
Chris Masonc8b97812008-10-29 14:49:59 -04001710 goto out_failed;
1711 }
1712 }
Chris Masonc8b97812008-10-29 14:49:59 -04001713
1714 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001715 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001716
1717 /* then test to make sure it is all still delalloc */
1718 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001719 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001720 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001721 unlock_extent_cached(tree, delalloc_start, delalloc_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001722 &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001723 __unlock_for_delalloc(inode, locked_page,
1724 delalloc_start, delalloc_end);
1725 cond_resched();
1726 goto again;
1727 }
Chris Mason9655d292009-09-02 15:22:30 -04001728 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001729 *start = delalloc_start;
1730 *end = delalloc_end;
1731out_failed:
1732 return found;
1733}
1734
Liu Boda2c7002017-02-10 16:41:05 +01001735static int __process_pages_contig(struct address_space *mapping,
1736 struct page *locked_page,
1737 pgoff_t start_index, pgoff_t end_index,
1738 unsigned long page_ops, pgoff_t *index_ret)
Chris Masonc8b97812008-10-29 14:49:59 -04001739{
Liu Bo873695b2017-02-02 17:49:22 -08001740 unsigned long nr_pages = end_index - start_index + 1;
Liu Boda2c7002017-02-10 16:41:05 +01001741 unsigned long pages_locked = 0;
Liu Bo873695b2017-02-02 17:49:22 -08001742 pgoff_t index = start_index;
Chris Masonc8b97812008-10-29 14:49:59 -04001743 struct page *pages[16];
Liu Bo873695b2017-02-02 17:49:22 -08001744 unsigned ret;
Liu Boda2c7002017-02-10 16:41:05 +01001745 int err = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001746 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001747
Liu Boda2c7002017-02-10 16:41:05 +01001748 if (page_ops & PAGE_LOCK) {
1749 ASSERT(page_ops == PAGE_LOCK);
1750 ASSERT(index_ret && *index_ret == start_index);
1751 }
1752
Filipe Manana704de492014-10-06 22:14:22 +01001753 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
Liu Bo873695b2017-02-02 17:49:22 -08001754 mapping_set_error(mapping, -EIO);
Filipe Manana704de492014-10-06 22:14:22 +01001755
Chris Masond3977122009-01-05 21:25:51 -05001756 while (nr_pages > 0) {
Liu Bo873695b2017-02-02 17:49:22 -08001757 ret = find_get_pages_contig(mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001758 min_t(unsigned long,
1759 nr_pages, ARRAY_SIZE(pages)), pages);
Liu Boda2c7002017-02-10 16:41:05 +01001760 if (ret == 0) {
1761 /*
1762 * Only if we're going to lock these pages,
1763 * can we find nothing at @index.
1764 */
1765 ASSERT(page_ops & PAGE_LOCK);
Liu Bo49d4a332017-03-06 18:20:56 -08001766 err = -EAGAIN;
1767 goto out;
Liu Boda2c7002017-02-10 16:41:05 +01001768 }
Chris Mason8b62b722009-09-02 16:53:46 -04001769
Liu Boda2c7002017-02-10 16:41:05 +01001770 for (i = 0; i < ret; i++) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001771 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001772 SetPagePrivate2(pages[i]);
1773
Chris Masonc8b97812008-10-29 14:49:59 -04001774 if (pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001775 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001776 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001777 continue;
1778 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001779 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001780 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001781 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001782 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001783 if (page_ops & PAGE_SET_ERROR)
1784 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001785 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001786 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001787 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001788 unlock_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001789 if (page_ops & PAGE_LOCK) {
1790 lock_page(pages[i]);
1791 if (!PageDirty(pages[i]) ||
1792 pages[i]->mapping != mapping) {
1793 unlock_page(pages[i]);
1794 put_page(pages[i]);
1795 err = -EAGAIN;
1796 goto out;
1797 }
1798 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001799 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001800 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001801 }
1802 nr_pages -= ret;
1803 index += ret;
1804 cond_resched();
1805 }
Liu Boda2c7002017-02-10 16:41:05 +01001806out:
1807 if (err && index_ret)
1808 *index_ret = start_index + pages_locked - 1;
1809 return err;
Chris Masonc8b97812008-10-29 14:49:59 -04001810}
Chris Masonc8b97812008-10-29 14:49:59 -04001811
Liu Bo873695b2017-02-02 17:49:22 -08001812void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1813 u64 delalloc_end, struct page *locked_page,
1814 unsigned clear_bits,
1815 unsigned long page_ops)
1816{
1817 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001818 NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001819
1820 __process_pages_contig(inode->i_mapping, locked_page,
1821 start >> PAGE_SHIFT, end >> PAGE_SHIFT,
Liu Boda2c7002017-02-10 16:41:05 +01001822 page_ops, NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001823}
1824
Chris Masond352ac62008-09-29 15:18:18 -04001825/*
1826 * count the number of bytes in the tree that have a given bit(s)
1827 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1828 * cached. The total number found is returned.
1829 */
Chris Masond1310b22008-01-24 16:13:08 -05001830u64 count_range_bits(struct extent_io_tree *tree,
1831 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001832 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001833{
1834 struct rb_node *node;
1835 struct extent_state *state;
1836 u64 cur_start = *start;
1837 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001838 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001839 int found = 0;
1840
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301841 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001842 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001843
Chris Masoncad321a2008-12-17 14:51:42 -05001844 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001845 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1846 total_bytes = tree->dirty_bytes;
1847 goto out;
1848 }
1849 /*
1850 * this search will find all the extents that end after
1851 * our range starts.
1852 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001853 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001854 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001855 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001856
Chris Masond3977122009-01-05 21:25:51 -05001857 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001858 state = rb_entry(node, struct extent_state, rb_node);
1859 if (state->start > search_end)
1860 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001861 if (contig && found && state->start > last + 1)
1862 break;
1863 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001864 total_bytes += min(search_end, state->end) + 1 -
1865 max(cur_start, state->start);
1866 if (total_bytes >= max_bytes)
1867 break;
1868 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001869 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001870 found = 1;
1871 }
Chris Masonec29ed52011-02-23 16:23:20 -05001872 last = state->end;
1873 } else if (contig && found) {
1874 break;
Chris Masond1310b22008-01-24 16:13:08 -05001875 }
1876 node = rb_next(node);
1877 if (!node)
1878 break;
1879 }
1880out:
Chris Masoncad321a2008-12-17 14:51:42 -05001881 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001882 return total_bytes;
1883}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001884
Chris Masond352ac62008-09-29 15:18:18 -04001885/*
1886 * set the private field for a given byte offset in the tree. If there isn't
1887 * an extent_state there already, this does nothing.
1888 */
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001889static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001890 struct io_failure_record *failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001891{
1892 struct rb_node *node;
1893 struct extent_state *state;
1894 int ret = 0;
1895
Chris Masoncad321a2008-12-17 14:51:42 -05001896 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001897 /*
1898 * this search will find all the extents that end after
1899 * our range starts.
1900 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001901 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001902 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001903 ret = -ENOENT;
1904 goto out;
1905 }
1906 state = rb_entry(node, struct extent_state, rb_node);
1907 if (state->start != start) {
1908 ret = -ENOENT;
1909 goto out;
1910 }
David Sterba47dc1962016-02-11 13:24:13 +01001911 state->failrec = failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001912out:
Chris Masoncad321a2008-12-17 14:51:42 -05001913 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001914 return ret;
1915}
1916
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001917static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001918 struct io_failure_record **failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001919{
1920 struct rb_node *node;
1921 struct extent_state *state;
1922 int ret = 0;
1923
Chris Masoncad321a2008-12-17 14:51:42 -05001924 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001925 /*
1926 * this search will find all the extents that end after
1927 * our range starts.
1928 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001929 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001930 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001931 ret = -ENOENT;
1932 goto out;
1933 }
1934 state = rb_entry(node, struct extent_state, rb_node);
1935 if (state->start != start) {
1936 ret = -ENOENT;
1937 goto out;
1938 }
David Sterba47dc1962016-02-11 13:24:13 +01001939 *failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001940out:
Chris Masoncad321a2008-12-17 14:51:42 -05001941 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001942 return ret;
1943}
1944
1945/*
1946 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001947 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001948 * has the bits set. Otherwise, 1 is returned if any bit in the
1949 * range is found set.
1950 */
1951int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001952 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001953{
1954 struct extent_state *state = NULL;
1955 struct rb_node *node;
1956 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001957
Chris Masoncad321a2008-12-17 14:51:42 -05001958 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01001959 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001960 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001961 node = &cached->rb_node;
1962 else
1963 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001964 while (node && start <= end) {
1965 state = rb_entry(node, struct extent_state, rb_node);
1966
1967 if (filled && state->start > start) {
1968 bitset = 0;
1969 break;
1970 }
1971
1972 if (state->start > end)
1973 break;
1974
1975 if (state->state & bits) {
1976 bitset = 1;
1977 if (!filled)
1978 break;
1979 } else if (filled) {
1980 bitset = 0;
1981 break;
1982 }
Chris Mason46562ce2009-09-23 20:23:16 -04001983
1984 if (state->end == (u64)-1)
1985 break;
1986
Chris Masond1310b22008-01-24 16:13:08 -05001987 start = state->end + 1;
1988 if (start > end)
1989 break;
1990 node = rb_next(node);
1991 if (!node) {
1992 if (filled)
1993 bitset = 0;
1994 break;
1995 }
1996 }
Chris Masoncad321a2008-12-17 14:51:42 -05001997 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001998 return bitset;
1999}
Chris Masond1310b22008-01-24 16:13:08 -05002000
2001/*
2002 * helper function to set a given page up to date if all the
2003 * extents in the tree for that page are up to date
2004 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002005static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05002006{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002007 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002008 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04002009 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05002010 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002011}
2012
Josef Bacik7870d082017-05-05 11:57:15 -04002013int free_io_failure(struct extent_io_tree *failure_tree,
2014 struct extent_io_tree *io_tree,
2015 struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002016{
2017 int ret;
2018 int err = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002019
David Sterba47dc1962016-02-11 13:24:13 +01002020 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002021 ret = clear_extent_bits(failure_tree, rec->start,
2022 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02002023 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002024 if (ret)
2025 err = ret;
2026
Josef Bacik7870d082017-05-05 11:57:15 -04002027 ret = clear_extent_bits(io_tree, rec->start,
David Woodhouse53b381b2013-01-29 18:40:14 -05002028 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02002029 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05002030 if (ret && !err)
2031 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002032
2033 kfree(rec);
2034 return err;
2035}
2036
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002037/*
2038 * this bypasses the standard btrfs submit functions deliberately, as
2039 * the standard behavior is to write all copies in a raid setup. here we only
2040 * want to write the one bad copy. so we do the mapping for ourselves and issue
2041 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002042 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002043 * actually prevents the read that triggered the error from finishing.
2044 * currently, there can be no more than two copies of every data bit. thus,
2045 * exactly one rewrite is required.
2046 */
Josef Bacik6ec656b2017-05-05 11:57:14 -04002047int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
2048 u64 length, u64 logical, struct page *page,
2049 unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002050{
2051 struct bio *bio;
2052 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002053 u64 map_length = 0;
2054 u64 sector;
2055 struct btrfs_bio *bbio = NULL;
2056 int ret;
2057
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002058 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002059 BUG_ON(!mirror_num);
2060
David Sterbac5e4c3d2017-06-12 17:29:41 +02002061 bio = btrfs_io_bio_alloc(1);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002062 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002063 map_length = length;
2064
Filipe Mananab5de8d02016-05-27 22:21:27 +01002065 /*
2066 * Avoid races with device replace and make sure our bbio has devices
2067 * associated to its stripes that don't go away while we are doing the
2068 * read repair operation.
2069 */
2070 btrfs_bio_counter_inc_blocked(fs_info);
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03002071 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
Liu Boc7253282017-03-29 10:53:58 -07002072 /*
2073 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
2074 * to update all raid stripes, but here we just want to correct
2075 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2076 * stripe's dev and sector.
2077 */
2078 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2079 &map_length, &bbio, 0);
2080 if (ret) {
2081 btrfs_bio_counter_dec(fs_info);
2082 bio_put(bio);
2083 return -EIO;
2084 }
2085 ASSERT(bbio->mirror_num == 1);
2086 } else {
2087 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2088 &map_length, &bbio, mirror_num);
2089 if (ret) {
2090 btrfs_bio_counter_dec(fs_info);
2091 bio_put(bio);
2092 return -EIO;
2093 }
2094 BUG_ON(mirror_num != bbio->mirror_num);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002095 }
Liu Boc7253282017-03-29 10:53:58 -07002096
2097 sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002098 bio->bi_iter.bi_sector = sector;
Liu Boc7253282017-03-29 10:53:58 -07002099 dev = bbio->stripes[bbio->mirror_num - 1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002100 btrfs_put_bbio(bbio);
Anand Jainebbede42017-12-04 12:54:52 +08002101 if (!dev || !dev->bdev ||
2102 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002103 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002104 bio_put(bio);
2105 return -EIO;
2106 }
Christoph Hellwig74d46992017-08-23 19:10:32 +02002107 bio_set_dev(bio, dev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002108 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Miao Xieffdd2012014-09-12 18:44:00 +08002109 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002110
Mike Christie4e49ea42016-06-05 14:31:41 -05002111 if (btrfsic_submit_bio_wait(bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002112 /* try to remap that extent elsewhere? */
Filipe Mananab5de8d02016-05-27 22:21:27 +01002113 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002114 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002115 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002116 return -EIO;
2117 }
2118
David Sterbab14af3b2015-10-08 10:43:10 +02002119 btrfs_info_rl_in_rcu(fs_info,
2120 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Josef Bacik6ec656b2017-05-05 11:57:14 -04002121 ino, start,
Miao Xie1203b682014-09-12 18:44:01 +08002122 rcu_str_deref(dev->name), sector);
Filipe Mananab5de8d02016-05-27 22:21:27 +01002123 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002124 bio_put(bio);
2125 return 0;
2126}
2127
David Sterba20a1fbf92019-03-20 11:23:44 +01002128int btrfs_repair_eb_io_failure(struct extent_buffer *eb, int mirror_num)
Josef Bacikea466792012-03-26 21:57:36 -04002129{
David Sterba20a1fbf92019-03-20 11:23:44 +01002130 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacikea466792012-03-26 21:57:36 -04002131 u64 start = eb->start;
David Sterbacc5e31a2018-03-01 18:20:27 +01002132 int i, num_pages = num_extent_pages(eb);
Chris Masond95603b2012-04-12 15:55:15 -04002133 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002134
David Howellsbc98a422017-07-17 08:45:34 +01002135 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002136 return -EROFS;
2137
Josef Bacikea466792012-03-26 21:57:36 -04002138 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002139 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002140
Josef Bacik6ec656b2017-05-05 11:57:14 -04002141 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002142 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002143 if (ret)
2144 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002145 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002146 }
2147
2148 return ret;
2149}
2150
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002151/*
2152 * each time an IO finishes, we do a fast check in the IO failure tree
2153 * to see if we need to process or clean up an io_failure_record
2154 */
Josef Bacik7870d082017-05-05 11:57:15 -04002155int clean_io_failure(struct btrfs_fs_info *fs_info,
2156 struct extent_io_tree *failure_tree,
2157 struct extent_io_tree *io_tree, u64 start,
2158 struct page *page, u64 ino, unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002159{
2160 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002161 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002162 struct extent_state *state;
2163 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002164 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002165
2166 private = 0;
Josef Bacik7870d082017-05-05 11:57:15 -04002167 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2168 EXTENT_DIRTY, 0);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002169 if (!ret)
2170 return 0;
2171
Josef Bacik7870d082017-05-05 11:57:15 -04002172 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002173 if (ret)
2174 return 0;
2175
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002176 BUG_ON(!failrec->this_mirror);
2177
2178 if (failrec->in_validation) {
2179 /* there was no real error, just free the record */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002180 btrfs_debug(fs_info,
2181 "clean_io_failure: freeing dummy error at %llu",
2182 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002183 goto out;
2184 }
David Howellsbc98a422017-07-17 08:45:34 +01002185 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002186 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002187
Josef Bacik7870d082017-05-05 11:57:15 -04002188 spin_lock(&io_tree->lock);
2189 state = find_first_extent_bit_state(io_tree,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002190 failrec->start,
2191 EXTENT_LOCKED);
Josef Bacik7870d082017-05-05 11:57:15 -04002192 spin_unlock(&io_tree->lock);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002193
Miao Xie883d0de2013-07-25 19:22:35 +08002194 if (state && state->start <= failrec->start &&
2195 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002196 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2197 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002198 if (num_copies > 1) {
Josef Bacik7870d082017-05-05 11:57:15 -04002199 repair_io_failure(fs_info, ino, start, failrec->len,
2200 failrec->logical, page, pg_offset,
2201 failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002202 }
2203 }
2204
2205out:
Josef Bacik7870d082017-05-05 11:57:15 -04002206 free_io_failure(failure_tree, io_tree, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002207
Miao Xie454ff3d2014-09-12 18:43:58 +08002208 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002209}
2210
Miao Xief6124962014-09-12 18:44:04 +08002211/*
2212 * Can be called when
2213 * - hold extent lock
2214 * - under ordered extent
2215 * - the inode is freeing
2216 */
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002217void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
Miao Xief6124962014-09-12 18:44:04 +08002218{
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002219 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
Miao Xief6124962014-09-12 18:44:04 +08002220 struct io_failure_record *failrec;
2221 struct extent_state *state, *next;
2222
2223 if (RB_EMPTY_ROOT(&failure_tree->state))
2224 return;
2225
2226 spin_lock(&failure_tree->lock);
2227 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2228 while (state) {
2229 if (state->start > end)
2230 break;
2231
2232 ASSERT(state->end <= end);
2233
2234 next = next_state(state);
2235
David Sterba47dc1962016-02-11 13:24:13 +01002236 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002237 free_extent_state(state);
2238 kfree(failrec);
2239
2240 state = next;
2241 }
2242 spin_unlock(&failure_tree->lock);
2243}
2244
Miao Xie2fe63032014-09-12 18:43:59 +08002245int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
David Sterba47dc1962016-02-11 13:24:13 +01002246 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002247{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002248 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002249 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002250 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002251 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2252 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2253 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002254 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002255 u64 logical;
2256
David Sterba47dc1962016-02-11 13:24:13 +01002257 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002258 if (ret) {
2259 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2260 if (!failrec)
2261 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002262
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002263 failrec->start = start;
2264 failrec->len = end - start + 1;
2265 failrec->this_mirror = 0;
2266 failrec->bio_flags = 0;
2267 failrec->in_validation = 0;
2268
2269 read_lock(&em_tree->lock);
2270 em = lookup_extent_mapping(em_tree, start, failrec->len);
2271 if (!em) {
2272 read_unlock(&em_tree->lock);
2273 kfree(failrec);
2274 return -EIO;
2275 }
2276
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002277 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002278 free_extent_map(em);
2279 em = NULL;
2280 }
2281 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002282 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002283 kfree(failrec);
2284 return -EIO;
2285 }
Miao Xie2fe63032014-09-12 18:43:59 +08002286
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002287 logical = start - em->start;
2288 logical = em->block_start + logical;
2289 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2290 logical = em->block_start;
2291 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2292 extent_set_compress_type(&failrec->bio_flags,
2293 em->compress_type);
2294 }
Miao Xie2fe63032014-09-12 18:43:59 +08002295
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002296 btrfs_debug(fs_info,
2297 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2298 logical, start, failrec->len);
Miao Xie2fe63032014-09-12 18:43:59 +08002299
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002300 failrec->logical = logical;
2301 free_extent_map(em);
2302
2303 /* set the bits in the private failure tree */
2304 ret = set_extent_bits(failure_tree, start, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002305 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002306 if (ret >= 0)
David Sterba47dc1962016-02-11 13:24:13 +01002307 ret = set_state_failrec(failure_tree, start, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002308 /* set the bits in the inode's tree */
2309 if (ret >= 0)
David Sterbaceeb0ae2016-04-26 23:54:39 +02002310 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002311 if (ret < 0) {
2312 kfree(failrec);
2313 return ret;
2314 }
2315 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002316 btrfs_debug(fs_info,
2317 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2318 failrec->logical, failrec->start, failrec->len,
2319 failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002320 /*
2321 * when data can be on disk more than twice, add to failrec here
2322 * (e.g. with a list for failed_mirror) to make
2323 * clean_io_failure() clean all those errors at once.
2324 */
2325 }
Miao Xie2fe63032014-09-12 18:43:59 +08002326
2327 *failrec_ret = failrec;
2328
2329 return 0;
2330}
2331
Ming Leia0b60d72017-12-18 20:22:11 +08002332bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages,
Miao Xie2fe63032014-09-12 18:43:59 +08002333 struct io_failure_record *failrec, int failed_mirror)
2334{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002335 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002336 int num_copies;
2337
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002338 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002339 if (num_copies == 1) {
2340 /*
2341 * we only have a single copy of the data, so don't bother with
2342 * all the retry and error correction code that follows. no
2343 * matter what the error is, it is very likely to persist.
2344 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002345 btrfs_debug(fs_info,
2346 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2347 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002348 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002349 }
2350
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002351 /*
2352 * there are two premises:
2353 * a) deliver good data to the caller
2354 * b) correct the bad sectors on disk
2355 */
Ming Leia0b60d72017-12-18 20:22:11 +08002356 if (failed_bio_pages > 1) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002357 /*
2358 * to fulfill b), we need to know the exact failing sectors, as
2359 * we don't want to rewrite any more than the failed ones. thus,
2360 * we need separate read requests for the failed bio
2361 *
2362 * if the following BUG_ON triggers, our validation request got
2363 * merged. we need separate requests for our algorithm to work.
2364 */
2365 BUG_ON(failrec->in_validation);
2366 failrec->in_validation = 1;
2367 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002368 } else {
2369 /*
2370 * we're ready to fulfill a) and b) alongside. get a good copy
2371 * of the failed sector and if we succeed, we have setup
2372 * everything for repair_io_failure to do the rest for us.
2373 */
2374 if (failrec->in_validation) {
2375 BUG_ON(failrec->this_mirror != failed_mirror);
2376 failrec->in_validation = 0;
2377 failrec->this_mirror = 0;
2378 }
2379 failrec->failed_mirror = failed_mirror;
2380 failrec->this_mirror++;
2381 if (failrec->this_mirror == failed_mirror)
2382 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002383 }
2384
Miao Xiefacc8a222013-07-25 19:22:34 +08002385 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002386 btrfs_debug(fs_info,
2387 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2388 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002389 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002390 }
2391
Liu Boc3cfb652017-07-13 15:00:50 -07002392 return true;
Miao Xie2fe63032014-09-12 18:43:59 +08002393}
2394
2395
2396struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2397 struct io_failure_record *failrec,
2398 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002399 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002400{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002401 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002402 struct bio *bio;
2403 struct btrfs_io_bio *btrfs_failed_bio;
2404 struct btrfs_io_bio *btrfs_bio;
2405
David Sterbac5e4c3d2017-06-12 17:29:41 +02002406 bio = btrfs_io_bio_alloc(1);
Miao Xie2fe63032014-09-12 18:43:59 +08002407 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002408 bio->bi_iter.bi_sector = failrec->logical >> 9;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002409 bio_set_dev(bio, fs_info->fs_devices->latest_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002410 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002411 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002412
Miao Xiefacc8a222013-07-25 19:22:34 +08002413 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2414 if (btrfs_failed_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002415 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2416
2417 btrfs_bio = btrfs_io_bio(bio);
2418 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002419 icsum *= csum_size;
2420 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002421 csum_size);
2422 }
2423
Miao Xie2fe63032014-09-12 18:43:59 +08002424 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002425
Miao Xie2fe63032014-09-12 18:43:59 +08002426 return bio;
2427}
2428
2429/*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002430 * This is a generic handler for readpage errors. If other copies exist, read
2431 * those and write back good data to the failed position. Does not investigate
2432 * in remapping the failed extent elsewhere, hoping the device will be smart
2433 * enough to do this as needed
Miao Xie2fe63032014-09-12 18:43:59 +08002434 */
Miao Xie2fe63032014-09-12 18:43:59 +08002435static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2436 struct page *page, u64 start, u64 end,
2437 int failed_mirror)
2438{
2439 struct io_failure_record *failrec;
2440 struct inode *inode = page->mapping->host;
2441 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002442 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie2fe63032014-09-12 18:43:59 +08002443 struct bio *bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002444 int read_mode = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002445 blk_status_t status;
Miao Xie2fe63032014-09-12 18:43:59 +08002446 int ret;
Christoph Hellwig8a2ee442019-02-15 19:13:07 +08002447 unsigned failed_bio_pages = failed_bio->bi_iter.bi_size >> PAGE_SHIFT;
Miao Xie2fe63032014-09-12 18:43:59 +08002448
Mike Christie1f7ad752016-06-05 14:31:51 -05002449 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002450
2451 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2452 if (ret)
2453 return ret;
2454
Ming Leia0b60d72017-12-18 20:22:11 +08002455 if (!btrfs_check_repairable(inode, failed_bio_pages, failrec,
Liu Boc3cfb652017-07-13 15:00:50 -07002456 failed_mirror)) {
Josef Bacik7870d082017-05-05 11:57:15 -04002457 free_io_failure(failure_tree, tree, failrec);
Miao Xie2fe63032014-09-12 18:43:59 +08002458 return -EIO;
2459 }
2460
Ming Leia0b60d72017-12-18 20:22:11 +08002461 if (failed_bio_pages > 1)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002462 read_mode |= REQ_FAILFAST_DEV;
Miao Xie2fe63032014-09-12 18:43:59 +08002463
2464 phy_offset >>= inode->i_sb->s_blocksize_bits;
2465 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2466 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002467 (int)phy_offset, failed_bio->bi_end_io,
2468 NULL);
David Sterbaebcc3262018-06-29 10:56:53 +02002469 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie2fe63032014-09-12 18:43:59 +08002470
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002471 btrfs_debug(btrfs_sb(inode->i_sb),
2472 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2473 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002474
Linus Torvalds8c27cb32017-07-05 16:41:23 -07002475 status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002476 failrec->bio_flags, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002477 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04002478 free_io_failure(failure_tree, tree, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002479 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002480 ret = blk_status_to_errno(status);
Miao Xie6c387ab2014-09-12 18:43:57 +08002481 }
2482
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002483 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002484}
2485
Chris Masond1310b22008-01-24 16:13:08 -05002486/* lots and lots of room for performance fixes in the end_bio funcs */
2487
David Sterbab5227c02015-12-03 13:08:59 +01002488void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002489{
2490 int uptodate = (err == 0);
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002491 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002492
Nikolay Borisovc6297322018-11-08 10:18:08 +02002493 btrfs_writepage_endio_finish_ordered(page, start, end, uptodate);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002494
Jeff Mahoney87826df2012-02-15 16:23:57 +01002495 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002496 ClearPageUptodate(page);
2497 SetPageError(page);
Colin Ian Kingbff5baf2017-05-09 18:14:01 +01002498 ret = err < 0 ? err : -EIO;
Liu Bo5dca6ee2014-05-12 12:47:36 +08002499 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002500 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002501}
2502
Chris Masond1310b22008-01-24 16:13:08 -05002503/*
2504 * after a writepage IO is done, we need to:
2505 * clear the uptodate bits on error
2506 * clear the writeback bits in the extent tree for this IO
2507 * end_page_writeback if the page has no more pending IO
2508 *
2509 * Scheduling is not allowed, so the extent state tree is expected
2510 * to have one and only one object corresponding to this IO.
2511 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002512static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002513{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002514 int error = blk_status_to_errno(bio->bi_status);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002515 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002516 u64 start;
2517 u64 end;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002518 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08002519 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002520
David Sterbac09abff2017-07-13 18:10:07 +02002521 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08002522 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002523 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002524 struct inode *inode = page->mapping->host;
2525 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002526
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002527 /* We always issue full-page reads, but if some block
2528 * in a page fails to read, blk_update_request() will
2529 * advance bv_offset and adjust bv_len to compensate.
2530 * Print a warning for nonzero offsets, and an error
2531 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002532 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2533 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002534 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002535 "partial page write in btrfs with offset %u and length %u",
2536 bvec->bv_offset, bvec->bv_len);
2537 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002538 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002539 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002540 bvec->bv_offset, bvec->bv_len);
2541 }
Chris Masond1310b22008-01-24 16:13:08 -05002542
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002543 start = page_offset(page);
2544 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002545
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002546 end_extent_writepage(page, error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002547 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002548 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002549
Chris Masond1310b22008-01-24 16:13:08 -05002550 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002551}
2552
Miao Xie883d0de2013-07-25 19:22:35 +08002553static void
2554endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2555 int uptodate)
2556{
2557 struct extent_state *cached = NULL;
2558 u64 end = start + len - 1;
2559
2560 if (uptodate && tree->track_uptodate)
2561 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
David Sterbad810a4b2017-12-07 18:52:54 +01002562 unlock_extent_cached_atomic(tree, start, end, &cached);
Miao Xie883d0de2013-07-25 19:22:35 +08002563}
2564
Chris Masond1310b22008-01-24 16:13:08 -05002565/*
2566 * after a readpage IO is done, we need to:
2567 * clear the uptodate bits on error
2568 * set the uptodate bits if things worked
2569 * set the page up to date if all extents in the tree are uptodate
2570 * clear the lock bit in the extent tree
2571 * unlock the page if there are no other extents locked for it
2572 *
2573 * Scheduling is not allowed, so the extent state tree is expected
2574 * to have one and only one object corresponding to this IO.
2575 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002576static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002577{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002578 struct bio_vec *bvec;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002579 int uptodate = !bio->bi_status;
Miao Xiefacc8a222013-07-25 19:22:34 +08002580 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04002581 struct extent_io_tree *tree, *failure_tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002582 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002583 u64 start;
2584 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002585 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002586 u64 extent_start = 0;
2587 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002588 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002589 int ret;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002590 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08002591 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002592
David Sterbac09abff2017-07-13 18:10:07 +02002593 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08002594 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002595 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002596 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002597 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002598 bool data_inode = btrfs_ino(BTRFS_I(inode))
2599 != BTRFS_BTREE_INODE_OBJECTID;
Arne Jansen507903b2011-04-06 10:02:20 +00002600
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002601 btrfs_debug(fs_info,
2602 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002603 (u64)bio->bi_iter.bi_sector, bio->bi_status,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002604 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002605 tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002606 failure_tree = &BTRFS_I(inode)->io_failure_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002607
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002608 /* We always issue full-page reads, but if some block
2609 * in a page fails to read, blk_update_request() will
2610 * advance bv_offset and adjust bv_len to compensate.
2611 * Print a warning for nonzero offsets, and an error
2612 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002613 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2614 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002615 btrfs_err(fs_info,
2616 "partial page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002617 bvec->bv_offset, bvec->bv_len);
2618 else
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002619 btrfs_info(fs_info,
2620 "incomplete page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002621 bvec->bv_offset, bvec->bv_len);
2622 }
Chris Masond1310b22008-01-24 16:13:08 -05002623
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002624 start = page_offset(page);
2625 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002626 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002627
Chris Mason9be33952013-05-17 18:30:14 -04002628 mirror = io_bio->mirror_num;
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002629 if (likely(uptodate)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002630 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2631 page, start, end,
2632 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002633 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002634 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002635 else
Josef Bacik7870d082017-05-05 11:57:15 -04002636 clean_io_failure(BTRFS_I(inode)->root->fs_info,
2637 failure_tree, tree, start,
2638 page,
2639 btrfs_ino(BTRFS_I(inode)), 0);
Chris Masond1310b22008-01-24 16:13:08 -05002640 }
Josef Bacikea466792012-03-26 21:57:36 -04002641
Miao Xief2a09da2013-07-25 19:22:33 +08002642 if (likely(uptodate))
2643 goto readpage_ok;
2644
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002645 if (data_inode) {
Liu Bo9d0d1c82017-03-24 15:04:50 -07002646
2647 /*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002648 * The generic bio_readpage_error handles errors the
2649 * following way: If possible, new read requests are
2650 * created and submitted and will end up in
2651 * end_bio_extent_readpage as well (if we're lucky,
2652 * not in the !uptodate case). In that case it returns
2653 * 0 and we just go on with the next page in our bio.
2654 * If it can't handle the error it will return -EIO and
2655 * we remain responsible for that page.
Liu Bo9d0d1c82017-03-24 15:04:50 -07002656 */
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002657 ret = bio_readpage_error(bio, offset, page, start, end,
2658 mirror);
2659 if (ret == 0) {
2660 uptodate = !bio->bi_status;
2661 offset += len;
2662 continue;
2663 }
2664 } else {
2665 struct extent_buffer *eb;
2666
2667 eb = (struct extent_buffer *)page->private;
2668 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
2669 eb->read_mirror = mirror;
2670 atomic_dec(&eb->io_pages);
2671 if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD,
2672 &eb->bflags))
2673 btree_readahead_hook(eb, -EIO);
Chris Mason7e383262008-04-09 16:28:12 -04002674 }
Miao Xief2a09da2013-07-25 19:22:33 +08002675readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002676 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002677 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002678 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002679 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002680
2681 /* Zero out the end if this page straddles i_size */
Johannes Thumshirn70730172018-12-05 15:23:03 +01002682 off = offset_in_page(i_size);
Liu Boa583c022014-08-19 23:32:22 +08002683 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002684 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002685 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002686 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002687 ClearPageUptodate(page);
2688 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002689 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002690 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002691 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002692
2693 if (unlikely(!uptodate)) {
2694 if (extent_len) {
2695 endio_readpage_release_extent(tree,
2696 extent_start,
2697 extent_len, 1);
2698 extent_start = 0;
2699 extent_len = 0;
2700 }
2701 endio_readpage_release_extent(tree, start,
2702 end - start + 1, 0);
2703 } else if (!extent_len) {
2704 extent_start = start;
2705 extent_len = end + 1 - start;
2706 } else if (extent_start + extent_len == start) {
2707 extent_len += end + 1 - start;
2708 } else {
2709 endio_readpage_release_extent(tree, extent_start,
2710 extent_len, uptodate);
2711 extent_start = start;
2712 extent_len = end + 1 - start;
2713 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002714 }
Chris Masond1310b22008-01-24 16:13:08 -05002715
Miao Xie883d0de2013-07-25 19:22:35 +08002716 if (extent_len)
2717 endio_readpage_release_extent(tree, extent_start, extent_len,
2718 uptodate);
David Sterbab3a0dd52018-11-22 17:16:49 +01002719 btrfs_io_bio_free_csum(io_bio);
Chris Masond1310b22008-01-24 16:13:08 -05002720 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002721}
2722
Chris Mason9be33952013-05-17 18:30:14 -04002723/*
David Sterba184f9992017-06-12 17:29:39 +02002724 * Initialize the members up to but not including 'bio'. Use after allocating a
2725 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
2726 * 'bio' because use of __GFP_ZERO is not supported.
Chris Mason9be33952013-05-17 18:30:14 -04002727 */
David Sterba184f9992017-06-12 17:29:39 +02002728static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
Chris Masond1310b22008-01-24 16:13:08 -05002729{
David Sterba184f9992017-06-12 17:29:39 +02002730 memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
2731}
2732
2733/*
David Sterba6e707bc2017-06-02 17:26:26 +02002734 * The following helpers allocate a bio. As it's backed by a bioset, it'll
2735 * never fail. We're returning a bio right now but you can call btrfs_io_bio
2736 * for the appropriate container_of magic
Chris Masond1310b22008-01-24 16:13:08 -05002737 */
David Sterbac821e7f32017-06-02 18:35:36 +02002738struct bio *btrfs_bio_alloc(struct block_device *bdev, u64 first_byte)
Chris Masond1310b22008-01-24 16:13:08 -05002739{
2740 struct bio *bio;
2741
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002742 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset);
Christoph Hellwig74d46992017-08-23 19:10:32 +02002743 bio_set_dev(bio, bdev);
David Sterbac821e7f32017-06-02 18:35:36 +02002744 bio->bi_iter.bi_sector = first_byte >> 9;
David Sterba184f9992017-06-12 17:29:39 +02002745 btrfs_io_bio_init(btrfs_io_bio(bio));
Chris Masond1310b22008-01-24 16:13:08 -05002746 return bio;
2747}
2748
David Sterba8b6c1d52017-06-02 17:48:13 +02002749struct bio *btrfs_bio_clone(struct bio *bio)
Chris Mason9be33952013-05-17 18:30:14 -04002750{
Miao Xie23ea8e52014-09-12 18:43:54 +08002751 struct btrfs_io_bio *btrfs_bio;
2752 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002753
David Sterba6e707bc2017-06-02 17:26:26 +02002754 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002755 new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset);
David Sterba6e707bc2017-06-02 17:26:26 +02002756 btrfs_bio = btrfs_io_bio(new);
David Sterba184f9992017-06-12 17:29:39 +02002757 btrfs_io_bio_init(btrfs_bio);
David Sterba6e707bc2017-06-02 17:26:26 +02002758 btrfs_bio->iter = bio->bi_iter;
Miao Xie23ea8e52014-09-12 18:43:54 +08002759 return new;
2760}
Chris Mason9be33952013-05-17 18:30:14 -04002761
David Sterbac5e4c3d2017-06-12 17:29:41 +02002762struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
Chris Mason9be33952013-05-17 18:30:14 -04002763{
Miao Xiefacc8a222013-07-25 19:22:34 +08002764 struct bio *bio;
2765
David Sterba6e707bc2017-06-02 17:26:26 +02002766 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002767 bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
David Sterba184f9992017-06-12 17:29:39 +02002768 btrfs_io_bio_init(btrfs_io_bio(bio));
Miao Xiefacc8a222013-07-25 19:22:34 +08002769 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002770}
2771
Liu Boe4770942017-05-16 10:57:14 -07002772struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
Liu Bo2f8e9142017-05-15 17:43:31 -07002773{
2774 struct bio *bio;
2775 struct btrfs_io_bio *btrfs_bio;
2776
2777 /* this will never fail when it's backed by a bioset */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002778 bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset);
Liu Bo2f8e9142017-05-15 17:43:31 -07002779 ASSERT(bio);
2780
2781 btrfs_bio = btrfs_io_bio(bio);
David Sterba184f9992017-06-12 17:29:39 +02002782 btrfs_io_bio_init(btrfs_bio);
Liu Bo2f8e9142017-05-15 17:43:31 -07002783
2784 bio_trim(bio, offset >> 9, size >> 9);
Liu Bo17347ce2017-05-15 15:33:27 -07002785 btrfs_bio->iter = bio->bi_iter;
Liu Bo2f8e9142017-05-15 17:43:31 -07002786 return bio;
2787}
Chris Mason9be33952013-05-17 18:30:14 -04002788
David Sterba4b81ba42017-06-06 19:14:26 +02002789/*
2790 * @opf: bio REQ_OP_* and REQ_* flags as one value
David Sterbab8b3d622017-06-12 19:50:41 +02002791 * @tree: tree so we can call our merge_bio hook
2792 * @wbc: optional writeback control for io accounting
2793 * @page: page to add to the bio
2794 * @pg_offset: offset of the new bio or to check whether we are adding
2795 * a contiguous page to the previous one
2796 * @size: portion of page that we want to write
2797 * @offset: starting offset in the page
2798 * @bdev: attach newly created bios to this bdev
David Sterba5c2b1fd2017-06-06 19:22:55 +02002799 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
David Sterbab8b3d622017-06-12 19:50:41 +02002800 * @end_io_func: end_io callback for new bio
2801 * @mirror_num: desired mirror to read/write
2802 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
2803 * @bio_flags: flags of the current bio to see if we can merge them
David Sterba4b81ba42017-06-06 19:14:26 +02002804 */
2805static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002806 struct writeback_control *wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02002807 struct page *page, u64 offset,
David Sterba6c5a4e22017-10-04 17:10:34 +02002808 size_t size, unsigned long pg_offset,
Chris Masond1310b22008-01-24 16:13:08 -05002809 struct block_device *bdev,
2810 struct bio **bio_ret,
Chris Masonf1885912008-04-09 16:28:12 -04002811 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002812 int mirror_num,
2813 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002814 unsigned long bio_flags,
2815 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002816{
2817 int ret = 0;
2818 struct bio *bio;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002819 size_t page_size = min_t(size_t, size, PAGE_SIZE);
David Sterba6273b7f2017-10-04 17:30:11 +02002820 sector_t sector = offset >> 9;
Chris Masond1310b22008-01-24 16:13:08 -05002821
David Sterba5c2b1fd2017-06-06 19:22:55 +02002822 ASSERT(bio_ret);
2823
2824 if (*bio_ret) {
David Sterba0c8508a2017-06-12 20:00:43 +02002825 bool contig;
2826 bool can_merge = true;
2827
Chris Masond1310b22008-01-24 16:13:08 -05002828 bio = *bio_ret;
David Sterba0c8508a2017-06-12 20:00:43 +02002829 if (prev_bio_flags & EXTENT_BIO_COMPRESSED)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002830 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002831 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002832 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002833
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002834 ASSERT(tree->ops);
2835 if (btrfs_bio_fits_in_stripe(page, page_size, bio, bio_flags))
David Sterba0c8508a2017-06-12 20:00:43 +02002836 can_merge = false;
2837
2838 if (prev_bio_flags != bio_flags || !contig || !can_merge ||
Filipe Manana005efed2015-09-14 09:09:31 +01002839 force_bio_submit ||
David Sterba6c5a4e22017-10-04 17:10:34 +02002840 bio_add_page(bio, page, page_size, pg_offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002841 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002842 if (ret < 0) {
2843 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002844 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002845 }
Chris Masond1310b22008-01-24 16:13:08 -05002846 bio = NULL;
2847 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002848 if (wbc)
2849 wbc_account_io(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002850 return 0;
2851 }
2852 }
Chris Masonc8b97812008-10-29 14:49:59 -04002853
David Sterba6273b7f2017-10-04 17:30:11 +02002854 bio = btrfs_bio_alloc(bdev, offset);
David Sterba6c5a4e22017-10-04 17:10:34 +02002855 bio_add_page(bio, page, page_size, pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002856 bio->bi_end_io = end_io_func;
2857 bio->bi_private = tree;
Jens Axboee6959b92017-06-27 11:51:28 -06002858 bio->bi_write_hint = page->mapping->host->i_write_hint;
David Sterba4b81ba42017-06-06 19:14:26 +02002859 bio->bi_opf = opf;
Chris Masonda2f0f72015-07-02 13:57:22 -07002860 if (wbc) {
2861 wbc_init_bio(wbc, bio);
2862 wbc_account_io(wbc, page, page_size);
2863 }
Chris Mason70dec802008-01-29 09:59:12 -05002864
David Sterba5c2b1fd2017-06-06 19:22:55 +02002865 *bio_ret = bio;
Chris Masond1310b22008-01-24 16:13:08 -05002866
2867 return ret;
2868}
2869
Eric Sandeen48a3b632013-04-25 20:41:01 +00002870static void attach_extent_buffer_page(struct extent_buffer *eb,
2871 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002872{
2873 if (!PagePrivate(page)) {
2874 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002875 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002876 set_page_private(page, (unsigned long)eb);
2877 } else {
2878 WARN_ON(page->private != (unsigned long)eb);
2879 }
2880}
2881
Chris Masond1310b22008-01-24 16:13:08 -05002882void set_page_extent_mapped(struct page *page)
2883{
2884 if (!PagePrivate(page)) {
2885 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002886 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002887 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002888 }
2889}
2890
Miao Xie125bac012013-07-25 19:22:37 +08002891static struct extent_map *
2892__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2893 u64 start, u64 len, get_extent_t *get_extent,
2894 struct extent_map **em_cached)
2895{
2896 struct extent_map *em;
2897
2898 if (em_cached && *em_cached) {
2899 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002900 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002901 start < extent_map_end(em)) {
Elena Reshetova490b54d2017-03-03 10:55:12 +02002902 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002903 return em;
2904 }
2905
2906 free_extent_map(em);
2907 *em_cached = NULL;
2908 }
2909
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02002910 em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
Miao Xie125bac012013-07-25 19:22:37 +08002911 if (em_cached && !IS_ERR_OR_NULL(em)) {
2912 BUG_ON(*em_cached);
Elena Reshetova490b54d2017-03-03 10:55:12 +02002913 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002914 *em_cached = em;
2915 }
2916 return em;
2917}
Chris Masond1310b22008-01-24 16:13:08 -05002918/*
2919 * basic readpage implementation. Locked extent state structs are inserted
2920 * into the tree that are removed when the IO is done (by the end_io
2921 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002922 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07002923 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05002924 */
Miao Xie99740902013-07-25 19:22:36 +08002925static int __do_readpage(struct extent_io_tree *tree,
2926 struct page *page,
2927 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002928 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002929 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02002930 unsigned long *bio_flags, unsigned int read_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002931 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05002932{
2933 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002934 u64 start = page_offset(page);
David Sterba8eec8292017-06-06 19:50:13 +02002935 const u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002936 u64 cur = start;
2937 u64 extent_offset;
2938 u64 last_byte = i_size_read(inode);
2939 u64 block_start;
2940 u64 cur_end;
Chris Masond1310b22008-01-24 16:13:08 -05002941 struct extent_map *em;
2942 struct block_device *bdev;
Liu Bobaf863b2016-07-11 10:39:07 -07002943 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002944 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002945 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002946 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002947 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002948 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00002949 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002950
2951 set_page_extent_mapped(page);
2952
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002953 if (!PageUptodate(page)) {
2954 if (cleancache_get_page(page) == 0) {
2955 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08002956 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002957 goto out;
2958 }
2959 }
2960
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002961 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04002962 char *userpage;
Johannes Thumshirn70730172018-12-05 15:23:03 +01002963 size_t zero_offset = offset_in_page(last_byte);
Chris Masonc8b97812008-10-29 14:49:59 -04002964
2965 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002966 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002967 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002968 memset(userpage + zero_offset, 0, iosize);
2969 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002970 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002971 }
2972 }
Chris Masond1310b22008-01-24 16:13:08 -05002973 while (cur <= end) {
Filipe Manana005efed2015-09-14 09:09:31 +01002974 bool force_bio_submit = false;
David Sterba6273b7f2017-10-04 17:30:11 +02002975 u64 offset;
Josef Bacikc8f2f242013-02-11 11:33:00 -05002976
Chris Masond1310b22008-01-24 16:13:08 -05002977 if (cur >= last_byte) {
2978 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002979 struct extent_state *cached = NULL;
2980
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002981 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002982 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002983 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002984 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002985 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002986 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002987 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00002988 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01002989 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05002990 break;
2991 }
Miao Xie125bac012013-07-25 19:22:37 +08002992 em = __get_extent_map(inode, page, pg_offset, cur,
2993 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02002994 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002995 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00002996 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002997 break;
2998 }
Chris Masond1310b22008-01-24 16:13:08 -05002999 extent_offset = cur - em->start;
3000 BUG_ON(extent_map_end(em) <= cur);
3001 BUG_ON(end < cur);
3002
Li Zefan261507a02010-12-17 14:21:50 +08003003 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07003004 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08003005 extent_set_compress_type(&this_bio_flag,
3006 em->compress_type);
3007 }
Chris Masonc8b97812008-10-29 14:49:59 -04003008
Chris Masond1310b22008-01-24 16:13:08 -05003009 iosize = min(extent_map_end(em) - cur, end - cur + 1);
3010 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00003011 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04003012 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
3013 disk_io_size = em->block_len;
David Sterba6273b7f2017-10-04 17:30:11 +02003014 offset = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003015 } else {
David Sterba6273b7f2017-10-04 17:30:11 +02003016 offset = em->block_start + extent_offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003017 disk_io_size = iosize;
3018 }
Chris Masond1310b22008-01-24 16:13:08 -05003019 bdev = em->bdev;
3020 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04003021 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
3022 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01003023
3024 /*
3025 * If we have a file range that points to a compressed extent
3026 * and it's followed by a consecutive file range that points to
3027 * to the same compressed extent (possibly with a different
3028 * offset and/or length, so it either points to the whole extent
3029 * or only part of it), we must make sure we do not submit a
3030 * single bio to populate the pages for the 2 ranges because
3031 * this makes the compressed extent read zero out the pages
3032 * belonging to the 2nd range. Imagine the following scenario:
3033 *
3034 * File layout
3035 * [0 - 8K] [8K - 24K]
3036 * | |
3037 * | |
3038 * points to extent X, points to extent X,
3039 * offset 4K, length of 8K offset 0, length 16K
3040 *
3041 * [extent X, compressed length = 4K uncompressed length = 16K]
3042 *
3043 * If the bio to read the compressed extent covers both ranges,
3044 * it will decompress extent X into the pages belonging to the
3045 * first range and then it will stop, zeroing out the remaining
3046 * pages that belong to the other range that points to extent X.
3047 * So here we make sure we submit 2 bios, one for the first
3048 * range and another one for the third range. Both will target
3049 * the same physical extent from disk, but we can't currently
3050 * make the compressed bio endio callback populate the pages
3051 * for both ranges because each compressed bio is tightly
3052 * coupled with a single extent map, and each range can have
3053 * an extent map with a different offset value relative to the
3054 * uncompressed data of our extent and different lengths. This
3055 * is a corner case so we prioritize correctness over
3056 * non-optimal behavior (submitting 2 bios for the same extent).
3057 */
3058 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3059 prev_em_start && *prev_em_start != (u64)-1 &&
Filipe Manana8e928212019-02-14 15:17:20 +00003060 *prev_em_start != em->start)
Filipe Manana005efed2015-09-14 09:09:31 +01003061 force_bio_submit = true;
3062
3063 if (prev_em_start)
Filipe Manana8e928212019-02-14 15:17:20 +00003064 *prev_em_start = em->start;
Filipe Manana005efed2015-09-14 09:09:31 +01003065
Chris Masond1310b22008-01-24 16:13:08 -05003066 free_extent_map(em);
3067 em = NULL;
3068
3069 /* we've found a hole, just zero and go on */
3070 if (block_start == EXTENT_MAP_HOLE) {
3071 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003072 struct extent_state *cached = NULL;
3073
Cong Wang7ac687d2011-11-25 23:14:28 +08003074 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003075 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003076 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003077 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003078
3079 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003080 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003081 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01003082 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003083 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003084 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003085 continue;
3086 }
3087 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003088 if (test_range_bit(tree, cur, cur_end,
3089 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003090 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003091 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003092 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003093 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003094 continue;
3095 }
Chris Mason70dec802008-01-29 09:59:12 -05003096 /* we have an inline extent but it didn't get marked up
3097 * to date. Error out
3098 */
3099 if (block_start == EXTENT_MAP_INLINE) {
3100 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003101 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003102 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003103 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003104 continue;
3105 }
Chris Masond1310b22008-01-24 16:13:08 -05003106
David Sterba4b81ba42017-06-06 19:14:26 +02003107 ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL,
David Sterba6273b7f2017-10-04 17:30:11 +02003108 page, offset, disk_io_size,
3109 pg_offset, bdev, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003110 end_bio_extent_readpage, mirror_num,
3111 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003112 this_bio_flag,
3113 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003114 if (!ret) {
3115 nr++;
3116 *bio_flags = this_bio_flag;
3117 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003118 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003119 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003120 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003121 }
Chris Masond1310b22008-01-24 16:13:08 -05003122 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003123 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003124 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003125out:
Chris Masond1310b22008-01-24 16:13:08 -05003126 if (!nr) {
3127 if (!PageError(page))
3128 SetPageUptodate(page);
3129 unlock_page(page);
3130 }
Liu Bobaf863b2016-07-11 10:39:07 -07003131 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003132}
3133
Nikolay Borisove65ef212019-03-11 09:55:38 +02003134static inline void contiguous_readpages(struct extent_io_tree *tree,
Miao Xie99740902013-07-25 19:22:36 +08003135 struct page *pages[], int nr_pages,
3136 u64 start, u64 end,
Miao Xie125bac012013-07-25 19:22:37 +08003137 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003138 struct bio **bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003139 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003140 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003141{
3142 struct inode *inode;
3143 struct btrfs_ordered_extent *ordered;
3144 int index;
3145
3146 inode = pages[0]->mapping->host;
3147 while (1) {
3148 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003149 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Miao Xie99740902013-07-25 19:22:36 +08003150 end - start + 1);
3151 if (!ordered)
3152 break;
3153 unlock_extent(tree, start, end);
3154 btrfs_start_ordered_extent(inode, ordered, 1);
3155 btrfs_put_ordered_extent(ordered);
3156 }
3157
3158 for (index = 0; index < nr_pages; index++) {
David Sterba4ef77692017-06-23 04:09:57 +02003159 __do_readpage(tree, pages[index], btrfs_get_extent, em_cached,
Jens Axboe5e9d3982018-08-17 15:45:39 -07003160 bio, 0, bio_flags, REQ_RAHEAD, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003161 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003162 }
3163}
3164
Miao Xie99740902013-07-25 19:22:36 +08003165static int __extent_read_full_page(struct extent_io_tree *tree,
3166 struct page *page,
3167 get_extent_t *get_extent,
3168 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003169 unsigned long *bio_flags,
3170 unsigned int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003171{
3172 struct inode *inode = page->mapping->host;
3173 struct btrfs_ordered_extent *ordered;
3174 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003175 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003176 int ret;
3177
3178 while (1) {
3179 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003180 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003181 PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003182 if (!ordered)
3183 break;
3184 unlock_extent(tree, start, end);
3185 btrfs_start_ordered_extent(inode, ordered, 1);
3186 btrfs_put_ordered_extent(ordered);
3187 }
3188
Miao Xie125bac012013-07-25 19:22:37 +08003189 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003190 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003191 return ret;
3192}
3193
Chris Masond1310b22008-01-24 16:13:08 -05003194int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003195 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003196{
3197 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003198 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003199 int ret;
3200
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003201 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003202 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003203 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003204 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003205 return ret;
3206}
Chris Masond1310b22008-01-24 16:13:08 -05003207
David Sterba3d4b9492017-02-10 19:33:41 +01003208static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003209 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003210{
3211 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003212}
3213
Chris Masond1310b22008-01-24 16:13:08 -05003214/*
Chris Mason40f76582014-05-21 13:35:51 -07003215 * helper for __extent_writepage, doing all of the delayed allocation setup.
3216 *
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003217 * This returns 1 if btrfs_run_delalloc_range function did all the work required
Chris Mason40f76582014-05-21 13:35:51 -07003218 * to write the page (copy into inline extent). In this case the IO has
3219 * been started and the page is already unlocked.
3220 *
3221 * This returns 0 if all went well (page still locked)
3222 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003223 */
Chris Mason40f76582014-05-21 13:35:51 -07003224static noinline_for_stack int writepage_delalloc(struct inode *inode,
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003225 struct page *page, struct writeback_control *wbc,
3226 u64 delalloc_start, unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003227{
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003228 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003229 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Lu Fengqi3522e902018-11-29 11:33:38 +08003230 bool found;
Chris Mason40f76582014-05-21 13:35:51 -07003231 u64 delalloc_to_write = 0;
3232 u64 delalloc_end = 0;
3233 int ret;
3234 int page_started = 0;
3235
Chris Mason40f76582014-05-21 13:35:51 -07003236
3237 while (delalloc_end < page_end) {
Lu Fengqi3522e902018-11-29 11:33:38 +08003238 found = find_lock_delalloc_range(inode, tree,
Chris Mason40f76582014-05-21 13:35:51 -07003239 page,
3240 &delalloc_start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03003241 &delalloc_end);
Lu Fengqi3522e902018-11-29 11:33:38 +08003242 if (!found) {
Chris Mason40f76582014-05-21 13:35:51 -07003243 delalloc_start = delalloc_end + 1;
3244 continue;
3245 }
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003246 ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
3247 delalloc_end, &page_started, nr_written, wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003248 /* File system has been set read-only */
3249 if (ret) {
3250 SetPageError(page);
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003251 /*
3252 * btrfs_run_delalloc_range should return < 0 for error
3253 * but just in case, we use > 0 here meaning the IO is
3254 * started, so we don't want to return > 0 unless
3255 * things are going well.
Chris Mason40f76582014-05-21 13:35:51 -07003256 */
3257 ret = ret < 0 ? ret : -EIO;
3258 goto done;
3259 }
3260 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003261 * delalloc_end is already one less than the total length, so
3262 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003263 */
3264 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003265 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003266 delalloc_start = delalloc_end + 1;
3267 }
3268 if (wbc->nr_to_write < delalloc_to_write) {
3269 int thresh = 8192;
3270
3271 if (delalloc_to_write < thresh * 2)
3272 thresh = delalloc_to_write;
3273 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3274 thresh);
3275 }
3276
3277 /* did the fill delalloc function already unlock and start
3278 * the IO?
3279 */
3280 if (page_started) {
3281 /*
3282 * we've unlocked the page, so we can't update
3283 * the mapping's writeback index, just update
3284 * nr_to_write.
3285 */
3286 wbc->nr_to_write -= *nr_written;
3287 return 1;
3288 }
3289
3290 ret = 0;
3291
3292done:
3293 return ret;
3294}
3295
3296/*
3297 * helper for __extent_writepage. This calls the writepage start hooks,
3298 * and does the loop to map the page into extents and bios.
3299 *
3300 * We return 1 if the IO is started and the page is unlocked,
3301 * 0 if all went well (page still locked)
3302 * < 0 if there were errors (page still locked)
3303 */
3304static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3305 struct page *page,
3306 struct writeback_control *wbc,
3307 struct extent_page_data *epd,
3308 loff_t i_size,
3309 unsigned long nr_written,
David Sterbaf1c77c52017-06-06 19:03:49 +02003310 unsigned int write_flags, int *nr_ret)
Chris Mason40f76582014-05-21 13:35:51 -07003311{
Chris Masond1310b22008-01-24 16:13:08 -05003312 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003313 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003314 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003315 u64 end;
3316 u64 cur = start;
3317 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003318 u64 block_start;
3319 u64 iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003320 struct extent_map *em;
3321 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003322 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003323 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003324 int ret = 0;
3325 int nr = 0;
3326 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003327
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003328 ret = btrfs_writepage_cow_fixup(page, start, page_end);
3329 if (ret) {
3330 /* Fixup worker will requeue */
3331 if (ret == -EBUSY)
3332 wbc->pages_skipped++;
3333 else
3334 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003335
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003336 update_nr_written(wbc, nr_written);
3337 unlock_page(page);
3338 return 1;
Chris Mason247e7432008-07-17 12:53:51 -04003339 }
3340
Chris Mason11c83492009-04-20 15:50:09 -04003341 /*
3342 * we don't want to touch the inode after unlocking the page,
3343 * so we update the mapping writeback index now
3344 */
David Sterba3d4b9492017-02-10 19:33:41 +01003345 update_nr_written(wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003346
Chris Masond1310b22008-01-24 16:13:08 -05003347 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003348 if (i_size <= start) {
Nikolay Borisovc6297322018-11-08 10:18:08 +02003349 btrfs_writepage_endio_finish_ordered(page, start, page_end, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003350 goto done;
3351 }
3352
Chris Masond1310b22008-01-24 16:13:08 -05003353 blocksize = inode->i_sb->s_blocksize;
3354
3355 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003356 u64 em_end;
David Sterba6273b7f2017-10-04 17:30:11 +02003357 u64 offset;
David Sterba58409ed2016-05-04 11:46:10 +02003358
Chris Mason40f76582014-05-21 13:35:51 -07003359 if (cur >= i_size) {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003360 btrfs_writepage_endio_finish_ordered(page, cur,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003361 page_end, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003362 break;
3363 }
David Sterba3c98c622017-06-23 04:01:08 +02003364 em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003365 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003366 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003367 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003368 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003369 break;
3370 }
3371
3372 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003373 em_end = extent_map_end(em);
3374 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003375 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003376 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003377 iosize = ALIGN(iosize, blocksize);
David Sterba6273b7f2017-10-04 17:30:11 +02003378 offset = em->block_start + extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003379 bdev = em->bdev;
3380 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003381 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003382 free_extent_map(em);
3383 em = NULL;
3384
Chris Masonc8b97812008-10-29 14:49:59 -04003385 /*
3386 * compressed and inline extents are written through other
3387 * paths in the FS
3388 */
3389 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003390 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003391 /*
3392 * end_io notification does not happen here for
3393 * compressed extents
3394 */
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003395 if (!compressed)
3396 btrfs_writepage_endio_finish_ordered(page, cur,
3397 cur + iosize - 1,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003398 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003399 else if (compressed) {
3400 /* we don't want to end_page_writeback on
3401 * a compressed extent. this happens
3402 * elsewhere
3403 */
3404 nr++;
3405 }
3406
3407 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003408 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003409 continue;
3410 }
Chris Masonc8b97812008-10-29 14:49:59 -04003411
David Sterba5cdc84b2018-07-18 20:32:52 +02003412 btrfs_set_range_writeback(tree, cur, cur + iosize - 1);
David Sterba58409ed2016-05-04 11:46:10 +02003413 if (!PageWriteback(page)) {
3414 btrfs_err(BTRFS_I(inode)->root->fs_info,
3415 "page %lu not writeback, cur %llu end %llu",
3416 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003417 }
David Sterba58409ed2016-05-04 11:46:10 +02003418
David Sterba4b81ba42017-06-06 19:14:26 +02003419 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003420 page, offset, iosize, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003421 bdev, &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003422 end_bio_extent_writepage,
3423 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003424 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003425 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003426 if (PageWriteback(page))
3427 end_page_writeback(page);
3428 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003429
Chris Masond1310b22008-01-24 16:13:08 -05003430 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003431 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003432 nr++;
3433 }
3434done:
Chris Mason40f76582014-05-21 13:35:51 -07003435 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003436 return ret;
3437}
3438
3439/*
3440 * the writepage semantics are similar to regular writepage. extent
3441 * records are inserted to lock ranges in the tree, and as dirty areas
3442 * are found, they are marked writeback. Then the lock bits are removed
3443 * and the end_io handler clears the writeback ranges
Qu Wenruo30659762019-03-20 14:27:42 +08003444 *
3445 * Return 0 if everything goes well.
3446 * Return <0 for error.
Chris Mason40f76582014-05-21 13:35:51 -07003447 */
3448static int __extent_writepage(struct page *page, struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003449 struct extent_page_data *epd)
Chris Mason40f76582014-05-21 13:35:51 -07003450{
3451 struct inode *inode = page->mapping->host;
Chris Mason40f76582014-05-21 13:35:51 -07003452 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003453 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003454 int ret;
3455 int nr = 0;
3456 size_t pg_offset = 0;
3457 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003458 unsigned long end_index = i_size >> PAGE_SHIFT;
David Sterbaf1c77c52017-06-06 19:03:49 +02003459 unsigned int write_flags = 0;
Chris Mason40f76582014-05-21 13:35:51 -07003460 unsigned long nr_written = 0;
3461
Liu Boff40adf2017-08-24 18:19:48 -06003462 write_flags = wbc_to_write_flags(wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003463
3464 trace___extent_writepage(page, inode, wbc);
3465
3466 WARN_ON(!PageLocked(page));
3467
3468 ClearPageError(page);
3469
Johannes Thumshirn70730172018-12-05 15:23:03 +01003470 pg_offset = offset_in_page(i_size);
Chris Mason40f76582014-05-21 13:35:51 -07003471 if (page->index > end_index ||
3472 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003473 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003474 unlock_page(page);
3475 return 0;
3476 }
3477
3478 if (page->index == end_index) {
3479 char *userpage;
3480
3481 userpage = kmap_atomic(page);
3482 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003483 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003484 kunmap_atomic(userpage);
3485 flush_dcache_page(page);
3486 }
3487
3488 pg_offset = 0;
3489
3490 set_page_extent_mapped(page);
3491
Nikolay Borisov7789a552018-11-08 10:18:06 +02003492 if (!epd->extent_locked) {
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003493 ret = writepage_delalloc(inode, page, wbc, start, &nr_written);
Nikolay Borisov7789a552018-11-08 10:18:06 +02003494 if (ret == 1)
3495 goto done_unlocked;
3496 if (ret)
3497 goto done;
3498 }
Chris Mason40f76582014-05-21 13:35:51 -07003499
3500 ret = __extent_writepage_io(inode, page, wbc, epd,
3501 i_size, nr_written, write_flags, &nr);
3502 if (ret == 1)
3503 goto done_unlocked;
3504
3505done:
Chris Masond1310b22008-01-24 16:13:08 -05003506 if (nr == 0) {
3507 /* make sure the mapping tag for page dirty gets cleared */
3508 set_page_writeback(page);
3509 end_page_writeback(page);
3510 }
Filipe Manana61391d52014-05-09 17:17:40 +01003511 if (PageError(page)) {
3512 ret = ret < 0 ? ret : -EIO;
3513 end_extent_writepage(page, ret, start, page_end);
3514 }
Chris Masond1310b22008-01-24 16:13:08 -05003515 unlock_page(page);
Qu Wenruo30659762019-03-20 14:27:42 +08003516 ASSERT(ret <= 0);
Chris Mason40f76582014-05-21 13:35:51 -07003517 return ret;
Chris Mason771ed682008-11-06 22:02:51 -05003518
Chris Mason11c83492009-04-20 15:50:09 -04003519done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003520 return 0;
3521}
3522
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003523void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003524{
NeilBrown74316202014-07-07 15:16:04 +10003525 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3526 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003527}
3528
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003529/*
3530 * Lock eb pages and flush the bio if we can't the locks
3531 *
3532 * Return 0 if nothing went wrong
3533 * Return >0 is same as 0, except bio is not submitted
3534 * Return <0 if something went wrong, no page is locked
3535 */
David Sterba9df76fb2019-03-20 11:21:41 +01003536static noinline_for_stack int lock_extent_buffer_for_io(struct extent_buffer *eb,
Chris Mason0e378df2014-05-19 20:55:27 -07003537 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003538{
David Sterba9df76fb2019-03-20 11:21:41 +01003539 struct btrfs_fs_info *fs_info = eb->fs_info;
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003540 int i, num_pages, failed_page_nr;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003541 int flush = 0;
3542 int ret = 0;
3543
3544 if (!btrfs_try_tree_write_lock(eb)) {
Qu Wenruof4340622019-03-20 14:27:41 +08003545 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003546 if (ret < 0)
3547 return ret;
3548 flush = 1;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003549 btrfs_tree_lock(eb);
3550 }
3551
3552 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3553 btrfs_tree_unlock(eb);
3554 if (!epd->sync_io)
3555 return 0;
3556 if (!flush) {
Qu Wenruof4340622019-03-20 14:27:41 +08003557 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003558 if (ret < 0)
3559 return ret;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003560 flush = 1;
3561 }
Chris Masona098d8e82012-03-21 12:09:56 -04003562 while (1) {
3563 wait_on_extent_buffer_writeback(eb);
3564 btrfs_tree_lock(eb);
3565 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3566 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003567 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003568 }
3569 }
3570
Josef Bacik51561ff2012-07-20 16:25:24 -04003571 /*
3572 * We need to do this to prevent races in people who check if the eb is
3573 * under IO since we can end up having no IO bits set for a short period
3574 * of time.
3575 */
3576 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003577 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3578 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003579 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003580 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Nikolay Borisov104b4e52017-06-20 21:01:20 +03003581 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3582 -eb->len,
3583 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003584 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003585 } else {
3586 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003587 }
3588
3589 btrfs_tree_unlock(eb);
3590
3591 if (!ret)
3592 return ret;
3593
David Sterba65ad0102018-06-29 10:56:49 +02003594 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003595 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003596 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003597
3598 if (!trylock_page(p)) {
3599 if (!flush) {
Qu Wenruof4340622019-03-20 14:27:41 +08003600 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003601 if (ret < 0) {
3602 failed_page_nr = i;
3603 goto err_unlock;
3604 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003605 flush = 1;
3606 }
3607 lock_page(p);
3608 }
3609 }
3610
3611 return ret;
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003612err_unlock:
3613 /* Unlock already locked pages */
3614 for (i = 0; i < failed_page_nr; i++)
3615 unlock_page(eb->pages[i]);
3616 return ret;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003617}
3618
3619static void end_extent_buffer_writeback(struct extent_buffer *eb)
3620{
3621 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003622 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003623 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3624}
3625
Filipe Manana656f30d2014-09-26 12:25:56 +01003626static void set_btree_ioerr(struct page *page)
3627{
3628 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Filipe Manana656f30d2014-09-26 12:25:56 +01003629
3630 SetPageError(page);
3631 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3632 return;
3633
3634 /*
3635 * If writeback for a btree extent that doesn't belong to a log tree
3636 * failed, increment the counter transaction->eb_write_errors.
3637 * We do this because while the transaction is running and before it's
3638 * committing (when we call filemap_fdata[write|wait]_range against
3639 * the btree inode), we might have
3640 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3641 * returns an error or an error happens during writeback, when we're
3642 * committing the transaction we wouldn't know about it, since the pages
3643 * can be no longer dirty nor marked anymore for writeback (if a
3644 * subsequent modification to the extent buffer didn't happen before the
3645 * transaction commit), which makes filemap_fdata[write|wait]_range not
3646 * able to find the pages tagged with SetPageError at transaction
3647 * commit time. So if this happens we must abort the transaction,
3648 * otherwise we commit a super block with btree roots that point to
3649 * btree nodes/leafs whose content on disk is invalid - either garbage
3650 * or the content of some node/leaf from a past generation that got
3651 * cowed or deleted and is no longer valid.
3652 *
3653 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3654 * not be enough - we need to distinguish between log tree extents vs
3655 * non-log tree extents, and the next filemap_fdatawait_range() call
3656 * will catch and clear such errors in the mapping - and that call might
3657 * be from a log sync and not from a transaction commit. Also, checking
3658 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3659 * not done and would not be reliable - the eb might have been released
3660 * from memory and reading it back again means that flag would not be
3661 * set (since it's a runtime flag, not persisted on disk).
3662 *
3663 * Using the flags below in the btree inode also makes us achieve the
3664 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3665 * writeback for all dirty pages and before filemap_fdatawait_range()
3666 * is called, the writeback for all dirty pages had already finished
3667 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3668 * filemap_fdatawait_range() would return success, as it could not know
3669 * that writeback errors happened (the pages were no longer tagged for
3670 * writeback).
3671 */
3672 switch (eb->log_index) {
3673 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003674 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003675 break;
3676 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003677 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003678 break;
3679 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003680 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003681 break;
3682 default:
3683 BUG(); /* unexpected, logic error */
3684 }
3685}
3686
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003687static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003688{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003689 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003690 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003691 int i, done;
Ming Lei6dc4f102019-02-15 19:13:19 +08003692 struct bvec_iter_all iter_all;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003693
David Sterbac09abff2017-07-13 18:10:07 +02003694 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08003695 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003696 struct page *page = bvec->bv_page;
3697
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003698 eb = (struct extent_buffer *)page->private;
3699 BUG_ON(!eb);
3700 done = atomic_dec_and_test(&eb->io_pages);
3701
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003702 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003703 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003704 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003705 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003706 }
3707
3708 end_page_writeback(page);
3709
3710 if (!done)
3711 continue;
3712
3713 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003714 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003715
3716 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003717}
3718
Chris Mason0e378df2014-05-19 20:55:27 -07003719static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003720 struct writeback_control *wbc,
3721 struct extent_page_data *epd)
3722{
David Sterba0ab02062019-03-20 11:27:57 +01003723 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003724 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003725 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003726 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003727 u32 nritems;
David Sterbacc5e31a2018-03-01 18:20:27 +01003728 int i, num_pages;
Liu Bo851cd172016-09-23 13:44:44 -07003729 unsigned long start, end;
Liu Boff40adf2017-08-24 18:19:48 -06003730 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003731 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003732
Filipe Manana656f30d2014-09-26 12:25:56 +01003733 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02003734 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003735 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003736
Liu Bo851cd172016-09-23 13:44:44 -07003737 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3738 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003739 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003740 end = btrfs_node_key_ptr_offset(nritems);
3741
David Sterbab159fa22016-11-08 18:09:03 +01003742 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003743 } else {
3744 /*
3745 * leaf:
3746 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3747 */
3748 start = btrfs_item_nr_offset(nritems);
David Sterba8f881e82019-03-20 11:33:10 +01003749 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(eb);
David Sterbab159fa22016-11-08 18:09:03 +01003750 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003751 }
3752
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003753 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003754 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003755
3756 clear_page_dirty_for_io(p);
3757 set_page_writeback(p);
David Sterba4b81ba42017-06-06 19:14:26 +02003758 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003759 p, offset, PAGE_SIZE, 0, bdev,
David Sterbac2df8bb2017-02-10 19:29:38 +01003760 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003761 end_bio_extent_buffer_writepage,
Liu Bo18fdc672017-09-13 12:18:22 -06003762 0, 0, 0, false);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003763 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003764 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003765 if (PageWriteback(p))
3766 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003767 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3768 end_extent_buffer_writeback(eb);
3769 ret = -EIO;
3770 break;
3771 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003772 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003773 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003774 unlock_page(p);
3775 }
3776
3777 if (unlikely(ret)) {
3778 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003779 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003780 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003781 unlock_page(p);
3782 }
3783 }
3784
3785 return ret;
3786}
3787
3788int btree_write_cache_pages(struct address_space *mapping,
3789 struct writeback_control *wbc)
3790{
3791 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003792 struct extent_buffer *eb, *prev_eb = NULL;
3793 struct extent_page_data epd = {
3794 .bio = NULL,
3795 .tree = tree,
3796 .extent_locked = 0,
3797 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3798 };
3799 int ret = 0;
3800 int done = 0;
3801 int nr_to_write_done = 0;
3802 struct pagevec pvec;
3803 int nr_pages;
3804 pgoff_t index;
3805 pgoff_t end; /* Inclusive */
3806 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003807 xa_mark_t tag;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003808
Mel Gorman86679822017-11-15 17:37:52 -08003809 pagevec_init(&pvec);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003810 if (wbc->range_cyclic) {
3811 index = mapping->writeback_index; /* Start from prev offset */
3812 end = -1;
3813 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003814 index = wbc->range_start >> PAGE_SHIFT;
3815 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003816 scanned = 1;
3817 }
3818 if (wbc->sync_mode == WB_SYNC_ALL)
3819 tag = PAGECACHE_TAG_TOWRITE;
3820 else
3821 tag = PAGECACHE_TAG_DIRTY;
3822retry:
3823 if (wbc->sync_mode == WB_SYNC_ALL)
3824 tag_pages_for_writeback(mapping, index, end);
3825 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara4006f432017-11-15 17:34:37 -08003826 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08003827 tag))) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003828 unsigned i;
3829
3830 scanned = 1;
3831 for (i = 0; i < nr_pages; i++) {
3832 struct page *page = pvec.pages[i];
3833
3834 if (!PagePrivate(page))
3835 continue;
3836
Josef Bacikb5bae262012-09-14 13:43:01 -04003837 spin_lock(&mapping->private_lock);
3838 if (!PagePrivate(page)) {
3839 spin_unlock(&mapping->private_lock);
3840 continue;
3841 }
3842
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003843 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003844
3845 /*
3846 * Shouldn't happen and normally this would be a BUG_ON
3847 * but no sense in crashing the users box for something
3848 * we can survive anyway.
3849 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303850 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003851 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003852 continue;
3853 }
3854
Josef Bacikb5bae262012-09-14 13:43:01 -04003855 if (eb == prev_eb) {
3856 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003857 continue;
3858 }
3859
Josef Bacikb5bae262012-09-14 13:43:01 -04003860 ret = atomic_inc_not_zero(&eb->refs);
3861 spin_unlock(&mapping->private_lock);
3862 if (!ret)
3863 continue;
3864
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003865 prev_eb = eb;
David Sterba9df76fb2019-03-20 11:21:41 +01003866 ret = lock_extent_buffer_for_io(eb, &epd);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003867 if (!ret) {
3868 free_extent_buffer(eb);
3869 continue;
3870 }
3871
David Sterba0ab02062019-03-20 11:27:57 +01003872 ret = write_one_eb(eb, wbc, &epd);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003873 if (ret) {
3874 done = 1;
3875 free_extent_buffer(eb);
3876 break;
3877 }
3878 free_extent_buffer(eb);
3879
3880 /*
3881 * the filesystem may choose to bump up nr_to_write.
3882 * We have to make sure to honor the new nr_to_write
3883 * at any time
3884 */
3885 nr_to_write_done = wbc->nr_to_write <= 0;
3886 }
3887 pagevec_release(&pvec);
3888 cond_resched();
3889 }
3890 if (!scanned && !done) {
3891 /*
3892 * We hit the last page and there is more work to be done: wrap
3893 * back to the start of the file
3894 */
3895 scanned = 1;
3896 index = 0;
3897 goto retry;
3898 }
Qu Wenruo2b952ee2019-03-20 14:27:43 +08003899 ASSERT(ret <= 0);
3900 if (ret < 0) {
3901 end_write_bio(&epd, ret);
3902 return ret;
3903 }
3904 ret = flush_write_bio(&epd);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003905 return ret;
3906}
3907
Chris Masond1310b22008-01-24 16:13:08 -05003908/**
Chris Mason4bef0842008-09-08 11:18:08 -04003909 * 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 -05003910 * @mapping: address space structure to write
3911 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
David Sterba935db852017-06-23 04:30:28 +02003912 * @data: data passed to __extent_writepage function
Chris Masond1310b22008-01-24 16:13:08 -05003913 *
3914 * If a page is already under I/O, write_cache_pages() skips it, even
3915 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3916 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3917 * and msync() need to guarantee that all the data which was dirty at the time
3918 * the call was made get new I/O started against them. If wbc->sync_mode is
3919 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3920 * existing IO to complete.
3921 */
David Sterba4242b642017-02-10 19:38:24 +01003922static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04003923 struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003924 struct extent_page_data *epd)
Chris Masond1310b22008-01-24 16:13:08 -05003925{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003926 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003927 int ret = 0;
3928 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003929 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003930 struct pagevec pvec;
3931 int nr_pages;
3932 pgoff_t index;
3933 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08003934 pgoff_t done_index;
3935 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003936 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003937 xa_mark_t tag;
Chris Masond1310b22008-01-24 16:13:08 -05003938
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003939 /*
3940 * We have to hold onto the inode so that ordered extents can do their
3941 * work when the IO finishes. The alternative to this is failing to add
3942 * an ordered extent if the igrab() fails there and that is a huge pain
3943 * to deal with, so instead just hold onto the inode throughout the
3944 * writepages operation. If it fails here we are freeing up the inode
3945 * anyway and we'd rather not waste our time writing out stuff that is
3946 * going to be truncated anyway.
3947 */
3948 if (!igrab(inode))
3949 return 0;
3950
Mel Gorman86679822017-11-15 17:37:52 -08003951 pagevec_init(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05003952 if (wbc->range_cyclic) {
3953 index = mapping->writeback_index; /* Start from prev offset */
3954 end = -1;
3955 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003956 index = wbc->range_start >> PAGE_SHIFT;
3957 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08003958 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3959 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05003960 scanned = 1;
3961 }
Ethan Lien3cd24c62018-11-01 14:49:03 +08003962
3963 /*
3964 * We do the tagged writepage as long as the snapshot flush bit is set
3965 * and we are the first one who do the filemap_flush() on this inode.
3966 *
3967 * The nr_to_write == LONG_MAX is needed to make sure other flushers do
3968 * not race in and drop the bit.
3969 */
3970 if (range_whole && wbc->nr_to_write == LONG_MAX &&
3971 test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
3972 &BTRFS_I(inode)->runtime_flags))
3973 wbc->tagged_writepages = 1;
3974
3975 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00003976 tag = PAGECACHE_TAG_TOWRITE;
3977 else
3978 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003979retry:
Ethan Lien3cd24c62018-11-01 14:49:03 +08003980 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00003981 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08003982 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003983 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara67fd7072017-11-15 17:35:19 -08003984 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
3985 &index, end, tag))) {
Chris Masond1310b22008-01-24 16:13:08 -05003986 unsigned i;
3987
3988 scanned = 1;
3989 for (i = 0; i < nr_pages; i++) {
3990 struct page *page = pvec.pages[i];
3991
Liu Boa91326672016-03-07 16:56:21 -08003992 done_index = page->index;
Chris Masond1310b22008-01-24 16:13:08 -05003993 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07003994 * At this point we hold neither the i_pages lock nor
3995 * the page lock: the page may be truncated or
3996 * invalidated (changing page->mapping to NULL),
3997 * or even swizzled back from swapper_space to
3998 * tmpfs file mapping
Chris Masond1310b22008-01-24 16:13:08 -05003999 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05004000 if (!trylock_page(page)) {
Qu Wenruof4340622019-03-20 14:27:41 +08004001 ret = flush_write_bio(epd);
4002 BUG_ON(ret < 0);
Josef Bacikc8f2f242013-02-11 11:33:00 -05004003 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04004004 }
Chris Masond1310b22008-01-24 16:13:08 -05004005
4006 if (unlikely(page->mapping != mapping)) {
4007 unlock_page(page);
4008 continue;
4009 }
4010
Chris Masond2c3f4f2008-11-19 12:44:22 -05004011 if (wbc->sync_mode != WB_SYNC_NONE) {
Qu Wenruof4340622019-03-20 14:27:41 +08004012 if (PageWriteback(page)) {
4013 ret = flush_write_bio(epd);
4014 BUG_ON(ret < 0);
4015 }
Chris Masond1310b22008-01-24 16:13:08 -05004016 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004017 }
Chris Masond1310b22008-01-24 16:13:08 -05004018
4019 if (PageWriteback(page) ||
4020 !clear_page_dirty_for_io(page)) {
4021 unlock_page(page);
4022 continue;
4023 }
4024
David Sterbaaab6e9e2017-11-30 18:00:02 +01004025 ret = __extent_writepage(page, wbc, epd);
Liu Boa91326672016-03-07 16:56:21 -08004026 if (ret < 0) {
4027 /*
4028 * done_index is set past this page,
4029 * so media errors will not choke
4030 * background writeout for the entire
4031 * file. This has consequences for
4032 * range_cyclic semantics (ie. it may
4033 * not be suitable for data integrity
4034 * writeout).
4035 */
4036 done_index = page->index + 1;
4037 done = 1;
4038 break;
4039 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004040
4041 /*
4042 * the filesystem may choose to bump up nr_to_write.
4043 * We have to make sure to honor the new nr_to_write
4044 * at any time
4045 */
4046 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004047 }
4048 pagevec_release(&pvec);
4049 cond_resched();
4050 }
Liu Bo894b36e2016-03-07 16:56:22 -08004051 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004052 /*
4053 * We hit the last page and there is more work to be done: wrap
4054 * back to the start of the file
4055 */
4056 scanned = 1;
4057 index = 0;
4058 goto retry;
4059 }
Liu Boa91326672016-03-07 16:56:21 -08004060
4061 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4062 mapping->writeback_index = done_index;
4063
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004064 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004065 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004066}
Chris Masond1310b22008-01-24 16:13:08 -05004067
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004068int extent_write_full_page(struct page *page, struct writeback_control *wbc)
Chris Masond1310b22008-01-24 16:13:08 -05004069{
4070 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004071 struct extent_page_data epd = {
4072 .bio = NULL,
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004073 .tree = &BTRFS_I(page->mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004074 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004075 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004076 };
Chris Masond1310b22008-01-24 16:13:08 -05004077
Chris Masond1310b22008-01-24 16:13:08 -05004078 ret = __extent_writepage(page, wbc, &epd);
Qu Wenruo30659762019-03-20 14:27:42 +08004079 ASSERT(ret <= 0);
4080 if (ret < 0) {
4081 end_write_bio(&epd, ret);
4082 return ret;
4083 }
Chris Masond1310b22008-01-24 16:13:08 -05004084
Qu Wenruo30659762019-03-20 14:27:42 +08004085 ret = flush_write_bio(&epd);
4086 ASSERT(ret <= 0);
Chris Masond1310b22008-01-24 16:13:08 -05004087 return ret;
4088}
Chris Masond1310b22008-01-24 16:13:08 -05004089
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004090int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
Chris Mason771ed682008-11-06 22:02:51 -05004091 int mode)
4092{
4093 int ret = 0;
4094 struct address_space *mapping = inode->i_mapping;
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004095 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Mason771ed682008-11-06 22:02:51 -05004096 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004097 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4098 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004099
4100 struct extent_page_data epd = {
4101 .bio = NULL,
4102 .tree = tree,
Chris Mason771ed682008-11-06 22:02:51 -05004103 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004104 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05004105 };
4106 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004107 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004108 .nr_to_write = nr_pages * 2,
4109 .range_start = start,
4110 .range_end = end + 1,
4111 };
4112
Chris Masond3977122009-01-05 21:25:51 -05004113 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004114 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004115 if (clear_page_dirty_for_io(page))
4116 ret = __extent_writepage(page, &wbc_writepages, &epd);
4117 else {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02004118 btrfs_writepage_endio_finish_ordered(page, start,
Nikolay Borisovc6297322018-11-08 10:18:08 +02004119 start + PAGE_SIZE - 1, 1);
Chris Mason771ed682008-11-06 22:02:51 -05004120 unlock_page(page);
4121 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004122 put_page(page);
4123 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004124 }
4125
Qu Wenruo02c6db42019-03-20 14:27:45 +08004126 ASSERT(ret <= 0);
4127 if (ret < 0) {
4128 end_write_bio(&epd, ret);
4129 return ret;
4130 }
4131 ret = flush_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004132 return ret;
4133}
Chris Masond1310b22008-01-24 16:13:08 -05004134
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004135int extent_writepages(struct address_space *mapping,
Chris Masond1310b22008-01-24 16:13:08 -05004136 struct writeback_control *wbc)
4137{
4138 int ret = 0;
4139 struct extent_page_data epd = {
4140 .bio = NULL,
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004141 .tree = &BTRFS_I(mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004142 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004143 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004144 };
4145
David Sterba935db852017-06-23 04:30:28 +02004146 ret = extent_write_cache_pages(mapping, wbc, &epd);
Qu Wenruoa2a72fb2019-03-20 14:27:48 +08004147 ASSERT(ret <= 0);
4148 if (ret < 0) {
4149 end_write_bio(&epd, ret);
4150 return ret;
4151 }
4152 ret = flush_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004153 return ret;
4154}
Chris Masond1310b22008-01-24 16:13:08 -05004155
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004156int extent_readpages(struct address_space *mapping, struct list_head *pages,
4157 unsigned nr_pages)
Chris Masond1310b22008-01-24 16:13:08 -05004158{
4159 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004160 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004161 struct page *pagepool[16];
Miao Xie125bac012013-07-25 19:22:37 +08004162 struct extent_map *em_cached = NULL;
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004163 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Liu Bo67c96842012-07-20 21:43:09 -06004164 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004165 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004166
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004167 while (!list_empty(pages)) {
Nikolay Borisove65ef212019-03-11 09:55:38 +02004168 u64 contig_end = 0;
4169
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004170 for (nr = 0; nr < ARRAY_SIZE(pagepool) && !list_empty(pages);) {
Nikolay Borisovf86196e2019-01-03 15:29:02 -08004171 struct page *page = lru_to_page(pages);
Chris Masond1310b22008-01-24 16:13:08 -05004172
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004173 prefetchw(&page->flags);
4174 list_del(&page->lru);
4175 if (add_to_page_cache_lru(page, mapping, page->index,
4176 readahead_gfp_mask(mapping))) {
4177 put_page(page);
Nikolay Borisove65ef212019-03-11 09:55:38 +02004178 break;
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004179 }
4180
4181 pagepool[nr++] = page;
Nikolay Borisove65ef212019-03-11 09:55:38 +02004182 contig_end = page_offset(page) + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004183 }
Liu Bo67c96842012-07-20 21:43:09 -06004184
Nikolay Borisove65ef212019-03-11 09:55:38 +02004185 if (nr) {
4186 u64 contig_start = page_offset(pagepool[0]);
4187
4188 ASSERT(contig_start + nr * PAGE_SIZE - 1 == contig_end);
4189
4190 contiguous_readpages(tree, pagepool, nr, contig_start,
4191 contig_end, &em_cached, &bio, &bio_flags,
4192 &prev_em_start);
4193 }
Chris Masond1310b22008-01-24 16:13:08 -05004194 }
Liu Bo67c96842012-07-20 21:43:09 -06004195
Miao Xie125bac012013-07-25 19:22:37 +08004196 if (em_cached)
4197 free_extent_map(em_cached);
4198
Chris Masond1310b22008-01-24 16:13:08 -05004199 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004200 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004201 return 0;
4202}
Chris Masond1310b22008-01-24 16:13:08 -05004203
4204/*
4205 * basic invalidatepage code, this waits on any locked or writeback
4206 * ranges corresponding to the page, and then deletes any extent state
4207 * records from the tree
4208 */
4209int extent_invalidatepage(struct extent_io_tree *tree,
4210 struct page *page, unsigned long offset)
4211{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004212 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004213 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004214 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004215 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4216
Qu Wenruofda28322013-02-26 08:10:22 +00004217 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004218 if (start > end)
4219 return 0;
4220
David Sterbaff13db42015-12-03 14:30:40 +01004221 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004222 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004223 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004224 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4225 EXTENT_DO_ACCOUNTING,
David Sterbaae0f1622017-10-31 16:37:52 +01004226 1, 1, &cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05004227 return 0;
4228}
Chris Masond1310b22008-01-24 16:13:08 -05004229
4230/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004231 * a helper for releasepage, this tests for areas of the page that
4232 * are locked or under IO and drops the related state bits if it is safe
4233 * to drop the page.
4234 */
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004235static int try_release_extent_state(struct extent_io_tree *tree,
Eric Sandeen48a3b632013-04-25 20:41:01 +00004236 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004237{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004238 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004239 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004240 int ret = 1;
4241
Nikolay Borisov88826792019-03-14 15:28:31 +02004242 if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) {
Chris Mason7b13b7b2008-04-18 10:29:50 -04004243 ret = 0;
Nikolay Borisov88826792019-03-14 15:28:31 +02004244 } else {
Chris Mason11ef1602009-09-23 20:28:46 -04004245 /*
4246 * at this point we can safely clear everything except the
4247 * locked bit and the nodatasum bit
4248 */
David Sterba66b0c882017-10-31 16:30:47 +01004249 ret = __clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004250 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
David Sterba66b0c882017-10-31 16:30:47 +01004251 0, 0, NULL, mask, NULL);
Chris Masone3f24cc2011-02-14 12:52:08 -05004252
4253 /* if clear_extent_bit failed for enomem reasons,
4254 * we can't allow the release to continue.
4255 */
4256 if (ret < 0)
4257 ret = 0;
4258 else
4259 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004260 }
4261 return ret;
4262}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004263
4264/*
Chris Masond1310b22008-01-24 16:13:08 -05004265 * a helper for releasepage. As long as there are no locked extents
4266 * in the range corresponding to the page, both state records and extent
4267 * map records are removed
4268 */
Nikolay Borisov477a30b2018-04-19 10:46:34 +03004269int try_release_extent_mapping(struct page *page, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004270{
4271 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004272 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004273 u64 end = start + PAGE_SIZE - 1;
Filipe Mananabd3599a2018-07-12 01:36:43 +01004274 struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
4275 struct extent_io_tree *tree = &btrfs_inode->io_tree;
4276 struct extent_map_tree *map = &btrfs_inode->extent_tree;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004277
Mel Gormand0164ad2015-11-06 16:28:21 -08004278 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004279 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004280 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004281 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004282 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004283 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004284 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004285 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004286 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004287 break;
4288 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004289 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4290 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004291 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004292 free_extent_map(em);
4293 break;
4294 }
4295 if (!test_range_bit(tree, em->start,
4296 extent_map_end(em) - 1,
Nikolay Borisov4e586ca2019-03-14 15:28:30 +02004297 EXTENT_LOCKED, 0, NULL)) {
Filipe Mananabd3599a2018-07-12 01:36:43 +01004298 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4299 &btrfs_inode->runtime_flags);
Chris Mason70dec802008-01-29 09:59:12 -05004300 remove_extent_mapping(map, em);
4301 /* once for the rb tree */
4302 free_extent_map(em);
4303 }
4304 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004305 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004306
4307 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004308 free_extent_map(em);
4309 }
Chris Masond1310b22008-01-24 16:13:08 -05004310 }
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004311 return try_release_extent_state(tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004312}
Chris Masond1310b22008-01-24 16:13:08 -05004313
Chris Masonec29ed52011-02-23 16:23:20 -05004314/*
4315 * helper function for fiemap, which doesn't want to see any holes.
4316 * This maps until we find something past 'last'
4317 */
4318static struct extent_map *get_extent_skip_holes(struct inode *inode,
David Sterbae3350e12017-06-23 04:09:57 +02004319 u64 offset, u64 last)
Chris Masonec29ed52011-02-23 16:23:20 -05004320{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004321 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004322 struct extent_map *em;
4323 u64 len;
4324
4325 if (offset >= last)
4326 return NULL;
4327
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304328 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004329 len = last - offset;
4330 if (len == 0)
4331 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004332 len = ALIGN(len, sectorsize);
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02004333 em = btrfs_get_extent_fiemap(BTRFS_I(inode), offset, len);
David Sterbac7040052011-04-19 18:00:01 +02004334 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004335 return em;
4336
4337 /* if this isn't a hole return it */
Nikolay Borisov4a2d25c2017-11-23 10:51:43 +02004338 if (em->block_start != EXTENT_MAP_HOLE)
Chris Masonec29ed52011-02-23 16:23:20 -05004339 return em;
Chris Masonec29ed52011-02-23 16:23:20 -05004340
4341 /* this is a hole, advance to the next extent */
4342 offset = extent_map_end(em);
4343 free_extent_map(em);
4344 if (offset >= last)
4345 break;
4346 }
4347 return NULL;
4348}
4349
Qu Wenruo47518322017-04-07 10:43:15 +08004350/*
4351 * To cache previous fiemap extent
4352 *
4353 * Will be used for merging fiemap extent
4354 */
4355struct fiemap_cache {
4356 u64 offset;
4357 u64 phys;
4358 u64 len;
4359 u32 flags;
4360 bool cached;
4361};
4362
4363/*
4364 * Helper to submit fiemap extent.
4365 *
4366 * Will try to merge current fiemap extent specified by @offset, @phys,
4367 * @len and @flags with cached one.
4368 * And only when we fails to merge, cached one will be submitted as
4369 * fiemap extent.
4370 *
4371 * Return value is the same as fiemap_fill_next_extent().
4372 */
4373static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4374 struct fiemap_cache *cache,
4375 u64 offset, u64 phys, u64 len, u32 flags)
4376{
4377 int ret = 0;
4378
4379 if (!cache->cached)
4380 goto assign;
4381
4382 /*
4383 * Sanity check, extent_fiemap() should have ensured that new
Andrea Gelmini52042d82018-11-28 12:05:13 +01004384 * fiemap extent won't overlap with cached one.
Qu Wenruo47518322017-04-07 10:43:15 +08004385 * Not recoverable.
4386 *
4387 * NOTE: Physical address can overlap, due to compression
4388 */
4389 if (cache->offset + cache->len > offset) {
4390 WARN_ON(1);
4391 return -EINVAL;
4392 }
4393
4394 /*
4395 * Only merges fiemap extents if
4396 * 1) Their logical addresses are continuous
4397 *
4398 * 2) Their physical addresses are continuous
4399 * So truly compressed (physical size smaller than logical size)
4400 * extents won't get merged with each other
4401 *
4402 * 3) Share same flags except FIEMAP_EXTENT_LAST
4403 * So regular extent won't get merged with prealloc extent
4404 */
4405 if (cache->offset + cache->len == offset &&
4406 cache->phys + cache->len == phys &&
4407 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4408 (flags & ~FIEMAP_EXTENT_LAST)) {
4409 cache->len += len;
4410 cache->flags |= flags;
4411 goto try_submit_last;
4412 }
4413
4414 /* Not mergeable, need to submit cached one */
4415 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4416 cache->len, cache->flags);
4417 cache->cached = false;
4418 if (ret)
4419 return ret;
4420assign:
4421 cache->cached = true;
4422 cache->offset = offset;
4423 cache->phys = phys;
4424 cache->len = len;
4425 cache->flags = flags;
4426try_submit_last:
4427 if (cache->flags & FIEMAP_EXTENT_LAST) {
4428 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4429 cache->phys, cache->len, cache->flags);
4430 cache->cached = false;
4431 }
4432 return ret;
4433}
4434
4435/*
Qu Wenruo848c23b2017-06-22 10:01:21 +08004436 * Emit last fiemap cache
Qu Wenruo47518322017-04-07 10:43:15 +08004437 *
Qu Wenruo848c23b2017-06-22 10:01:21 +08004438 * The last fiemap cache may still be cached in the following case:
4439 * 0 4k 8k
4440 * |<- Fiemap range ->|
4441 * |<------------ First extent ----------->|
4442 *
4443 * In this case, the first extent range will be cached but not emitted.
4444 * So we must emit it before ending extent_fiemap().
Qu Wenruo47518322017-04-07 10:43:15 +08004445 */
Qu Wenruo848c23b2017-06-22 10:01:21 +08004446static int emit_last_fiemap_cache(struct btrfs_fs_info *fs_info,
4447 struct fiemap_extent_info *fieinfo,
4448 struct fiemap_cache *cache)
Qu Wenruo47518322017-04-07 10:43:15 +08004449{
4450 int ret;
4451
4452 if (!cache->cached)
4453 return 0;
4454
Qu Wenruo47518322017-04-07 10:43:15 +08004455 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4456 cache->len, cache->flags);
4457 cache->cached = false;
4458 if (ret > 0)
4459 ret = 0;
4460 return ret;
4461}
4462
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004463int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterba2135fb92017-06-23 04:09:57 +02004464 __u64 start, __u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004465{
Josef Bacik975f84f2010-11-23 19:36:57 +00004466 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004467 u64 off = start;
4468 u64 max = start + len;
4469 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004470 u32 found_type;
4471 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004472 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004473 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004474 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004475 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004476 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004477 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004478 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004479 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004480 struct fiemap_cache cache = { 0 };
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004481 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004482 u64 em_start = 0;
4483 u64 em_len = 0;
4484 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004485
4486 if (len == 0)
4487 return -EINVAL;
4488
Josef Bacik975f84f2010-11-23 19:36:57 +00004489 path = btrfs_alloc_path();
4490 if (!path)
4491 return -ENOMEM;
4492 path->leave_spinning = 1;
4493
Jeff Mahoneyda170662016-06-15 09:22:56 -04004494 start = round_down(start, btrfs_inode_sectorsize(inode));
4495 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004496
Chris Masonec29ed52011-02-23 16:23:20 -05004497 /*
4498 * lookup the last file extent. We're not using i_size here
4499 * because there might be preallocation past i_size
4500 */
David Sterbaf85b7372017-01-20 14:54:07 +01004501 ret = btrfs_lookup_file_extent(NULL, root, path,
4502 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004503 if (ret < 0) {
4504 btrfs_free_path(path);
4505 return ret;
Liu Bo2d324f52016-05-17 17:21:48 -07004506 } else {
4507 WARN_ON(!ret);
4508 if (ret == 1)
4509 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004510 }
Liu Bo2d324f52016-05-17 17:21:48 -07004511
Josef Bacik975f84f2010-11-23 19:36:57 +00004512 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004513 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004514 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004515
Chris Masonec29ed52011-02-23 16:23:20 -05004516 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004517 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004518 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004519 /* have to trust i_size as the end */
4520 last = (u64)-1;
4521 last_for_get_extent = isize;
4522 } else {
4523 /*
4524 * remember the start of the last extent. There are a
4525 * bunch of different factors that go into the length of the
4526 * extent, so its much less complex to remember where it started
4527 */
4528 last = found_key.offset;
4529 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004530 }
Liu Bofe09e162013-09-22 12:54:23 +08004531 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004532
Chris Masonec29ed52011-02-23 16:23:20 -05004533 /*
4534 * we might have some extents allocated but more delalloc past those
4535 * extents. so, we trust isize unless the start of the last extent is
4536 * beyond isize
4537 */
4538 if (last < isize) {
4539 last = (u64)-1;
4540 last_for_get_extent = isize;
4541 }
4542
David Sterbaff13db42015-12-03 14:30:40 +01004543 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004544 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004545
David Sterbae3350e12017-06-23 04:09:57 +02004546 em = get_extent_skip_holes(inode, start, last_for_get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004547 if (!em)
4548 goto out;
4549 if (IS_ERR(em)) {
4550 ret = PTR_ERR(em);
4551 goto out;
4552 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004553
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004554 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004555 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004556
Chris Masonea8efc72011-03-08 11:54:40 -05004557 /* break if the extent we found is outside the range */
4558 if (em->start >= max || extent_map_end(em) < off)
4559 break;
4560
4561 /*
4562 * get_extent may return an extent that starts before our
4563 * requested range. We have to make sure the ranges
4564 * we return to fiemap always move forward and don't
4565 * overlap, so adjust the offsets here
4566 */
4567 em_start = max(em->start, off);
4568
4569 /*
4570 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004571 * for adjusting the disk offset below. Only do this if the
4572 * extent isn't compressed since our in ram offset may be past
4573 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004574 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004575 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4576 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004577 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004578 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004579 flags = 0;
Filipe Mananaf0986312018-06-20 10:02:30 +01004580 if (em->block_start < EXTENT_MAP_LAST_BYTE)
4581 disko = em->block_start + offset_in_extent;
4582 else
4583 disko = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004584
Chris Masonea8efc72011-03-08 11:54:40 -05004585 /*
4586 * bump off for our next call to get_extent
4587 */
4588 off = extent_map_end(em);
4589 if (off >= max)
4590 end = 1;
4591
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004592 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004593 end = 1;
4594 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004595 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004596 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4597 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004598 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004599 flags |= (FIEMAP_EXTENT_DELALLOC |
4600 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004601 } else if (fieinfo->fi_extents_max) {
4602 u64 bytenr = em->block_start -
4603 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004604
Liu Bofe09e162013-09-22 12:54:23 +08004605 /*
4606 * As btrfs supports shared space, this information
4607 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004608 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4609 * then we're just getting a count and we can skip the
4610 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004611 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06004612 ret = btrfs_check_shared(root,
4613 btrfs_ino(BTRFS_I(inode)),
4614 bytenr);
Josef Bacikdc046b12014-09-10 16:20:45 -04004615 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004616 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004617 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004618 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004619 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004620 }
4621 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4622 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004623 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4624 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004625
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004626 free_extent_map(em);
4627 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004628 if ((em_start >= last) || em_len == (u64)-1 ||
4629 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004630 flags |= FIEMAP_EXTENT_LAST;
4631 end = 1;
4632 }
4633
Chris Masonec29ed52011-02-23 16:23:20 -05004634 /* now scan forward to see if this is really the last extent. */
David Sterbae3350e12017-06-23 04:09:57 +02004635 em = get_extent_skip_holes(inode, off, last_for_get_extent);
Chris Masonec29ed52011-02-23 16:23:20 -05004636 if (IS_ERR(em)) {
4637 ret = PTR_ERR(em);
4638 goto out;
4639 }
4640 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004641 flags |= FIEMAP_EXTENT_LAST;
4642 end = 1;
4643 }
Qu Wenruo47518322017-04-07 10:43:15 +08004644 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4645 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004646 if (ret) {
4647 if (ret == 1)
4648 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004649 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004650 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004651 }
4652out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004653 if (!ret)
Qu Wenruo848c23b2017-06-22 10:01:21 +08004654 ret = emit_last_fiemap_cache(root->fs_info, fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004655 free_extent_map(em);
4656out:
Liu Bofe09e162013-09-22 12:54:23 +08004657 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004658 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004659 &cached_state);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004660 return ret;
4661}
4662
Chris Mason727011e2010-08-06 13:21:20 -04004663static void __free_extent_buffer(struct extent_buffer *eb)
4664{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004665 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004666 kmem_cache_free(extent_buffer_cache, eb);
4667}
4668
Josef Bacika26e8c92014-03-28 17:07:27 -04004669int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004670{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004671 return (atomic_read(&eb->io_pages) ||
4672 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4673 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004674}
4675
Miao Xie897ca6e92010-10-26 20:57:29 -04004676/*
David Sterba55ac0132018-07-19 17:24:32 +02004677 * Release all pages attached to the extent buffer.
Miao Xie897ca6e92010-10-26 20:57:29 -04004678 */
David Sterba55ac0132018-07-19 17:24:32 +02004679static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004680{
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004681 int i;
4682 int num_pages;
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004683 int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004684
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004685 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004686
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004687 num_pages = num_extent_pages(eb);
4688 for (i = 0; i < num_pages; i++) {
4689 struct page *page = eb->pages[i];
Miao Xie897ca6e92010-10-26 20:57:29 -04004690
Forrest Liu5d2361d2015-02-09 17:31:45 +08004691 if (!page)
4692 continue;
4693 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004694 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004695 /*
4696 * We do this since we'll remove the pages after we've
4697 * removed the eb from the radix tree, so we could race
4698 * and have this page now attached to the new eb. So
4699 * only clear page_private if it's still connected to
4700 * this eb.
4701 */
4702 if (PagePrivate(page) &&
4703 page->private == (unsigned long)eb) {
4704 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4705 BUG_ON(PageDirty(page));
4706 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004707 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004708 * We need to make sure we haven't be attached
4709 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004710 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004711 ClearPagePrivate(page);
4712 set_page_private(page, 0);
4713 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004714 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004715 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004716
4717 if (mapped)
4718 spin_unlock(&page->mapping->private_lock);
4719
Nicholas D Steeves01327612016-05-19 21:18:45 -04004720 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004721 put_page(page);
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004722 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004723}
4724
4725/*
4726 * Helper for releasing the extent buffer.
4727 */
4728static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4729{
David Sterba55ac0132018-07-19 17:24:32 +02004730 btrfs_release_extent_buffer_pages(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004731 __free_extent_buffer(eb);
4732}
4733
Josef Bacikf28491e2013-12-16 13:24:27 -05004734static struct extent_buffer *
4735__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004736 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004737{
4738 struct extent_buffer *eb = NULL;
4739
Michal Hockod1b5c562015-08-19 14:17:40 +02004740 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004741 eb->start = start;
4742 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004743 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004744 eb->bflags = 0;
4745 rwlock_init(&eb->lock);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004746 atomic_set(&eb->blocking_readers, 0);
4747 atomic_set(&eb->blocking_writers, 0);
David Sterbaed1b4ed2018-08-24 16:31:17 +02004748 eb->lock_nested = false;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004749 init_waitqueue_head(&eb->write_lock_wq);
4750 init_waitqueue_head(&eb->read_lock_wq);
4751
4752 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4753
4754 spin_lock_init(&eb->refs_lock);
4755 atomic_set(&eb->refs, 1);
4756 atomic_set(&eb->io_pages, 0);
4757
4758 /*
4759 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4760 */
4761 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4762 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4763 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4764
David Sterba843ccf92018-08-24 14:56:28 +02004765#ifdef CONFIG_BTRFS_DEBUG
4766 atomic_set(&eb->spinning_writers, 0);
David Sterbaafd495a2018-08-24 15:57:38 +02004767 atomic_set(&eb->spinning_readers, 0);
David Sterba5c9c7992018-08-24 16:15:51 +02004768 atomic_set(&eb->read_locks, 0);
David Sterbac79adfc2018-08-24 16:24:26 +02004769 atomic_set(&eb->write_locks, 0);
David Sterba843ccf92018-08-24 14:56:28 +02004770#endif
4771
Josef Bacikdb7f3432013-08-07 14:54:37 -04004772 return eb;
4773}
4774
4775struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4776{
David Sterbacc5e31a2018-03-01 18:20:27 +01004777 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004778 struct page *p;
4779 struct extent_buffer *new;
David Sterbacc5e31a2018-03-01 18:20:27 +01004780 int num_pages = num_extent_pages(src);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004781
David Sterba3f556f72014-06-15 03:20:26 +02004782 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004783 if (new == NULL)
4784 return NULL;
4785
4786 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004787 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004788 if (!p) {
4789 btrfs_release_extent_buffer(new);
4790 return NULL;
4791 }
4792 attach_extent_buffer_page(new, p);
4793 WARN_ON(PageDirty(p));
4794 SetPageUptodate(p);
4795 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004796 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004797 }
4798
Josef Bacikdb7f3432013-08-07 14:54:37 -04004799 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004800 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004801
4802 return new;
4803}
4804
Omar Sandoval0f331222015-09-29 20:50:31 -07004805struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4806 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004807{
4808 struct extent_buffer *eb;
David Sterbacc5e31a2018-03-01 18:20:27 +01004809 int num_pages;
4810 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004811
David Sterba3f556f72014-06-15 03:20:26 +02004812 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004813 if (!eb)
4814 return NULL;
4815
David Sterba65ad0102018-06-29 10:56:49 +02004816 num_pages = num_extent_pages(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004817 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004818 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004819 if (!eb->pages[i])
4820 goto err;
4821 }
4822 set_extent_buffer_uptodate(eb);
4823 btrfs_set_header_nritems(eb, 0);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004824 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004825
4826 return eb;
4827err:
4828 for (; i > 0; i--)
4829 __free_page(eb->pages[i - 1]);
4830 __free_extent_buffer(eb);
4831 return NULL;
4832}
4833
Omar Sandoval0f331222015-09-29 20:50:31 -07004834struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004835 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004836{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004837 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004838}
4839
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004840static void check_buffer_tree_ref(struct extent_buffer *eb)
4841{
Chris Mason242e18c2013-01-29 17:49:37 -05004842 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004843 /* the ref bit is tricky. We have to make sure it is set
4844 * if we have the buffer dirty. Otherwise the
4845 * code to free a buffer can end up dropping a dirty
4846 * page
4847 *
4848 * Once the ref bit is set, it won't go away while the
4849 * buffer is dirty or in writeback, and it also won't
4850 * go away while we have the reference count on the
4851 * eb bumped.
4852 *
4853 * We can't just set the ref bit without bumping the
4854 * ref on the eb because free_extent_buffer might
4855 * see the ref bit and try to clear it. If this happens
4856 * free_extent_buffer might end up dropping our original
4857 * ref by mistake and freeing the page before we are able
4858 * to add one more ref.
4859 *
4860 * So bump the ref count first, then set the bit. If someone
4861 * beat us to it, drop the ref we added.
4862 */
Chris Mason242e18c2013-01-29 17:49:37 -05004863 refs = atomic_read(&eb->refs);
4864 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4865 return;
4866
Josef Bacik594831c2012-07-20 16:11:08 -04004867 spin_lock(&eb->refs_lock);
4868 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004869 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004870 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004871}
4872
Mel Gorman2457aec2014-06-04 16:10:31 -07004873static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4874 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004875{
David Sterbacc5e31a2018-03-01 18:20:27 +01004876 int num_pages, i;
Josef Bacik5df42352012-03-15 18:24:42 -04004877
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004878 check_buffer_tree_ref(eb);
4879
David Sterba65ad0102018-06-29 10:56:49 +02004880 num_pages = num_extent_pages(eb);
Josef Bacik5df42352012-03-15 18:24:42 -04004881 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004882 struct page *p = eb->pages[i];
4883
Mel Gorman2457aec2014-06-04 16:10:31 -07004884 if (p != accessed)
4885 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004886 }
4887}
4888
Josef Bacikf28491e2013-12-16 13:24:27 -05004889struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4890 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004891{
4892 struct extent_buffer *eb;
4893
4894 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004895 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004896 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004897 if (eb && atomic_inc_not_zero(&eb->refs)) {
4898 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004899 /*
4900 * Lock our eb's refs_lock to avoid races with
4901 * free_extent_buffer. When we get our eb it might be flagged
4902 * with EXTENT_BUFFER_STALE and another task running
4903 * free_extent_buffer might have seen that flag set,
4904 * eb->refs == 2, that the buffer isn't under IO (dirty and
4905 * writeback flags not set) and it's still in the tree (flag
4906 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4907 * of decrementing the extent buffer's reference count twice.
4908 * So here we could race and increment the eb's reference count,
4909 * clear its stale flag, mark it as dirty and drop our reference
4910 * before the other task finishes executing free_extent_buffer,
4911 * which would later result in an attempt to free an extent
4912 * buffer that is dirty.
4913 */
4914 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4915 spin_lock(&eb->refs_lock);
4916 spin_unlock(&eb->refs_lock);
4917 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004918 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004919 return eb;
4920 }
4921 rcu_read_unlock();
4922
4923 return NULL;
4924}
4925
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004926#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4927struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004928 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004929{
4930 struct extent_buffer *eb, *exists = NULL;
4931 int ret;
4932
4933 eb = find_extent_buffer(fs_info, start);
4934 if (eb)
4935 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04004936 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004937 if (!eb)
4938 return NULL;
4939 eb->fs_info = fs_info;
4940again:
David Sterbae1860a72016-05-09 14:11:38 +02004941 ret = radix_tree_preload(GFP_NOFS);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004942 if (ret)
4943 goto free_eb;
4944 spin_lock(&fs_info->buffer_lock);
4945 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004946 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004947 spin_unlock(&fs_info->buffer_lock);
4948 radix_tree_preload_end();
4949 if (ret == -EEXIST) {
4950 exists = find_extent_buffer(fs_info, start);
4951 if (exists)
4952 goto free_eb;
4953 else
4954 goto again;
4955 }
4956 check_buffer_tree_ref(eb);
4957 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
4958
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004959 return eb;
4960free_eb:
4961 btrfs_release_extent_buffer(eb);
4962 return exists;
4963}
4964#endif
4965
Josef Bacikf28491e2013-12-16 13:24:27 -05004966struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004967 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05004968{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004969 unsigned long len = fs_info->nodesize;
David Sterbacc5e31a2018-03-01 18:20:27 +01004970 int num_pages;
4971 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004972 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004973 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004974 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004975 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05004976 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05004977 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004978 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004979
Jeff Mahoneyda170662016-06-15 09:22:56 -04004980 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07004981 btrfs_err(fs_info, "bad tree block start %llu", start);
4982 return ERR_PTR(-EINVAL);
4983 }
4984
Josef Bacikf28491e2013-12-16 13:24:27 -05004985 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004986 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004987 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004988
David Sterba23d79d82014-06-15 02:55:29 +02004989 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04004990 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07004991 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05004992
David Sterba65ad0102018-06-29 10:56:49 +02004993 num_pages = num_extent_pages(eb);
Chris Mason727011e2010-08-06 13:21:20 -04004994 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02004995 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07004996 if (!p) {
4997 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04004998 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004999 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005000
5001 spin_lock(&mapping->private_lock);
5002 if (PagePrivate(p)) {
5003 /*
5004 * We could have already allocated an eb for this page
5005 * and attached one so lets see if we can get a ref on
5006 * the existing eb, and if we can we know it's good and
5007 * we can just return that one, else we know we can just
5008 * overwrite page->private.
5009 */
5010 exists = (struct extent_buffer *)p->private;
5011 if (atomic_inc_not_zero(&exists->refs)) {
5012 spin_unlock(&mapping->private_lock);
5013 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005014 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07005015 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005016 goto free_eb;
5017 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005018 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005019
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005020 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005021 * Do this so attach doesn't complain and we need to
5022 * drop the ref the old guy had.
5023 */
5024 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005025 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005026 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05005027 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005028 attach_extent_buffer_page(eb, p);
5029 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005030 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04005031 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05005032 if (!PageUptodate(p))
5033 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05005034
5035 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005036 * We can't unlock the pages just yet since the extent buffer
5037 * hasn't been properly inserted in the radix tree, this
5038 * opens a race with btree_releasepage which can free a page
5039 * while we are still filling in all pages for the buffer and
5040 * we could crash.
Chris Masoneb14ab82011-02-10 12:35:00 -05005041 */
Chris Masond1310b22008-01-24 16:13:08 -05005042 }
5043 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05005044 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05005045again:
David Sterbae1860a72016-05-09 14:11:38 +02005046 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07005047 if (ret) {
5048 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04005049 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005050 }
Miao Xie19fe0a82010-10-26 20:57:29 -04005051
Josef Bacikf28491e2013-12-16 13:24:27 -05005052 spin_lock(&fs_info->buffer_lock);
5053 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005054 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05005055 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005056 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04005057 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005058 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005059 if (exists)
5060 goto free_eb;
5061 else
Josef Bacik115391d2012-03-09 09:51:43 -05005062 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04005063 }
Chris Mason6af118ce2008-07-22 11:18:07 -04005064 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005065 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005066 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005067
5068 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005069 * Now it's safe to unlock the pages because any calls to
5070 * btree_releasepage will correctly detect that a page belongs to a
5071 * live buffer and won't free them prematurely.
Chris Masoneb14ab82011-02-10 12:35:00 -05005072 */
Nikolay Borisov28187ae2018-07-04 10:24:51 +03005073 for (i = 0; i < num_pages; i++)
5074 unlock_page(eb->pages[i]);
Chris Masond1310b22008-01-24 16:13:08 -05005075 return eb;
5076
Chris Mason6af118ce2008-07-22 11:18:07 -04005077free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005078 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005079 for (i = 0; i < num_pages; i++) {
5080 if (eb->pages[i])
5081 unlock_page(eb->pages[i]);
5082 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005083
Miao Xie897ca6e92010-10-26 20:57:29 -04005084 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005085 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005086}
Chris Masond1310b22008-01-24 16:13:08 -05005087
Josef Bacik3083ee22012-03-09 16:01:49 -05005088static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5089{
5090 struct extent_buffer *eb =
5091 container_of(head, struct extent_buffer, rcu_head);
5092
5093 __free_extent_buffer(eb);
5094}
5095
David Sterbaf7a52a42013-04-26 14:56:29 +00005096static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005097{
Nikolay Borisov07e21c42018-06-27 16:38:23 +03005098 lockdep_assert_held(&eb->refs_lock);
5099
Josef Bacik3083ee22012-03-09 16:01:49 -05005100 WARN_ON(atomic_read(&eb->refs) == 0);
5101 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005102 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005103 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005104
Jan Schmidt815a51c2012-05-16 17:00:02 +02005105 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005106
Josef Bacikf28491e2013-12-16 13:24:27 -05005107 spin_lock(&fs_info->buffer_lock);
5108 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005109 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005110 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005111 } else {
5112 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005113 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005114
5115 /* Should be safe to release our pages at this point */
David Sterba55ac0132018-07-19 17:24:32 +02005116 btrfs_release_extent_buffer_pages(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005117#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005118 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
Josef Bacikbcb7e442015-03-16 17:38:02 -04005119 __free_extent_buffer(eb);
5120 return 1;
5121 }
5122#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005123 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005124 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005125 }
5126 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005127
5128 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005129}
5130
Chris Masond1310b22008-01-24 16:13:08 -05005131void free_extent_buffer(struct extent_buffer *eb)
5132{
Chris Mason242e18c2013-01-29 17:49:37 -05005133 int refs;
5134 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005135 if (!eb)
5136 return;
5137
Chris Mason242e18c2013-01-29 17:49:37 -05005138 while (1) {
5139 refs = atomic_read(&eb->refs);
Nikolay Borisov46cc7752018-10-15 17:04:01 +03005140 if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
5141 || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
5142 refs == 1))
Chris Mason242e18c2013-01-29 17:49:37 -05005143 break;
5144 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5145 if (old == refs)
5146 return;
5147 }
5148
Josef Bacik3083ee22012-03-09 16:01:49 -05005149 spin_lock(&eb->refs_lock);
5150 if (atomic_read(&eb->refs) == 2 &&
5151 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005152 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005153 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5154 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005155
Josef Bacik3083ee22012-03-09 16:01:49 -05005156 /*
5157 * I know this is terrible, but it's temporary until we stop tracking
5158 * the uptodate bits and such for the extent buffers.
5159 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005160 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005161}
Chris Masond1310b22008-01-24 16:13:08 -05005162
Josef Bacik3083ee22012-03-09 16:01:49 -05005163void free_extent_buffer_stale(struct extent_buffer *eb)
5164{
5165 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005166 return;
5167
Josef Bacik3083ee22012-03-09 16:01:49 -05005168 spin_lock(&eb->refs_lock);
5169 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5170
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005171 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005172 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5173 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005174 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005175}
5176
Chris Mason1d4284b2012-03-28 20:31:37 -04005177void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005178{
David Sterbacc5e31a2018-03-01 18:20:27 +01005179 int i;
5180 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005181 struct page *page;
5182
David Sterba65ad0102018-06-29 10:56:49 +02005183 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005184
5185 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005186 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005187 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005188 continue;
5189
Chris Masona61e6f22008-07-22 11:18:08 -04005190 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005191 WARN_ON(!PagePrivate(page));
5192
Chris Masond1310b22008-01-24 16:13:08 -05005193 clear_page_dirty_for_io(page);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005194 xa_lock_irq(&page->mapping->i_pages);
Matthew Wilcox0a943c62017-12-04 10:37:22 -05005195 if (!PageDirty(page))
5196 __xa_clear_mark(&page->mapping->i_pages,
5197 page_index(page), PAGECACHE_TAG_DIRTY);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005198 xa_unlock_irq(&page->mapping->i_pages);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005199 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005200 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005201 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005202 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005203}
Chris Masond1310b22008-01-24 16:13:08 -05005204
Liu Boabb57ef2018-09-14 01:44:42 +08005205bool set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005206{
David Sterbacc5e31a2018-03-01 18:20:27 +01005207 int i;
5208 int num_pages;
Liu Boabb57ef2018-09-14 01:44:42 +08005209 bool was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005210
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005211 check_buffer_tree_ref(eb);
5212
Chris Masonb9473432009-03-13 11:00:37 -04005213 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005214
David Sterba65ad0102018-06-29 10:56:49 +02005215 num_pages = num_extent_pages(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05005216 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005217 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5218
Liu Boabb57ef2018-09-14 01:44:42 +08005219 if (!was_dirty)
5220 for (i = 0; i < num_pages; i++)
5221 set_page_dirty(eb->pages[i]);
Liu Bo51995c32018-09-14 01:46:08 +08005222
5223#ifdef CONFIG_BTRFS_DEBUG
5224 for (i = 0; i < num_pages; i++)
5225 ASSERT(PageDirty(eb->pages[i]));
5226#endif
5227
Chris Masonb9473432009-03-13 11:00:37 -04005228 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005229}
Chris Masond1310b22008-01-24 16:13:08 -05005230
David Sterba69ba3922015-12-03 13:08:59 +01005231void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005232{
David Sterbacc5e31a2018-03-01 18:20:27 +01005233 int i;
Chris Mason1259ab72008-05-12 13:39:03 -04005234 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005235 int num_pages;
Chris Mason1259ab72008-05-12 13:39:03 -04005236
Chris Masonb4ce94d2009-02-04 09:25:08 -05005237 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005238 num_pages = num_extent_pages(eb);
Chris Mason1259ab72008-05-12 13:39:03 -04005239 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005240 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005241 if (page)
5242 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005243 }
Chris Mason1259ab72008-05-12 13:39:03 -04005244}
5245
David Sterba09c25a82015-12-03 13:08:59 +01005246void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005247{
David Sterbacc5e31a2018-03-01 18:20:27 +01005248 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005249 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005250 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005251
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005252 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005253 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005254 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005255 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005256 SetPageUptodate(page);
5257 }
Chris Masond1310b22008-01-24 16:13:08 -05005258}
Chris Masond1310b22008-01-24 16:13:08 -05005259
Chris Masond1310b22008-01-24 16:13:08 -05005260int read_extent_buffer_pages(struct extent_io_tree *tree,
David Sterba6af49db2017-06-23 04:09:57 +02005261 struct extent_buffer *eb, int wait, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005262{
David Sterbacc5e31a2018-03-01 18:20:27 +01005263 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005264 struct page *page;
5265 int err;
5266 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005267 int locked_pages = 0;
5268 int all_uptodate = 1;
David Sterbacc5e31a2018-03-01 18:20:27 +01005269 int num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005270 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005271 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005272 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005273
Chris Masonb4ce94d2009-02-04 09:25:08 -05005274 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005275 return 0;
5276
David Sterba65ad0102018-06-29 10:56:49 +02005277 num_pages = num_extent_pages(eb);
Josef Bacik8436ea912016-09-02 15:40:03 -04005278 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005279 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005280 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005281 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005282 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005283 } else {
5284 lock_page(page);
5285 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005286 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005287 }
5288 /*
5289 * We need to firstly lock all pages to make sure that
5290 * the uptodate bit of our pages won't be affected by
5291 * clear_extent_buffer_uptodate().
5292 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005293 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005294 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005295 if (!PageUptodate(page)) {
5296 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005297 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005298 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005299 }
Liu Bo2571e732016-08-03 12:33:01 -07005300
Chris Masonce9adaa2008-04-09 16:28:12 -04005301 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005302 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005303 goto unlock_exit;
5304 }
5305
Filipe Manana656f30d2014-09-26 12:25:56 +01005306 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005307 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005308 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005309 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005310 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005311
Chris Masonce9adaa2008-04-09 16:28:12 -04005312 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005313 if (ret) {
5314 atomic_dec(&eb->io_pages);
5315 unlock_page(page);
5316 continue;
5317 }
5318
Chris Masonf1885912008-04-09 16:28:12 -04005319 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005320 err = __extent_read_full_page(tree, page,
David Sterba6af49db2017-06-23 04:09:57 +02005321 btree_get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005322 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005323 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005324 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005325 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005326 /*
5327 * We use &bio in above __extent_read_full_page,
5328 * so we ensure that if it returns error, the
5329 * current page fails to add itself to bio and
5330 * it's been unlocked.
5331 *
5332 * We must dec io_pages by ourselves.
5333 */
5334 atomic_dec(&eb->io_pages);
5335 }
Chris Masond1310b22008-01-24 16:13:08 -05005336 } else {
5337 unlock_page(page);
5338 }
5339 }
5340
Jeff Mahoney355808c2011-10-03 23:23:14 -04005341 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005342 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005343 if (err)
5344 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005345 }
Chris Masona86c12c2008-02-07 10:50:54 -05005346
Arne Jansenbb82ab82011-06-10 14:06:53 +02005347 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005348 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005349
Josef Bacik8436ea912016-09-02 15:40:03 -04005350 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005351 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005352 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005353 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005354 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005355 }
Chris Masond3977122009-01-05 21:25:51 -05005356
Chris Masond1310b22008-01-24 16:13:08 -05005357 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005358
5359unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005360 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005361 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005362 page = eb->pages[locked_pages];
5363 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005364 }
5365 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005366}
Chris Masond1310b22008-01-24 16:13:08 -05005367
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005368void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5369 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005370{
5371 size_t cur;
5372 size_t offset;
5373 struct page *page;
5374 char *kaddr;
5375 char *dst = (char *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005376 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005377 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005378
Liu Bof716abd2017-08-09 11:10:16 -06005379 if (start + len > eb->len) {
5380 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5381 eb->start, eb->len, start, len);
5382 memset(dst, 0, len);
5383 return;
5384 }
Chris Masond1310b22008-01-24 16:13:08 -05005385
Johannes Thumshirn70730172018-12-05 15:23:03 +01005386 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005387
Chris Masond3977122009-01-05 21:25:51 -05005388 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005389 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005390
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005391 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005392 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005393 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005394
5395 dst += cur;
5396 len -= cur;
5397 offset = 0;
5398 i++;
5399 }
5400}
Chris Masond1310b22008-01-24 16:13:08 -05005401
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005402int read_extent_buffer_to_user(const struct extent_buffer *eb,
5403 void __user *dstv,
5404 unsigned long start, unsigned long len)
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005405{
5406 size_t cur;
5407 size_t offset;
5408 struct page *page;
5409 char *kaddr;
5410 char __user *dst = (char __user *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005411 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005412 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005413 int ret = 0;
5414
5415 WARN_ON(start > eb->len);
5416 WARN_ON(start + len > eb->start + eb->len);
5417
Johannes Thumshirn70730172018-12-05 15:23:03 +01005418 offset = offset_in_page(start_offset + start);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005419
5420 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005421 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005422
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005423 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005424 kaddr = page_address(page);
5425 if (copy_to_user(dst, kaddr + offset, cur)) {
5426 ret = -EFAULT;
5427 break;
5428 }
5429
5430 dst += cur;
5431 len -= cur;
5432 offset = 0;
5433 i++;
5434 }
5435
5436 return ret;
5437}
5438
Liu Bo415b35a2016-06-17 19:16:21 -07005439/*
5440 * return 0 if the item is found within a page.
5441 * return 1 if the item spans two pages.
5442 * return -EINVAL otherwise.
5443 */
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005444int map_private_extent_buffer(const struct extent_buffer *eb,
5445 unsigned long start, unsigned long min_len,
5446 char **map, unsigned long *map_start,
5447 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005448{
Johannes Thumshirncc2c39d2018-11-28 09:54:54 +01005449 size_t offset;
Chris Masond1310b22008-01-24 16:13:08 -05005450 char *kaddr;
5451 struct page *p;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005452 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005453 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005454 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005455 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005456
Liu Bof716abd2017-08-09 11:10:16 -06005457 if (start + min_len > eb->len) {
5458 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5459 eb->start, eb->len, start, min_len);
5460 return -EINVAL;
5461 }
5462
Chris Masond1310b22008-01-24 16:13:08 -05005463 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005464 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005465
5466 if (i == 0) {
5467 offset = start_offset;
5468 *map_start = 0;
5469 } else {
5470 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005471 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005472 }
Chris Masond3977122009-01-05 21:25:51 -05005473
David Sterbafb85fc92014-07-31 01:03:53 +02005474 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005475 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005476 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005477 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005478 return 0;
5479}
Chris Masond1310b22008-01-24 16:13:08 -05005480
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005481int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5482 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005483{
5484 size_t cur;
5485 size_t offset;
5486 struct page *page;
5487 char *kaddr;
5488 char *ptr = (char *)ptrv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005489 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005490 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005491 int ret = 0;
5492
5493 WARN_ON(start > eb->len);
5494 WARN_ON(start + len > eb->start + eb->len);
5495
Johannes Thumshirn70730172018-12-05 15:23:03 +01005496 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005497
Chris Masond3977122009-01-05 21:25:51 -05005498 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005499 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005500
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005501 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005502
Chris Masona6591712011-07-19 12:04:14 -04005503 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005504 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005505 if (ret)
5506 break;
5507
5508 ptr += cur;
5509 len -= cur;
5510 offset = 0;
5511 i++;
5512 }
5513 return ret;
5514}
Chris Masond1310b22008-01-24 16:13:08 -05005515
David Sterbaf157bf72016-11-09 17:43:38 +01005516void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5517 const void *srcv)
5518{
5519 char *kaddr;
5520
5521 WARN_ON(!PageUptodate(eb->pages[0]));
5522 kaddr = page_address(eb->pages[0]);
5523 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5524 BTRFS_FSID_SIZE);
5525}
5526
5527void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5528{
5529 char *kaddr;
5530
5531 WARN_ON(!PageUptodate(eb->pages[0]));
5532 kaddr = page_address(eb->pages[0]);
5533 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5534 BTRFS_FSID_SIZE);
5535}
5536
Chris Masond1310b22008-01-24 16:13:08 -05005537void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5538 unsigned long start, unsigned long len)
5539{
5540 size_t cur;
5541 size_t offset;
5542 struct page *page;
5543 char *kaddr;
5544 char *src = (char *)srcv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005545 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005546 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005547
5548 WARN_ON(start > eb->len);
5549 WARN_ON(start + len > eb->start + eb->len);
5550
Johannes Thumshirn70730172018-12-05 15:23:03 +01005551 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005552
Chris Masond3977122009-01-05 21:25:51 -05005553 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005554 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005555 WARN_ON(!PageUptodate(page));
5556
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005557 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005558 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005559 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005560
5561 src += cur;
5562 len -= cur;
5563 offset = 0;
5564 i++;
5565 }
5566}
Chris Masond1310b22008-01-24 16:13:08 -05005567
David Sterbab159fa22016-11-08 18:09:03 +01005568void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5569 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005570{
5571 size_t cur;
5572 size_t offset;
5573 struct page *page;
5574 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005575 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005576 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005577
5578 WARN_ON(start > eb->len);
5579 WARN_ON(start + len > eb->start + eb->len);
5580
Johannes Thumshirn70730172018-12-05 15:23:03 +01005581 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005582
Chris Masond3977122009-01-05 21:25:51 -05005583 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005584 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005585 WARN_ON(!PageUptodate(page));
5586
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005587 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005588 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005589 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005590
5591 len -= cur;
5592 offset = 0;
5593 i++;
5594 }
5595}
Chris Masond1310b22008-01-24 16:13:08 -05005596
David Sterba58e80122016-11-08 18:30:31 +01005597void copy_extent_buffer_full(struct extent_buffer *dst,
5598 struct extent_buffer *src)
5599{
5600 int i;
David Sterbacc5e31a2018-03-01 18:20:27 +01005601 int num_pages;
David Sterba58e80122016-11-08 18:30:31 +01005602
5603 ASSERT(dst->len == src->len);
5604
David Sterba65ad0102018-06-29 10:56:49 +02005605 num_pages = num_extent_pages(dst);
David Sterba58e80122016-11-08 18:30:31 +01005606 for (i = 0; i < num_pages; i++)
5607 copy_page(page_address(dst->pages[i]),
5608 page_address(src->pages[i]));
5609}
5610
Chris Masond1310b22008-01-24 16:13:08 -05005611void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5612 unsigned long dst_offset, unsigned long src_offset,
5613 unsigned long len)
5614{
5615 u64 dst_len = dst->len;
5616 size_t cur;
5617 size_t offset;
5618 struct page *page;
5619 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005620 size_t start_offset = offset_in_page(dst->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005621 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005622
5623 WARN_ON(src->len != dst_len);
5624
Johannes Thumshirn70730172018-12-05 15:23:03 +01005625 offset = offset_in_page(start_offset + dst_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005626
Chris Masond3977122009-01-05 21:25:51 -05005627 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005628 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005629 WARN_ON(!PageUptodate(page));
5630
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005631 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005632
Chris Masona6591712011-07-19 12:04:14 -04005633 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005634 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005635
5636 src_offset += cur;
5637 len -= cur;
5638 offset = 0;
5639 i++;
5640 }
5641}
Chris Masond1310b22008-01-24 16:13:08 -05005642
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005643/*
5644 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5645 * given bit number
5646 * @eb: the extent buffer
5647 * @start: offset of the bitmap item in the extent buffer
5648 * @nr: bit number
5649 * @page_index: return index of the page in the extent buffer that contains the
5650 * given bit number
5651 * @page_offset: return offset into the page given by page_index
5652 *
5653 * This helper hides the ugliness of finding the byte in an extent buffer which
5654 * contains a given bit.
5655 */
5656static inline void eb_bitmap_offset(struct extent_buffer *eb,
5657 unsigned long start, unsigned long nr,
5658 unsigned long *page_index,
5659 size_t *page_offset)
5660{
Johannes Thumshirn70730172018-12-05 15:23:03 +01005661 size_t start_offset = offset_in_page(eb->start);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005662 size_t byte_offset = BIT_BYTE(nr);
5663 size_t offset;
5664
5665 /*
5666 * The byte we want is the offset of the extent buffer + the offset of
5667 * the bitmap item in the extent buffer + the offset of the byte in the
5668 * bitmap item.
5669 */
5670 offset = start_offset + start + byte_offset;
5671
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005672 *page_index = offset >> PAGE_SHIFT;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005673 *page_offset = offset_in_page(offset);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005674}
5675
5676/**
5677 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5678 * @eb: the extent buffer
5679 * @start: offset of the bitmap item in the extent buffer
5680 * @nr: bit number to test
5681 */
5682int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5683 unsigned long nr)
5684{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005685 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005686 struct page *page;
5687 unsigned long i;
5688 size_t offset;
5689
5690 eb_bitmap_offset(eb, start, nr, &i, &offset);
5691 page = eb->pages[i];
5692 WARN_ON(!PageUptodate(page));
5693 kaddr = page_address(page);
5694 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5695}
5696
5697/**
5698 * extent_buffer_bitmap_set - set an area of a bitmap
5699 * @eb: the extent buffer
5700 * @start: offset of the bitmap item in the extent buffer
5701 * @pos: bit number of the first bit
5702 * @len: number of bits to set
5703 */
5704void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5705 unsigned long pos, unsigned long len)
5706{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005707 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005708 struct page *page;
5709 unsigned long i;
5710 size_t offset;
5711 const unsigned int size = pos + len;
5712 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005713 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005714
5715 eb_bitmap_offset(eb, start, pos, &i, &offset);
5716 page = eb->pages[i];
5717 WARN_ON(!PageUptodate(page));
5718 kaddr = page_address(page);
5719
5720 while (len >= bits_to_set) {
5721 kaddr[offset] |= mask_to_set;
5722 len -= bits_to_set;
5723 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005724 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005725 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005726 offset = 0;
5727 page = eb->pages[++i];
5728 WARN_ON(!PageUptodate(page));
5729 kaddr = page_address(page);
5730 }
5731 }
5732 if (len) {
5733 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5734 kaddr[offset] |= mask_to_set;
5735 }
5736}
5737
5738
5739/**
5740 * extent_buffer_bitmap_clear - clear an area of a bitmap
5741 * @eb: the extent buffer
5742 * @start: offset of the bitmap item in the extent buffer
5743 * @pos: bit number of the first bit
5744 * @len: number of bits to clear
5745 */
5746void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5747 unsigned long pos, unsigned long len)
5748{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005749 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005750 struct page *page;
5751 unsigned long i;
5752 size_t offset;
5753 const unsigned int size = pos + len;
5754 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005755 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005756
5757 eb_bitmap_offset(eb, start, pos, &i, &offset);
5758 page = eb->pages[i];
5759 WARN_ON(!PageUptodate(page));
5760 kaddr = page_address(page);
5761
5762 while (len >= bits_to_clear) {
5763 kaddr[offset] &= ~mask_to_clear;
5764 len -= bits_to_clear;
5765 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005766 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005767 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005768 offset = 0;
5769 page = eb->pages[++i];
5770 WARN_ON(!PageUptodate(page));
5771 kaddr = page_address(page);
5772 }
5773 }
5774 if (len) {
5775 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5776 kaddr[offset] &= ~mask_to_clear;
5777 }
5778}
5779
Sergei Trofimovich33872062011-04-11 21:52:52 +00005780static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5781{
5782 unsigned long distance = (src > dst) ? src - dst : dst - src;
5783 return distance < len;
5784}
5785
Chris Masond1310b22008-01-24 16:13:08 -05005786static void copy_pages(struct page *dst_page, struct page *src_page,
5787 unsigned long dst_off, unsigned long src_off,
5788 unsigned long len)
5789{
Chris Masona6591712011-07-19 12:04:14 -04005790 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005791 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005792 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005793
Sergei Trofimovich33872062011-04-11 21:52:52 +00005794 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005795 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005796 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005797 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005798 if (areas_overlap(src_off, dst_off, len))
5799 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005800 }
Chris Masond1310b22008-01-24 16:13:08 -05005801
Chris Mason727011e2010-08-06 13:21:20 -04005802 if (must_memmove)
5803 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5804 else
5805 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005806}
5807
5808void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5809 unsigned long src_offset, unsigned long len)
5810{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005811 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005812 size_t cur;
5813 size_t dst_off_in_page;
5814 size_t src_off_in_page;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005815 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005816 unsigned long dst_i;
5817 unsigned long src_i;
5818
5819 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005820 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005821 "memmove bogus src_offset %lu move len %lu dst len %lu",
5822 src_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01005823 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05005824 }
5825 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005826 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005827 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5828 dst_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01005829 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05005830 }
5831
Chris Masond3977122009-01-05 21:25:51 -05005832 while (len > 0) {
Johannes Thumshirn70730172018-12-05 15:23:03 +01005833 dst_off_in_page = offset_in_page(start_offset + dst_offset);
5834 src_off_in_page = offset_in_page(start_offset + src_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005835
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005836 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5837 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005838
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005839 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005840 src_off_in_page));
5841 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005842 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005843
David Sterbafb85fc92014-07-31 01:03:53 +02005844 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005845 dst_off_in_page, src_off_in_page, cur);
5846
5847 src_offset += cur;
5848 dst_offset += cur;
5849 len -= cur;
5850 }
5851}
Chris Masond1310b22008-01-24 16:13:08 -05005852
5853void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5854 unsigned long src_offset, unsigned long len)
5855{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005856 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005857 size_t cur;
5858 size_t dst_off_in_page;
5859 size_t src_off_in_page;
5860 unsigned long dst_end = dst_offset + len - 1;
5861 unsigned long src_end = src_offset + len - 1;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005862 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005863 unsigned long dst_i;
5864 unsigned long src_i;
5865
5866 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005867 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005868 "memmove bogus src_offset %lu move len %lu len %lu",
5869 src_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01005870 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05005871 }
5872 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005873 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005874 "memmove bogus dst_offset %lu move len %lu len %lu",
5875 dst_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01005876 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05005877 }
Chris Mason727011e2010-08-06 13:21:20 -04005878 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005879 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5880 return;
5881 }
Chris Masond3977122009-01-05 21:25:51 -05005882 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005883 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
5884 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005885
Johannes Thumshirn70730172018-12-05 15:23:03 +01005886 dst_off_in_page = offset_in_page(start_offset + dst_end);
5887 src_off_in_page = offset_in_page(start_offset + src_end);
Chris Masond1310b22008-01-24 16:13:08 -05005888
5889 cur = min_t(unsigned long, len, src_off_in_page + 1);
5890 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005891 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005892 dst_off_in_page - cur + 1,
5893 src_off_in_page - cur + 1, cur);
5894
5895 dst_end -= cur;
5896 src_end -= cur;
5897 len -= cur;
5898 }
5899}
Chris Mason6af118ce2008-07-22 11:18:07 -04005900
David Sterbaf7a52a42013-04-26 14:56:29 +00005901int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005902{
Chris Mason6af118ce2008-07-22 11:18:07 -04005903 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005904
Miao Xie19fe0a82010-10-26 20:57:29 -04005905 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005906 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05005907 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005908 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005909 spin_lock(&page->mapping->private_lock);
5910 if (!PagePrivate(page)) {
5911 spin_unlock(&page->mapping->private_lock);
5912 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005913 }
5914
Josef Bacik3083ee22012-03-09 16:01:49 -05005915 eb = (struct extent_buffer *)page->private;
5916 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005917
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005918 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005919 * This is a little awful but should be ok, we need to make sure that
5920 * the eb doesn't disappear out from under us while we're looking at
5921 * this page.
5922 */
5923 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005924 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005925 spin_unlock(&eb->refs_lock);
5926 spin_unlock(&page->mapping->private_lock);
5927 return 0;
5928 }
5929 spin_unlock(&page->mapping->private_lock);
5930
Josef Bacik3083ee22012-03-09 16:01:49 -05005931 /*
5932 * If tree ref isn't set then we know the ref on this eb is a real ref,
5933 * so just return, this page will likely be freed soon anyway.
5934 */
5935 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5936 spin_unlock(&eb->refs_lock);
5937 return 0;
5938 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005939
David Sterbaf7a52a42013-04-26 14:56:29 +00005940 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005941}