blob: 26d7b6a5d567bbfc4f3befbd96b6aba3103c6ec7 [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
Qu Wenruofefdc552015-10-12 15:35:38 +08001371int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1372 unsigned bits, int wake, int delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001373 struct extent_state **cached)
Qu Wenruofefdc552015-10-12 15:35:38 +08001374{
1375 return __clear_extent_bit(tree, start, end, bits, wake, delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001376 cached, GFP_NOFS, NULL);
Qu Wenruofefdc552015-10-12 15:35:38 +08001377}
1378
Qu Wenruofefdc552015-10-12 15:35:38 +08001379int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaf734c442016-04-26 23:54:39 +02001380 unsigned bits, struct extent_changeset *changeset)
Qu Wenruofefdc552015-10-12 15:35:38 +08001381{
1382 /*
1383 * Don't support EXTENT_LOCKED case, same reason as
1384 * set_record_extent_bits().
1385 */
1386 BUG_ON(bits & EXTENT_LOCKED);
1387
David Sterbaf734c442016-04-26 23:54:39 +02001388 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
Qu Wenruofefdc552015-10-12 15:35:38 +08001389 changeset);
1390}
1391
Chris Masond352ac62008-09-29 15:18:18 -04001392/*
1393 * either insert or lock state struct between start and end use mask to tell
1394 * us if waiting is desired.
1395 */
Chris Mason1edbb732009-09-02 13:24:36 -04001396int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaff13db42015-12-03 14:30:40 +01001397 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001398{
1399 int err;
1400 u64 failed_start;
David Sterba9ee49a042015-01-14 19:52:13 +01001401
Chris Masond1310b22008-01-24 16:13:08 -05001402 while (1) {
David Sterbaff13db42015-12-03 14:30:40 +01001403 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001404 EXTENT_LOCKED, &failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001405 cached_state, GFP_NOFS, NULL);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001406 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001407 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1408 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001409 } else
Chris Masond1310b22008-01-24 16:13:08 -05001410 break;
Chris Masond1310b22008-01-24 16:13:08 -05001411 WARN_ON(start > end);
1412 }
1413 return err;
1414}
Chris Masond1310b22008-01-24 16:13:08 -05001415
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001416int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001417{
1418 int err;
1419 u64 failed_start;
1420
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001421 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
Qu Wenruod38ed272015-10-12 14:53:37 +08001422 &failed_start, NULL, GFP_NOFS, NULL);
Yan Zheng66435582008-10-30 14:19:50 -04001423 if (err == -EEXIST) {
1424 if (failed_start > start)
1425 clear_extent_bit(tree, start, failed_start - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001426 EXTENT_LOCKED, 1, 0, NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001427 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001428 }
Josef Bacik25179202008-10-29 14:49:05 -04001429 return 1;
1430}
Josef Bacik25179202008-10-29 14:49:05 -04001431
David Sterbabd1fa4f2015-12-03 13:08:59 +01001432void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001433{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001434 unsigned long index = start >> PAGE_SHIFT;
1435 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001436 struct page *page;
1437
1438 while (index <= end_index) {
1439 page = find_get_page(inode->i_mapping, index);
1440 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1441 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001442 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001443 index++;
1444 }
Chris Mason4adaa612013-03-26 13:07:00 -04001445}
1446
David Sterbaf6311572015-12-03 13:08:59 +01001447void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001448{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001449 unsigned long index = start >> PAGE_SHIFT;
1450 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001451 struct page *page;
1452
1453 while (index <= end_index) {
1454 page = find_get_page(inode->i_mapping, index);
1455 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Mason4adaa612013-03-26 13:07:00 -04001456 __set_page_dirty_nobuffers(page);
Konstantin Khebnikov8d386332015-02-11 15:26:55 -08001457 account_page_redirty(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001458 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001459 index++;
1460 }
Chris Mason4adaa612013-03-26 13:07:00 -04001461}
1462
Chris Masond352ac62008-09-29 15:18:18 -04001463/* find the first state struct with 'bits' set after 'start', and
1464 * return it. tree->lock must be held. NULL will returned if
1465 * nothing was found after 'start'
1466 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001467static struct extent_state *
1468find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +01001469 u64 start, unsigned bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001470{
1471 struct rb_node *node;
1472 struct extent_state *state;
1473
1474 /*
1475 * this search will find all the extents that end after
1476 * our range starts.
1477 */
1478 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001479 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001480 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001481
Chris Masond3977122009-01-05 21:25:51 -05001482 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001483 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001484 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001485 return state;
Chris Masond3977122009-01-05 21:25:51 -05001486
Chris Masond7fc6402008-02-18 12:12:38 -05001487 node = rb_next(node);
1488 if (!node)
1489 break;
1490 }
1491out:
1492 return NULL;
1493}
Chris Masond7fc6402008-02-18 12:12:38 -05001494
Chris Masond352ac62008-09-29 15:18:18 -04001495/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001496 * find the first offset in the io tree with 'bits' set. zero is
1497 * returned if we find something, and *start_ret and *end_ret are
1498 * set to reflect the state struct that was found.
1499 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001500 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001501 */
1502int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba9ee49a042015-01-14 19:52:13 +01001503 u64 *start_ret, u64 *end_ret, unsigned bits,
Josef Bacike6138872012-09-27 17:07:30 -04001504 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001505{
1506 struct extent_state *state;
1507 int ret = 1;
1508
1509 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001510 if (cached_state && *cached_state) {
1511 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001512 if (state->end == start - 1 && extent_state_in_tree(state)) {
Liu Bo9688e9a2018-08-23 03:14:53 +08001513 while ((state = next_state(state)) != NULL) {
Josef Bacike6138872012-09-27 17:07:30 -04001514 if (state->state & bits)
1515 goto got_it;
Josef Bacike6138872012-09-27 17:07:30 -04001516 }
1517 free_extent_state(*cached_state);
1518 *cached_state = NULL;
1519 goto out;
1520 }
1521 free_extent_state(*cached_state);
1522 *cached_state = NULL;
1523 }
1524
Xiao Guangrong69261c42011-07-14 03:19:45 +00001525 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001526got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001527 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001528 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001529 *start_ret = state->start;
1530 *end_ret = state->end;
1531 ret = 0;
1532 }
Josef Bacike6138872012-09-27 17:07:30 -04001533out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001534 spin_unlock(&tree->lock);
1535 return ret;
1536}
1537
1538/*
Chris Masond352ac62008-09-29 15:18:18 -04001539 * find a contiguous range of bytes in the file marked as delalloc, not
1540 * more than 'max_bytes'. start and end are used to return the range,
1541 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001542 * true is returned if we find something, false if nothing was in the tree
Chris Masond352ac62008-09-29 15:18:18 -04001543 */
Lu Fengqi3522e902018-11-29 11:33:38 +08001544static noinline bool find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001545 u64 *start, u64 *end, u64 max_bytes,
1546 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001547{
1548 struct rb_node *node;
1549 struct extent_state *state;
1550 u64 cur_start = *start;
Lu Fengqi3522e902018-11-29 11:33:38 +08001551 bool found = false;
Chris Masond1310b22008-01-24 16:13:08 -05001552 u64 total_bytes = 0;
1553
Chris Masoncad321a2008-12-17 14:51:42 -05001554 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001555
Chris Masond1310b22008-01-24 16:13:08 -05001556 /*
1557 * this search will find all the extents that end after
1558 * our range starts.
1559 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001560 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001561 if (!node) {
Lu Fengqi3522e902018-11-29 11:33:38 +08001562 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001563 goto out;
1564 }
1565
Chris Masond3977122009-01-05 21:25:51 -05001566 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001567 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001568 if (found && (state->start != cur_start ||
1569 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001570 goto out;
1571 }
1572 if (!(state->state & EXTENT_DELALLOC)) {
1573 if (!found)
1574 *end = state->end;
1575 goto out;
1576 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001577 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001578 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001579 *cached_state = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +02001580 refcount_inc(&state->refs);
Josef Bacikc2a128d2010-02-02 21:19:11 +00001581 }
Lu Fengqi3522e902018-11-29 11:33:38 +08001582 found = true;
Chris Masond1310b22008-01-24 16:13:08 -05001583 *end = state->end;
1584 cur_start = state->end + 1;
1585 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001586 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001587 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001588 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001589 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001590 break;
1591 }
1592out:
Chris Masoncad321a2008-12-17 14:51:42 -05001593 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001594 return found;
1595}
1596
Liu Boda2c7002017-02-10 16:41:05 +01001597static int __process_pages_contig(struct address_space *mapping,
1598 struct page *locked_page,
1599 pgoff_t start_index, pgoff_t end_index,
1600 unsigned long page_ops, pgoff_t *index_ret);
1601
Jeff Mahoney143bede2012-03-01 14:56:26 +01001602static noinline void __unlock_for_delalloc(struct inode *inode,
1603 struct page *locked_page,
1604 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001605{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001606 unsigned long index = start >> PAGE_SHIFT;
1607 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001608
Liu Bo76c00212017-02-10 16:42:14 +01001609 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001610 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001611 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001612
Liu Bo76c00212017-02-10 16:42:14 +01001613 __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1614 PAGE_UNLOCK, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -04001615}
1616
1617static noinline int lock_delalloc_pages(struct inode *inode,
1618 struct page *locked_page,
1619 u64 delalloc_start,
1620 u64 delalloc_end)
1621{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001622 unsigned long index = delalloc_start >> PAGE_SHIFT;
Liu Bo76c00212017-02-10 16:42:14 +01001623 unsigned long index_ret = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001624 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001625 int ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001626
Liu Bo76c00212017-02-10 16:42:14 +01001627 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001628 if (index == locked_page->index && index == end_index)
1629 return 0;
1630
Liu Bo76c00212017-02-10 16:42:14 +01001631 ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1632 end_index, PAGE_LOCK, &index_ret);
1633 if (ret == -EAGAIN)
1634 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1635 (u64)index_ret << PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001636 return ret;
1637}
1638
1639/*
Lu Fengqi3522e902018-11-29 11:33:38 +08001640 * Find and lock a contiguous range of bytes in the file marked as delalloc, no
1641 * more than @max_bytes. @Start and @end are used to return the range,
Chris Masonc8b97812008-10-29 14:49:59 -04001642 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001643 * Return: true if we find something
1644 * false if nothing was in the tree
Chris Masonc8b97812008-10-29 14:49:59 -04001645 */
Johannes Thumshirnce9f9672018-11-19 10:38:17 +01001646EXPORT_FOR_TESTS
Lu Fengqi3522e902018-11-29 11:33:38 +08001647noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
Josef Bacik294e30f2013-10-09 12:00:56 -04001648 struct extent_io_tree *tree,
1649 struct page *locked_page, u64 *start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03001650 u64 *end)
Chris Masonc8b97812008-10-29 14:49:59 -04001651{
Nikolay Borisov917aace2018-10-26 14:43:20 +03001652 u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001653 u64 delalloc_start;
1654 u64 delalloc_end;
Lu Fengqi3522e902018-11-29 11:33:38 +08001655 bool found;
Chris Mason9655d292009-09-02 15:22:30 -04001656 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001657 int ret;
1658 int loops = 0;
1659
1660again:
1661 /* step one, find a bunch of delalloc bytes starting at start */
1662 delalloc_start = *start;
1663 delalloc_end = 0;
1664 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001665 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001666 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001667 *start = delalloc_start;
1668 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001669 free_extent_state(cached_state);
Lu Fengqi3522e902018-11-29 11:33:38 +08001670 return false;
Chris Masonc8b97812008-10-29 14:49:59 -04001671 }
1672
1673 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001674 * start comes from the offset of locked_page. We have to lock
1675 * pages in order, so we can't process delalloc bytes before
1676 * locked_page
1677 */
Chris Masond3977122009-01-05 21:25:51 -05001678 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001679 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001680
1681 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001682 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001683 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001684 if (delalloc_end + 1 - delalloc_start > max_bytes)
1685 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001686
Chris Masonc8b97812008-10-29 14:49:59 -04001687 /* step two, lock all the pages after the page that has start */
1688 ret = lock_delalloc_pages(inode, locked_page,
1689 delalloc_start, delalloc_end);
Nikolay Borisov9bfd61d2018-10-26 14:43:21 +03001690 ASSERT(!ret || ret == -EAGAIN);
Chris Masonc8b97812008-10-29 14:49:59 -04001691 if (ret == -EAGAIN) {
1692 /* some of the pages are gone, lets avoid looping by
1693 * shortening the size of the delalloc range we're searching
1694 */
Chris Mason9655d292009-09-02 15:22:30 -04001695 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001696 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001697 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001698 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001699 loops = 1;
1700 goto again;
1701 } else {
Lu Fengqi3522e902018-11-29 11:33:38 +08001702 found = false;
Chris Masonc8b97812008-10-29 14:49:59 -04001703 goto out_failed;
1704 }
1705 }
Chris Masonc8b97812008-10-29 14:49:59 -04001706
1707 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001708 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001709
1710 /* then test to make sure it is all still delalloc */
1711 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001712 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001713 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001714 unlock_extent_cached(tree, delalloc_start, delalloc_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001715 &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001716 __unlock_for_delalloc(inode, locked_page,
1717 delalloc_start, delalloc_end);
1718 cond_resched();
1719 goto again;
1720 }
Chris Mason9655d292009-09-02 15:22:30 -04001721 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001722 *start = delalloc_start;
1723 *end = delalloc_end;
1724out_failed:
1725 return found;
1726}
1727
Liu Boda2c7002017-02-10 16:41:05 +01001728static int __process_pages_contig(struct address_space *mapping,
1729 struct page *locked_page,
1730 pgoff_t start_index, pgoff_t end_index,
1731 unsigned long page_ops, pgoff_t *index_ret)
Chris Masonc8b97812008-10-29 14:49:59 -04001732{
Liu Bo873695b2017-02-02 17:49:22 -08001733 unsigned long nr_pages = end_index - start_index + 1;
Liu Boda2c7002017-02-10 16:41:05 +01001734 unsigned long pages_locked = 0;
Liu Bo873695b2017-02-02 17:49:22 -08001735 pgoff_t index = start_index;
Chris Masonc8b97812008-10-29 14:49:59 -04001736 struct page *pages[16];
Liu Bo873695b2017-02-02 17:49:22 -08001737 unsigned ret;
Liu Boda2c7002017-02-10 16:41:05 +01001738 int err = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001739 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001740
Liu Boda2c7002017-02-10 16:41:05 +01001741 if (page_ops & PAGE_LOCK) {
1742 ASSERT(page_ops == PAGE_LOCK);
1743 ASSERT(index_ret && *index_ret == start_index);
1744 }
1745
Filipe Manana704de492014-10-06 22:14:22 +01001746 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
Liu Bo873695b2017-02-02 17:49:22 -08001747 mapping_set_error(mapping, -EIO);
Filipe Manana704de492014-10-06 22:14:22 +01001748
Chris Masond3977122009-01-05 21:25:51 -05001749 while (nr_pages > 0) {
Liu Bo873695b2017-02-02 17:49:22 -08001750 ret = find_get_pages_contig(mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001751 min_t(unsigned long,
1752 nr_pages, ARRAY_SIZE(pages)), pages);
Liu Boda2c7002017-02-10 16:41:05 +01001753 if (ret == 0) {
1754 /*
1755 * Only if we're going to lock these pages,
1756 * can we find nothing at @index.
1757 */
1758 ASSERT(page_ops & PAGE_LOCK);
Liu Bo49d4a332017-03-06 18:20:56 -08001759 err = -EAGAIN;
1760 goto out;
Liu Boda2c7002017-02-10 16:41:05 +01001761 }
Chris Mason8b62b722009-09-02 16:53:46 -04001762
Liu Boda2c7002017-02-10 16:41:05 +01001763 for (i = 0; i < ret; i++) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001764 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001765 SetPagePrivate2(pages[i]);
1766
Chris Masonc8b97812008-10-29 14:49:59 -04001767 if (pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001768 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001769 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001770 continue;
1771 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001772 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001773 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001774 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001775 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001776 if (page_ops & PAGE_SET_ERROR)
1777 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001778 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001779 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001780 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001781 unlock_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001782 if (page_ops & PAGE_LOCK) {
1783 lock_page(pages[i]);
1784 if (!PageDirty(pages[i]) ||
1785 pages[i]->mapping != mapping) {
1786 unlock_page(pages[i]);
1787 put_page(pages[i]);
1788 err = -EAGAIN;
1789 goto out;
1790 }
1791 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001792 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001793 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001794 }
1795 nr_pages -= ret;
1796 index += ret;
1797 cond_resched();
1798 }
Liu Boda2c7002017-02-10 16:41:05 +01001799out:
1800 if (err && index_ret)
1801 *index_ret = start_index + pages_locked - 1;
1802 return err;
Chris Masonc8b97812008-10-29 14:49:59 -04001803}
Chris Masonc8b97812008-10-29 14:49:59 -04001804
Liu Bo873695b2017-02-02 17:49:22 -08001805void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1806 u64 delalloc_end, struct page *locked_page,
1807 unsigned clear_bits,
1808 unsigned long page_ops)
1809{
1810 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001811 NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001812
1813 __process_pages_contig(inode->i_mapping, locked_page,
1814 start >> PAGE_SHIFT, end >> PAGE_SHIFT,
Liu Boda2c7002017-02-10 16:41:05 +01001815 page_ops, NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001816}
1817
Chris Masond352ac62008-09-29 15:18:18 -04001818/*
1819 * count the number of bytes in the tree that have a given bit(s)
1820 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1821 * cached. The total number found is returned.
1822 */
Chris Masond1310b22008-01-24 16:13:08 -05001823u64 count_range_bits(struct extent_io_tree *tree,
1824 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001825 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001826{
1827 struct rb_node *node;
1828 struct extent_state *state;
1829 u64 cur_start = *start;
1830 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001831 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001832 int found = 0;
1833
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301834 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001835 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001836
Chris Masoncad321a2008-12-17 14:51:42 -05001837 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001838 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1839 total_bytes = tree->dirty_bytes;
1840 goto out;
1841 }
1842 /*
1843 * this search will find all the extents that end after
1844 * our range starts.
1845 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001846 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001847 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001848 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001849
Chris Masond3977122009-01-05 21:25:51 -05001850 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001851 state = rb_entry(node, struct extent_state, rb_node);
1852 if (state->start > search_end)
1853 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001854 if (contig && found && state->start > last + 1)
1855 break;
1856 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001857 total_bytes += min(search_end, state->end) + 1 -
1858 max(cur_start, state->start);
1859 if (total_bytes >= max_bytes)
1860 break;
1861 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001862 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001863 found = 1;
1864 }
Chris Masonec29ed52011-02-23 16:23:20 -05001865 last = state->end;
1866 } else if (contig && found) {
1867 break;
Chris Masond1310b22008-01-24 16:13:08 -05001868 }
1869 node = rb_next(node);
1870 if (!node)
1871 break;
1872 }
1873out:
Chris Masoncad321a2008-12-17 14:51:42 -05001874 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001875 return total_bytes;
1876}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001877
Chris Masond352ac62008-09-29 15:18:18 -04001878/*
1879 * set the private field for a given byte offset in the tree. If there isn't
1880 * an extent_state there already, this does nothing.
1881 */
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001882static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001883 struct io_failure_record *failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001884{
1885 struct rb_node *node;
1886 struct extent_state *state;
1887 int ret = 0;
1888
Chris Masoncad321a2008-12-17 14:51:42 -05001889 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001890 /*
1891 * this search will find all the extents that end after
1892 * our range starts.
1893 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001894 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001895 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001896 ret = -ENOENT;
1897 goto out;
1898 }
1899 state = rb_entry(node, struct extent_state, rb_node);
1900 if (state->start != start) {
1901 ret = -ENOENT;
1902 goto out;
1903 }
David Sterba47dc1962016-02-11 13:24:13 +01001904 state->failrec = failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001905out:
Chris Masoncad321a2008-12-17 14:51:42 -05001906 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001907 return ret;
1908}
1909
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001910static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001911 struct io_failure_record **failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001912{
1913 struct rb_node *node;
1914 struct extent_state *state;
1915 int ret = 0;
1916
Chris Masoncad321a2008-12-17 14:51:42 -05001917 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001918 /*
1919 * this search will find all the extents that end after
1920 * our range starts.
1921 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001922 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001923 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001924 ret = -ENOENT;
1925 goto out;
1926 }
1927 state = rb_entry(node, struct extent_state, rb_node);
1928 if (state->start != start) {
1929 ret = -ENOENT;
1930 goto out;
1931 }
David Sterba47dc1962016-02-11 13:24:13 +01001932 *failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001933out:
Chris Masoncad321a2008-12-17 14:51:42 -05001934 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001935 return ret;
1936}
1937
1938/*
1939 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001940 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001941 * has the bits set. Otherwise, 1 is returned if any bit in the
1942 * range is found set.
1943 */
1944int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001945 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001946{
1947 struct extent_state *state = NULL;
1948 struct rb_node *node;
1949 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001950
Chris Masoncad321a2008-12-17 14:51:42 -05001951 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01001952 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001953 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001954 node = &cached->rb_node;
1955 else
1956 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001957 while (node && start <= end) {
1958 state = rb_entry(node, struct extent_state, rb_node);
1959
1960 if (filled && state->start > start) {
1961 bitset = 0;
1962 break;
1963 }
1964
1965 if (state->start > end)
1966 break;
1967
1968 if (state->state & bits) {
1969 bitset = 1;
1970 if (!filled)
1971 break;
1972 } else if (filled) {
1973 bitset = 0;
1974 break;
1975 }
Chris Mason46562ce2009-09-23 20:23:16 -04001976
1977 if (state->end == (u64)-1)
1978 break;
1979
Chris Masond1310b22008-01-24 16:13:08 -05001980 start = state->end + 1;
1981 if (start > end)
1982 break;
1983 node = rb_next(node);
1984 if (!node) {
1985 if (filled)
1986 bitset = 0;
1987 break;
1988 }
1989 }
Chris Masoncad321a2008-12-17 14:51:42 -05001990 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001991 return bitset;
1992}
Chris Masond1310b22008-01-24 16:13:08 -05001993
1994/*
1995 * helper function to set a given page up to date if all the
1996 * extents in the tree for that page are up to date
1997 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001998static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001999{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002000 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002001 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04002002 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05002003 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002004}
2005
Josef Bacik7870d082017-05-05 11:57:15 -04002006int free_io_failure(struct extent_io_tree *failure_tree,
2007 struct extent_io_tree *io_tree,
2008 struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002009{
2010 int ret;
2011 int err = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002012
David Sterba47dc1962016-02-11 13:24:13 +01002013 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002014 ret = clear_extent_bits(failure_tree, rec->start,
2015 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02002016 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002017 if (ret)
2018 err = ret;
2019
Josef Bacik7870d082017-05-05 11:57:15 -04002020 ret = clear_extent_bits(io_tree, rec->start,
David Woodhouse53b381b2013-01-29 18:40:14 -05002021 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02002022 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05002023 if (ret && !err)
2024 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002025
2026 kfree(rec);
2027 return err;
2028}
2029
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002030/*
2031 * this bypasses the standard btrfs submit functions deliberately, as
2032 * the standard behavior is to write all copies in a raid setup. here we only
2033 * want to write the one bad copy. so we do the mapping for ourselves and issue
2034 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002035 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002036 * actually prevents the read that triggered the error from finishing.
2037 * currently, there can be no more than two copies of every data bit. thus,
2038 * exactly one rewrite is required.
2039 */
Josef Bacik6ec656b2017-05-05 11:57:14 -04002040int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
2041 u64 length, u64 logical, struct page *page,
2042 unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002043{
2044 struct bio *bio;
2045 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002046 u64 map_length = 0;
2047 u64 sector;
2048 struct btrfs_bio *bbio = NULL;
2049 int ret;
2050
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002051 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002052 BUG_ON(!mirror_num);
2053
David Sterbac5e4c3d2017-06-12 17:29:41 +02002054 bio = btrfs_io_bio_alloc(1);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002055 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002056 map_length = length;
2057
Filipe Mananab5de8d02016-05-27 22:21:27 +01002058 /*
2059 * Avoid races with device replace and make sure our bbio has devices
2060 * associated to its stripes that don't go away while we are doing the
2061 * read repair operation.
2062 */
2063 btrfs_bio_counter_inc_blocked(fs_info);
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03002064 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
Liu Boc7253282017-03-29 10:53:58 -07002065 /*
2066 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
2067 * to update all raid stripes, but here we just want to correct
2068 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2069 * stripe's dev and sector.
2070 */
2071 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2072 &map_length, &bbio, 0);
2073 if (ret) {
2074 btrfs_bio_counter_dec(fs_info);
2075 bio_put(bio);
2076 return -EIO;
2077 }
2078 ASSERT(bbio->mirror_num == 1);
2079 } else {
2080 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2081 &map_length, &bbio, mirror_num);
2082 if (ret) {
2083 btrfs_bio_counter_dec(fs_info);
2084 bio_put(bio);
2085 return -EIO;
2086 }
2087 BUG_ON(mirror_num != bbio->mirror_num);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002088 }
Liu Boc7253282017-03-29 10:53:58 -07002089
2090 sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002091 bio->bi_iter.bi_sector = sector;
Liu Boc7253282017-03-29 10:53:58 -07002092 dev = bbio->stripes[bbio->mirror_num - 1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002093 btrfs_put_bbio(bbio);
Anand Jainebbede42017-12-04 12:54:52 +08002094 if (!dev || !dev->bdev ||
2095 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002096 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002097 bio_put(bio);
2098 return -EIO;
2099 }
Christoph Hellwig74d46992017-08-23 19:10:32 +02002100 bio_set_dev(bio, dev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002101 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Miao Xieffdd2012014-09-12 18:44:00 +08002102 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002103
Mike Christie4e49ea42016-06-05 14:31:41 -05002104 if (btrfsic_submit_bio_wait(bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002105 /* try to remap that extent elsewhere? */
Filipe Mananab5de8d02016-05-27 22:21:27 +01002106 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002107 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002108 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002109 return -EIO;
2110 }
2111
David Sterbab14af3b2015-10-08 10:43:10 +02002112 btrfs_info_rl_in_rcu(fs_info,
2113 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Josef Bacik6ec656b2017-05-05 11:57:14 -04002114 ino, start,
Miao Xie1203b682014-09-12 18:44:01 +08002115 rcu_str_deref(dev->name), sector);
Filipe Mananab5de8d02016-05-27 22:21:27 +01002116 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002117 bio_put(bio);
2118 return 0;
2119}
2120
David Sterba20a1fbf92019-03-20 11:23:44 +01002121int btrfs_repair_eb_io_failure(struct extent_buffer *eb, int mirror_num)
Josef Bacikea466792012-03-26 21:57:36 -04002122{
David Sterba20a1fbf92019-03-20 11:23:44 +01002123 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacikea466792012-03-26 21:57:36 -04002124 u64 start = eb->start;
David Sterbacc5e31a2018-03-01 18:20:27 +01002125 int i, num_pages = num_extent_pages(eb);
Chris Masond95603b2012-04-12 15:55:15 -04002126 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002127
David Howellsbc98a422017-07-17 08:45:34 +01002128 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002129 return -EROFS;
2130
Josef Bacikea466792012-03-26 21:57:36 -04002131 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002132 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002133
Josef Bacik6ec656b2017-05-05 11:57:14 -04002134 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002135 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002136 if (ret)
2137 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002138 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002139 }
2140
2141 return ret;
2142}
2143
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002144/*
2145 * each time an IO finishes, we do a fast check in the IO failure tree
2146 * to see if we need to process or clean up an io_failure_record
2147 */
Josef Bacik7870d082017-05-05 11:57:15 -04002148int clean_io_failure(struct btrfs_fs_info *fs_info,
2149 struct extent_io_tree *failure_tree,
2150 struct extent_io_tree *io_tree, u64 start,
2151 struct page *page, u64 ino, unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002152{
2153 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002154 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002155 struct extent_state *state;
2156 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002157 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002158
2159 private = 0;
Josef Bacik7870d082017-05-05 11:57:15 -04002160 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2161 EXTENT_DIRTY, 0);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002162 if (!ret)
2163 return 0;
2164
Josef Bacik7870d082017-05-05 11:57:15 -04002165 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002166 if (ret)
2167 return 0;
2168
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002169 BUG_ON(!failrec->this_mirror);
2170
2171 if (failrec->in_validation) {
2172 /* there was no real error, just free the record */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002173 btrfs_debug(fs_info,
2174 "clean_io_failure: freeing dummy error at %llu",
2175 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002176 goto out;
2177 }
David Howellsbc98a422017-07-17 08:45:34 +01002178 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002179 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002180
Josef Bacik7870d082017-05-05 11:57:15 -04002181 spin_lock(&io_tree->lock);
2182 state = find_first_extent_bit_state(io_tree,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002183 failrec->start,
2184 EXTENT_LOCKED);
Josef Bacik7870d082017-05-05 11:57:15 -04002185 spin_unlock(&io_tree->lock);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002186
Miao Xie883d0de2013-07-25 19:22:35 +08002187 if (state && state->start <= failrec->start &&
2188 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002189 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2190 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002191 if (num_copies > 1) {
Josef Bacik7870d082017-05-05 11:57:15 -04002192 repair_io_failure(fs_info, ino, start, failrec->len,
2193 failrec->logical, page, pg_offset,
2194 failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002195 }
2196 }
2197
2198out:
Josef Bacik7870d082017-05-05 11:57:15 -04002199 free_io_failure(failure_tree, io_tree, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002200
Miao Xie454ff3d2014-09-12 18:43:58 +08002201 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002202}
2203
Miao Xief6124962014-09-12 18:44:04 +08002204/*
2205 * Can be called when
2206 * - hold extent lock
2207 * - under ordered extent
2208 * - the inode is freeing
2209 */
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002210void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
Miao Xief6124962014-09-12 18:44:04 +08002211{
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002212 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
Miao Xief6124962014-09-12 18:44:04 +08002213 struct io_failure_record *failrec;
2214 struct extent_state *state, *next;
2215
2216 if (RB_EMPTY_ROOT(&failure_tree->state))
2217 return;
2218
2219 spin_lock(&failure_tree->lock);
2220 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2221 while (state) {
2222 if (state->start > end)
2223 break;
2224
2225 ASSERT(state->end <= end);
2226
2227 next = next_state(state);
2228
David Sterba47dc1962016-02-11 13:24:13 +01002229 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002230 free_extent_state(state);
2231 kfree(failrec);
2232
2233 state = next;
2234 }
2235 spin_unlock(&failure_tree->lock);
2236}
2237
Miao Xie2fe63032014-09-12 18:43:59 +08002238int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
David Sterba47dc1962016-02-11 13:24:13 +01002239 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002240{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002241 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002242 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002243 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002244 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2245 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2246 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002247 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002248 u64 logical;
2249
David Sterba47dc1962016-02-11 13:24:13 +01002250 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002251 if (ret) {
2252 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2253 if (!failrec)
2254 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002255
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002256 failrec->start = start;
2257 failrec->len = end - start + 1;
2258 failrec->this_mirror = 0;
2259 failrec->bio_flags = 0;
2260 failrec->in_validation = 0;
2261
2262 read_lock(&em_tree->lock);
2263 em = lookup_extent_mapping(em_tree, start, failrec->len);
2264 if (!em) {
2265 read_unlock(&em_tree->lock);
2266 kfree(failrec);
2267 return -EIO;
2268 }
2269
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002270 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002271 free_extent_map(em);
2272 em = NULL;
2273 }
2274 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002275 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002276 kfree(failrec);
2277 return -EIO;
2278 }
Miao Xie2fe63032014-09-12 18:43:59 +08002279
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002280 logical = start - em->start;
2281 logical = em->block_start + logical;
2282 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2283 logical = em->block_start;
2284 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2285 extent_set_compress_type(&failrec->bio_flags,
2286 em->compress_type);
2287 }
Miao Xie2fe63032014-09-12 18:43:59 +08002288
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002289 btrfs_debug(fs_info,
2290 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2291 logical, start, failrec->len);
Miao Xie2fe63032014-09-12 18:43:59 +08002292
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002293 failrec->logical = logical;
2294 free_extent_map(em);
2295
2296 /* set the bits in the private failure tree */
2297 ret = set_extent_bits(failure_tree, start, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002298 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002299 if (ret >= 0)
David Sterba47dc1962016-02-11 13:24:13 +01002300 ret = set_state_failrec(failure_tree, start, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002301 /* set the bits in the inode's tree */
2302 if (ret >= 0)
David Sterbaceeb0ae2016-04-26 23:54:39 +02002303 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002304 if (ret < 0) {
2305 kfree(failrec);
2306 return ret;
2307 }
2308 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002309 btrfs_debug(fs_info,
2310 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2311 failrec->logical, failrec->start, failrec->len,
2312 failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002313 /*
2314 * when data can be on disk more than twice, add to failrec here
2315 * (e.g. with a list for failed_mirror) to make
2316 * clean_io_failure() clean all those errors at once.
2317 */
2318 }
Miao Xie2fe63032014-09-12 18:43:59 +08002319
2320 *failrec_ret = failrec;
2321
2322 return 0;
2323}
2324
Ming Leia0b60d72017-12-18 20:22:11 +08002325bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages,
Miao Xie2fe63032014-09-12 18:43:59 +08002326 struct io_failure_record *failrec, int failed_mirror)
2327{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002328 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002329 int num_copies;
2330
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002331 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002332 if (num_copies == 1) {
2333 /*
2334 * we only have a single copy of the data, so don't bother with
2335 * all the retry and error correction code that follows. no
2336 * matter what the error is, it is very likely to persist.
2337 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002338 btrfs_debug(fs_info,
2339 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2340 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002341 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002342 }
2343
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002344 /*
2345 * there are two premises:
2346 * a) deliver good data to the caller
2347 * b) correct the bad sectors on disk
2348 */
Ming Leia0b60d72017-12-18 20:22:11 +08002349 if (failed_bio_pages > 1) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002350 /*
2351 * to fulfill b), we need to know the exact failing sectors, as
2352 * we don't want to rewrite any more than the failed ones. thus,
2353 * we need separate read requests for the failed bio
2354 *
2355 * if the following BUG_ON triggers, our validation request got
2356 * merged. we need separate requests for our algorithm to work.
2357 */
2358 BUG_ON(failrec->in_validation);
2359 failrec->in_validation = 1;
2360 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002361 } else {
2362 /*
2363 * we're ready to fulfill a) and b) alongside. get a good copy
2364 * of the failed sector and if we succeed, we have setup
2365 * everything for repair_io_failure to do the rest for us.
2366 */
2367 if (failrec->in_validation) {
2368 BUG_ON(failrec->this_mirror != failed_mirror);
2369 failrec->in_validation = 0;
2370 failrec->this_mirror = 0;
2371 }
2372 failrec->failed_mirror = failed_mirror;
2373 failrec->this_mirror++;
2374 if (failrec->this_mirror == failed_mirror)
2375 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002376 }
2377
Miao Xiefacc8a222013-07-25 19:22:34 +08002378 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002379 btrfs_debug(fs_info,
2380 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2381 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002382 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002383 }
2384
Liu Boc3cfb652017-07-13 15:00:50 -07002385 return true;
Miao Xie2fe63032014-09-12 18:43:59 +08002386}
2387
2388
2389struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2390 struct io_failure_record *failrec,
2391 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002392 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002393{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002394 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002395 struct bio *bio;
2396 struct btrfs_io_bio *btrfs_failed_bio;
2397 struct btrfs_io_bio *btrfs_bio;
2398
David Sterbac5e4c3d2017-06-12 17:29:41 +02002399 bio = btrfs_io_bio_alloc(1);
Miao Xie2fe63032014-09-12 18:43:59 +08002400 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002401 bio->bi_iter.bi_sector = failrec->logical >> 9;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002402 bio_set_dev(bio, fs_info->fs_devices->latest_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002403 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002404 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002405
Miao Xiefacc8a222013-07-25 19:22:34 +08002406 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2407 if (btrfs_failed_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002408 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2409
2410 btrfs_bio = btrfs_io_bio(bio);
2411 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002412 icsum *= csum_size;
2413 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002414 csum_size);
2415 }
2416
Miao Xie2fe63032014-09-12 18:43:59 +08002417 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002418
Miao Xie2fe63032014-09-12 18:43:59 +08002419 return bio;
2420}
2421
2422/*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002423 * This is a generic handler for readpage errors. If other copies exist, read
2424 * those and write back good data to the failed position. Does not investigate
2425 * in remapping the failed extent elsewhere, hoping the device will be smart
2426 * enough to do this as needed
Miao Xie2fe63032014-09-12 18:43:59 +08002427 */
Miao Xie2fe63032014-09-12 18:43:59 +08002428static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2429 struct page *page, u64 start, u64 end,
2430 int failed_mirror)
2431{
2432 struct io_failure_record *failrec;
2433 struct inode *inode = page->mapping->host;
2434 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002435 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie2fe63032014-09-12 18:43:59 +08002436 struct bio *bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002437 int read_mode = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002438 blk_status_t status;
Miao Xie2fe63032014-09-12 18:43:59 +08002439 int ret;
Christoph Hellwig8a2ee442019-02-15 19:13:07 +08002440 unsigned failed_bio_pages = failed_bio->bi_iter.bi_size >> PAGE_SHIFT;
Miao Xie2fe63032014-09-12 18:43:59 +08002441
Mike Christie1f7ad752016-06-05 14:31:51 -05002442 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002443
2444 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2445 if (ret)
2446 return ret;
2447
Ming Leia0b60d72017-12-18 20:22:11 +08002448 if (!btrfs_check_repairable(inode, failed_bio_pages, failrec,
Liu Boc3cfb652017-07-13 15:00:50 -07002449 failed_mirror)) {
Josef Bacik7870d082017-05-05 11:57:15 -04002450 free_io_failure(failure_tree, tree, failrec);
Miao Xie2fe63032014-09-12 18:43:59 +08002451 return -EIO;
2452 }
2453
Ming Leia0b60d72017-12-18 20:22:11 +08002454 if (failed_bio_pages > 1)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002455 read_mode |= REQ_FAILFAST_DEV;
Miao Xie2fe63032014-09-12 18:43:59 +08002456
2457 phy_offset >>= inode->i_sb->s_blocksize_bits;
2458 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2459 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002460 (int)phy_offset, failed_bio->bi_end_io,
2461 NULL);
David Sterbaebcc3262018-06-29 10:56:53 +02002462 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie2fe63032014-09-12 18:43:59 +08002463
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002464 btrfs_debug(btrfs_sb(inode->i_sb),
2465 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2466 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002467
Linus Torvalds8c27cb32017-07-05 16:41:23 -07002468 status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002469 failrec->bio_flags, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002470 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04002471 free_io_failure(failure_tree, tree, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002472 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002473 ret = blk_status_to_errno(status);
Miao Xie6c387ab2014-09-12 18:43:57 +08002474 }
2475
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002476 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002477}
2478
Chris Masond1310b22008-01-24 16:13:08 -05002479/* lots and lots of room for performance fixes in the end_bio funcs */
2480
David Sterbab5227c02015-12-03 13:08:59 +01002481void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002482{
2483 int uptodate = (err == 0);
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002484 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002485
Nikolay Borisovc6297322018-11-08 10:18:08 +02002486 btrfs_writepage_endio_finish_ordered(page, start, end, uptodate);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002487
Jeff Mahoney87826df2012-02-15 16:23:57 +01002488 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002489 ClearPageUptodate(page);
2490 SetPageError(page);
Colin Ian Kingbff5baf2017-05-09 18:14:01 +01002491 ret = err < 0 ? err : -EIO;
Liu Bo5dca6ee2014-05-12 12:47:36 +08002492 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002493 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002494}
2495
Chris Masond1310b22008-01-24 16:13:08 -05002496/*
2497 * after a writepage IO is done, we need to:
2498 * clear the uptodate bits on error
2499 * clear the writeback bits in the extent tree for this IO
2500 * end_page_writeback if the page has no more pending IO
2501 *
2502 * Scheduling is not allowed, so the extent state tree is expected
2503 * to have one and only one object corresponding to this IO.
2504 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002505static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002506{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002507 int error = blk_status_to_errno(bio->bi_status);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002508 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002509 u64 start;
2510 u64 end;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002511 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08002512 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002513
David Sterbac09abff2017-07-13 18:10:07 +02002514 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08002515 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002516 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002517 struct inode *inode = page->mapping->host;
2518 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002519
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002520 /* We always issue full-page reads, but if some block
2521 * in a page fails to read, blk_update_request() will
2522 * advance bv_offset and adjust bv_len to compensate.
2523 * Print a warning for nonzero offsets, and an error
2524 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002525 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2526 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002527 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002528 "partial page write in btrfs with offset %u and length %u",
2529 bvec->bv_offset, bvec->bv_len);
2530 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002531 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002532 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002533 bvec->bv_offset, bvec->bv_len);
2534 }
Chris Masond1310b22008-01-24 16:13:08 -05002535
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002536 start = page_offset(page);
2537 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002538
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002539 end_extent_writepage(page, error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002540 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002541 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002542
Chris Masond1310b22008-01-24 16:13:08 -05002543 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002544}
2545
Miao Xie883d0de2013-07-25 19:22:35 +08002546static void
2547endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2548 int uptodate)
2549{
2550 struct extent_state *cached = NULL;
2551 u64 end = start + len - 1;
2552
2553 if (uptodate && tree->track_uptodate)
2554 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
David Sterbad810a4b2017-12-07 18:52:54 +01002555 unlock_extent_cached_atomic(tree, start, end, &cached);
Miao Xie883d0de2013-07-25 19:22:35 +08002556}
2557
Chris Masond1310b22008-01-24 16:13:08 -05002558/*
2559 * after a readpage IO is done, we need to:
2560 * clear the uptodate bits on error
2561 * set the uptodate bits if things worked
2562 * set the page up to date if all extents in the tree are uptodate
2563 * clear the lock bit in the extent tree
2564 * unlock the page if there are no other extents locked for it
2565 *
2566 * Scheduling is not allowed, so the extent state tree is expected
2567 * to have one and only one object corresponding to this IO.
2568 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002569static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002570{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002571 struct bio_vec *bvec;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002572 int uptodate = !bio->bi_status;
Miao Xiefacc8a222013-07-25 19:22:34 +08002573 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04002574 struct extent_io_tree *tree, *failure_tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002575 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002576 u64 start;
2577 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002578 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002579 u64 extent_start = 0;
2580 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002581 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002582 int ret;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002583 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08002584 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002585
David Sterbac09abff2017-07-13 18:10:07 +02002586 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08002587 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002588 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002589 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002590 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002591 bool data_inode = btrfs_ino(BTRFS_I(inode))
2592 != BTRFS_BTREE_INODE_OBJECTID;
Arne Jansen507903b2011-04-06 10:02:20 +00002593
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002594 btrfs_debug(fs_info,
2595 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002596 (u64)bio->bi_iter.bi_sector, bio->bi_status,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002597 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002598 tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002599 failure_tree = &BTRFS_I(inode)->io_failure_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002600
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002601 /* We always issue full-page reads, but if some block
2602 * in a page fails to read, blk_update_request() will
2603 * advance bv_offset and adjust bv_len to compensate.
2604 * Print a warning for nonzero offsets, and an error
2605 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002606 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2607 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002608 btrfs_err(fs_info,
2609 "partial page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002610 bvec->bv_offset, bvec->bv_len);
2611 else
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002612 btrfs_info(fs_info,
2613 "incomplete page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002614 bvec->bv_offset, bvec->bv_len);
2615 }
Chris Masond1310b22008-01-24 16:13:08 -05002616
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002617 start = page_offset(page);
2618 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002619 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002620
Chris Mason9be33952013-05-17 18:30:14 -04002621 mirror = io_bio->mirror_num;
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002622 if (likely(uptodate)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002623 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2624 page, start, end,
2625 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002626 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002627 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002628 else
Josef Bacik7870d082017-05-05 11:57:15 -04002629 clean_io_failure(BTRFS_I(inode)->root->fs_info,
2630 failure_tree, tree, start,
2631 page,
2632 btrfs_ino(BTRFS_I(inode)), 0);
Chris Masond1310b22008-01-24 16:13:08 -05002633 }
Josef Bacikea466792012-03-26 21:57:36 -04002634
Miao Xief2a09da2013-07-25 19:22:33 +08002635 if (likely(uptodate))
2636 goto readpage_ok;
2637
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002638 if (data_inode) {
Liu Bo9d0d1c82017-03-24 15:04:50 -07002639
2640 /*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002641 * The generic bio_readpage_error handles errors the
2642 * following way: If possible, new read requests are
2643 * created and submitted and will end up in
2644 * end_bio_extent_readpage as well (if we're lucky,
2645 * not in the !uptodate case). In that case it returns
2646 * 0 and we just go on with the next page in our bio.
2647 * If it can't handle the error it will return -EIO and
2648 * we remain responsible for that page.
Liu Bo9d0d1c82017-03-24 15:04:50 -07002649 */
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002650 ret = bio_readpage_error(bio, offset, page, start, end,
2651 mirror);
2652 if (ret == 0) {
2653 uptodate = !bio->bi_status;
2654 offset += len;
2655 continue;
2656 }
2657 } else {
2658 struct extent_buffer *eb;
2659
2660 eb = (struct extent_buffer *)page->private;
2661 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
2662 eb->read_mirror = mirror;
2663 atomic_dec(&eb->io_pages);
2664 if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD,
2665 &eb->bflags))
2666 btree_readahead_hook(eb, -EIO);
Chris Mason7e383262008-04-09 16:28:12 -04002667 }
Miao Xief2a09da2013-07-25 19:22:33 +08002668readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002669 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002670 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002671 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002672 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002673
2674 /* Zero out the end if this page straddles i_size */
Johannes Thumshirn70730172018-12-05 15:23:03 +01002675 off = offset_in_page(i_size);
Liu Boa583c022014-08-19 23:32:22 +08002676 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002677 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002678 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002679 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002680 ClearPageUptodate(page);
2681 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002682 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002683 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002684 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002685
2686 if (unlikely(!uptodate)) {
2687 if (extent_len) {
2688 endio_readpage_release_extent(tree,
2689 extent_start,
2690 extent_len, 1);
2691 extent_start = 0;
2692 extent_len = 0;
2693 }
2694 endio_readpage_release_extent(tree, start,
2695 end - start + 1, 0);
2696 } else if (!extent_len) {
2697 extent_start = start;
2698 extent_len = end + 1 - start;
2699 } else if (extent_start + extent_len == start) {
2700 extent_len += end + 1 - start;
2701 } else {
2702 endio_readpage_release_extent(tree, extent_start,
2703 extent_len, uptodate);
2704 extent_start = start;
2705 extent_len = end + 1 - start;
2706 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002707 }
Chris Masond1310b22008-01-24 16:13:08 -05002708
Miao Xie883d0de2013-07-25 19:22:35 +08002709 if (extent_len)
2710 endio_readpage_release_extent(tree, extent_start, extent_len,
2711 uptodate);
David Sterbab3a0dd52018-11-22 17:16:49 +01002712 btrfs_io_bio_free_csum(io_bio);
Chris Masond1310b22008-01-24 16:13:08 -05002713 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002714}
2715
Chris Mason9be33952013-05-17 18:30:14 -04002716/*
David Sterba184f9992017-06-12 17:29:39 +02002717 * Initialize the members up to but not including 'bio'. Use after allocating a
2718 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
2719 * 'bio' because use of __GFP_ZERO is not supported.
Chris Mason9be33952013-05-17 18:30:14 -04002720 */
David Sterba184f9992017-06-12 17:29:39 +02002721static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
Chris Masond1310b22008-01-24 16:13:08 -05002722{
David Sterba184f9992017-06-12 17:29:39 +02002723 memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
2724}
2725
2726/*
David Sterba6e707bc2017-06-02 17:26:26 +02002727 * The following helpers allocate a bio. As it's backed by a bioset, it'll
2728 * never fail. We're returning a bio right now but you can call btrfs_io_bio
2729 * for the appropriate container_of magic
Chris Masond1310b22008-01-24 16:13:08 -05002730 */
David Sterbac821e7f32017-06-02 18:35:36 +02002731struct bio *btrfs_bio_alloc(struct block_device *bdev, u64 first_byte)
Chris Masond1310b22008-01-24 16:13:08 -05002732{
2733 struct bio *bio;
2734
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002735 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset);
Christoph Hellwig74d46992017-08-23 19:10:32 +02002736 bio_set_dev(bio, bdev);
David Sterbac821e7f32017-06-02 18:35:36 +02002737 bio->bi_iter.bi_sector = first_byte >> 9;
David Sterba184f9992017-06-12 17:29:39 +02002738 btrfs_io_bio_init(btrfs_io_bio(bio));
Chris Masond1310b22008-01-24 16:13:08 -05002739 return bio;
2740}
2741
David Sterba8b6c1d52017-06-02 17:48:13 +02002742struct bio *btrfs_bio_clone(struct bio *bio)
Chris Mason9be33952013-05-17 18:30:14 -04002743{
Miao Xie23ea8e52014-09-12 18:43:54 +08002744 struct btrfs_io_bio *btrfs_bio;
2745 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002746
David Sterba6e707bc2017-06-02 17:26:26 +02002747 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002748 new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset);
David Sterba6e707bc2017-06-02 17:26:26 +02002749 btrfs_bio = btrfs_io_bio(new);
David Sterba184f9992017-06-12 17:29:39 +02002750 btrfs_io_bio_init(btrfs_bio);
David Sterba6e707bc2017-06-02 17:26:26 +02002751 btrfs_bio->iter = bio->bi_iter;
Miao Xie23ea8e52014-09-12 18:43:54 +08002752 return new;
2753}
Chris Mason9be33952013-05-17 18:30:14 -04002754
David Sterbac5e4c3d2017-06-12 17:29:41 +02002755struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
Chris Mason9be33952013-05-17 18:30:14 -04002756{
Miao Xiefacc8a222013-07-25 19:22:34 +08002757 struct bio *bio;
2758
David Sterba6e707bc2017-06-02 17:26:26 +02002759 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002760 bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
David Sterba184f9992017-06-12 17:29:39 +02002761 btrfs_io_bio_init(btrfs_io_bio(bio));
Miao Xiefacc8a222013-07-25 19:22:34 +08002762 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002763}
2764
Liu Boe4770942017-05-16 10:57:14 -07002765struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
Liu Bo2f8e9142017-05-15 17:43:31 -07002766{
2767 struct bio *bio;
2768 struct btrfs_io_bio *btrfs_bio;
2769
2770 /* this will never fail when it's backed by a bioset */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002771 bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset);
Liu Bo2f8e9142017-05-15 17:43:31 -07002772 ASSERT(bio);
2773
2774 btrfs_bio = btrfs_io_bio(bio);
David Sterba184f9992017-06-12 17:29:39 +02002775 btrfs_io_bio_init(btrfs_bio);
Liu Bo2f8e9142017-05-15 17:43:31 -07002776
2777 bio_trim(bio, offset >> 9, size >> 9);
Liu Bo17347ce2017-05-15 15:33:27 -07002778 btrfs_bio->iter = bio->bi_iter;
Liu Bo2f8e9142017-05-15 17:43:31 -07002779 return bio;
2780}
Chris Mason9be33952013-05-17 18:30:14 -04002781
David Sterba4b81ba42017-06-06 19:14:26 +02002782/*
2783 * @opf: bio REQ_OP_* and REQ_* flags as one value
David Sterbab8b3d622017-06-12 19:50:41 +02002784 * @tree: tree so we can call our merge_bio hook
2785 * @wbc: optional writeback control for io accounting
2786 * @page: page to add to the bio
2787 * @pg_offset: offset of the new bio or to check whether we are adding
2788 * a contiguous page to the previous one
2789 * @size: portion of page that we want to write
2790 * @offset: starting offset in the page
2791 * @bdev: attach newly created bios to this bdev
David Sterba5c2b1fd2017-06-06 19:22:55 +02002792 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
David Sterbab8b3d622017-06-12 19:50:41 +02002793 * @end_io_func: end_io callback for new bio
2794 * @mirror_num: desired mirror to read/write
2795 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
2796 * @bio_flags: flags of the current bio to see if we can merge them
David Sterba4b81ba42017-06-06 19:14:26 +02002797 */
2798static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002799 struct writeback_control *wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02002800 struct page *page, u64 offset,
David Sterba6c5a4e22017-10-04 17:10:34 +02002801 size_t size, unsigned long pg_offset,
Chris Masond1310b22008-01-24 16:13:08 -05002802 struct block_device *bdev,
2803 struct bio **bio_ret,
Chris Masonf1885912008-04-09 16:28:12 -04002804 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002805 int mirror_num,
2806 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002807 unsigned long bio_flags,
2808 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002809{
2810 int ret = 0;
2811 struct bio *bio;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002812 size_t page_size = min_t(size_t, size, PAGE_SIZE);
David Sterba6273b7f2017-10-04 17:30:11 +02002813 sector_t sector = offset >> 9;
Chris Masond1310b22008-01-24 16:13:08 -05002814
David Sterba5c2b1fd2017-06-06 19:22:55 +02002815 ASSERT(bio_ret);
2816
2817 if (*bio_ret) {
David Sterba0c8508a2017-06-12 20:00:43 +02002818 bool contig;
2819 bool can_merge = true;
2820
Chris Masond1310b22008-01-24 16:13:08 -05002821 bio = *bio_ret;
David Sterba0c8508a2017-06-12 20:00:43 +02002822 if (prev_bio_flags & EXTENT_BIO_COMPRESSED)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002823 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002824 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002825 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002826
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002827 ASSERT(tree->ops);
2828 if (btrfs_bio_fits_in_stripe(page, page_size, bio, bio_flags))
David Sterba0c8508a2017-06-12 20:00:43 +02002829 can_merge = false;
2830
2831 if (prev_bio_flags != bio_flags || !contig || !can_merge ||
Filipe Manana005efed2015-09-14 09:09:31 +01002832 force_bio_submit ||
David Sterba6c5a4e22017-10-04 17:10:34 +02002833 bio_add_page(bio, page, page_size, pg_offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002834 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002835 if (ret < 0) {
2836 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002837 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002838 }
Chris Masond1310b22008-01-24 16:13:08 -05002839 bio = NULL;
2840 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002841 if (wbc)
2842 wbc_account_io(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002843 return 0;
2844 }
2845 }
Chris Masonc8b97812008-10-29 14:49:59 -04002846
David Sterba6273b7f2017-10-04 17:30:11 +02002847 bio = btrfs_bio_alloc(bdev, offset);
David Sterba6c5a4e22017-10-04 17:10:34 +02002848 bio_add_page(bio, page, page_size, pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002849 bio->bi_end_io = end_io_func;
2850 bio->bi_private = tree;
Jens Axboee6959b92017-06-27 11:51:28 -06002851 bio->bi_write_hint = page->mapping->host->i_write_hint;
David Sterba4b81ba42017-06-06 19:14:26 +02002852 bio->bi_opf = opf;
Chris Masonda2f0f72015-07-02 13:57:22 -07002853 if (wbc) {
2854 wbc_init_bio(wbc, bio);
2855 wbc_account_io(wbc, page, page_size);
2856 }
Chris Mason70dec802008-01-29 09:59:12 -05002857
David Sterba5c2b1fd2017-06-06 19:22:55 +02002858 *bio_ret = bio;
Chris Masond1310b22008-01-24 16:13:08 -05002859
2860 return ret;
2861}
2862
Eric Sandeen48a3b632013-04-25 20:41:01 +00002863static void attach_extent_buffer_page(struct extent_buffer *eb,
2864 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002865{
2866 if (!PagePrivate(page)) {
2867 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002868 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002869 set_page_private(page, (unsigned long)eb);
2870 } else {
2871 WARN_ON(page->private != (unsigned long)eb);
2872 }
2873}
2874
Chris Masond1310b22008-01-24 16:13:08 -05002875void set_page_extent_mapped(struct page *page)
2876{
2877 if (!PagePrivate(page)) {
2878 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002879 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002880 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002881 }
2882}
2883
Miao Xie125bac012013-07-25 19:22:37 +08002884static struct extent_map *
2885__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2886 u64 start, u64 len, get_extent_t *get_extent,
2887 struct extent_map **em_cached)
2888{
2889 struct extent_map *em;
2890
2891 if (em_cached && *em_cached) {
2892 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002893 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002894 start < extent_map_end(em)) {
Elena Reshetova490b54d2017-03-03 10:55:12 +02002895 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002896 return em;
2897 }
2898
2899 free_extent_map(em);
2900 *em_cached = NULL;
2901 }
2902
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02002903 em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
Miao Xie125bac012013-07-25 19:22:37 +08002904 if (em_cached && !IS_ERR_OR_NULL(em)) {
2905 BUG_ON(*em_cached);
Elena Reshetova490b54d2017-03-03 10:55:12 +02002906 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002907 *em_cached = em;
2908 }
2909 return em;
2910}
Chris Masond1310b22008-01-24 16:13:08 -05002911/*
2912 * basic readpage implementation. Locked extent state structs are inserted
2913 * into the tree that are removed when the IO is done (by the end_io
2914 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002915 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07002916 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05002917 */
Miao Xie99740902013-07-25 19:22:36 +08002918static int __do_readpage(struct extent_io_tree *tree,
2919 struct page *page,
2920 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002921 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002922 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02002923 unsigned long *bio_flags, unsigned int read_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002924 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05002925{
2926 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002927 u64 start = page_offset(page);
David Sterba8eec8292017-06-06 19:50:13 +02002928 const u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002929 u64 cur = start;
2930 u64 extent_offset;
2931 u64 last_byte = i_size_read(inode);
2932 u64 block_start;
2933 u64 cur_end;
Chris Masond1310b22008-01-24 16:13:08 -05002934 struct extent_map *em;
2935 struct block_device *bdev;
Liu Bobaf863b2016-07-11 10:39:07 -07002936 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002937 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002938 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002939 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002940 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002941 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00002942 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002943
2944 set_page_extent_mapped(page);
2945
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002946 if (!PageUptodate(page)) {
2947 if (cleancache_get_page(page) == 0) {
2948 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08002949 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002950 goto out;
2951 }
2952 }
2953
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002954 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04002955 char *userpage;
Johannes Thumshirn70730172018-12-05 15:23:03 +01002956 size_t zero_offset = offset_in_page(last_byte);
Chris Masonc8b97812008-10-29 14:49:59 -04002957
2958 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002959 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002960 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002961 memset(userpage + zero_offset, 0, iosize);
2962 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002963 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002964 }
2965 }
Chris Masond1310b22008-01-24 16:13:08 -05002966 while (cur <= end) {
Filipe Manana005efed2015-09-14 09:09:31 +01002967 bool force_bio_submit = false;
David Sterba6273b7f2017-10-04 17:30:11 +02002968 u64 offset;
Josef Bacikc8f2f242013-02-11 11:33:00 -05002969
Chris Masond1310b22008-01-24 16:13:08 -05002970 if (cur >= last_byte) {
2971 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002972 struct extent_state *cached = NULL;
2973
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002974 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002975 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002976 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002977 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002978 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002979 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002980 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00002981 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01002982 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05002983 break;
2984 }
Miao Xie125bac012013-07-25 19:22:37 +08002985 em = __get_extent_map(inode, page, pg_offset, cur,
2986 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02002987 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002988 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00002989 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002990 break;
2991 }
Chris Masond1310b22008-01-24 16:13:08 -05002992 extent_offset = cur - em->start;
2993 BUG_ON(extent_map_end(em) <= cur);
2994 BUG_ON(end < cur);
2995
Li Zefan261507a02010-12-17 14:21:50 +08002996 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07002997 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002998 extent_set_compress_type(&this_bio_flag,
2999 em->compress_type);
3000 }
Chris Masonc8b97812008-10-29 14:49:59 -04003001
Chris Masond1310b22008-01-24 16:13:08 -05003002 iosize = min(extent_map_end(em) - cur, end - cur + 1);
3003 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00003004 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04003005 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
3006 disk_io_size = em->block_len;
David Sterba6273b7f2017-10-04 17:30:11 +02003007 offset = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003008 } else {
David Sterba6273b7f2017-10-04 17:30:11 +02003009 offset = em->block_start + extent_offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003010 disk_io_size = iosize;
3011 }
Chris Masond1310b22008-01-24 16:13:08 -05003012 bdev = em->bdev;
3013 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04003014 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
3015 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01003016
3017 /*
3018 * If we have a file range that points to a compressed extent
3019 * and it's followed by a consecutive file range that points to
3020 * to the same compressed extent (possibly with a different
3021 * offset and/or length, so it either points to the whole extent
3022 * or only part of it), we must make sure we do not submit a
3023 * single bio to populate the pages for the 2 ranges because
3024 * this makes the compressed extent read zero out the pages
3025 * belonging to the 2nd range. Imagine the following scenario:
3026 *
3027 * File layout
3028 * [0 - 8K] [8K - 24K]
3029 * | |
3030 * | |
3031 * points to extent X, points to extent X,
3032 * offset 4K, length of 8K offset 0, length 16K
3033 *
3034 * [extent X, compressed length = 4K uncompressed length = 16K]
3035 *
3036 * If the bio to read the compressed extent covers both ranges,
3037 * it will decompress extent X into the pages belonging to the
3038 * first range and then it will stop, zeroing out the remaining
3039 * pages that belong to the other range that points to extent X.
3040 * So here we make sure we submit 2 bios, one for the first
3041 * range and another one for the third range. Both will target
3042 * the same physical extent from disk, but we can't currently
3043 * make the compressed bio endio callback populate the pages
3044 * for both ranges because each compressed bio is tightly
3045 * coupled with a single extent map, and each range can have
3046 * an extent map with a different offset value relative to the
3047 * uncompressed data of our extent and different lengths. This
3048 * is a corner case so we prioritize correctness over
3049 * non-optimal behavior (submitting 2 bios for the same extent).
3050 */
3051 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3052 prev_em_start && *prev_em_start != (u64)-1 &&
Filipe Manana8e928212019-02-14 15:17:20 +00003053 *prev_em_start != em->start)
Filipe Manana005efed2015-09-14 09:09:31 +01003054 force_bio_submit = true;
3055
3056 if (prev_em_start)
Filipe Manana8e928212019-02-14 15:17:20 +00003057 *prev_em_start = em->start;
Filipe Manana005efed2015-09-14 09:09:31 +01003058
Chris Masond1310b22008-01-24 16:13:08 -05003059 free_extent_map(em);
3060 em = NULL;
3061
3062 /* we've found a hole, just zero and go on */
3063 if (block_start == EXTENT_MAP_HOLE) {
3064 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003065 struct extent_state *cached = NULL;
3066
Cong Wang7ac687d2011-11-25 23:14:28 +08003067 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003068 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003069 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003070 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003071
3072 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003073 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003074 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01003075 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003076 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003077 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003078 continue;
3079 }
3080 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003081 if (test_range_bit(tree, cur, cur_end,
3082 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003083 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003084 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003085 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003086 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003087 continue;
3088 }
Chris Mason70dec802008-01-29 09:59:12 -05003089 /* we have an inline extent but it didn't get marked up
3090 * to date. Error out
3091 */
3092 if (block_start == EXTENT_MAP_INLINE) {
3093 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003094 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003095 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003096 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003097 continue;
3098 }
Chris Masond1310b22008-01-24 16:13:08 -05003099
David Sterba4b81ba42017-06-06 19:14:26 +02003100 ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL,
David Sterba6273b7f2017-10-04 17:30:11 +02003101 page, offset, disk_io_size,
3102 pg_offset, bdev, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003103 end_bio_extent_readpage, mirror_num,
3104 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003105 this_bio_flag,
3106 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003107 if (!ret) {
3108 nr++;
3109 *bio_flags = this_bio_flag;
3110 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003111 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003112 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003113 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003114 }
Chris Masond1310b22008-01-24 16:13:08 -05003115 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003116 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003117 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003118out:
Chris Masond1310b22008-01-24 16:13:08 -05003119 if (!nr) {
3120 if (!PageError(page))
3121 SetPageUptodate(page);
3122 unlock_page(page);
3123 }
Liu Bobaf863b2016-07-11 10:39:07 -07003124 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003125}
3126
Nikolay Borisove65ef212019-03-11 09:55:38 +02003127static inline void contiguous_readpages(struct extent_io_tree *tree,
Miao Xie99740902013-07-25 19:22:36 +08003128 struct page *pages[], int nr_pages,
3129 u64 start, u64 end,
Miao Xie125bac012013-07-25 19:22:37 +08003130 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003131 struct bio **bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003132 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003133 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003134{
3135 struct inode *inode;
3136 struct btrfs_ordered_extent *ordered;
3137 int index;
3138
3139 inode = pages[0]->mapping->host;
3140 while (1) {
3141 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003142 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Miao Xie99740902013-07-25 19:22:36 +08003143 end - start + 1);
3144 if (!ordered)
3145 break;
3146 unlock_extent(tree, start, end);
3147 btrfs_start_ordered_extent(inode, ordered, 1);
3148 btrfs_put_ordered_extent(ordered);
3149 }
3150
3151 for (index = 0; index < nr_pages; index++) {
David Sterba4ef77692017-06-23 04:09:57 +02003152 __do_readpage(tree, pages[index], btrfs_get_extent, em_cached,
Jens Axboe5e9d3982018-08-17 15:45:39 -07003153 bio, 0, bio_flags, REQ_RAHEAD, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003154 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003155 }
3156}
3157
Miao Xie99740902013-07-25 19:22:36 +08003158static int __extent_read_full_page(struct extent_io_tree *tree,
3159 struct page *page,
3160 get_extent_t *get_extent,
3161 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003162 unsigned long *bio_flags,
3163 unsigned int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003164{
3165 struct inode *inode = page->mapping->host;
3166 struct btrfs_ordered_extent *ordered;
3167 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003168 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003169 int ret;
3170
3171 while (1) {
3172 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003173 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003174 PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003175 if (!ordered)
3176 break;
3177 unlock_extent(tree, start, end);
3178 btrfs_start_ordered_extent(inode, ordered, 1);
3179 btrfs_put_ordered_extent(ordered);
3180 }
3181
Miao Xie125bac012013-07-25 19:22:37 +08003182 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003183 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003184 return ret;
3185}
3186
Chris Masond1310b22008-01-24 16:13:08 -05003187int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003188 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003189{
3190 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003191 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003192 int ret;
3193
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003194 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003195 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003196 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003197 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003198 return ret;
3199}
Chris Masond1310b22008-01-24 16:13:08 -05003200
David Sterba3d4b9492017-02-10 19:33:41 +01003201static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003202 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003203{
3204 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003205}
3206
Chris Masond1310b22008-01-24 16:13:08 -05003207/*
Chris Mason40f76582014-05-21 13:35:51 -07003208 * helper for __extent_writepage, doing all of the delayed allocation setup.
3209 *
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003210 * This returns 1 if btrfs_run_delalloc_range function did all the work required
Chris Mason40f76582014-05-21 13:35:51 -07003211 * to write the page (copy into inline extent). In this case the IO has
3212 * been started and the page is already unlocked.
3213 *
3214 * This returns 0 if all went well (page still locked)
3215 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003216 */
Chris Mason40f76582014-05-21 13:35:51 -07003217static noinline_for_stack int writepage_delalloc(struct inode *inode,
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003218 struct page *page, struct writeback_control *wbc,
3219 u64 delalloc_start, unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003220{
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003221 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003222 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Lu Fengqi3522e902018-11-29 11:33:38 +08003223 bool found;
Chris Mason40f76582014-05-21 13:35:51 -07003224 u64 delalloc_to_write = 0;
3225 u64 delalloc_end = 0;
3226 int ret;
3227 int page_started = 0;
3228
Chris Mason40f76582014-05-21 13:35:51 -07003229
3230 while (delalloc_end < page_end) {
Lu Fengqi3522e902018-11-29 11:33:38 +08003231 found = find_lock_delalloc_range(inode, tree,
Chris Mason40f76582014-05-21 13:35:51 -07003232 page,
3233 &delalloc_start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03003234 &delalloc_end);
Lu Fengqi3522e902018-11-29 11:33:38 +08003235 if (!found) {
Chris Mason40f76582014-05-21 13:35:51 -07003236 delalloc_start = delalloc_end + 1;
3237 continue;
3238 }
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003239 ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
3240 delalloc_end, &page_started, nr_written, wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003241 /* File system has been set read-only */
3242 if (ret) {
3243 SetPageError(page);
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003244 /*
3245 * btrfs_run_delalloc_range should return < 0 for error
3246 * but just in case, we use > 0 here meaning the IO is
3247 * started, so we don't want to return > 0 unless
3248 * things are going well.
Chris Mason40f76582014-05-21 13:35:51 -07003249 */
3250 ret = ret < 0 ? ret : -EIO;
3251 goto done;
3252 }
3253 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003254 * delalloc_end is already one less than the total length, so
3255 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003256 */
3257 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003258 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003259 delalloc_start = delalloc_end + 1;
3260 }
3261 if (wbc->nr_to_write < delalloc_to_write) {
3262 int thresh = 8192;
3263
3264 if (delalloc_to_write < thresh * 2)
3265 thresh = delalloc_to_write;
3266 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3267 thresh);
3268 }
3269
3270 /* did the fill delalloc function already unlock and start
3271 * the IO?
3272 */
3273 if (page_started) {
3274 /*
3275 * we've unlocked the page, so we can't update
3276 * the mapping's writeback index, just update
3277 * nr_to_write.
3278 */
3279 wbc->nr_to_write -= *nr_written;
3280 return 1;
3281 }
3282
3283 ret = 0;
3284
3285done:
3286 return ret;
3287}
3288
3289/*
3290 * helper for __extent_writepage. This calls the writepage start hooks,
3291 * and does the loop to map the page into extents and bios.
3292 *
3293 * We return 1 if the IO is started and the page is unlocked,
3294 * 0 if all went well (page still locked)
3295 * < 0 if there were errors (page still locked)
3296 */
3297static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3298 struct page *page,
3299 struct writeback_control *wbc,
3300 struct extent_page_data *epd,
3301 loff_t i_size,
3302 unsigned long nr_written,
David Sterbaf1c77c52017-06-06 19:03:49 +02003303 unsigned int write_flags, int *nr_ret)
Chris Mason40f76582014-05-21 13:35:51 -07003304{
Chris Masond1310b22008-01-24 16:13:08 -05003305 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003306 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003307 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003308 u64 end;
3309 u64 cur = start;
3310 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003311 u64 block_start;
3312 u64 iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003313 struct extent_map *em;
3314 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003315 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003316 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003317 int ret = 0;
3318 int nr = 0;
3319 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003320
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003321 ret = btrfs_writepage_cow_fixup(page, start, page_end);
3322 if (ret) {
3323 /* Fixup worker will requeue */
3324 if (ret == -EBUSY)
3325 wbc->pages_skipped++;
3326 else
3327 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003328
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003329 update_nr_written(wbc, nr_written);
3330 unlock_page(page);
3331 return 1;
Chris Mason247e7432008-07-17 12:53:51 -04003332 }
3333
Chris Mason11c83492009-04-20 15:50:09 -04003334 /*
3335 * we don't want to touch the inode after unlocking the page,
3336 * so we update the mapping writeback index now
3337 */
David Sterba3d4b9492017-02-10 19:33:41 +01003338 update_nr_written(wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003339
Chris Masond1310b22008-01-24 16:13:08 -05003340 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003341 if (i_size <= start) {
Nikolay Borisovc6297322018-11-08 10:18:08 +02003342 btrfs_writepage_endio_finish_ordered(page, start, page_end, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003343 goto done;
3344 }
3345
Chris Masond1310b22008-01-24 16:13:08 -05003346 blocksize = inode->i_sb->s_blocksize;
3347
3348 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003349 u64 em_end;
David Sterba6273b7f2017-10-04 17:30:11 +02003350 u64 offset;
David Sterba58409ed2016-05-04 11:46:10 +02003351
Chris Mason40f76582014-05-21 13:35:51 -07003352 if (cur >= i_size) {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003353 btrfs_writepage_endio_finish_ordered(page, cur,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003354 page_end, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003355 break;
3356 }
David Sterba3c98c622017-06-23 04:01:08 +02003357 em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003358 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003359 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003360 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003361 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003362 break;
3363 }
3364
3365 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003366 em_end = extent_map_end(em);
3367 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003368 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003369 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003370 iosize = ALIGN(iosize, blocksize);
David Sterba6273b7f2017-10-04 17:30:11 +02003371 offset = em->block_start + extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003372 bdev = em->bdev;
3373 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003374 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003375 free_extent_map(em);
3376 em = NULL;
3377
Chris Masonc8b97812008-10-29 14:49:59 -04003378 /*
3379 * compressed and inline extents are written through other
3380 * paths in the FS
3381 */
3382 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003383 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003384 /*
3385 * end_io notification does not happen here for
3386 * compressed extents
3387 */
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003388 if (!compressed)
3389 btrfs_writepage_endio_finish_ordered(page, cur,
3390 cur + iosize - 1,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003391 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003392 else if (compressed) {
3393 /* we don't want to end_page_writeback on
3394 * a compressed extent. this happens
3395 * elsewhere
3396 */
3397 nr++;
3398 }
3399
3400 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003401 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003402 continue;
3403 }
Chris Masonc8b97812008-10-29 14:49:59 -04003404
David Sterba5cdc84b2018-07-18 20:32:52 +02003405 btrfs_set_range_writeback(tree, cur, cur + iosize - 1);
David Sterba58409ed2016-05-04 11:46:10 +02003406 if (!PageWriteback(page)) {
3407 btrfs_err(BTRFS_I(inode)->root->fs_info,
3408 "page %lu not writeback, cur %llu end %llu",
3409 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003410 }
David Sterba58409ed2016-05-04 11:46:10 +02003411
David Sterba4b81ba42017-06-06 19:14:26 +02003412 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003413 page, offset, iosize, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003414 bdev, &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003415 end_bio_extent_writepage,
3416 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003417 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003418 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003419 if (PageWriteback(page))
3420 end_page_writeback(page);
3421 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003422
Chris Masond1310b22008-01-24 16:13:08 -05003423 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003424 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003425 nr++;
3426 }
3427done:
Chris Mason40f76582014-05-21 13:35:51 -07003428 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003429 return ret;
3430}
3431
3432/*
3433 * the writepage semantics are similar to regular writepage. extent
3434 * records are inserted to lock ranges in the tree, and as dirty areas
3435 * are found, they are marked writeback. Then the lock bits are removed
3436 * and the end_io handler clears the writeback ranges
Qu Wenruo30659762019-03-20 14:27:42 +08003437 *
3438 * Return 0 if everything goes well.
3439 * Return <0 for error.
Chris Mason40f76582014-05-21 13:35:51 -07003440 */
3441static int __extent_writepage(struct page *page, struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003442 struct extent_page_data *epd)
Chris Mason40f76582014-05-21 13:35:51 -07003443{
3444 struct inode *inode = page->mapping->host;
Chris Mason40f76582014-05-21 13:35:51 -07003445 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003446 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003447 int ret;
3448 int nr = 0;
3449 size_t pg_offset = 0;
3450 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003451 unsigned long end_index = i_size >> PAGE_SHIFT;
David Sterbaf1c77c52017-06-06 19:03:49 +02003452 unsigned int write_flags = 0;
Chris Mason40f76582014-05-21 13:35:51 -07003453 unsigned long nr_written = 0;
3454
Liu Boff40adf2017-08-24 18:19:48 -06003455 write_flags = wbc_to_write_flags(wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003456
3457 trace___extent_writepage(page, inode, wbc);
3458
3459 WARN_ON(!PageLocked(page));
3460
3461 ClearPageError(page);
3462
Johannes Thumshirn70730172018-12-05 15:23:03 +01003463 pg_offset = offset_in_page(i_size);
Chris Mason40f76582014-05-21 13:35:51 -07003464 if (page->index > end_index ||
3465 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003466 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003467 unlock_page(page);
3468 return 0;
3469 }
3470
3471 if (page->index == end_index) {
3472 char *userpage;
3473
3474 userpage = kmap_atomic(page);
3475 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003476 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003477 kunmap_atomic(userpage);
3478 flush_dcache_page(page);
3479 }
3480
3481 pg_offset = 0;
3482
3483 set_page_extent_mapped(page);
3484
Nikolay Borisov7789a552018-11-08 10:18:06 +02003485 if (!epd->extent_locked) {
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003486 ret = writepage_delalloc(inode, page, wbc, start, &nr_written);
Nikolay Borisov7789a552018-11-08 10:18:06 +02003487 if (ret == 1)
3488 goto done_unlocked;
3489 if (ret)
3490 goto done;
3491 }
Chris Mason40f76582014-05-21 13:35:51 -07003492
3493 ret = __extent_writepage_io(inode, page, wbc, epd,
3494 i_size, nr_written, write_flags, &nr);
3495 if (ret == 1)
3496 goto done_unlocked;
3497
3498done:
Chris Masond1310b22008-01-24 16:13:08 -05003499 if (nr == 0) {
3500 /* make sure the mapping tag for page dirty gets cleared */
3501 set_page_writeback(page);
3502 end_page_writeback(page);
3503 }
Filipe Manana61391d52014-05-09 17:17:40 +01003504 if (PageError(page)) {
3505 ret = ret < 0 ? ret : -EIO;
3506 end_extent_writepage(page, ret, start, page_end);
3507 }
Chris Masond1310b22008-01-24 16:13:08 -05003508 unlock_page(page);
Qu Wenruo30659762019-03-20 14:27:42 +08003509 ASSERT(ret <= 0);
Chris Mason40f76582014-05-21 13:35:51 -07003510 return ret;
Chris Mason771ed682008-11-06 22:02:51 -05003511
Chris Mason11c83492009-04-20 15:50:09 -04003512done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003513 return 0;
3514}
3515
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003516void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003517{
NeilBrown74316202014-07-07 15:16:04 +10003518 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3519 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003520}
3521
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003522/*
3523 * Lock eb pages and flush the bio if we can't the locks
3524 *
3525 * Return 0 if nothing went wrong
3526 * Return >0 is same as 0, except bio is not submitted
3527 * Return <0 if something went wrong, no page is locked
3528 */
David Sterba9df76fb2019-03-20 11:21:41 +01003529static noinline_for_stack int lock_extent_buffer_for_io(struct extent_buffer *eb,
Chris Mason0e378df2014-05-19 20:55:27 -07003530 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003531{
David Sterba9df76fb2019-03-20 11:21:41 +01003532 struct btrfs_fs_info *fs_info = eb->fs_info;
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003533 int i, num_pages, failed_page_nr;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003534 int flush = 0;
3535 int ret = 0;
3536
3537 if (!btrfs_try_tree_write_lock(eb)) {
Qu Wenruof4340622019-03-20 14:27:41 +08003538 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003539 if (ret < 0)
3540 return ret;
3541 flush = 1;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003542 btrfs_tree_lock(eb);
3543 }
3544
3545 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3546 btrfs_tree_unlock(eb);
3547 if (!epd->sync_io)
3548 return 0;
3549 if (!flush) {
Qu Wenruof4340622019-03-20 14:27:41 +08003550 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003551 if (ret < 0)
3552 return ret;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003553 flush = 1;
3554 }
Chris Masona098d8e82012-03-21 12:09:56 -04003555 while (1) {
3556 wait_on_extent_buffer_writeback(eb);
3557 btrfs_tree_lock(eb);
3558 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3559 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003560 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003561 }
3562 }
3563
Josef Bacik51561ff2012-07-20 16:25:24 -04003564 /*
3565 * We need to do this to prevent races in people who check if the eb is
3566 * under IO since we can end up having no IO bits set for a short period
3567 * of time.
3568 */
3569 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003570 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3571 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003572 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003573 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Nikolay Borisov104b4e52017-06-20 21:01:20 +03003574 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3575 -eb->len,
3576 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003577 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003578 } else {
3579 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003580 }
3581
3582 btrfs_tree_unlock(eb);
3583
3584 if (!ret)
3585 return ret;
3586
David Sterba65ad0102018-06-29 10:56:49 +02003587 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003588 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003589 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003590
3591 if (!trylock_page(p)) {
3592 if (!flush) {
Qu Wenruof4340622019-03-20 14:27:41 +08003593 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003594 if (ret < 0) {
3595 failed_page_nr = i;
3596 goto err_unlock;
3597 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003598 flush = 1;
3599 }
3600 lock_page(p);
3601 }
3602 }
3603
3604 return ret;
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003605err_unlock:
3606 /* Unlock already locked pages */
3607 for (i = 0; i < failed_page_nr; i++)
3608 unlock_page(eb->pages[i]);
3609 return ret;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003610}
3611
3612static void end_extent_buffer_writeback(struct extent_buffer *eb)
3613{
3614 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003615 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003616 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3617}
3618
Filipe Manana656f30d2014-09-26 12:25:56 +01003619static void set_btree_ioerr(struct page *page)
3620{
3621 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Filipe Manana656f30d2014-09-26 12:25:56 +01003622
3623 SetPageError(page);
3624 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3625 return;
3626
3627 /*
3628 * If writeback for a btree extent that doesn't belong to a log tree
3629 * failed, increment the counter transaction->eb_write_errors.
3630 * We do this because while the transaction is running and before it's
3631 * committing (when we call filemap_fdata[write|wait]_range against
3632 * the btree inode), we might have
3633 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3634 * returns an error or an error happens during writeback, when we're
3635 * committing the transaction we wouldn't know about it, since the pages
3636 * can be no longer dirty nor marked anymore for writeback (if a
3637 * subsequent modification to the extent buffer didn't happen before the
3638 * transaction commit), which makes filemap_fdata[write|wait]_range not
3639 * able to find the pages tagged with SetPageError at transaction
3640 * commit time. So if this happens we must abort the transaction,
3641 * otherwise we commit a super block with btree roots that point to
3642 * btree nodes/leafs whose content on disk is invalid - either garbage
3643 * or the content of some node/leaf from a past generation that got
3644 * cowed or deleted and is no longer valid.
3645 *
3646 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3647 * not be enough - we need to distinguish between log tree extents vs
3648 * non-log tree extents, and the next filemap_fdatawait_range() call
3649 * will catch and clear such errors in the mapping - and that call might
3650 * be from a log sync and not from a transaction commit. Also, checking
3651 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3652 * not done and would not be reliable - the eb might have been released
3653 * from memory and reading it back again means that flag would not be
3654 * set (since it's a runtime flag, not persisted on disk).
3655 *
3656 * Using the flags below in the btree inode also makes us achieve the
3657 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3658 * writeback for all dirty pages and before filemap_fdatawait_range()
3659 * is called, the writeback for all dirty pages had already finished
3660 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3661 * filemap_fdatawait_range() would return success, as it could not know
3662 * that writeback errors happened (the pages were no longer tagged for
3663 * writeback).
3664 */
3665 switch (eb->log_index) {
3666 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003667 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003668 break;
3669 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003670 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003671 break;
3672 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003673 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003674 break;
3675 default:
3676 BUG(); /* unexpected, logic error */
3677 }
3678}
3679
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003680static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003681{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003682 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003683 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003684 int i, done;
Ming Lei6dc4f102019-02-15 19:13:19 +08003685 struct bvec_iter_all iter_all;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003686
David Sterbac09abff2017-07-13 18:10:07 +02003687 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08003688 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003689 struct page *page = bvec->bv_page;
3690
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003691 eb = (struct extent_buffer *)page->private;
3692 BUG_ON(!eb);
3693 done = atomic_dec_and_test(&eb->io_pages);
3694
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003695 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003696 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003697 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003698 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003699 }
3700
3701 end_page_writeback(page);
3702
3703 if (!done)
3704 continue;
3705
3706 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003707 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003708
3709 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003710}
3711
Chris Mason0e378df2014-05-19 20:55:27 -07003712static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003713 struct writeback_control *wbc,
3714 struct extent_page_data *epd)
3715{
David Sterba0ab02062019-03-20 11:27:57 +01003716 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003717 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003718 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003719 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003720 u32 nritems;
David Sterbacc5e31a2018-03-01 18:20:27 +01003721 int i, num_pages;
Liu Bo851cd172016-09-23 13:44:44 -07003722 unsigned long start, end;
Liu Boff40adf2017-08-24 18:19:48 -06003723 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003724 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003725
Filipe Manana656f30d2014-09-26 12:25:56 +01003726 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02003727 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003728 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003729
Liu Bo851cd172016-09-23 13:44:44 -07003730 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3731 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003732 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003733 end = btrfs_node_key_ptr_offset(nritems);
3734
David Sterbab159fa22016-11-08 18:09:03 +01003735 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003736 } else {
3737 /*
3738 * leaf:
3739 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3740 */
3741 start = btrfs_item_nr_offset(nritems);
David Sterba8f881e82019-03-20 11:33:10 +01003742 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(eb);
David Sterbab159fa22016-11-08 18:09:03 +01003743 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003744 }
3745
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003746 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003747 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003748
3749 clear_page_dirty_for_io(p);
3750 set_page_writeback(p);
David Sterba4b81ba42017-06-06 19:14:26 +02003751 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003752 p, offset, PAGE_SIZE, 0, bdev,
David Sterbac2df8bb2017-02-10 19:29:38 +01003753 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003754 end_bio_extent_buffer_writepage,
Liu Bo18fdc672017-09-13 12:18:22 -06003755 0, 0, 0, false);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003756 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003757 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003758 if (PageWriteback(p))
3759 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003760 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3761 end_extent_buffer_writeback(eb);
3762 ret = -EIO;
3763 break;
3764 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003765 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003766 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003767 unlock_page(p);
3768 }
3769
3770 if (unlikely(ret)) {
3771 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003772 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003773 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003774 unlock_page(p);
3775 }
3776 }
3777
3778 return ret;
3779}
3780
3781int btree_write_cache_pages(struct address_space *mapping,
3782 struct writeback_control *wbc)
3783{
3784 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003785 struct extent_buffer *eb, *prev_eb = NULL;
3786 struct extent_page_data epd = {
3787 .bio = NULL,
3788 .tree = tree,
3789 .extent_locked = 0,
3790 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3791 };
3792 int ret = 0;
3793 int done = 0;
3794 int nr_to_write_done = 0;
3795 struct pagevec pvec;
3796 int nr_pages;
3797 pgoff_t index;
3798 pgoff_t end; /* Inclusive */
3799 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003800 xa_mark_t tag;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003801
Mel Gorman86679822017-11-15 17:37:52 -08003802 pagevec_init(&pvec);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003803 if (wbc->range_cyclic) {
3804 index = mapping->writeback_index; /* Start from prev offset */
3805 end = -1;
3806 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003807 index = wbc->range_start >> PAGE_SHIFT;
3808 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003809 scanned = 1;
3810 }
3811 if (wbc->sync_mode == WB_SYNC_ALL)
3812 tag = PAGECACHE_TAG_TOWRITE;
3813 else
3814 tag = PAGECACHE_TAG_DIRTY;
3815retry:
3816 if (wbc->sync_mode == WB_SYNC_ALL)
3817 tag_pages_for_writeback(mapping, index, end);
3818 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara4006f432017-11-15 17:34:37 -08003819 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08003820 tag))) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003821 unsigned i;
3822
3823 scanned = 1;
3824 for (i = 0; i < nr_pages; i++) {
3825 struct page *page = pvec.pages[i];
3826
3827 if (!PagePrivate(page))
3828 continue;
3829
Josef Bacikb5bae262012-09-14 13:43:01 -04003830 spin_lock(&mapping->private_lock);
3831 if (!PagePrivate(page)) {
3832 spin_unlock(&mapping->private_lock);
3833 continue;
3834 }
3835
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003836 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003837
3838 /*
3839 * Shouldn't happen and normally this would be a BUG_ON
3840 * but no sense in crashing the users box for something
3841 * we can survive anyway.
3842 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303843 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003844 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003845 continue;
3846 }
3847
Josef Bacikb5bae262012-09-14 13:43:01 -04003848 if (eb == prev_eb) {
3849 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003850 continue;
3851 }
3852
Josef Bacikb5bae262012-09-14 13:43:01 -04003853 ret = atomic_inc_not_zero(&eb->refs);
3854 spin_unlock(&mapping->private_lock);
3855 if (!ret)
3856 continue;
3857
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003858 prev_eb = eb;
David Sterba9df76fb2019-03-20 11:21:41 +01003859 ret = lock_extent_buffer_for_io(eb, &epd);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003860 if (!ret) {
3861 free_extent_buffer(eb);
3862 continue;
3863 }
3864
David Sterba0ab02062019-03-20 11:27:57 +01003865 ret = write_one_eb(eb, wbc, &epd);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003866 if (ret) {
3867 done = 1;
3868 free_extent_buffer(eb);
3869 break;
3870 }
3871 free_extent_buffer(eb);
3872
3873 /*
3874 * the filesystem may choose to bump up nr_to_write.
3875 * We have to make sure to honor the new nr_to_write
3876 * at any time
3877 */
3878 nr_to_write_done = wbc->nr_to_write <= 0;
3879 }
3880 pagevec_release(&pvec);
3881 cond_resched();
3882 }
3883 if (!scanned && !done) {
3884 /*
3885 * We hit the last page and there is more work to be done: wrap
3886 * back to the start of the file
3887 */
3888 scanned = 1;
3889 index = 0;
3890 goto retry;
3891 }
Qu Wenruo2b952ee2019-03-20 14:27:43 +08003892 ASSERT(ret <= 0);
3893 if (ret < 0) {
3894 end_write_bio(&epd, ret);
3895 return ret;
3896 }
3897 ret = flush_write_bio(&epd);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003898 return ret;
3899}
3900
Chris Masond1310b22008-01-24 16:13:08 -05003901/**
Chris Mason4bef0842008-09-08 11:18:08 -04003902 * 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 -05003903 * @mapping: address space structure to write
3904 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
David Sterba935db852017-06-23 04:30:28 +02003905 * @data: data passed to __extent_writepage function
Chris Masond1310b22008-01-24 16:13:08 -05003906 *
3907 * If a page is already under I/O, write_cache_pages() skips it, even
3908 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3909 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3910 * and msync() need to guarantee that all the data which was dirty at the time
3911 * the call was made get new I/O started against them. If wbc->sync_mode is
3912 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3913 * existing IO to complete.
3914 */
David Sterba4242b642017-02-10 19:38:24 +01003915static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04003916 struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003917 struct extent_page_data *epd)
Chris Masond1310b22008-01-24 16:13:08 -05003918{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003919 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003920 int ret = 0;
3921 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003922 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003923 struct pagevec pvec;
3924 int nr_pages;
3925 pgoff_t index;
3926 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08003927 pgoff_t done_index;
3928 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003929 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003930 xa_mark_t tag;
Chris Masond1310b22008-01-24 16:13:08 -05003931
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003932 /*
3933 * We have to hold onto the inode so that ordered extents can do their
3934 * work when the IO finishes. The alternative to this is failing to add
3935 * an ordered extent if the igrab() fails there and that is a huge pain
3936 * to deal with, so instead just hold onto the inode throughout the
3937 * writepages operation. If it fails here we are freeing up the inode
3938 * anyway and we'd rather not waste our time writing out stuff that is
3939 * going to be truncated anyway.
3940 */
3941 if (!igrab(inode))
3942 return 0;
3943
Mel Gorman86679822017-11-15 17:37:52 -08003944 pagevec_init(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05003945 if (wbc->range_cyclic) {
3946 index = mapping->writeback_index; /* Start from prev offset */
3947 end = -1;
3948 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003949 index = wbc->range_start >> PAGE_SHIFT;
3950 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08003951 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3952 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05003953 scanned = 1;
3954 }
Ethan Lien3cd24c62018-11-01 14:49:03 +08003955
3956 /*
3957 * We do the tagged writepage as long as the snapshot flush bit is set
3958 * and we are the first one who do the filemap_flush() on this inode.
3959 *
3960 * The nr_to_write == LONG_MAX is needed to make sure other flushers do
3961 * not race in and drop the bit.
3962 */
3963 if (range_whole && wbc->nr_to_write == LONG_MAX &&
3964 test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
3965 &BTRFS_I(inode)->runtime_flags))
3966 wbc->tagged_writepages = 1;
3967
3968 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00003969 tag = PAGECACHE_TAG_TOWRITE;
3970 else
3971 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003972retry:
Ethan Lien3cd24c62018-11-01 14:49:03 +08003973 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00003974 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08003975 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003976 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara67fd7072017-11-15 17:35:19 -08003977 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
3978 &index, end, tag))) {
Chris Masond1310b22008-01-24 16:13:08 -05003979 unsigned i;
3980
3981 scanned = 1;
3982 for (i = 0; i < nr_pages; i++) {
3983 struct page *page = pvec.pages[i];
3984
Liu Boa91326672016-03-07 16:56:21 -08003985 done_index = page->index;
Chris Masond1310b22008-01-24 16:13:08 -05003986 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07003987 * At this point we hold neither the i_pages lock nor
3988 * the page lock: the page may be truncated or
3989 * invalidated (changing page->mapping to NULL),
3990 * or even swizzled back from swapper_space to
3991 * tmpfs file mapping
Chris Masond1310b22008-01-24 16:13:08 -05003992 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003993 if (!trylock_page(page)) {
Qu Wenruof4340622019-03-20 14:27:41 +08003994 ret = flush_write_bio(epd);
3995 BUG_ON(ret < 0);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003996 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003997 }
Chris Masond1310b22008-01-24 16:13:08 -05003998
3999 if (unlikely(page->mapping != mapping)) {
4000 unlock_page(page);
4001 continue;
4002 }
4003
Chris Masond2c3f4f2008-11-19 12:44:22 -05004004 if (wbc->sync_mode != WB_SYNC_NONE) {
Qu Wenruof4340622019-03-20 14:27:41 +08004005 if (PageWriteback(page)) {
4006 ret = flush_write_bio(epd);
4007 BUG_ON(ret < 0);
4008 }
Chris Masond1310b22008-01-24 16:13:08 -05004009 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004010 }
Chris Masond1310b22008-01-24 16:13:08 -05004011
4012 if (PageWriteback(page) ||
4013 !clear_page_dirty_for_io(page)) {
4014 unlock_page(page);
4015 continue;
4016 }
4017
David Sterbaaab6e9e2017-11-30 18:00:02 +01004018 ret = __extent_writepage(page, wbc, epd);
Liu Boa91326672016-03-07 16:56:21 -08004019 if (ret < 0) {
4020 /*
4021 * done_index is set past this page,
4022 * so media errors will not choke
4023 * background writeout for the entire
4024 * file. This has consequences for
4025 * range_cyclic semantics (ie. it may
4026 * not be suitable for data integrity
4027 * writeout).
4028 */
4029 done_index = page->index + 1;
4030 done = 1;
4031 break;
4032 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004033
4034 /*
4035 * the filesystem may choose to bump up nr_to_write.
4036 * We have to make sure to honor the new nr_to_write
4037 * at any time
4038 */
4039 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004040 }
4041 pagevec_release(&pvec);
4042 cond_resched();
4043 }
Liu Bo894b36e2016-03-07 16:56:22 -08004044 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004045 /*
4046 * We hit the last page and there is more work to be done: wrap
4047 * back to the start of the file
4048 */
4049 scanned = 1;
4050 index = 0;
4051 goto retry;
4052 }
Liu Boa91326672016-03-07 16:56:21 -08004053
4054 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4055 mapping->writeback_index = done_index;
4056
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004057 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004058 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004059}
Chris Masond1310b22008-01-24 16:13:08 -05004060
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004061int extent_write_full_page(struct page *page, struct writeback_control *wbc)
Chris Masond1310b22008-01-24 16:13:08 -05004062{
4063 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004064 struct extent_page_data epd = {
4065 .bio = NULL,
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004066 .tree = &BTRFS_I(page->mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004067 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004068 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004069 };
Chris Masond1310b22008-01-24 16:13:08 -05004070
Chris Masond1310b22008-01-24 16:13:08 -05004071 ret = __extent_writepage(page, wbc, &epd);
Qu Wenruo30659762019-03-20 14:27:42 +08004072 ASSERT(ret <= 0);
4073 if (ret < 0) {
4074 end_write_bio(&epd, ret);
4075 return ret;
4076 }
Chris Masond1310b22008-01-24 16:13:08 -05004077
Qu Wenruo30659762019-03-20 14:27:42 +08004078 ret = flush_write_bio(&epd);
4079 ASSERT(ret <= 0);
Chris Masond1310b22008-01-24 16:13:08 -05004080 return ret;
4081}
Chris Masond1310b22008-01-24 16:13:08 -05004082
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004083int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
Chris Mason771ed682008-11-06 22:02:51 -05004084 int mode)
4085{
4086 int ret = 0;
4087 struct address_space *mapping = inode->i_mapping;
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004088 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Mason771ed682008-11-06 22:02:51 -05004089 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004090 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4091 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004092
4093 struct extent_page_data epd = {
4094 .bio = NULL,
4095 .tree = tree,
Chris Mason771ed682008-11-06 22:02:51 -05004096 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004097 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05004098 };
4099 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004100 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004101 .nr_to_write = nr_pages * 2,
4102 .range_start = start,
4103 .range_end = end + 1,
4104 };
4105
Chris Masond3977122009-01-05 21:25:51 -05004106 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004107 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004108 if (clear_page_dirty_for_io(page))
4109 ret = __extent_writepage(page, &wbc_writepages, &epd);
4110 else {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02004111 btrfs_writepage_endio_finish_ordered(page, start,
Nikolay Borisovc6297322018-11-08 10:18:08 +02004112 start + PAGE_SIZE - 1, 1);
Chris Mason771ed682008-11-06 22:02:51 -05004113 unlock_page(page);
4114 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004115 put_page(page);
4116 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004117 }
4118
Qu Wenruo02c6db42019-03-20 14:27:45 +08004119 ASSERT(ret <= 0);
4120 if (ret < 0) {
4121 end_write_bio(&epd, ret);
4122 return ret;
4123 }
4124 ret = flush_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004125 return ret;
4126}
Chris Masond1310b22008-01-24 16:13:08 -05004127
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004128int extent_writepages(struct address_space *mapping,
Chris Masond1310b22008-01-24 16:13:08 -05004129 struct writeback_control *wbc)
4130{
4131 int ret = 0;
4132 struct extent_page_data epd = {
4133 .bio = NULL,
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004134 .tree = &BTRFS_I(mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004135 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004136 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004137 };
4138
David Sterba935db852017-06-23 04:30:28 +02004139 ret = extent_write_cache_pages(mapping, wbc, &epd);
Qu Wenruoa2a72fb2019-03-20 14:27:48 +08004140 ASSERT(ret <= 0);
4141 if (ret < 0) {
4142 end_write_bio(&epd, ret);
4143 return ret;
4144 }
4145 ret = flush_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004146 return ret;
4147}
Chris Masond1310b22008-01-24 16:13:08 -05004148
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004149int extent_readpages(struct address_space *mapping, struct list_head *pages,
4150 unsigned nr_pages)
Chris Masond1310b22008-01-24 16:13:08 -05004151{
4152 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004153 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004154 struct page *pagepool[16];
Miao Xie125bac012013-07-25 19:22:37 +08004155 struct extent_map *em_cached = NULL;
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004156 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Liu Bo67c96842012-07-20 21:43:09 -06004157 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004158 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004159
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004160 while (!list_empty(pages)) {
Nikolay Borisove65ef212019-03-11 09:55:38 +02004161 u64 contig_end = 0;
4162
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004163 for (nr = 0; nr < ARRAY_SIZE(pagepool) && !list_empty(pages);) {
Nikolay Borisovf86196e2019-01-03 15:29:02 -08004164 struct page *page = lru_to_page(pages);
Chris Masond1310b22008-01-24 16:13:08 -05004165
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004166 prefetchw(&page->flags);
4167 list_del(&page->lru);
4168 if (add_to_page_cache_lru(page, mapping, page->index,
4169 readahead_gfp_mask(mapping))) {
4170 put_page(page);
Nikolay Borisove65ef212019-03-11 09:55:38 +02004171 break;
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004172 }
4173
4174 pagepool[nr++] = page;
Nikolay Borisove65ef212019-03-11 09:55:38 +02004175 contig_end = page_offset(page) + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004176 }
Liu Bo67c96842012-07-20 21:43:09 -06004177
Nikolay Borisove65ef212019-03-11 09:55:38 +02004178 if (nr) {
4179 u64 contig_start = page_offset(pagepool[0]);
4180
4181 ASSERT(contig_start + nr * PAGE_SIZE - 1 == contig_end);
4182
4183 contiguous_readpages(tree, pagepool, nr, contig_start,
4184 contig_end, &em_cached, &bio, &bio_flags,
4185 &prev_em_start);
4186 }
Chris Masond1310b22008-01-24 16:13:08 -05004187 }
Liu Bo67c96842012-07-20 21:43:09 -06004188
Miao Xie125bac012013-07-25 19:22:37 +08004189 if (em_cached)
4190 free_extent_map(em_cached);
4191
Chris Masond1310b22008-01-24 16:13:08 -05004192 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004193 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004194 return 0;
4195}
Chris Masond1310b22008-01-24 16:13:08 -05004196
4197/*
4198 * basic invalidatepage code, this waits on any locked or writeback
4199 * ranges corresponding to the page, and then deletes any extent state
4200 * records from the tree
4201 */
4202int extent_invalidatepage(struct extent_io_tree *tree,
4203 struct page *page, unsigned long offset)
4204{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004205 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004206 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004207 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004208 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4209
Qu Wenruofda28322013-02-26 08:10:22 +00004210 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004211 if (start > end)
4212 return 0;
4213
David Sterbaff13db42015-12-03 14:30:40 +01004214 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004215 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004216 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004217 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4218 EXTENT_DO_ACCOUNTING,
David Sterbaae0f1622017-10-31 16:37:52 +01004219 1, 1, &cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05004220 return 0;
4221}
Chris Masond1310b22008-01-24 16:13:08 -05004222
4223/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004224 * a helper for releasepage, this tests for areas of the page that
4225 * are locked or under IO and drops the related state bits if it is safe
4226 * to drop the page.
4227 */
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004228static int try_release_extent_state(struct extent_io_tree *tree,
Eric Sandeen48a3b632013-04-25 20:41:01 +00004229 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004230{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004231 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004232 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004233 int ret = 1;
4234
Nikolay Borisov88826792019-03-14 15:28:31 +02004235 if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) {
Chris Mason7b13b7b2008-04-18 10:29:50 -04004236 ret = 0;
Nikolay Borisov88826792019-03-14 15:28:31 +02004237 } else {
Chris Mason11ef1602009-09-23 20:28:46 -04004238 /*
4239 * at this point we can safely clear everything except the
4240 * locked bit and the nodatasum bit
4241 */
David Sterba66b0c882017-10-31 16:30:47 +01004242 ret = __clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004243 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
David Sterba66b0c882017-10-31 16:30:47 +01004244 0, 0, NULL, mask, NULL);
Chris Masone3f24cc2011-02-14 12:52:08 -05004245
4246 /* if clear_extent_bit failed for enomem reasons,
4247 * we can't allow the release to continue.
4248 */
4249 if (ret < 0)
4250 ret = 0;
4251 else
4252 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004253 }
4254 return ret;
4255}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004256
4257/*
Chris Masond1310b22008-01-24 16:13:08 -05004258 * a helper for releasepage. As long as there are no locked extents
4259 * in the range corresponding to the page, both state records and extent
4260 * map records are removed
4261 */
Nikolay Borisov477a30b2018-04-19 10:46:34 +03004262int try_release_extent_mapping(struct page *page, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004263{
4264 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004265 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004266 u64 end = start + PAGE_SIZE - 1;
Filipe Mananabd3599a2018-07-12 01:36:43 +01004267 struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
4268 struct extent_io_tree *tree = &btrfs_inode->io_tree;
4269 struct extent_map_tree *map = &btrfs_inode->extent_tree;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004270
Mel Gormand0164ad2015-11-06 16:28:21 -08004271 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004272 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004273 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004274 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004275 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004276 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004277 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004278 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004279 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004280 break;
4281 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004282 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4283 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004284 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004285 free_extent_map(em);
4286 break;
4287 }
4288 if (!test_range_bit(tree, em->start,
4289 extent_map_end(em) - 1,
Nikolay Borisov4e586ca2019-03-14 15:28:30 +02004290 EXTENT_LOCKED, 0, NULL)) {
Filipe Mananabd3599a2018-07-12 01:36:43 +01004291 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4292 &btrfs_inode->runtime_flags);
Chris Mason70dec802008-01-29 09:59:12 -05004293 remove_extent_mapping(map, em);
4294 /* once for the rb tree */
4295 free_extent_map(em);
4296 }
4297 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004298 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004299
4300 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004301 free_extent_map(em);
4302 }
Chris Masond1310b22008-01-24 16:13:08 -05004303 }
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004304 return try_release_extent_state(tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004305}
Chris Masond1310b22008-01-24 16:13:08 -05004306
Chris Masonec29ed52011-02-23 16:23:20 -05004307/*
4308 * helper function for fiemap, which doesn't want to see any holes.
4309 * This maps until we find something past 'last'
4310 */
4311static struct extent_map *get_extent_skip_holes(struct inode *inode,
David Sterbae3350e12017-06-23 04:09:57 +02004312 u64 offset, u64 last)
Chris Masonec29ed52011-02-23 16:23:20 -05004313{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004314 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004315 struct extent_map *em;
4316 u64 len;
4317
4318 if (offset >= last)
4319 return NULL;
4320
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304321 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004322 len = last - offset;
4323 if (len == 0)
4324 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004325 len = ALIGN(len, sectorsize);
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02004326 em = btrfs_get_extent_fiemap(BTRFS_I(inode), offset, len);
David Sterbac7040052011-04-19 18:00:01 +02004327 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004328 return em;
4329
4330 /* if this isn't a hole return it */
Nikolay Borisov4a2d25c2017-11-23 10:51:43 +02004331 if (em->block_start != EXTENT_MAP_HOLE)
Chris Masonec29ed52011-02-23 16:23:20 -05004332 return em;
Chris Masonec29ed52011-02-23 16:23:20 -05004333
4334 /* this is a hole, advance to the next extent */
4335 offset = extent_map_end(em);
4336 free_extent_map(em);
4337 if (offset >= last)
4338 break;
4339 }
4340 return NULL;
4341}
4342
Qu Wenruo47518322017-04-07 10:43:15 +08004343/*
4344 * To cache previous fiemap extent
4345 *
4346 * Will be used for merging fiemap extent
4347 */
4348struct fiemap_cache {
4349 u64 offset;
4350 u64 phys;
4351 u64 len;
4352 u32 flags;
4353 bool cached;
4354};
4355
4356/*
4357 * Helper to submit fiemap extent.
4358 *
4359 * Will try to merge current fiemap extent specified by @offset, @phys,
4360 * @len and @flags with cached one.
4361 * And only when we fails to merge, cached one will be submitted as
4362 * fiemap extent.
4363 *
4364 * Return value is the same as fiemap_fill_next_extent().
4365 */
4366static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4367 struct fiemap_cache *cache,
4368 u64 offset, u64 phys, u64 len, u32 flags)
4369{
4370 int ret = 0;
4371
4372 if (!cache->cached)
4373 goto assign;
4374
4375 /*
4376 * Sanity check, extent_fiemap() should have ensured that new
Andrea Gelmini52042d82018-11-28 12:05:13 +01004377 * fiemap extent won't overlap with cached one.
Qu Wenruo47518322017-04-07 10:43:15 +08004378 * Not recoverable.
4379 *
4380 * NOTE: Physical address can overlap, due to compression
4381 */
4382 if (cache->offset + cache->len > offset) {
4383 WARN_ON(1);
4384 return -EINVAL;
4385 }
4386
4387 /*
4388 * Only merges fiemap extents if
4389 * 1) Their logical addresses are continuous
4390 *
4391 * 2) Their physical addresses are continuous
4392 * So truly compressed (physical size smaller than logical size)
4393 * extents won't get merged with each other
4394 *
4395 * 3) Share same flags except FIEMAP_EXTENT_LAST
4396 * So regular extent won't get merged with prealloc extent
4397 */
4398 if (cache->offset + cache->len == offset &&
4399 cache->phys + cache->len == phys &&
4400 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4401 (flags & ~FIEMAP_EXTENT_LAST)) {
4402 cache->len += len;
4403 cache->flags |= flags;
4404 goto try_submit_last;
4405 }
4406
4407 /* Not mergeable, need to submit cached one */
4408 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4409 cache->len, cache->flags);
4410 cache->cached = false;
4411 if (ret)
4412 return ret;
4413assign:
4414 cache->cached = true;
4415 cache->offset = offset;
4416 cache->phys = phys;
4417 cache->len = len;
4418 cache->flags = flags;
4419try_submit_last:
4420 if (cache->flags & FIEMAP_EXTENT_LAST) {
4421 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4422 cache->phys, cache->len, cache->flags);
4423 cache->cached = false;
4424 }
4425 return ret;
4426}
4427
4428/*
Qu Wenruo848c23b2017-06-22 10:01:21 +08004429 * Emit last fiemap cache
Qu Wenruo47518322017-04-07 10:43:15 +08004430 *
Qu Wenruo848c23b2017-06-22 10:01:21 +08004431 * The last fiemap cache may still be cached in the following case:
4432 * 0 4k 8k
4433 * |<- Fiemap range ->|
4434 * |<------------ First extent ----------->|
4435 *
4436 * In this case, the first extent range will be cached but not emitted.
4437 * So we must emit it before ending extent_fiemap().
Qu Wenruo47518322017-04-07 10:43:15 +08004438 */
Qu Wenruo848c23b2017-06-22 10:01:21 +08004439static int emit_last_fiemap_cache(struct btrfs_fs_info *fs_info,
4440 struct fiemap_extent_info *fieinfo,
4441 struct fiemap_cache *cache)
Qu Wenruo47518322017-04-07 10:43:15 +08004442{
4443 int ret;
4444
4445 if (!cache->cached)
4446 return 0;
4447
Qu Wenruo47518322017-04-07 10:43:15 +08004448 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4449 cache->len, cache->flags);
4450 cache->cached = false;
4451 if (ret > 0)
4452 ret = 0;
4453 return ret;
4454}
4455
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004456int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterba2135fb92017-06-23 04:09:57 +02004457 __u64 start, __u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004458{
Josef Bacik975f84f2010-11-23 19:36:57 +00004459 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004460 u64 off = start;
4461 u64 max = start + len;
4462 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004463 u32 found_type;
4464 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004465 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004466 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004467 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004468 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004469 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004470 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004471 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004472 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004473 struct fiemap_cache cache = { 0 };
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004474 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004475 u64 em_start = 0;
4476 u64 em_len = 0;
4477 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004478
4479 if (len == 0)
4480 return -EINVAL;
4481
Josef Bacik975f84f2010-11-23 19:36:57 +00004482 path = btrfs_alloc_path();
4483 if (!path)
4484 return -ENOMEM;
4485 path->leave_spinning = 1;
4486
Jeff Mahoneyda170662016-06-15 09:22:56 -04004487 start = round_down(start, btrfs_inode_sectorsize(inode));
4488 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004489
Chris Masonec29ed52011-02-23 16:23:20 -05004490 /*
4491 * lookup the last file extent. We're not using i_size here
4492 * because there might be preallocation past i_size
4493 */
David Sterbaf85b7372017-01-20 14:54:07 +01004494 ret = btrfs_lookup_file_extent(NULL, root, path,
4495 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004496 if (ret < 0) {
4497 btrfs_free_path(path);
4498 return ret;
Liu Bo2d324f52016-05-17 17:21:48 -07004499 } else {
4500 WARN_ON(!ret);
4501 if (ret == 1)
4502 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004503 }
Liu Bo2d324f52016-05-17 17:21:48 -07004504
Josef Bacik975f84f2010-11-23 19:36:57 +00004505 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004506 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004507 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004508
Chris Masonec29ed52011-02-23 16:23:20 -05004509 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004510 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004511 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004512 /* have to trust i_size as the end */
4513 last = (u64)-1;
4514 last_for_get_extent = isize;
4515 } else {
4516 /*
4517 * remember the start of the last extent. There are a
4518 * bunch of different factors that go into the length of the
4519 * extent, so its much less complex to remember where it started
4520 */
4521 last = found_key.offset;
4522 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004523 }
Liu Bofe09e162013-09-22 12:54:23 +08004524 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004525
Chris Masonec29ed52011-02-23 16:23:20 -05004526 /*
4527 * we might have some extents allocated but more delalloc past those
4528 * extents. so, we trust isize unless the start of the last extent is
4529 * beyond isize
4530 */
4531 if (last < isize) {
4532 last = (u64)-1;
4533 last_for_get_extent = isize;
4534 }
4535
David Sterbaff13db42015-12-03 14:30:40 +01004536 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004537 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004538
David Sterbae3350e12017-06-23 04:09:57 +02004539 em = get_extent_skip_holes(inode, start, last_for_get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004540 if (!em)
4541 goto out;
4542 if (IS_ERR(em)) {
4543 ret = PTR_ERR(em);
4544 goto out;
4545 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004546
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004547 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004548 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004549
Chris Masonea8efc72011-03-08 11:54:40 -05004550 /* break if the extent we found is outside the range */
4551 if (em->start >= max || extent_map_end(em) < off)
4552 break;
4553
4554 /*
4555 * get_extent may return an extent that starts before our
4556 * requested range. We have to make sure the ranges
4557 * we return to fiemap always move forward and don't
4558 * overlap, so adjust the offsets here
4559 */
4560 em_start = max(em->start, off);
4561
4562 /*
4563 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004564 * for adjusting the disk offset below. Only do this if the
4565 * extent isn't compressed since our in ram offset may be past
4566 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004567 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004568 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4569 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004570 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004571 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004572 flags = 0;
Filipe Mananaf0986312018-06-20 10:02:30 +01004573 if (em->block_start < EXTENT_MAP_LAST_BYTE)
4574 disko = em->block_start + offset_in_extent;
4575 else
4576 disko = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004577
Chris Masonea8efc72011-03-08 11:54:40 -05004578 /*
4579 * bump off for our next call to get_extent
4580 */
4581 off = extent_map_end(em);
4582 if (off >= max)
4583 end = 1;
4584
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004585 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004586 end = 1;
4587 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004588 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004589 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4590 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004591 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004592 flags |= (FIEMAP_EXTENT_DELALLOC |
4593 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004594 } else if (fieinfo->fi_extents_max) {
4595 u64 bytenr = em->block_start -
4596 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004597
Liu Bofe09e162013-09-22 12:54:23 +08004598 /*
4599 * As btrfs supports shared space, this information
4600 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004601 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4602 * then we're just getting a count and we can skip the
4603 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004604 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06004605 ret = btrfs_check_shared(root,
4606 btrfs_ino(BTRFS_I(inode)),
4607 bytenr);
Josef Bacikdc046b12014-09-10 16:20:45 -04004608 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004609 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004610 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004611 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004612 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004613 }
4614 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4615 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004616 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4617 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004618
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004619 free_extent_map(em);
4620 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004621 if ((em_start >= last) || em_len == (u64)-1 ||
4622 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004623 flags |= FIEMAP_EXTENT_LAST;
4624 end = 1;
4625 }
4626
Chris Masonec29ed52011-02-23 16:23:20 -05004627 /* now scan forward to see if this is really the last extent. */
David Sterbae3350e12017-06-23 04:09:57 +02004628 em = get_extent_skip_holes(inode, off, last_for_get_extent);
Chris Masonec29ed52011-02-23 16:23:20 -05004629 if (IS_ERR(em)) {
4630 ret = PTR_ERR(em);
4631 goto out;
4632 }
4633 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004634 flags |= FIEMAP_EXTENT_LAST;
4635 end = 1;
4636 }
Qu Wenruo47518322017-04-07 10:43:15 +08004637 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4638 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004639 if (ret) {
4640 if (ret == 1)
4641 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004642 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004643 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004644 }
4645out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004646 if (!ret)
Qu Wenruo848c23b2017-06-22 10:01:21 +08004647 ret = emit_last_fiemap_cache(root->fs_info, fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004648 free_extent_map(em);
4649out:
Liu Bofe09e162013-09-22 12:54:23 +08004650 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004651 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004652 &cached_state);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004653 return ret;
4654}
4655
Chris Mason727011e2010-08-06 13:21:20 -04004656static void __free_extent_buffer(struct extent_buffer *eb)
4657{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004658 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004659 kmem_cache_free(extent_buffer_cache, eb);
4660}
4661
Josef Bacika26e8c92014-03-28 17:07:27 -04004662int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004663{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004664 return (atomic_read(&eb->io_pages) ||
4665 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4666 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004667}
4668
Miao Xie897ca6e92010-10-26 20:57:29 -04004669/*
David Sterba55ac0132018-07-19 17:24:32 +02004670 * Release all pages attached to the extent buffer.
Miao Xie897ca6e92010-10-26 20:57:29 -04004671 */
David Sterba55ac0132018-07-19 17:24:32 +02004672static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004673{
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004674 int i;
4675 int num_pages;
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004676 int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004677
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004678 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004679
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004680 num_pages = num_extent_pages(eb);
4681 for (i = 0; i < num_pages; i++) {
4682 struct page *page = eb->pages[i];
Miao Xie897ca6e92010-10-26 20:57:29 -04004683
Forrest Liu5d2361d2015-02-09 17:31:45 +08004684 if (!page)
4685 continue;
4686 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004687 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004688 /*
4689 * We do this since we'll remove the pages after we've
4690 * removed the eb from the radix tree, so we could race
4691 * and have this page now attached to the new eb. So
4692 * only clear page_private if it's still connected to
4693 * this eb.
4694 */
4695 if (PagePrivate(page) &&
4696 page->private == (unsigned long)eb) {
4697 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4698 BUG_ON(PageDirty(page));
4699 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004700 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004701 * We need to make sure we haven't be attached
4702 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004703 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004704 ClearPagePrivate(page);
4705 set_page_private(page, 0);
4706 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004707 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004708 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004709
4710 if (mapped)
4711 spin_unlock(&page->mapping->private_lock);
4712
Nicholas D Steeves01327612016-05-19 21:18:45 -04004713 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004714 put_page(page);
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004715 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004716}
4717
4718/*
4719 * Helper for releasing the extent buffer.
4720 */
4721static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4722{
David Sterba55ac0132018-07-19 17:24:32 +02004723 btrfs_release_extent_buffer_pages(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004724 __free_extent_buffer(eb);
4725}
4726
Josef Bacikf28491e2013-12-16 13:24:27 -05004727static struct extent_buffer *
4728__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004729 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004730{
4731 struct extent_buffer *eb = NULL;
4732
Michal Hockod1b5c562015-08-19 14:17:40 +02004733 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004734 eb->start = start;
4735 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004736 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004737 eb->bflags = 0;
4738 rwlock_init(&eb->lock);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004739 atomic_set(&eb->blocking_readers, 0);
4740 atomic_set(&eb->blocking_writers, 0);
David Sterbaed1b4ed2018-08-24 16:31:17 +02004741 eb->lock_nested = false;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004742 init_waitqueue_head(&eb->write_lock_wq);
4743 init_waitqueue_head(&eb->read_lock_wq);
4744
4745 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4746
4747 spin_lock_init(&eb->refs_lock);
4748 atomic_set(&eb->refs, 1);
4749 atomic_set(&eb->io_pages, 0);
4750
4751 /*
4752 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4753 */
4754 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4755 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4756 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4757
David Sterba843ccf92018-08-24 14:56:28 +02004758#ifdef CONFIG_BTRFS_DEBUG
4759 atomic_set(&eb->spinning_writers, 0);
David Sterbaafd495a2018-08-24 15:57:38 +02004760 atomic_set(&eb->spinning_readers, 0);
David Sterba5c9c7992018-08-24 16:15:51 +02004761 atomic_set(&eb->read_locks, 0);
David Sterbac79adfc2018-08-24 16:24:26 +02004762 atomic_set(&eb->write_locks, 0);
David Sterba843ccf92018-08-24 14:56:28 +02004763#endif
4764
Josef Bacikdb7f3432013-08-07 14:54:37 -04004765 return eb;
4766}
4767
4768struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4769{
David Sterbacc5e31a2018-03-01 18:20:27 +01004770 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004771 struct page *p;
4772 struct extent_buffer *new;
David Sterbacc5e31a2018-03-01 18:20:27 +01004773 int num_pages = num_extent_pages(src);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004774
David Sterba3f556f72014-06-15 03:20:26 +02004775 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004776 if (new == NULL)
4777 return NULL;
4778
4779 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004780 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004781 if (!p) {
4782 btrfs_release_extent_buffer(new);
4783 return NULL;
4784 }
4785 attach_extent_buffer_page(new, p);
4786 WARN_ON(PageDirty(p));
4787 SetPageUptodate(p);
4788 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004789 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004790 }
4791
Josef Bacikdb7f3432013-08-07 14:54:37 -04004792 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004793 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004794
4795 return new;
4796}
4797
Omar Sandoval0f331222015-09-29 20:50:31 -07004798struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4799 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004800{
4801 struct extent_buffer *eb;
David Sterbacc5e31a2018-03-01 18:20:27 +01004802 int num_pages;
4803 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004804
David Sterba3f556f72014-06-15 03:20:26 +02004805 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004806 if (!eb)
4807 return NULL;
4808
David Sterba65ad0102018-06-29 10:56:49 +02004809 num_pages = num_extent_pages(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004810 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004811 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004812 if (!eb->pages[i])
4813 goto err;
4814 }
4815 set_extent_buffer_uptodate(eb);
4816 btrfs_set_header_nritems(eb, 0);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004817 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004818
4819 return eb;
4820err:
4821 for (; i > 0; i--)
4822 __free_page(eb->pages[i - 1]);
4823 __free_extent_buffer(eb);
4824 return NULL;
4825}
4826
Omar Sandoval0f331222015-09-29 20:50:31 -07004827struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004828 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004829{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004830 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004831}
4832
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004833static void check_buffer_tree_ref(struct extent_buffer *eb)
4834{
Chris Mason242e18c2013-01-29 17:49:37 -05004835 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004836 /* the ref bit is tricky. We have to make sure it is set
4837 * if we have the buffer dirty. Otherwise the
4838 * code to free a buffer can end up dropping a dirty
4839 * page
4840 *
4841 * Once the ref bit is set, it won't go away while the
4842 * buffer is dirty or in writeback, and it also won't
4843 * go away while we have the reference count on the
4844 * eb bumped.
4845 *
4846 * We can't just set the ref bit without bumping the
4847 * ref on the eb because free_extent_buffer might
4848 * see the ref bit and try to clear it. If this happens
4849 * free_extent_buffer might end up dropping our original
4850 * ref by mistake and freeing the page before we are able
4851 * to add one more ref.
4852 *
4853 * So bump the ref count first, then set the bit. If someone
4854 * beat us to it, drop the ref we added.
4855 */
Chris Mason242e18c2013-01-29 17:49:37 -05004856 refs = atomic_read(&eb->refs);
4857 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4858 return;
4859
Josef Bacik594831c2012-07-20 16:11:08 -04004860 spin_lock(&eb->refs_lock);
4861 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004862 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004863 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004864}
4865
Mel Gorman2457aec2014-06-04 16:10:31 -07004866static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4867 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004868{
David Sterbacc5e31a2018-03-01 18:20:27 +01004869 int num_pages, i;
Josef Bacik5df42352012-03-15 18:24:42 -04004870
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004871 check_buffer_tree_ref(eb);
4872
David Sterba65ad0102018-06-29 10:56:49 +02004873 num_pages = num_extent_pages(eb);
Josef Bacik5df42352012-03-15 18:24:42 -04004874 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004875 struct page *p = eb->pages[i];
4876
Mel Gorman2457aec2014-06-04 16:10:31 -07004877 if (p != accessed)
4878 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004879 }
4880}
4881
Josef Bacikf28491e2013-12-16 13:24:27 -05004882struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4883 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004884{
4885 struct extent_buffer *eb;
4886
4887 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004888 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004889 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004890 if (eb && atomic_inc_not_zero(&eb->refs)) {
4891 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004892 /*
4893 * Lock our eb's refs_lock to avoid races with
4894 * free_extent_buffer. When we get our eb it might be flagged
4895 * with EXTENT_BUFFER_STALE and another task running
4896 * free_extent_buffer might have seen that flag set,
4897 * eb->refs == 2, that the buffer isn't under IO (dirty and
4898 * writeback flags not set) and it's still in the tree (flag
4899 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4900 * of decrementing the extent buffer's reference count twice.
4901 * So here we could race and increment the eb's reference count,
4902 * clear its stale flag, mark it as dirty and drop our reference
4903 * before the other task finishes executing free_extent_buffer,
4904 * which would later result in an attempt to free an extent
4905 * buffer that is dirty.
4906 */
4907 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4908 spin_lock(&eb->refs_lock);
4909 spin_unlock(&eb->refs_lock);
4910 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004911 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004912 return eb;
4913 }
4914 rcu_read_unlock();
4915
4916 return NULL;
4917}
4918
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004919#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4920struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004921 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004922{
4923 struct extent_buffer *eb, *exists = NULL;
4924 int ret;
4925
4926 eb = find_extent_buffer(fs_info, start);
4927 if (eb)
4928 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04004929 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004930 if (!eb)
4931 return NULL;
4932 eb->fs_info = fs_info;
4933again:
David Sterbae1860a72016-05-09 14:11:38 +02004934 ret = radix_tree_preload(GFP_NOFS);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004935 if (ret)
4936 goto free_eb;
4937 spin_lock(&fs_info->buffer_lock);
4938 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004939 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004940 spin_unlock(&fs_info->buffer_lock);
4941 radix_tree_preload_end();
4942 if (ret == -EEXIST) {
4943 exists = find_extent_buffer(fs_info, start);
4944 if (exists)
4945 goto free_eb;
4946 else
4947 goto again;
4948 }
4949 check_buffer_tree_ref(eb);
4950 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
4951
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004952 return eb;
4953free_eb:
4954 btrfs_release_extent_buffer(eb);
4955 return exists;
4956}
4957#endif
4958
Josef Bacikf28491e2013-12-16 13:24:27 -05004959struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004960 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05004961{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004962 unsigned long len = fs_info->nodesize;
David Sterbacc5e31a2018-03-01 18:20:27 +01004963 int num_pages;
4964 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004965 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004966 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004967 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004968 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05004969 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05004970 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004971 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004972
Jeff Mahoneyda170662016-06-15 09:22:56 -04004973 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07004974 btrfs_err(fs_info, "bad tree block start %llu", start);
4975 return ERR_PTR(-EINVAL);
4976 }
4977
Josef Bacikf28491e2013-12-16 13:24:27 -05004978 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004979 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004980 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004981
David Sterba23d79d82014-06-15 02:55:29 +02004982 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04004983 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07004984 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05004985
David Sterba65ad0102018-06-29 10:56:49 +02004986 num_pages = num_extent_pages(eb);
Chris Mason727011e2010-08-06 13:21:20 -04004987 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02004988 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07004989 if (!p) {
4990 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04004991 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004992 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004993
4994 spin_lock(&mapping->private_lock);
4995 if (PagePrivate(p)) {
4996 /*
4997 * We could have already allocated an eb for this page
4998 * and attached one so lets see if we can get a ref on
4999 * the existing eb, and if we can we know it's good and
5000 * we can just return that one, else we know we can just
5001 * overwrite page->private.
5002 */
5003 exists = (struct extent_buffer *)p->private;
5004 if (atomic_inc_not_zero(&exists->refs)) {
5005 spin_unlock(&mapping->private_lock);
5006 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005007 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07005008 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005009 goto free_eb;
5010 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005011 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005012
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005013 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005014 * Do this so attach doesn't complain and we need to
5015 * drop the ref the old guy had.
5016 */
5017 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005018 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005019 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05005020 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005021 attach_extent_buffer_page(eb, p);
5022 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005023 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04005024 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05005025 if (!PageUptodate(p))
5026 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05005027
5028 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005029 * We can't unlock the pages just yet since the extent buffer
5030 * hasn't been properly inserted in the radix tree, this
5031 * opens a race with btree_releasepage which can free a page
5032 * while we are still filling in all pages for the buffer and
5033 * we could crash.
Chris Masoneb14ab82011-02-10 12:35:00 -05005034 */
Chris Masond1310b22008-01-24 16:13:08 -05005035 }
5036 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05005037 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05005038again:
David Sterbae1860a72016-05-09 14:11:38 +02005039 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07005040 if (ret) {
5041 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04005042 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005043 }
Miao Xie19fe0a82010-10-26 20:57:29 -04005044
Josef Bacikf28491e2013-12-16 13:24:27 -05005045 spin_lock(&fs_info->buffer_lock);
5046 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005047 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05005048 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005049 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04005050 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005051 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005052 if (exists)
5053 goto free_eb;
5054 else
Josef Bacik115391d2012-03-09 09:51:43 -05005055 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04005056 }
Chris Mason6af118ce2008-07-22 11:18:07 -04005057 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005058 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005059 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005060
5061 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005062 * Now it's safe to unlock the pages because any calls to
5063 * btree_releasepage will correctly detect that a page belongs to a
5064 * live buffer and won't free them prematurely.
Chris Masoneb14ab82011-02-10 12:35:00 -05005065 */
Nikolay Borisov28187ae2018-07-04 10:24:51 +03005066 for (i = 0; i < num_pages; i++)
5067 unlock_page(eb->pages[i]);
Chris Masond1310b22008-01-24 16:13:08 -05005068 return eb;
5069
Chris Mason6af118ce2008-07-22 11:18:07 -04005070free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005071 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005072 for (i = 0; i < num_pages; i++) {
5073 if (eb->pages[i])
5074 unlock_page(eb->pages[i]);
5075 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005076
Miao Xie897ca6e92010-10-26 20:57:29 -04005077 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005078 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005079}
Chris Masond1310b22008-01-24 16:13:08 -05005080
Josef Bacik3083ee22012-03-09 16:01:49 -05005081static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5082{
5083 struct extent_buffer *eb =
5084 container_of(head, struct extent_buffer, rcu_head);
5085
5086 __free_extent_buffer(eb);
5087}
5088
David Sterbaf7a52a42013-04-26 14:56:29 +00005089static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005090{
Nikolay Borisov07e21c42018-06-27 16:38:23 +03005091 lockdep_assert_held(&eb->refs_lock);
5092
Josef Bacik3083ee22012-03-09 16:01:49 -05005093 WARN_ON(atomic_read(&eb->refs) == 0);
5094 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005095 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005096 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005097
Jan Schmidt815a51c2012-05-16 17:00:02 +02005098 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005099
Josef Bacikf28491e2013-12-16 13:24:27 -05005100 spin_lock(&fs_info->buffer_lock);
5101 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005102 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005103 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005104 } else {
5105 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005106 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005107
5108 /* Should be safe to release our pages at this point */
David Sterba55ac0132018-07-19 17:24:32 +02005109 btrfs_release_extent_buffer_pages(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005110#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005111 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
Josef Bacikbcb7e442015-03-16 17:38:02 -04005112 __free_extent_buffer(eb);
5113 return 1;
5114 }
5115#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005116 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005117 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005118 }
5119 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005120
5121 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005122}
5123
Chris Masond1310b22008-01-24 16:13:08 -05005124void free_extent_buffer(struct extent_buffer *eb)
5125{
Chris Mason242e18c2013-01-29 17:49:37 -05005126 int refs;
5127 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005128 if (!eb)
5129 return;
5130
Chris Mason242e18c2013-01-29 17:49:37 -05005131 while (1) {
5132 refs = atomic_read(&eb->refs);
Nikolay Borisov46cc7752018-10-15 17:04:01 +03005133 if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
5134 || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
5135 refs == 1))
Chris Mason242e18c2013-01-29 17:49:37 -05005136 break;
5137 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5138 if (old == refs)
5139 return;
5140 }
5141
Josef Bacik3083ee22012-03-09 16:01:49 -05005142 spin_lock(&eb->refs_lock);
5143 if (atomic_read(&eb->refs) == 2 &&
5144 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005145 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005146 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5147 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005148
Josef Bacik3083ee22012-03-09 16:01:49 -05005149 /*
5150 * I know this is terrible, but it's temporary until we stop tracking
5151 * the uptodate bits and such for the extent buffers.
5152 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005153 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005154}
Chris Masond1310b22008-01-24 16:13:08 -05005155
Josef Bacik3083ee22012-03-09 16:01:49 -05005156void free_extent_buffer_stale(struct extent_buffer *eb)
5157{
5158 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005159 return;
5160
Josef Bacik3083ee22012-03-09 16:01:49 -05005161 spin_lock(&eb->refs_lock);
5162 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5163
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005164 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005165 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5166 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005167 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005168}
5169
Chris Mason1d4284b2012-03-28 20:31:37 -04005170void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005171{
David Sterbacc5e31a2018-03-01 18:20:27 +01005172 int i;
5173 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005174 struct page *page;
5175
David Sterba65ad0102018-06-29 10:56:49 +02005176 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005177
5178 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005179 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005180 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005181 continue;
5182
Chris Masona61e6f22008-07-22 11:18:08 -04005183 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005184 WARN_ON(!PagePrivate(page));
5185
Chris Masond1310b22008-01-24 16:13:08 -05005186 clear_page_dirty_for_io(page);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005187 xa_lock_irq(&page->mapping->i_pages);
Matthew Wilcox0a943c62017-12-04 10:37:22 -05005188 if (!PageDirty(page))
5189 __xa_clear_mark(&page->mapping->i_pages,
5190 page_index(page), PAGECACHE_TAG_DIRTY);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005191 xa_unlock_irq(&page->mapping->i_pages);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005192 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005193 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005194 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005195 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005196}
Chris Masond1310b22008-01-24 16:13:08 -05005197
Liu Boabb57ef2018-09-14 01:44:42 +08005198bool set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005199{
David Sterbacc5e31a2018-03-01 18:20:27 +01005200 int i;
5201 int num_pages;
Liu Boabb57ef2018-09-14 01:44:42 +08005202 bool was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005203
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005204 check_buffer_tree_ref(eb);
5205
Chris Masonb9473432009-03-13 11:00:37 -04005206 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005207
David Sterba65ad0102018-06-29 10:56:49 +02005208 num_pages = num_extent_pages(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05005209 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005210 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5211
Liu Boabb57ef2018-09-14 01:44:42 +08005212 if (!was_dirty)
5213 for (i = 0; i < num_pages; i++)
5214 set_page_dirty(eb->pages[i]);
Liu Bo51995c32018-09-14 01:46:08 +08005215
5216#ifdef CONFIG_BTRFS_DEBUG
5217 for (i = 0; i < num_pages; i++)
5218 ASSERT(PageDirty(eb->pages[i]));
5219#endif
5220
Chris Masonb9473432009-03-13 11:00:37 -04005221 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005222}
Chris Masond1310b22008-01-24 16:13:08 -05005223
David Sterba69ba3922015-12-03 13:08:59 +01005224void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005225{
David Sterbacc5e31a2018-03-01 18:20:27 +01005226 int i;
Chris Mason1259ab72008-05-12 13:39:03 -04005227 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005228 int num_pages;
Chris Mason1259ab72008-05-12 13:39:03 -04005229
Chris Masonb4ce94d2009-02-04 09:25:08 -05005230 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005231 num_pages = num_extent_pages(eb);
Chris Mason1259ab72008-05-12 13:39:03 -04005232 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005233 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005234 if (page)
5235 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005236 }
Chris Mason1259ab72008-05-12 13:39:03 -04005237}
5238
David Sterba09c25a82015-12-03 13:08:59 +01005239void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005240{
David Sterbacc5e31a2018-03-01 18:20:27 +01005241 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005242 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005243 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005244
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005245 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005246 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005247 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005248 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005249 SetPageUptodate(page);
5250 }
Chris Masond1310b22008-01-24 16:13:08 -05005251}
Chris Masond1310b22008-01-24 16:13:08 -05005252
Chris Masond1310b22008-01-24 16:13:08 -05005253int read_extent_buffer_pages(struct extent_io_tree *tree,
David Sterba6af49db2017-06-23 04:09:57 +02005254 struct extent_buffer *eb, int wait, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005255{
David Sterbacc5e31a2018-03-01 18:20:27 +01005256 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005257 struct page *page;
5258 int err;
5259 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005260 int locked_pages = 0;
5261 int all_uptodate = 1;
David Sterbacc5e31a2018-03-01 18:20:27 +01005262 int num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005263 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005264 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005265 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005266
Chris Masonb4ce94d2009-02-04 09:25:08 -05005267 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005268 return 0;
5269
David Sterba65ad0102018-06-29 10:56:49 +02005270 num_pages = num_extent_pages(eb);
Josef Bacik8436ea912016-09-02 15:40:03 -04005271 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005272 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005273 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005274 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005275 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005276 } else {
5277 lock_page(page);
5278 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005279 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005280 }
5281 /*
5282 * We need to firstly lock all pages to make sure that
5283 * the uptodate bit of our pages won't be affected by
5284 * clear_extent_buffer_uptodate().
5285 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005286 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005287 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005288 if (!PageUptodate(page)) {
5289 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005290 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005291 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005292 }
Liu Bo2571e732016-08-03 12:33:01 -07005293
Chris Masonce9adaa2008-04-09 16:28:12 -04005294 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005295 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005296 goto unlock_exit;
5297 }
5298
Filipe Manana656f30d2014-09-26 12:25:56 +01005299 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005300 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005301 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005302 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005303 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005304
Chris Masonce9adaa2008-04-09 16:28:12 -04005305 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005306 if (ret) {
5307 atomic_dec(&eb->io_pages);
5308 unlock_page(page);
5309 continue;
5310 }
5311
Chris Masonf1885912008-04-09 16:28:12 -04005312 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005313 err = __extent_read_full_page(tree, page,
David Sterba6af49db2017-06-23 04:09:57 +02005314 btree_get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005315 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005316 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005317 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005318 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005319 /*
5320 * We use &bio in above __extent_read_full_page,
5321 * so we ensure that if it returns error, the
5322 * current page fails to add itself to bio and
5323 * it's been unlocked.
5324 *
5325 * We must dec io_pages by ourselves.
5326 */
5327 atomic_dec(&eb->io_pages);
5328 }
Chris Masond1310b22008-01-24 16:13:08 -05005329 } else {
5330 unlock_page(page);
5331 }
5332 }
5333
Jeff Mahoney355808c2011-10-03 23:23:14 -04005334 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005335 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005336 if (err)
5337 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005338 }
Chris Masona86c12c2008-02-07 10:50:54 -05005339
Arne Jansenbb82ab82011-06-10 14:06:53 +02005340 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005341 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005342
Josef Bacik8436ea912016-09-02 15:40:03 -04005343 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005344 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005345 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005346 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005347 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005348 }
Chris Masond3977122009-01-05 21:25:51 -05005349
Chris Masond1310b22008-01-24 16:13:08 -05005350 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005351
5352unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005353 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005354 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005355 page = eb->pages[locked_pages];
5356 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005357 }
5358 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005359}
Chris Masond1310b22008-01-24 16:13:08 -05005360
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005361void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5362 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005363{
5364 size_t cur;
5365 size_t offset;
5366 struct page *page;
5367 char *kaddr;
5368 char *dst = (char *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005369 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005370 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005371
Liu Bof716abd2017-08-09 11:10:16 -06005372 if (start + len > eb->len) {
5373 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5374 eb->start, eb->len, start, len);
5375 memset(dst, 0, len);
5376 return;
5377 }
Chris Masond1310b22008-01-24 16:13:08 -05005378
Johannes Thumshirn70730172018-12-05 15:23:03 +01005379 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005380
Chris Masond3977122009-01-05 21:25:51 -05005381 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005382 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005383
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005384 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005385 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005386 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005387
5388 dst += cur;
5389 len -= cur;
5390 offset = 0;
5391 i++;
5392 }
5393}
Chris Masond1310b22008-01-24 16:13:08 -05005394
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005395int read_extent_buffer_to_user(const struct extent_buffer *eb,
5396 void __user *dstv,
5397 unsigned long start, unsigned long len)
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005398{
5399 size_t cur;
5400 size_t offset;
5401 struct page *page;
5402 char *kaddr;
5403 char __user *dst = (char __user *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005404 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005405 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005406 int ret = 0;
5407
5408 WARN_ON(start > eb->len);
5409 WARN_ON(start + len > eb->start + eb->len);
5410
Johannes Thumshirn70730172018-12-05 15:23:03 +01005411 offset = offset_in_page(start_offset + start);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005412
5413 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005414 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005415
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005416 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005417 kaddr = page_address(page);
5418 if (copy_to_user(dst, kaddr + offset, cur)) {
5419 ret = -EFAULT;
5420 break;
5421 }
5422
5423 dst += cur;
5424 len -= cur;
5425 offset = 0;
5426 i++;
5427 }
5428
5429 return ret;
5430}
5431
Liu Bo415b35a2016-06-17 19:16:21 -07005432/*
5433 * return 0 if the item is found within a page.
5434 * return 1 if the item spans two pages.
5435 * return -EINVAL otherwise.
5436 */
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005437int map_private_extent_buffer(const struct extent_buffer *eb,
5438 unsigned long start, unsigned long min_len,
5439 char **map, unsigned long *map_start,
5440 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005441{
Johannes Thumshirncc2c39d2018-11-28 09:54:54 +01005442 size_t offset;
Chris Masond1310b22008-01-24 16:13:08 -05005443 char *kaddr;
5444 struct page *p;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005445 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005446 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005447 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005448 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005449
Liu Bof716abd2017-08-09 11:10:16 -06005450 if (start + min_len > eb->len) {
5451 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5452 eb->start, eb->len, start, min_len);
5453 return -EINVAL;
5454 }
5455
Chris Masond1310b22008-01-24 16:13:08 -05005456 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005457 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005458
5459 if (i == 0) {
5460 offset = start_offset;
5461 *map_start = 0;
5462 } else {
5463 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005464 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005465 }
Chris Masond3977122009-01-05 21:25:51 -05005466
David Sterbafb85fc92014-07-31 01:03:53 +02005467 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005468 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005469 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005470 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005471 return 0;
5472}
Chris Masond1310b22008-01-24 16:13:08 -05005473
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005474int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5475 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005476{
5477 size_t cur;
5478 size_t offset;
5479 struct page *page;
5480 char *kaddr;
5481 char *ptr = (char *)ptrv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005482 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005483 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005484 int ret = 0;
5485
5486 WARN_ON(start > eb->len);
5487 WARN_ON(start + len > eb->start + eb->len);
5488
Johannes Thumshirn70730172018-12-05 15:23:03 +01005489 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005490
Chris Masond3977122009-01-05 21:25:51 -05005491 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005492 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005493
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005494 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005495
Chris Masona6591712011-07-19 12:04:14 -04005496 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005497 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005498 if (ret)
5499 break;
5500
5501 ptr += cur;
5502 len -= cur;
5503 offset = 0;
5504 i++;
5505 }
5506 return ret;
5507}
Chris Masond1310b22008-01-24 16:13:08 -05005508
David Sterbaf157bf72016-11-09 17:43:38 +01005509void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5510 const void *srcv)
5511{
5512 char *kaddr;
5513
5514 WARN_ON(!PageUptodate(eb->pages[0]));
5515 kaddr = page_address(eb->pages[0]);
5516 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5517 BTRFS_FSID_SIZE);
5518}
5519
5520void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5521{
5522 char *kaddr;
5523
5524 WARN_ON(!PageUptodate(eb->pages[0]));
5525 kaddr = page_address(eb->pages[0]);
5526 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5527 BTRFS_FSID_SIZE);
5528}
5529
Chris Masond1310b22008-01-24 16:13:08 -05005530void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5531 unsigned long start, unsigned long len)
5532{
5533 size_t cur;
5534 size_t offset;
5535 struct page *page;
5536 char *kaddr;
5537 char *src = (char *)srcv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005538 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005539 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005540
5541 WARN_ON(start > eb->len);
5542 WARN_ON(start + len > eb->start + eb->len);
5543
Johannes Thumshirn70730172018-12-05 15:23:03 +01005544 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005545
Chris Masond3977122009-01-05 21:25:51 -05005546 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005547 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005548 WARN_ON(!PageUptodate(page));
5549
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005550 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005551 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005552 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005553
5554 src += cur;
5555 len -= cur;
5556 offset = 0;
5557 i++;
5558 }
5559}
Chris Masond1310b22008-01-24 16:13:08 -05005560
David Sterbab159fa22016-11-08 18:09:03 +01005561void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5562 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005563{
5564 size_t cur;
5565 size_t offset;
5566 struct page *page;
5567 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005568 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005569 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005570
5571 WARN_ON(start > eb->len);
5572 WARN_ON(start + len > eb->start + eb->len);
5573
Johannes Thumshirn70730172018-12-05 15:23:03 +01005574 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005575
Chris Masond3977122009-01-05 21:25:51 -05005576 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005577 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005578 WARN_ON(!PageUptodate(page));
5579
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005580 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005581 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005582 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005583
5584 len -= cur;
5585 offset = 0;
5586 i++;
5587 }
5588}
Chris Masond1310b22008-01-24 16:13:08 -05005589
David Sterba58e80122016-11-08 18:30:31 +01005590void copy_extent_buffer_full(struct extent_buffer *dst,
5591 struct extent_buffer *src)
5592{
5593 int i;
David Sterbacc5e31a2018-03-01 18:20:27 +01005594 int num_pages;
David Sterba58e80122016-11-08 18:30:31 +01005595
5596 ASSERT(dst->len == src->len);
5597
David Sterba65ad0102018-06-29 10:56:49 +02005598 num_pages = num_extent_pages(dst);
David Sterba58e80122016-11-08 18:30:31 +01005599 for (i = 0; i < num_pages; i++)
5600 copy_page(page_address(dst->pages[i]),
5601 page_address(src->pages[i]));
5602}
5603
Chris Masond1310b22008-01-24 16:13:08 -05005604void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5605 unsigned long dst_offset, unsigned long src_offset,
5606 unsigned long len)
5607{
5608 u64 dst_len = dst->len;
5609 size_t cur;
5610 size_t offset;
5611 struct page *page;
5612 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005613 size_t start_offset = offset_in_page(dst->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005614 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005615
5616 WARN_ON(src->len != dst_len);
5617
Johannes Thumshirn70730172018-12-05 15:23:03 +01005618 offset = offset_in_page(start_offset + dst_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005619
Chris Masond3977122009-01-05 21:25:51 -05005620 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005621 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005622 WARN_ON(!PageUptodate(page));
5623
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005624 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005625
Chris Masona6591712011-07-19 12:04:14 -04005626 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005627 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005628
5629 src_offset += cur;
5630 len -= cur;
5631 offset = 0;
5632 i++;
5633 }
5634}
Chris Masond1310b22008-01-24 16:13:08 -05005635
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005636/*
5637 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5638 * given bit number
5639 * @eb: the extent buffer
5640 * @start: offset of the bitmap item in the extent buffer
5641 * @nr: bit number
5642 * @page_index: return index of the page in the extent buffer that contains the
5643 * given bit number
5644 * @page_offset: return offset into the page given by page_index
5645 *
5646 * This helper hides the ugliness of finding the byte in an extent buffer which
5647 * contains a given bit.
5648 */
5649static inline void eb_bitmap_offset(struct extent_buffer *eb,
5650 unsigned long start, unsigned long nr,
5651 unsigned long *page_index,
5652 size_t *page_offset)
5653{
Johannes Thumshirn70730172018-12-05 15:23:03 +01005654 size_t start_offset = offset_in_page(eb->start);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005655 size_t byte_offset = BIT_BYTE(nr);
5656 size_t offset;
5657
5658 /*
5659 * The byte we want is the offset of the extent buffer + the offset of
5660 * the bitmap item in the extent buffer + the offset of the byte in the
5661 * bitmap item.
5662 */
5663 offset = start_offset + start + byte_offset;
5664
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005665 *page_index = offset >> PAGE_SHIFT;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005666 *page_offset = offset_in_page(offset);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005667}
5668
5669/**
5670 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5671 * @eb: the extent buffer
5672 * @start: offset of the bitmap item in the extent buffer
5673 * @nr: bit number to test
5674 */
5675int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5676 unsigned long nr)
5677{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005678 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005679 struct page *page;
5680 unsigned long i;
5681 size_t offset;
5682
5683 eb_bitmap_offset(eb, start, nr, &i, &offset);
5684 page = eb->pages[i];
5685 WARN_ON(!PageUptodate(page));
5686 kaddr = page_address(page);
5687 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5688}
5689
5690/**
5691 * extent_buffer_bitmap_set - set an area of a bitmap
5692 * @eb: the extent buffer
5693 * @start: offset of the bitmap item in the extent buffer
5694 * @pos: bit number of the first bit
5695 * @len: number of bits to set
5696 */
5697void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5698 unsigned long pos, unsigned long len)
5699{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005700 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005701 struct page *page;
5702 unsigned long i;
5703 size_t offset;
5704 const unsigned int size = pos + len;
5705 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005706 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005707
5708 eb_bitmap_offset(eb, start, pos, &i, &offset);
5709 page = eb->pages[i];
5710 WARN_ON(!PageUptodate(page));
5711 kaddr = page_address(page);
5712
5713 while (len >= bits_to_set) {
5714 kaddr[offset] |= mask_to_set;
5715 len -= bits_to_set;
5716 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005717 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005718 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005719 offset = 0;
5720 page = eb->pages[++i];
5721 WARN_ON(!PageUptodate(page));
5722 kaddr = page_address(page);
5723 }
5724 }
5725 if (len) {
5726 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5727 kaddr[offset] |= mask_to_set;
5728 }
5729}
5730
5731
5732/**
5733 * extent_buffer_bitmap_clear - clear an area of a bitmap
5734 * @eb: the extent buffer
5735 * @start: offset of the bitmap item in the extent buffer
5736 * @pos: bit number of the first bit
5737 * @len: number of bits to clear
5738 */
5739void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5740 unsigned long pos, unsigned long len)
5741{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005742 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005743 struct page *page;
5744 unsigned long i;
5745 size_t offset;
5746 const unsigned int size = pos + len;
5747 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005748 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005749
5750 eb_bitmap_offset(eb, start, pos, &i, &offset);
5751 page = eb->pages[i];
5752 WARN_ON(!PageUptodate(page));
5753 kaddr = page_address(page);
5754
5755 while (len >= bits_to_clear) {
5756 kaddr[offset] &= ~mask_to_clear;
5757 len -= bits_to_clear;
5758 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005759 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005760 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005761 offset = 0;
5762 page = eb->pages[++i];
5763 WARN_ON(!PageUptodate(page));
5764 kaddr = page_address(page);
5765 }
5766 }
5767 if (len) {
5768 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5769 kaddr[offset] &= ~mask_to_clear;
5770 }
5771}
5772
Sergei Trofimovich33872062011-04-11 21:52:52 +00005773static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5774{
5775 unsigned long distance = (src > dst) ? src - dst : dst - src;
5776 return distance < len;
5777}
5778
Chris Masond1310b22008-01-24 16:13:08 -05005779static void copy_pages(struct page *dst_page, struct page *src_page,
5780 unsigned long dst_off, unsigned long src_off,
5781 unsigned long len)
5782{
Chris Masona6591712011-07-19 12:04:14 -04005783 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005784 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005785 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005786
Sergei Trofimovich33872062011-04-11 21:52:52 +00005787 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005788 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005789 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005790 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005791 if (areas_overlap(src_off, dst_off, len))
5792 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005793 }
Chris Masond1310b22008-01-24 16:13:08 -05005794
Chris Mason727011e2010-08-06 13:21:20 -04005795 if (must_memmove)
5796 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5797 else
5798 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005799}
5800
5801void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5802 unsigned long src_offset, unsigned long len)
5803{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005804 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005805 size_t cur;
5806 size_t dst_off_in_page;
5807 size_t src_off_in_page;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005808 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005809 unsigned long dst_i;
5810 unsigned long src_i;
5811
5812 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005813 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005814 "memmove bogus src_offset %lu move len %lu dst len %lu",
5815 src_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01005816 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05005817 }
5818 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005819 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005820 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5821 dst_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01005822 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05005823 }
5824
Chris Masond3977122009-01-05 21:25:51 -05005825 while (len > 0) {
Johannes Thumshirn70730172018-12-05 15:23:03 +01005826 dst_off_in_page = offset_in_page(start_offset + dst_offset);
5827 src_off_in_page = offset_in_page(start_offset + src_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005828
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005829 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5830 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005831
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005832 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005833 src_off_in_page));
5834 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005835 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005836
David Sterbafb85fc92014-07-31 01:03:53 +02005837 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005838 dst_off_in_page, src_off_in_page, cur);
5839
5840 src_offset += cur;
5841 dst_offset += cur;
5842 len -= cur;
5843 }
5844}
Chris Masond1310b22008-01-24 16:13:08 -05005845
5846void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5847 unsigned long src_offset, unsigned long len)
5848{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005849 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005850 size_t cur;
5851 size_t dst_off_in_page;
5852 size_t src_off_in_page;
5853 unsigned long dst_end = dst_offset + len - 1;
5854 unsigned long src_end = src_offset + len - 1;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005855 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005856 unsigned long dst_i;
5857 unsigned long src_i;
5858
5859 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005860 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005861 "memmove bogus src_offset %lu move len %lu len %lu",
5862 src_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01005863 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05005864 }
5865 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005866 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005867 "memmove bogus dst_offset %lu move len %lu len %lu",
5868 dst_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01005869 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05005870 }
Chris Mason727011e2010-08-06 13:21:20 -04005871 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005872 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5873 return;
5874 }
Chris Masond3977122009-01-05 21:25:51 -05005875 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005876 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
5877 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005878
Johannes Thumshirn70730172018-12-05 15:23:03 +01005879 dst_off_in_page = offset_in_page(start_offset + dst_end);
5880 src_off_in_page = offset_in_page(start_offset + src_end);
Chris Masond1310b22008-01-24 16:13:08 -05005881
5882 cur = min_t(unsigned long, len, src_off_in_page + 1);
5883 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005884 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005885 dst_off_in_page - cur + 1,
5886 src_off_in_page - cur + 1, cur);
5887
5888 dst_end -= cur;
5889 src_end -= cur;
5890 len -= cur;
5891 }
5892}
Chris Mason6af118ce2008-07-22 11:18:07 -04005893
David Sterbaf7a52a42013-04-26 14:56:29 +00005894int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005895{
Chris Mason6af118ce2008-07-22 11:18:07 -04005896 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005897
Miao Xie19fe0a82010-10-26 20:57:29 -04005898 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005899 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05005900 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005901 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005902 spin_lock(&page->mapping->private_lock);
5903 if (!PagePrivate(page)) {
5904 spin_unlock(&page->mapping->private_lock);
5905 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005906 }
5907
Josef Bacik3083ee22012-03-09 16:01:49 -05005908 eb = (struct extent_buffer *)page->private;
5909 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005910
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005911 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005912 * This is a little awful but should be ok, we need to make sure that
5913 * the eb doesn't disappear out from under us while we're looking at
5914 * this page.
5915 */
5916 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005917 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005918 spin_unlock(&eb->refs_lock);
5919 spin_unlock(&page->mapping->private_lock);
5920 return 0;
5921 }
5922 spin_unlock(&page->mapping->private_lock);
5923
Josef Bacik3083ee22012-03-09 16:01:49 -05005924 /*
5925 * If tree ref isn't set then we know the ref on this eb is a real ref,
5926 * so just return, this page will likely be freed soon anyway.
5927 */
5928 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5929 spin_unlock(&eb->refs_lock);
5930 return 0;
5931 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005932
David Sterbaf7a52a42013-04-26 14:56:29 +00005933 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005934}