blob: 048d6c6fe7b9bd815d47ee3ed3df26fa7ccad6b2 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
David Sterbac1d7c512018-04-03 19:23:33 +02002
Chris Masond1310b22008-01-24 16:13:08 -05003#include <linux/bitops.h>
4#include <linux/slab.h>
5#include <linux/bio.h>
6#include <linux/mm.h>
Chris Masond1310b22008-01-24 16:13:08 -05007#include <linux/pagemap.h>
8#include <linux/page-flags.h>
Chris Masond1310b22008-01-24 16:13:08 -05009#include <linux/spinlock.h>
10#include <linux/blkdev.h>
11#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050012#include <linux/writeback.h>
13#include <linux/pagevec.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070014#include <linux/prefetch.h>
Dan Magenheimer90a887c2011-05-26 10:01:56 -060015#include <linux/cleancache.h>
Chris Masond1310b22008-01-24 16:13:08 -050016#include "extent_io.h"
17#include "extent_map.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040018#include "ctree.h"
19#include "btrfs_inode.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020020#include "volumes.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010021#include "check-integrity.h"
Josef Bacik0b32f4b2012-03-13 09:38:00 -040022#include "locking.h"
Josef Bacik606686e2012-06-04 14:03:51 -040023#include "rcu-string.h"
Liu Bofe09e162013-09-22 12:54:23 +080024#include "backref.h"
David Sterba6af49db2017-06-23 04:09:57 +020025#include "disk-io.h"
Chris Masond1310b22008-01-24 16:13:08 -050026
Chris Masond1310b22008-01-24 16:13:08 -050027static struct kmem_cache *extent_state_cache;
28static struct kmem_cache *extent_buffer_cache;
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -040029static struct bio_set btrfs_bioset;
Chris Masond1310b22008-01-24 16:13:08 -050030
Filipe Manana27a35072014-07-06 20:09:59 +010031static inline bool extent_state_in_tree(const struct extent_state *state)
32{
33 return !RB_EMPTY_NODE(&state->rb_node);
34}
35
Eric Sandeen6d49ba12013-04-22 16:12:31 +000036#ifdef CONFIG_BTRFS_DEBUG
Chris Masond1310b22008-01-24 16:13:08 -050037static LIST_HEAD(buffers);
38static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040039
Chris Masond3977122009-01-05 21:25:51 -050040static DEFINE_SPINLOCK(leak_lock);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000041
42static inline
43void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
44{
45 unsigned long flags;
46
47 spin_lock_irqsave(&leak_lock, flags);
48 list_add(new, head);
49 spin_unlock_irqrestore(&leak_lock, flags);
50}
51
52static inline
53void btrfs_leak_debug_del(struct list_head *entry)
54{
55 unsigned long flags;
56
57 spin_lock_irqsave(&leak_lock, flags);
58 list_del(entry);
59 spin_unlock_irqrestore(&leak_lock, flags);
60}
61
62static inline
63void btrfs_leak_debug_check(void)
64{
65 struct extent_state *state;
66 struct extent_buffer *eb;
67
68 while (!list_empty(&states)) {
69 state = list_entry(states.next, struct extent_state, leak_list);
David Sterba9ee49a042015-01-14 19:52:13 +010070 pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
Filipe Manana27a35072014-07-06 20:09:59 +010071 state->start, state->end, state->state,
72 extent_state_in_tree(state),
Elena Reshetovab7ac31b2017-03-03 10:55:19 +020073 refcount_read(&state->refs));
Eric Sandeen6d49ba12013-04-22 16:12:31 +000074 list_del(&state->leak_list);
75 kmem_cache_free(extent_state_cache, state);
76 }
77
78 while (!list_empty(&buffers)) {
79 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Liu Boaf2679e2018-01-25 11:02:48 -070080 pr_err("BTRFS: buffer leak start %llu len %lu refs %d bflags %lu\n",
81 eb->start, eb->len, atomic_read(&eb->refs), eb->bflags);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000082 list_del(&eb->leak_list);
83 kmem_cache_free(extent_buffer_cache, eb);
84 }
85}
David Sterba8d599ae2013-04-30 15:22:23 +000086
Josef Bacika5dee372013-12-13 10:02:44 -050087#define btrfs_debug_check_extent_io_range(tree, start, end) \
88 __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
David Sterba8d599ae2013-04-30 15:22:23 +000089static inline void __btrfs_debug_check_extent_io_range(const char *caller,
Josef Bacika5dee372013-12-13 10:02:44 -050090 struct extent_io_tree *tree, u64 start, u64 end)
David Sterba8d599ae2013-04-30 15:22:23 +000091{
Nikolay Borisov65a680f2018-11-01 14:09:49 +020092 struct inode *inode = tree->private_data;
93 u64 isize;
94
95 if (!inode || !is_data_inode(inode))
96 return;
97
98 isize = i_size_read(inode);
99 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
100 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
101 "%s: ino %llu isize %llu odd range [%llu,%llu]",
102 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
103 }
David Sterba8d599ae2013-04-30 15:22:23 +0000104}
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000105#else
106#define btrfs_leak_debug_add(new, head) do {} while (0)
107#define btrfs_leak_debug_del(entry) do {} while (0)
108#define btrfs_leak_debug_check() do {} while (0)
David Sterba8d599ae2013-04-30 15:22:23 +0000109#define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
Chris Mason4bef0842008-09-08 11:18:08 -0400110#endif
Chris Masond1310b22008-01-24 16:13:08 -0500111
Chris Masond1310b22008-01-24 16:13:08 -0500112struct tree_entry {
113 u64 start;
114 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -0500115 struct rb_node rb_node;
116};
117
118struct extent_page_data {
119 struct bio *bio;
120 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -0500121 /* tells writepage not to lock the state bits for this range
122 * it still does the unlocking
123 */
Chris Masonffbd5172009-04-20 15:50:09 -0400124 unsigned int extent_locked:1;
125
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600126 /* tells the submit_bio code to use REQ_SYNC */
Chris Masonffbd5172009-04-20 15:50:09 -0400127 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -0500128};
129
David Sterba57599c72018-03-01 17:56:34 +0100130static int add_extent_changeset(struct extent_state *state, unsigned bits,
Qu Wenruod38ed272015-10-12 14:53:37 +0800131 struct extent_changeset *changeset,
132 int set)
133{
134 int ret;
135
136 if (!changeset)
David Sterba57599c72018-03-01 17:56:34 +0100137 return 0;
Qu Wenruod38ed272015-10-12 14:53:37 +0800138 if (set && (state->state & bits) == bits)
David Sterba57599c72018-03-01 17:56:34 +0100139 return 0;
Qu Wenruofefdc552015-10-12 15:35:38 +0800140 if (!set && (state->state & bits) == 0)
David Sterba57599c72018-03-01 17:56:34 +0100141 return 0;
Qu Wenruod38ed272015-10-12 14:53:37 +0800142 changeset->bytes_changed += state->end - state->start + 1;
David Sterba53d32352017-02-13 13:42:29 +0100143 ret = ulist_add(&changeset->range_changed, state->start, state->end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800144 GFP_ATOMIC);
David Sterba57599c72018-03-01 17:56:34 +0100145 return ret;
Qu Wenruod38ed272015-10-12 14:53:37 +0800146}
147
Qu Wenruobb58eb92019-01-25 13:09:15 +0800148static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
149 unsigned long bio_flags)
150{
151 blk_status_t ret = 0;
152 struct bio_vec *bvec = bio_last_bvec_all(bio);
Linus Torvalds80201fe2019-03-08 14:12:17 -0800153 struct bio_vec bv;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800154 struct extent_io_tree *tree = bio->bi_private;
155 u64 start;
156
Linus Torvalds80201fe2019-03-08 14:12:17 -0800157 mp_bvec_last_segment(bvec, &bv);
158 start = page_offset(bv.bv_page) + bv.bv_offset;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800159
160 bio->bi_private = NULL;
161
162 if (tree->ops)
163 ret = tree->ops->submit_bio_hook(tree->private_data, bio,
164 mirror_num, bio_flags, start);
165 else
166 btrfsic_submit_bio(bio);
167
168 return blk_status_to_errno(ret);
169}
170
Qu Wenruo30659762019-03-20 14:27:42 +0800171/* Cleanup unsubmitted bios */
172static void end_write_bio(struct extent_page_data *epd, int ret)
173{
174 if (epd->bio) {
175 epd->bio->bi_status = errno_to_blk_status(ret);
176 bio_endio(epd->bio);
177 epd->bio = NULL;
178 }
179}
180
Qu Wenruof4340622019-03-20 14:27:41 +0800181/*
182 * Submit bio from extent page data via submit_one_bio
183 *
184 * Return 0 if everything is OK.
185 * Return <0 for error.
186 */
187static int __must_check flush_write_bio(struct extent_page_data *epd)
Qu Wenruobb58eb92019-01-25 13:09:15 +0800188{
Qu Wenruof4340622019-03-20 14:27:41 +0800189 int ret = 0;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800190
Qu Wenruof4340622019-03-20 14:27:41 +0800191 if (epd->bio) {
Qu Wenruobb58eb92019-01-25 13:09:15 +0800192 ret = submit_one_bio(epd->bio, 0, 0);
Qu Wenruof4340622019-03-20 14:27:41 +0800193 /*
194 * Clean up of epd->bio is handled by its endio function.
195 * And endio is either triggered by successful bio execution
196 * or the error handler of submit bio hook.
197 * So at this point, no matter what happened, we don't need
198 * to clean up epd->bio.
199 */
Qu Wenruobb58eb92019-01-25 13:09:15 +0800200 epd->bio = NULL;
201 }
Qu Wenruof4340622019-03-20 14:27:41 +0800202 return ret;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800203}
David Sterbae2932ee2017-06-23 04:16:17 +0200204
Chris Masond1310b22008-01-24 16:13:08 -0500205int __init extent_io_init(void)
206{
David Sterba837e1972012-09-07 03:00:48 -0600207 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200208 sizeof(struct extent_state), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300209 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500210 if (!extent_state_cache)
211 return -ENOMEM;
212
David Sterba837e1972012-09-07 03:00:48 -0600213 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200214 sizeof(struct extent_buffer), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300215 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500216 if (!extent_buffer_cache)
217 goto free_state_cache;
Chris Mason9be33952013-05-17 18:30:14 -0400218
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400219 if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
220 offsetof(struct btrfs_io_bio, bio),
221 BIOSET_NEED_BVECS))
Chris Mason9be33952013-05-17 18:30:14 -0400222 goto free_buffer_cache;
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700223
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400224 if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE))
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700225 goto free_bioset;
226
Chris Masond1310b22008-01-24 16:13:08 -0500227 return 0;
228
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700229free_bioset:
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400230 bioset_exit(&btrfs_bioset);
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700231
Chris Mason9be33952013-05-17 18:30:14 -0400232free_buffer_cache:
233 kmem_cache_destroy(extent_buffer_cache);
234 extent_buffer_cache = NULL;
235
Chris Masond1310b22008-01-24 16:13:08 -0500236free_state_cache:
237 kmem_cache_destroy(extent_state_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400238 extent_state_cache = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500239 return -ENOMEM;
240}
241
David Sterbae67c7182018-02-19 17:24:18 +0100242void __cold extent_io_exit(void)
Chris Masond1310b22008-01-24 16:13:08 -0500243{
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000244 btrfs_leak_debug_check();
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000245
246 /*
247 * Make sure all delayed rcu free are flushed before we
248 * destroy caches.
249 */
250 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +0800251 kmem_cache_destroy(extent_state_cache);
252 kmem_cache_destroy(extent_buffer_cache);
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400253 bioset_exit(&btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -0500254}
255
Qu Wenruoc258d6e2019-03-01 10:47:58 +0800256void extent_io_tree_init(struct btrfs_fs_info *fs_info,
Qu Wenruo43eb5f22019-03-01 10:47:59 +0800257 struct extent_io_tree *tree, unsigned int owner,
258 void *private_data)
Chris Masond1310b22008-01-24 16:13:08 -0500259{
Qu Wenruoc258d6e2019-03-01 10:47:58 +0800260 tree->fs_info = fs_info;
Eric Paris6bef4d32010-02-23 19:43:04 +0000261 tree->state = RB_ROOT;
Chris Masond1310b22008-01-24 16:13:08 -0500262 tree->ops = NULL;
263 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500264 spin_lock_init(&tree->lock);
Josef Bacikc6100a42017-05-05 11:57:13 -0400265 tree->private_data = private_data;
Qu Wenruo43eb5f22019-03-01 10:47:59 +0800266 tree->owner = owner;
Chris Masond1310b22008-01-24 16:13:08 -0500267}
Chris Masond1310b22008-01-24 16:13:08 -0500268
Nikolay Borisov41e7acd2019-03-25 14:31:24 +0200269void extent_io_tree_release(struct extent_io_tree *tree)
270{
271 spin_lock(&tree->lock);
272 /*
273 * Do a single barrier for the waitqueue_active check here, the state
274 * of the waitqueue should not change once extent_io_tree_release is
275 * called.
276 */
277 smp_mb();
278 while (!RB_EMPTY_ROOT(&tree->state)) {
279 struct rb_node *node;
280 struct extent_state *state;
281
282 node = rb_first(&tree->state);
283 state = rb_entry(node, struct extent_state, rb_node);
284 rb_erase(&state->rb_node, &tree->state);
285 RB_CLEAR_NODE(&state->rb_node);
286 /*
287 * btree io trees aren't supposed to have tasks waiting for
288 * changes in the flags of extent states ever.
289 */
290 ASSERT(!waitqueue_active(&state->wq));
291 free_extent_state(state);
292
293 cond_resched_lock(&tree->lock);
294 }
295 spin_unlock(&tree->lock);
296}
297
Christoph Hellwigb2950862008-12-02 09:54:17 -0500298static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500299{
300 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500301
Michal Hocko3ba7ab22017-01-09 15:39:02 +0100302 /*
303 * The given mask might be not appropriate for the slab allocator,
304 * drop the unsupported bits
305 */
306 mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
Chris Masond1310b22008-01-24 16:13:08 -0500307 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400308 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500309 return state;
310 state->state = 0;
David Sterba47dc1962016-02-11 13:24:13 +0100311 state->failrec = NULL;
Filipe Manana27a35072014-07-06 20:09:59 +0100312 RB_CLEAR_NODE(&state->rb_node);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000313 btrfs_leak_debug_add(&state->leak_list, &states);
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200314 refcount_set(&state->refs, 1);
Chris Masond1310b22008-01-24 16:13:08 -0500315 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100316 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500317 return state;
318}
Chris Masond1310b22008-01-24 16:13:08 -0500319
Chris Mason4845e442010-05-25 20:56:50 -0400320void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500321{
Chris Masond1310b22008-01-24 16:13:08 -0500322 if (!state)
323 return;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200324 if (refcount_dec_and_test(&state->refs)) {
Filipe Manana27a35072014-07-06 20:09:59 +0100325 WARN_ON(extent_state_in_tree(state));
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000326 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100327 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500328 kmem_cache_free(extent_state_cache, state);
329 }
330}
Chris Masond1310b22008-01-24 16:13:08 -0500331
Filipe Mananaf2071b22014-02-12 15:05:53 +0000332static struct rb_node *tree_insert(struct rb_root *root,
333 struct rb_node *search_start,
334 u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000335 struct rb_node *node,
336 struct rb_node ***p_in,
337 struct rb_node **parent_in)
Chris Masond1310b22008-01-24 16:13:08 -0500338{
Filipe Mananaf2071b22014-02-12 15:05:53 +0000339 struct rb_node **p;
Chris Masond3977122009-01-05 21:25:51 -0500340 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500341 struct tree_entry *entry;
342
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000343 if (p_in && parent_in) {
344 p = *p_in;
345 parent = *parent_in;
346 goto do_insert;
347 }
348
Filipe Mananaf2071b22014-02-12 15:05:53 +0000349 p = search_start ? &search_start : &root->rb_node;
Chris Masond3977122009-01-05 21:25:51 -0500350 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500351 parent = *p;
352 entry = rb_entry(parent, struct tree_entry, rb_node);
353
354 if (offset < entry->start)
355 p = &(*p)->rb_left;
356 else if (offset > entry->end)
357 p = &(*p)->rb_right;
358 else
359 return parent;
360 }
361
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000362do_insert:
Chris Masond1310b22008-01-24 16:13:08 -0500363 rb_link_node(node, parent, p);
364 rb_insert_color(node, root);
365 return NULL;
366}
367
Chris Mason80ea96b2008-02-01 14:51:59 -0500368static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000369 struct rb_node **next_ret,
Nikolay Borisov352646c2019-01-30 16:51:00 +0200370 struct rb_node **prev_ret,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000371 struct rb_node ***p_ret,
372 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500373{
Chris Mason80ea96b2008-02-01 14:51:59 -0500374 struct rb_root *root = &tree->state;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000375 struct rb_node **n = &root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500376 struct rb_node *prev = NULL;
377 struct rb_node *orig_prev = NULL;
378 struct tree_entry *entry;
379 struct tree_entry *prev_entry = NULL;
380
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000381 while (*n) {
382 prev = *n;
383 entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500384 prev_entry = entry;
385
386 if (offset < entry->start)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000387 n = &(*n)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500388 else if (offset > entry->end)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000389 n = &(*n)->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500390 else
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000391 return *n;
Chris Masond1310b22008-01-24 16:13:08 -0500392 }
393
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000394 if (p_ret)
395 *p_ret = n;
396 if (parent_ret)
397 *parent_ret = prev;
398
Nikolay Borisov352646c2019-01-30 16:51:00 +0200399 if (next_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500400 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500401 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500402 prev = rb_next(prev);
403 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
404 }
Nikolay Borisov352646c2019-01-30 16:51:00 +0200405 *next_ret = prev;
Chris Masond1310b22008-01-24 16:13:08 -0500406 prev = orig_prev;
407 }
408
Nikolay Borisov352646c2019-01-30 16:51:00 +0200409 if (prev_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500410 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500411 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500412 prev = rb_prev(prev);
413 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
414 }
Nikolay Borisov352646c2019-01-30 16:51:00 +0200415 *prev_ret = prev;
Chris Masond1310b22008-01-24 16:13:08 -0500416 }
417 return NULL;
418}
419
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000420static inline struct rb_node *
421tree_search_for_insert(struct extent_io_tree *tree,
422 u64 offset,
423 struct rb_node ***p_ret,
424 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500425{
Nikolay Borisov352646c2019-01-30 16:51:00 +0200426 struct rb_node *next= NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500427 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500428
Nikolay Borisov352646c2019-01-30 16:51:00 +0200429 ret = __etree_search(tree, offset, &next, NULL, p_ret, parent_ret);
Chris Masond3977122009-01-05 21:25:51 -0500430 if (!ret)
Nikolay Borisov352646c2019-01-30 16:51:00 +0200431 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500432 return ret;
433}
434
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000435static inline struct rb_node *tree_search(struct extent_io_tree *tree,
436 u64 offset)
437{
438 return tree_search_for_insert(tree, offset, NULL, NULL);
439}
440
Chris Masond1310b22008-01-24 16:13:08 -0500441/*
442 * utility function to look for merge candidates inside a given range.
443 * Any extents with matching state are merged together into a single
444 * extent in the tree. Extents with EXTENT_IO in their state field
445 * are not merged because the end_io handlers need to be able to do
446 * operations on them without sleeping (or doing allocations/splits).
447 *
448 * This should be called with the tree lock held.
449 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000450static void merge_state(struct extent_io_tree *tree,
451 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500452{
453 struct extent_state *other;
454 struct rb_node *other_node;
455
Nikolay Borisov88826792019-03-14 15:28:31 +0200456 if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000457 return;
Chris Masond1310b22008-01-24 16:13:08 -0500458
459 other_node = rb_prev(&state->rb_node);
460 if (other_node) {
461 other = rb_entry(other_node, struct extent_state, rb_node);
462 if (other->end == state->start - 1 &&
463 other->state == state->state) {
Nikolay Borisov5c848192018-11-01 14:09:52 +0200464 if (tree->private_data &&
465 is_data_inode(tree->private_data))
466 btrfs_merge_delalloc_extent(tree->private_data,
467 state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500468 state->start = other->start;
Chris Masond1310b22008-01-24 16:13:08 -0500469 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100470 RB_CLEAR_NODE(&other->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500471 free_extent_state(other);
472 }
473 }
474 other_node = rb_next(&state->rb_node);
475 if (other_node) {
476 other = rb_entry(other_node, struct extent_state, rb_node);
477 if (other->start == state->end + 1 &&
478 other->state == state->state) {
Nikolay Borisov5c848192018-11-01 14:09:52 +0200479 if (tree->private_data &&
480 is_data_inode(tree->private_data))
481 btrfs_merge_delalloc_extent(tree->private_data,
482 state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400483 state->end = other->end;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400484 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100485 RB_CLEAR_NODE(&other->rb_node);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400486 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500487 }
488 }
Chris Masond1310b22008-01-24 16:13:08 -0500489}
490
Xiao Guangrong3150b692011-07-14 03:19:08 +0000491static void set_state_bits(struct extent_io_tree *tree,
Qu Wenruod38ed272015-10-12 14:53:37 +0800492 struct extent_state *state, unsigned *bits,
493 struct extent_changeset *changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000494
Chris Masond1310b22008-01-24 16:13:08 -0500495/*
496 * insert an extent_state struct into the tree. 'bits' are set on the
497 * struct before it is inserted.
498 *
499 * This may return -EEXIST if the extent is already there, in which case the
500 * state struct is freed.
501 *
502 * The tree lock is not taken internally. This is a utility function and
503 * probably isn't what you want to call (see set/clear_extent_bit).
504 */
505static int insert_state(struct extent_io_tree *tree,
506 struct extent_state *state, u64 start, u64 end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000507 struct rb_node ***p,
508 struct rb_node **parent,
Qu Wenruod38ed272015-10-12 14:53:37 +0800509 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500510{
511 struct rb_node *node;
512
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000513 if (end < start)
Frank Holtonefe120a2013-12-20 11:37:06 -0500514 WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200515 end, start);
Chris Masond1310b22008-01-24 16:13:08 -0500516 state->start = start;
517 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400518
Qu Wenruod38ed272015-10-12 14:53:37 +0800519 set_state_bits(tree, state, bits, changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000520
Filipe Mananaf2071b22014-02-12 15:05:53 +0000521 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
Chris Masond1310b22008-01-24 16:13:08 -0500522 if (node) {
523 struct extent_state *found;
524 found = rb_entry(node, struct extent_state, rb_node);
Jeff Mahoney62e85572016-09-20 10:05:01 -0400525 pr_err("BTRFS: found node %llu %llu on insert of %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200526 found->start, found->end, start, end);
Chris Masond1310b22008-01-24 16:13:08 -0500527 return -EEXIST;
528 }
529 merge_state(tree, state);
530 return 0;
531}
532
533/*
534 * split a given extent state struct in two, inserting the preallocated
535 * struct 'prealloc' as the newly created second half. 'split' indicates an
536 * offset inside 'orig' where it should be split.
537 *
538 * Before calling,
539 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
540 * are two extent state structs in the tree:
541 * prealloc: [orig->start, split - 1]
542 * orig: [ split, orig->end ]
543 *
544 * The tree locks are not taken by this function. They need to be held
545 * by the caller.
546 */
547static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
548 struct extent_state *prealloc, u64 split)
549{
550 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400551
Nikolay Borisovabbb55f2018-11-01 14:09:53 +0200552 if (tree->private_data && is_data_inode(tree->private_data))
553 btrfs_split_delalloc_extent(tree->private_data, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400554
Chris Masond1310b22008-01-24 16:13:08 -0500555 prealloc->start = orig->start;
556 prealloc->end = split - 1;
557 prealloc->state = orig->state;
558 orig->start = split;
559
Filipe Mananaf2071b22014-02-12 15:05:53 +0000560 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
561 &prealloc->rb_node, NULL, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500562 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500563 free_extent_state(prealloc);
564 return -EEXIST;
565 }
566 return 0;
567}
568
Li Zefancdc6a392012-03-12 16:39:48 +0800569static struct extent_state *next_state(struct extent_state *state)
570{
571 struct rb_node *next = rb_next(&state->rb_node);
572 if (next)
573 return rb_entry(next, struct extent_state, rb_node);
574 else
575 return NULL;
576}
577
Chris Masond1310b22008-01-24 16:13:08 -0500578/*
579 * utility function to clear some bits in an extent state struct.
Andrea Gelmini52042d82018-11-28 12:05:13 +0100580 * it will optionally wake up anyone waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500581 *
582 * If no bits are set on the state struct after clearing things, the
583 * struct is freed and removed from the tree
584 */
Li Zefancdc6a392012-03-12 16:39:48 +0800585static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
586 struct extent_state *state,
Qu Wenruofefdc552015-10-12 15:35:38 +0800587 unsigned *bits, int wake,
588 struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500589{
Li Zefancdc6a392012-03-12 16:39:48 +0800590 struct extent_state *next;
David Sterba9ee49a042015-01-14 19:52:13 +0100591 unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100592 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500593
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400594 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500595 u64 range = state->end - state->start + 1;
596 WARN_ON(range > tree->dirty_bytes);
597 tree->dirty_bytes -= range;
598 }
Nikolay Borisova36bb5f2018-11-01 14:09:51 +0200599
600 if (tree->private_data && is_data_inode(tree->private_data))
601 btrfs_clear_delalloc_extent(tree->private_data, state, bits);
602
David Sterba57599c72018-03-01 17:56:34 +0100603 ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
604 BUG_ON(ret < 0);
Josef Bacik32c00af2009-10-08 13:34:05 -0400605 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500606 if (wake)
607 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400608 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800609 next = next_state(state);
Filipe Manana27a35072014-07-06 20:09:59 +0100610 if (extent_state_in_tree(state)) {
Chris Masond1310b22008-01-24 16:13:08 -0500611 rb_erase(&state->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100612 RB_CLEAR_NODE(&state->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500613 free_extent_state(state);
614 } else {
615 WARN_ON(1);
616 }
617 } else {
618 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800619 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500620 }
Li Zefancdc6a392012-03-12 16:39:48 +0800621 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500622}
623
Xiao Guangrong82337672011-04-20 06:44:57 +0000624static struct extent_state *
625alloc_extent_state_atomic(struct extent_state *prealloc)
626{
627 if (!prealloc)
628 prealloc = alloc_extent_state(GFP_ATOMIC);
629
630 return prealloc;
631}
632
Eric Sandeen48a3b632013-04-25 20:41:01 +0000633static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400634{
David Sterba05912a32018-07-18 19:23:45 +0200635 struct inode *inode = tree->private_data;
636
637 btrfs_panic(btrfs_sb(inode->i_sb), err,
638 "locking error: extent tree was modified by another thread while locked");
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400639}
640
Chris Masond1310b22008-01-24 16:13:08 -0500641/*
642 * clear some bits on a range in the tree. This may require splitting
643 * or inserting elements in the tree, so the gfp mask is used to
644 * indicate which allocations or sleeping are allowed.
645 *
646 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
647 * the given range from the tree regardless of state (ie for truncate).
648 *
649 * the range [start, end] is inclusive.
650 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100651 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500652 */
David Sterba66b0c882017-10-31 16:30:47 +0100653int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Qu Wenruofefdc552015-10-12 15:35:38 +0800654 unsigned bits, int wake, int delete,
655 struct extent_state **cached_state,
656 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500657{
658 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400659 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500660 struct extent_state *prealloc = NULL;
661 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400662 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500663 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000664 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500665
Josef Bacika5dee372013-12-13 10:02:44 -0500666 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +0800667 trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits);
David Sterba8d599ae2013-04-30 15:22:23 +0000668
Josef Bacik7ee9e442013-06-21 16:37:03 -0400669 if (bits & EXTENT_DELALLOC)
670 bits |= EXTENT_NORESERVE;
671
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400672 if (delete)
673 bits |= ~EXTENT_CTLBITS;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400674
Nikolay Borisov88826792019-03-14 15:28:31 +0200675 if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY))
Josef Bacik2ac55d42010-02-03 19:33:23 +0000676 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500677again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800678 if (!prealloc && gfpflags_allow_blocking(mask)) {
Filipe Mananac7bc6312014-11-03 14:12:57 +0000679 /*
680 * Don't care for allocation failure here because we might end
681 * up not needing the pre-allocated extent state at all, which
682 * is the case if we only have in the tree extent states that
683 * cover our input range and don't cover too any other range.
684 * If we end up needing a new extent state we allocate it later.
685 */
Chris Masond1310b22008-01-24 16:13:08 -0500686 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500687 }
688
Chris Masoncad321a2008-12-17 14:51:42 -0500689 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400690 if (cached_state) {
691 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000692
693 if (clear) {
694 *cached_state = NULL;
695 cached_state = NULL;
696 }
697
Filipe Manana27a35072014-07-06 20:09:59 +0100698 if (cached && extent_state_in_tree(cached) &&
699 cached->start <= start && cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000700 if (clear)
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200701 refcount_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400702 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400703 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400704 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000705 if (clear)
706 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400707 }
Chris Masond1310b22008-01-24 16:13:08 -0500708 /*
709 * this search will find the extents that end after
710 * our range starts
711 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500712 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500713 if (!node)
714 goto out;
715 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400716hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500717 if (state->start > end)
718 goto out;
719 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400720 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500721
Liu Bo04493142012-02-16 18:34:37 +0800722 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800723 if (!(state->state & bits)) {
724 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800725 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800726 }
Liu Bo04493142012-02-16 18:34:37 +0800727
Chris Masond1310b22008-01-24 16:13:08 -0500728 /*
729 * | ---- desired range ---- |
730 * | state | or
731 * | ------------- state -------------- |
732 *
733 * We need to split the extent we found, and may flip
734 * bits on second half.
735 *
736 * If the extent we found extends past our range, we
737 * just split and search again. It'll get split again
738 * the next time though.
739 *
740 * If the extent we found is inside our range, we clear
741 * the desired bit on it.
742 */
743
744 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000745 prealloc = alloc_extent_state_atomic(prealloc);
746 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500747 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400748 if (err)
749 extent_io_tree_panic(tree, err);
750
Chris Masond1310b22008-01-24 16:13:08 -0500751 prealloc = NULL;
752 if (err)
753 goto out;
754 if (state->end <= end) {
Qu Wenruofefdc552015-10-12 15:35:38 +0800755 state = clear_state_bit(tree, state, &bits, wake,
756 changeset);
Liu Bod1ac6e42012-05-10 18:10:39 +0800757 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500758 }
759 goto search_again;
760 }
761 /*
762 * | ---- desired range ---- |
763 * | state |
764 * We need to split the extent, and clear the bit
765 * on the first half
766 */
767 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000768 prealloc = alloc_extent_state_atomic(prealloc);
769 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500770 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400771 if (err)
772 extent_io_tree_panic(tree, err);
773
Chris Masond1310b22008-01-24 16:13:08 -0500774 if (wake)
775 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400776
Qu Wenruofefdc552015-10-12 15:35:38 +0800777 clear_state_bit(tree, prealloc, &bits, wake, changeset);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400778
Chris Masond1310b22008-01-24 16:13:08 -0500779 prealloc = NULL;
780 goto out;
781 }
Chris Mason42daec22009-09-23 19:51:09 -0400782
Qu Wenruofefdc552015-10-12 15:35:38 +0800783 state = clear_state_bit(tree, state, &bits, wake, changeset);
Liu Bo04493142012-02-16 18:34:37 +0800784next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400785 if (last_end == (u64)-1)
786 goto out;
787 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800788 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800789 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500790
791search_again:
792 if (start > end)
793 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500794 spin_unlock(&tree->lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800795 if (gfpflags_allow_blocking(mask))
Chris Masond1310b22008-01-24 16:13:08 -0500796 cond_resched();
797 goto again;
David Sterba7ab5cb22016-04-27 01:02:15 +0200798
799out:
800 spin_unlock(&tree->lock);
801 if (prealloc)
802 free_extent_state(prealloc);
803
804 return 0;
805
Chris Masond1310b22008-01-24 16:13:08 -0500806}
Chris Masond1310b22008-01-24 16:13:08 -0500807
Jeff Mahoney143bede2012-03-01 14:56:26 +0100808static void wait_on_state(struct extent_io_tree *tree,
809 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500810 __releases(tree->lock)
811 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500812{
813 DEFINE_WAIT(wait);
814 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500815 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500816 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500817 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500818 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500819}
820
821/*
822 * waits for one or more bits to clear on a range in the state tree.
823 * The range [start, end] is inclusive.
824 * The tree lock is taken by this function
825 */
David Sterba41074882013-04-29 13:38:46 +0000826static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
827 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500828{
829 struct extent_state *state;
830 struct rb_node *node;
831
Josef Bacika5dee372013-12-13 10:02:44 -0500832 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000833
Chris Masoncad321a2008-12-17 14:51:42 -0500834 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500835again:
836 while (1) {
837 /*
838 * this search will find all the extents that end after
839 * our range starts
840 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500841 node = tree_search(tree, start);
Filipe Mananac50d3e72014-03-31 14:53:25 +0100842process_node:
Chris Masond1310b22008-01-24 16:13:08 -0500843 if (!node)
844 break;
845
846 state = rb_entry(node, struct extent_state, rb_node);
847
848 if (state->start > end)
849 goto out;
850
851 if (state->state & bits) {
852 start = state->start;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200853 refcount_inc(&state->refs);
Chris Masond1310b22008-01-24 16:13:08 -0500854 wait_on_state(tree, state);
855 free_extent_state(state);
856 goto again;
857 }
858 start = state->end + 1;
859
860 if (start > end)
861 break;
862
Filipe Mananac50d3e72014-03-31 14:53:25 +0100863 if (!cond_resched_lock(&tree->lock)) {
864 node = rb_next(node);
865 goto process_node;
866 }
Chris Masond1310b22008-01-24 16:13:08 -0500867 }
868out:
Chris Masoncad321a2008-12-17 14:51:42 -0500869 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500870}
Chris Masond1310b22008-01-24 16:13:08 -0500871
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000872static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500873 struct extent_state *state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800874 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500875{
David Sterba9ee49a042015-01-14 19:52:13 +0100876 unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100877 int ret;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400878
Nikolay Borisove06a1fc2018-11-01 14:09:50 +0200879 if (tree->private_data && is_data_inode(tree->private_data))
880 btrfs_set_delalloc_extent(tree->private_data, state, bits);
881
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400882 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500883 u64 range = state->end - state->start + 1;
884 tree->dirty_bytes += range;
885 }
David Sterba57599c72018-03-01 17:56:34 +0100886 ret = add_extent_changeset(state, bits_to_set, changeset, 1);
887 BUG_ON(ret < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400888 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500889}
890
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100891static void cache_state_if_flags(struct extent_state *state,
892 struct extent_state **cached_ptr,
David Sterba9ee49a042015-01-14 19:52:13 +0100893 unsigned flags)
Chris Mason2c64c532009-09-02 15:04:12 -0400894{
895 if (cached_ptr && !(*cached_ptr)) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100896 if (!flags || (state->state & flags)) {
Chris Mason2c64c532009-09-02 15:04:12 -0400897 *cached_ptr = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200898 refcount_inc(&state->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400899 }
900 }
901}
902
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100903static void cache_state(struct extent_state *state,
904 struct extent_state **cached_ptr)
905{
906 return cache_state_if_flags(state, cached_ptr,
Nikolay Borisov88826792019-03-14 15:28:31 +0200907 EXTENT_LOCKED | EXTENT_BOUNDARY);
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100908}
909
Chris Masond1310b22008-01-24 16:13:08 -0500910/*
Chris Mason1edbb732009-09-02 13:24:36 -0400911 * set some bits on a range in the tree. This may require allocations or
912 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500913 *
Chris Mason1edbb732009-09-02 13:24:36 -0400914 * If any of the exclusive bits are set, this will fail with -EEXIST if some
915 * part of the range already has the desired bits set. The start of the
916 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500917 *
Chris Mason1edbb732009-09-02 13:24:36 -0400918 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500919 */
Chris Mason1edbb732009-09-02 13:24:36 -0400920
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100921static int __must_check
922__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +0100923 unsigned bits, unsigned exclusive_bits,
David Sterba41074882013-04-29 13:38:46 +0000924 u64 *failed_start, struct extent_state **cached_state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800925 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500926{
927 struct extent_state *state;
928 struct extent_state *prealloc = NULL;
929 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000930 struct rb_node **p;
931 struct rb_node *parent;
Chris Masond1310b22008-01-24 16:13:08 -0500932 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500933 u64 last_start;
934 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400935
Josef Bacika5dee372013-12-13 10:02:44 -0500936 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +0800937 trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits);
David Sterba8d599ae2013-04-30 15:22:23 +0000938
Chris Masond1310b22008-01-24 16:13:08 -0500939again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800940 if (!prealloc && gfpflags_allow_blocking(mask)) {
David Sterba059f7912016-04-27 01:03:45 +0200941 /*
942 * Don't care for allocation failure here because we might end
943 * up not needing the pre-allocated extent state at all, which
944 * is the case if we only have in the tree extent states that
945 * cover our input range and don't cover too any other range.
946 * If we end up needing a new extent state we allocate it later.
947 */
Chris Masond1310b22008-01-24 16:13:08 -0500948 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500949 }
950
Chris Masoncad321a2008-12-17 14:51:42 -0500951 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400952 if (cached_state && *cached_state) {
953 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400954 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +0100955 extent_state_in_tree(state)) {
Chris Mason9655d292009-09-02 15:22:30 -0400956 node = &state->rb_node;
957 goto hit_next;
958 }
959 }
Chris Masond1310b22008-01-24 16:13:08 -0500960 /*
961 * this search will find all the extents that end after
962 * our range starts.
963 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000964 node = tree_search_for_insert(tree, start, &p, &parent);
Chris Masond1310b22008-01-24 16:13:08 -0500965 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000966 prealloc = alloc_extent_state_atomic(prealloc);
967 BUG_ON(!prealloc);
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000968 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800969 &p, &parent, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400970 if (err)
971 extent_io_tree_panic(tree, err);
972
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +0000973 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500974 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500975 goto out;
976 }
Chris Masond1310b22008-01-24 16:13:08 -0500977 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400978hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500979 last_start = state->start;
980 last_end = state->end;
981
982 /*
983 * | ---- desired range ---- |
984 * | state |
985 *
986 * Just lock what we found and keep going
987 */
988 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400989 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500990 *failed_start = state->start;
991 err = -EEXIST;
992 goto out;
993 }
Chris Mason42daec22009-09-23 19:51:09 -0400994
Qu Wenruod38ed272015-10-12 14:53:37 +0800995 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400996 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500997 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400998 if (last_end == (u64)-1)
999 goto out;
1000 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001001 state = next_state(state);
1002 if (start < end && state && state->start == start &&
1003 !need_resched())
1004 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -05001005 goto search_again;
1006 }
1007
1008 /*
1009 * | ---- desired range ---- |
1010 * | state |
1011 * or
1012 * | ------------- state -------------- |
1013 *
1014 * We need to split the extent we found, and may flip bits on
1015 * second half.
1016 *
1017 * If the extent we found extends past our
1018 * range, we just split and search again. It'll get split
1019 * again the next time though.
1020 *
1021 * If the extent we found is inside our range, we set the
1022 * desired bit on it.
1023 */
1024 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -04001025 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001026 *failed_start = start;
1027 err = -EEXIST;
1028 goto out;
1029 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001030
1031 prealloc = alloc_extent_state_atomic(prealloc);
1032 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001033 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001034 if (err)
1035 extent_io_tree_panic(tree, err);
1036
Chris Masond1310b22008-01-24 16:13:08 -05001037 prealloc = NULL;
1038 if (err)
1039 goto out;
1040 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001041 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001042 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001043 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -04001044 if (last_end == (u64)-1)
1045 goto out;
1046 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001047 state = next_state(state);
1048 if (start < end && state && state->start == start &&
1049 !need_resched())
1050 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -05001051 }
1052 goto search_again;
1053 }
1054 /*
1055 * | ---- desired range ---- |
1056 * | state | or | state |
1057 *
1058 * There's a hole, we need to insert something in it and
1059 * ignore the extent we found.
1060 */
1061 if (state->start > start) {
1062 u64 this_end;
1063 if (end < last_start)
1064 this_end = end;
1065 else
Chris Masond3977122009-01-05 21:25:51 -05001066 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +00001067
1068 prealloc = alloc_extent_state_atomic(prealloc);
1069 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +00001070
1071 /*
1072 * Avoid to free 'prealloc' if it can be merged with
1073 * the later extent.
1074 */
Chris Masond1310b22008-01-24 16:13:08 -05001075 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001076 NULL, NULL, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001077 if (err)
1078 extent_io_tree_panic(tree, err);
1079
Chris Mason2c64c532009-09-02 15:04:12 -04001080 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001081 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001082 start = this_end + 1;
1083 goto search_again;
1084 }
1085 /*
1086 * | ---- desired range ---- |
1087 * | state |
1088 * We need to split the extent, and set the bit
1089 * on the first half
1090 */
1091 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -04001092 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001093 *failed_start = start;
1094 err = -EEXIST;
1095 goto out;
1096 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001097
1098 prealloc = alloc_extent_state_atomic(prealloc);
1099 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001100 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001101 if (err)
1102 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -05001103
Qu Wenruod38ed272015-10-12 14:53:37 +08001104 set_state_bits(tree, prealloc, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001105 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001106 merge_state(tree, prealloc);
1107 prealloc = NULL;
1108 goto out;
1109 }
1110
David Sterbab5a4ba142016-04-27 01:02:15 +02001111search_again:
1112 if (start > end)
1113 goto out;
1114 spin_unlock(&tree->lock);
1115 if (gfpflags_allow_blocking(mask))
1116 cond_resched();
1117 goto again;
Chris Masond1310b22008-01-24 16:13:08 -05001118
1119out:
Chris Masoncad321a2008-12-17 14:51:42 -05001120 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001121 if (prealloc)
1122 free_extent_state(prealloc);
1123
1124 return err;
1125
Chris Masond1310b22008-01-24 16:13:08 -05001126}
Chris Masond1310b22008-01-24 16:13:08 -05001127
David Sterba41074882013-04-29 13:38:46 +00001128int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001129 unsigned bits, u64 * failed_start,
David Sterba41074882013-04-29 13:38:46 +00001130 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001131{
1132 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001133 cached_state, mask, NULL);
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001134}
1135
1136
Josef Bacik462d6fa2011-09-26 13:56:12 -04001137/**
Liu Bo10983f22012-07-11 15:26:19 +08001138 * convert_extent_bit - convert all bits in a given range from one bit to
1139 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -04001140 * @tree: the io tree to search
1141 * @start: the start offset in bytes
1142 * @end: the end offset in bytes (inclusive)
1143 * @bits: the bits to set in this range
1144 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001145 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001146 *
1147 * This will go through and set bits for the given range. If any states exist
1148 * already in this range they are set with the given bit and cleared of the
1149 * clear_bits. This is only meant to be used by things that are mergeable, ie
1150 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1151 * boundary bits like LOCK.
David Sterba210aa272016-04-26 23:54:39 +02001152 *
1153 * All allocations are done with GFP_NOFS.
Josef Bacik462d6fa2011-09-26 13:56:12 -04001154 */
1155int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001156 unsigned bits, unsigned clear_bits,
David Sterba210aa272016-04-26 23:54:39 +02001157 struct extent_state **cached_state)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001158{
1159 struct extent_state *state;
1160 struct extent_state *prealloc = NULL;
1161 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001162 struct rb_node **p;
1163 struct rb_node *parent;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001164 int err = 0;
1165 u64 last_start;
1166 u64 last_end;
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001167 bool first_iteration = true;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001168
Josef Bacika5dee372013-12-13 10:02:44 -05001169 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +08001170 trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits,
1171 clear_bits);
David Sterba8d599ae2013-04-30 15:22:23 +00001172
Josef Bacik462d6fa2011-09-26 13:56:12 -04001173again:
David Sterba210aa272016-04-26 23:54:39 +02001174 if (!prealloc) {
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001175 /*
1176 * Best effort, don't worry if extent state allocation fails
1177 * here for the first iteration. We might have a cached state
1178 * that matches exactly the target range, in which case no
1179 * extent state allocations are needed. We'll only know this
1180 * after locking the tree.
1181 */
David Sterba210aa272016-04-26 23:54:39 +02001182 prealloc = alloc_extent_state(GFP_NOFS);
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001183 if (!prealloc && !first_iteration)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001184 return -ENOMEM;
1185 }
1186
1187 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001188 if (cached_state && *cached_state) {
1189 state = *cached_state;
1190 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +01001191 extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001192 node = &state->rb_node;
1193 goto hit_next;
1194 }
1195 }
1196
Josef Bacik462d6fa2011-09-26 13:56:12 -04001197 /*
1198 * this search will find all the extents that end after
1199 * our range starts.
1200 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001201 node = tree_search_for_insert(tree, start, &p, &parent);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001202 if (!node) {
1203 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001204 if (!prealloc) {
1205 err = -ENOMEM;
1206 goto out;
1207 }
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001208 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001209 &p, &parent, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001210 if (err)
1211 extent_io_tree_panic(tree, err);
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001212 cache_state(prealloc, cached_state);
1213 prealloc = NULL;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001214 goto out;
1215 }
1216 state = rb_entry(node, struct extent_state, rb_node);
1217hit_next:
1218 last_start = state->start;
1219 last_end = state->end;
1220
1221 /*
1222 * | ---- desired range ---- |
1223 * | state |
1224 *
1225 * Just lock what we found and keep going
1226 */
1227 if (state->start == start && state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001228 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001229 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001230 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001231 if (last_end == (u64)-1)
1232 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001233 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001234 if (start < end && state && state->start == start &&
1235 !need_resched())
1236 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001237 goto search_again;
1238 }
1239
1240 /*
1241 * | ---- desired range ---- |
1242 * | state |
1243 * or
1244 * | ------------- state -------------- |
1245 *
1246 * We need to split the extent we found, and may flip bits on
1247 * second half.
1248 *
1249 * If the extent we found extends past our
1250 * range, we just split and search again. It'll get split
1251 * again the next time though.
1252 *
1253 * If the extent we found is inside our range, we set the
1254 * desired bit on it.
1255 */
1256 if (state->start < start) {
1257 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001258 if (!prealloc) {
1259 err = -ENOMEM;
1260 goto out;
1261 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001262 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001263 if (err)
1264 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001265 prealloc = NULL;
1266 if (err)
1267 goto out;
1268 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001269 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001270 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001271 state = clear_state_bit(tree, state, &clear_bits, 0,
1272 NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001273 if (last_end == (u64)-1)
1274 goto out;
1275 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001276 if (start < end && state && state->start == start &&
1277 !need_resched())
1278 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001279 }
1280 goto search_again;
1281 }
1282 /*
1283 * | ---- desired range ---- |
1284 * | state | or | state |
1285 *
1286 * There's a hole, we need to insert something in it and
1287 * ignore the extent we found.
1288 */
1289 if (state->start > start) {
1290 u64 this_end;
1291 if (end < last_start)
1292 this_end = end;
1293 else
1294 this_end = last_start - 1;
1295
1296 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001297 if (!prealloc) {
1298 err = -ENOMEM;
1299 goto out;
1300 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001301
1302 /*
1303 * Avoid to free 'prealloc' if it can be merged with
1304 * the later extent.
1305 */
1306 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001307 NULL, NULL, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001308 if (err)
1309 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001310 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001311 prealloc = NULL;
1312 start = this_end + 1;
1313 goto search_again;
1314 }
1315 /*
1316 * | ---- desired range ---- |
1317 * | state |
1318 * We need to split the extent, and set the bit
1319 * on the first half
1320 */
1321 if (state->start <= end && state->end > end) {
1322 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001323 if (!prealloc) {
1324 err = -ENOMEM;
1325 goto out;
1326 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001327
1328 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001329 if (err)
1330 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001331
Qu Wenruod38ed272015-10-12 14:53:37 +08001332 set_state_bits(tree, prealloc, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001333 cache_state(prealloc, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001334 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001335 prealloc = NULL;
1336 goto out;
1337 }
1338
Josef Bacik462d6fa2011-09-26 13:56:12 -04001339search_again:
1340 if (start > end)
1341 goto out;
1342 spin_unlock(&tree->lock);
David Sterba210aa272016-04-26 23:54:39 +02001343 cond_resched();
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001344 first_iteration = false;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001345 goto again;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001346
1347out:
1348 spin_unlock(&tree->lock);
1349 if (prealloc)
1350 free_extent_state(prealloc);
1351
1352 return err;
1353}
1354
Chris Masond1310b22008-01-24 16:13:08 -05001355/* wrappers around set/clear extent bit */
Qu Wenruod38ed272015-10-12 14:53:37 +08001356int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba2c53b912016-04-26 23:54:39 +02001357 unsigned bits, struct extent_changeset *changeset)
Qu Wenruod38ed272015-10-12 14:53:37 +08001358{
1359 /*
1360 * We don't support EXTENT_LOCKED yet, as current changeset will
1361 * record any bits changed, so for EXTENT_LOCKED case, it will
1362 * either fail with -EEXIST or changeset will record the whole
1363 * range.
1364 */
1365 BUG_ON(bits & EXTENT_LOCKED);
1366
David Sterba2c53b912016-04-26 23:54:39 +02001367 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
Qu Wenruod38ed272015-10-12 14:53:37 +08001368 changeset);
1369}
1370
Nikolay Borisov4ca73652019-03-27 14:24:10 +02001371int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start, u64 end,
1372 unsigned bits)
1373{
1374 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL,
1375 GFP_NOWAIT, NULL);
1376}
1377
Qu Wenruofefdc552015-10-12 15:35:38 +08001378int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1379 unsigned bits, int wake, int delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001380 struct extent_state **cached)
Qu Wenruofefdc552015-10-12 15:35:38 +08001381{
1382 return __clear_extent_bit(tree, start, end, bits, wake, delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001383 cached, GFP_NOFS, NULL);
Qu Wenruofefdc552015-10-12 15:35:38 +08001384}
1385
Qu Wenruofefdc552015-10-12 15:35:38 +08001386int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaf734c442016-04-26 23:54:39 +02001387 unsigned bits, struct extent_changeset *changeset)
Qu Wenruofefdc552015-10-12 15:35:38 +08001388{
1389 /*
1390 * Don't support EXTENT_LOCKED case, same reason as
1391 * set_record_extent_bits().
1392 */
1393 BUG_ON(bits & EXTENT_LOCKED);
1394
David Sterbaf734c442016-04-26 23:54:39 +02001395 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
Qu Wenruofefdc552015-10-12 15:35:38 +08001396 changeset);
1397}
1398
Chris Masond352ac62008-09-29 15:18:18 -04001399/*
1400 * either insert or lock state struct between start and end use mask to tell
1401 * us if waiting is desired.
1402 */
Chris Mason1edbb732009-09-02 13:24:36 -04001403int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaff13db42015-12-03 14:30:40 +01001404 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001405{
1406 int err;
1407 u64 failed_start;
David Sterba9ee49a042015-01-14 19:52:13 +01001408
Chris Masond1310b22008-01-24 16:13:08 -05001409 while (1) {
David Sterbaff13db42015-12-03 14:30:40 +01001410 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001411 EXTENT_LOCKED, &failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001412 cached_state, GFP_NOFS, NULL);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001413 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001414 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1415 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001416 } else
Chris Masond1310b22008-01-24 16:13:08 -05001417 break;
Chris Masond1310b22008-01-24 16:13:08 -05001418 WARN_ON(start > end);
1419 }
1420 return err;
1421}
Chris Masond1310b22008-01-24 16:13:08 -05001422
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001423int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001424{
1425 int err;
1426 u64 failed_start;
1427
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001428 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
Qu Wenruod38ed272015-10-12 14:53:37 +08001429 &failed_start, NULL, GFP_NOFS, NULL);
Yan Zheng66435582008-10-30 14:19:50 -04001430 if (err == -EEXIST) {
1431 if (failed_start > start)
1432 clear_extent_bit(tree, start, failed_start - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001433 EXTENT_LOCKED, 1, 0, NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001434 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001435 }
Josef Bacik25179202008-10-29 14:49:05 -04001436 return 1;
1437}
Josef Bacik25179202008-10-29 14:49:05 -04001438
David Sterbabd1fa4f2015-12-03 13:08:59 +01001439void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001440{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001441 unsigned long index = start >> PAGE_SHIFT;
1442 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001443 struct page *page;
1444
1445 while (index <= end_index) {
1446 page = find_get_page(inode->i_mapping, index);
1447 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1448 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001449 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001450 index++;
1451 }
Chris Mason4adaa612013-03-26 13:07:00 -04001452}
1453
David Sterbaf6311572015-12-03 13:08:59 +01001454void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001455{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001456 unsigned long index = start >> PAGE_SHIFT;
1457 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001458 struct page *page;
1459
1460 while (index <= end_index) {
1461 page = find_get_page(inode->i_mapping, index);
1462 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Mason4adaa612013-03-26 13:07:00 -04001463 __set_page_dirty_nobuffers(page);
Konstantin Khebnikov8d386332015-02-11 15:26:55 -08001464 account_page_redirty(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001465 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001466 index++;
1467 }
Chris Mason4adaa612013-03-26 13:07:00 -04001468}
1469
Chris Masond352ac62008-09-29 15:18:18 -04001470/* find the first state struct with 'bits' set after 'start', and
1471 * return it. tree->lock must be held. NULL will returned if
1472 * nothing was found after 'start'
1473 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001474static struct extent_state *
1475find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +01001476 u64 start, unsigned bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001477{
1478 struct rb_node *node;
1479 struct extent_state *state;
1480
1481 /*
1482 * this search will find all the extents that end after
1483 * our range starts.
1484 */
1485 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001486 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001487 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001488
Chris Masond3977122009-01-05 21:25:51 -05001489 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001490 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001491 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001492 return state;
Chris Masond3977122009-01-05 21:25:51 -05001493
Chris Masond7fc6402008-02-18 12:12:38 -05001494 node = rb_next(node);
1495 if (!node)
1496 break;
1497 }
1498out:
1499 return NULL;
1500}
Chris Masond7fc6402008-02-18 12:12:38 -05001501
Chris Masond352ac62008-09-29 15:18:18 -04001502/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001503 * find the first offset in the io tree with 'bits' set. zero is
1504 * returned if we find something, and *start_ret and *end_ret are
1505 * set to reflect the state struct that was found.
1506 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001507 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001508 */
1509int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba9ee49a042015-01-14 19:52:13 +01001510 u64 *start_ret, u64 *end_ret, unsigned bits,
Josef Bacike6138872012-09-27 17:07:30 -04001511 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001512{
1513 struct extent_state *state;
1514 int ret = 1;
1515
1516 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001517 if (cached_state && *cached_state) {
1518 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001519 if (state->end == start - 1 && extent_state_in_tree(state)) {
Liu Bo9688e9a2018-08-23 03:14:53 +08001520 while ((state = next_state(state)) != NULL) {
Josef Bacike6138872012-09-27 17:07:30 -04001521 if (state->state & bits)
1522 goto got_it;
Josef Bacike6138872012-09-27 17:07:30 -04001523 }
1524 free_extent_state(*cached_state);
1525 *cached_state = NULL;
1526 goto out;
1527 }
1528 free_extent_state(*cached_state);
1529 *cached_state = NULL;
1530 }
1531
Xiao Guangrong69261c42011-07-14 03:19:45 +00001532 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001533got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001534 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001535 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001536 *start_ret = state->start;
1537 *end_ret = state->end;
1538 ret = 0;
1539 }
Josef Bacike6138872012-09-27 17:07:30 -04001540out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001541 spin_unlock(&tree->lock);
1542 return ret;
1543}
1544
Nikolay Borisov45bfcfc2019-03-27 14:24:17 +02001545/**
1546 * find_first_clear_extent_bit - finds the first range that has @bits not set
1547 * and that starts after @start
1548 *
1549 * @tree - the tree to search
1550 * @start - the offset at/after which the found extent should start
1551 * @start_ret - records the beginning of the range
1552 * @end_ret - records the end of the range (inclusive)
1553 * @bits - the set of bits which must be unset
1554 *
1555 * Since unallocated range is also considered one which doesn't have the bits
1556 * set it's possible that @end_ret contains -1, this happens in case the range
1557 * spans (last_range_end, end of device]. In this case it's up to the caller to
1558 * trim @end_ret to the appropriate size.
1559 */
1560void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
1561 u64 *start_ret, u64 *end_ret, unsigned bits)
1562{
1563 struct extent_state *state;
1564 struct rb_node *node, *prev = NULL, *next;
1565
1566 spin_lock(&tree->lock);
1567
1568 /* Find first extent with bits cleared */
1569 while (1) {
1570 node = __etree_search(tree, start, &next, &prev, NULL, NULL);
1571 if (!node) {
1572 node = next;
1573 if (!node) {
1574 /*
1575 * We are past the last allocated chunk,
1576 * set start at the end of the last extent. The
1577 * device alloc tree should never be empty so
1578 * prev is always set.
1579 */
1580 ASSERT(prev);
1581 state = rb_entry(prev, struct extent_state, rb_node);
1582 *start_ret = state->end + 1;
1583 *end_ret = -1;
1584 goto out;
1585 }
1586 }
1587 state = rb_entry(node, struct extent_state, rb_node);
1588 if (in_range(start, state->start, state->end - state->start + 1) &&
1589 (state->state & bits)) {
1590 start = state->end + 1;
1591 } else {
1592 *start_ret = start;
1593 break;
1594 }
1595 }
1596
1597 /*
1598 * Find the longest stretch from start until an entry which has the
1599 * bits set
1600 */
1601 while (1) {
1602 state = rb_entry(node, struct extent_state, rb_node);
1603 if (state->end >= start && !(state->state & bits)) {
1604 *end_ret = state->end;
1605 } else {
1606 *end_ret = state->start - 1;
1607 break;
1608 }
1609
1610 node = rb_next(node);
1611 if (!node)
1612 break;
1613 }
1614out:
1615 spin_unlock(&tree->lock);
1616}
1617
Xiao Guangrong69261c42011-07-14 03:19:45 +00001618/*
Chris Masond352ac62008-09-29 15:18:18 -04001619 * find a contiguous range of bytes in the file marked as delalloc, not
1620 * more than 'max_bytes'. start and end are used to return the range,
1621 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001622 * true is returned if we find something, false if nothing was in the tree
Chris Masond352ac62008-09-29 15:18:18 -04001623 */
Lu Fengqi3522e902018-11-29 11:33:38 +08001624static noinline bool find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001625 u64 *start, u64 *end, u64 max_bytes,
1626 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001627{
1628 struct rb_node *node;
1629 struct extent_state *state;
1630 u64 cur_start = *start;
Lu Fengqi3522e902018-11-29 11:33:38 +08001631 bool found = false;
Chris Masond1310b22008-01-24 16:13:08 -05001632 u64 total_bytes = 0;
1633
Chris Masoncad321a2008-12-17 14:51:42 -05001634 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001635
Chris Masond1310b22008-01-24 16:13:08 -05001636 /*
1637 * this search will find all the extents that end after
1638 * our range starts.
1639 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001640 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001641 if (!node) {
Lu Fengqi3522e902018-11-29 11:33:38 +08001642 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001643 goto out;
1644 }
1645
Chris Masond3977122009-01-05 21:25:51 -05001646 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001647 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001648 if (found && (state->start != cur_start ||
1649 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001650 goto out;
1651 }
1652 if (!(state->state & EXTENT_DELALLOC)) {
1653 if (!found)
1654 *end = state->end;
1655 goto out;
1656 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001657 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001658 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001659 *cached_state = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +02001660 refcount_inc(&state->refs);
Josef Bacikc2a128d2010-02-02 21:19:11 +00001661 }
Lu Fengqi3522e902018-11-29 11:33:38 +08001662 found = true;
Chris Masond1310b22008-01-24 16:13:08 -05001663 *end = state->end;
1664 cur_start = state->end + 1;
1665 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001666 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001667 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001668 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001669 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001670 break;
1671 }
1672out:
Chris Masoncad321a2008-12-17 14:51:42 -05001673 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001674 return found;
1675}
1676
Liu Boda2c7002017-02-10 16:41:05 +01001677static int __process_pages_contig(struct address_space *mapping,
1678 struct page *locked_page,
1679 pgoff_t start_index, pgoff_t end_index,
1680 unsigned long page_ops, pgoff_t *index_ret);
1681
Jeff Mahoney143bede2012-03-01 14:56:26 +01001682static noinline void __unlock_for_delalloc(struct inode *inode,
1683 struct page *locked_page,
1684 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001685{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001686 unsigned long index = start >> PAGE_SHIFT;
1687 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001688
Liu Bo76c00212017-02-10 16:42:14 +01001689 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001690 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001691 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001692
Liu Bo76c00212017-02-10 16:42:14 +01001693 __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1694 PAGE_UNLOCK, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -04001695}
1696
1697static noinline int lock_delalloc_pages(struct inode *inode,
1698 struct page *locked_page,
1699 u64 delalloc_start,
1700 u64 delalloc_end)
1701{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001702 unsigned long index = delalloc_start >> PAGE_SHIFT;
Liu Bo76c00212017-02-10 16:42:14 +01001703 unsigned long index_ret = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001704 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001705 int ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001706
Liu Bo76c00212017-02-10 16:42:14 +01001707 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001708 if (index == locked_page->index && index == end_index)
1709 return 0;
1710
Liu Bo76c00212017-02-10 16:42:14 +01001711 ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1712 end_index, PAGE_LOCK, &index_ret);
1713 if (ret == -EAGAIN)
1714 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1715 (u64)index_ret << PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001716 return ret;
1717}
1718
1719/*
Lu Fengqi3522e902018-11-29 11:33:38 +08001720 * Find and lock a contiguous range of bytes in the file marked as delalloc, no
1721 * more than @max_bytes. @Start and @end are used to return the range,
Chris Masonc8b97812008-10-29 14:49:59 -04001722 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001723 * Return: true if we find something
1724 * false if nothing was in the tree
Chris Masonc8b97812008-10-29 14:49:59 -04001725 */
Johannes Thumshirnce9f9672018-11-19 10:38:17 +01001726EXPORT_FOR_TESTS
Lu Fengqi3522e902018-11-29 11:33:38 +08001727noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
Josef Bacik294e30f2013-10-09 12:00:56 -04001728 struct extent_io_tree *tree,
1729 struct page *locked_page, u64 *start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03001730 u64 *end)
Chris Masonc8b97812008-10-29 14:49:59 -04001731{
Nikolay Borisov917aace2018-10-26 14:43:20 +03001732 u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001733 u64 delalloc_start;
1734 u64 delalloc_end;
Lu Fengqi3522e902018-11-29 11:33:38 +08001735 bool found;
Chris Mason9655d292009-09-02 15:22:30 -04001736 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001737 int ret;
1738 int loops = 0;
1739
1740again:
1741 /* step one, find a bunch of delalloc bytes starting at start */
1742 delalloc_start = *start;
1743 delalloc_end = 0;
1744 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001745 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001746 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001747 *start = delalloc_start;
1748 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001749 free_extent_state(cached_state);
Lu Fengqi3522e902018-11-29 11:33:38 +08001750 return false;
Chris Masonc8b97812008-10-29 14:49:59 -04001751 }
1752
1753 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001754 * start comes from the offset of locked_page. We have to lock
1755 * pages in order, so we can't process delalloc bytes before
1756 * locked_page
1757 */
Chris Masond3977122009-01-05 21:25:51 -05001758 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001759 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001760
1761 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001762 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001763 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001764 if (delalloc_end + 1 - delalloc_start > max_bytes)
1765 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001766
Chris Masonc8b97812008-10-29 14:49:59 -04001767 /* step two, lock all the pages after the page that has start */
1768 ret = lock_delalloc_pages(inode, locked_page,
1769 delalloc_start, delalloc_end);
Nikolay Borisov9bfd61d2018-10-26 14:43:21 +03001770 ASSERT(!ret || ret == -EAGAIN);
Chris Masonc8b97812008-10-29 14:49:59 -04001771 if (ret == -EAGAIN) {
1772 /* some of the pages are gone, lets avoid looping by
1773 * shortening the size of the delalloc range we're searching
1774 */
Chris Mason9655d292009-09-02 15:22:30 -04001775 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001776 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001777 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001778 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001779 loops = 1;
1780 goto again;
1781 } else {
Lu Fengqi3522e902018-11-29 11:33:38 +08001782 found = false;
Chris Masonc8b97812008-10-29 14:49:59 -04001783 goto out_failed;
1784 }
1785 }
Chris Masonc8b97812008-10-29 14:49:59 -04001786
1787 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001788 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001789
1790 /* then test to make sure it is all still delalloc */
1791 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001792 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001793 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001794 unlock_extent_cached(tree, delalloc_start, delalloc_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001795 &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001796 __unlock_for_delalloc(inode, locked_page,
1797 delalloc_start, delalloc_end);
1798 cond_resched();
1799 goto again;
1800 }
Chris Mason9655d292009-09-02 15:22:30 -04001801 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001802 *start = delalloc_start;
1803 *end = delalloc_end;
1804out_failed:
1805 return found;
1806}
1807
Liu Boda2c7002017-02-10 16:41:05 +01001808static int __process_pages_contig(struct address_space *mapping,
1809 struct page *locked_page,
1810 pgoff_t start_index, pgoff_t end_index,
1811 unsigned long page_ops, pgoff_t *index_ret)
Chris Masonc8b97812008-10-29 14:49:59 -04001812{
Liu Bo873695b2017-02-02 17:49:22 -08001813 unsigned long nr_pages = end_index - start_index + 1;
Liu Boda2c7002017-02-10 16:41:05 +01001814 unsigned long pages_locked = 0;
Liu Bo873695b2017-02-02 17:49:22 -08001815 pgoff_t index = start_index;
Chris Masonc8b97812008-10-29 14:49:59 -04001816 struct page *pages[16];
Liu Bo873695b2017-02-02 17:49:22 -08001817 unsigned ret;
Liu Boda2c7002017-02-10 16:41:05 +01001818 int err = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001819 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001820
Liu Boda2c7002017-02-10 16:41:05 +01001821 if (page_ops & PAGE_LOCK) {
1822 ASSERT(page_ops == PAGE_LOCK);
1823 ASSERT(index_ret && *index_ret == start_index);
1824 }
1825
Filipe Manana704de492014-10-06 22:14:22 +01001826 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
Liu Bo873695b2017-02-02 17:49:22 -08001827 mapping_set_error(mapping, -EIO);
Filipe Manana704de492014-10-06 22:14:22 +01001828
Chris Masond3977122009-01-05 21:25:51 -05001829 while (nr_pages > 0) {
Liu Bo873695b2017-02-02 17:49:22 -08001830 ret = find_get_pages_contig(mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001831 min_t(unsigned long,
1832 nr_pages, ARRAY_SIZE(pages)), pages);
Liu Boda2c7002017-02-10 16:41:05 +01001833 if (ret == 0) {
1834 /*
1835 * Only if we're going to lock these pages,
1836 * can we find nothing at @index.
1837 */
1838 ASSERT(page_ops & PAGE_LOCK);
Liu Bo49d4a332017-03-06 18:20:56 -08001839 err = -EAGAIN;
1840 goto out;
Liu Boda2c7002017-02-10 16:41:05 +01001841 }
Chris Mason8b62b722009-09-02 16:53:46 -04001842
Liu Boda2c7002017-02-10 16:41:05 +01001843 for (i = 0; i < ret; i++) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001844 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001845 SetPagePrivate2(pages[i]);
1846
Chris Masonc8b97812008-10-29 14:49:59 -04001847 if (pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001848 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001849 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001850 continue;
1851 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001852 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001853 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001854 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001855 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001856 if (page_ops & PAGE_SET_ERROR)
1857 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001858 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001859 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001860 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001861 unlock_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001862 if (page_ops & PAGE_LOCK) {
1863 lock_page(pages[i]);
1864 if (!PageDirty(pages[i]) ||
1865 pages[i]->mapping != mapping) {
1866 unlock_page(pages[i]);
1867 put_page(pages[i]);
1868 err = -EAGAIN;
1869 goto out;
1870 }
1871 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001872 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001873 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001874 }
1875 nr_pages -= ret;
1876 index += ret;
1877 cond_resched();
1878 }
Liu Boda2c7002017-02-10 16:41:05 +01001879out:
1880 if (err && index_ret)
1881 *index_ret = start_index + pages_locked - 1;
1882 return err;
Chris Masonc8b97812008-10-29 14:49:59 -04001883}
Chris Masonc8b97812008-10-29 14:49:59 -04001884
Liu Bo873695b2017-02-02 17:49:22 -08001885void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1886 u64 delalloc_end, struct page *locked_page,
1887 unsigned clear_bits,
1888 unsigned long page_ops)
1889{
1890 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001891 NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001892
1893 __process_pages_contig(inode->i_mapping, locked_page,
1894 start >> PAGE_SHIFT, end >> PAGE_SHIFT,
Liu Boda2c7002017-02-10 16:41:05 +01001895 page_ops, NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001896}
1897
Chris Masond352ac62008-09-29 15:18:18 -04001898/*
1899 * count the number of bytes in the tree that have a given bit(s)
1900 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1901 * cached. The total number found is returned.
1902 */
Chris Masond1310b22008-01-24 16:13:08 -05001903u64 count_range_bits(struct extent_io_tree *tree,
1904 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001905 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001906{
1907 struct rb_node *node;
1908 struct extent_state *state;
1909 u64 cur_start = *start;
1910 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001911 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001912 int found = 0;
1913
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301914 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001915 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001916
Chris Masoncad321a2008-12-17 14:51:42 -05001917 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001918 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1919 total_bytes = tree->dirty_bytes;
1920 goto out;
1921 }
1922 /*
1923 * this search will find all the extents that end after
1924 * our range starts.
1925 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001926 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001927 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001928 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001929
Chris Masond3977122009-01-05 21:25:51 -05001930 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001931 state = rb_entry(node, struct extent_state, rb_node);
1932 if (state->start > search_end)
1933 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001934 if (contig && found && state->start > last + 1)
1935 break;
1936 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001937 total_bytes += min(search_end, state->end) + 1 -
1938 max(cur_start, state->start);
1939 if (total_bytes >= max_bytes)
1940 break;
1941 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001942 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001943 found = 1;
1944 }
Chris Masonec29ed52011-02-23 16:23:20 -05001945 last = state->end;
1946 } else if (contig && found) {
1947 break;
Chris Masond1310b22008-01-24 16:13:08 -05001948 }
1949 node = rb_next(node);
1950 if (!node)
1951 break;
1952 }
1953out:
Chris Masoncad321a2008-12-17 14:51:42 -05001954 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001955 return total_bytes;
1956}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001957
Chris Masond352ac62008-09-29 15:18:18 -04001958/*
1959 * set the private field for a given byte offset in the tree. If there isn't
1960 * an extent_state there already, this does nothing.
1961 */
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001962static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001963 struct io_failure_record *failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001964{
1965 struct rb_node *node;
1966 struct extent_state *state;
1967 int ret = 0;
1968
Chris Masoncad321a2008-12-17 14:51:42 -05001969 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001970 /*
1971 * this search will find all the extents that end after
1972 * our range starts.
1973 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001974 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001975 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001976 ret = -ENOENT;
1977 goto out;
1978 }
1979 state = rb_entry(node, struct extent_state, rb_node);
1980 if (state->start != start) {
1981 ret = -ENOENT;
1982 goto out;
1983 }
David Sterba47dc1962016-02-11 13:24:13 +01001984 state->failrec = failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001985out:
Chris Masoncad321a2008-12-17 14:51:42 -05001986 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001987 return ret;
1988}
1989
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001990static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001991 struct io_failure_record **failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001992{
1993 struct rb_node *node;
1994 struct extent_state *state;
1995 int ret = 0;
1996
Chris Masoncad321a2008-12-17 14:51:42 -05001997 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001998 /*
1999 * this search will find all the extents that end after
2000 * our range starts.
2001 */
Chris Mason80ea96b2008-02-01 14:51:59 -05002002 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04002003 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05002004 ret = -ENOENT;
2005 goto out;
2006 }
2007 state = rb_entry(node, struct extent_state, rb_node);
2008 if (state->start != start) {
2009 ret = -ENOENT;
2010 goto out;
2011 }
David Sterba47dc1962016-02-11 13:24:13 +01002012 *failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05002013out:
Chris Masoncad321a2008-12-17 14:51:42 -05002014 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002015 return ret;
2016}
2017
2018/*
2019 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05002020 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05002021 * has the bits set. Otherwise, 1 is returned if any bit in the
2022 * range is found set.
2023 */
2024int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01002025 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05002026{
2027 struct extent_state *state = NULL;
2028 struct rb_node *node;
2029 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002030
Chris Masoncad321a2008-12-17 14:51:42 -05002031 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01002032 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04002033 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04002034 node = &cached->rb_node;
2035 else
2036 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05002037 while (node && start <= end) {
2038 state = rb_entry(node, struct extent_state, rb_node);
2039
2040 if (filled && state->start > start) {
2041 bitset = 0;
2042 break;
2043 }
2044
2045 if (state->start > end)
2046 break;
2047
2048 if (state->state & bits) {
2049 bitset = 1;
2050 if (!filled)
2051 break;
2052 } else if (filled) {
2053 bitset = 0;
2054 break;
2055 }
Chris Mason46562ce2009-09-23 20:23:16 -04002056
2057 if (state->end == (u64)-1)
2058 break;
2059
Chris Masond1310b22008-01-24 16:13:08 -05002060 start = state->end + 1;
2061 if (start > end)
2062 break;
2063 node = rb_next(node);
2064 if (!node) {
2065 if (filled)
2066 bitset = 0;
2067 break;
2068 }
2069 }
Chris Masoncad321a2008-12-17 14:51:42 -05002070 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002071 return bitset;
2072}
Chris Masond1310b22008-01-24 16:13:08 -05002073
2074/*
2075 * helper function to set a given page up to date if all the
2076 * extents in the tree for that page are up to date
2077 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002078static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05002079{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002080 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002081 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04002082 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05002083 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002084}
2085
Josef Bacik7870d082017-05-05 11:57:15 -04002086int free_io_failure(struct extent_io_tree *failure_tree,
2087 struct extent_io_tree *io_tree,
2088 struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002089{
2090 int ret;
2091 int err = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002092
David Sterba47dc1962016-02-11 13:24:13 +01002093 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002094 ret = clear_extent_bits(failure_tree, rec->start,
2095 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02002096 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002097 if (ret)
2098 err = ret;
2099
Josef Bacik7870d082017-05-05 11:57:15 -04002100 ret = clear_extent_bits(io_tree, rec->start,
David Woodhouse53b381b2013-01-29 18:40:14 -05002101 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02002102 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05002103 if (ret && !err)
2104 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002105
2106 kfree(rec);
2107 return err;
2108}
2109
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002110/*
2111 * this bypasses the standard btrfs submit functions deliberately, as
2112 * the standard behavior is to write all copies in a raid setup. here we only
2113 * want to write the one bad copy. so we do the mapping for ourselves and issue
2114 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002115 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002116 * actually prevents the read that triggered the error from finishing.
2117 * currently, there can be no more than two copies of every data bit. thus,
2118 * exactly one rewrite is required.
2119 */
Josef Bacik6ec656b2017-05-05 11:57:14 -04002120int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
2121 u64 length, u64 logical, struct page *page,
2122 unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002123{
2124 struct bio *bio;
2125 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002126 u64 map_length = 0;
2127 u64 sector;
2128 struct btrfs_bio *bbio = NULL;
2129 int ret;
2130
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002131 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002132 BUG_ON(!mirror_num);
2133
David Sterbac5e4c3d2017-06-12 17:29:41 +02002134 bio = btrfs_io_bio_alloc(1);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002135 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002136 map_length = length;
2137
Filipe Mananab5de8d02016-05-27 22:21:27 +01002138 /*
2139 * Avoid races with device replace and make sure our bbio has devices
2140 * associated to its stripes that don't go away while we are doing the
2141 * read repair operation.
2142 */
2143 btrfs_bio_counter_inc_blocked(fs_info);
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03002144 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
Liu Boc7253282017-03-29 10:53:58 -07002145 /*
2146 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
2147 * to update all raid stripes, but here we just want to correct
2148 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2149 * stripe's dev and sector.
2150 */
2151 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2152 &map_length, &bbio, 0);
2153 if (ret) {
2154 btrfs_bio_counter_dec(fs_info);
2155 bio_put(bio);
2156 return -EIO;
2157 }
2158 ASSERT(bbio->mirror_num == 1);
2159 } else {
2160 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2161 &map_length, &bbio, mirror_num);
2162 if (ret) {
2163 btrfs_bio_counter_dec(fs_info);
2164 bio_put(bio);
2165 return -EIO;
2166 }
2167 BUG_ON(mirror_num != bbio->mirror_num);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002168 }
Liu Boc7253282017-03-29 10:53:58 -07002169
2170 sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002171 bio->bi_iter.bi_sector = sector;
Liu Boc7253282017-03-29 10:53:58 -07002172 dev = bbio->stripes[bbio->mirror_num - 1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002173 btrfs_put_bbio(bbio);
Anand Jainebbede42017-12-04 12:54:52 +08002174 if (!dev || !dev->bdev ||
2175 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002176 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002177 bio_put(bio);
2178 return -EIO;
2179 }
Christoph Hellwig74d46992017-08-23 19:10:32 +02002180 bio_set_dev(bio, dev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002181 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Miao Xieffdd2012014-09-12 18:44:00 +08002182 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002183
Mike Christie4e49ea42016-06-05 14:31:41 -05002184 if (btrfsic_submit_bio_wait(bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002185 /* try to remap that extent elsewhere? */
Filipe Mananab5de8d02016-05-27 22:21:27 +01002186 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002187 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002188 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002189 return -EIO;
2190 }
2191
David Sterbab14af3b2015-10-08 10:43:10 +02002192 btrfs_info_rl_in_rcu(fs_info,
2193 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Josef Bacik6ec656b2017-05-05 11:57:14 -04002194 ino, start,
Miao Xie1203b682014-09-12 18:44:01 +08002195 rcu_str_deref(dev->name), sector);
Filipe Mananab5de8d02016-05-27 22:21:27 +01002196 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002197 bio_put(bio);
2198 return 0;
2199}
2200
David Sterba20a1fbf92019-03-20 11:23:44 +01002201int btrfs_repair_eb_io_failure(struct extent_buffer *eb, int mirror_num)
Josef Bacikea466792012-03-26 21:57:36 -04002202{
David Sterba20a1fbf92019-03-20 11:23:44 +01002203 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacikea466792012-03-26 21:57:36 -04002204 u64 start = eb->start;
David Sterbacc5e31a2018-03-01 18:20:27 +01002205 int i, num_pages = num_extent_pages(eb);
Chris Masond95603b2012-04-12 15:55:15 -04002206 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002207
David Howellsbc98a422017-07-17 08:45:34 +01002208 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002209 return -EROFS;
2210
Josef Bacikea466792012-03-26 21:57:36 -04002211 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002212 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002213
Josef Bacik6ec656b2017-05-05 11:57:14 -04002214 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002215 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002216 if (ret)
2217 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002218 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002219 }
2220
2221 return ret;
2222}
2223
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002224/*
2225 * each time an IO finishes, we do a fast check in the IO failure tree
2226 * to see if we need to process or clean up an io_failure_record
2227 */
Josef Bacik7870d082017-05-05 11:57:15 -04002228int clean_io_failure(struct btrfs_fs_info *fs_info,
2229 struct extent_io_tree *failure_tree,
2230 struct extent_io_tree *io_tree, u64 start,
2231 struct page *page, u64 ino, unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002232{
2233 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002234 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002235 struct extent_state *state;
2236 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002237 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002238
2239 private = 0;
Josef Bacik7870d082017-05-05 11:57:15 -04002240 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2241 EXTENT_DIRTY, 0);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002242 if (!ret)
2243 return 0;
2244
Josef Bacik7870d082017-05-05 11:57:15 -04002245 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002246 if (ret)
2247 return 0;
2248
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002249 BUG_ON(!failrec->this_mirror);
2250
2251 if (failrec->in_validation) {
2252 /* there was no real error, just free the record */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002253 btrfs_debug(fs_info,
2254 "clean_io_failure: freeing dummy error at %llu",
2255 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002256 goto out;
2257 }
David Howellsbc98a422017-07-17 08:45:34 +01002258 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002259 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002260
Josef Bacik7870d082017-05-05 11:57:15 -04002261 spin_lock(&io_tree->lock);
2262 state = find_first_extent_bit_state(io_tree,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002263 failrec->start,
2264 EXTENT_LOCKED);
Josef Bacik7870d082017-05-05 11:57:15 -04002265 spin_unlock(&io_tree->lock);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002266
Miao Xie883d0de2013-07-25 19:22:35 +08002267 if (state && state->start <= failrec->start &&
2268 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002269 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2270 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002271 if (num_copies > 1) {
Josef Bacik7870d082017-05-05 11:57:15 -04002272 repair_io_failure(fs_info, ino, start, failrec->len,
2273 failrec->logical, page, pg_offset,
2274 failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002275 }
2276 }
2277
2278out:
Josef Bacik7870d082017-05-05 11:57:15 -04002279 free_io_failure(failure_tree, io_tree, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002280
Miao Xie454ff3d2014-09-12 18:43:58 +08002281 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002282}
2283
Miao Xief6124962014-09-12 18:44:04 +08002284/*
2285 * Can be called when
2286 * - hold extent lock
2287 * - under ordered extent
2288 * - the inode is freeing
2289 */
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002290void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
Miao Xief6124962014-09-12 18:44:04 +08002291{
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002292 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
Miao Xief6124962014-09-12 18:44:04 +08002293 struct io_failure_record *failrec;
2294 struct extent_state *state, *next;
2295
2296 if (RB_EMPTY_ROOT(&failure_tree->state))
2297 return;
2298
2299 spin_lock(&failure_tree->lock);
2300 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2301 while (state) {
2302 if (state->start > end)
2303 break;
2304
2305 ASSERT(state->end <= end);
2306
2307 next = next_state(state);
2308
David Sterba47dc1962016-02-11 13:24:13 +01002309 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002310 free_extent_state(state);
2311 kfree(failrec);
2312
2313 state = next;
2314 }
2315 spin_unlock(&failure_tree->lock);
2316}
2317
Miao Xie2fe63032014-09-12 18:43:59 +08002318int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
David Sterba47dc1962016-02-11 13:24:13 +01002319 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002320{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002321 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002322 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002323 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002324 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2325 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2326 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002327 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002328 u64 logical;
2329
David Sterba47dc1962016-02-11 13:24:13 +01002330 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002331 if (ret) {
2332 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2333 if (!failrec)
2334 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002335
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002336 failrec->start = start;
2337 failrec->len = end - start + 1;
2338 failrec->this_mirror = 0;
2339 failrec->bio_flags = 0;
2340 failrec->in_validation = 0;
2341
2342 read_lock(&em_tree->lock);
2343 em = lookup_extent_mapping(em_tree, start, failrec->len);
2344 if (!em) {
2345 read_unlock(&em_tree->lock);
2346 kfree(failrec);
2347 return -EIO;
2348 }
2349
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002350 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002351 free_extent_map(em);
2352 em = NULL;
2353 }
2354 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002355 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002356 kfree(failrec);
2357 return -EIO;
2358 }
Miao Xie2fe63032014-09-12 18:43:59 +08002359
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002360 logical = start - em->start;
2361 logical = em->block_start + logical;
2362 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2363 logical = em->block_start;
2364 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2365 extent_set_compress_type(&failrec->bio_flags,
2366 em->compress_type);
2367 }
Miao Xie2fe63032014-09-12 18:43:59 +08002368
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002369 btrfs_debug(fs_info,
2370 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2371 logical, start, failrec->len);
Miao Xie2fe63032014-09-12 18:43:59 +08002372
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002373 failrec->logical = logical;
2374 free_extent_map(em);
2375
2376 /* set the bits in the private failure tree */
2377 ret = set_extent_bits(failure_tree, start, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002378 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002379 if (ret >= 0)
David Sterba47dc1962016-02-11 13:24:13 +01002380 ret = set_state_failrec(failure_tree, start, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002381 /* set the bits in the inode's tree */
2382 if (ret >= 0)
David Sterbaceeb0ae2016-04-26 23:54:39 +02002383 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002384 if (ret < 0) {
2385 kfree(failrec);
2386 return ret;
2387 }
2388 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002389 btrfs_debug(fs_info,
2390 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2391 failrec->logical, failrec->start, failrec->len,
2392 failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002393 /*
2394 * when data can be on disk more than twice, add to failrec here
2395 * (e.g. with a list for failed_mirror) to make
2396 * clean_io_failure() clean all those errors at once.
2397 */
2398 }
Miao Xie2fe63032014-09-12 18:43:59 +08002399
2400 *failrec_ret = failrec;
2401
2402 return 0;
2403}
2404
Ming Leia0b60d72017-12-18 20:22:11 +08002405bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages,
Miao Xie2fe63032014-09-12 18:43:59 +08002406 struct io_failure_record *failrec, int failed_mirror)
2407{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002408 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002409 int num_copies;
2410
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002411 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002412 if (num_copies == 1) {
2413 /*
2414 * we only have a single copy of the data, so don't bother with
2415 * all the retry and error correction code that follows. no
2416 * matter what the error is, it is very likely to persist.
2417 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002418 btrfs_debug(fs_info,
2419 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2420 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002421 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002422 }
2423
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002424 /*
2425 * there are two premises:
2426 * a) deliver good data to the caller
2427 * b) correct the bad sectors on disk
2428 */
Ming Leia0b60d72017-12-18 20:22:11 +08002429 if (failed_bio_pages > 1) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002430 /*
2431 * to fulfill b), we need to know the exact failing sectors, as
2432 * we don't want to rewrite any more than the failed ones. thus,
2433 * we need separate read requests for the failed bio
2434 *
2435 * if the following BUG_ON triggers, our validation request got
2436 * merged. we need separate requests for our algorithm to work.
2437 */
2438 BUG_ON(failrec->in_validation);
2439 failrec->in_validation = 1;
2440 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002441 } else {
2442 /*
2443 * we're ready to fulfill a) and b) alongside. get a good copy
2444 * of the failed sector and if we succeed, we have setup
2445 * everything for repair_io_failure to do the rest for us.
2446 */
2447 if (failrec->in_validation) {
2448 BUG_ON(failrec->this_mirror != failed_mirror);
2449 failrec->in_validation = 0;
2450 failrec->this_mirror = 0;
2451 }
2452 failrec->failed_mirror = failed_mirror;
2453 failrec->this_mirror++;
2454 if (failrec->this_mirror == failed_mirror)
2455 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002456 }
2457
Miao Xiefacc8a222013-07-25 19:22:34 +08002458 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002459 btrfs_debug(fs_info,
2460 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2461 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002462 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002463 }
2464
Liu Boc3cfb652017-07-13 15:00:50 -07002465 return true;
Miao Xie2fe63032014-09-12 18:43:59 +08002466}
2467
2468
2469struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2470 struct io_failure_record *failrec,
2471 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002472 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002473{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002474 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002475 struct bio *bio;
2476 struct btrfs_io_bio *btrfs_failed_bio;
2477 struct btrfs_io_bio *btrfs_bio;
2478
David Sterbac5e4c3d2017-06-12 17:29:41 +02002479 bio = btrfs_io_bio_alloc(1);
Miao Xie2fe63032014-09-12 18:43:59 +08002480 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002481 bio->bi_iter.bi_sector = failrec->logical >> 9;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002482 bio_set_dev(bio, fs_info->fs_devices->latest_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002483 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002484 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002485
Miao Xiefacc8a222013-07-25 19:22:34 +08002486 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2487 if (btrfs_failed_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002488 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2489
2490 btrfs_bio = btrfs_io_bio(bio);
2491 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002492 icsum *= csum_size;
2493 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002494 csum_size);
2495 }
2496
Miao Xie2fe63032014-09-12 18:43:59 +08002497 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002498
Miao Xie2fe63032014-09-12 18:43:59 +08002499 return bio;
2500}
2501
2502/*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002503 * This is a generic handler for readpage errors. If other copies exist, read
2504 * those and write back good data to the failed position. Does not investigate
2505 * in remapping the failed extent elsewhere, hoping the device will be smart
2506 * enough to do this as needed
Miao Xie2fe63032014-09-12 18:43:59 +08002507 */
Miao Xie2fe63032014-09-12 18:43:59 +08002508static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2509 struct page *page, u64 start, u64 end,
2510 int failed_mirror)
2511{
2512 struct io_failure_record *failrec;
2513 struct inode *inode = page->mapping->host;
2514 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002515 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie2fe63032014-09-12 18:43:59 +08002516 struct bio *bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002517 int read_mode = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002518 blk_status_t status;
Miao Xie2fe63032014-09-12 18:43:59 +08002519 int ret;
Christoph Hellwig8a2ee442019-02-15 19:13:07 +08002520 unsigned failed_bio_pages = failed_bio->bi_iter.bi_size >> PAGE_SHIFT;
Miao Xie2fe63032014-09-12 18:43:59 +08002521
Mike Christie1f7ad752016-06-05 14:31:51 -05002522 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002523
2524 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2525 if (ret)
2526 return ret;
2527
Ming Leia0b60d72017-12-18 20:22:11 +08002528 if (!btrfs_check_repairable(inode, failed_bio_pages, failrec,
Liu Boc3cfb652017-07-13 15:00:50 -07002529 failed_mirror)) {
Josef Bacik7870d082017-05-05 11:57:15 -04002530 free_io_failure(failure_tree, tree, failrec);
Miao Xie2fe63032014-09-12 18:43:59 +08002531 return -EIO;
2532 }
2533
Ming Leia0b60d72017-12-18 20:22:11 +08002534 if (failed_bio_pages > 1)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002535 read_mode |= REQ_FAILFAST_DEV;
Miao Xie2fe63032014-09-12 18:43:59 +08002536
2537 phy_offset >>= inode->i_sb->s_blocksize_bits;
2538 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2539 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002540 (int)phy_offset, failed_bio->bi_end_io,
2541 NULL);
David Sterbaebcc3262018-06-29 10:56:53 +02002542 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie2fe63032014-09-12 18:43:59 +08002543
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002544 btrfs_debug(btrfs_sb(inode->i_sb),
2545 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2546 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002547
Linus Torvalds8c27cb32017-07-05 16:41:23 -07002548 status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002549 failrec->bio_flags, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002550 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04002551 free_io_failure(failure_tree, tree, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002552 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002553 ret = blk_status_to_errno(status);
Miao Xie6c387ab2014-09-12 18:43:57 +08002554 }
2555
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002556 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002557}
2558
Chris Masond1310b22008-01-24 16:13:08 -05002559/* lots and lots of room for performance fixes in the end_bio funcs */
2560
David Sterbab5227c02015-12-03 13:08:59 +01002561void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002562{
2563 int uptodate = (err == 0);
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002564 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002565
Nikolay Borisovc6297322018-11-08 10:18:08 +02002566 btrfs_writepage_endio_finish_ordered(page, start, end, uptodate);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002567
Jeff Mahoney87826df2012-02-15 16:23:57 +01002568 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002569 ClearPageUptodate(page);
2570 SetPageError(page);
Colin Ian Kingbff5baf2017-05-09 18:14:01 +01002571 ret = err < 0 ? err : -EIO;
Liu Bo5dca6ee2014-05-12 12:47:36 +08002572 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002573 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002574}
2575
Chris Masond1310b22008-01-24 16:13:08 -05002576/*
2577 * after a writepage IO is done, we need to:
2578 * clear the uptodate bits on error
2579 * clear the writeback bits in the extent tree for this IO
2580 * end_page_writeback if the page has no more pending IO
2581 *
2582 * Scheduling is not allowed, so the extent state tree is expected
2583 * to have one and only one object corresponding to this IO.
2584 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002585static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002586{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002587 int error = blk_status_to_errno(bio->bi_status);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002588 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002589 u64 start;
2590 u64 end;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002591 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08002592 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002593
David Sterbac09abff2017-07-13 18:10:07 +02002594 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08002595 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002596 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002597 struct inode *inode = page->mapping->host;
2598 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002599
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002600 /* We always issue full-page reads, but if some block
2601 * in a page fails to read, blk_update_request() will
2602 * advance bv_offset and adjust bv_len to compensate.
2603 * Print a warning for nonzero offsets, and an error
2604 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002605 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2606 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002607 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002608 "partial page write in btrfs with offset %u and length %u",
2609 bvec->bv_offset, bvec->bv_len);
2610 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002611 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002612 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002613 bvec->bv_offset, bvec->bv_len);
2614 }
Chris Masond1310b22008-01-24 16:13:08 -05002615
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002616 start = page_offset(page);
2617 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002618
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002619 end_extent_writepage(page, error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002620 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002621 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002622
Chris Masond1310b22008-01-24 16:13:08 -05002623 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002624}
2625
Miao Xie883d0de2013-07-25 19:22:35 +08002626static void
2627endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2628 int uptodate)
2629{
2630 struct extent_state *cached = NULL;
2631 u64 end = start + len - 1;
2632
2633 if (uptodate && tree->track_uptodate)
2634 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
David Sterbad810a4b2017-12-07 18:52:54 +01002635 unlock_extent_cached_atomic(tree, start, end, &cached);
Miao Xie883d0de2013-07-25 19:22:35 +08002636}
2637
Chris Masond1310b22008-01-24 16:13:08 -05002638/*
2639 * after a readpage IO is done, we need to:
2640 * clear the uptodate bits on error
2641 * set the uptodate bits if things worked
2642 * set the page up to date if all extents in the tree are uptodate
2643 * clear the lock bit in the extent tree
2644 * unlock the page if there are no other extents locked for it
2645 *
2646 * Scheduling is not allowed, so the extent state tree is expected
2647 * to have one and only one object corresponding to this IO.
2648 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002649static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002650{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002651 struct bio_vec *bvec;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002652 int uptodate = !bio->bi_status;
Miao Xiefacc8a222013-07-25 19:22:34 +08002653 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04002654 struct extent_io_tree *tree, *failure_tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002655 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002656 u64 start;
2657 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002658 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002659 u64 extent_start = 0;
2660 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002661 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002662 int ret;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002663 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08002664 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002665
David Sterbac09abff2017-07-13 18:10:07 +02002666 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08002667 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002668 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002669 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002670 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002671 bool data_inode = btrfs_ino(BTRFS_I(inode))
2672 != BTRFS_BTREE_INODE_OBJECTID;
Arne Jansen507903b2011-04-06 10:02:20 +00002673
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002674 btrfs_debug(fs_info,
2675 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002676 (u64)bio->bi_iter.bi_sector, bio->bi_status,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002677 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002678 tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002679 failure_tree = &BTRFS_I(inode)->io_failure_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002680
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002681 /* We always issue full-page reads, but if some block
2682 * in a page fails to read, blk_update_request() will
2683 * advance bv_offset and adjust bv_len to compensate.
2684 * Print a warning for nonzero offsets, and an error
2685 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002686 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2687 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002688 btrfs_err(fs_info,
2689 "partial page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002690 bvec->bv_offset, bvec->bv_len);
2691 else
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002692 btrfs_info(fs_info,
2693 "incomplete page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002694 bvec->bv_offset, bvec->bv_len);
2695 }
Chris Masond1310b22008-01-24 16:13:08 -05002696
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002697 start = page_offset(page);
2698 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002699 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002700
Chris Mason9be33952013-05-17 18:30:14 -04002701 mirror = io_bio->mirror_num;
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002702 if (likely(uptodate)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002703 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2704 page, start, end,
2705 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002706 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002707 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002708 else
Josef Bacik7870d082017-05-05 11:57:15 -04002709 clean_io_failure(BTRFS_I(inode)->root->fs_info,
2710 failure_tree, tree, start,
2711 page,
2712 btrfs_ino(BTRFS_I(inode)), 0);
Chris Masond1310b22008-01-24 16:13:08 -05002713 }
Josef Bacikea466792012-03-26 21:57:36 -04002714
Miao Xief2a09da2013-07-25 19:22:33 +08002715 if (likely(uptodate))
2716 goto readpage_ok;
2717
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002718 if (data_inode) {
Liu Bo9d0d1c82017-03-24 15:04:50 -07002719
2720 /*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002721 * The generic bio_readpage_error handles errors the
2722 * following way: If possible, new read requests are
2723 * created and submitted and will end up in
2724 * end_bio_extent_readpage as well (if we're lucky,
2725 * not in the !uptodate case). In that case it returns
2726 * 0 and we just go on with the next page in our bio.
2727 * If it can't handle the error it will return -EIO and
2728 * we remain responsible for that page.
Liu Bo9d0d1c82017-03-24 15:04:50 -07002729 */
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002730 ret = bio_readpage_error(bio, offset, page, start, end,
2731 mirror);
2732 if (ret == 0) {
2733 uptodate = !bio->bi_status;
2734 offset += len;
2735 continue;
2736 }
2737 } else {
2738 struct extent_buffer *eb;
2739
2740 eb = (struct extent_buffer *)page->private;
2741 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
2742 eb->read_mirror = mirror;
2743 atomic_dec(&eb->io_pages);
2744 if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD,
2745 &eb->bflags))
2746 btree_readahead_hook(eb, -EIO);
Chris Mason7e383262008-04-09 16:28:12 -04002747 }
Miao Xief2a09da2013-07-25 19:22:33 +08002748readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002749 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002750 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002751 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002752 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002753
2754 /* Zero out the end if this page straddles i_size */
Johannes Thumshirn70730172018-12-05 15:23:03 +01002755 off = offset_in_page(i_size);
Liu Boa583c022014-08-19 23:32:22 +08002756 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002757 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002758 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002759 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002760 ClearPageUptodate(page);
2761 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002762 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002763 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002764 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002765
2766 if (unlikely(!uptodate)) {
2767 if (extent_len) {
2768 endio_readpage_release_extent(tree,
2769 extent_start,
2770 extent_len, 1);
2771 extent_start = 0;
2772 extent_len = 0;
2773 }
2774 endio_readpage_release_extent(tree, start,
2775 end - start + 1, 0);
2776 } else if (!extent_len) {
2777 extent_start = start;
2778 extent_len = end + 1 - start;
2779 } else if (extent_start + extent_len == start) {
2780 extent_len += end + 1 - start;
2781 } else {
2782 endio_readpage_release_extent(tree, extent_start,
2783 extent_len, uptodate);
2784 extent_start = start;
2785 extent_len = end + 1 - start;
2786 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002787 }
Chris Masond1310b22008-01-24 16:13:08 -05002788
Miao Xie883d0de2013-07-25 19:22:35 +08002789 if (extent_len)
2790 endio_readpage_release_extent(tree, extent_start, extent_len,
2791 uptodate);
David Sterbab3a0dd52018-11-22 17:16:49 +01002792 btrfs_io_bio_free_csum(io_bio);
Chris Masond1310b22008-01-24 16:13:08 -05002793 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002794}
2795
Chris Mason9be33952013-05-17 18:30:14 -04002796/*
David Sterba184f9992017-06-12 17:29:39 +02002797 * Initialize the members up to but not including 'bio'. Use after allocating a
2798 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
2799 * 'bio' because use of __GFP_ZERO is not supported.
Chris Mason9be33952013-05-17 18:30:14 -04002800 */
David Sterba184f9992017-06-12 17:29:39 +02002801static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
Chris Masond1310b22008-01-24 16:13:08 -05002802{
David Sterba184f9992017-06-12 17:29:39 +02002803 memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
2804}
2805
2806/*
David Sterba6e707bc2017-06-02 17:26:26 +02002807 * The following helpers allocate a bio. As it's backed by a bioset, it'll
2808 * never fail. We're returning a bio right now but you can call btrfs_io_bio
2809 * for the appropriate container_of magic
Chris Masond1310b22008-01-24 16:13:08 -05002810 */
David Sterbac821e7f32017-06-02 18:35:36 +02002811struct bio *btrfs_bio_alloc(struct block_device *bdev, u64 first_byte)
Chris Masond1310b22008-01-24 16:13:08 -05002812{
2813 struct bio *bio;
2814
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002815 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset);
Christoph Hellwig74d46992017-08-23 19:10:32 +02002816 bio_set_dev(bio, bdev);
David Sterbac821e7f32017-06-02 18:35:36 +02002817 bio->bi_iter.bi_sector = first_byte >> 9;
David Sterba184f9992017-06-12 17:29:39 +02002818 btrfs_io_bio_init(btrfs_io_bio(bio));
Chris Masond1310b22008-01-24 16:13:08 -05002819 return bio;
2820}
2821
David Sterba8b6c1d52017-06-02 17:48:13 +02002822struct bio *btrfs_bio_clone(struct bio *bio)
Chris Mason9be33952013-05-17 18:30:14 -04002823{
Miao Xie23ea8e52014-09-12 18:43:54 +08002824 struct btrfs_io_bio *btrfs_bio;
2825 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002826
David Sterba6e707bc2017-06-02 17:26:26 +02002827 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002828 new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset);
David Sterba6e707bc2017-06-02 17:26:26 +02002829 btrfs_bio = btrfs_io_bio(new);
David Sterba184f9992017-06-12 17:29:39 +02002830 btrfs_io_bio_init(btrfs_bio);
David Sterba6e707bc2017-06-02 17:26:26 +02002831 btrfs_bio->iter = bio->bi_iter;
Miao Xie23ea8e52014-09-12 18:43:54 +08002832 return new;
2833}
Chris Mason9be33952013-05-17 18:30:14 -04002834
David Sterbac5e4c3d2017-06-12 17:29:41 +02002835struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
Chris Mason9be33952013-05-17 18:30:14 -04002836{
Miao Xiefacc8a222013-07-25 19:22:34 +08002837 struct bio *bio;
2838
David Sterba6e707bc2017-06-02 17:26:26 +02002839 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002840 bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
David Sterba184f9992017-06-12 17:29:39 +02002841 btrfs_io_bio_init(btrfs_io_bio(bio));
Miao Xiefacc8a222013-07-25 19:22:34 +08002842 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002843}
2844
Liu Boe4770942017-05-16 10:57:14 -07002845struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
Liu Bo2f8e9142017-05-15 17:43:31 -07002846{
2847 struct bio *bio;
2848 struct btrfs_io_bio *btrfs_bio;
2849
2850 /* this will never fail when it's backed by a bioset */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002851 bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset);
Liu Bo2f8e9142017-05-15 17:43:31 -07002852 ASSERT(bio);
2853
2854 btrfs_bio = btrfs_io_bio(bio);
David Sterba184f9992017-06-12 17:29:39 +02002855 btrfs_io_bio_init(btrfs_bio);
Liu Bo2f8e9142017-05-15 17:43:31 -07002856
2857 bio_trim(bio, offset >> 9, size >> 9);
Liu Bo17347ce2017-05-15 15:33:27 -07002858 btrfs_bio->iter = bio->bi_iter;
Liu Bo2f8e9142017-05-15 17:43:31 -07002859 return bio;
2860}
Chris Mason9be33952013-05-17 18:30:14 -04002861
David Sterba4b81ba42017-06-06 19:14:26 +02002862/*
2863 * @opf: bio REQ_OP_* and REQ_* flags as one value
David Sterbab8b3d622017-06-12 19:50:41 +02002864 * @tree: tree so we can call our merge_bio hook
2865 * @wbc: optional writeback control for io accounting
2866 * @page: page to add to the bio
2867 * @pg_offset: offset of the new bio or to check whether we are adding
2868 * a contiguous page to the previous one
2869 * @size: portion of page that we want to write
2870 * @offset: starting offset in the page
2871 * @bdev: attach newly created bios to this bdev
David Sterba5c2b1fd2017-06-06 19:22:55 +02002872 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
David Sterbab8b3d622017-06-12 19:50:41 +02002873 * @end_io_func: end_io callback for new bio
2874 * @mirror_num: desired mirror to read/write
2875 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
2876 * @bio_flags: flags of the current bio to see if we can merge them
David Sterba4b81ba42017-06-06 19:14:26 +02002877 */
2878static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002879 struct writeback_control *wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02002880 struct page *page, u64 offset,
David Sterba6c5a4e22017-10-04 17:10:34 +02002881 size_t size, unsigned long pg_offset,
Chris Masond1310b22008-01-24 16:13:08 -05002882 struct block_device *bdev,
2883 struct bio **bio_ret,
Chris Masonf1885912008-04-09 16:28:12 -04002884 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002885 int mirror_num,
2886 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002887 unsigned long bio_flags,
2888 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002889{
2890 int ret = 0;
2891 struct bio *bio;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002892 size_t page_size = min_t(size_t, size, PAGE_SIZE);
David Sterba6273b7f2017-10-04 17:30:11 +02002893 sector_t sector = offset >> 9;
Chris Masond1310b22008-01-24 16:13:08 -05002894
David Sterba5c2b1fd2017-06-06 19:22:55 +02002895 ASSERT(bio_ret);
2896
2897 if (*bio_ret) {
David Sterba0c8508a2017-06-12 20:00:43 +02002898 bool contig;
2899 bool can_merge = true;
2900
Chris Masond1310b22008-01-24 16:13:08 -05002901 bio = *bio_ret;
David Sterba0c8508a2017-06-12 20:00:43 +02002902 if (prev_bio_flags & EXTENT_BIO_COMPRESSED)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002903 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002904 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002905 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002906
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002907 ASSERT(tree->ops);
2908 if (btrfs_bio_fits_in_stripe(page, page_size, bio, bio_flags))
David Sterba0c8508a2017-06-12 20:00:43 +02002909 can_merge = false;
2910
2911 if (prev_bio_flags != bio_flags || !contig || !can_merge ||
Filipe Manana005efed2015-09-14 09:09:31 +01002912 force_bio_submit ||
David Sterba6c5a4e22017-10-04 17:10:34 +02002913 bio_add_page(bio, page, page_size, pg_offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002914 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002915 if (ret < 0) {
2916 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002917 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002918 }
Chris Masond1310b22008-01-24 16:13:08 -05002919 bio = NULL;
2920 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002921 if (wbc)
2922 wbc_account_io(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002923 return 0;
2924 }
2925 }
Chris Masonc8b97812008-10-29 14:49:59 -04002926
David Sterba6273b7f2017-10-04 17:30:11 +02002927 bio = btrfs_bio_alloc(bdev, offset);
David Sterba6c5a4e22017-10-04 17:10:34 +02002928 bio_add_page(bio, page, page_size, pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002929 bio->bi_end_io = end_io_func;
2930 bio->bi_private = tree;
Jens Axboee6959b92017-06-27 11:51:28 -06002931 bio->bi_write_hint = page->mapping->host->i_write_hint;
David Sterba4b81ba42017-06-06 19:14:26 +02002932 bio->bi_opf = opf;
Chris Masonda2f0f72015-07-02 13:57:22 -07002933 if (wbc) {
2934 wbc_init_bio(wbc, bio);
2935 wbc_account_io(wbc, page, page_size);
2936 }
Chris Mason70dec802008-01-29 09:59:12 -05002937
David Sterba5c2b1fd2017-06-06 19:22:55 +02002938 *bio_ret = bio;
Chris Masond1310b22008-01-24 16:13:08 -05002939
2940 return ret;
2941}
2942
Eric Sandeen48a3b632013-04-25 20:41:01 +00002943static void attach_extent_buffer_page(struct extent_buffer *eb,
2944 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002945{
2946 if (!PagePrivate(page)) {
2947 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002948 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002949 set_page_private(page, (unsigned long)eb);
2950 } else {
2951 WARN_ON(page->private != (unsigned long)eb);
2952 }
2953}
2954
Chris Masond1310b22008-01-24 16:13:08 -05002955void set_page_extent_mapped(struct page *page)
2956{
2957 if (!PagePrivate(page)) {
2958 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002959 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002960 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002961 }
2962}
2963
Miao Xie125bac012013-07-25 19:22:37 +08002964static struct extent_map *
2965__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2966 u64 start, u64 len, get_extent_t *get_extent,
2967 struct extent_map **em_cached)
2968{
2969 struct extent_map *em;
2970
2971 if (em_cached && *em_cached) {
2972 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002973 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002974 start < extent_map_end(em)) {
Elena Reshetova490b54d2017-03-03 10:55:12 +02002975 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002976 return em;
2977 }
2978
2979 free_extent_map(em);
2980 *em_cached = NULL;
2981 }
2982
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02002983 em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
Miao Xie125bac012013-07-25 19:22:37 +08002984 if (em_cached && !IS_ERR_OR_NULL(em)) {
2985 BUG_ON(*em_cached);
Elena Reshetova490b54d2017-03-03 10:55:12 +02002986 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002987 *em_cached = em;
2988 }
2989 return em;
2990}
Chris Masond1310b22008-01-24 16:13:08 -05002991/*
2992 * basic readpage implementation. Locked extent state structs are inserted
2993 * into the tree that are removed when the IO is done (by the end_io
2994 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002995 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07002996 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05002997 */
Miao Xie99740902013-07-25 19:22:36 +08002998static int __do_readpage(struct extent_io_tree *tree,
2999 struct page *page,
3000 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08003001 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003002 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003003 unsigned long *bio_flags, unsigned int read_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003004 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05003005{
3006 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003007 u64 start = page_offset(page);
David Sterba8eec8292017-06-06 19:50:13 +02003008 const u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003009 u64 cur = start;
3010 u64 extent_offset;
3011 u64 last_byte = i_size_read(inode);
3012 u64 block_start;
3013 u64 cur_end;
Chris Masond1310b22008-01-24 16:13:08 -05003014 struct extent_map *em;
3015 struct block_device *bdev;
Liu Bobaf863b2016-07-11 10:39:07 -07003016 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003017 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02003018 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003019 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04003020 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05003021 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00003022 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003023
3024 set_page_extent_mapped(page);
3025
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003026 if (!PageUptodate(page)) {
3027 if (cleancache_get_page(page) == 0) {
3028 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003029 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003030 goto out;
3031 }
3032 }
3033
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003034 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04003035 char *userpage;
Johannes Thumshirn70730172018-12-05 15:23:03 +01003036 size_t zero_offset = offset_in_page(last_byte);
Chris Masonc8b97812008-10-29 14:49:59 -04003037
3038 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003039 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08003040 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04003041 memset(userpage + zero_offset, 0, iosize);
3042 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003043 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04003044 }
3045 }
Chris Masond1310b22008-01-24 16:13:08 -05003046 while (cur <= end) {
Filipe Manana005efed2015-09-14 09:09:31 +01003047 bool force_bio_submit = false;
David Sterba6273b7f2017-10-04 17:30:11 +02003048 u64 offset;
Josef Bacikc8f2f242013-02-11 11:33:00 -05003049
Chris Masond1310b22008-01-24 16:13:08 -05003050 if (cur >= last_byte) {
3051 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003052 struct extent_state *cached = NULL;
3053
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003054 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08003055 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003056 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003057 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003058 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003059 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003060 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003061 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01003062 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003063 break;
3064 }
Miao Xie125bac012013-07-25 19:22:37 +08003065 em = __get_extent_map(inode, page, pg_offset, cur,
3066 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02003067 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003068 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003069 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003070 break;
3071 }
Chris Masond1310b22008-01-24 16:13:08 -05003072 extent_offset = cur - em->start;
3073 BUG_ON(extent_map_end(em) <= cur);
3074 BUG_ON(end < cur);
3075
Li Zefan261507a02010-12-17 14:21:50 +08003076 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07003077 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08003078 extent_set_compress_type(&this_bio_flag,
3079 em->compress_type);
3080 }
Chris Masonc8b97812008-10-29 14:49:59 -04003081
Chris Masond1310b22008-01-24 16:13:08 -05003082 iosize = min(extent_map_end(em) - cur, end - cur + 1);
3083 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00003084 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04003085 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
3086 disk_io_size = em->block_len;
David Sterba6273b7f2017-10-04 17:30:11 +02003087 offset = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003088 } else {
David Sterba6273b7f2017-10-04 17:30:11 +02003089 offset = em->block_start + extent_offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003090 disk_io_size = iosize;
3091 }
Chris Masond1310b22008-01-24 16:13:08 -05003092 bdev = em->bdev;
3093 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04003094 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
3095 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01003096
3097 /*
3098 * If we have a file range that points to a compressed extent
3099 * and it's followed by a consecutive file range that points to
3100 * to the same compressed extent (possibly with a different
3101 * offset and/or length, so it either points to the whole extent
3102 * or only part of it), we must make sure we do not submit a
3103 * single bio to populate the pages for the 2 ranges because
3104 * this makes the compressed extent read zero out the pages
3105 * belonging to the 2nd range. Imagine the following scenario:
3106 *
3107 * File layout
3108 * [0 - 8K] [8K - 24K]
3109 * | |
3110 * | |
3111 * points to extent X, points to extent X,
3112 * offset 4K, length of 8K offset 0, length 16K
3113 *
3114 * [extent X, compressed length = 4K uncompressed length = 16K]
3115 *
3116 * If the bio to read the compressed extent covers both ranges,
3117 * it will decompress extent X into the pages belonging to the
3118 * first range and then it will stop, zeroing out the remaining
3119 * pages that belong to the other range that points to extent X.
3120 * So here we make sure we submit 2 bios, one for the first
3121 * range and another one for the third range. Both will target
3122 * the same physical extent from disk, but we can't currently
3123 * make the compressed bio endio callback populate the pages
3124 * for both ranges because each compressed bio is tightly
3125 * coupled with a single extent map, and each range can have
3126 * an extent map with a different offset value relative to the
3127 * uncompressed data of our extent and different lengths. This
3128 * is a corner case so we prioritize correctness over
3129 * non-optimal behavior (submitting 2 bios for the same extent).
3130 */
3131 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3132 prev_em_start && *prev_em_start != (u64)-1 &&
Filipe Manana8e928212019-02-14 15:17:20 +00003133 *prev_em_start != em->start)
Filipe Manana005efed2015-09-14 09:09:31 +01003134 force_bio_submit = true;
3135
3136 if (prev_em_start)
Filipe Manana8e928212019-02-14 15:17:20 +00003137 *prev_em_start = em->start;
Filipe Manana005efed2015-09-14 09:09:31 +01003138
Chris Masond1310b22008-01-24 16:13:08 -05003139 free_extent_map(em);
3140 em = NULL;
3141
3142 /* we've found a hole, just zero and go on */
3143 if (block_start == EXTENT_MAP_HOLE) {
3144 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003145 struct extent_state *cached = NULL;
3146
Cong Wang7ac687d2011-11-25 23:14:28 +08003147 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003148 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003149 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003150 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003151
3152 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003153 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003154 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01003155 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003156 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003157 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003158 continue;
3159 }
3160 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003161 if (test_range_bit(tree, cur, cur_end,
3162 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003163 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003164 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003165 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003166 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003167 continue;
3168 }
Chris Mason70dec802008-01-29 09:59:12 -05003169 /* we have an inline extent but it didn't get marked up
3170 * to date. Error out
3171 */
3172 if (block_start == EXTENT_MAP_INLINE) {
3173 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003174 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003175 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003176 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003177 continue;
3178 }
Chris Masond1310b22008-01-24 16:13:08 -05003179
David Sterba4b81ba42017-06-06 19:14:26 +02003180 ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL,
David Sterba6273b7f2017-10-04 17:30:11 +02003181 page, offset, disk_io_size,
3182 pg_offset, bdev, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003183 end_bio_extent_readpage, mirror_num,
3184 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003185 this_bio_flag,
3186 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003187 if (!ret) {
3188 nr++;
3189 *bio_flags = this_bio_flag;
3190 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003191 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003192 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003193 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003194 }
Chris Masond1310b22008-01-24 16:13:08 -05003195 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003196 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003197 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003198out:
Chris Masond1310b22008-01-24 16:13:08 -05003199 if (!nr) {
3200 if (!PageError(page))
3201 SetPageUptodate(page);
3202 unlock_page(page);
3203 }
Liu Bobaf863b2016-07-11 10:39:07 -07003204 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003205}
3206
Nikolay Borisove65ef212019-03-11 09:55:38 +02003207static inline void contiguous_readpages(struct extent_io_tree *tree,
Miao Xie99740902013-07-25 19:22:36 +08003208 struct page *pages[], int nr_pages,
3209 u64 start, u64 end,
Miao Xie125bac012013-07-25 19:22:37 +08003210 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003211 struct bio **bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003212 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003213 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003214{
3215 struct inode *inode;
3216 struct btrfs_ordered_extent *ordered;
3217 int index;
3218
3219 inode = pages[0]->mapping->host;
3220 while (1) {
3221 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003222 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Miao Xie99740902013-07-25 19:22:36 +08003223 end - start + 1);
3224 if (!ordered)
3225 break;
3226 unlock_extent(tree, start, end);
3227 btrfs_start_ordered_extent(inode, ordered, 1);
3228 btrfs_put_ordered_extent(ordered);
3229 }
3230
3231 for (index = 0; index < nr_pages; index++) {
David Sterba4ef77692017-06-23 04:09:57 +02003232 __do_readpage(tree, pages[index], btrfs_get_extent, em_cached,
Jens Axboe5e9d3982018-08-17 15:45:39 -07003233 bio, 0, bio_flags, REQ_RAHEAD, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003234 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003235 }
3236}
3237
Miao Xie99740902013-07-25 19:22:36 +08003238static int __extent_read_full_page(struct extent_io_tree *tree,
3239 struct page *page,
3240 get_extent_t *get_extent,
3241 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003242 unsigned long *bio_flags,
3243 unsigned int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003244{
3245 struct inode *inode = page->mapping->host;
3246 struct btrfs_ordered_extent *ordered;
3247 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003248 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003249 int ret;
3250
3251 while (1) {
3252 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003253 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003254 PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003255 if (!ordered)
3256 break;
3257 unlock_extent(tree, start, end);
3258 btrfs_start_ordered_extent(inode, ordered, 1);
3259 btrfs_put_ordered_extent(ordered);
3260 }
3261
Miao Xie125bac012013-07-25 19:22:37 +08003262 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003263 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003264 return ret;
3265}
3266
Chris Masond1310b22008-01-24 16:13:08 -05003267int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003268 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003269{
3270 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003271 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003272 int ret;
3273
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003274 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003275 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003276 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003277 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003278 return ret;
3279}
Chris Masond1310b22008-01-24 16:13:08 -05003280
David Sterba3d4b9492017-02-10 19:33:41 +01003281static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003282 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003283{
3284 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003285}
3286
Chris Masond1310b22008-01-24 16:13:08 -05003287/*
Chris Mason40f76582014-05-21 13:35:51 -07003288 * helper for __extent_writepage, doing all of the delayed allocation setup.
3289 *
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003290 * This returns 1 if btrfs_run_delalloc_range function did all the work required
Chris Mason40f76582014-05-21 13:35:51 -07003291 * to write the page (copy into inline extent). In this case the IO has
3292 * been started and the page is already unlocked.
3293 *
3294 * This returns 0 if all went well (page still locked)
3295 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003296 */
Chris Mason40f76582014-05-21 13:35:51 -07003297static noinline_for_stack int writepage_delalloc(struct inode *inode,
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003298 struct page *page, struct writeback_control *wbc,
3299 u64 delalloc_start, unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003300{
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003301 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003302 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Lu Fengqi3522e902018-11-29 11:33:38 +08003303 bool found;
Chris Mason40f76582014-05-21 13:35:51 -07003304 u64 delalloc_to_write = 0;
3305 u64 delalloc_end = 0;
3306 int ret;
3307 int page_started = 0;
3308
Chris Mason40f76582014-05-21 13:35:51 -07003309
3310 while (delalloc_end < page_end) {
Lu Fengqi3522e902018-11-29 11:33:38 +08003311 found = find_lock_delalloc_range(inode, tree,
Chris Mason40f76582014-05-21 13:35:51 -07003312 page,
3313 &delalloc_start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03003314 &delalloc_end);
Lu Fengqi3522e902018-11-29 11:33:38 +08003315 if (!found) {
Chris Mason40f76582014-05-21 13:35:51 -07003316 delalloc_start = delalloc_end + 1;
3317 continue;
3318 }
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003319 ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
3320 delalloc_end, &page_started, nr_written, wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003321 /* File system has been set read-only */
3322 if (ret) {
3323 SetPageError(page);
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003324 /*
3325 * btrfs_run_delalloc_range should return < 0 for error
3326 * but just in case, we use > 0 here meaning the IO is
3327 * started, so we don't want to return > 0 unless
3328 * things are going well.
Chris Mason40f76582014-05-21 13:35:51 -07003329 */
3330 ret = ret < 0 ? ret : -EIO;
3331 goto done;
3332 }
3333 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003334 * delalloc_end is already one less than the total length, so
3335 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003336 */
3337 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003338 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003339 delalloc_start = delalloc_end + 1;
3340 }
3341 if (wbc->nr_to_write < delalloc_to_write) {
3342 int thresh = 8192;
3343
3344 if (delalloc_to_write < thresh * 2)
3345 thresh = delalloc_to_write;
3346 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3347 thresh);
3348 }
3349
3350 /* did the fill delalloc function already unlock and start
3351 * the IO?
3352 */
3353 if (page_started) {
3354 /*
3355 * we've unlocked the page, so we can't update
3356 * the mapping's writeback index, just update
3357 * nr_to_write.
3358 */
3359 wbc->nr_to_write -= *nr_written;
3360 return 1;
3361 }
3362
3363 ret = 0;
3364
3365done:
3366 return ret;
3367}
3368
3369/*
3370 * helper for __extent_writepage. This calls the writepage start hooks,
3371 * and does the loop to map the page into extents and bios.
3372 *
3373 * We return 1 if the IO is started and the page is unlocked,
3374 * 0 if all went well (page still locked)
3375 * < 0 if there were errors (page still locked)
3376 */
3377static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3378 struct page *page,
3379 struct writeback_control *wbc,
3380 struct extent_page_data *epd,
3381 loff_t i_size,
3382 unsigned long nr_written,
David Sterbaf1c77c52017-06-06 19:03:49 +02003383 unsigned int write_flags, int *nr_ret)
Chris Mason40f76582014-05-21 13:35:51 -07003384{
Chris Masond1310b22008-01-24 16:13:08 -05003385 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003386 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003387 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003388 u64 end;
3389 u64 cur = start;
3390 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003391 u64 block_start;
3392 u64 iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003393 struct extent_map *em;
3394 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003395 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003396 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003397 int ret = 0;
3398 int nr = 0;
3399 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003400
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003401 ret = btrfs_writepage_cow_fixup(page, start, page_end);
3402 if (ret) {
3403 /* Fixup worker will requeue */
3404 if (ret == -EBUSY)
3405 wbc->pages_skipped++;
3406 else
3407 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003408
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003409 update_nr_written(wbc, nr_written);
3410 unlock_page(page);
3411 return 1;
Chris Mason247e7432008-07-17 12:53:51 -04003412 }
3413
Chris Mason11c83492009-04-20 15:50:09 -04003414 /*
3415 * we don't want to touch the inode after unlocking the page,
3416 * so we update the mapping writeback index now
3417 */
David Sterba3d4b9492017-02-10 19:33:41 +01003418 update_nr_written(wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003419
Chris Masond1310b22008-01-24 16:13:08 -05003420 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003421 if (i_size <= start) {
Nikolay Borisovc6297322018-11-08 10:18:08 +02003422 btrfs_writepage_endio_finish_ordered(page, start, page_end, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003423 goto done;
3424 }
3425
Chris Masond1310b22008-01-24 16:13:08 -05003426 blocksize = inode->i_sb->s_blocksize;
3427
3428 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003429 u64 em_end;
David Sterba6273b7f2017-10-04 17:30:11 +02003430 u64 offset;
David Sterba58409ed2016-05-04 11:46:10 +02003431
Chris Mason40f76582014-05-21 13:35:51 -07003432 if (cur >= i_size) {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003433 btrfs_writepage_endio_finish_ordered(page, cur,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003434 page_end, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003435 break;
3436 }
David Sterba3c98c622017-06-23 04:01:08 +02003437 em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003438 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003439 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003440 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003441 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003442 break;
3443 }
3444
3445 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003446 em_end = extent_map_end(em);
3447 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003448 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003449 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003450 iosize = ALIGN(iosize, blocksize);
David Sterba6273b7f2017-10-04 17:30:11 +02003451 offset = em->block_start + extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003452 bdev = em->bdev;
3453 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003454 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003455 free_extent_map(em);
3456 em = NULL;
3457
Chris Masonc8b97812008-10-29 14:49:59 -04003458 /*
3459 * compressed and inline extents are written through other
3460 * paths in the FS
3461 */
3462 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003463 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003464 /*
3465 * end_io notification does not happen here for
3466 * compressed extents
3467 */
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003468 if (!compressed)
3469 btrfs_writepage_endio_finish_ordered(page, cur,
3470 cur + iosize - 1,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003471 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003472 else if (compressed) {
3473 /* we don't want to end_page_writeback on
3474 * a compressed extent. this happens
3475 * elsewhere
3476 */
3477 nr++;
3478 }
3479
3480 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003481 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003482 continue;
3483 }
Chris Masonc8b97812008-10-29 14:49:59 -04003484
David Sterba5cdc84b2018-07-18 20:32:52 +02003485 btrfs_set_range_writeback(tree, cur, cur + iosize - 1);
David Sterba58409ed2016-05-04 11:46:10 +02003486 if (!PageWriteback(page)) {
3487 btrfs_err(BTRFS_I(inode)->root->fs_info,
3488 "page %lu not writeback, cur %llu end %llu",
3489 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003490 }
David Sterba58409ed2016-05-04 11:46:10 +02003491
David Sterba4b81ba42017-06-06 19:14:26 +02003492 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003493 page, offset, iosize, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003494 bdev, &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003495 end_bio_extent_writepage,
3496 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003497 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003498 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003499 if (PageWriteback(page))
3500 end_page_writeback(page);
3501 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003502
Chris Masond1310b22008-01-24 16:13:08 -05003503 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003504 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003505 nr++;
3506 }
3507done:
Chris Mason40f76582014-05-21 13:35:51 -07003508 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003509 return ret;
3510}
3511
3512/*
3513 * the writepage semantics are similar to regular writepage. extent
3514 * records are inserted to lock ranges in the tree, and as dirty areas
3515 * are found, they are marked writeback. Then the lock bits are removed
3516 * and the end_io handler clears the writeback ranges
Qu Wenruo30659762019-03-20 14:27:42 +08003517 *
3518 * Return 0 if everything goes well.
3519 * Return <0 for error.
Chris Mason40f76582014-05-21 13:35:51 -07003520 */
3521static int __extent_writepage(struct page *page, struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003522 struct extent_page_data *epd)
Chris Mason40f76582014-05-21 13:35:51 -07003523{
3524 struct inode *inode = page->mapping->host;
Chris Mason40f76582014-05-21 13:35:51 -07003525 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003526 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003527 int ret;
3528 int nr = 0;
3529 size_t pg_offset = 0;
3530 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003531 unsigned long end_index = i_size >> PAGE_SHIFT;
David Sterbaf1c77c52017-06-06 19:03:49 +02003532 unsigned int write_flags = 0;
Chris Mason40f76582014-05-21 13:35:51 -07003533 unsigned long nr_written = 0;
3534
Liu Boff40adf2017-08-24 18:19:48 -06003535 write_flags = wbc_to_write_flags(wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003536
3537 trace___extent_writepage(page, inode, wbc);
3538
3539 WARN_ON(!PageLocked(page));
3540
3541 ClearPageError(page);
3542
Johannes Thumshirn70730172018-12-05 15:23:03 +01003543 pg_offset = offset_in_page(i_size);
Chris Mason40f76582014-05-21 13:35:51 -07003544 if (page->index > end_index ||
3545 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003546 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003547 unlock_page(page);
3548 return 0;
3549 }
3550
3551 if (page->index == end_index) {
3552 char *userpage;
3553
3554 userpage = kmap_atomic(page);
3555 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003556 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003557 kunmap_atomic(userpage);
3558 flush_dcache_page(page);
3559 }
3560
3561 pg_offset = 0;
3562
3563 set_page_extent_mapped(page);
3564
Nikolay Borisov7789a552018-11-08 10:18:06 +02003565 if (!epd->extent_locked) {
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003566 ret = writepage_delalloc(inode, page, wbc, start, &nr_written);
Nikolay Borisov7789a552018-11-08 10:18:06 +02003567 if (ret == 1)
3568 goto done_unlocked;
3569 if (ret)
3570 goto done;
3571 }
Chris Mason40f76582014-05-21 13:35:51 -07003572
3573 ret = __extent_writepage_io(inode, page, wbc, epd,
3574 i_size, nr_written, write_flags, &nr);
3575 if (ret == 1)
3576 goto done_unlocked;
3577
3578done:
Chris Masond1310b22008-01-24 16:13:08 -05003579 if (nr == 0) {
3580 /* make sure the mapping tag for page dirty gets cleared */
3581 set_page_writeback(page);
3582 end_page_writeback(page);
3583 }
Filipe Manana61391d52014-05-09 17:17:40 +01003584 if (PageError(page)) {
3585 ret = ret < 0 ? ret : -EIO;
3586 end_extent_writepage(page, ret, start, page_end);
3587 }
Chris Masond1310b22008-01-24 16:13:08 -05003588 unlock_page(page);
Qu Wenruo30659762019-03-20 14:27:42 +08003589 ASSERT(ret <= 0);
Chris Mason40f76582014-05-21 13:35:51 -07003590 return ret;
Chris Mason771ed682008-11-06 22:02:51 -05003591
Chris Mason11c83492009-04-20 15:50:09 -04003592done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003593 return 0;
3594}
3595
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003596void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003597{
NeilBrown74316202014-07-07 15:16:04 +10003598 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3599 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003600}
3601
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003602/*
3603 * Lock eb pages and flush the bio if we can't the locks
3604 *
3605 * Return 0 if nothing went wrong
3606 * Return >0 is same as 0, except bio is not submitted
3607 * Return <0 if something went wrong, no page is locked
3608 */
David Sterba9df76fb2019-03-20 11:21:41 +01003609static noinline_for_stack int lock_extent_buffer_for_io(struct extent_buffer *eb,
Chris Mason0e378df2014-05-19 20:55:27 -07003610 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003611{
David Sterba9df76fb2019-03-20 11:21:41 +01003612 struct btrfs_fs_info *fs_info = eb->fs_info;
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003613 int i, num_pages, failed_page_nr;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003614 int flush = 0;
3615 int ret = 0;
3616
3617 if (!btrfs_try_tree_write_lock(eb)) {
Qu Wenruof4340622019-03-20 14:27:41 +08003618 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003619 if (ret < 0)
3620 return ret;
3621 flush = 1;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003622 btrfs_tree_lock(eb);
3623 }
3624
3625 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3626 btrfs_tree_unlock(eb);
3627 if (!epd->sync_io)
3628 return 0;
3629 if (!flush) {
Qu Wenruof4340622019-03-20 14:27:41 +08003630 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003631 if (ret < 0)
3632 return ret;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003633 flush = 1;
3634 }
Chris Masona098d8e82012-03-21 12:09:56 -04003635 while (1) {
3636 wait_on_extent_buffer_writeback(eb);
3637 btrfs_tree_lock(eb);
3638 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3639 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003640 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003641 }
3642 }
3643
Josef Bacik51561ff2012-07-20 16:25:24 -04003644 /*
3645 * We need to do this to prevent races in people who check if the eb is
3646 * under IO since we can end up having no IO bits set for a short period
3647 * of time.
3648 */
3649 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003650 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3651 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003652 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003653 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Nikolay Borisov104b4e52017-06-20 21:01:20 +03003654 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3655 -eb->len,
3656 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003657 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003658 } else {
3659 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003660 }
3661
3662 btrfs_tree_unlock(eb);
3663
3664 if (!ret)
3665 return ret;
3666
David Sterba65ad0102018-06-29 10:56:49 +02003667 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003668 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003669 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003670
3671 if (!trylock_page(p)) {
3672 if (!flush) {
Qu Wenruof4340622019-03-20 14:27:41 +08003673 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003674 if (ret < 0) {
3675 failed_page_nr = i;
3676 goto err_unlock;
3677 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003678 flush = 1;
3679 }
3680 lock_page(p);
3681 }
3682 }
3683
3684 return ret;
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003685err_unlock:
3686 /* Unlock already locked pages */
3687 for (i = 0; i < failed_page_nr; i++)
3688 unlock_page(eb->pages[i]);
3689 return ret;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003690}
3691
3692static void end_extent_buffer_writeback(struct extent_buffer *eb)
3693{
3694 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003695 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003696 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3697}
3698
Filipe Manana656f30d2014-09-26 12:25:56 +01003699static void set_btree_ioerr(struct page *page)
3700{
3701 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Filipe Manana656f30d2014-09-26 12:25:56 +01003702
3703 SetPageError(page);
3704 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3705 return;
3706
3707 /*
3708 * If writeback for a btree extent that doesn't belong to a log tree
3709 * failed, increment the counter transaction->eb_write_errors.
3710 * We do this because while the transaction is running and before it's
3711 * committing (when we call filemap_fdata[write|wait]_range against
3712 * the btree inode), we might have
3713 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3714 * returns an error or an error happens during writeback, when we're
3715 * committing the transaction we wouldn't know about it, since the pages
3716 * can be no longer dirty nor marked anymore for writeback (if a
3717 * subsequent modification to the extent buffer didn't happen before the
3718 * transaction commit), which makes filemap_fdata[write|wait]_range not
3719 * able to find the pages tagged with SetPageError at transaction
3720 * commit time. So if this happens we must abort the transaction,
3721 * otherwise we commit a super block with btree roots that point to
3722 * btree nodes/leafs whose content on disk is invalid - either garbage
3723 * or the content of some node/leaf from a past generation that got
3724 * cowed or deleted and is no longer valid.
3725 *
3726 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3727 * not be enough - we need to distinguish between log tree extents vs
3728 * non-log tree extents, and the next filemap_fdatawait_range() call
3729 * will catch and clear such errors in the mapping - and that call might
3730 * be from a log sync and not from a transaction commit. Also, checking
3731 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3732 * not done and would not be reliable - the eb might have been released
3733 * from memory and reading it back again means that flag would not be
3734 * set (since it's a runtime flag, not persisted on disk).
3735 *
3736 * Using the flags below in the btree inode also makes us achieve the
3737 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3738 * writeback for all dirty pages and before filemap_fdatawait_range()
3739 * is called, the writeback for all dirty pages had already finished
3740 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3741 * filemap_fdatawait_range() would return success, as it could not know
3742 * that writeback errors happened (the pages were no longer tagged for
3743 * writeback).
3744 */
3745 switch (eb->log_index) {
3746 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003747 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003748 break;
3749 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003750 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003751 break;
3752 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003753 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003754 break;
3755 default:
3756 BUG(); /* unexpected, logic error */
3757 }
3758}
3759
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003760static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003761{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003762 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003763 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003764 int i, done;
Ming Lei6dc4f102019-02-15 19:13:19 +08003765 struct bvec_iter_all iter_all;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003766
David Sterbac09abff2017-07-13 18:10:07 +02003767 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08003768 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003769 struct page *page = bvec->bv_page;
3770
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003771 eb = (struct extent_buffer *)page->private;
3772 BUG_ON(!eb);
3773 done = atomic_dec_and_test(&eb->io_pages);
3774
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003775 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003776 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003777 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003778 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003779 }
3780
3781 end_page_writeback(page);
3782
3783 if (!done)
3784 continue;
3785
3786 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003787 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003788
3789 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003790}
3791
Chris Mason0e378df2014-05-19 20:55:27 -07003792static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003793 struct writeback_control *wbc,
3794 struct extent_page_data *epd)
3795{
David Sterba0ab02062019-03-20 11:27:57 +01003796 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003797 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003798 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003799 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003800 u32 nritems;
David Sterbacc5e31a2018-03-01 18:20:27 +01003801 int i, num_pages;
Liu Bo851cd172016-09-23 13:44:44 -07003802 unsigned long start, end;
Liu Boff40adf2017-08-24 18:19:48 -06003803 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003804 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003805
Filipe Manana656f30d2014-09-26 12:25:56 +01003806 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02003807 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003808 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003809
Liu Bo851cd172016-09-23 13:44:44 -07003810 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3811 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003812 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003813 end = btrfs_node_key_ptr_offset(nritems);
3814
David Sterbab159fa22016-11-08 18:09:03 +01003815 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003816 } else {
3817 /*
3818 * leaf:
3819 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3820 */
3821 start = btrfs_item_nr_offset(nritems);
David Sterba8f881e82019-03-20 11:33:10 +01003822 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(eb);
David Sterbab159fa22016-11-08 18:09:03 +01003823 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003824 }
3825
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003826 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003827 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003828
3829 clear_page_dirty_for_io(p);
3830 set_page_writeback(p);
David Sterba4b81ba42017-06-06 19:14:26 +02003831 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003832 p, offset, PAGE_SIZE, 0, bdev,
David Sterbac2df8bb2017-02-10 19:29:38 +01003833 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003834 end_bio_extent_buffer_writepage,
Liu Bo18fdc672017-09-13 12:18:22 -06003835 0, 0, 0, false);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003836 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003837 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003838 if (PageWriteback(p))
3839 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003840 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3841 end_extent_buffer_writeback(eb);
3842 ret = -EIO;
3843 break;
3844 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003845 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003846 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003847 unlock_page(p);
3848 }
3849
3850 if (unlikely(ret)) {
3851 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003852 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003853 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003854 unlock_page(p);
3855 }
3856 }
3857
3858 return ret;
3859}
3860
3861int btree_write_cache_pages(struct address_space *mapping,
3862 struct writeback_control *wbc)
3863{
3864 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003865 struct extent_buffer *eb, *prev_eb = NULL;
3866 struct extent_page_data epd = {
3867 .bio = NULL,
3868 .tree = tree,
3869 .extent_locked = 0,
3870 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3871 };
3872 int ret = 0;
3873 int done = 0;
3874 int nr_to_write_done = 0;
3875 struct pagevec pvec;
3876 int nr_pages;
3877 pgoff_t index;
3878 pgoff_t end; /* Inclusive */
3879 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003880 xa_mark_t tag;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003881
Mel Gorman86679822017-11-15 17:37:52 -08003882 pagevec_init(&pvec);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003883 if (wbc->range_cyclic) {
3884 index = mapping->writeback_index; /* Start from prev offset */
3885 end = -1;
3886 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003887 index = wbc->range_start >> PAGE_SHIFT;
3888 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003889 scanned = 1;
3890 }
3891 if (wbc->sync_mode == WB_SYNC_ALL)
3892 tag = PAGECACHE_TAG_TOWRITE;
3893 else
3894 tag = PAGECACHE_TAG_DIRTY;
3895retry:
3896 if (wbc->sync_mode == WB_SYNC_ALL)
3897 tag_pages_for_writeback(mapping, index, end);
3898 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara4006f432017-11-15 17:34:37 -08003899 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08003900 tag))) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003901 unsigned i;
3902
3903 scanned = 1;
3904 for (i = 0; i < nr_pages; i++) {
3905 struct page *page = pvec.pages[i];
3906
3907 if (!PagePrivate(page))
3908 continue;
3909
Josef Bacikb5bae262012-09-14 13:43:01 -04003910 spin_lock(&mapping->private_lock);
3911 if (!PagePrivate(page)) {
3912 spin_unlock(&mapping->private_lock);
3913 continue;
3914 }
3915
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003916 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003917
3918 /*
3919 * Shouldn't happen and normally this would be a BUG_ON
3920 * but no sense in crashing the users box for something
3921 * we can survive anyway.
3922 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303923 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003924 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003925 continue;
3926 }
3927
Josef Bacikb5bae262012-09-14 13:43:01 -04003928 if (eb == prev_eb) {
3929 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003930 continue;
3931 }
3932
Josef Bacikb5bae262012-09-14 13:43:01 -04003933 ret = atomic_inc_not_zero(&eb->refs);
3934 spin_unlock(&mapping->private_lock);
3935 if (!ret)
3936 continue;
3937
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003938 prev_eb = eb;
David Sterba9df76fb2019-03-20 11:21:41 +01003939 ret = lock_extent_buffer_for_io(eb, &epd);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003940 if (!ret) {
3941 free_extent_buffer(eb);
3942 continue;
3943 }
3944
David Sterba0ab02062019-03-20 11:27:57 +01003945 ret = write_one_eb(eb, wbc, &epd);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003946 if (ret) {
3947 done = 1;
3948 free_extent_buffer(eb);
3949 break;
3950 }
3951 free_extent_buffer(eb);
3952
3953 /*
3954 * the filesystem may choose to bump up nr_to_write.
3955 * We have to make sure to honor the new nr_to_write
3956 * at any time
3957 */
3958 nr_to_write_done = wbc->nr_to_write <= 0;
3959 }
3960 pagevec_release(&pvec);
3961 cond_resched();
3962 }
3963 if (!scanned && !done) {
3964 /*
3965 * We hit the last page and there is more work to be done: wrap
3966 * back to the start of the file
3967 */
3968 scanned = 1;
3969 index = 0;
3970 goto retry;
3971 }
Qu Wenruo2b952ee2019-03-20 14:27:43 +08003972 ASSERT(ret <= 0);
3973 if (ret < 0) {
3974 end_write_bio(&epd, ret);
3975 return ret;
3976 }
3977 ret = flush_write_bio(&epd);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003978 return ret;
3979}
3980
Chris Masond1310b22008-01-24 16:13:08 -05003981/**
Chris Mason4bef0842008-09-08 11:18:08 -04003982 * 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 -05003983 * @mapping: address space structure to write
3984 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
David Sterba935db852017-06-23 04:30:28 +02003985 * @data: data passed to __extent_writepage function
Chris Masond1310b22008-01-24 16:13:08 -05003986 *
3987 * If a page is already under I/O, write_cache_pages() skips it, even
3988 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3989 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3990 * and msync() need to guarantee that all the data which was dirty at the time
3991 * the call was made get new I/O started against them. If wbc->sync_mode is
3992 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3993 * existing IO to complete.
3994 */
David Sterba4242b642017-02-10 19:38:24 +01003995static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04003996 struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003997 struct extent_page_data *epd)
Chris Masond1310b22008-01-24 16:13:08 -05003998{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003999 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05004000 int ret = 0;
4001 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004002 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004003 struct pagevec pvec;
4004 int nr_pages;
4005 pgoff_t index;
4006 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08004007 pgoff_t done_index;
4008 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004009 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05004010 xa_mark_t tag;
Chris Masond1310b22008-01-24 16:13:08 -05004011
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004012 /*
4013 * We have to hold onto the inode so that ordered extents can do their
4014 * work when the IO finishes. The alternative to this is failing to add
4015 * an ordered extent if the igrab() fails there and that is a huge pain
4016 * to deal with, so instead just hold onto the inode throughout the
4017 * writepages operation. If it fails here we are freeing up the inode
4018 * anyway and we'd rather not waste our time writing out stuff that is
4019 * going to be truncated anyway.
4020 */
4021 if (!igrab(inode))
4022 return 0;
4023
Mel Gorman86679822017-11-15 17:37:52 -08004024 pagevec_init(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05004025 if (wbc->range_cyclic) {
4026 index = mapping->writeback_index; /* Start from prev offset */
4027 end = -1;
4028 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004029 index = wbc->range_start >> PAGE_SHIFT;
4030 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08004031 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
4032 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004033 scanned = 1;
4034 }
Ethan Lien3cd24c62018-11-01 14:49:03 +08004035
4036 /*
4037 * We do the tagged writepage as long as the snapshot flush bit is set
4038 * and we are the first one who do the filemap_flush() on this inode.
4039 *
4040 * The nr_to_write == LONG_MAX is needed to make sure other flushers do
4041 * not race in and drop the bit.
4042 */
4043 if (range_whole && wbc->nr_to_write == LONG_MAX &&
4044 test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
4045 &BTRFS_I(inode)->runtime_flags))
4046 wbc->tagged_writepages = 1;
4047
4048 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00004049 tag = PAGECACHE_TAG_TOWRITE;
4050 else
4051 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05004052retry:
Ethan Lien3cd24c62018-11-01 14:49:03 +08004053 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00004054 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08004055 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004056 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara67fd7072017-11-15 17:35:19 -08004057 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
4058 &index, end, tag))) {
Chris Masond1310b22008-01-24 16:13:08 -05004059 unsigned i;
4060
4061 scanned = 1;
4062 for (i = 0; i < nr_pages; i++) {
4063 struct page *page = pvec.pages[i];
4064
Liu Boa91326672016-03-07 16:56:21 -08004065 done_index = page->index;
Chris Masond1310b22008-01-24 16:13:08 -05004066 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07004067 * At this point we hold neither the i_pages lock nor
4068 * the page lock: the page may be truncated or
4069 * invalidated (changing page->mapping to NULL),
4070 * or even swizzled back from swapper_space to
4071 * tmpfs file mapping
Chris Masond1310b22008-01-24 16:13:08 -05004072 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05004073 if (!trylock_page(page)) {
Qu Wenruof4340622019-03-20 14:27:41 +08004074 ret = flush_write_bio(epd);
4075 BUG_ON(ret < 0);
Josef Bacikc8f2f242013-02-11 11:33:00 -05004076 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04004077 }
Chris Masond1310b22008-01-24 16:13:08 -05004078
4079 if (unlikely(page->mapping != mapping)) {
4080 unlock_page(page);
4081 continue;
4082 }
4083
Chris Masond2c3f4f2008-11-19 12:44:22 -05004084 if (wbc->sync_mode != WB_SYNC_NONE) {
Qu Wenruof4340622019-03-20 14:27:41 +08004085 if (PageWriteback(page)) {
4086 ret = flush_write_bio(epd);
4087 BUG_ON(ret < 0);
4088 }
Chris Masond1310b22008-01-24 16:13:08 -05004089 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004090 }
Chris Masond1310b22008-01-24 16:13:08 -05004091
4092 if (PageWriteback(page) ||
4093 !clear_page_dirty_for_io(page)) {
4094 unlock_page(page);
4095 continue;
4096 }
4097
David Sterbaaab6e9e2017-11-30 18:00:02 +01004098 ret = __extent_writepage(page, wbc, epd);
Liu Boa91326672016-03-07 16:56:21 -08004099 if (ret < 0) {
4100 /*
4101 * done_index is set past this page,
4102 * so media errors will not choke
4103 * background writeout for the entire
4104 * file. This has consequences for
4105 * range_cyclic semantics (ie. it may
4106 * not be suitable for data integrity
4107 * writeout).
4108 */
4109 done_index = page->index + 1;
4110 done = 1;
4111 break;
4112 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004113
4114 /*
4115 * the filesystem may choose to bump up nr_to_write.
4116 * We have to make sure to honor the new nr_to_write
4117 * at any time
4118 */
4119 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004120 }
4121 pagevec_release(&pvec);
4122 cond_resched();
4123 }
Liu Bo894b36e2016-03-07 16:56:22 -08004124 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004125 /*
4126 * We hit the last page and there is more work to be done: wrap
4127 * back to the start of the file
4128 */
4129 scanned = 1;
4130 index = 0;
4131 goto retry;
4132 }
Liu Boa91326672016-03-07 16:56:21 -08004133
4134 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4135 mapping->writeback_index = done_index;
4136
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004137 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004138 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004139}
Chris Masond1310b22008-01-24 16:13:08 -05004140
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004141int extent_write_full_page(struct page *page, struct writeback_control *wbc)
Chris Masond1310b22008-01-24 16:13:08 -05004142{
4143 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004144 struct extent_page_data epd = {
4145 .bio = NULL,
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004146 .tree = &BTRFS_I(page->mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004147 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004148 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004149 };
Chris Masond1310b22008-01-24 16:13:08 -05004150
Chris Masond1310b22008-01-24 16:13:08 -05004151 ret = __extent_writepage(page, wbc, &epd);
Qu Wenruo30659762019-03-20 14:27:42 +08004152 ASSERT(ret <= 0);
4153 if (ret < 0) {
4154 end_write_bio(&epd, ret);
4155 return ret;
4156 }
Chris Masond1310b22008-01-24 16:13:08 -05004157
Qu Wenruo30659762019-03-20 14:27:42 +08004158 ret = flush_write_bio(&epd);
4159 ASSERT(ret <= 0);
Chris Masond1310b22008-01-24 16:13:08 -05004160 return ret;
4161}
Chris Masond1310b22008-01-24 16:13:08 -05004162
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004163int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
Chris Mason771ed682008-11-06 22:02:51 -05004164 int mode)
4165{
4166 int ret = 0;
4167 struct address_space *mapping = inode->i_mapping;
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004168 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Mason771ed682008-11-06 22:02:51 -05004169 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004170 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4171 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004172
4173 struct extent_page_data epd = {
4174 .bio = NULL,
4175 .tree = tree,
Chris Mason771ed682008-11-06 22:02:51 -05004176 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004177 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05004178 };
4179 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004180 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004181 .nr_to_write = nr_pages * 2,
4182 .range_start = start,
4183 .range_end = end + 1,
4184 };
4185
Chris Masond3977122009-01-05 21:25:51 -05004186 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004187 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004188 if (clear_page_dirty_for_io(page))
4189 ret = __extent_writepage(page, &wbc_writepages, &epd);
4190 else {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02004191 btrfs_writepage_endio_finish_ordered(page, start,
Nikolay Borisovc6297322018-11-08 10:18:08 +02004192 start + PAGE_SIZE - 1, 1);
Chris Mason771ed682008-11-06 22:02:51 -05004193 unlock_page(page);
4194 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004195 put_page(page);
4196 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004197 }
4198
Qu Wenruo02c6db42019-03-20 14:27:45 +08004199 ASSERT(ret <= 0);
4200 if (ret < 0) {
4201 end_write_bio(&epd, ret);
4202 return ret;
4203 }
4204 ret = flush_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004205 return ret;
4206}
Chris Masond1310b22008-01-24 16:13:08 -05004207
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004208int extent_writepages(struct address_space *mapping,
Chris Masond1310b22008-01-24 16:13:08 -05004209 struct writeback_control *wbc)
4210{
4211 int ret = 0;
4212 struct extent_page_data epd = {
4213 .bio = NULL,
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004214 .tree = &BTRFS_I(mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004215 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004216 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004217 };
4218
David Sterba935db852017-06-23 04:30:28 +02004219 ret = extent_write_cache_pages(mapping, wbc, &epd);
Qu Wenruoa2a72fb2019-03-20 14:27:48 +08004220 ASSERT(ret <= 0);
4221 if (ret < 0) {
4222 end_write_bio(&epd, ret);
4223 return ret;
4224 }
4225 ret = flush_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004226 return ret;
4227}
Chris Masond1310b22008-01-24 16:13:08 -05004228
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004229int extent_readpages(struct address_space *mapping, struct list_head *pages,
4230 unsigned nr_pages)
Chris Masond1310b22008-01-24 16:13:08 -05004231{
4232 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004233 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004234 struct page *pagepool[16];
Miao Xie125bac012013-07-25 19:22:37 +08004235 struct extent_map *em_cached = NULL;
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004236 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Liu Bo67c96842012-07-20 21:43:09 -06004237 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004238 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004239
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004240 while (!list_empty(pages)) {
Nikolay Borisove65ef212019-03-11 09:55:38 +02004241 u64 contig_end = 0;
4242
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004243 for (nr = 0; nr < ARRAY_SIZE(pagepool) && !list_empty(pages);) {
Nikolay Borisovf86196e2019-01-03 15:29:02 -08004244 struct page *page = lru_to_page(pages);
Chris Masond1310b22008-01-24 16:13:08 -05004245
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004246 prefetchw(&page->flags);
4247 list_del(&page->lru);
4248 if (add_to_page_cache_lru(page, mapping, page->index,
4249 readahead_gfp_mask(mapping))) {
4250 put_page(page);
Nikolay Borisove65ef212019-03-11 09:55:38 +02004251 break;
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004252 }
4253
4254 pagepool[nr++] = page;
Nikolay Borisove65ef212019-03-11 09:55:38 +02004255 contig_end = page_offset(page) + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004256 }
Liu Bo67c96842012-07-20 21:43:09 -06004257
Nikolay Borisove65ef212019-03-11 09:55:38 +02004258 if (nr) {
4259 u64 contig_start = page_offset(pagepool[0]);
4260
4261 ASSERT(contig_start + nr * PAGE_SIZE - 1 == contig_end);
4262
4263 contiguous_readpages(tree, pagepool, nr, contig_start,
4264 contig_end, &em_cached, &bio, &bio_flags,
4265 &prev_em_start);
4266 }
Chris Masond1310b22008-01-24 16:13:08 -05004267 }
Liu Bo67c96842012-07-20 21:43:09 -06004268
Miao Xie125bac012013-07-25 19:22:37 +08004269 if (em_cached)
4270 free_extent_map(em_cached);
4271
Chris Masond1310b22008-01-24 16:13:08 -05004272 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004273 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004274 return 0;
4275}
Chris Masond1310b22008-01-24 16:13:08 -05004276
4277/*
4278 * basic invalidatepage code, this waits on any locked or writeback
4279 * ranges corresponding to the page, and then deletes any extent state
4280 * records from the tree
4281 */
4282int extent_invalidatepage(struct extent_io_tree *tree,
4283 struct page *page, unsigned long offset)
4284{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004285 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004286 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004287 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004288 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4289
Qu Wenruofda28322013-02-26 08:10:22 +00004290 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004291 if (start > end)
4292 return 0;
4293
David Sterbaff13db42015-12-03 14:30:40 +01004294 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004295 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004296 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004297 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4298 EXTENT_DO_ACCOUNTING,
David Sterbaae0f1622017-10-31 16:37:52 +01004299 1, 1, &cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05004300 return 0;
4301}
Chris Masond1310b22008-01-24 16:13:08 -05004302
4303/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004304 * a helper for releasepage, this tests for areas of the page that
4305 * are locked or under IO and drops the related state bits if it is safe
4306 * to drop the page.
4307 */
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004308static int try_release_extent_state(struct extent_io_tree *tree,
Eric Sandeen48a3b632013-04-25 20:41:01 +00004309 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004310{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004311 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004312 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004313 int ret = 1;
4314
Nikolay Borisov88826792019-03-14 15:28:31 +02004315 if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) {
Chris Mason7b13b7b2008-04-18 10:29:50 -04004316 ret = 0;
Nikolay Borisov88826792019-03-14 15:28:31 +02004317 } else {
Chris Mason11ef1602009-09-23 20:28:46 -04004318 /*
4319 * at this point we can safely clear everything except the
4320 * locked bit and the nodatasum bit
4321 */
David Sterba66b0c882017-10-31 16:30:47 +01004322 ret = __clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004323 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
David Sterba66b0c882017-10-31 16:30:47 +01004324 0, 0, NULL, mask, NULL);
Chris Masone3f24cc2011-02-14 12:52:08 -05004325
4326 /* if clear_extent_bit failed for enomem reasons,
4327 * we can't allow the release to continue.
4328 */
4329 if (ret < 0)
4330 ret = 0;
4331 else
4332 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004333 }
4334 return ret;
4335}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004336
4337/*
Chris Masond1310b22008-01-24 16:13:08 -05004338 * a helper for releasepage. As long as there are no locked extents
4339 * in the range corresponding to the page, both state records and extent
4340 * map records are removed
4341 */
Nikolay Borisov477a30b2018-04-19 10:46:34 +03004342int try_release_extent_mapping(struct page *page, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004343{
4344 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004345 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004346 u64 end = start + PAGE_SIZE - 1;
Filipe Mananabd3599a2018-07-12 01:36:43 +01004347 struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
4348 struct extent_io_tree *tree = &btrfs_inode->io_tree;
4349 struct extent_map_tree *map = &btrfs_inode->extent_tree;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004350
Mel Gormand0164ad2015-11-06 16:28:21 -08004351 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004352 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004353 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004354 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004355 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004356 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004357 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004358 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004359 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004360 break;
4361 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004362 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4363 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004364 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004365 free_extent_map(em);
4366 break;
4367 }
4368 if (!test_range_bit(tree, em->start,
4369 extent_map_end(em) - 1,
Nikolay Borisov4e586ca2019-03-14 15:28:30 +02004370 EXTENT_LOCKED, 0, NULL)) {
Filipe Mananabd3599a2018-07-12 01:36:43 +01004371 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4372 &btrfs_inode->runtime_flags);
Chris Mason70dec802008-01-29 09:59:12 -05004373 remove_extent_mapping(map, em);
4374 /* once for the rb tree */
4375 free_extent_map(em);
4376 }
4377 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004378 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004379
4380 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004381 free_extent_map(em);
4382 }
Chris Masond1310b22008-01-24 16:13:08 -05004383 }
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004384 return try_release_extent_state(tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004385}
Chris Masond1310b22008-01-24 16:13:08 -05004386
Chris Masonec29ed52011-02-23 16:23:20 -05004387/*
4388 * helper function for fiemap, which doesn't want to see any holes.
4389 * This maps until we find something past 'last'
4390 */
4391static struct extent_map *get_extent_skip_holes(struct inode *inode,
David Sterbae3350e12017-06-23 04:09:57 +02004392 u64 offset, u64 last)
Chris Masonec29ed52011-02-23 16:23:20 -05004393{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004394 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004395 struct extent_map *em;
4396 u64 len;
4397
4398 if (offset >= last)
4399 return NULL;
4400
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304401 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004402 len = last - offset;
4403 if (len == 0)
4404 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004405 len = ALIGN(len, sectorsize);
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02004406 em = btrfs_get_extent_fiemap(BTRFS_I(inode), offset, len);
David Sterbac7040052011-04-19 18:00:01 +02004407 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004408 return em;
4409
4410 /* if this isn't a hole return it */
Nikolay Borisov4a2d25c2017-11-23 10:51:43 +02004411 if (em->block_start != EXTENT_MAP_HOLE)
Chris Masonec29ed52011-02-23 16:23:20 -05004412 return em;
Chris Masonec29ed52011-02-23 16:23:20 -05004413
4414 /* this is a hole, advance to the next extent */
4415 offset = extent_map_end(em);
4416 free_extent_map(em);
4417 if (offset >= last)
4418 break;
4419 }
4420 return NULL;
4421}
4422
Qu Wenruo47518322017-04-07 10:43:15 +08004423/*
4424 * To cache previous fiemap extent
4425 *
4426 * Will be used for merging fiemap extent
4427 */
4428struct fiemap_cache {
4429 u64 offset;
4430 u64 phys;
4431 u64 len;
4432 u32 flags;
4433 bool cached;
4434};
4435
4436/*
4437 * Helper to submit fiemap extent.
4438 *
4439 * Will try to merge current fiemap extent specified by @offset, @phys,
4440 * @len and @flags with cached one.
4441 * And only when we fails to merge, cached one will be submitted as
4442 * fiemap extent.
4443 *
4444 * Return value is the same as fiemap_fill_next_extent().
4445 */
4446static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4447 struct fiemap_cache *cache,
4448 u64 offset, u64 phys, u64 len, u32 flags)
4449{
4450 int ret = 0;
4451
4452 if (!cache->cached)
4453 goto assign;
4454
4455 /*
4456 * Sanity check, extent_fiemap() should have ensured that new
Andrea Gelmini52042d82018-11-28 12:05:13 +01004457 * fiemap extent won't overlap with cached one.
Qu Wenruo47518322017-04-07 10:43:15 +08004458 * Not recoverable.
4459 *
4460 * NOTE: Physical address can overlap, due to compression
4461 */
4462 if (cache->offset + cache->len > offset) {
4463 WARN_ON(1);
4464 return -EINVAL;
4465 }
4466
4467 /*
4468 * Only merges fiemap extents if
4469 * 1) Their logical addresses are continuous
4470 *
4471 * 2) Their physical addresses are continuous
4472 * So truly compressed (physical size smaller than logical size)
4473 * extents won't get merged with each other
4474 *
4475 * 3) Share same flags except FIEMAP_EXTENT_LAST
4476 * So regular extent won't get merged with prealloc extent
4477 */
4478 if (cache->offset + cache->len == offset &&
4479 cache->phys + cache->len == phys &&
4480 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4481 (flags & ~FIEMAP_EXTENT_LAST)) {
4482 cache->len += len;
4483 cache->flags |= flags;
4484 goto try_submit_last;
4485 }
4486
4487 /* Not mergeable, need to submit cached one */
4488 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4489 cache->len, cache->flags);
4490 cache->cached = false;
4491 if (ret)
4492 return ret;
4493assign:
4494 cache->cached = true;
4495 cache->offset = offset;
4496 cache->phys = phys;
4497 cache->len = len;
4498 cache->flags = flags;
4499try_submit_last:
4500 if (cache->flags & FIEMAP_EXTENT_LAST) {
4501 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4502 cache->phys, cache->len, cache->flags);
4503 cache->cached = false;
4504 }
4505 return ret;
4506}
4507
4508/*
Qu Wenruo848c23b2017-06-22 10:01:21 +08004509 * Emit last fiemap cache
Qu Wenruo47518322017-04-07 10:43:15 +08004510 *
Qu Wenruo848c23b2017-06-22 10:01:21 +08004511 * The last fiemap cache may still be cached in the following case:
4512 * 0 4k 8k
4513 * |<- Fiemap range ->|
4514 * |<------------ First extent ----------->|
4515 *
4516 * In this case, the first extent range will be cached but not emitted.
4517 * So we must emit it before ending extent_fiemap().
Qu Wenruo47518322017-04-07 10:43:15 +08004518 */
Qu Wenruo848c23b2017-06-22 10:01:21 +08004519static int emit_last_fiemap_cache(struct btrfs_fs_info *fs_info,
4520 struct fiemap_extent_info *fieinfo,
4521 struct fiemap_cache *cache)
Qu Wenruo47518322017-04-07 10:43:15 +08004522{
4523 int ret;
4524
4525 if (!cache->cached)
4526 return 0;
4527
Qu Wenruo47518322017-04-07 10:43:15 +08004528 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4529 cache->len, cache->flags);
4530 cache->cached = false;
4531 if (ret > 0)
4532 ret = 0;
4533 return ret;
4534}
4535
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004536int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterba2135fb92017-06-23 04:09:57 +02004537 __u64 start, __u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004538{
Josef Bacik975f84f2010-11-23 19:36:57 +00004539 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004540 u64 off = start;
4541 u64 max = start + len;
4542 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004543 u32 found_type;
4544 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004545 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004546 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004547 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004548 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004549 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004550 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004551 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004552 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004553 struct fiemap_cache cache = { 0 };
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004554 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004555 u64 em_start = 0;
4556 u64 em_len = 0;
4557 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004558
4559 if (len == 0)
4560 return -EINVAL;
4561
Josef Bacik975f84f2010-11-23 19:36:57 +00004562 path = btrfs_alloc_path();
4563 if (!path)
4564 return -ENOMEM;
4565 path->leave_spinning = 1;
4566
Jeff Mahoneyda170662016-06-15 09:22:56 -04004567 start = round_down(start, btrfs_inode_sectorsize(inode));
4568 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004569
Chris Masonec29ed52011-02-23 16:23:20 -05004570 /*
4571 * lookup the last file extent. We're not using i_size here
4572 * because there might be preallocation past i_size
4573 */
David Sterbaf85b7372017-01-20 14:54:07 +01004574 ret = btrfs_lookup_file_extent(NULL, root, path,
4575 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004576 if (ret < 0) {
4577 btrfs_free_path(path);
4578 return ret;
Liu Bo2d324f52016-05-17 17:21:48 -07004579 } else {
4580 WARN_ON(!ret);
4581 if (ret == 1)
4582 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004583 }
Liu Bo2d324f52016-05-17 17:21:48 -07004584
Josef Bacik975f84f2010-11-23 19:36:57 +00004585 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004586 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004587 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004588
Chris Masonec29ed52011-02-23 16:23:20 -05004589 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004590 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004591 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004592 /* have to trust i_size as the end */
4593 last = (u64)-1;
4594 last_for_get_extent = isize;
4595 } else {
4596 /*
4597 * remember the start of the last extent. There are a
4598 * bunch of different factors that go into the length of the
4599 * extent, so its much less complex to remember where it started
4600 */
4601 last = found_key.offset;
4602 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004603 }
Liu Bofe09e162013-09-22 12:54:23 +08004604 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004605
Chris Masonec29ed52011-02-23 16:23:20 -05004606 /*
4607 * we might have some extents allocated but more delalloc past those
4608 * extents. so, we trust isize unless the start of the last extent is
4609 * beyond isize
4610 */
4611 if (last < isize) {
4612 last = (u64)-1;
4613 last_for_get_extent = isize;
4614 }
4615
David Sterbaff13db42015-12-03 14:30:40 +01004616 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004617 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004618
David Sterbae3350e12017-06-23 04:09:57 +02004619 em = get_extent_skip_holes(inode, start, last_for_get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004620 if (!em)
4621 goto out;
4622 if (IS_ERR(em)) {
4623 ret = PTR_ERR(em);
4624 goto out;
4625 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004626
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004627 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004628 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004629
Chris Masonea8efc72011-03-08 11:54:40 -05004630 /* break if the extent we found is outside the range */
4631 if (em->start >= max || extent_map_end(em) < off)
4632 break;
4633
4634 /*
4635 * get_extent may return an extent that starts before our
4636 * requested range. We have to make sure the ranges
4637 * we return to fiemap always move forward and don't
4638 * overlap, so adjust the offsets here
4639 */
4640 em_start = max(em->start, off);
4641
4642 /*
4643 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004644 * for adjusting the disk offset below. Only do this if the
4645 * extent isn't compressed since our in ram offset may be past
4646 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004647 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004648 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4649 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004650 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004651 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004652 flags = 0;
Filipe Mananaf0986312018-06-20 10:02:30 +01004653 if (em->block_start < EXTENT_MAP_LAST_BYTE)
4654 disko = em->block_start + offset_in_extent;
4655 else
4656 disko = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004657
Chris Masonea8efc72011-03-08 11:54:40 -05004658 /*
4659 * bump off for our next call to get_extent
4660 */
4661 off = extent_map_end(em);
4662 if (off >= max)
4663 end = 1;
4664
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004665 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004666 end = 1;
4667 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004668 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004669 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4670 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004671 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004672 flags |= (FIEMAP_EXTENT_DELALLOC |
4673 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004674 } else if (fieinfo->fi_extents_max) {
4675 u64 bytenr = em->block_start -
4676 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004677
Liu Bofe09e162013-09-22 12:54:23 +08004678 /*
4679 * As btrfs supports shared space, this information
4680 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004681 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4682 * then we're just getting a count and we can skip the
4683 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004684 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06004685 ret = btrfs_check_shared(root,
4686 btrfs_ino(BTRFS_I(inode)),
4687 bytenr);
Josef Bacikdc046b12014-09-10 16:20:45 -04004688 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004689 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004690 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004691 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004692 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004693 }
4694 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4695 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004696 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4697 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004698
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004699 free_extent_map(em);
4700 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004701 if ((em_start >= last) || em_len == (u64)-1 ||
4702 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004703 flags |= FIEMAP_EXTENT_LAST;
4704 end = 1;
4705 }
4706
Chris Masonec29ed52011-02-23 16:23:20 -05004707 /* now scan forward to see if this is really the last extent. */
David Sterbae3350e12017-06-23 04:09:57 +02004708 em = get_extent_skip_holes(inode, off, last_for_get_extent);
Chris Masonec29ed52011-02-23 16:23:20 -05004709 if (IS_ERR(em)) {
4710 ret = PTR_ERR(em);
4711 goto out;
4712 }
4713 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004714 flags |= FIEMAP_EXTENT_LAST;
4715 end = 1;
4716 }
Qu Wenruo47518322017-04-07 10:43:15 +08004717 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4718 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004719 if (ret) {
4720 if (ret == 1)
4721 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004722 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004723 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004724 }
4725out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004726 if (!ret)
Qu Wenruo848c23b2017-06-22 10:01:21 +08004727 ret = emit_last_fiemap_cache(root->fs_info, fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004728 free_extent_map(em);
4729out:
Liu Bofe09e162013-09-22 12:54:23 +08004730 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004731 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004732 &cached_state);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004733 return ret;
4734}
4735
Chris Mason727011e2010-08-06 13:21:20 -04004736static void __free_extent_buffer(struct extent_buffer *eb)
4737{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004738 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004739 kmem_cache_free(extent_buffer_cache, eb);
4740}
4741
Josef Bacika26e8c92014-03-28 17:07:27 -04004742int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004743{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004744 return (atomic_read(&eb->io_pages) ||
4745 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4746 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004747}
4748
Miao Xie897ca6e92010-10-26 20:57:29 -04004749/*
David Sterba55ac0132018-07-19 17:24:32 +02004750 * Release all pages attached to the extent buffer.
Miao Xie897ca6e92010-10-26 20:57:29 -04004751 */
David Sterba55ac0132018-07-19 17:24:32 +02004752static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004753{
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004754 int i;
4755 int num_pages;
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004756 int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004757
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004758 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004759
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004760 num_pages = num_extent_pages(eb);
4761 for (i = 0; i < num_pages; i++) {
4762 struct page *page = eb->pages[i];
Miao Xie897ca6e92010-10-26 20:57:29 -04004763
Forrest Liu5d2361d2015-02-09 17:31:45 +08004764 if (!page)
4765 continue;
4766 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004767 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004768 /*
4769 * We do this since we'll remove the pages after we've
4770 * removed the eb from the radix tree, so we could race
4771 * and have this page now attached to the new eb. So
4772 * only clear page_private if it's still connected to
4773 * this eb.
4774 */
4775 if (PagePrivate(page) &&
4776 page->private == (unsigned long)eb) {
4777 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4778 BUG_ON(PageDirty(page));
4779 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004780 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004781 * We need to make sure we haven't be attached
4782 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004783 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004784 ClearPagePrivate(page);
4785 set_page_private(page, 0);
4786 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004787 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004788 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004789
4790 if (mapped)
4791 spin_unlock(&page->mapping->private_lock);
4792
Nicholas D Steeves01327612016-05-19 21:18:45 -04004793 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004794 put_page(page);
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004795 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004796}
4797
4798/*
4799 * Helper for releasing the extent buffer.
4800 */
4801static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4802{
David Sterba55ac0132018-07-19 17:24:32 +02004803 btrfs_release_extent_buffer_pages(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004804 __free_extent_buffer(eb);
4805}
4806
Josef Bacikf28491e2013-12-16 13:24:27 -05004807static struct extent_buffer *
4808__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004809 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004810{
4811 struct extent_buffer *eb = NULL;
4812
Michal Hockod1b5c562015-08-19 14:17:40 +02004813 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004814 eb->start = start;
4815 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004816 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004817 eb->bflags = 0;
4818 rwlock_init(&eb->lock);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004819 atomic_set(&eb->blocking_readers, 0);
4820 atomic_set(&eb->blocking_writers, 0);
David Sterbaed1b4ed2018-08-24 16:31:17 +02004821 eb->lock_nested = false;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004822 init_waitqueue_head(&eb->write_lock_wq);
4823 init_waitqueue_head(&eb->read_lock_wq);
4824
4825 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4826
4827 spin_lock_init(&eb->refs_lock);
4828 atomic_set(&eb->refs, 1);
4829 atomic_set(&eb->io_pages, 0);
4830
4831 /*
4832 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4833 */
4834 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4835 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4836 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4837
David Sterba843ccf92018-08-24 14:56:28 +02004838#ifdef CONFIG_BTRFS_DEBUG
4839 atomic_set(&eb->spinning_writers, 0);
David Sterbaafd495a2018-08-24 15:57:38 +02004840 atomic_set(&eb->spinning_readers, 0);
David Sterba5c9c7992018-08-24 16:15:51 +02004841 atomic_set(&eb->read_locks, 0);
David Sterbac79adfc2018-08-24 16:24:26 +02004842 atomic_set(&eb->write_locks, 0);
David Sterba843ccf92018-08-24 14:56:28 +02004843#endif
4844
Josef Bacikdb7f3432013-08-07 14:54:37 -04004845 return eb;
4846}
4847
4848struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4849{
David Sterbacc5e31a2018-03-01 18:20:27 +01004850 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004851 struct page *p;
4852 struct extent_buffer *new;
David Sterbacc5e31a2018-03-01 18:20:27 +01004853 int num_pages = num_extent_pages(src);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004854
David Sterba3f556f72014-06-15 03:20:26 +02004855 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004856 if (new == NULL)
4857 return NULL;
4858
4859 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004860 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004861 if (!p) {
4862 btrfs_release_extent_buffer(new);
4863 return NULL;
4864 }
4865 attach_extent_buffer_page(new, p);
4866 WARN_ON(PageDirty(p));
4867 SetPageUptodate(p);
4868 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004869 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004870 }
4871
Josef Bacikdb7f3432013-08-07 14:54:37 -04004872 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004873 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004874
4875 return new;
4876}
4877
Omar Sandoval0f331222015-09-29 20:50:31 -07004878struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4879 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004880{
4881 struct extent_buffer *eb;
David Sterbacc5e31a2018-03-01 18:20:27 +01004882 int num_pages;
4883 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004884
David Sterba3f556f72014-06-15 03:20:26 +02004885 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004886 if (!eb)
4887 return NULL;
4888
David Sterba65ad0102018-06-29 10:56:49 +02004889 num_pages = num_extent_pages(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004890 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004891 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004892 if (!eb->pages[i])
4893 goto err;
4894 }
4895 set_extent_buffer_uptodate(eb);
4896 btrfs_set_header_nritems(eb, 0);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004897 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004898
4899 return eb;
4900err:
4901 for (; i > 0; i--)
4902 __free_page(eb->pages[i - 1]);
4903 __free_extent_buffer(eb);
4904 return NULL;
4905}
4906
Omar Sandoval0f331222015-09-29 20:50:31 -07004907struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004908 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004909{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004910 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004911}
4912
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004913static void check_buffer_tree_ref(struct extent_buffer *eb)
4914{
Chris Mason242e18c2013-01-29 17:49:37 -05004915 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004916 /* the ref bit is tricky. We have to make sure it is set
4917 * if we have the buffer dirty. Otherwise the
4918 * code to free a buffer can end up dropping a dirty
4919 * page
4920 *
4921 * Once the ref bit is set, it won't go away while the
4922 * buffer is dirty or in writeback, and it also won't
4923 * go away while we have the reference count on the
4924 * eb bumped.
4925 *
4926 * We can't just set the ref bit without bumping the
4927 * ref on the eb because free_extent_buffer might
4928 * see the ref bit and try to clear it. If this happens
4929 * free_extent_buffer might end up dropping our original
4930 * ref by mistake and freeing the page before we are able
4931 * to add one more ref.
4932 *
4933 * So bump the ref count first, then set the bit. If someone
4934 * beat us to it, drop the ref we added.
4935 */
Chris Mason242e18c2013-01-29 17:49:37 -05004936 refs = atomic_read(&eb->refs);
4937 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4938 return;
4939
Josef Bacik594831c2012-07-20 16:11:08 -04004940 spin_lock(&eb->refs_lock);
4941 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004942 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004943 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004944}
4945
Mel Gorman2457aec2014-06-04 16:10:31 -07004946static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4947 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004948{
David Sterbacc5e31a2018-03-01 18:20:27 +01004949 int num_pages, i;
Josef Bacik5df42352012-03-15 18:24:42 -04004950
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004951 check_buffer_tree_ref(eb);
4952
David Sterba65ad0102018-06-29 10:56:49 +02004953 num_pages = num_extent_pages(eb);
Josef Bacik5df42352012-03-15 18:24:42 -04004954 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004955 struct page *p = eb->pages[i];
4956
Mel Gorman2457aec2014-06-04 16:10:31 -07004957 if (p != accessed)
4958 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004959 }
4960}
4961
Josef Bacikf28491e2013-12-16 13:24:27 -05004962struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4963 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004964{
4965 struct extent_buffer *eb;
4966
4967 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004968 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004969 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004970 if (eb && atomic_inc_not_zero(&eb->refs)) {
4971 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004972 /*
4973 * Lock our eb's refs_lock to avoid races with
4974 * free_extent_buffer. When we get our eb it might be flagged
4975 * with EXTENT_BUFFER_STALE and another task running
4976 * free_extent_buffer might have seen that flag set,
4977 * eb->refs == 2, that the buffer isn't under IO (dirty and
4978 * writeback flags not set) and it's still in the tree (flag
4979 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4980 * of decrementing the extent buffer's reference count twice.
4981 * So here we could race and increment the eb's reference count,
4982 * clear its stale flag, mark it as dirty and drop our reference
4983 * before the other task finishes executing free_extent_buffer,
4984 * which would later result in an attempt to free an extent
4985 * buffer that is dirty.
4986 */
4987 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4988 spin_lock(&eb->refs_lock);
4989 spin_unlock(&eb->refs_lock);
4990 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004991 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004992 return eb;
4993 }
4994 rcu_read_unlock();
4995
4996 return NULL;
4997}
4998
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004999#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
5000struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005001 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005002{
5003 struct extent_buffer *eb, *exists = NULL;
5004 int ret;
5005
5006 eb = find_extent_buffer(fs_info, start);
5007 if (eb)
5008 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04005009 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005010 if (!eb)
5011 return NULL;
5012 eb->fs_info = fs_info;
5013again:
David Sterbae1860a72016-05-09 14:11:38 +02005014 ret = radix_tree_preload(GFP_NOFS);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005015 if (ret)
5016 goto free_eb;
5017 spin_lock(&fs_info->buffer_lock);
5018 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005019 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005020 spin_unlock(&fs_info->buffer_lock);
5021 radix_tree_preload_end();
5022 if (ret == -EEXIST) {
5023 exists = find_extent_buffer(fs_info, start);
5024 if (exists)
5025 goto free_eb;
5026 else
5027 goto again;
5028 }
5029 check_buffer_tree_ref(eb);
5030 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
5031
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005032 return eb;
5033free_eb:
5034 btrfs_release_extent_buffer(eb);
5035 return exists;
5036}
5037#endif
5038
Josef Bacikf28491e2013-12-16 13:24:27 -05005039struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02005040 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05005041{
Jeff Mahoneyda170662016-06-15 09:22:56 -04005042 unsigned long len = fs_info->nodesize;
David Sterbacc5e31a2018-03-01 18:20:27 +01005043 int num_pages;
5044 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005045 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005046 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04005047 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05005048 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05005049 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05005050 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005051 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05005052
Jeff Mahoneyda170662016-06-15 09:22:56 -04005053 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07005054 btrfs_err(fs_info, "bad tree block start %llu", start);
5055 return ERR_PTR(-EINVAL);
5056 }
5057
Josef Bacikf28491e2013-12-16 13:24:27 -05005058 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005059 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04005060 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04005061
David Sterba23d79d82014-06-15 02:55:29 +02005062 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04005063 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07005064 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05005065
David Sterba65ad0102018-06-29 10:56:49 +02005066 num_pages = num_extent_pages(eb);
Chris Mason727011e2010-08-06 13:21:20 -04005067 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02005068 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07005069 if (!p) {
5070 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04005071 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005072 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005073
5074 spin_lock(&mapping->private_lock);
5075 if (PagePrivate(p)) {
5076 /*
5077 * We could have already allocated an eb for this page
5078 * and attached one so lets see if we can get a ref on
5079 * the existing eb, and if we can we know it's good and
5080 * we can just return that one, else we know we can just
5081 * overwrite page->private.
5082 */
5083 exists = (struct extent_buffer *)p->private;
5084 if (atomic_inc_not_zero(&exists->refs)) {
5085 spin_unlock(&mapping->private_lock);
5086 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005087 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07005088 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005089 goto free_eb;
5090 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005091 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005092
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005093 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005094 * Do this so attach doesn't complain and we need to
5095 * drop the ref the old guy had.
5096 */
5097 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005098 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005099 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05005100 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005101 attach_extent_buffer_page(eb, p);
5102 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005103 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04005104 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05005105 if (!PageUptodate(p))
5106 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05005107
5108 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005109 * We can't unlock the pages just yet since the extent buffer
5110 * hasn't been properly inserted in the radix tree, this
5111 * opens a race with btree_releasepage which can free a page
5112 * while we are still filling in all pages for the buffer and
5113 * we could crash.
Chris Masoneb14ab82011-02-10 12:35:00 -05005114 */
Chris Masond1310b22008-01-24 16:13:08 -05005115 }
5116 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05005117 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05005118again:
David Sterbae1860a72016-05-09 14:11:38 +02005119 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07005120 if (ret) {
5121 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04005122 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005123 }
Miao Xie19fe0a82010-10-26 20:57:29 -04005124
Josef Bacikf28491e2013-12-16 13:24:27 -05005125 spin_lock(&fs_info->buffer_lock);
5126 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005127 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05005128 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005129 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04005130 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005131 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005132 if (exists)
5133 goto free_eb;
5134 else
Josef Bacik115391d2012-03-09 09:51:43 -05005135 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04005136 }
Chris Mason6af118ce2008-07-22 11:18:07 -04005137 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005138 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005139 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005140
5141 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005142 * Now it's safe to unlock the pages because any calls to
5143 * btree_releasepage will correctly detect that a page belongs to a
5144 * live buffer and won't free them prematurely.
Chris Masoneb14ab82011-02-10 12:35:00 -05005145 */
Nikolay Borisov28187ae2018-07-04 10:24:51 +03005146 for (i = 0; i < num_pages; i++)
5147 unlock_page(eb->pages[i]);
Chris Masond1310b22008-01-24 16:13:08 -05005148 return eb;
5149
Chris Mason6af118ce2008-07-22 11:18:07 -04005150free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005151 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005152 for (i = 0; i < num_pages; i++) {
5153 if (eb->pages[i])
5154 unlock_page(eb->pages[i]);
5155 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005156
Miao Xie897ca6e92010-10-26 20:57:29 -04005157 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005158 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005159}
Chris Masond1310b22008-01-24 16:13:08 -05005160
Josef Bacik3083ee22012-03-09 16:01:49 -05005161static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5162{
5163 struct extent_buffer *eb =
5164 container_of(head, struct extent_buffer, rcu_head);
5165
5166 __free_extent_buffer(eb);
5167}
5168
David Sterbaf7a52a42013-04-26 14:56:29 +00005169static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005170{
Nikolay Borisov07e21c42018-06-27 16:38:23 +03005171 lockdep_assert_held(&eb->refs_lock);
5172
Josef Bacik3083ee22012-03-09 16:01:49 -05005173 WARN_ON(atomic_read(&eb->refs) == 0);
5174 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005175 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005176 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005177
Jan Schmidt815a51c2012-05-16 17:00:02 +02005178 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005179
Josef Bacikf28491e2013-12-16 13:24:27 -05005180 spin_lock(&fs_info->buffer_lock);
5181 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005182 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005183 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005184 } else {
5185 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005186 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005187
5188 /* Should be safe to release our pages at this point */
David Sterba55ac0132018-07-19 17:24:32 +02005189 btrfs_release_extent_buffer_pages(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005190#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005191 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
Josef Bacikbcb7e442015-03-16 17:38:02 -04005192 __free_extent_buffer(eb);
5193 return 1;
5194 }
5195#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005196 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005197 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005198 }
5199 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005200
5201 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005202}
5203
Chris Masond1310b22008-01-24 16:13:08 -05005204void free_extent_buffer(struct extent_buffer *eb)
5205{
Chris Mason242e18c2013-01-29 17:49:37 -05005206 int refs;
5207 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005208 if (!eb)
5209 return;
5210
Chris Mason242e18c2013-01-29 17:49:37 -05005211 while (1) {
5212 refs = atomic_read(&eb->refs);
Nikolay Borisov46cc7752018-10-15 17:04:01 +03005213 if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
5214 || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
5215 refs == 1))
Chris Mason242e18c2013-01-29 17:49:37 -05005216 break;
5217 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5218 if (old == refs)
5219 return;
5220 }
5221
Josef Bacik3083ee22012-03-09 16:01:49 -05005222 spin_lock(&eb->refs_lock);
5223 if (atomic_read(&eb->refs) == 2 &&
5224 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005225 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005226 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5227 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005228
Josef Bacik3083ee22012-03-09 16:01:49 -05005229 /*
5230 * I know this is terrible, but it's temporary until we stop tracking
5231 * the uptodate bits and such for the extent buffers.
5232 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005233 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005234}
Chris Masond1310b22008-01-24 16:13:08 -05005235
Josef Bacik3083ee22012-03-09 16:01:49 -05005236void free_extent_buffer_stale(struct extent_buffer *eb)
5237{
5238 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005239 return;
5240
Josef Bacik3083ee22012-03-09 16:01:49 -05005241 spin_lock(&eb->refs_lock);
5242 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5243
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005244 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005245 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5246 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005247 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005248}
5249
Chris Mason1d4284b2012-03-28 20:31:37 -04005250void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005251{
David Sterbacc5e31a2018-03-01 18:20:27 +01005252 int i;
5253 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005254 struct page *page;
5255
David Sterba65ad0102018-06-29 10:56:49 +02005256 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005257
5258 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005259 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005260 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005261 continue;
5262
Chris Masona61e6f22008-07-22 11:18:08 -04005263 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005264 WARN_ON(!PagePrivate(page));
5265
Chris Masond1310b22008-01-24 16:13:08 -05005266 clear_page_dirty_for_io(page);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005267 xa_lock_irq(&page->mapping->i_pages);
Matthew Wilcox0a943c62017-12-04 10:37:22 -05005268 if (!PageDirty(page))
5269 __xa_clear_mark(&page->mapping->i_pages,
5270 page_index(page), PAGECACHE_TAG_DIRTY);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005271 xa_unlock_irq(&page->mapping->i_pages);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005272 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005273 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005274 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005275 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005276}
Chris Masond1310b22008-01-24 16:13:08 -05005277
Liu Boabb57ef2018-09-14 01:44:42 +08005278bool set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005279{
David Sterbacc5e31a2018-03-01 18:20:27 +01005280 int i;
5281 int num_pages;
Liu Boabb57ef2018-09-14 01:44:42 +08005282 bool was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005283
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005284 check_buffer_tree_ref(eb);
5285
Chris Masonb9473432009-03-13 11:00:37 -04005286 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005287
David Sterba65ad0102018-06-29 10:56:49 +02005288 num_pages = num_extent_pages(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05005289 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005290 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5291
Liu Boabb57ef2018-09-14 01:44:42 +08005292 if (!was_dirty)
5293 for (i = 0; i < num_pages; i++)
5294 set_page_dirty(eb->pages[i]);
Liu Bo51995c32018-09-14 01:46:08 +08005295
5296#ifdef CONFIG_BTRFS_DEBUG
5297 for (i = 0; i < num_pages; i++)
5298 ASSERT(PageDirty(eb->pages[i]));
5299#endif
5300
Chris Masonb9473432009-03-13 11:00:37 -04005301 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005302}
Chris Masond1310b22008-01-24 16:13:08 -05005303
David Sterba69ba3922015-12-03 13:08:59 +01005304void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005305{
David Sterbacc5e31a2018-03-01 18:20:27 +01005306 int i;
Chris Mason1259ab72008-05-12 13:39:03 -04005307 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005308 int num_pages;
Chris Mason1259ab72008-05-12 13:39:03 -04005309
Chris Masonb4ce94d2009-02-04 09:25:08 -05005310 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005311 num_pages = num_extent_pages(eb);
Chris Mason1259ab72008-05-12 13:39:03 -04005312 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005313 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005314 if (page)
5315 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005316 }
Chris Mason1259ab72008-05-12 13:39:03 -04005317}
5318
David Sterba09c25a82015-12-03 13:08:59 +01005319void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005320{
David Sterbacc5e31a2018-03-01 18:20:27 +01005321 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005322 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005323 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005324
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005325 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005326 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005327 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005328 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005329 SetPageUptodate(page);
5330 }
Chris Masond1310b22008-01-24 16:13:08 -05005331}
Chris Masond1310b22008-01-24 16:13:08 -05005332
Nikolay Borisovc2ccfbc2019-04-10 17:24:40 +03005333int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005334{
David Sterbacc5e31a2018-03-01 18:20:27 +01005335 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005336 struct page *page;
5337 int err;
5338 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005339 int locked_pages = 0;
5340 int all_uptodate = 1;
David Sterbacc5e31a2018-03-01 18:20:27 +01005341 int num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005342 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005343 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005344 unsigned long bio_flags = 0;
Nikolay Borisovc2ccfbc2019-04-10 17:24:40 +03005345 struct extent_io_tree *tree = &BTRFS_I(eb->fs_info->btree_inode)->io_tree;
Chris Masona86c12c2008-02-07 10:50:54 -05005346
Chris Masonb4ce94d2009-02-04 09:25:08 -05005347 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005348 return 0;
5349
David Sterba65ad0102018-06-29 10:56:49 +02005350 num_pages = num_extent_pages(eb);
Josef Bacik8436ea912016-09-02 15:40:03 -04005351 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005352 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005353 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005354 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005355 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005356 } else {
5357 lock_page(page);
5358 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005359 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005360 }
5361 /*
5362 * We need to firstly lock all pages to make sure that
5363 * the uptodate bit of our pages won't be affected by
5364 * clear_extent_buffer_uptodate().
5365 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005366 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005367 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005368 if (!PageUptodate(page)) {
5369 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005370 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005371 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005372 }
Liu Bo2571e732016-08-03 12:33:01 -07005373
Chris Masonce9adaa2008-04-09 16:28:12 -04005374 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005375 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005376 goto unlock_exit;
5377 }
5378
Filipe Manana656f30d2014-09-26 12:25:56 +01005379 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005380 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005381 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005382 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005383 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005384
Chris Masonce9adaa2008-04-09 16:28:12 -04005385 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005386 if (ret) {
5387 atomic_dec(&eb->io_pages);
5388 unlock_page(page);
5389 continue;
5390 }
5391
Chris Masonf1885912008-04-09 16:28:12 -04005392 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005393 err = __extent_read_full_page(tree, page,
David Sterba6af49db2017-06-23 04:09:57 +02005394 btree_get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005395 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005396 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005397 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005398 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005399 /*
5400 * We use &bio in above __extent_read_full_page,
5401 * so we ensure that if it returns error, the
5402 * current page fails to add itself to bio and
5403 * it's been unlocked.
5404 *
5405 * We must dec io_pages by ourselves.
5406 */
5407 atomic_dec(&eb->io_pages);
5408 }
Chris Masond1310b22008-01-24 16:13:08 -05005409 } else {
5410 unlock_page(page);
5411 }
5412 }
5413
Jeff Mahoney355808c2011-10-03 23:23:14 -04005414 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005415 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005416 if (err)
5417 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005418 }
Chris Masona86c12c2008-02-07 10:50:54 -05005419
Arne Jansenbb82ab82011-06-10 14:06:53 +02005420 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005421 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005422
Josef Bacik8436ea912016-09-02 15:40:03 -04005423 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005424 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005425 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005426 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005427 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005428 }
Chris Masond3977122009-01-05 21:25:51 -05005429
Chris Masond1310b22008-01-24 16:13:08 -05005430 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005431
5432unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005433 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005434 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005435 page = eb->pages[locked_pages];
5436 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005437 }
5438 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005439}
Chris Masond1310b22008-01-24 16:13:08 -05005440
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005441void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5442 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005443{
5444 size_t cur;
5445 size_t offset;
5446 struct page *page;
5447 char *kaddr;
5448 char *dst = (char *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005449 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005450 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005451
Liu Bof716abd2017-08-09 11:10:16 -06005452 if (start + len > eb->len) {
5453 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5454 eb->start, eb->len, start, len);
5455 memset(dst, 0, len);
5456 return;
5457 }
Chris Masond1310b22008-01-24 16:13:08 -05005458
Johannes Thumshirn70730172018-12-05 15:23:03 +01005459 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005460
Chris Masond3977122009-01-05 21:25:51 -05005461 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005462 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005463
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005464 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005465 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005466 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005467
5468 dst += cur;
5469 len -= cur;
5470 offset = 0;
5471 i++;
5472 }
5473}
Chris Masond1310b22008-01-24 16:13:08 -05005474
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005475int read_extent_buffer_to_user(const struct extent_buffer *eb,
5476 void __user *dstv,
5477 unsigned long start, unsigned long len)
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005478{
5479 size_t cur;
5480 size_t offset;
5481 struct page *page;
5482 char *kaddr;
5483 char __user *dst = (char __user *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005484 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005485 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005486 int ret = 0;
5487
5488 WARN_ON(start > eb->len);
5489 WARN_ON(start + len > eb->start + eb->len);
5490
Johannes Thumshirn70730172018-12-05 15:23:03 +01005491 offset = offset_in_page(start_offset + start);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005492
5493 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005494 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005495
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005496 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005497 kaddr = page_address(page);
5498 if (copy_to_user(dst, kaddr + offset, cur)) {
5499 ret = -EFAULT;
5500 break;
5501 }
5502
5503 dst += cur;
5504 len -= cur;
5505 offset = 0;
5506 i++;
5507 }
5508
5509 return ret;
5510}
5511
Liu Bo415b35a2016-06-17 19:16:21 -07005512/*
5513 * return 0 if the item is found within a page.
5514 * return 1 if the item spans two pages.
5515 * return -EINVAL otherwise.
5516 */
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005517int map_private_extent_buffer(const struct extent_buffer *eb,
5518 unsigned long start, unsigned long min_len,
5519 char **map, unsigned long *map_start,
5520 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005521{
Johannes Thumshirncc2c39d2018-11-28 09:54:54 +01005522 size_t offset;
Chris Masond1310b22008-01-24 16:13:08 -05005523 char *kaddr;
5524 struct page *p;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005525 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005526 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005527 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005528 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005529
Liu Bof716abd2017-08-09 11:10:16 -06005530 if (start + min_len > eb->len) {
5531 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5532 eb->start, eb->len, start, min_len);
5533 return -EINVAL;
5534 }
5535
Chris Masond1310b22008-01-24 16:13:08 -05005536 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005537 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005538
5539 if (i == 0) {
5540 offset = start_offset;
5541 *map_start = 0;
5542 } else {
5543 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005544 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005545 }
Chris Masond3977122009-01-05 21:25:51 -05005546
David Sterbafb85fc92014-07-31 01:03:53 +02005547 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005548 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005549 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005550 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005551 return 0;
5552}
Chris Masond1310b22008-01-24 16:13:08 -05005553
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005554int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5555 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005556{
5557 size_t cur;
5558 size_t offset;
5559 struct page *page;
5560 char *kaddr;
5561 char *ptr = (char *)ptrv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005562 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005563 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005564 int ret = 0;
5565
5566 WARN_ON(start > eb->len);
5567 WARN_ON(start + len > eb->start + eb->len);
5568
Johannes Thumshirn70730172018-12-05 15:23:03 +01005569 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005570
Chris Masond3977122009-01-05 21:25:51 -05005571 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005572 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005573
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005574 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005575
Chris Masona6591712011-07-19 12:04:14 -04005576 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005577 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005578 if (ret)
5579 break;
5580
5581 ptr += cur;
5582 len -= cur;
5583 offset = 0;
5584 i++;
5585 }
5586 return ret;
5587}
Chris Masond1310b22008-01-24 16:13:08 -05005588
David Sterbaf157bf72016-11-09 17:43:38 +01005589void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5590 const void *srcv)
5591{
5592 char *kaddr;
5593
5594 WARN_ON(!PageUptodate(eb->pages[0]));
5595 kaddr = page_address(eb->pages[0]);
5596 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5597 BTRFS_FSID_SIZE);
5598}
5599
5600void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5601{
5602 char *kaddr;
5603
5604 WARN_ON(!PageUptodate(eb->pages[0]));
5605 kaddr = page_address(eb->pages[0]);
5606 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5607 BTRFS_FSID_SIZE);
5608}
5609
Chris Masond1310b22008-01-24 16:13:08 -05005610void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5611 unsigned long start, unsigned long len)
5612{
5613 size_t cur;
5614 size_t offset;
5615 struct page *page;
5616 char *kaddr;
5617 char *src = (char *)srcv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005618 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005619 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005620
5621 WARN_ON(start > eb->len);
5622 WARN_ON(start + len > eb->start + eb->len);
5623
Johannes Thumshirn70730172018-12-05 15:23:03 +01005624 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005625
Chris Masond3977122009-01-05 21:25:51 -05005626 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005627 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005628 WARN_ON(!PageUptodate(page));
5629
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005630 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005631 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005632 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005633
5634 src += cur;
5635 len -= cur;
5636 offset = 0;
5637 i++;
5638 }
5639}
Chris Masond1310b22008-01-24 16:13:08 -05005640
David Sterbab159fa22016-11-08 18:09:03 +01005641void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5642 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005643{
5644 size_t cur;
5645 size_t offset;
5646 struct page *page;
5647 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005648 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005649 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005650
5651 WARN_ON(start > eb->len);
5652 WARN_ON(start + len > eb->start + eb->len);
5653
Johannes Thumshirn70730172018-12-05 15:23:03 +01005654 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005655
Chris Masond3977122009-01-05 21:25:51 -05005656 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005657 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005658 WARN_ON(!PageUptodate(page));
5659
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005660 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005661 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005662 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005663
5664 len -= cur;
5665 offset = 0;
5666 i++;
5667 }
5668}
Chris Masond1310b22008-01-24 16:13:08 -05005669
David Sterba58e80122016-11-08 18:30:31 +01005670void copy_extent_buffer_full(struct extent_buffer *dst,
5671 struct extent_buffer *src)
5672{
5673 int i;
David Sterbacc5e31a2018-03-01 18:20:27 +01005674 int num_pages;
David Sterba58e80122016-11-08 18:30:31 +01005675
5676 ASSERT(dst->len == src->len);
5677
David Sterba65ad0102018-06-29 10:56:49 +02005678 num_pages = num_extent_pages(dst);
David Sterba58e80122016-11-08 18:30:31 +01005679 for (i = 0; i < num_pages; i++)
5680 copy_page(page_address(dst->pages[i]),
5681 page_address(src->pages[i]));
5682}
5683
Chris Masond1310b22008-01-24 16:13:08 -05005684void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5685 unsigned long dst_offset, unsigned long src_offset,
5686 unsigned long len)
5687{
5688 u64 dst_len = dst->len;
5689 size_t cur;
5690 size_t offset;
5691 struct page *page;
5692 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005693 size_t start_offset = offset_in_page(dst->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005694 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005695
5696 WARN_ON(src->len != dst_len);
5697
Johannes Thumshirn70730172018-12-05 15:23:03 +01005698 offset = offset_in_page(start_offset + dst_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005699
Chris Masond3977122009-01-05 21:25:51 -05005700 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005701 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005702 WARN_ON(!PageUptodate(page));
5703
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005704 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005705
Chris Masona6591712011-07-19 12:04:14 -04005706 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005707 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005708
5709 src_offset += cur;
5710 len -= cur;
5711 offset = 0;
5712 i++;
5713 }
5714}
Chris Masond1310b22008-01-24 16:13:08 -05005715
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005716/*
5717 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5718 * given bit number
5719 * @eb: the extent buffer
5720 * @start: offset of the bitmap item in the extent buffer
5721 * @nr: bit number
5722 * @page_index: return index of the page in the extent buffer that contains the
5723 * given bit number
5724 * @page_offset: return offset into the page given by page_index
5725 *
5726 * This helper hides the ugliness of finding the byte in an extent buffer which
5727 * contains a given bit.
5728 */
5729static inline void eb_bitmap_offset(struct extent_buffer *eb,
5730 unsigned long start, unsigned long nr,
5731 unsigned long *page_index,
5732 size_t *page_offset)
5733{
Johannes Thumshirn70730172018-12-05 15:23:03 +01005734 size_t start_offset = offset_in_page(eb->start);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005735 size_t byte_offset = BIT_BYTE(nr);
5736 size_t offset;
5737
5738 /*
5739 * The byte we want is the offset of the extent buffer + the offset of
5740 * the bitmap item in the extent buffer + the offset of the byte in the
5741 * bitmap item.
5742 */
5743 offset = start_offset + start + byte_offset;
5744
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005745 *page_index = offset >> PAGE_SHIFT;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005746 *page_offset = offset_in_page(offset);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005747}
5748
5749/**
5750 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5751 * @eb: the extent buffer
5752 * @start: offset of the bitmap item in the extent buffer
5753 * @nr: bit number to test
5754 */
5755int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5756 unsigned long nr)
5757{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005758 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005759 struct page *page;
5760 unsigned long i;
5761 size_t offset;
5762
5763 eb_bitmap_offset(eb, start, nr, &i, &offset);
5764 page = eb->pages[i];
5765 WARN_ON(!PageUptodate(page));
5766 kaddr = page_address(page);
5767 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5768}
5769
5770/**
5771 * extent_buffer_bitmap_set - set an area of a bitmap
5772 * @eb: the extent buffer
5773 * @start: offset of the bitmap item in the extent buffer
5774 * @pos: bit number of the first bit
5775 * @len: number of bits to set
5776 */
5777void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5778 unsigned long pos, unsigned long len)
5779{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005780 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005781 struct page *page;
5782 unsigned long i;
5783 size_t offset;
5784 const unsigned int size = pos + len;
5785 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005786 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005787
5788 eb_bitmap_offset(eb, start, pos, &i, &offset);
5789 page = eb->pages[i];
5790 WARN_ON(!PageUptodate(page));
5791 kaddr = page_address(page);
5792
5793 while (len >= bits_to_set) {
5794 kaddr[offset] |= mask_to_set;
5795 len -= bits_to_set;
5796 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005797 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005798 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005799 offset = 0;
5800 page = eb->pages[++i];
5801 WARN_ON(!PageUptodate(page));
5802 kaddr = page_address(page);
5803 }
5804 }
5805 if (len) {
5806 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5807 kaddr[offset] |= mask_to_set;
5808 }
5809}
5810
5811
5812/**
5813 * extent_buffer_bitmap_clear - clear an area of a bitmap
5814 * @eb: the extent buffer
5815 * @start: offset of the bitmap item in the extent buffer
5816 * @pos: bit number of the first bit
5817 * @len: number of bits to clear
5818 */
5819void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5820 unsigned long pos, unsigned long len)
5821{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005822 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005823 struct page *page;
5824 unsigned long i;
5825 size_t offset;
5826 const unsigned int size = pos + len;
5827 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005828 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005829
5830 eb_bitmap_offset(eb, start, pos, &i, &offset);
5831 page = eb->pages[i];
5832 WARN_ON(!PageUptodate(page));
5833 kaddr = page_address(page);
5834
5835 while (len >= bits_to_clear) {
5836 kaddr[offset] &= ~mask_to_clear;
5837 len -= bits_to_clear;
5838 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005839 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005840 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005841 offset = 0;
5842 page = eb->pages[++i];
5843 WARN_ON(!PageUptodate(page));
5844 kaddr = page_address(page);
5845 }
5846 }
5847 if (len) {
5848 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5849 kaddr[offset] &= ~mask_to_clear;
5850 }
5851}
5852
Sergei Trofimovich33872062011-04-11 21:52:52 +00005853static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5854{
5855 unsigned long distance = (src > dst) ? src - dst : dst - src;
5856 return distance < len;
5857}
5858
Chris Masond1310b22008-01-24 16:13:08 -05005859static void copy_pages(struct page *dst_page, struct page *src_page,
5860 unsigned long dst_off, unsigned long src_off,
5861 unsigned long len)
5862{
Chris Masona6591712011-07-19 12:04:14 -04005863 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005864 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005865 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005866
Sergei Trofimovich33872062011-04-11 21:52:52 +00005867 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005868 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005869 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005870 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005871 if (areas_overlap(src_off, dst_off, len))
5872 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005873 }
Chris Masond1310b22008-01-24 16:13:08 -05005874
Chris Mason727011e2010-08-06 13:21:20 -04005875 if (must_memmove)
5876 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5877 else
5878 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005879}
5880
5881void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5882 unsigned long src_offset, unsigned long len)
5883{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005884 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005885 size_t cur;
5886 size_t dst_off_in_page;
5887 size_t src_off_in_page;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005888 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005889 unsigned long dst_i;
5890 unsigned long src_i;
5891
5892 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005893 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005894 "memmove bogus src_offset %lu move len %lu dst len %lu",
5895 src_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01005896 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05005897 }
5898 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005899 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005900 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5901 dst_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01005902 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05005903 }
5904
Chris Masond3977122009-01-05 21:25:51 -05005905 while (len > 0) {
Johannes Thumshirn70730172018-12-05 15:23:03 +01005906 dst_off_in_page = offset_in_page(start_offset + dst_offset);
5907 src_off_in_page = offset_in_page(start_offset + src_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005908
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005909 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5910 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005911
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005912 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005913 src_off_in_page));
5914 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005915 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005916
David Sterbafb85fc92014-07-31 01:03:53 +02005917 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005918 dst_off_in_page, src_off_in_page, cur);
5919
5920 src_offset += cur;
5921 dst_offset += cur;
5922 len -= cur;
5923 }
5924}
Chris Masond1310b22008-01-24 16:13:08 -05005925
5926void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5927 unsigned long src_offset, unsigned long len)
5928{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005929 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005930 size_t cur;
5931 size_t dst_off_in_page;
5932 size_t src_off_in_page;
5933 unsigned long dst_end = dst_offset + len - 1;
5934 unsigned long src_end = src_offset + len - 1;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005935 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005936 unsigned long dst_i;
5937 unsigned long src_i;
5938
5939 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005940 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005941 "memmove bogus src_offset %lu move len %lu len %lu",
5942 src_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01005943 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05005944 }
5945 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005946 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005947 "memmove bogus dst_offset %lu move len %lu len %lu",
5948 dst_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01005949 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05005950 }
Chris Mason727011e2010-08-06 13:21:20 -04005951 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005952 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5953 return;
5954 }
Chris Masond3977122009-01-05 21:25:51 -05005955 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005956 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
5957 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005958
Johannes Thumshirn70730172018-12-05 15:23:03 +01005959 dst_off_in_page = offset_in_page(start_offset + dst_end);
5960 src_off_in_page = offset_in_page(start_offset + src_end);
Chris Masond1310b22008-01-24 16:13:08 -05005961
5962 cur = min_t(unsigned long, len, src_off_in_page + 1);
5963 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005964 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005965 dst_off_in_page - cur + 1,
5966 src_off_in_page - cur + 1, cur);
5967
5968 dst_end -= cur;
5969 src_end -= cur;
5970 len -= cur;
5971 }
5972}
Chris Mason6af118ce2008-07-22 11:18:07 -04005973
David Sterbaf7a52a42013-04-26 14:56:29 +00005974int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005975{
Chris Mason6af118ce2008-07-22 11:18:07 -04005976 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005977
Miao Xie19fe0a82010-10-26 20:57:29 -04005978 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005979 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05005980 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005981 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005982 spin_lock(&page->mapping->private_lock);
5983 if (!PagePrivate(page)) {
5984 spin_unlock(&page->mapping->private_lock);
5985 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005986 }
5987
Josef Bacik3083ee22012-03-09 16:01:49 -05005988 eb = (struct extent_buffer *)page->private;
5989 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005990
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005991 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005992 * This is a little awful but should be ok, we need to make sure that
5993 * the eb doesn't disappear out from under us while we're looking at
5994 * this page.
5995 */
5996 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005997 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005998 spin_unlock(&eb->refs_lock);
5999 spin_unlock(&page->mapping->private_lock);
6000 return 0;
6001 }
6002 spin_unlock(&page->mapping->private_lock);
6003
Josef Bacik3083ee22012-03-09 16:01:49 -05006004 /*
6005 * If tree ref isn't set then we know the ref on this eb is a real ref,
6006 * so just return, this page will likely be freed soon anyway.
6007 */
6008 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
6009 spin_unlock(&eb->refs_lock);
6010 return 0;
6011 }
Josef Bacik3083ee22012-03-09 16:01:49 -05006012
David Sterbaf7a52a42013-04-26 14:56:29 +00006013 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04006014}