blob: 6e3b72e63e4226d50014f5058da3b3cabd2c31a8 [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"
Josef Bacik9c7d3a52019-09-23 10:05:19 -040017#include "extent-io-tree.h"
Chris Masond1310b22008-01-24 16:13:08 -050018#include "extent_map.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040019#include "ctree.h"
20#include "btrfs_inode.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020021#include "volumes.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010022#include "check-integrity.h"
Josef Bacik0b32f4b2012-03-13 09:38:00 -040023#include "locking.h"
Josef Bacik606686e2012-06-04 14:03:51 -040024#include "rcu-string.h"
Liu Bofe09e162013-09-22 12:54:23 +080025#include "backref.h"
David Sterba6af49db2017-06-23 04:09:57 +020026#include "disk-io.h"
Chris Masond1310b22008-01-24 16:13:08 -050027
Chris Masond1310b22008-01-24 16:13:08 -050028static struct kmem_cache *extent_state_cache;
29static struct kmem_cache *extent_buffer_cache;
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -040030static struct bio_set btrfs_bioset;
Chris Masond1310b22008-01-24 16:13:08 -050031
Filipe Manana27a35072014-07-06 20:09:59 +010032static inline bool extent_state_in_tree(const struct extent_state *state)
33{
34 return !RB_EMPTY_NODE(&state->rb_node);
35}
36
Eric Sandeen6d49ba12013-04-22 16:12:31 +000037#ifdef CONFIG_BTRFS_DEBUG
Chris Masond1310b22008-01-24 16:13:08 -050038static LIST_HEAD(states);
Chris Masond3977122009-01-05 21:25:51 -050039static DEFINE_SPINLOCK(leak_lock);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000040
Josef Bacik3fd63722020-02-14 16:11:40 -050041static inline void btrfs_leak_debug_add(spinlock_t *lock,
42 struct list_head *new,
43 struct list_head *head)
Eric Sandeen6d49ba12013-04-22 16:12:31 +000044{
45 unsigned long flags;
46
Josef Bacik3fd63722020-02-14 16:11:40 -050047 spin_lock_irqsave(lock, flags);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000048 list_add(new, head);
Josef Bacik3fd63722020-02-14 16:11:40 -050049 spin_unlock_irqrestore(lock, flags);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000050}
51
Josef Bacik3fd63722020-02-14 16:11:40 -050052static inline void btrfs_leak_debug_del(spinlock_t *lock,
53 struct list_head *entry)
Eric Sandeen6d49ba12013-04-22 16:12:31 +000054{
55 unsigned long flags;
56
Josef Bacik3fd63722020-02-14 16:11:40 -050057 spin_lock_irqsave(lock, flags);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000058 list_del(entry);
Josef Bacik3fd63722020-02-14 16:11:40 -050059 spin_unlock_irqrestore(lock, flags);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000060}
61
Josef Bacik3fd63722020-02-14 16:11:40 -050062void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info)
Josef Bacik33ca8322019-09-23 10:05:17 -040063{
64 struct extent_buffer *eb;
Josef Bacik3fd63722020-02-14 16:11:40 -050065 unsigned long flags;
Josef Bacik33ca8322019-09-23 10:05:17 -040066
Josef Bacik8c389382020-02-14 16:11:42 -050067 /*
68 * If we didn't get into open_ctree our allocated_ebs will not be
69 * initialized, so just skip this.
70 */
71 if (!fs_info->allocated_ebs.next)
72 return;
73
Josef Bacik3fd63722020-02-14 16:11:40 -050074 spin_lock_irqsave(&fs_info->eb_leak_lock, flags);
75 while (!list_empty(&fs_info->allocated_ebs)) {
76 eb = list_first_entry(&fs_info->allocated_ebs,
77 struct extent_buffer, leak_list);
Josef Bacik8c389382020-02-14 16:11:42 -050078 pr_err(
79 "BTRFS: buffer leak start %llu len %lu refs %d bflags %lu owner %llu\n",
80 eb->start, eb->len, atomic_read(&eb->refs), eb->bflags,
81 btrfs_header_owner(eb));
Josef Bacik33ca8322019-09-23 10:05:17 -040082 list_del(&eb->leak_list);
83 kmem_cache_free(extent_buffer_cache, eb);
84 }
Josef Bacik3fd63722020-02-14 16:11:40 -050085 spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags);
Josef Bacik33ca8322019-09-23 10:05:17 -040086}
87
88static inline void btrfs_extent_state_leak_debug_check(void)
Eric Sandeen6d49ba12013-04-22 16:12:31 +000089{
90 struct extent_state *state;
Eric Sandeen6d49ba12013-04-22 16:12:31 +000091
92 while (!list_empty(&states)) {
93 state = list_entry(states.next, struct extent_state, leak_list);
David Sterba9ee49a042015-01-14 19:52:13 +010094 pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
Filipe Manana27a35072014-07-06 20:09:59 +010095 state->start, state->end, state->state,
96 extent_state_in_tree(state),
Elena Reshetovab7ac31b2017-03-03 10:55:19 +020097 refcount_read(&state->refs));
Eric Sandeen6d49ba12013-04-22 16:12:31 +000098 list_del(&state->leak_list);
99 kmem_cache_free(extent_state_cache, state);
100 }
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000101}
David Sterba8d599ae2013-04-30 15:22:23 +0000102
Josef Bacika5dee372013-12-13 10:02:44 -0500103#define btrfs_debug_check_extent_io_range(tree, start, end) \
104 __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
David Sterba8d599ae2013-04-30 15:22:23 +0000105static inline void __btrfs_debug_check_extent_io_range(const char *caller,
Josef Bacika5dee372013-12-13 10:02:44 -0500106 struct extent_io_tree *tree, u64 start, u64 end)
David Sterba8d599ae2013-04-30 15:22:23 +0000107{
Nikolay Borisov65a680f2018-11-01 14:09:49 +0200108 struct inode *inode = tree->private_data;
109 u64 isize;
110
111 if (!inode || !is_data_inode(inode))
112 return;
113
114 isize = i_size_read(inode);
115 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
116 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
117 "%s: ino %llu isize %llu odd range [%llu,%llu]",
118 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
119 }
David Sterba8d599ae2013-04-30 15:22:23 +0000120}
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000121#else
Josef Bacik3fd63722020-02-14 16:11:40 -0500122#define btrfs_leak_debug_add(lock, new, head) do {} while (0)
123#define btrfs_leak_debug_del(lock, entry) do {} while (0)
Josef Bacik33ca8322019-09-23 10:05:17 -0400124#define btrfs_extent_state_leak_debug_check() do {} while (0)
David Sterba8d599ae2013-04-30 15:22:23 +0000125#define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
Chris Mason4bef0842008-09-08 11:18:08 -0400126#endif
Chris Masond1310b22008-01-24 16:13:08 -0500127
Chris Masond1310b22008-01-24 16:13:08 -0500128struct tree_entry {
129 u64 start;
130 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -0500131 struct rb_node rb_node;
132};
133
134struct extent_page_data {
135 struct bio *bio;
Chris Mason771ed682008-11-06 22:02:51 -0500136 /* tells writepage not to lock the state bits for this range
137 * it still does the unlocking
138 */
Chris Masonffbd5172009-04-20 15:50:09 -0400139 unsigned int extent_locked:1;
140
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600141 /* tells the submit_bio code to use REQ_SYNC */
Chris Masonffbd5172009-04-20 15:50:09 -0400142 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -0500143};
144
Qu Wenruof97e27e2020-11-13 20:51:40 +0800145static int add_extent_changeset(struct extent_state *state, u32 bits,
Qu Wenruod38ed272015-10-12 14:53:37 +0800146 struct extent_changeset *changeset,
147 int set)
148{
149 int ret;
150
151 if (!changeset)
David Sterba57599c72018-03-01 17:56:34 +0100152 return 0;
Qu Wenruod38ed272015-10-12 14:53:37 +0800153 if (set && (state->state & bits) == bits)
David Sterba57599c72018-03-01 17:56:34 +0100154 return 0;
Qu Wenruofefdc552015-10-12 15:35:38 +0800155 if (!set && (state->state & bits) == 0)
David Sterba57599c72018-03-01 17:56:34 +0100156 return 0;
Qu Wenruod38ed272015-10-12 14:53:37 +0800157 changeset->bytes_changed += state->end - state->start + 1;
David Sterba53d32352017-02-13 13:42:29 +0100158 ret = ulist_add(&changeset->range_changed, state->start, state->end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800159 GFP_ATOMIC);
David Sterba57599c72018-03-01 17:56:34 +0100160 return ret;
Qu Wenruod38ed272015-10-12 14:53:37 +0800161}
162
Nikolay Borisovc1be9c12020-09-14 12:37:08 +0300163int __must_check submit_one_bio(struct bio *bio, int mirror_num,
164 unsigned long bio_flags)
Qu Wenruobb58eb92019-01-25 13:09:15 +0800165{
166 blk_status_t ret = 0;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800167 struct extent_io_tree *tree = bio->bi_private;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800168
169 bio->bi_private = NULL;
170
Nikolay Borisov908930f2020-09-18 16:34:37 +0300171 if (is_data_inode(tree->private_data))
172 ret = btrfs_submit_data_bio(tree->private_data, bio, mirror_num,
173 bio_flags);
174 else
Nikolay Borisov1b362942020-09-18 16:34:38 +0300175 ret = btrfs_submit_metadata_bio(tree->private_data, bio,
176 mirror_num, bio_flags);
Qu Wenruobb58eb92019-01-25 13:09:15 +0800177
178 return blk_status_to_errno(ret);
179}
180
Qu Wenruo30659762019-03-20 14:27:42 +0800181/* Cleanup unsubmitted bios */
182static void end_write_bio(struct extent_page_data *epd, int ret)
183{
184 if (epd->bio) {
185 epd->bio->bi_status = errno_to_blk_status(ret);
186 bio_endio(epd->bio);
187 epd->bio = NULL;
188 }
189}
190
Qu Wenruof4340622019-03-20 14:27:41 +0800191/*
192 * Submit bio from extent page data via submit_one_bio
193 *
194 * Return 0 if everything is OK.
195 * Return <0 for error.
196 */
197static int __must_check flush_write_bio(struct extent_page_data *epd)
Qu Wenruobb58eb92019-01-25 13:09:15 +0800198{
Qu Wenruof4340622019-03-20 14:27:41 +0800199 int ret = 0;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800200
Qu Wenruof4340622019-03-20 14:27:41 +0800201 if (epd->bio) {
Qu Wenruobb58eb92019-01-25 13:09:15 +0800202 ret = submit_one_bio(epd->bio, 0, 0);
Qu Wenruof4340622019-03-20 14:27:41 +0800203 /*
204 * Clean up of epd->bio is handled by its endio function.
205 * And endio is either triggered by successful bio execution
206 * or the error handler of submit bio hook.
207 * So at this point, no matter what happened, we don't need
208 * to clean up epd->bio.
209 */
Qu Wenruobb58eb92019-01-25 13:09:15 +0800210 epd->bio = NULL;
211 }
Qu Wenruof4340622019-03-20 14:27:41 +0800212 return ret;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800213}
David Sterbae2932ee2017-06-23 04:16:17 +0200214
Josef Bacik6f0d04f2019-09-23 10:05:18 -0400215int __init extent_state_cache_init(void)
Chris Masond1310b22008-01-24 16:13:08 -0500216{
David Sterba837e1972012-09-07 03:00:48 -0600217 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200218 sizeof(struct extent_state), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300219 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500220 if (!extent_state_cache)
221 return -ENOMEM;
Josef Bacik6f0d04f2019-09-23 10:05:18 -0400222 return 0;
223}
Chris Masond1310b22008-01-24 16:13:08 -0500224
Josef Bacik6f0d04f2019-09-23 10:05:18 -0400225int __init extent_io_init(void)
226{
David Sterba837e1972012-09-07 03:00:48 -0600227 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200228 sizeof(struct extent_buffer), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300229 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500230 if (!extent_buffer_cache)
Josef Bacik6f0d04f2019-09-23 10:05:18 -0400231 return -ENOMEM;
Chris Mason9be33952013-05-17 18:30:14 -0400232
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400233 if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
234 offsetof(struct btrfs_io_bio, bio),
235 BIOSET_NEED_BVECS))
Chris Mason9be33952013-05-17 18:30:14 -0400236 goto free_buffer_cache;
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700237
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400238 if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE))
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700239 goto free_bioset;
240
Chris Masond1310b22008-01-24 16:13:08 -0500241 return 0;
242
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700243free_bioset:
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400244 bioset_exit(&btrfs_bioset);
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700245
Chris Mason9be33952013-05-17 18:30:14 -0400246free_buffer_cache:
247 kmem_cache_destroy(extent_buffer_cache);
248 extent_buffer_cache = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500249 return -ENOMEM;
250}
251
Josef Bacik6f0d04f2019-09-23 10:05:18 -0400252void __cold extent_state_cache_exit(void)
253{
254 btrfs_extent_state_leak_debug_check();
255 kmem_cache_destroy(extent_state_cache);
256}
257
David Sterbae67c7182018-02-19 17:24:18 +0100258void __cold extent_io_exit(void)
Chris Masond1310b22008-01-24 16:13:08 -0500259{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000260 /*
261 * Make sure all delayed rcu free are flushed before we
262 * destroy caches.
263 */
264 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +0800265 kmem_cache_destroy(extent_buffer_cache);
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400266 bioset_exit(&btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -0500267}
268
Josef Bacik41a2ee72020-01-17 09:02:21 -0500269/*
270 * For the file_extent_tree, we want to hold the inode lock when we lookup and
271 * update the disk_i_size, but lockdep will complain because our io_tree we hold
272 * the tree lock and get the inode lock when setting delalloc. These two things
273 * are unrelated, so make a class for the file_extent_tree so we don't get the
274 * two locking patterns mixed up.
275 */
276static struct lock_class_key file_extent_tree_class;
277
Qu Wenruoc258d6e2019-03-01 10:47:58 +0800278void extent_io_tree_init(struct btrfs_fs_info *fs_info,
Qu Wenruo43eb5f22019-03-01 10:47:59 +0800279 struct extent_io_tree *tree, unsigned int owner,
280 void *private_data)
Chris Masond1310b22008-01-24 16:13:08 -0500281{
Qu Wenruoc258d6e2019-03-01 10:47:58 +0800282 tree->fs_info = fs_info;
Eric Paris6bef4d32010-02-23 19:43:04 +0000283 tree->state = RB_ROOT;
Chris Masond1310b22008-01-24 16:13:08 -0500284 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500285 spin_lock_init(&tree->lock);
Josef Bacikc6100a42017-05-05 11:57:13 -0400286 tree->private_data = private_data;
Qu Wenruo43eb5f22019-03-01 10:47:59 +0800287 tree->owner = owner;
Josef Bacik41a2ee72020-01-17 09:02:21 -0500288 if (owner == IO_TREE_INODE_FILE_EXTENT)
289 lockdep_set_class(&tree->lock, &file_extent_tree_class);
Chris Masond1310b22008-01-24 16:13:08 -0500290}
Chris Masond1310b22008-01-24 16:13:08 -0500291
Nikolay Borisov41e7acd2019-03-25 14:31:24 +0200292void extent_io_tree_release(struct extent_io_tree *tree)
293{
294 spin_lock(&tree->lock);
295 /*
296 * Do a single barrier for the waitqueue_active check here, the state
297 * of the waitqueue should not change once extent_io_tree_release is
298 * called.
299 */
300 smp_mb();
301 while (!RB_EMPTY_ROOT(&tree->state)) {
302 struct rb_node *node;
303 struct extent_state *state;
304
305 node = rb_first(&tree->state);
306 state = rb_entry(node, struct extent_state, rb_node);
307 rb_erase(&state->rb_node, &tree->state);
308 RB_CLEAR_NODE(&state->rb_node);
309 /*
310 * btree io trees aren't supposed to have tasks waiting for
311 * changes in the flags of extent states ever.
312 */
313 ASSERT(!waitqueue_active(&state->wq));
314 free_extent_state(state);
315
316 cond_resched_lock(&tree->lock);
317 }
318 spin_unlock(&tree->lock);
319}
320
Christoph Hellwigb2950862008-12-02 09:54:17 -0500321static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500322{
323 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500324
Michal Hocko3ba7ab22017-01-09 15:39:02 +0100325 /*
326 * The given mask might be not appropriate for the slab allocator,
327 * drop the unsupported bits
328 */
329 mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
Chris Masond1310b22008-01-24 16:13:08 -0500330 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400331 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500332 return state;
333 state->state = 0;
David Sterba47dc1962016-02-11 13:24:13 +0100334 state->failrec = NULL;
Filipe Manana27a35072014-07-06 20:09:59 +0100335 RB_CLEAR_NODE(&state->rb_node);
Josef Bacik3fd63722020-02-14 16:11:40 -0500336 btrfs_leak_debug_add(&leak_lock, &state->leak_list, &states);
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200337 refcount_set(&state->refs, 1);
Chris Masond1310b22008-01-24 16:13:08 -0500338 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100339 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500340 return state;
341}
Chris Masond1310b22008-01-24 16:13:08 -0500342
Chris Mason4845e442010-05-25 20:56:50 -0400343void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500344{
Chris Masond1310b22008-01-24 16:13:08 -0500345 if (!state)
346 return;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200347 if (refcount_dec_and_test(&state->refs)) {
Filipe Manana27a35072014-07-06 20:09:59 +0100348 WARN_ON(extent_state_in_tree(state));
Josef Bacik3fd63722020-02-14 16:11:40 -0500349 btrfs_leak_debug_del(&leak_lock, &state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100350 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500351 kmem_cache_free(extent_state_cache, state);
352 }
353}
Chris Masond1310b22008-01-24 16:13:08 -0500354
Filipe Mananaf2071b22014-02-12 15:05:53 +0000355static struct rb_node *tree_insert(struct rb_root *root,
356 struct rb_node *search_start,
357 u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000358 struct rb_node *node,
359 struct rb_node ***p_in,
360 struct rb_node **parent_in)
Chris Masond1310b22008-01-24 16:13:08 -0500361{
Filipe Mananaf2071b22014-02-12 15:05:53 +0000362 struct rb_node **p;
Chris Masond3977122009-01-05 21:25:51 -0500363 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500364 struct tree_entry *entry;
365
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000366 if (p_in && parent_in) {
367 p = *p_in;
368 parent = *parent_in;
369 goto do_insert;
370 }
371
Filipe Mananaf2071b22014-02-12 15:05:53 +0000372 p = search_start ? &search_start : &root->rb_node;
Chris Masond3977122009-01-05 21:25:51 -0500373 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500374 parent = *p;
375 entry = rb_entry(parent, struct tree_entry, rb_node);
376
377 if (offset < entry->start)
378 p = &(*p)->rb_left;
379 else if (offset > entry->end)
380 p = &(*p)->rb_right;
381 else
382 return parent;
383 }
384
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000385do_insert:
Chris Masond1310b22008-01-24 16:13:08 -0500386 rb_link_node(node, parent, p);
387 rb_insert_color(node, root);
388 return NULL;
389}
390
Nikolay Borisov8666e632019-06-05 14:50:04 +0300391/**
392 * __etree_search - searche @tree for an entry that contains @offset. Such
393 * entry would have entry->start <= offset && entry->end >= offset.
394 *
395 * @tree - the tree to search
396 * @offset - offset that should fall within an entry in @tree
397 * @next_ret - pointer to the first entry whose range ends after @offset
398 * @prev - pointer to the first entry whose range begins before @offset
399 * @p_ret - pointer where new node should be anchored (used when inserting an
400 * entry in the tree)
401 * @parent_ret - points to entry which would have been the parent of the entry,
402 * containing @offset
403 *
404 * This function returns a pointer to the entry that contains @offset byte
405 * address. If no such entry exists, then NULL is returned and the other
406 * pointer arguments to the function are filled, otherwise the found entry is
407 * returned and other pointers are left untouched.
408 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500409static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000410 struct rb_node **next_ret,
Nikolay Borisov352646c2019-01-30 16:51:00 +0200411 struct rb_node **prev_ret,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000412 struct rb_node ***p_ret,
413 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500414{
Chris Mason80ea96b2008-02-01 14:51:59 -0500415 struct rb_root *root = &tree->state;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000416 struct rb_node **n = &root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500417 struct rb_node *prev = NULL;
418 struct rb_node *orig_prev = NULL;
419 struct tree_entry *entry;
420 struct tree_entry *prev_entry = NULL;
421
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000422 while (*n) {
423 prev = *n;
424 entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500425 prev_entry = entry;
426
427 if (offset < entry->start)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000428 n = &(*n)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500429 else if (offset > entry->end)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000430 n = &(*n)->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500431 else
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000432 return *n;
Chris Masond1310b22008-01-24 16:13:08 -0500433 }
434
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000435 if (p_ret)
436 *p_ret = n;
437 if (parent_ret)
438 *parent_ret = prev;
439
Nikolay Borisov352646c2019-01-30 16:51:00 +0200440 if (next_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500441 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500442 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500443 prev = rb_next(prev);
444 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
445 }
Nikolay Borisov352646c2019-01-30 16:51:00 +0200446 *next_ret = prev;
Chris Masond1310b22008-01-24 16:13:08 -0500447 prev = orig_prev;
448 }
449
Nikolay Borisov352646c2019-01-30 16:51:00 +0200450 if (prev_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500451 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500452 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500453 prev = rb_prev(prev);
454 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
455 }
Nikolay Borisov352646c2019-01-30 16:51:00 +0200456 *prev_ret = prev;
Chris Masond1310b22008-01-24 16:13:08 -0500457 }
458 return NULL;
459}
460
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000461static inline struct rb_node *
462tree_search_for_insert(struct extent_io_tree *tree,
463 u64 offset,
464 struct rb_node ***p_ret,
465 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500466{
Nikolay Borisov352646c2019-01-30 16:51:00 +0200467 struct rb_node *next= NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500468 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500469
Nikolay Borisov352646c2019-01-30 16:51:00 +0200470 ret = __etree_search(tree, offset, &next, NULL, p_ret, parent_ret);
Chris Masond3977122009-01-05 21:25:51 -0500471 if (!ret)
Nikolay Borisov352646c2019-01-30 16:51:00 +0200472 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500473 return ret;
474}
475
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000476static inline struct rb_node *tree_search(struct extent_io_tree *tree,
477 u64 offset)
478{
479 return tree_search_for_insert(tree, offset, NULL, NULL);
480}
481
Chris Masond1310b22008-01-24 16:13:08 -0500482/*
483 * utility function to look for merge candidates inside a given range.
484 * Any extents with matching state are merged together into a single
485 * extent in the tree. Extents with EXTENT_IO in their state field
486 * are not merged because the end_io handlers need to be able to do
487 * operations on them without sleeping (or doing allocations/splits).
488 *
489 * This should be called with the tree lock held.
490 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000491static void merge_state(struct extent_io_tree *tree,
492 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500493{
494 struct extent_state *other;
495 struct rb_node *other_node;
496
Nikolay Borisov88826792019-03-14 15:28:31 +0200497 if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000498 return;
Chris Masond1310b22008-01-24 16:13:08 -0500499
500 other_node = rb_prev(&state->rb_node);
501 if (other_node) {
502 other = rb_entry(other_node, struct extent_state, rb_node);
503 if (other->end == state->start - 1 &&
504 other->state == state->state) {
Nikolay Borisov5c848192018-11-01 14:09:52 +0200505 if (tree->private_data &&
506 is_data_inode(tree->private_data))
507 btrfs_merge_delalloc_extent(tree->private_data,
508 state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500509 state->start = other->start;
Chris Masond1310b22008-01-24 16:13:08 -0500510 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100511 RB_CLEAR_NODE(&other->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500512 free_extent_state(other);
513 }
514 }
515 other_node = rb_next(&state->rb_node);
516 if (other_node) {
517 other = rb_entry(other_node, struct extent_state, rb_node);
518 if (other->start == state->end + 1 &&
519 other->state == state->state) {
Nikolay Borisov5c848192018-11-01 14:09:52 +0200520 if (tree->private_data &&
521 is_data_inode(tree->private_data))
522 btrfs_merge_delalloc_extent(tree->private_data,
523 state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400524 state->end = other->end;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400525 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100526 RB_CLEAR_NODE(&other->rb_node);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400527 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500528 }
529 }
Chris Masond1310b22008-01-24 16:13:08 -0500530}
531
Xiao Guangrong3150b692011-07-14 03:19:08 +0000532static void set_state_bits(struct extent_io_tree *tree,
Qu Wenruof97e27e2020-11-13 20:51:40 +0800533 struct extent_state *state, u32 *bits,
Qu Wenruod38ed272015-10-12 14:53:37 +0800534 struct extent_changeset *changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000535
Chris Masond1310b22008-01-24 16:13:08 -0500536/*
537 * insert an extent_state struct into the tree. 'bits' are set on the
538 * struct before it is inserted.
539 *
540 * This may return -EEXIST if the extent is already there, in which case the
541 * state struct is freed.
542 *
543 * The tree lock is not taken internally. This is a utility function and
544 * probably isn't what you want to call (see set/clear_extent_bit).
545 */
546static int insert_state(struct extent_io_tree *tree,
547 struct extent_state *state, u64 start, u64 end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000548 struct rb_node ***p,
549 struct rb_node **parent,
Qu Wenruof97e27e2020-11-13 20:51:40 +0800550 u32 *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500551{
552 struct rb_node *node;
553
David Sterba27922372019-06-18 20:00:05 +0200554 if (end < start) {
555 btrfs_err(tree->fs_info,
556 "insert state: end < start %llu %llu", end, start);
557 WARN_ON(1);
558 }
Chris Masond1310b22008-01-24 16:13:08 -0500559 state->start = start;
560 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400561
Qu Wenruod38ed272015-10-12 14:53:37 +0800562 set_state_bits(tree, state, bits, changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000563
Filipe Mananaf2071b22014-02-12 15:05:53 +0000564 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
Chris Masond1310b22008-01-24 16:13:08 -0500565 if (node) {
566 struct extent_state *found;
567 found = rb_entry(node, struct extent_state, rb_node);
David Sterba27922372019-06-18 20:00:05 +0200568 btrfs_err(tree->fs_info,
569 "found node %llu %llu on insert of %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200570 found->start, found->end, start, end);
Chris Masond1310b22008-01-24 16:13:08 -0500571 return -EEXIST;
572 }
573 merge_state(tree, state);
574 return 0;
575}
576
577/*
578 * split a given extent state struct in two, inserting the preallocated
579 * struct 'prealloc' as the newly created second half. 'split' indicates an
580 * offset inside 'orig' where it should be split.
581 *
582 * Before calling,
583 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
584 * are two extent state structs in the tree:
585 * prealloc: [orig->start, split - 1]
586 * orig: [ split, orig->end ]
587 *
588 * The tree locks are not taken by this function. They need to be held
589 * by the caller.
590 */
591static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
592 struct extent_state *prealloc, u64 split)
593{
594 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400595
Nikolay Borisovabbb55f2018-11-01 14:09:53 +0200596 if (tree->private_data && is_data_inode(tree->private_data))
597 btrfs_split_delalloc_extent(tree->private_data, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400598
Chris Masond1310b22008-01-24 16:13:08 -0500599 prealloc->start = orig->start;
600 prealloc->end = split - 1;
601 prealloc->state = orig->state;
602 orig->start = split;
603
Filipe Mananaf2071b22014-02-12 15:05:53 +0000604 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
605 &prealloc->rb_node, NULL, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500606 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500607 free_extent_state(prealloc);
608 return -EEXIST;
609 }
610 return 0;
611}
612
Li Zefancdc6a392012-03-12 16:39:48 +0800613static struct extent_state *next_state(struct extent_state *state)
614{
615 struct rb_node *next = rb_next(&state->rb_node);
616 if (next)
617 return rb_entry(next, struct extent_state, rb_node);
618 else
619 return NULL;
620}
621
Chris Masond1310b22008-01-24 16:13:08 -0500622/*
623 * utility function to clear some bits in an extent state struct.
Andrea Gelmini52042d82018-11-28 12:05:13 +0100624 * it will optionally wake up anyone waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500625 *
626 * If no bits are set on the state struct after clearing things, the
627 * struct is freed and removed from the tree
628 */
Li Zefancdc6a392012-03-12 16:39:48 +0800629static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
630 struct extent_state *state,
Qu Wenruof97e27e2020-11-13 20:51:40 +0800631 u32 *bits, int wake,
Qu Wenruofefdc552015-10-12 15:35:38 +0800632 struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500633{
Li Zefancdc6a392012-03-12 16:39:48 +0800634 struct extent_state *next;
Qu Wenruof97e27e2020-11-13 20:51:40 +0800635 u32 bits_to_clear = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100636 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500637
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400638 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500639 u64 range = state->end - state->start + 1;
640 WARN_ON(range > tree->dirty_bytes);
641 tree->dirty_bytes -= range;
642 }
Nikolay Borisova36bb5f2018-11-01 14:09:51 +0200643
644 if (tree->private_data && is_data_inode(tree->private_data))
645 btrfs_clear_delalloc_extent(tree->private_data, state, bits);
646
David Sterba57599c72018-03-01 17:56:34 +0100647 ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
648 BUG_ON(ret < 0);
Josef Bacik32c00af2009-10-08 13:34:05 -0400649 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500650 if (wake)
651 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400652 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800653 next = next_state(state);
Filipe Manana27a35072014-07-06 20:09:59 +0100654 if (extent_state_in_tree(state)) {
Chris Masond1310b22008-01-24 16:13:08 -0500655 rb_erase(&state->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100656 RB_CLEAR_NODE(&state->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500657 free_extent_state(state);
658 } else {
659 WARN_ON(1);
660 }
661 } else {
662 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800663 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500664 }
Li Zefancdc6a392012-03-12 16:39:48 +0800665 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500666}
667
Xiao Guangrong82337672011-04-20 06:44:57 +0000668static struct extent_state *
669alloc_extent_state_atomic(struct extent_state *prealloc)
670{
671 if (!prealloc)
672 prealloc = alloc_extent_state(GFP_ATOMIC);
673
674 return prealloc;
675}
676
Eric Sandeen48a3b632013-04-25 20:41:01 +0000677static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400678{
David Sterba05912a32018-07-18 19:23:45 +0200679 struct inode *inode = tree->private_data;
680
681 btrfs_panic(btrfs_sb(inode->i_sb), err,
682 "locking error: extent tree was modified by another thread while locked");
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400683}
684
Chris Masond1310b22008-01-24 16:13:08 -0500685/*
686 * clear some bits on a range in the tree. This may require splitting
687 * or inserting elements in the tree, so the gfp mask is used to
688 * indicate which allocations or sleeping are allowed.
689 *
690 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
691 * the given range from the tree regardless of state (ie for truncate).
692 *
693 * the range [start, end] is inclusive.
694 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100695 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500696 */
David Sterba66b0c882017-10-31 16:30:47 +0100697int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Qu Wenruof97e27e2020-11-13 20:51:40 +0800698 u32 bits, int wake, int delete,
699 struct extent_state **cached_state,
700 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500701{
702 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400703 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500704 struct extent_state *prealloc = NULL;
705 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400706 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500707 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000708 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500709
Josef Bacika5dee372013-12-13 10:02:44 -0500710 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +0800711 trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits);
David Sterba8d599ae2013-04-30 15:22:23 +0000712
Josef Bacik7ee9e442013-06-21 16:37:03 -0400713 if (bits & EXTENT_DELALLOC)
714 bits |= EXTENT_NORESERVE;
715
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400716 if (delete)
717 bits |= ~EXTENT_CTLBITS;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400718
Nikolay Borisov88826792019-03-14 15:28:31 +0200719 if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY))
Josef Bacik2ac55d42010-02-03 19:33:23 +0000720 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500721again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800722 if (!prealloc && gfpflags_allow_blocking(mask)) {
Filipe Mananac7bc6312014-11-03 14:12:57 +0000723 /*
724 * Don't care for allocation failure here because we might end
725 * up not needing the pre-allocated extent state at all, which
726 * is the case if we only have in the tree extent states that
727 * cover our input range and don't cover too any other range.
728 * If we end up needing a new extent state we allocate it later.
729 */
Chris Masond1310b22008-01-24 16:13:08 -0500730 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500731 }
732
Chris Masoncad321a2008-12-17 14:51:42 -0500733 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400734 if (cached_state) {
735 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000736
737 if (clear) {
738 *cached_state = NULL;
739 cached_state = NULL;
740 }
741
Filipe Manana27a35072014-07-06 20:09:59 +0100742 if (cached && extent_state_in_tree(cached) &&
743 cached->start <= start && cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000744 if (clear)
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200745 refcount_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400746 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400747 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400748 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000749 if (clear)
750 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400751 }
Chris Masond1310b22008-01-24 16:13:08 -0500752 /*
753 * this search will find the extents that end after
754 * our range starts
755 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500756 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500757 if (!node)
758 goto out;
759 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400760hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500761 if (state->start > end)
762 goto out;
763 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400764 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500765
Liu Bo04493142012-02-16 18:34:37 +0800766 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800767 if (!(state->state & bits)) {
768 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800769 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800770 }
Liu Bo04493142012-02-16 18:34:37 +0800771
Chris Masond1310b22008-01-24 16:13:08 -0500772 /*
773 * | ---- desired range ---- |
774 * | state | or
775 * | ------------- state -------------- |
776 *
777 * We need to split the extent we found, and may flip
778 * bits on second half.
779 *
780 * If the extent we found extends past our range, we
781 * just split and search again. It'll get split again
782 * the next time though.
783 *
784 * If the extent we found is inside our range, we clear
785 * the desired bit on it.
786 */
787
788 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000789 prealloc = alloc_extent_state_atomic(prealloc);
790 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500791 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400792 if (err)
793 extent_io_tree_panic(tree, err);
794
Chris Masond1310b22008-01-24 16:13:08 -0500795 prealloc = NULL;
796 if (err)
797 goto out;
798 if (state->end <= end) {
Qu Wenruofefdc552015-10-12 15:35:38 +0800799 state = clear_state_bit(tree, state, &bits, wake,
800 changeset);
Liu Bod1ac6e42012-05-10 18:10:39 +0800801 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500802 }
803 goto search_again;
804 }
805 /*
806 * | ---- desired range ---- |
807 * | state |
808 * We need to split the extent, and clear the bit
809 * on the first half
810 */
811 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000812 prealloc = alloc_extent_state_atomic(prealloc);
813 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500814 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400815 if (err)
816 extent_io_tree_panic(tree, err);
817
Chris Masond1310b22008-01-24 16:13:08 -0500818 if (wake)
819 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400820
Qu Wenruofefdc552015-10-12 15:35:38 +0800821 clear_state_bit(tree, prealloc, &bits, wake, changeset);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400822
Chris Masond1310b22008-01-24 16:13:08 -0500823 prealloc = NULL;
824 goto out;
825 }
Chris Mason42daec22009-09-23 19:51:09 -0400826
Qu Wenruofefdc552015-10-12 15:35:38 +0800827 state = clear_state_bit(tree, state, &bits, wake, changeset);
Liu Bo04493142012-02-16 18:34:37 +0800828next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400829 if (last_end == (u64)-1)
830 goto out;
831 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800832 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800833 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500834
835search_again:
836 if (start > end)
837 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500838 spin_unlock(&tree->lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800839 if (gfpflags_allow_blocking(mask))
Chris Masond1310b22008-01-24 16:13:08 -0500840 cond_resched();
841 goto again;
David Sterba7ab5cb22016-04-27 01:02:15 +0200842
843out:
844 spin_unlock(&tree->lock);
845 if (prealloc)
846 free_extent_state(prealloc);
847
848 return 0;
849
Chris Masond1310b22008-01-24 16:13:08 -0500850}
Chris Masond1310b22008-01-24 16:13:08 -0500851
Jeff Mahoney143bede2012-03-01 14:56:26 +0100852static void wait_on_state(struct extent_io_tree *tree,
853 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500854 __releases(tree->lock)
855 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500856{
857 DEFINE_WAIT(wait);
858 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500859 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500860 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500861 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500862 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500863}
864
865/*
866 * waits for one or more bits to clear on a range in the state tree.
867 * The range [start, end] is inclusive.
868 * The tree lock is taken by this function
869 */
David Sterba41074882013-04-29 13:38:46 +0000870static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Qu Wenruof97e27e2020-11-13 20:51:40 +0800871 u32 bits)
Chris Masond1310b22008-01-24 16:13:08 -0500872{
873 struct extent_state *state;
874 struct rb_node *node;
875
Josef Bacika5dee372013-12-13 10:02:44 -0500876 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000877
Chris Masoncad321a2008-12-17 14:51:42 -0500878 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500879again:
880 while (1) {
881 /*
882 * this search will find all the extents that end after
883 * our range starts
884 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500885 node = tree_search(tree, start);
Filipe Mananac50d3e72014-03-31 14:53:25 +0100886process_node:
Chris Masond1310b22008-01-24 16:13:08 -0500887 if (!node)
888 break;
889
890 state = rb_entry(node, struct extent_state, rb_node);
891
892 if (state->start > end)
893 goto out;
894
895 if (state->state & bits) {
896 start = state->start;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200897 refcount_inc(&state->refs);
Chris Masond1310b22008-01-24 16:13:08 -0500898 wait_on_state(tree, state);
899 free_extent_state(state);
900 goto again;
901 }
902 start = state->end + 1;
903
904 if (start > end)
905 break;
906
Filipe Mananac50d3e72014-03-31 14:53:25 +0100907 if (!cond_resched_lock(&tree->lock)) {
908 node = rb_next(node);
909 goto process_node;
910 }
Chris Masond1310b22008-01-24 16:13:08 -0500911 }
912out:
Chris Masoncad321a2008-12-17 14:51:42 -0500913 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500914}
Chris Masond1310b22008-01-24 16:13:08 -0500915
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000916static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500917 struct extent_state *state,
Qu Wenruof97e27e2020-11-13 20:51:40 +0800918 u32 *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500919{
Qu Wenruof97e27e2020-11-13 20:51:40 +0800920 u32 bits_to_set = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100921 int ret;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400922
Nikolay Borisove06a1fc2018-11-01 14:09:50 +0200923 if (tree->private_data && is_data_inode(tree->private_data))
924 btrfs_set_delalloc_extent(tree->private_data, state, bits);
925
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400926 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500927 u64 range = state->end - state->start + 1;
928 tree->dirty_bytes += range;
929 }
David Sterba57599c72018-03-01 17:56:34 +0100930 ret = add_extent_changeset(state, bits_to_set, changeset, 1);
931 BUG_ON(ret < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400932 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500933}
934
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100935static void cache_state_if_flags(struct extent_state *state,
936 struct extent_state **cached_ptr,
David Sterba9ee49a042015-01-14 19:52:13 +0100937 unsigned flags)
Chris Mason2c64c532009-09-02 15:04:12 -0400938{
939 if (cached_ptr && !(*cached_ptr)) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100940 if (!flags || (state->state & flags)) {
Chris Mason2c64c532009-09-02 15:04:12 -0400941 *cached_ptr = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200942 refcount_inc(&state->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400943 }
944 }
945}
946
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100947static void cache_state(struct extent_state *state,
948 struct extent_state **cached_ptr)
949{
950 return cache_state_if_flags(state, cached_ptr,
Nikolay Borisov88826792019-03-14 15:28:31 +0200951 EXTENT_LOCKED | EXTENT_BOUNDARY);
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100952}
953
Chris Masond1310b22008-01-24 16:13:08 -0500954/*
Chris Mason1edbb732009-09-02 13:24:36 -0400955 * set some bits on a range in the tree. This may require allocations or
956 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500957 *
Chris Mason1edbb732009-09-02 13:24:36 -0400958 * If any of the exclusive bits are set, this will fail with -EEXIST if some
959 * part of the range already has the desired bits set. The start of the
960 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500961 *
Chris Mason1edbb732009-09-02 13:24:36 -0400962 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500963 */
Qu Wenruof97e27e2020-11-13 20:51:40 +0800964int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
965 u32 exclusive_bits, u64 *failed_start,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +0200966 struct extent_state **cached_state, gfp_t mask,
967 struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500968{
969 struct extent_state *state;
970 struct extent_state *prealloc = NULL;
971 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000972 struct rb_node **p;
973 struct rb_node *parent;
Chris Masond1310b22008-01-24 16:13:08 -0500974 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500975 u64 last_start;
976 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400977
Josef Bacika5dee372013-12-13 10:02:44 -0500978 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +0800979 trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits);
David Sterba8d599ae2013-04-30 15:22:23 +0000980
Qu Wenruo3f6bb4a2020-10-21 14:24:51 +0800981 if (exclusive_bits)
982 ASSERT(failed_start);
983 else
984 ASSERT(failed_start == NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500985again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800986 if (!prealloc && gfpflags_allow_blocking(mask)) {
David Sterba059f7912016-04-27 01:03:45 +0200987 /*
988 * Don't care for allocation failure here because we might end
989 * up not needing the pre-allocated extent state at all, which
990 * is the case if we only have in the tree extent states that
991 * cover our input range and don't cover too any other range.
992 * If we end up needing a new extent state we allocate it later.
993 */
Chris Masond1310b22008-01-24 16:13:08 -0500994 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500995 }
996
Chris Masoncad321a2008-12-17 14:51:42 -0500997 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400998 if (cached_state && *cached_state) {
999 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001000 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +01001001 extent_state_in_tree(state)) {
Chris Mason9655d292009-09-02 15:22:30 -04001002 node = &state->rb_node;
1003 goto hit_next;
1004 }
1005 }
Chris Masond1310b22008-01-24 16:13:08 -05001006 /*
1007 * this search will find all the extents that end after
1008 * our range starts.
1009 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001010 node = tree_search_for_insert(tree, start, &p, &parent);
Chris Masond1310b22008-01-24 16:13:08 -05001011 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +00001012 prealloc = alloc_extent_state_atomic(prealloc);
1013 BUG_ON(!prealloc);
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001014 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001015 &p, &parent, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001016 if (err)
1017 extent_io_tree_panic(tree, err);
1018
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001019 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001020 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001021 goto out;
1022 }
Chris Masond1310b22008-01-24 16:13:08 -05001023 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -04001024hit_next:
Chris Masond1310b22008-01-24 16:13:08 -05001025 last_start = state->start;
1026 last_end = state->end;
1027
1028 /*
1029 * | ---- desired range ---- |
1030 * | state |
1031 *
1032 * Just lock what we found and keep going
1033 */
1034 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -04001035 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001036 *failed_start = state->start;
1037 err = -EEXIST;
1038 goto out;
1039 }
Chris Mason42daec22009-09-23 19:51:09 -04001040
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 goto search_again;
1052 }
1053
1054 /*
1055 * | ---- desired range ---- |
1056 * | state |
1057 * or
1058 * | ------------- state -------------- |
1059 *
1060 * We need to split the extent we found, and may flip bits on
1061 * second half.
1062 *
1063 * If the extent we found extends past our
1064 * range, we just split and search again. It'll get split
1065 * again the next time though.
1066 *
1067 * If the extent we found is inside our range, we set the
1068 * desired bit on it.
1069 */
1070 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -04001071 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001072 *failed_start = start;
1073 err = -EEXIST;
1074 goto out;
1075 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001076
Filipe Manana55ffaab2020-02-13 10:20:02 +00001077 /*
1078 * If this extent already has all the bits we want set, then
1079 * skip it, not necessary to split it or do anything with it.
1080 */
1081 if ((state->state & bits) == bits) {
1082 start = state->end + 1;
1083 cache_state(state, cached_state);
1084 goto search_again;
1085 }
1086
Xiao Guangrong82337672011-04-20 06:44:57 +00001087 prealloc = alloc_extent_state_atomic(prealloc);
1088 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001089 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001090 if (err)
1091 extent_io_tree_panic(tree, err);
1092
Chris Masond1310b22008-01-24 16:13:08 -05001093 prealloc = NULL;
1094 if (err)
1095 goto out;
1096 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001097 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001098 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001099 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -04001100 if (last_end == (u64)-1)
1101 goto out;
1102 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001103 state = next_state(state);
1104 if (start < end && state && state->start == start &&
1105 !need_resched())
1106 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -05001107 }
1108 goto search_again;
1109 }
1110 /*
1111 * | ---- desired range ---- |
1112 * | state | or | state |
1113 *
1114 * There's a hole, we need to insert something in it and
1115 * ignore the extent we found.
1116 */
1117 if (state->start > start) {
1118 u64 this_end;
1119 if (end < last_start)
1120 this_end = end;
1121 else
Chris Masond3977122009-01-05 21:25:51 -05001122 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +00001123
1124 prealloc = alloc_extent_state_atomic(prealloc);
1125 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +00001126
1127 /*
1128 * Avoid to free 'prealloc' if it can be merged with
1129 * the later extent.
1130 */
Chris Masond1310b22008-01-24 16:13:08 -05001131 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001132 NULL, NULL, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001133 if (err)
1134 extent_io_tree_panic(tree, err);
1135
Chris Mason2c64c532009-09-02 15:04:12 -04001136 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001137 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001138 start = this_end + 1;
1139 goto search_again;
1140 }
1141 /*
1142 * | ---- desired range ---- |
1143 * | state |
1144 * We need to split the extent, and set the bit
1145 * on the first half
1146 */
1147 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -04001148 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001149 *failed_start = start;
1150 err = -EEXIST;
1151 goto out;
1152 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001153
1154 prealloc = alloc_extent_state_atomic(prealloc);
1155 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001156 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001157 if (err)
1158 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -05001159
Qu Wenruod38ed272015-10-12 14:53:37 +08001160 set_state_bits(tree, prealloc, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001161 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001162 merge_state(tree, prealloc);
1163 prealloc = NULL;
1164 goto out;
1165 }
1166
David Sterbab5a4ba142016-04-27 01:02:15 +02001167search_again:
1168 if (start > end)
1169 goto out;
1170 spin_unlock(&tree->lock);
1171 if (gfpflags_allow_blocking(mask))
1172 cond_resched();
1173 goto again;
Chris Masond1310b22008-01-24 16:13:08 -05001174
1175out:
Chris Masoncad321a2008-12-17 14:51:42 -05001176 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001177 if (prealloc)
1178 free_extent_state(prealloc);
1179
1180 return err;
1181
Chris Masond1310b22008-01-24 16:13:08 -05001182}
Chris Masond1310b22008-01-24 16:13:08 -05001183
Josef Bacik462d6fa2011-09-26 13:56:12 -04001184/**
Liu Bo10983f22012-07-11 15:26:19 +08001185 * convert_extent_bit - convert all bits in a given range from one bit to
1186 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -04001187 * @tree: the io tree to search
1188 * @start: the start offset in bytes
1189 * @end: the end offset in bytes (inclusive)
1190 * @bits: the bits to set in this range
1191 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001192 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001193 *
1194 * This will go through and set bits for the given range. If any states exist
1195 * already in this range they are set with the given bit and cleared of the
1196 * clear_bits. This is only meant to be used by things that are mergeable, ie
1197 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1198 * boundary bits like LOCK.
David Sterba210aa272016-04-26 23:54:39 +02001199 *
1200 * All allocations are done with GFP_NOFS.
Josef Bacik462d6fa2011-09-26 13:56:12 -04001201 */
1202int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Qu Wenruof97e27e2020-11-13 20:51:40 +08001203 u32 bits, u32 clear_bits,
David Sterba210aa272016-04-26 23:54:39 +02001204 struct extent_state **cached_state)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001205{
1206 struct extent_state *state;
1207 struct extent_state *prealloc = NULL;
1208 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001209 struct rb_node **p;
1210 struct rb_node *parent;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001211 int err = 0;
1212 u64 last_start;
1213 u64 last_end;
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001214 bool first_iteration = true;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001215
Josef Bacika5dee372013-12-13 10:02:44 -05001216 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +08001217 trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits,
1218 clear_bits);
David Sterba8d599ae2013-04-30 15:22:23 +00001219
Josef Bacik462d6fa2011-09-26 13:56:12 -04001220again:
David Sterba210aa272016-04-26 23:54:39 +02001221 if (!prealloc) {
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001222 /*
1223 * Best effort, don't worry if extent state allocation fails
1224 * here for the first iteration. We might have a cached state
1225 * that matches exactly the target range, in which case no
1226 * extent state allocations are needed. We'll only know this
1227 * after locking the tree.
1228 */
David Sterba210aa272016-04-26 23:54:39 +02001229 prealloc = alloc_extent_state(GFP_NOFS);
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001230 if (!prealloc && !first_iteration)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001231 return -ENOMEM;
1232 }
1233
1234 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001235 if (cached_state && *cached_state) {
1236 state = *cached_state;
1237 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +01001238 extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001239 node = &state->rb_node;
1240 goto hit_next;
1241 }
1242 }
1243
Josef Bacik462d6fa2011-09-26 13:56:12 -04001244 /*
1245 * this search will find all the extents that end after
1246 * our range starts.
1247 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001248 node = tree_search_for_insert(tree, start, &p, &parent);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001249 if (!node) {
1250 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001251 if (!prealloc) {
1252 err = -ENOMEM;
1253 goto out;
1254 }
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001255 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001256 &p, &parent, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001257 if (err)
1258 extent_io_tree_panic(tree, err);
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001259 cache_state(prealloc, cached_state);
1260 prealloc = NULL;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001261 goto out;
1262 }
1263 state = rb_entry(node, struct extent_state, rb_node);
1264hit_next:
1265 last_start = state->start;
1266 last_end = state->end;
1267
1268 /*
1269 * | ---- desired range ---- |
1270 * | state |
1271 *
1272 * Just lock what we found and keep going
1273 */
1274 if (state->start == start && state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001275 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001276 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001277 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001278 if (last_end == (u64)-1)
1279 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001280 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001281 if (start < end && state && state->start == start &&
1282 !need_resched())
1283 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001284 goto search_again;
1285 }
1286
1287 /*
1288 * | ---- desired range ---- |
1289 * | state |
1290 * or
1291 * | ------------- state -------------- |
1292 *
1293 * We need to split the extent we found, and may flip bits on
1294 * second half.
1295 *
1296 * If the extent we found extends past our
1297 * range, we just split and search again. It'll get split
1298 * again the next time though.
1299 *
1300 * If the extent we found is inside our range, we set the
1301 * desired bit on it.
1302 */
1303 if (state->start < start) {
1304 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001305 if (!prealloc) {
1306 err = -ENOMEM;
1307 goto out;
1308 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001309 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001310 if (err)
1311 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001312 prealloc = NULL;
1313 if (err)
1314 goto out;
1315 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001316 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001317 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001318 state = clear_state_bit(tree, state, &clear_bits, 0,
1319 NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001320 if (last_end == (u64)-1)
1321 goto out;
1322 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001323 if (start < end && state && state->start == start &&
1324 !need_resched())
1325 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001326 }
1327 goto search_again;
1328 }
1329 /*
1330 * | ---- desired range ---- |
1331 * | state | or | state |
1332 *
1333 * There's a hole, we need to insert something in it and
1334 * ignore the extent we found.
1335 */
1336 if (state->start > start) {
1337 u64 this_end;
1338 if (end < last_start)
1339 this_end = end;
1340 else
1341 this_end = last_start - 1;
1342
1343 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001344 if (!prealloc) {
1345 err = -ENOMEM;
1346 goto out;
1347 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001348
1349 /*
1350 * Avoid to free 'prealloc' if it can be merged with
1351 * the later extent.
1352 */
1353 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001354 NULL, NULL, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001355 if (err)
1356 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001357 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001358 prealloc = NULL;
1359 start = this_end + 1;
1360 goto search_again;
1361 }
1362 /*
1363 * | ---- desired range ---- |
1364 * | state |
1365 * We need to split the extent, and set the bit
1366 * on the first half
1367 */
1368 if (state->start <= end && state->end > end) {
1369 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001370 if (!prealloc) {
1371 err = -ENOMEM;
1372 goto out;
1373 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001374
1375 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001376 if (err)
1377 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001378
Qu Wenruod38ed272015-10-12 14:53:37 +08001379 set_state_bits(tree, prealloc, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001380 cache_state(prealloc, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001381 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001382 prealloc = NULL;
1383 goto out;
1384 }
1385
Josef Bacik462d6fa2011-09-26 13:56:12 -04001386search_again:
1387 if (start > end)
1388 goto out;
1389 spin_unlock(&tree->lock);
David Sterba210aa272016-04-26 23:54:39 +02001390 cond_resched();
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001391 first_iteration = false;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001392 goto again;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001393
1394out:
1395 spin_unlock(&tree->lock);
1396 if (prealloc)
1397 free_extent_state(prealloc);
1398
1399 return err;
1400}
1401
Chris Masond1310b22008-01-24 16:13:08 -05001402/* wrappers around set/clear extent bit */
Qu Wenruod38ed272015-10-12 14:53:37 +08001403int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Qu Wenruof97e27e2020-11-13 20:51:40 +08001404 u32 bits, struct extent_changeset *changeset)
Qu Wenruod38ed272015-10-12 14:53:37 +08001405{
1406 /*
1407 * We don't support EXTENT_LOCKED yet, as current changeset will
1408 * record any bits changed, so for EXTENT_LOCKED case, it will
1409 * either fail with -EEXIST or changeset will record the whole
1410 * range.
1411 */
1412 BUG_ON(bits & EXTENT_LOCKED);
1413
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02001414 return set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
1415 changeset);
Qu Wenruod38ed272015-10-12 14:53:37 +08001416}
1417
Nikolay Borisov4ca73652019-03-27 14:24:10 +02001418int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start, u64 end,
Qu Wenruof97e27e2020-11-13 20:51:40 +08001419 u32 bits)
Nikolay Borisov4ca73652019-03-27 14:24:10 +02001420{
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02001421 return set_extent_bit(tree, start, end, bits, 0, NULL, NULL,
1422 GFP_NOWAIT, NULL);
Nikolay Borisov4ca73652019-03-27 14:24:10 +02001423}
1424
Qu Wenruofefdc552015-10-12 15:35:38 +08001425int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Qu Wenruof97e27e2020-11-13 20:51:40 +08001426 u32 bits, int wake, int delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001427 struct extent_state **cached)
Qu Wenruofefdc552015-10-12 15:35:38 +08001428{
1429 return __clear_extent_bit(tree, start, end, bits, wake, delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001430 cached, GFP_NOFS, NULL);
Qu Wenruofefdc552015-10-12 15:35:38 +08001431}
1432
Qu Wenruofefdc552015-10-12 15:35:38 +08001433int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Qu Wenruof97e27e2020-11-13 20:51:40 +08001434 u32 bits, struct extent_changeset *changeset)
Qu Wenruofefdc552015-10-12 15:35:38 +08001435{
1436 /*
1437 * Don't support EXTENT_LOCKED case, same reason as
1438 * set_record_extent_bits().
1439 */
1440 BUG_ON(bits & EXTENT_LOCKED);
1441
David Sterbaf734c442016-04-26 23:54:39 +02001442 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
Qu Wenruofefdc552015-10-12 15:35:38 +08001443 changeset);
1444}
1445
Chris Masond352ac62008-09-29 15:18:18 -04001446/*
1447 * either insert or lock state struct between start and end use mask to tell
1448 * us if waiting is desired.
1449 */
Chris Mason1edbb732009-09-02 13:24:36 -04001450int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaff13db42015-12-03 14:30:40 +01001451 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001452{
1453 int err;
1454 u64 failed_start;
David Sterba9ee49a042015-01-14 19:52:13 +01001455
Chris Masond1310b22008-01-24 16:13:08 -05001456 while (1) {
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02001457 err = set_extent_bit(tree, start, end, EXTENT_LOCKED,
1458 EXTENT_LOCKED, &failed_start,
1459 cached_state, GFP_NOFS, NULL);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001460 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001461 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1462 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001463 } else
Chris Masond1310b22008-01-24 16:13:08 -05001464 break;
Chris Masond1310b22008-01-24 16:13:08 -05001465 WARN_ON(start > end);
1466 }
1467 return err;
1468}
Chris Masond1310b22008-01-24 16:13:08 -05001469
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001470int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001471{
1472 int err;
1473 u64 failed_start;
1474
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02001475 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1476 &failed_start, NULL, GFP_NOFS, NULL);
Yan Zheng66435582008-10-30 14:19:50 -04001477 if (err == -EEXIST) {
1478 if (failed_start > start)
1479 clear_extent_bit(tree, start, failed_start - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001480 EXTENT_LOCKED, 1, 0, NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001481 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001482 }
Josef Bacik25179202008-10-29 14:49:05 -04001483 return 1;
1484}
Josef Bacik25179202008-10-29 14:49:05 -04001485
David Sterbabd1fa4f2015-12-03 13:08:59 +01001486void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001487{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001488 unsigned long index = start >> PAGE_SHIFT;
1489 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001490 struct page *page;
1491
1492 while (index <= end_index) {
1493 page = find_get_page(inode->i_mapping, index);
1494 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1495 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001496 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001497 index++;
1498 }
Chris Mason4adaa612013-03-26 13:07:00 -04001499}
1500
David Sterbaf6311572015-12-03 13:08:59 +01001501void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001502{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001503 unsigned long index = start >> PAGE_SHIFT;
1504 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001505 struct page *page;
1506
1507 while (index <= end_index) {
1508 page = find_get_page(inode->i_mapping, index);
1509 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Mason4adaa612013-03-26 13:07:00 -04001510 __set_page_dirty_nobuffers(page);
Konstantin Khebnikov8d386332015-02-11 15:26:55 -08001511 account_page_redirty(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001512 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001513 index++;
1514 }
Chris Mason4adaa612013-03-26 13:07:00 -04001515}
1516
Chris Masond352ac62008-09-29 15:18:18 -04001517/* find the first state struct with 'bits' set after 'start', and
1518 * return it. tree->lock must be held. NULL will returned if
1519 * nothing was found after 'start'
1520 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001521static struct extent_state *
Qu Wenruof97e27e2020-11-13 20:51:40 +08001522find_first_extent_bit_state(struct extent_io_tree *tree, u64 start, u32 bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001523{
1524 struct rb_node *node;
1525 struct extent_state *state;
1526
1527 /*
1528 * this search will find all the extents that end after
1529 * our range starts.
1530 */
1531 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001532 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001533 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001534
Chris Masond3977122009-01-05 21:25:51 -05001535 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001536 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001537 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001538 return state;
Chris Masond3977122009-01-05 21:25:51 -05001539
Chris Masond7fc6402008-02-18 12:12:38 -05001540 node = rb_next(node);
1541 if (!node)
1542 break;
1543 }
1544out:
1545 return NULL;
1546}
Chris Masond7fc6402008-02-18 12:12:38 -05001547
Chris Masond352ac62008-09-29 15:18:18 -04001548/*
Qu Wenruo03509b72020-10-21 14:24:50 +08001549 * Find the first offset in the io tree with one or more @bits set.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001550 *
Qu Wenruo03509b72020-10-21 14:24:50 +08001551 * Note: If there are multiple bits set in @bits, any of them will match.
1552 *
1553 * Return 0 if we find something, and update @start_ret and @end_ret.
1554 * Return 1 if we found nothing.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001555 */
1556int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
Qu Wenruof97e27e2020-11-13 20:51:40 +08001557 u64 *start_ret, u64 *end_ret, u32 bits,
Josef Bacike6138872012-09-27 17:07:30 -04001558 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001559{
1560 struct extent_state *state;
1561 int ret = 1;
1562
1563 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001564 if (cached_state && *cached_state) {
1565 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001566 if (state->end == start - 1 && extent_state_in_tree(state)) {
Liu Bo9688e9a2018-08-23 03:14:53 +08001567 while ((state = next_state(state)) != NULL) {
Josef Bacike6138872012-09-27 17:07:30 -04001568 if (state->state & bits)
1569 goto got_it;
Josef Bacike6138872012-09-27 17:07:30 -04001570 }
1571 free_extent_state(*cached_state);
1572 *cached_state = NULL;
1573 goto out;
1574 }
1575 free_extent_state(*cached_state);
1576 *cached_state = NULL;
1577 }
1578
Xiao Guangrong69261c42011-07-14 03:19:45 +00001579 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001580got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001581 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001582 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001583 *start_ret = state->start;
1584 *end_ret = state->end;
1585 ret = 0;
1586 }
Josef Bacike6138872012-09-27 17:07:30 -04001587out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001588 spin_unlock(&tree->lock);
1589 return ret;
1590}
1591
Nikolay Borisov45bfcfc2019-03-27 14:24:17 +02001592/**
Josef Bacik41a2ee72020-01-17 09:02:21 -05001593 * find_contiguous_extent_bit: find a contiguous area of bits
1594 * @tree - io tree to check
1595 * @start - offset to start the search from
1596 * @start_ret - the first offset we found with the bits set
1597 * @end_ret - the final contiguous range of the bits that were set
1598 * @bits - bits to look for
1599 *
1600 * set_extent_bit and clear_extent_bit can temporarily split contiguous ranges
1601 * to set bits appropriately, and then merge them again. During this time it
1602 * will drop the tree->lock, so use this helper if you want to find the actual
1603 * contiguous area for given bits. We will search to the first bit we find, and
1604 * then walk down the tree until we find a non-contiguous area. The area
1605 * returned will be the full contiguous area with the bits set.
1606 */
1607int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
Qu Wenruof97e27e2020-11-13 20:51:40 +08001608 u64 *start_ret, u64 *end_ret, u32 bits)
Josef Bacik41a2ee72020-01-17 09:02:21 -05001609{
1610 struct extent_state *state;
1611 int ret = 1;
1612
1613 spin_lock(&tree->lock);
1614 state = find_first_extent_bit_state(tree, start, bits);
1615 if (state) {
1616 *start_ret = state->start;
1617 *end_ret = state->end;
1618 while ((state = next_state(state)) != NULL) {
1619 if (state->start > (*end_ret + 1))
1620 break;
1621 *end_ret = state->end;
1622 }
1623 ret = 0;
1624 }
1625 spin_unlock(&tree->lock);
1626 return ret;
1627}
1628
1629/**
Nikolay Borisov1eaebb32019-06-03 13:06:02 +03001630 * find_first_clear_extent_bit - find the first range that has @bits not set.
1631 * This range could start before @start.
Nikolay Borisov45bfcfc2019-03-27 14:24:17 +02001632 *
1633 * @tree - the tree to search
1634 * @start - the offset at/after which the found extent should start
1635 * @start_ret - records the beginning of the range
1636 * @end_ret - records the end of the range (inclusive)
1637 * @bits - the set of bits which must be unset
1638 *
1639 * Since unallocated range is also considered one which doesn't have the bits
1640 * set it's possible that @end_ret contains -1, this happens in case the range
1641 * spans (last_range_end, end of device]. In this case it's up to the caller to
1642 * trim @end_ret to the appropriate size.
1643 */
1644void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
Qu Wenruof97e27e2020-11-13 20:51:40 +08001645 u64 *start_ret, u64 *end_ret, u32 bits)
Nikolay Borisov45bfcfc2019-03-27 14:24:17 +02001646{
1647 struct extent_state *state;
1648 struct rb_node *node, *prev = NULL, *next;
1649
1650 spin_lock(&tree->lock);
1651
1652 /* Find first extent with bits cleared */
1653 while (1) {
1654 node = __etree_search(tree, start, &next, &prev, NULL, NULL);
Nikolay Borisov5750c372020-01-27 11:59:26 +02001655 if (!node && !next && !prev) {
1656 /*
1657 * Tree is completely empty, send full range and let
1658 * caller deal with it
1659 */
1660 *start_ret = 0;
1661 *end_ret = -1;
1662 goto out;
1663 } else if (!node && !next) {
1664 /*
1665 * We are past the last allocated chunk, set start at
1666 * the end of the last extent.
1667 */
1668 state = rb_entry(prev, struct extent_state, rb_node);
1669 *start_ret = state->end + 1;
1670 *end_ret = -1;
1671 goto out;
1672 } else if (!node) {
Nikolay Borisov45bfcfc2019-03-27 14:24:17 +02001673 node = next;
Nikolay Borisov45bfcfc2019-03-27 14:24:17 +02001674 }
Nikolay Borisov1eaebb32019-06-03 13:06:02 +03001675 /*
1676 * At this point 'node' either contains 'start' or start is
1677 * before 'node'
1678 */
Nikolay Borisov45bfcfc2019-03-27 14:24:17 +02001679 state = rb_entry(node, struct extent_state, rb_node);
Nikolay Borisov1eaebb32019-06-03 13:06:02 +03001680
1681 if (in_range(start, state->start, state->end - state->start + 1)) {
1682 if (state->state & bits) {
1683 /*
1684 * |--range with bits sets--|
1685 * |
1686 * start
1687 */
1688 start = state->end + 1;
1689 } else {
1690 /*
1691 * 'start' falls within a range that doesn't
1692 * have the bits set, so take its start as
1693 * the beginning of the desired range
1694 *
1695 * |--range with bits cleared----|
1696 * |
1697 * start
1698 */
1699 *start_ret = state->start;
1700 break;
1701 }
Nikolay Borisov45bfcfc2019-03-27 14:24:17 +02001702 } else {
Nikolay Borisov1eaebb32019-06-03 13:06:02 +03001703 /*
1704 * |---prev range---|---hole/unset---|---node range---|
1705 * |
1706 * start
1707 *
1708 * or
1709 *
1710 * |---hole/unset--||--first node--|
1711 * 0 |
1712 * start
1713 */
1714 if (prev) {
1715 state = rb_entry(prev, struct extent_state,
1716 rb_node);
1717 *start_ret = state->end + 1;
1718 } else {
1719 *start_ret = 0;
1720 }
Nikolay Borisov45bfcfc2019-03-27 14:24:17 +02001721 break;
1722 }
1723 }
1724
1725 /*
1726 * Find the longest stretch from start until an entry which has the
1727 * bits set
1728 */
1729 while (1) {
1730 state = rb_entry(node, struct extent_state, rb_node);
1731 if (state->end >= start && !(state->state & bits)) {
1732 *end_ret = state->end;
1733 } else {
1734 *end_ret = state->start - 1;
1735 break;
1736 }
1737
1738 node = rb_next(node);
1739 if (!node)
1740 break;
1741 }
1742out:
1743 spin_unlock(&tree->lock);
1744}
1745
Xiao Guangrong69261c42011-07-14 03:19:45 +00001746/*
Chris Masond352ac62008-09-29 15:18:18 -04001747 * find a contiguous range of bytes in the file marked as delalloc, not
1748 * more than 'max_bytes'. start and end are used to return the range,
1749 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001750 * true is returned if we find something, false if nothing was in the tree
Chris Masond352ac62008-09-29 15:18:18 -04001751 */
Josef Bacik083e75e2019-09-23 10:05:20 -04001752bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
1753 u64 *end, u64 max_bytes,
1754 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001755{
1756 struct rb_node *node;
1757 struct extent_state *state;
1758 u64 cur_start = *start;
Lu Fengqi3522e902018-11-29 11:33:38 +08001759 bool found = false;
Chris Masond1310b22008-01-24 16:13:08 -05001760 u64 total_bytes = 0;
1761
Chris Masoncad321a2008-12-17 14:51:42 -05001762 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001763
Chris Masond1310b22008-01-24 16:13:08 -05001764 /*
1765 * this search will find all the extents that end after
1766 * our range starts.
1767 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001768 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001769 if (!node) {
Lu Fengqi3522e902018-11-29 11:33:38 +08001770 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001771 goto out;
1772 }
1773
Chris Masond3977122009-01-05 21:25:51 -05001774 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001775 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001776 if (found && (state->start != cur_start ||
1777 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001778 goto out;
1779 }
1780 if (!(state->state & EXTENT_DELALLOC)) {
1781 if (!found)
1782 *end = state->end;
1783 goto out;
1784 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001785 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001786 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001787 *cached_state = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +02001788 refcount_inc(&state->refs);
Josef Bacikc2a128d2010-02-02 21:19:11 +00001789 }
Lu Fengqi3522e902018-11-29 11:33:38 +08001790 found = true;
Chris Masond1310b22008-01-24 16:13:08 -05001791 *end = state->end;
1792 cur_start = state->end + 1;
1793 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001794 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001795 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001796 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001797 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001798 break;
1799 }
1800out:
Chris Masoncad321a2008-12-17 14:51:42 -05001801 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001802 return found;
1803}
1804
Liu Boda2c7002017-02-10 16:41:05 +01001805static int __process_pages_contig(struct address_space *mapping,
1806 struct page *locked_page,
1807 pgoff_t start_index, pgoff_t end_index,
1808 unsigned long page_ops, pgoff_t *index_ret);
1809
Jeff Mahoney143bede2012-03-01 14:56:26 +01001810static noinline void __unlock_for_delalloc(struct inode *inode,
1811 struct page *locked_page,
1812 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001813{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001814 unsigned long index = start >> PAGE_SHIFT;
1815 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001816
Liu Bo76c00212017-02-10 16:42:14 +01001817 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001818 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001819 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001820
Liu Bo76c00212017-02-10 16:42:14 +01001821 __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1822 PAGE_UNLOCK, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -04001823}
1824
1825static noinline int lock_delalloc_pages(struct inode *inode,
1826 struct page *locked_page,
1827 u64 delalloc_start,
1828 u64 delalloc_end)
1829{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001830 unsigned long index = delalloc_start >> PAGE_SHIFT;
Liu Bo76c00212017-02-10 16:42:14 +01001831 unsigned long index_ret = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001832 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001833 int ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001834
Liu Bo76c00212017-02-10 16:42:14 +01001835 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001836 if (index == locked_page->index && index == end_index)
1837 return 0;
1838
Liu Bo76c00212017-02-10 16:42:14 +01001839 ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1840 end_index, PAGE_LOCK, &index_ret);
1841 if (ret == -EAGAIN)
1842 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1843 (u64)index_ret << PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001844 return ret;
1845}
1846
1847/*
Lu Fengqi3522e902018-11-29 11:33:38 +08001848 * Find and lock a contiguous range of bytes in the file marked as delalloc, no
1849 * more than @max_bytes. @Start and @end are used to return the range,
Chris Masonc8b97812008-10-29 14:49:59 -04001850 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001851 * Return: true if we find something
1852 * false if nothing was in the tree
Chris Masonc8b97812008-10-29 14:49:59 -04001853 */
Johannes Thumshirnce9f9672018-11-19 10:38:17 +01001854EXPORT_FOR_TESTS
Lu Fengqi3522e902018-11-29 11:33:38 +08001855noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
Josef Bacik294e30f2013-10-09 12:00:56 -04001856 struct page *locked_page, u64 *start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03001857 u64 *end)
Chris Masonc8b97812008-10-29 14:49:59 -04001858{
Goldwyn Rodrigues99780592019-06-21 10:02:54 -05001859 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Nikolay Borisov917aace2018-10-26 14:43:20 +03001860 u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001861 u64 delalloc_start;
1862 u64 delalloc_end;
Lu Fengqi3522e902018-11-29 11:33:38 +08001863 bool found;
Chris Mason9655d292009-09-02 15:22:30 -04001864 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001865 int ret;
1866 int loops = 0;
1867
1868again:
1869 /* step one, find a bunch of delalloc bytes starting at start */
1870 delalloc_start = *start;
1871 delalloc_end = 0;
Josef Bacik083e75e2019-09-23 10:05:20 -04001872 found = btrfs_find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1873 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001874 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001875 *start = delalloc_start;
1876 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001877 free_extent_state(cached_state);
Lu Fengqi3522e902018-11-29 11:33:38 +08001878 return false;
Chris Masonc8b97812008-10-29 14:49:59 -04001879 }
1880
1881 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001882 * start comes from the offset of locked_page. We have to lock
1883 * pages in order, so we can't process delalloc bytes before
1884 * locked_page
1885 */
Chris Masond3977122009-01-05 21:25:51 -05001886 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001887 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001888
1889 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001890 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001891 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001892 if (delalloc_end + 1 - delalloc_start > max_bytes)
1893 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001894
Chris Masonc8b97812008-10-29 14:49:59 -04001895 /* step two, lock all the pages after the page that has start */
1896 ret = lock_delalloc_pages(inode, locked_page,
1897 delalloc_start, delalloc_end);
Nikolay Borisov9bfd61d2018-10-26 14:43:21 +03001898 ASSERT(!ret || ret == -EAGAIN);
Chris Masonc8b97812008-10-29 14:49:59 -04001899 if (ret == -EAGAIN) {
1900 /* some of the pages are gone, lets avoid looping by
1901 * shortening the size of the delalloc range we're searching
1902 */
Chris Mason9655d292009-09-02 15:22:30 -04001903 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001904 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001905 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001906 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001907 loops = 1;
1908 goto again;
1909 } else {
Lu Fengqi3522e902018-11-29 11:33:38 +08001910 found = false;
Chris Masonc8b97812008-10-29 14:49:59 -04001911 goto out_failed;
1912 }
1913 }
Chris Masonc8b97812008-10-29 14:49:59 -04001914
1915 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001916 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001917
1918 /* then test to make sure it is all still delalloc */
1919 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001920 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001921 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001922 unlock_extent_cached(tree, delalloc_start, delalloc_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001923 &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001924 __unlock_for_delalloc(inode, locked_page,
1925 delalloc_start, delalloc_end);
1926 cond_resched();
1927 goto again;
1928 }
Chris Mason9655d292009-09-02 15:22:30 -04001929 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001930 *start = delalloc_start;
1931 *end = delalloc_end;
1932out_failed:
1933 return found;
1934}
1935
Liu Boda2c7002017-02-10 16:41:05 +01001936static int __process_pages_contig(struct address_space *mapping,
1937 struct page *locked_page,
1938 pgoff_t start_index, pgoff_t end_index,
1939 unsigned long page_ops, pgoff_t *index_ret)
Chris Masonc8b97812008-10-29 14:49:59 -04001940{
Liu Bo873695b2017-02-02 17:49:22 -08001941 unsigned long nr_pages = end_index - start_index + 1;
Qu Wenruo12e33602020-10-21 14:24:57 +08001942 unsigned long pages_processed = 0;
Liu Bo873695b2017-02-02 17:49:22 -08001943 pgoff_t index = start_index;
Chris Masonc8b97812008-10-29 14:49:59 -04001944 struct page *pages[16];
Liu Bo873695b2017-02-02 17:49:22 -08001945 unsigned ret;
Liu Boda2c7002017-02-10 16:41:05 +01001946 int err = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001947 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001948
Liu Boda2c7002017-02-10 16:41:05 +01001949 if (page_ops & PAGE_LOCK) {
1950 ASSERT(page_ops == PAGE_LOCK);
1951 ASSERT(index_ret && *index_ret == start_index);
1952 }
1953
Filipe Manana704de492014-10-06 22:14:22 +01001954 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
Liu Bo873695b2017-02-02 17:49:22 -08001955 mapping_set_error(mapping, -EIO);
Filipe Manana704de492014-10-06 22:14:22 +01001956
Chris Masond3977122009-01-05 21:25:51 -05001957 while (nr_pages > 0) {
Liu Bo873695b2017-02-02 17:49:22 -08001958 ret = find_get_pages_contig(mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001959 min_t(unsigned long,
1960 nr_pages, ARRAY_SIZE(pages)), pages);
Liu Boda2c7002017-02-10 16:41:05 +01001961 if (ret == 0) {
1962 /*
1963 * Only if we're going to lock these pages,
1964 * can we find nothing at @index.
1965 */
1966 ASSERT(page_ops & PAGE_LOCK);
Liu Bo49d4a332017-03-06 18:20:56 -08001967 err = -EAGAIN;
1968 goto out;
Liu Boda2c7002017-02-10 16:41:05 +01001969 }
Chris Mason8b62b722009-09-02 16:53:46 -04001970
Liu Boda2c7002017-02-10 16:41:05 +01001971 for (i = 0; i < ret; i++) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001972 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001973 SetPagePrivate2(pages[i]);
1974
Chris Mason1d53c9e2019-07-10 12:28:16 -07001975 if (locked_page && pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001976 put_page(pages[i]);
Qu Wenruo12e33602020-10-21 14:24:57 +08001977 pages_processed++;
Chris Masonc8b97812008-10-29 14:49:59 -04001978 continue;
1979 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001980 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001981 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001982 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001983 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001984 if (page_ops & PAGE_SET_ERROR)
1985 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001986 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001987 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001988 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001989 unlock_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001990 if (page_ops & PAGE_LOCK) {
1991 lock_page(pages[i]);
1992 if (!PageDirty(pages[i]) ||
1993 pages[i]->mapping != mapping) {
1994 unlock_page(pages[i]);
Robbie Ko5909ca12020-07-20 09:42:09 +08001995 for (; i < ret; i++)
1996 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001997 err = -EAGAIN;
1998 goto out;
1999 }
2000 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002001 put_page(pages[i]);
Qu Wenruo12e33602020-10-21 14:24:57 +08002002 pages_processed++;
Chris Masonc8b97812008-10-29 14:49:59 -04002003 }
2004 nr_pages -= ret;
2005 index += ret;
2006 cond_resched();
2007 }
Liu Boda2c7002017-02-10 16:41:05 +01002008out:
2009 if (err && index_ret)
Qu Wenruo12e33602020-10-21 14:24:57 +08002010 *index_ret = start_index + pages_processed - 1;
Liu Boda2c7002017-02-10 16:41:05 +01002011 return err;
Chris Masonc8b97812008-10-29 14:49:59 -04002012}
Chris Masonc8b97812008-10-29 14:49:59 -04002013
Nikolay Borisovad7ff172020-06-03 08:55:06 +03002014void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Nikolay Borisov74e91942019-07-17 16:18:16 +03002015 struct page *locked_page,
Qu Wenruof97e27e2020-11-13 20:51:40 +08002016 u32 clear_bits, unsigned long page_ops)
Liu Bo873695b2017-02-02 17:49:22 -08002017{
Nikolay Borisovad7ff172020-06-03 08:55:06 +03002018 clear_extent_bit(&inode->io_tree, start, end, clear_bits, 1, 0, NULL);
Liu Bo873695b2017-02-02 17:49:22 -08002019
Nikolay Borisovad7ff172020-06-03 08:55:06 +03002020 __process_pages_contig(inode->vfs_inode.i_mapping, locked_page,
Liu Bo873695b2017-02-02 17:49:22 -08002021 start >> PAGE_SHIFT, end >> PAGE_SHIFT,
Liu Boda2c7002017-02-10 16:41:05 +01002022 page_ops, NULL);
Liu Bo873695b2017-02-02 17:49:22 -08002023}
2024
Chris Masond352ac62008-09-29 15:18:18 -04002025/*
2026 * count the number of bytes in the tree that have a given bit(s)
2027 * set. This can be fairly slow, except for EXTENT_DIRTY which is
2028 * cached. The total number found is returned.
2029 */
Chris Masond1310b22008-01-24 16:13:08 -05002030u64 count_range_bits(struct extent_io_tree *tree,
2031 u64 *start, u64 search_end, u64 max_bytes,
Qu Wenruof97e27e2020-11-13 20:51:40 +08002032 u32 bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05002033{
2034 struct rb_node *node;
2035 struct extent_state *state;
2036 u64 cur_start = *start;
2037 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05002038 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002039 int found = 0;
2040
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302041 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05002042 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05002043
Chris Masoncad321a2008-12-17 14:51:42 -05002044 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002045 if (cur_start == 0 && bits == EXTENT_DIRTY) {
2046 total_bytes = tree->dirty_bytes;
2047 goto out;
2048 }
2049 /*
2050 * this search will find all the extents that end after
2051 * our range starts.
2052 */
Chris Mason80ea96b2008-02-01 14:51:59 -05002053 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05002054 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05002055 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05002056
Chris Masond3977122009-01-05 21:25:51 -05002057 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05002058 state = rb_entry(node, struct extent_state, rb_node);
2059 if (state->start > search_end)
2060 break;
Chris Masonec29ed52011-02-23 16:23:20 -05002061 if (contig && found && state->start > last + 1)
2062 break;
2063 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05002064 total_bytes += min(search_end, state->end) + 1 -
2065 max(cur_start, state->start);
2066 if (total_bytes >= max_bytes)
2067 break;
2068 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04002069 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05002070 found = 1;
2071 }
Chris Masonec29ed52011-02-23 16:23:20 -05002072 last = state->end;
2073 } else if (contig && found) {
2074 break;
Chris Masond1310b22008-01-24 16:13:08 -05002075 }
2076 node = rb_next(node);
2077 if (!node)
2078 break;
2079 }
2080out:
Chris Masoncad321a2008-12-17 14:51:42 -05002081 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002082 return total_bytes;
2083}
Christoph Hellwigb2950862008-12-02 09:54:17 -05002084
Chris Masond352ac62008-09-29 15:18:18 -04002085/*
2086 * set the private field for a given byte offset in the tree. If there isn't
2087 * an extent_state there already, this does nothing.
2088 */
Josef Bacikb3f167a2019-09-23 10:05:21 -04002089int set_state_failrec(struct extent_io_tree *tree, u64 start,
2090 struct io_failure_record *failrec)
Chris Masond1310b22008-01-24 16:13:08 -05002091{
2092 struct rb_node *node;
2093 struct extent_state *state;
2094 int ret = 0;
2095
Chris Masoncad321a2008-12-17 14:51:42 -05002096 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002097 /*
2098 * this search will find all the extents that end after
2099 * our range starts.
2100 */
Chris Mason80ea96b2008-02-01 14:51:59 -05002101 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04002102 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05002103 ret = -ENOENT;
2104 goto out;
2105 }
2106 state = rb_entry(node, struct extent_state, rb_node);
2107 if (state->start != start) {
2108 ret = -ENOENT;
2109 goto out;
2110 }
David Sterba47dc1962016-02-11 13:24:13 +01002111 state->failrec = failrec;
Chris Masond1310b22008-01-24 16:13:08 -05002112out:
Chris Masoncad321a2008-12-17 14:51:42 -05002113 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002114 return ret;
2115}
2116
Nikolay Borisov2279a272020-07-02 15:23:28 +03002117struct io_failure_record *get_state_failrec(struct extent_io_tree *tree, u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05002118{
2119 struct rb_node *node;
2120 struct extent_state *state;
Nikolay Borisov2279a272020-07-02 15:23:28 +03002121 struct io_failure_record *failrec;
Chris Masond1310b22008-01-24 16:13:08 -05002122
Chris Masoncad321a2008-12-17 14:51:42 -05002123 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002124 /*
2125 * this search will find all the extents that end after
2126 * our range starts.
2127 */
Chris Mason80ea96b2008-02-01 14:51:59 -05002128 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04002129 if (!node) {
Nikolay Borisov2279a272020-07-02 15:23:28 +03002130 failrec = ERR_PTR(-ENOENT);
Chris Masond1310b22008-01-24 16:13:08 -05002131 goto out;
2132 }
2133 state = rb_entry(node, struct extent_state, rb_node);
2134 if (state->start != start) {
Nikolay Borisov2279a272020-07-02 15:23:28 +03002135 failrec = ERR_PTR(-ENOENT);
Chris Masond1310b22008-01-24 16:13:08 -05002136 goto out;
2137 }
Nikolay Borisov2279a272020-07-02 15:23:28 +03002138
2139 failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05002140out:
Chris Masoncad321a2008-12-17 14:51:42 -05002141 spin_unlock(&tree->lock);
Nikolay Borisov2279a272020-07-02 15:23:28 +03002142 return failrec;
Chris Masond1310b22008-01-24 16:13:08 -05002143}
2144
2145/*
2146 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05002147 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05002148 * has the bits set. Otherwise, 1 is returned if any bit in the
2149 * range is found set.
2150 */
2151int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Qu Wenruof97e27e2020-11-13 20:51:40 +08002152 u32 bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05002153{
2154 struct extent_state *state = NULL;
2155 struct rb_node *node;
2156 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002157
Chris Masoncad321a2008-12-17 14:51:42 -05002158 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01002159 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04002160 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04002161 node = &cached->rb_node;
2162 else
2163 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05002164 while (node && start <= end) {
2165 state = rb_entry(node, struct extent_state, rb_node);
2166
2167 if (filled && state->start > start) {
2168 bitset = 0;
2169 break;
2170 }
2171
2172 if (state->start > end)
2173 break;
2174
2175 if (state->state & bits) {
2176 bitset = 1;
2177 if (!filled)
2178 break;
2179 } else if (filled) {
2180 bitset = 0;
2181 break;
2182 }
Chris Mason46562ce2009-09-23 20:23:16 -04002183
2184 if (state->end == (u64)-1)
2185 break;
2186
Chris Masond1310b22008-01-24 16:13:08 -05002187 start = state->end + 1;
2188 if (start > end)
2189 break;
2190 node = rb_next(node);
2191 if (!node) {
2192 if (filled)
2193 bitset = 0;
2194 break;
2195 }
2196 }
Chris Masoncad321a2008-12-17 14:51:42 -05002197 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002198 return bitset;
2199}
Chris Masond1310b22008-01-24 16:13:08 -05002200
2201/*
2202 * helper function to set a given page up to date if all the
2203 * extents in the tree for that page are up to date
2204 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002205static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05002206{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002207 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002208 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04002209 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05002210 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002211}
2212
Josef Bacik7870d082017-05-05 11:57:15 -04002213int free_io_failure(struct extent_io_tree *failure_tree,
2214 struct extent_io_tree *io_tree,
2215 struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002216{
2217 int ret;
2218 int err = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002219
David Sterba47dc1962016-02-11 13:24:13 +01002220 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002221 ret = clear_extent_bits(failure_tree, rec->start,
2222 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02002223 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002224 if (ret)
2225 err = ret;
2226
Josef Bacik7870d082017-05-05 11:57:15 -04002227 ret = clear_extent_bits(io_tree, rec->start,
David Woodhouse53b381b2013-01-29 18:40:14 -05002228 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02002229 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05002230 if (ret && !err)
2231 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002232
2233 kfree(rec);
2234 return err;
2235}
2236
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002237/*
2238 * this bypasses the standard btrfs submit functions deliberately, as
2239 * the standard behavior is to write all copies in a raid setup. here we only
2240 * want to write the one bad copy. so we do the mapping for ourselves and issue
2241 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002242 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002243 * actually prevents the read that triggered the error from finishing.
2244 * currently, there can be no more than two copies of every data bit. thus,
2245 * exactly one rewrite is required.
2246 */
Josef Bacik6ec656b2017-05-05 11:57:14 -04002247int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
2248 u64 length, u64 logical, struct page *page,
2249 unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002250{
2251 struct bio *bio;
2252 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002253 u64 map_length = 0;
2254 u64 sector;
2255 struct btrfs_bio *bbio = NULL;
2256 int ret;
2257
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002258 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002259 BUG_ON(!mirror_num);
2260
David Sterbac5e4c3d2017-06-12 17:29:41 +02002261 bio = btrfs_io_bio_alloc(1);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002262 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002263 map_length = length;
2264
Filipe Mananab5de8d02016-05-27 22:21:27 +01002265 /*
2266 * Avoid races with device replace and make sure our bbio has devices
2267 * associated to its stripes that don't go away while we are doing the
2268 * read repair operation.
2269 */
2270 btrfs_bio_counter_inc_blocked(fs_info);
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03002271 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
Liu Boc7253282017-03-29 10:53:58 -07002272 /*
2273 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
2274 * to update all raid stripes, but here we just want to correct
2275 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2276 * stripe's dev and sector.
2277 */
2278 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2279 &map_length, &bbio, 0);
2280 if (ret) {
2281 btrfs_bio_counter_dec(fs_info);
2282 bio_put(bio);
2283 return -EIO;
2284 }
2285 ASSERT(bbio->mirror_num == 1);
2286 } else {
2287 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2288 &map_length, &bbio, mirror_num);
2289 if (ret) {
2290 btrfs_bio_counter_dec(fs_info);
2291 bio_put(bio);
2292 return -EIO;
2293 }
2294 BUG_ON(mirror_num != bbio->mirror_num);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002295 }
Liu Boc7253282017-03-29 10:53:58 -07002296
2297 sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002298 bio->bi_iter.bi_sector = sector;
Liu Boc7253282017-03-29 10:53:58 -07002299 dev = bbio->stripes[bbio->mirror_num - 1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002300 btrfs_put_bbio(bbio);
Anand Jainebbede42017-12-04 12:54:52 +08002301 if (!dev || !dev->bdev ||
2302 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002303 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002304 bio_put(bio);
2305 return -EIO;
2306 }
Christoph Hellwig74d46992017-08-23 19:10:32 +02002307 bio_set_dev(bio, dev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002308 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Miao Xieffdd2012014-09-12 18:44:00 +08002309 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002310
Mike Christie4e49ea42016-06-05 14:31:41 -05002311 if (btrfsic_submit_bio_wait(bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002312 /* try to remap that extent elsewhere? */
Filipe Mananab5de8d02016-05-27 22:21:27 +01002313 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002314 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002315 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002316 return -EIO;
2317 }
2318
David Sterbab14af3b2015-10-08 10:43:10 +02002319 btrfs_info_rl_in_rcu(fs_info,
2320 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Josef Bacik6ec656b2017-05-05 11:57:14 -04002321 ino, start,
Miao Xie1203b682014-09-12 18:44:01 +08002322 rcu_str_deref(dev->name), sector);
Filipe Mananab5de8d02016-05-27 22:21:27 +01002323 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002324 bio_put(bio);
2325 return 0;
2326}
2327
David Sterba2b489662020-04-29 03:04:10 +02002328int btrfs_repair_eb_io_failure(const struct extent_buffer *eb, int mirror_num)
Josef Bacikea466792012-03-26 21:57:36 -04002329{
David Sterba20a1fbf92019-03-20 11:23:44 +01002330 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacikea466792012-03-26 21:57:36 -04002331 u64 start = eb->start;
David Sterbacc5e31a2018-03-01 18:20:27 +01002332 int i, num_pages = num_extent_pages(eb);
Chris Masond95603b2012-04-12 15:55:15 -04002333 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002334
David Howellsbc98a422017-07-17 08:45:34 +01002335 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002336 return -EROFS;
2337
Josef Bacikea466792012-03-26 21:57:36 -04002338 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002339 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002340
Josef Bacik6ec656b2017-05-05 11:57:14 -04002341 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002342 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002343 if (ret)
2344 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002345 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002346 }
2347
2348 return ret;
2349}
2350
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002351/*
2352 * each time an IO finishes, we do a fast check in the IO failure tree
2353 * to see if we need to process or clean up an io_failure_record
2354 */
Josef Bacik7870d082017-05-05 11:57:15 -04002355int clean_io_failure(struct btrfs_fs_info *fs_info,
2356 struct extent_io_tree *failure_tree,
2357 struct extent_io_tree *io_tree, u64 start,
2358 struct page *page, u64 ino, unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002359{
2360 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002361 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002362 struct extent_state *state;
2363 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002364 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002365
2366 private = 0;
Josef Bacik7870d082017-05-05 11:57:15 -04002367 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2368 EXTENT_DIRTY, 0);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002369 if (!ret)
2370 return 0;
2371
Nikolay Borisov2279a272020-07-02 15:23:28 +03002372 failrec = get_state_failrec(failure_tree, start);
2373 if (IS_ERR(failrec))
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002374 return 0;
2375
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002376 BUG_ON(!failrec->this_mirror);
2377
2378 if (failrec->in_validation) {
2379 /* there was no real error, just free the record */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002380 btrfs_debug(fs_info,
2381 "clean_io_failure: freeing dummy error at %llu",
2382 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002383 goto out;
2384 }
David Howellsbc98a422017-07-17 08:45:34 +01002385 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002386 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002387
Josef Bacik7870d082017-05-05 11:57:15 -04002388 spin_lock(&io_tree->lock);
2389 state = find_first_extent_bit_state(io_tree,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002390 failrec->start,
2391 EXTENT_LOCKED);
Josef Bacik7870d082017-05-05 11:57:15 -04002392 spin_unlock(&io_tree->lock);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002393
Miao Xie883d0de2013-07-25 19:22:35 +08002394 if (state && state->start <= failrec->start &&
2395 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002396 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2397 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002398 if (num_copies > 1) {
Josef Bacik7870d082017-05-05 11:57:15 -04002399 repair_io_failure(fs_info, ino, start, failrec->len,
2400 failrec->logical, page, pg_offset,
2401 failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002402 }
2403 }
2404
2405out:
Josef Bacik7870d082017-05-05 11:57:15 -04002406 free_io_failure(failure_tree, io_tree, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002407
Miao Xie454ff3d2014-09-12 18:43:58 +08002408 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002409}
2410
Miao Xief6124962014-09-12 18:44:04 +08002411/*
2412 * Can be called when
2413 * - hold extent lock
2414 * - under ordered extent
2415 * - the inode is freeing
2416 */
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002417void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
Miao Xief6124962014-09-12 18:44:04 +08002418{
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002419 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
Miao Xief6124962014-09-12 18:44:04 +08002420 struct io_failure_record *failrec;
2421 struct extent_state *state, *next;
2422
2423 if (RB_EMPTY_ROOT(&failure_tree->state))
2424 return;
2425
2426 spin_lock(&failure_tree->lock);
2427 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2428 while (state) {
2429 if (state->start > end)
2430 break;
2431
2432 ASSERT(state->end <= end);
2433
2434 next = next_state(state);
2435
David Sterba47dc1962016-02-11 13:24:13 +01002436 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002437 free_extent_state(state);
2438 kfree(failrec);
2439
2440 state = next;
2441 }
2442 spin_unlock(&failure_tree->lock);
2443}
2444
Nikolay Borisov35263022020-07-02 15:23:29 +03002445static struct io_failure_record *btrfs_get_io_failure_record(struct inode *inode,
2446 u64 start, u64 end)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002447{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002448 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002449 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002450 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002451 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2452 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2453 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002454 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002455 u64 logical;
2456
Nikolay Borisov2279a272020-07-02 15:23:28 +03002457 failrec = get_state_failrec(failure_tree, start);
Nikolay Borisov35263022020-07-02 15:23:29 +03002458 if (!IS_ERR(failrec)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002459 btrfs_debug(fs_info,
2460 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2461 failrec->logical, failrec->start, failrec->len,
2462 failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002463 /*
2464 * when data can be on disk more than twice, add to failrec here
2465 * (e.g. with a list for failed_mirror) to make
2466 * clean_io_failure() clean all those errors at once.
2467 */
Nikolay Borisov35263022020-07-02 15:23:29 +03002468
2469 return failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002470 }
Miao Xie2fe63032014-09-12 18:43:59 +08002471
Nikolay Borisov35263022020-07-02 15:23:29 +03002472 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2473 if (!failrec)
2474 return ERR_PTR(-ENOMEM);
Miao Xie2fe63032014-09-12 18:43:59 +08002475
Nikolay Borisov35263022020-07-02 15:23:29 +03002476 failrec->start = start;
2477 failrec->len = end - start + 1;
2478 failrec->this_mirror = 0;
2479 failrec->bio_flags = 0;
2480 failrec->in_validation = 0;
2481
2482 read_lock(&em_tree->lock);
2483 em = lookup_extent_mapping(em_tree, start, failrec->len);
2484 if (!em) {
2485 read_unlock(&em_tree->lock);
2486 kfree(failrec);
2487 return ERR_PTR(-EIO);
2488 }
2489
2490 if (em->start > start || em->start + em->len <= start) {
2491 free_extent_map(em);
2492 em = NULL;
2493 }
2494 read_unlock(&em_tree->lock);
2495 if (!em) {
2496 kfree(failrec);
2497 return ERR_PTR(-EIO);
2498 }
2499
2500 logical = start - em->start;
2501 logical = em->block_start + logical;
2502 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2503 logical = em->block_start;
2504 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2505 extent_set_compress_type(&failrec->bio_flags, em->compress_type);
2506 }
2507
2508 btrfs_debug(fs_info,
2509 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2510 logical, start, failrec->len);
2511
2512 failrec->logical = logical;
2513 free_extent_map(em);
2514
2515 /* Set the bits in the private failure tree */
2516 ret = set_extent_bits(failure_tree, start, end,
2517 EXTENT_LOCKED | EXTENT_DIRTY);
2518 if (ret >= 0) {
2519 ret = set_state_failrec(failure_tree, start, failrec);
2520 /* Set the bits in the inode's tree */
2521 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
2522 } else if (ret < 0) {
2523 kfree(failrec);
2524 return ERR_PTR(ret);
2525 }
2526
2527 return failrec;
Miao Xie2fe63032014-09-12 18:43:59 +08002528}
2529
Omar Sandovalce06d3e2020-04-16 14:46:18 -07002530static bool btrfs_check_repairable(struct inode *inode, bool needs_validation,
2531 struct io_failure_record *failrec,
2532 int failed_mirror)
Miao Xie2fe63032014-09-12 18:43:59 +08002533{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002534 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002535 int num_copies;
2536
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002537 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002538 if (num_copies == 1) {
2539 /*
2540 * we only have a single copy of the data, so don't bother with
2541 * all the retry and error correction code that follows. no
2542 * matter what the error is, it is very likely to persist.
2543 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002544 btrfs_debug(fs_info,
2545 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2546 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002547 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002548 }
2549
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002550 /*
2551 * there are two premises:
2552 * a) deliver good data to the caller
2553 * b) correct the bad sectors on disk
2554 */
Omar Sandovalc7333972020-04-16 14:46:14 -07002555 if (needs_validation) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002556 /*
2557 * to fulfill b), we need to know the exact failing sectors, as
2558 * we don't want to rewrite any more than the failed ones. thus,
2559 * we need separate read requests for the failed bio
2560 *
2561 * if the following BUG_ON triggers, our validation request got
2562 * merged. we need separate requests for our algorithm to work.
2563 */
2564 BUG_ON(failrec->in_validation);
2565 failrec->in_validation = 1;
2566 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002567 } else {
2568 /*
2569 * we're ready to fulfill a) and b) alongside. get a good copy
2570 * of the failed sector and if we succeed, we have setup
2571 * everything for repair_io_failure to do the rest for us.
2572 */
2573 if (failrec->in_validation) {
2574 BUG_ON(failrec->this_mirror != failed_mirror);
2575 failrec->in_validation = 0;
2576 failrec->this_mirror = 0;
2577 }
2578 failrec->failed_mirror = failed_mirror;
2579 failrec->this_mirror++;
2580 if (failrec->this_mirror == failed_mirror)
2581 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002582 }
2583
Miao Xiefacc8a222013-07-25 19:22:34 +08002584 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002585 btrfs_debug(fs_info,
2586 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2587 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002588 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002589 }
2590
Liu Boc3cfb652017-07-13 15:00:50 -07002591 return true;
Miao Xie2fe63032014-09-12 18:43:59 +08002592}
2593
Omar Sandovalc7333972020-04-16 14:46:14 -07002594static bool btrfs_io_needs_validation(struct inode *inode, struct bio *bio)
Miao Xie2fe63032014-09-12 18:43:59 +08002595{
Omar Sandovalc7333972020-04-16 14:46:14 -07002596 u64 len = 0;
Omar Sandoval77d5d682020-04-16 14:46:25 -07002597 const u32 blocksize = inode->i_sb->s_blocksize;
Miao Xie2fe63032014-09-12 18:43:59 +08002598
Omar Sandovalc7333972020-04-16 14:46:14 -07002599 /*
Omar Sandovalf337bd72020-04-16 14:46:15 -07002600 * If bi_status is BLK_STS_OK, then this was a checksum error, not an
2601 * I/O error. In this case, we already know exactly which sector was
2602 * bad, so we don't need to validate.
2603 */
2604 if (bio->bi_status == BLK_STS_OK)
2605 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002606
Omar Sandovalf337bd72020-04-16 14:46:15 -07002607 /*
Omar Sandovalc7333972020-04-16 14:46:14 -07002608 * We need to validate each sector individually if the failed I/O was
2609 * for multiple sectors.
Omar Sandoval77d5d682020-04-16 14:46:25 -07002610 *
2611 * There are a few possible bios that can end up here:
2612 * 1. A buffered read bio, which is not cloned.
2613 * 2. A direct I/O read bio, which is cloned.
2614 * 3. A (buffered or direct) repair bio, which is not cloned.
2615 *
2616 * For cloned bios (case 2), we can get the size from
2617 * btrfs_io_bio->iter; for non-cloned bios (cases 1 and 3), we can get
2618 * it from the bvecs.
Omar Sandovalc7333972020-04-16 14:46:14 -07002619 */
Omar Sandoval77d5d682020-04-16 14:46:25 -07002620 if (bio_flagged(bio, BIO_CLONED)) {
2621 if (btrfs_io_bio(bio)->iter.bi_size > blocksize)
Omar Sandovalc7333972020-04-16 14:46:14 -07002622 return true;
Omar Sandoval77d5d682020-04-16 14:46:25 -07002623 } else {
2624 struct bio_vec *bvec;
2625 int i;
Miao Xiefacc8a222013-07-25 19:22:34 +08002626
Omar Sandoval77d5d682020-04-16 14:46:25 -07002627 bio_for_each_bvec_all(bvec, bio, i) {
2628 len += bvec->bv_len;
2629 if (len > blocksize)
2630 return true;
2631 }
Miao Xiefacc8a222013-07-25 19:22:34 +08002632 }
Omar Sandovalc7333972020-04-16 14:46:14 -07002633 return false;
Miao Xie2fe63032014-09-12 18:43:59 +08002634}
2635
Omar Sandoval77d5d682020-04-16 14:46:25 -07002636blk_status_t btrfs_submit_read_repair(struct inode *inode,
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002637 struct bio *failed_bio, u32 bio_offset,
Omar Sandoval77d5d682020-04-16 14:46:25 -07002638 struct page *page, unsigned int pgoff,
2639 u64 start, u64 end, int failed_mirror,
2640 submit_bio_hook_t *submit_bio_hook)
Miao Xie2fe63032014-09-12 18:43:59 +08002641{
2642 struct io_failure_record *failrec;
Omar Sandoval77d5d682020-04-16 14:46:25 -07002643 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002644 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002645 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandoval77d5d682020-04-16 14:46:25 -07002646 struct btrfs_io_bio *failed_io_bio = btrfs_io_bio(failed_bio);
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002647 const int icsum = bio_offset >> fs_info->sectorsize_bits;
Omar Sandovalc7333972020-04-16 14:46:14 -07002648 bool need_validation;
Omar Sandoval77d5d682020-04-16 14:46:25 -07002649 struct bio *repair_bio;
2650 struct btrfs_io_bio *repair_io_bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002651 blk_status_t status;
Miao Xie2fe63032014-09-12 18:43:59 +08002652
Omar Sandoval77d5d682020-04-16 14:46:25 -07002653 btrfs_debug(fs_info,
2654 "repair read error: read error at %llu", start);
Miao Xie2fe63032014-09-12 18:43:59 +08002655
Mike Christie1f7ad752016-06-05 14:31:51 -05002656 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002657
Nikolay Borisov35263022020-07-02 15:23:29 +03002658 failrec = btrfs_get_io_failure_record(inode, start, end);
2659 if (IS_ERR(failrec))
2660 return errno_to_blk_status(PTR_ERR(failrec));
Miao Xie2fe63032014-09-12 18:43:59 +08002661
Omar Sandovalc7333972020-04-16 14:46:14 -07002662 need_validation = btrfs_io_needs_validation(inode, failed_bio);
2663
2664 if (!btrfs_check_repairable(inode, need_validation, failrec,
Liu Boc3cfb652017-07-13 15:00:50 -07002665 failed_mirror)) {
Josef Bacik7870d082017-05-05 11:57:15 -04002666 free_io_failure(failure_tree, tree, failrec);
Omar Sandoval77d5d682020-04-16 14:46:25 -07002667 return BLK_STS_IOERR;
Miao Xie2fe63032014-09-12 18:43:59 +08002668 }
2669
Omar Sandoval77d5d682020-04-16 14:46:25 -07002670 repair_bio = btrfs_io_bio_alloc(1);
2671 repair_io_bio = btrfs_io_bio(repair_bio);
2672 repair_bio->bi_opf = REQ_OP_READ;
Omar Sandovalc7333972020-04-16 14:46:14 -07002673 if (need_validation)
Omar Sandoval77d5d682020-04-16 14:46:25 -07002674 repair_bio->bi_opf |= REQ_FAILFAST_DEV;
2675 repair_bio->bi_end_io = failed_bio->bi_end_io;
2676 repair_bio->bi_iter.bi_sector = failrec->logical >> 9;
2677 repair_bio->bi_private = failed_bio->bi_private;
Miao Xie2fe63032014-09-12 18:43:59 +08002678
Omar Sandoval77d5d682020-04-16 14:46:25 -07002679 if (failed_io_bio->csum) {
David Sterba223486c2020-07-02 11:27:30 +02002680 const u32 csum_size = fs_info->csum_size;
Omar Sandoval77d5d682020-04-16 14:46:25 -07002681
2682 repair_io_bio->csum = repair_io_bio->csum_inline;
2683 memcpy(repair_io_bio->csum,
2684 failed_io_bio->csum + csum_size * icsum, csum_size);
2685 }
2686
2687 bio_add_page(repair_bio, page, failrec->len, pgoff);
2688 repair_io_bio->logical = failrec->start;
2689 repair_io_bio->iter = repair_bio->bi_iter;
Miao Xie2fe63032014-09-12 18:43:59 +08002690
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002691 btrfs_debug(btrfs_sb(inode->i_sb),
Omar Sandoval77d5d682020-04-16 14:46:25 -07002692"repair read error: submitting new read to mirror %d, in_validation=%d",
2693 failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002694
Omar Sandoval77d5d682020-04-16 14:46:25 -07002695 status = submit_bio_hook(inode, repair_bio, failrec->this_mirror,
2696 failrec->bio_flags);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002697 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04002698 free_io_failure(failure_tree, tree, failrec);
Omar Sandoval77d5d682020-04-16 14:46:25 -07002699 bio_put(repair_bio);
Miao Xie6c387ab2014-09-12 18:43:57 +08002700 }
Omar Sandoval77d5d682020-04-16 14:46:25 -07002701 return status;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002702}
2703
Chris Masond1310b22008-01-24 16:13:08 -05002704/* lots and lots of room for performance fixes in the end_bio funcs */
2705
David Sterbab5227c02015-12-03 13:08:59 +01002706void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002707{
2708 int uptodate = (err == 0);
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002709 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002710
Nikolay Borisovc6297322018-11-08 10:18:08 +02002711 btrfs_writepage_endio_finish_ordered(page, start, end, uptodate);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002712
Jeff Mahoney87826df2012-02-15 16:23:57 +01002713 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002714 ClearPageUptodate(page);
2715 SetPageError(page);
Colin Ian Kingbff5baf2017-05-09 18:14:01 +01002716 ret = err < 0 ? err : -EIO;
Liu Bo5dca6ee2014-05-12 12:47:36 +08002717 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002718 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002719}
2720
Chris Masond1310b22008-01-24 16:13:08 -05002721/*
2722 * after a writepage IO is done, we need to:
2723 * clear the uptodate bits on error
2724 * clear the writeback bits in the extent tree for this IO
2725 * end_page_writeback if the page has no more pending IO
2726 *
2727 * Scheduling is not allowed, so the extent state tree is expected
2728 * to have one and only one object corresponding to this IO.
2729 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002730static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002731{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002732 int error = blk_status_to_errno(bio->bi_status);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002733 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002734 u64 start;
2735 u64 end;
Ming Lei6dc4f102019-02-15 19:13:19 +08002736 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002737
David Sterbac09abff2017-07-13 18:10:07 +02002738 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02002739 bio_for_each_segment_all(bvec, bio, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002740 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002741 struct inode *inode = page->mapping->host;
2742 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002743
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002744 /* We always issue full-page reads, but if some block
2745 * in a page fails to read, blk_update_request() will
2746 * advance bv_offset and adjust bv_len to compensate.
2747 * Print a warning for nonzero offsets, and an error
2748 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002749 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2750 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002751 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002752 "partial page write in btrfs with offset %u and length %u",
2753 bvec->bv_offset, bvec->bv_len);
2754 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002755 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002756 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002757 bvec->bv_offset, bvec->bv_len);
2758 }
Chris Masond1310b22008-01-24 16:13:08 -05002759
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002760 start = page_offset(page);
2761 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002762
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002763 end_extent_writepage(page, error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002764 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002765 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002766
Chris Masond1310b22008-01-24 16:13:08 -05002767 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002768}
2769
Qu Wenruo94e8c952020-11-13 20:51:28 +08002770/*
2771 * Record previously processed extent range
2772 *
2773 * For endio_readpage_release_extent() to handle a full extent range, reducing
2774 * the extent io operations.
2775 */
2776struct processed_extent {
2777 struct btrfs_inode *inode;
2778 /* Start of the range in @inode */
2779 u64 start;
2780 /* End of the range in in @inode */
2781 u64 end;
2782 bool uptodate;
2783};
2784
2785/*
2786 * Try to release processed extent range
2787 *
2788 * May not release the extent range right now if the current range is
2789 * contiguous to processed extent.
2790 *
2791 * Will release processed extent when any of @inode, @uptodate, the range is
2792 * no longer contiguous to the processed range.
2793 *
2794 * Passing @inode == NULL will force processed extent to be released.
2795 */
2796static void endio_readpage_release_extent(struct processed_extent *processed,
2797 struct btrfs_inode *inode, u64 start, u64 end,
2798 bool uptodate)
Miao Xie883d0de2013-07-25 19:22:35 +08002799{
2800 struct extent_state *cached = NULL;
Qu Wenruo94e8c952020-11-13 20:51:28 +08002801 struct extent_io_tree *tree;
Miao Xie883d0de2013-07-25 19:22:35 +08002802
Qu Wenruo94e8c952020-11-13 20:51:28 +08002803 /* The first extent, initialize @processed */
2804 if (!processed->inode)
2805 goto update;
2806
2807 /*
2808 * Contiguous to processed extent, just uptodate the end.
2809 *
2810 * Several things to notice:
2811 *
2812 * - bio can be merged as long as on-disk bytenr is contiguous
2813 * This means we can have page belonging to other inodes, thus need to
2814 * check if the inode still matches.
2815 * - bvec can contain range beyond current page for multi-page bvec
2816 * Thus we need to do processed->end + 1 >= start check
2817 */
2818 if (processed->inode == inode && processed->uptodate == uptodate &&
2819 processed->end + 1 >= start && end >= processed->end) {
2820 processed->end = end;
2821 return;
2822 }
2823
2824 tree = &processed->inode->io_tree;
2825 /*
2826 * Now we don't have range contiguous to the processed range, release
2827 * the processed range now.
2828 */
2829 if (processed->uptodate && tree->track_uptodate)
2830 set_extent_uptodate(tree, processed->start, processed->end,
2831 &cached, GFP_ATOMIC);
2832 unlock_extent_cached_atomic(tree, processed->start, processed->end,
2833 &cached);
2834
2835update:
2836 /* Update processed to current range */
2837 processed->inode = inode;
2838 processed->start = start;
2839 processed->end = end;
2840 processed->uptodate = uptodate;
Miao Xie883d0de2013-07-25 19:22:35 +08002841}
2842
Qu Wenruoe09caaf2020-11-13 20:51:29 +08002843static void endio_readpage_update_page_status(struct page *page, bool uptodate)
2844{
2845 if (uptodate) {
2846 SetPageUptodate(page);
2847 } else {
2848 ClearPageUptodate(page);
2849 SetPageError(page);
2850 }
2851 unlock_page(page);
2852}
2853
Chris Masond1310b22008-01-24 16:13:08 -05002854/*
2855 * after a readpage IO is done, we need to:
2856 * clear the uptodate bits on error
2857 * set the uptodate bits if things worked
2858 * set the page up to date if all extents in the tree are uptodate
2859 * clear the lock bit in the extent tree
2860 * unlock the page if there are no other extents locked for it
2861 *
2862 * Scheduling is not allowed, so the extent state tree is expected
2863 * to have one and only one object corresponding to this IO.
2864 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002865static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002866{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002867 struct bio_vec *bvec;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002868 int uptodate = !bio->bi_status;
Miao Xiefacc8a222013-07-25 19:22:34 +08002869 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04002870 struct extent_io_tree *tree, *failure_tree;
Qu Wenruo94e8c952020-11-13 20:51:28 +08002871 struct processed_extent processed = { 0 };
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002872 /*
2873 * The offset to the beginning of a bio, since one bio can never be
2874 * larger than UINT_MAX, u32 here is enough.
2875 */
2876 u32 bio_offset = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002877 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002878 int ret;
Ming Lei6dc4f102019-02-15 19:13:19 +08002879 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002880
David Sterbac09abff2017-07-13 18:10:07 +02002881 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02002882 bio_for_each_segment_all(bvec, bio, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002883 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002884 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002885 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002886 const u32 sectorsize = fs_info->sectorsize;
2887 u64 start;
2888 u64 end;
2889 u32 len;
Arne Jansen507903b2011-04-06 10:02:20 +00002890
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002891 btrfs_debug(fs_info,
2892 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
David Sterba1201b582020-11-26 15:41:27 +01002893 bio->bi_iter.bi_sector, bio->bi_status,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002894 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002895 tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002896 failure_tree = &BTRFS_I(inode)->io_failure_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002897
Qu Wenruo8b8bbd42020-10-21 14:24:58 +08002898 /*
2899 * We always issue full-sector reads, but if some block in a
2900 * page fails to read, blk_update_request() will advance
2901 * bv_offset and adjust bv_len to compensate. Print a warning
2902 * for unaligned offsets, and an error if they don't add up to
2903 * a full sector.
2904 */
2905 if (!IS_ALIGNED(bvec->bv_offset, sectorsize))
2906 btrfs_err(fs_info,
2907 "partial page read in btrfs with offset %u and length %u",
2908 bvec->bv_offset, bvec->bv_len);
2909 else if (!IS_ALIGNED(bvec->bv_offset + bvec->bv_len,
2910 sectorsize))
2911 btrfs_info(fs_info,
2912 "incomplete page read with offset %u and length %u",
2913 bvec->bv_offset, bvec->bv_len);
Chris Masond1310b22008-01-24 16:13:08 -05002914
Qu Wenruo8b8bbd42020-10-21 14:24:58 +08002915 start = page_offset(page) + bvec->bv_offset;
2916 end = start + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002917 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002918
Chris Mason9be33952013-05-17 18:30:14 -04002919 mirror = io_bio->mirror_num;
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002920 if (likely(uptodate)) {
Nikolay Borisovbe17b3a2020-09-18 16:34:36 +03002921 if (is_data_inode(inode))
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002922 ret = btrfs_verify_data_csum(io_bio,
2923 bio_offset, page, start, end,
2924 mirror);
Nikolay Borisov9a446d62020-09-18 16:34:33 +03002925 else
2926 ret = btrfs_validate_metadata_buffer(io_bio,
Qu Wenruo8e1dc982020-11-12 16:47:57 +08002927 page, start, end, mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002928 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002929 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002930 else
Josef Bacik7870d082017-05-05 11:57:15 -04002931 clean_io_failure(BTRFS_I(inode)->root->fs_info,
2932 failure_tree, tree, start,
2933 page,
2934 btrfs_ino(BTRFS_I(inode)), 0);
Chris Masond1310b22008-01-24 16:13:08 -05002935 }
Josef Bacikea466792012-03-26 21:57:36 -04002936
Miao Xief2a09da2013-07-25 19:22:33 +08002937 if (likely(uptodate))
2938 goto readpage_ok;
2939
Nikolay Borisovbe17b3a2020-09-18 16:34:36 +03002940 if (is_data_inode(inode)) {
Liu Bo9d0d1c82017-03-24 15:04:50 -07002941
2942 /*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002943 * The generic bio_readpage_error handles errors the
2944 * following way: If possible, new read requests are
2945 * created and submitted and will end up in
2946 * end_bio_extent_readpage as well (if we're lucky,
2947 * not in the !uptodate case). In that case it returns
2948 * 0 and we just go on with the next page in our bio.
2949 * If it can't handle the error it will return -EIO and
2950 * we remain responsible for that page.
Liu Bo9d0d1c82017-03-24 15:04:50 -07002951 */
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002952 if (!btrfs_submit_read_repair(inode, bio, bio_offset,
2953 page,
Omar Sandoval77d5d682020-04-16 14:46:25 -07002954 start - page_offset(page),
2955 start, end, mirror,
Nikolay Borisov908930f2020-09-18 16:34:37 +03002956 btrfs_submit_data_bio)) {
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002957 uptodate = !bio->bi_status;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002958 ASSERT(bio_offset + len > bio_offset);
2959 bio_offset += len;
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002960 continue;
2961 }
2962 } else {
2963 struct extent_buffer *eb;
2964
2965 eb = (struct extent_buffer *)page->private;
2966 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
2967 eb->read_mirror = mirror;
2968 atomic_dec(&eb->io_pages);
2969 if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD,
2970 &eb->bflags))
2971 btree_readahead_hook(eb, -EIO);
Chris Mason7e383262008-04-09 16:28:12 -04002972 }
Miao Xief2a09da2013-07-25 19:22:33 +08002973readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002974 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002975 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002976 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002977 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002978
2979 /* Zero out the end if this page straddles i_size */
Johannes Thumshirn70730172018-12-05 15:23:03 +01002980 off = offset_in_page(i_size);
Liu Boa583c022014-08-19 23:32:22 +08002981 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002982 zero_user_segment(page, off, PAGE_SIZE);
Chris Mason70dec802008-01-29 09:59:12 -05002983 }
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002984 ASSERT(bio_offset + len > bio_offset);
2985 bio_offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002986
Qu Wenruoe09caaf2020-11-13 20:51:29 +08002987 /* Update page status and unlock */
2988 endio_readpage_update_page_status(page, uptodate);
Qu Wenruo94e8c952020-11-13 20:51:28 +08002989 endio_readpage_release_extent(&processed, BTRFS_I(inode),
2990 start, end, uptodate);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002991 }
Qu Wenruo94e8c952020-11-13 20:51:28 +08002992 /* Release the last extent */
2993 endio_readpage_release_extent(&processed, NULL, 0, 0, false);
David Sterbab3a0dd52018-11-22 17:16:49 +01002994 btrfs_io_bio_free_csum(io_bio);
Chris Masond1310b22008-01-24 16:13:08 -05002995 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002996}
2997
Chris Mason9be33952013-05-17 18:30:14 -04002998/*
David Sterba184f9992017-06-12 17:29:39 +02002999 * Initialize the members up to but not including 'bio'. Use after allocating a
3000 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
3001 * 'bio' because use of __GFP_ZERO is not supported.
Chris Mason9be33952013-05-17 18:30:14 -04003002 */
David Sterba184f9992017-06-12 17:29:39 +02003003static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
Chris Masond1310b22008-01-24 16:13:08 -05003004{
David Sterba184f9992017-06-12 17:29:39 +02003005 memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
3006}
3007
3008/*
David Sterba6e707bc2017-06-02 17:26:26 +02003009 * The following helpers allocate a bio. As it's backed by a bioset, it'll
3010 * never fail. We're returning a bio right now but you can call btrfs_io_bio
3011 * for the appropriate container_of magic
Chris Masond1310b22008-01-24 16:13:08 -05003012 */
David Sterbae749af442019-06-18 20:00:16 +02003013struct bio *btrfs_bio_alloc(u64 first_byte)
Chris Masond1310b22008-01-24 16:13:08 -05003014{
3015 struct bio *bio;
3016
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04003017 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset);
David Sterbac821e7f32017-06-02 18:35:36 +02003018 bio->bi_iter.bi_sector = first_byte >> 9;
David Sterba184f9992017-06-12 17:29:39 +02003019 btrfs_io_bio_init(btrfs_io_bio(bio));
Chris Masond1310b22008-01-24 16:13:08 -05003020 return bio;
3021}
3022
David Sterba8b6c1d52017-06-02 17:48:13 +02003023struct bio *btrfs_bio_clone(struct bio *bio)
Chris Mason9be33952013-05-17 18:30:14 -04003024{
Miao Xie23ea8e52014-09-12 18:43:54 +08003025 struct btrfs_io_bio *btrfs_bio;
3026 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04003027
David Sterba6e707bc2017-06-02 17:26:26 +02003028 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04003029 new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset);
David Sterba6e707bc2017-06-02 17:26:26 +02003030 btrfs_bio = btrfs_io_bio(new);
David Sterba184f9992017-06-12 17:29:39 +02003031 btrfs_io_bio_init(btrfs_bio);
David Sterba6e707bc2017-06-02 17:26:26 +02003032 btrfs_bio->iter = bio->bi_iter;
Miao Xie23ea8e52014-09-12 18:43:54 +08003033 return new;
3034}
Chris Mason9be33952013-05-17 18:30:14 -04003035
David Sterbac5e4c3d2017-06-12 17:29:41 +02003036struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
Chris Mason9be33952013-05-17 18:30:14 -04003037{
Miao Xiefacc8a222013-07-25 19:22:34 +08003038 struct bio *bio;
3039
David Sterba6e707bc2017-06-02 17:26:26 +02003040 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04003041 bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
David Sterba184f9992017-06-12 17:29:39 +02003042 btrfs_io_bio_init(btrfs_io_bio(bio));
Miao Xiefacc8a222013-07-25 19:22:34 +08003043 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04003044}
3045
Liu Boe4770942017-05-16 10:57:14 -07003046struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
Liu Bo2f8e9142017-05-15 17:43:31 -07003047{
3048 struct bio *bio;
3049 struct btrfs_io_bio *btrfs_bio;
3050
3051 /* this will never fail when it's backed by a bioset */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04003052 bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset);
Liu Bo2f8e9142017-05-15 17:43:31 -07003053 ASSERT(bio);
3054
3055 btrfs_bio = btrfs_io_bio(bio);
David Sterba184f9992017-06-12 17:29:39 +02003056 btrfs_io_bio_init(btrfs_bio);
Liu Bo2f8e9142017-05-15 17:43:31 -07003057
3058 bio_trim(bio, offset >> 9, size >> 9);
Liu Bo17347ce2017-05-15 15:33:27 -07003059 btrfs_bio->iter = bio->bi_iter;
Liu Bo2f8e9142017-05-15 17:43:31 -07003060 return bio;
3061}
Chris Mason9be33952013-05-17 18:30:14 -04003062
David Sterba4b81ba42017-06-06 19:14:26 +02003063/*
3064 * @opf: bio REQ_OP_* and REQ_* flags as one value
David Sterbab8b3d622017-06-12 19:50:41 +02003065 * @wbc: optional writeback control for io accounting
3066 * @page: page to add to the bio
3067 * @pg_offset: offset of the new bio or to check whether we are adding
3068 * a contiguous page to the previous one
3069 * @size: portion of page that we want to write
3070 * @offset: starting offset in the page
David Sterba5c2b1fd2017-06-06 19:22:55 +02003071 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
David Sterbab8b3d622017-06-12 19:50:41 +02003072 * @end_io_func: end_io callback for new bio
3073 * @mirror_num: desired mirror to read/write
3074 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
3075 * @bio_flags: flags of the current bio to see if we can merge them
David Sterba4b81ba42017-06-06 19:14:26 +02003076 */
David Sterba0ceb34b2020-02-05 19:09:28 +01003077static int submit_extent_page(unsigned int opf,
Chris Masonda2f0f72015-07-02 13:57:22 -07003078 struct writeback_control *wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003079 struct page *page, u64 offset,
David Sterba6c5a4e22017-10-04 17:10:34 +02003080 size_t size, unsigned long pg_offset,
Chris Masond1310b22008-01-24 16:13:08 -05003081 struct bio **bio_ret,
Chris Masonf1885912008-04-09 16:28:12 -04003082 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04003083 int mirror_num,
3084 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003085 unsigned long bio_flags,
3086 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05003087{
3088 int ret = 0;
3089 struct bio *bio;
Qu Wenruoe940e9a2020-10-21 14:25:01 +08003090 size_t io_size = min_t(size_t, size, PAGE_SIZE);
David Sterba6273b7f2017-10-04 17:30:11 +02003091 sector_t sector = offset >> 9;
David Sterba0ceb34b2020-02-05 19:09:28 +01003092 struct extent_io_tree *tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masond1310b22008-01-24 16:13:08 -05003093
David Sterba5c2b1fd2017-06-06 19:22:55 +02003094 ASSERT(bio_ret);
3095
3096 if (*bio_ret) {
David Sterba0c8508a2017-06-12 20:00:43 +02003097 bool contig;
3098 bool can_merge = true;
3099
Chris Masond1310b22008-01-24 16:13:08 -05003100 bio = *bio_ret;
David Sterba0c8508a2017-06-12 20:00:43 +02003101 if (prev_bio_flags & EXTENT_BIO_COMPRESSED)
Kent Overstreet4f024f32013-10-11 15:44:27 -07003102 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04003103 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07003104 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04003105
Qu Wenruoe940e9a2020-10-21 14:25:01 +08003106 if (btrfs_bio_fits_in_stripe(page, io_size, bio, bio_flags))
David Sterba0c8508a2017-06-12 20:00:43 +02003107 can_merge = false;
3108
3109 if (prev_bio_flags != bio_flags || !contig || !can_merge ||
Filipe Manana005efed2015-09-14 09:09:31 +01003110 force_bio_submit ||
Qu Wenruoe940e9a2020-10-21 14:25:01 +08003111 bio_add_page(bio, page, io_size, pg_offset) < io_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05003112 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09003113 if (ret < 0) {
3114 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003115 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09003116 }
Chris Masond1310b22008-01-24 16:13:08 -05003117 bio = NULL;
3118 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07003119 if (wbc)
Qu Wenruoe940e9a2020-10-21 14:25:01 +08003120 wbc_account_cgroup_owner(wbc, page, io_size);
Chris Masond1310b22008-01-24 16:13:08 -05003121 return 0;
3122 }
3123 }
Chris Masonc8b97812008-10-29 14:49:59 -04003124
David Sterbae749af442019-06-18 20:00:16 +02003125 bio = btrfs_bio_alloc(offset);
Qu Wenruoe940e9a2020-10-21 14:25:01 +08003126 bio_add_page(bio, page, io_size, pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05003127 bio->bi_end_io = end_io_func;
3128 bio->bi_private = tree;
Jens Axboee6959b92017-06-27 11:51:28 -06003129 bio->bi_write_hint = page->mapping->host->i_write_hint;
David Sterba4b81ba42017-06-06 19:14:26 +02003130 bio->bi_opf = opf;
Chris Masonda2f0f72015-07-02 13:57:22 -07003131 if (wbc) {
David Sterba429aebc2019-11-18 23:27:55 +01003132 struct block_device *bdev;
3133
3134 bdev = BTRFS_I(page->mapping->host)->root->fs_info->fs_devices->latest_bdev;
3135 bio_set_dev(bio, bdev);
Chris Masonda2f0f72015-07-02 13:57:22 -07003136 wbc_init_bio(wbc, bio);
Qu Wenruoe940e9a2020-10-21 14:25:01 +08003137 wbc_account_cgroup_owner(wbc, page, io_size);
Chris Masonda2f0f72015-07-02 13:57:22 -07003138 }
Chris Mason70dec802008-01-29 09:59:12 -05003139
David Sterba5c2b1fd2017-06-06 19:22:55 +02003140 *bio_ret = bio;
Chris Masond1310b22008-01-24 16:13:08 -05003141
3142 return ret;
3143}
3144
Eric Sandeen48a3b632013-04-25 20:41:01 +00003145static void attach_extent_buffer_page(struct extent_buffer *eb,
3146 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003147{
Qu Wenruo0d01e242020-10-21 14:25:02 +08003148 /*
3149 * If the page is mapped to btree inode, we should hold the private
3150 * lock to prevent race.
3151 * For cloned or dummy extent buffers, their pages are not mapped and
3152 * will not race with any other ebs.
3153 */
3154 if (page->mapping)
3155 lockdep_assert_held(&page->mapping->private_lock);
3156
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07003157 if (!PagePrivate(page))
3158 attach_page_private(page, eb);
3159 else
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003160 WARN_ON(page->private != (unsigned long)eb);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003161}
3162
Chris Masond1310b22008-01-24 16:13:08 -05003163void set_page_extent_mapped(struct page *page)
3164{
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07003165 if (!PagePrivate(page))
3166 attach_page_private(page, (void *)EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05003167}
3168
Miao Xie125bac012013-07-25 19:22:37 +08003169static struct extent_map *
3170__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
Nikolay Borisov1a5ee1e2020-09-14 12:37:06 +03003171 u64 start, u64 len, struct extent_map **em_cached)
Miao Xie125bac012013-07-25 19:22:37 +08003172{
3173 struct extent_map *em;
3174
3175 if (em_cached && *em_cached) {
3176 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00003177 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08003178 start < extent_map_end(em)) {
Elena Reshetova490b54d2017-03-03 10:55:12 +02003179 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08003180 return em;
3181 }
3182
3183 free_extent_map(em);
3184 *em_cached = NULL;
3185 }
3186
Nikolay Borisov1a5ee1e2020-09-14 12:37:06 +03003187 em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, start, len);
Miao Xie125bac012013-07-25 19:22:37 +08003188 if (em_cached && !IS_ERR_OR_NULL(em)) {
3189 BUG_ON(*em_cached);
Elena Reshetova490b54d2017-03-03 10:55:12 +02003190 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08003191 *em_cached = em;
3192 }
3193 return em;
3194}
Chris Masond1310b22008-01-24 16:13:08 -05003195/*
3196 * basic readpage implementation. Locked extent state structs are inserted
3197 * into the tree that are removed when the IO is done (by the end_io
3198 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003199 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07003200 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05003201 */
Nikolay Borisov0f208812020-09-14 14:39:16 +03003202int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,
3203 struct bio **bio, unsigned long *bio_flags,
3204 unsigned int read_flags, u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05003205{
3206 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003207 u64 start = page_offset(page);
David Sterba8eec8292017-06-06 19:50:13 +02003208 const u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003209 u64 cur = start;
3210 u64 extent_offset;
3211 u64 last_byte = i_size_read(inode);
3212 u64 block_start;
3213 u64 cur_end;
Chris Masond1310b22008-01-24 16:13:08 -05003214 struct extent_map *em;
Liu Bobaf863b2016-07-11 10:39:07 -07003215 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003216 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02003217 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003218 size_t iosize;
3219 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00003220 unsigned long this_bio_flag = 0;
David Sterbaf657a312020-02-05 19:09:42 +01003221 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
David Sterbaae6957e2020-02-05 19:09:30 +01003222
Chris Masond1310b22008-01-24 16:13:08 -05003223 set_page_extent_mapped(page);
3224
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003225 if (!PageUptodate(page)) {
3226 if (cleancache_get_page(page) == 0) {
3227 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003228 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003229 goto out;
3230 }
3231 }
3232
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003233 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04003234 char *userpage;
Johannes Thumshirn70730172018-12-05 15:23:03 +01003235 size_t zero_offset = offset_in_page(last_byte);
Chris Masonc8b97812008-10-29 14:49:59 -04003236
3237 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003238 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08003239 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04003240 memset(userpage + zero_offset, 0, iosize);
3241 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003242 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04003243 }
3244 }
Chris Masond1310b22008-01-24 16:13:08 -05003245 while (cur <= end) {
Filipe Manana005efed2015-09-14 09:09:31 +01003246 bool force_bio_submit = false;
David Sterba6273b7f2017-10-04 17:30:11 +02003247 u64 offset;
Josef Bacikc8f2f242013-02-11 11:33:00 -05003248
Chris Masond1310b22008-01-24 16:13:08 -05003249 if (cur >= last_byte) {
3250 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003251 struct extent_state *cached = NULL;
3252
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003253 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08003254 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003255 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003256 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003257 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003258 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003259 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003260 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01003261 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003262 break;
3263 }
Miao Xie125bac012013-07-25 19:22:37 +08003264 em = __get_extent_map(inode, page, pg_offset, cur,
Nikolay Borisov1a5ee1e2020-09-14 12:37:06 +03003265 end - cur + 1, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02003266 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003267 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003268 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003269 break;
3270 }
Chris Masond1310b22008-01-24 16:13:08 -05003271 extent_offset = cur - em->start;
3272 BUG_ON(extent_map_end(em) <= cur);
3273 BUG_ON(end < cur);
3274
Li Zefan261507a02010-12-17 14:21:50 +08003275 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07003276 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08003277 extent_set_compress_type(&this_bio_flag,
3278 em->compress_type);
3279 }
Chris Masonc8b97812008-10-29 14:49:59 -04003280
Chris Masond1310b22008-01-24 16:13:08 -05003281 iosize = min(extent_map_end(em) - cur, end - cur + 1);
3282 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00003283 iosize = ALIGN(iosize, blocksize);
Goldwyn Rodrigues949b3272020-09-15 10:41:40 -05003284 if (this_bio_flag & EXTENT_BIO_COMPRESSED)
David Sterba6273b7f2017-10-04 17:30:11 +02003285 offset = em->block_start;
Goldwyn Rodrigues949b3272020-09-15 10:41:40 -05003286 else
David Sterba6273b7f2017-10-04 17:30:11 +02003287 offset = em->block_start + extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003288 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04003289 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
3290 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01003291
3292 /*
3293 * If we have a file range that points to a compressed extent
Randy Dunlap260db432020-08-04 19:48:34 -07003294 * and it's followed by a consecutive file range that points
Filipe Manana005efed2015-09-14 09:09:31 +01003295 * to the same compressed extent (possibly with a different
3296 * offset and/or length, so it either points to the whole extent
3297 * or only part of it), we must make sure we do not submit a
3298 * single bio to populate the pages for the 2 ranges because
3299 * this makes the compressed extent read zero out the pages
3300 * belonging to the 2nd range. Imagine the following scenario:
3301 *
3302 * File layout
3303 * [0 - 8K] [8K - 24K]
3304 * | |
3305 * | |
3306 * points to extent X, points to extent X,
3307 * offset 4K, length of 8K offset 0, length 16K
3308 *
3309 * [extent X, compressed length = 4K uncompressed length = 16K]
3310 *
3311 * If the bio to read the compressed extent covers both ranges,
3312 * it will decompress extent X into the pages belonging to the
3313 * first range and then it will stop, zeroing out the remaining
3314 * pages that belong to the other range that points to extent X.
3315 * So here we make sure we submit 2 bios, one for the first
3316 * range and another one for the third range. Both will target
3317 * the same physical extent from disk, but we can't currently
3318 * make the compressed bio endio callback populate the pages
3319 * for both ranges because each compressed bio is tightly
3320 * coupled with a single extent map, and each range can have
3321 * an extent map with a different offset value relative to the
3322 * uncompressed data of our extent and different lengths. This
3323 * is a corner case so we prioritize correctness over
3324 * non-optimal behavior (submitting 2 bios for the same extent).
3325 */
3326 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3327 prev_em_start && *prev_em_start != (u64)-1 &&
Filipe Manana8e928212019-02-14 15:17:20 +00003328 *prev_em_start != em->start)
Filipe Manana005efed2015-09-14 09:09:31 +01003329 force_bio_submit = true;
3330
3331 if (prev_em_start)
Filipe Manana8e928212019-02-14 15:17:20 +00003332 *prev_em_start = em->start;
Filipe Manana005efed2015-09-14 09:09:31 +01003333
Chris Masond1310b22008-01-24 16:13:08 -05003334 free_extent_map(em);
3335 em = NULL;
3336
3337 /* we've found a hole, just zero and go on */
3338 if (block_start == EXTENT_MAP_HOLE) {
3339 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003340 struct extent_state *cached = NULL;
3341
Cong Wang7ac687d2011-11-25 23:14:28 +08003342 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003343 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003344 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003345 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003346
3347 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003348 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003349 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01003350 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003351 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003352 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003353 continue;
3354 }
3355 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003356 if (test_range_bit(tree, cur, cur_end,
3357 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003358 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003359 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003360 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003361 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003362 continue;
3363 }
Chris Mason70dec802008-01-29 09:59:12 -05003364 /* we have an inline extent but it didn't get marked up
3365 * to date. Error out
3366 */
3367 if (block_start == EXTENT_MAP_INLINE) {
3368 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003369 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003370 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003371 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003372 continue;
3373 }
Chris Masond1310b22008-01-24 16:13:08 -05003374
David Sterba0ceb34b2020-02-05 19:09:28 +01003375 ret = submit_extent_page(REQ_OP_READ | read_flags, NULL,
Goldwyn Rodrigues949b3272020-09-15 10:41:40 -05003376 page, offset, iosize,
David Sterbafa17ed02019-10-03 17:29:05 +02003377 pg_offset, bio,
Nikolay Borisovfd513002020-09-14 12:37:11 +03003378 end_bio_extent_readpage, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04003379 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003380 this_bio_flag,
3381 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003382 if (!ret) {
3383 nr++;
3384 *bio_flags = this_bio_flag;
3385 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003386 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003387 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003388 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003389 }
Chris Masond1310b22008-01-24 16:13:08 -05003390 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003391 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003392 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003393out:
Chris Masond1310b22008-01-24 16:13:08 -05003394 if (!nr) {
3395 if (!PageError(page))
3396 SetPageUptodate(page);
3397 unlock_page(page);
3398 }
Liu Bobaf863b2016-07-11 10:39:07 -07003399 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003400}
3401
David Sterbab6660e82020-02-05 19:09:40 +01003402static inline void contiguous_readpages(struct page *pages[], int nr_pages,
Miao Xie99740902013-07-25 19:22:36 +08003403 u64 start, u64 end,
Miao Xie125bac012013-07-25 19:22:37 +08003404 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003405 struct bio **bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003406 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003407 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003408{
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03003409 struct btrfs_inode *inode = BTRFS_I(pages[0]->mapping->host);
Miao Xie99740902013-07-25 19:22:36 +08003410 int index;
3411
David Sterbab272ae22020-02-05 19:09:33 +01003412 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003413
3414 for (index = 0; index < nr_pages; index++) {
Nikolay Borisov0f208812020-09-14 14:39:16 +03003415 btrfs_do_readpage(pages[index], em_cached, bio, bio_flags,
3416 REQ_RAHEAD, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003417 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003418 }
3419}
3420
David Sterba3d4b9492017-02-10 19:33:41 +01003421static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003422 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003423{
3424 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003425}
3426
Chris Masond1310b22008-01-24 16:13:08 -05003427/*
Chris Mason40f76582014-05-21 13:35:51 -07003428 * helper for __extent_writepage, doing all of the delayed allocation setup.
3429 *
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003430 * This returns 1 if btrfs_run_delalloc_range function did all the work required
Chris Mason40f76582014-05-21 13:35:51 -07003431 * to write the page (copy into inline extent). In this case the IO has
3432 * been started and the page is already unlocked.
3433 *
3434 * This returns 0 if all went well (page still locked)
3435 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003436 */
Nikolay Borisovcd4c0bf942020-06-05 10:42:10 +03003437static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode,
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003438 struct page *page, struct writeback_control *wbc,
3439 u64 delalloc_start, unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003440{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003441 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Lu Fengqi3522e902018-11-29 11:33:38 +08003442 bool found;
Chris Mason40f76582014-05-21 13:35:51 -07003443 u64 delalloc_to_write = 0;
3444 u64 delalloc_end = 0;
3445 int ret;
3446 int page_started = 0;
3447
Chris Mason40f76582014-05-21 13:35:51 -07003448
3449 while (delalloc_end < page_end) {
Nikolay Borisovcd4c0bf942020-06-05 10:42:10 +03003450 found = find_lock_delalloc_range(&inode->vfs_inode, page,
Chris Mason40f76582014-05-21 13:35:51 -07003451 &delalloc_start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03003452 &delalloc_end);
Lu Fengqi3522e902018-11-29 11:33:38 +08003453 if (!found) {
Chris Mason40f76582014-05-21 13:35:51 -07003454 delalloc_start = delalloc_end + 1;
3455 continue;
3456 }
Nikolay Borisovcd4c0bf942020-06-05 10:42:10 +03003457 ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003458 delalloc_end, &page_started, nr_written, wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003459 if (ret) {
3460 SetPageError(page);
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003461 /*
3462 * btrfs_run_delalloc_range should return < 0 for error
3463 * but just in case, we use > 0 here meaning the IO is
3464 * started, so we don't want to return > 0 unless
3465 * things are going well.
Chris Mason40f76582014-05-21 13:35:51 -07003466 */
Nikolay Borisovb69d1ee2020-07-16 18:17:19 +03003467 return ret < 0 ? ret : -EIO;
Chris Mason40f76582014-05-21 13:35:51 -07003468 }
3469 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003470 * delalloc_end is already one less than the total length, so
3471 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003472 */
3473 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003474 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003475 delalloc_start = delalloc_end + 1;
3476 }
3477 if (wbc->nr_to_write < delalloc_to_write) {
3478 int thresh = 8192;
3479
3480 if (delalloc_to_write < thresh * 2)
3481 thresh = delalloc_to_write;
3482 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3483 thresh);
3484 }
3485
3486 /* did the fill delalloc function already unlock and start
3487 * the IO?
3488 */
3489 if (page_started) {
3490 /*
3491 * we've unlocked the page, so we can't update
3492 * the mapping's writeback index, just update
3493 * nr_to_write.
3494 */
3495 wbc->nr_to_write -= *nr_written;
3496 return 1;
3497 }
3498
Nikolay Borisovb69d1ee2020-07-16 18:17:19 +03003499 return 0;
Chris Mason40f76582014-05-21 13:35:51 -07003500}
3501
3502/*
3503 * helper for __extent_writepage. This calls the writepage start hooks,
3504 * and does the loop to map the page into extents and bios.
3505 *
3506 * We return 1 if the IO is started and the page is unlocked,
3507 * 0 if all went well (page still locked)
3508 * < 0 if there were errors (page still locked)
3509 */
Nikolay Borisovd4580fe2020-06-03 08:55:33 +03003510static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode,
Chris Mason40f76582014-05-21 13:35:51 -07003511 struct page *page,
3512 struct writeback_control *wbc,
3513 struct extent_page_data *epd,
3514 loff_t i_size,
3515 unsigned long nr_written,
David Sterba57e5ffe2019-10-29 18:28:55 +01003516 int *nr_ret)
Chris Mason40f76582014-05-21 13:35:51 -07003517{
Nikolay Borisovd4580fe2020-06-03 08:55:33 +03003518 struct extent_io_tree *tree = &inode->io_tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003519 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003520 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003521 u64 end;
3522 u64 cur = start;
3523 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003524 u64 block_start;
3525 u64 iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003526 struct extent_map *em;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003527 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003528 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003529 int ret = 0;
3530 int nr = 0;
David Sterba57e5ffe2019-10-29 18:28:55 +01003531 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003532 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003533
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003534 ret = btrfs_writepage_cow_fixup(page, start, page_end);
3535 if (ret) {
3536 /* Fixup worker will requeue */
Josef Bacik5ab58052020-01-21 11:51:43 -05003537 redirty_page_for_writepage(wbc, page);
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003538 update_nr_written(wbc, nr_written);
3539 unlock_page(page);
3540 return 1;
Chris Mason247e7432008-07-17 12:53:51 -04003541 }
3542
Chris Mason11c83492009-04-20 15:50:09 -04003543 /*
3544 * we don't want to touch the inode after unlocking the page,
3545 * so we update the mapping writeback index now
3546 */
David Sterba3d4b9492017-02-10 19:33:41 +01003547 update_nr_written(wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003548
Chris Masond1310b22008-01-24 16:13:08 -05003549 end = page_end;
Nikolay Borisovd4580fe2020-06-03 08:55:33 +03003550 blocksize = inode->vfs_inode.i_sb->s_blocksize;
Chris Masond1310b22008-01-24 16:13:08 -05003551
3552 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003553 u64 em_end;
David Sterba6273b7f2017-10-04 17:30:11 +02003554 u64 offset;
David Sterba58409ed2016-05-04 11:46:10 +02003555
Chris Mason40f76582014-05-21 13:35:51 -07003556 if (cur >= i_size) {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003557 btrfs_writepage_endio_finish_ordered(page, cur,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003558 page_end, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003559 break;
3560 }
Nikolay Borisovd4580fe2020-06-03 08:55:33 +03003561 em = btrfs_get_extent(inode, NULL, 0, cur, end - cur + 1);
David Sterbac7040052011-04-19 18:00:01 +02003562 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003563 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003564 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003565 break;
3566 }
3567
3568 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003569 em_end = extent_map_end(em);
3570 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003571 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003572 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003573 iosize = ALIGN(iosize, blocksize);
David Sterba6273b7f2017-10-04 17:30:11 +02003574 offset = em->block_start + extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003575 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003576 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003577 free_extent_map(em);
3578 em = NULL;
3579
Chris Masonc8b97812008-10-29 14:49:59 -04003580 /*
3581 * compressed and inline extents are written through other
3582 * paths in the FS
3583 */
3584 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003585 block_start == EXTENT_MAP_INLINE) {
Omar Sandovalc8b04032019-12-02 17:34:24 -08003586 if (compressed)
Chris Masonc8b97812008-10-29 14:49:59 -04003587 nr++;
Omar Sandovalc8b04032019-12-02 17:34:24 -08003588 else
3589 btrfs_writepage_endio_finish_ordered(page, cur,
3590 cur + iosize - 1, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003591 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003592 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003593 continue;
3594 }
Chris Masonc8b97812008-10-29 14:49:59 -04003595
David Sterba5cdc84b2018-07-18 20:32:52 +02003596 btrfs_set_range_writeback(tree, cur, cur + iosize - 1);
David Sterba58409ed2016-05-04 11:46:10 +02003597 if (!PageWriteback(page)) {
Nikolay Borisovd4580fe2020-06-03 08:55:33 +03003598 btrfs_err(inode->root->fs_info,
David Sterba58409ed2016-05-04 11:46:10 +02003599 "page %lu not writeback, cur %llu end %llu",
3600 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003601 }
David Sterba58409ed2016-05-04 11:46:10 +02003602
David Sterba0ceb34b2020-02-05 19:09:28 +01003603 ret = submit_extent_page(REQ_OP_WRITE | write_flags, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003604 page, offset, iosize, pg_offset,
David Sterbafa17ed02019-10-03 17:29:05 +02003605 &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003606 end_bio_extent_writepage,
3607 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003608 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003609 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003610 if (PageWriteback(page))
3611 end_page_writeback(page);
3612 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003613
Chris Masond1310b22008-01-24 16:13:08 -05003614 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003615 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003616 nr++;
3617 }
Chris Mason40f76582014-05-21 13:35:51 -07003618 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003619 return ret;
3620}
3621
3622/*
3623 * the writepage semantics are similar to regular writepage. extent
3624 * records are inserted to lock ranges in the tree, and as dirty areas
3625 * are found, they are marked writeback. Then the lock bits are removed
3626 * and the end_io handler clears the writeback ranges
Qu Wenruo30659762019-03-20 14:27:42 +08003627 *
3628 * Return 0 if everything goes well.
3629 * Return <0 for error.
Chris Mason40f76582014-05-21 13:35:51 -07003630 */
3631static int __extent_writepage(struct page *page, struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003632 struct extent_page_data *epd)
Chris Mason40f76582014-05-21 13:35:51 -07003633{
3634 struct inode *inode = page->mapping->host;
Chris Mason40f76582014-05-21 13:35:51 -07003635 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003636 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003637 int ret;
3638 int nr = 0;
Omar Sandovaleb70d222019-12-02 17:34:20 -08003639 size_t pg_offset;
Chris Mason40f76582014-05-21 13:35:51 -07003640 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003641 unsigned long end_index = i_size >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003642 unsigned long nr_written = 0;
3643
Chris Mason40f76582014-05-21 13:35:51 -07003644 trace___extent_writepage(page, inode, wbc);
3645
3646 WARN_ON(!PageLocked(page));
3647
3648 ClearPageError(page);
3649
Johannes Thumshirn70730172018-12-05 15:23:03 +01003650 pg_offset = offset_in_page(i_size);
Chris Mason40f76582014-05-21 13:35:51 -07003651 if (page->index > end_index ||
3652 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003653 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003654 unlock_page(page);
3655 return 0;
3656 }
3657
3658 if (page->index == end_index) {
3659 char *userpage;
3660
3661 userpage = kmap_atomic(page);
3662 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003663 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003664 kunmap_atomic(userpage);
3665 flush_dcache_page(page);
3666 }
3667
Chris Mason40f76582014-05-21 13:35:51 -07003668 set_page_extent_mapped(page);
3669
Nikolay Borisov7789a552018-11-08 10:18:06 +02003670 if (!epd->extent_locked) {
Nikolay Borisovcd4c0bf942020-06-05 10:42:10 +03003671 ret = writepage_delalloc(BTRFS_I(inode), page, wbc, start,
3672 &nr_written);
Nikolay Borisov7789a552018-11-08 10:18:06 +02003673 if (ret == 1)
Omar Sandoval169d2c82019-12-02 17:34:21 -08003674 return 0;
Nikolay Borisov7789a552018-11-08 10:18:06 +02003675 if (ret)
3676 goto done;
3677 }
Chris Mason40f76582014-05-21 13:35:51 -07003678
Nikolay Borisovd4580fe2020-06-03 08:55:33 +03003679 ret = __extent_writepage_io(BTRFS_I(inode), page, wbc, epd, i_size,
3680 nr_written, &nr);
Chris Mason40f76582014-05-21 13:35:51 -07003681 if (ret == 1)
Omar Sandoval169d2c82019-12-02 17:34:21 -08003682 return 0;
Chris Mason40f76582014-05-21 13:35:51 -07003683
3684done:
Chris Masond1310b22008-01-24 16:13:08 -05003685 if (nr == 0) {
3686 /* make sure the mapping tag for page dirty gets cleared */
3687 set_page_writeback(page);
3688 end_page_writeback(page);
3689 }
Filipe Manana61391d52014-05-09 17:17:40 +01003690 if (PageError(page)) {
3691 ret = ret < 0 ? ret : -EIO;
3692 end_extent_writepage(page, ret, start, page_end);
3693 }
Chris Masond1310b22008-01-24 16:13:08 -05003694 unlock_page(page);
Qu Wenruo30659762019-03-20 14:27:42 +08003695 ASSERT(ret <= 0);
Chris Mason40f76582014-05-21 13:35:51 -07003696 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003697}
3698
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003699void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003700{
NeilBrown74316202014-07-07 15:16:04 +10003701 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3702 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003703}
3704
Filipe Manana18dfa712019-09-11 17:42:00 +01003705static void end_extent_buffer_writeback(struct extent_buffer *eb)
3706{
3707 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3708 smp_mb__after_atomic();
3709 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3710}
3711
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003712/*
Qu Wenruoa3efb2f2020-10-21 14:24:49 +08003713 * Lock extent buffer status and pages for writeback.
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003714 *
Qu Wenruoa3efb2f2020-10-21 14:24:49 +08003715 * May try to flush write bio if we can't get the lock.
3716 *
3717 * Return 0 if the extent buffer doesn't need to be submitted.
3718 * (E.g. the extent buffer is not dirty)
3719 * Return >0 is the extent buffer is submitted to bio.
3720 * Return <0 if something went wrong, no page is locked.
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003721 */
David Sterba9df76fb2019-03-20 11:21:41 +01003722static noinline_for_stack int lock_extent_buffer_for_io(struct extent_buffer *eb,
Chris Mason0e378df2014-05-19 20:55:27 -07003723 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003724{
David Sterba9df76fb2019-03-20 11:21:41 +01003725 struct btrfs_fs_info *fs_info = eb->fs_info;
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003726 int i, num_pages, failed_page_nr;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003727 int flush = 0;
3728 int ret = 0;
3729
3730 if (!btrfs_try_tree_write_lock(eb)) {
Qu Wenruof4340622019-03-20 14:27:41 +08003731 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003732 if (ret < 0)
3733 return ret;
3734 flush = 1;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003735 btrfs_tree_lock(eb);
3736 }
3737
3738 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3739 btrfs_tree_unlock(eb);
3740 if (!epd->sync_io)
3741 return 0;
3742 if (!flush) {
Qu Wenruof4340622019-03-20 14:27:41 +08003743 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003744 if (ret < 0)
3745 return ret;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003746 flush = 1;
3747 }
Chris Masona098d8e82012-03-21 12:09:56 -04003748 while (1) {
3749 wait_on_extent_buffer_writeback(eb);
3750 btrfs_tree_lock(eb);
3751 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3752 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003753 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003754 }
3755 }
3756
Josef Bacik51561ff2012-07-20 16:25:24 -04003757 /*
3758 * We need to do this to prevent races in people who check if the eb is
3759 * under IO since we can end up having no IO bits set for a short period
3760 * of time.
3761 */
3762 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003763 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3764 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003765 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003766 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Nikolay Borisov104b4e52017-06-20 21:01:20 +03003767 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3768 -eb->len,
3769 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003770 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003771 } else {
3772 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003773 }
3774
3775 btrfs_tree_unlock(eb);
3776
3777 if (!ret)
3778 return ret;
3779
David Sterba65ad0102018-06-29 10:56:49 +02003780 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003781 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003782 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003783
3784 if (!trylock_page(p)) {
3785 if (!flush) {
Filipe Manana18dfa712019-09-11 17:42:00 +01003786 int err;
3787
3788 err = flush_write_bio(epd);
3789 if (err < 0) {
3790 ret = err;
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003791 failed_page_nr = i;
3792 goto err_unlock;
3793 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003794 flush = 1;
3795 }
3796 lock_page(p);
3797 }
3798 }
3799
3800 return ret;
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003801err_unlock:
3802 /* Unlock already locked pages */
3803 for (i = 0; i < failed_page_nr; i++)
3804 unlock_page(eb->pages[i]);
Filipe Manana18dfa712019-09-11 17:42:00 +01003805 /*
3806 * Clear EXTENT_BUFFER_WRITEBACK and wake up anyone waiting on it.
3807 * Also set back EXTENT_BUFFER_DIRTY so future attempts to this eb can
3808 * be made and undo everything done before.
3809 */
3810 btrfs_tree_lock(eb);
3811 spin_lock(&eb->refs_lock);
3812 set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
3813 end_extent_buffer_writeback(eb);
3814 spin_unlock(&eb->refs_lock);
3815 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, eb->len,
3816 fs_info->dirty_metadata_batch);
3817 btrfs_clear_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3818 btrfs_tree_unlock(eb);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003819 return ret;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003820}
3821
Filipe Manana656f30d2014-09-26 12:25:56 +01003822static void set_btree_ioerr(struct page *page)
3823{
3824 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Dennis Zhoueb5b64f2019-09-13 14:54:07 +01003825 struct btrfs_fs_info *fs_info;
Filipe Manana656f30d2014-09-26 12:25:56 +01003826
3827 SetPageError(page);
3828 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3829 return;
3830
3831 /*
Dennis Zhoueb5b64f2019-09-13 14:54:07 +01003832 * If we error out, we should add back the dirty_metadata_bytes
3833 * to make it consistent.
3834 */
3835 fs_info = eb->fs_info;
3836 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3837 eb->len, fs_info->dirty_metadata_batch);
3838
3839 /*
Filipe Manana656f30d2014-09-26 12:25:56 +01003840 * If writeback for a btree extent that doesn't belong to a log tree
3841 * failed, increment the counter transaction->eb_write_errors.
3842 * We do this because while the transaction is running and before it's
3843 * committing (when we call filemap_fdata[write|wait]_range against
3844 * the btree inode), we might have
3845 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3846 * returns an error or an error happens during writeback, when we're
3847 * committing the transaction we wouldn't know about it, since the pages
3848 * can be no longer dirty nor marked anymore for writeback (if a
3849 * subsequent modification to the extent buffer didn't happen before the
3850 * transaction commit), which makes filemap_fdata[write|wait]_range not
3851 * able to find the pages tagged with SetPageError at transaction
3852 * commit time. So if this happens we must abort the transaction,
3853 * otherwise we commit a super block with btree roots that point to
3854 * btree nodes/leafs whose content on disk is invalid - either garbage
3855 * or the content of some node/leaf from a past generation that got
3856 * cowed or deleted and is no longer valid.
3857 *
3858 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3859 * not be enough - we need to distinguish between log tree extents vs
3860 * non-log tree extents, and the next filemap_fdatawait_range() call
3861 * will catch and clear such errors in the mapping - and that call might
3862 * be from a log sync and not from a transaction commit. Also, checking
3863 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3864 * not done and would not be reliable - the eb might have been released
3865 * from memory and reading it back again means that flag would not be
3866 * set (since it's a runtime flag, not persisted on disk).
3867 *
3868 * Using the flags below in the btree inode also makes us achieve the
3869 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3870 * writeback for all dirty pages and before filemap_fdatawait_range()
3871 * is called, the writeback for all dirty pages had already finished
3872 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3873 * filemap_fdatawait_range() would return success, as it could not know
3874 * that writeback errors happened (the pages were no longer tagged for
3875 * writeback).
3876 */
3877 switch (eb->log_index) {
3878 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003879 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003880 break;
3881 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003882 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003883 break;
3884 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003885 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003886 break;
3887 default:
3888 BUG(); /* unexpected, logic error */
3889 }
3890}
3891
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003892static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003893{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003894 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003895 struct extent_buffer *eb;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02003896 int done;
Ming Lei6dc4f102019-02-15 19:13:19 +08003897 struct bvec_iter_all iter_all;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003898
David Sterbac09abff2017-07-13 18:10:07 +02003899 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02003900 bio_for_each_segment_all(bvec, bio, iter_all) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003901 struct page *page = bvec->bv_page;
3902
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003903 eb = (struct extent_buffer *)page->private;
3904 BUG_ON(!eb);
3905 done = atomic_dec_and_test(&eb->io_pages);
3906
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003907 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003908 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003909 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003910 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003911 }
3912
3913 end_page_writeback(page);
3914
3915 if (!done)
3916 continue;
3917
3918 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003919 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003920
3921 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003922}
3923
Chris Mason0e378df2014-05-19 20:55:27 -07003924static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003925 struct writeback_control *wbc,
3926 struct extent_page_data *epd)
3927{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003928 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003929 u32 nritems;
David Sterbacc5e31a2018-03-01 18:20:27 +01003930 int i, num_pages;
Liu Bo851cd172016-09-23 13:44:44 -07003931 unsigned long start, end;
Liu Boff40adf2017-08-24 18:19:48 -06003932 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003933 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003934
Filipe Manana656f30d2014-09-26 12:25:56 +01003935 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02003936 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003937 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003938
Liu Bo851cd172016-09-23 13:44:44 -07003939 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3940 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003941 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003942 end = btrfs_node_key_ptr_offset(nritems);
3943
David Sterbab159fa22016-11-08 18:09:03 +01003944 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003945 } else {
3946 /*
3947 * leaf:
3948 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3949 */
3950 start = btrfs_item_nr_offset(nritems);
David Sterba8f881e82019-03-20 11:33:10 +01003951 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(eb);
David Sterbab159fa22016-11-08 18:09:03 +01003952 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003953 }
3954
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003955 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003956 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003957
3958 clear_page_dirty_for_io(p);
3959 set_page_writeback(p);
David Sterba0ceb34b2020-02-05 19:09:28 +01003960 ret = submit_extent_page(REQ_OP_WRITE | write_flags, wbc,
David Sterbafa17ed02019-10-03 17:29:05 +02003961 p, offset, PAGE_SIZE, 0,
David Sterbac2df8bb2017-02-10 19:29:38 +01003962 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003963 end_bio_extent_buffer_writepage,
Liu Bo18fdc672017-09-13 12:18:22 -06003964 0, 0, 0, false);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003965 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003966 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003967 if (PageWriteback(p))
3968 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003969 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3970 end_extent_buffer_writeback(eb);
3971 ret = -EIO;
3972 break;
3973 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003974 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003975 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003976 unlock_page(p);
3977 }
3978
3979 if (unlikely(ret)) {
3980 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003981 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003982 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003983 unlock_page(p);
3984 }
3985 }
3986
3987 return ret;
3988}
3989
Qu Wenruof91e0d02020-12-02 14:48:00 +08003990/*
3991 * Submit all page(s) of one extent buffer.
3992 *
3993 * @page: the page of one extent buffer
3994 * @eb_context: to determine if we need to submit this page, if current page
3995 * belongs to this eb, we don't need to submit
3996 *
3997 * The caller should pass each page in their bytenr order, and here we use
3998 * @eb_context to determine if we have submitted pages of one extent buffer.
3999 *
4000 * If we have, we just skip until we hit a new page that doesn't belong to
4001 * current @eb_context.
4002 *
4003 * If not, we submit all the page(s) of the extent buffer.
4004 *
4005 * Return >0 if we have submitted the extent buffer successfully.
4006 * Return 0 if we don't need to submit the page, as it's already submitted by
4007 * previous call.
4008 * Return <0 for fatal error.
4009 */
4010static int submit_eb_page(struct page *page, struct writeback_control *wbc,
4011 struct extent_page_data *epd,
4012 struct extent_buffer **eb_context)
4013{
4014 struct address_space *mapping = page->mapping;
4015 struct extent_buffer *eb;
4016 int ret;
4017
4018 if (!PagePrivate(page))
4019 return 0;
4020
4021 spin_lock(&mapping->private_lock);
4022 if (!PagePrivate(page)) {
4023 spin_unlock(&mapping->private_lock);
4024 return 0;
4025 }
4026
4027 eb = (struct extent_buffer *)page->private;
4028
4029 /*
4030 * Shouldn't happen and normally this would be a BUG_ON but no point
4031 * crashing the machine for something we can survive anyway.
4032 */
4033 if (WARN_ON(!eb)) {
4034 spin_unlock(&mapping->private_lock);
4035 return 0;
4036 }
4037
4038 if (eb == *eb_context) {
4039 spin_unlock(&mapping->private_lock);
4040 return 0;
4041 }
4042 ret = atomic_inc_not_zero(&eb->refs);
4043 spin_unlock(&mapping->private_lock);
4044 if (!ret)
4045 return 0;
4046
4047 *eb_context = eb;
4048
4049 ret = lock_extent_buffer_for_io(eb, epd);
4050 if (ret <= 0) {
4051 free_extent_buffer(eb);
4052 return ret;
4053 }
4054 ret = write_one_eb(eb, wbc, epd);
4055 free_extent_buffer(eb);
4056 if (ret < 0)
4057 return ret;
4058 return 1;
4059}
4060
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004061int btree_write_cache_pages(struct address_space *mapping,
4062 struct writeback_control *wbc)
4063{
Qu Wenruof91e0d02020-12-02 14:48:00 +08004064 struct extent_buffer *eb_context = NULL;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004065 struct extent_page_data epd = {
4066 .bio = NULL,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004067 .extent_locked = 0,
4068 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
4069 };
Qu Wenruob3ff8f12020-02-12 14:12:44 +08004070 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004071 int ret = 0;
4072 int done = 0;
4073 int nr_to_write_done = 0;
4074 struct pagevec pvec;
4075 int nr_pages;
4076 pgoff_t index;
4077 pgoff_t end; /* Inclusive */
4078 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05004079 xa_mark_t tag;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004080
Mel Gorman86679822017-11-15 17:37:52 -08004081 pagevec_init(&pvec);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004082 if (wbc->range_cyclic) {
4083 index = mapping->writeback_index; /* Start from prev offset */
4084 end = -1;
Josef Bacik556755a2020-01-03 10:38:44 -05004085 /*
4086 * Start from the beginning does not need to cycle over the
4087 * range, mark it as scanned.
4088 */
4089 scanned = (index == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004090 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004091 index = wbc->range_start >> PAGE_SHIFT;
4092 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004093 scanned = 1;
4094 }
4095 if (wbc->sync_mode == WB_SYNC_ALL)
4096 tag = PAGECACHE_TAG_TOWRITE;
4097 else
4098 tag = PAGECACHE_TAG_DIRTY;
4099retry:
4100 if (wbc->sync_mode == WB_SYNC_ALL)
4101 tag_pages_for_writeback(mapping, index, end);
4102 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara4006f432017-11-15 17:34:37 -08004103 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08004104 tag))) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004105 unsigned i;
4106
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004107 for (i = 0; i < nr_pages; i++) {
4108 struct page *page = pvec.pages[i];
4109
Qu Wenruof91e0d02020-12-02 14:48:00 +08004110 ret = submit_eb_page(page, wbc, &epd, &eb_context);
4111 if (ret == 0)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004112 continue;
Qu Wenruof91e0d02020-12-02 14:48:00 +08004113 if (ret < 0) {
Filipe Manana0607eb1d2019-09-11 17:42:28 +01004114 done = 1;
Filipe Manana0607eb1d2019-09-11 17:42:28 +01004115 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004116 }
4117
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004118 /*
4119 * the filesystem may choose to bump up nr_to_write.
4120 * We have to make sure to honor the new nr_to_write
4121 * at any time
4122 */
4123 nr_to_write_done = wbc->nr_to_write <= 0;
4124 }
4125 pagevec_release(&pvec);
4126 cond_resched();
4127 }
4128 if (!scanned && !done) {
4129 /*
4130 * We hit the last page and there is more work to be done: wrap
4131 * back to the start of the file
4132 */
4133 scanned = 1;
4134 index = 0;
4135 goto retry;
4136 }
Qu Wenruo2b952ee2019-03-20 14:27:43 +08004137 if (ret < 0) {
4138 end_write_bio(&epd, ret);
4139 return ret;
4140 }
Qu Wenruob3ff8f12020-02-12 14:12:44 +08004141 /*
4142 * If something went wrong, don't allow any metadata write bio to be
4143 * submitted.
4144 *
4145 * This would prevent use-after-free if we had dirty pages not
4146 * cleaned up, which can still happen by fuzzed images.
4147 *
4148 * - Bad extent tree
4149 * Allowing existing tree block to be allocated for other trees.
4150 *
4151 * - Log tree operations
4152 * Exiting tree blocks get allocated to log tree, bumps its
4153 * generation, then get cleaned in tree re-balance.
4154 * Such tree block will not be written back, since it's clean,
4155 * thus no WRITTEN flag set.
4156 * And after log writes back, this tree block is not traced by
4157 * any dirty extent_io_tree.
4158 *
4159 * - Offending tree block gets re-dirtied from its original owner
4160 * Since it has bumped generation, no WRITTEN flag, it can be
4161 * reused without COWing. This tree block will not be traced
4162 * by btrfs_transaction::dirty_pages.
4163 *
4164 * Now such dirty tree block will not be cleaned by any dirty
4165 * extent io tree. Thus we don't want to submit such wild eb
4166 * if the fs already has error.
4167 */
4168 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
4169 ret = flush_write_bio(&epd);
4170 } else {
Josef Bacikfbabd4a2020-07-21 10:38:37 -04004171 ret = -EROFS;
Qu Wenruob3ff8f12020-02-12 14:12:44 +08004172 end_write_bio(&epd, ret);
4173 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004174 return ret;
4175}
4176
Chris Masond1310b22008-01-24 16:13:08 -05004177/**
Chris Mason4bef0842008-09-08 11:18:08 -04004178 * 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 -05004179 * @mapping: address space structure to write
4180 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
David Sterba935db852017-06-23 04:30:28 +02004181 * @data: data passed to __extent_writepage function
Chris Masond1310b22008-01-24 16:13:08 -05004182 *
4183 * If a page is already under I/O, write_cache_pages() skips it, even
4184 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
4185 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
4186 * and msync() need to guarantee that all the data which was dirty at the time
4187 * the call was made get new I/O started against them. If wbc->sync_mode is
4188 * WB_SYNC_ALL then we were called for data integrity and we must wait for
4189 * existing IO to complete.
4190 */
David Sterba4242b642017-02-10 19:38:24 +01004191static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04004192 struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01004193 struct extent_page_data *epd)
Chris Masond1310b22008-01-24 16:13:08 -05004194{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004195 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05004196 int ret = 0;
4197 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004198 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004199 struct pagevec pvec;
4200 int nr_pages;
4201 pgoff_t index;
4202 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08004203 pgoff_t done_index;
4204 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004205 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05004206 xa_mark_t tag;
Chris Masond1310b22008-01-24 16:13:08 -05004207
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004208 /*
4209 * We have to hold onto the inode so that ordered extents can do their
4210 * work when the IO finishes. The alternative to this is failing to add
4211 * an ordered extent if the igrab() fails there and that is a huge pain
4212 * to deal with, so instead just hold onto the inode throughout the
4213 * writepages operation. If it fails here we are freeing up the inode
4214 * anyway and we'd rather not waste our time writing out stuff that is
4215 * going to be truncated anyway.
4216 */
4217 if (!igrab(inode))
4218 return 0;
4219
Mel Gorman86679822017-11-15 17:37:52 -08004220 pagevec_init(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05004221 if (wbc->range_cyclic) {
4222 index = mapping->writeback_index; /* Start from prev offset */
4223 end = -1;
Josef Bacik556755a2020-01-03 10:38:44 -05004224 /*
4225 * Start from the beginning does not need to cycle over the
4226 * range, mark it as scanned.
4227 */
4228 scanned = (index == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004229 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004230 index = wbc->range_start >> PAGE_SHIFT;
4231 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08004232 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
4233 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004234 scanned = 1;
4235 }
Ethan Lien3cd24c62018-11-01 14:49:03 +08004236
4237 /*
4238 * We do the tagged writepage as long as the snapshot flush bit is set
4239 * and we are the first one who do the filemap_flush() on this inode.
4240 *
4241 * The nr_to_write == LONG_MAX is needed to make sure other flushers do
4242 * not race in and drop the bit.
4243 */
4244 if (range_whole && wbc->nr_to_write == LONG_MAX &&
4245 test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
4246 &BTRFS_I(inode)->runtime_flags))
4247 wbc->tagged_writepages = 1;
4248
4249 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00004250 tag = PAGECACHE_TAG_TOWRITE;
4251 else
4252 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05004253retry:
Ethan Lien3cd24c62018-11-01 14:49:03 +08004254 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00004255 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08004256 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004257 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara67fd7072017-11-15 17:35:19 -08004258 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
4259 &index, end, tag))) {
Chris Masond1310b22008-01-24 16:13:08 -05004260 unsigned i;
4261
Chris Masond1310b22008-01-24 16:13:08 -05004262 for (i = 0; i < nr_pages; i++) {
4263 struct page *page = pvec.pages[i];
4264
Tejun Heof7bddf12019-10-03 07:27:13 -07004265 done_index = page->index + 1;
Chris Masond1310b22008-01-24 16:13:08 -05004266 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07004267 * At this point we hold neither the i_pages lock nor
4268 * the page lock: the page may be truncated or
4269 * invalidated (changing page->mapping to NULL),
4270 * or even swizzled back from swapper_space to
4271 * tmpfs file mapping
Chris Masond1310b22008-01-24 16:13:08 -05004272 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05004273 if (!trylock_page(page)) {
Qu Wenruof4340622019-03-20 14:27:41 +08004274 ret = flush_write_bio(epd);
4275 BUG_ON(ret < 0);
Josef Bacikc8f2f242013-02-11 11:33:00 -05004276 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04004277 }
Chris Masond1310b22008-01-24 16:13:08 -05004278
4279 if (unlikely(page->mapping != mapping)) {
4280 unlock_page(page);
4281 continue;
4282 }
4283
Chris Masond2c3f4f2008-11-19 12:44:22 -05004284 if (wbc->sync_mode != WB_SYNC_NONE) {
Qu Wenruof4340622019-03-20 14:27:41 +08004285 if (PageWriteback(page)) {
4286 ret = flush_write_bio(epd);
4287 BUG_ON(ret < 0);
4288 }
Chris Masond1310b22008-01-24 16:13:08 -05004289 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004290 }
Chris Masond1310b22008-01-24 16:13:08 -05004291
4292 if (PageWriteback(page) ||
4293 !clear_page_dirty_for_io(page)) {
4294 unlock_page(page);
4295 continue;
4296 }
4297
David Sterbaaab6e9e2017-11-30 18:00:02 +01004298 ret = __extent_writepage(page, wbc, epd);
Liu Boa91326672016-03-07 16:56:21 -08004299 if (ret < 0) {
Liu Boa91326672016-03-07 16:56:21 -08004300 done = 1;
4301 break;
4302 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004303
4304 /*
4305 * the filesystem may choose to bump up nr_to_write.
4306 * We have to make sure to honor the new nr_to_write
4307 * at any time
4308 */
4309 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004310 }
4311 pagevec_release(&pvec);
4312 cond_resched();
4313 }
Liu Bo894b36e2016-03-07 16:56:22 -08004314 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004315 /*
4316 * We hit the last page and there is more work to be done: wrap
4317 * back to the start of the file
4318 */
4319 scanned = 1;
4320 index = 0;
Josef Bacik42ffb0b2020-01-23 15:33:02 -05004321
4322 /*
4323 * If we're looping we could run into a page that is locked by a
4324 * writer and that writer could be waiting on writeback for a
4325 * page in our current bio, and thus deadlock, so flush the
4326 * write bio here.
4327 */
4328 ret = flush_write_bio(epd);
4329 if (!ret)
4330 goto retry;
Chris Masond1310b22008-01-24 16:13:08 -05004331 }
Liu Boa91326672016-03-07 16:56:21 -08004332
4333 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4334 mapping->writeback_index = done_index;
4335
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004336 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004337 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004338}
Chris Masond1310b22008-01-24 16:13:08 -05004339
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004340int extent_write_full_page(struct page *page, struct writeback_control *wbc)
Chris Masond1310b22008-01-24 16:13:08 -05004341{
4342 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004343 struct extent_page_data epd = {
4344 .bio = NULL,
Chris Mason771ed682008-11-06 22:02:51 -05004345 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004346 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004347 };
Chris Masond1310b22008-01-24 16:13:08 -05004348
Chris Masond1310b22008-01-24 16:13:08 -05004349 ret = __extent_writepage(page, wbc, &epd);
Qu Wenruo30659762019-03-20 14:27:42 +08004350 ASSERT(ret <= 0);
4351 if (ret < 0) {
4352 end_write_bio(&epd, ret);
4353 return ret;
4354 }
Chris Masond1310b22008-01-24 16:13:08 -05004355
Qu Wenruo30659762019-03-20 14:27:42 +08004356 ret = flush_write_bio(&epd);
4357 ASSERT(ret <= 0);
Chris Masond1310b22008-01-24 16:13:08 -05004358 return ret;
4359}
Chris Masond1310b22008-01-24 16:13:08 -05004360
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004361int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
Chris Mason771ed682008-11-06 22:02:51 -05004362 int mode)
4363{
4364 int ret = 0;
4365 struct address_space *mapping = inode->i_mapping;
4366 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004367 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4368 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004369
4370 struct extent_page_data epd = {
4371 .bio = NULL,
Chris Mason771ed682008-11-06 22:02:51 -05004372 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004373 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05004374 };
4375 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004376 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004377 .nr_to_write = nr_pages * 2,
4378 .range_start = start,
4379 .range_end = end + 1,
Chris Masonec39f762019-07-10 12:28:17 -07004380 /* We're called from an async helper function */
4381 .punt_to_cgroup = 1,
4382 .no_cgroup_owner = 1,
Chris Mason771ed682008-11-06 22:02:51 -05004383 };
4384
Chris Masondbb70be2019-07-10 12:28:18 -07004385 wbc_attach_fdatawrite_inode(&wbc_writepages, inode);
Chris Masond3977122009-01-05 21:25:51 -05004386 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004387 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004388 if (clear_page_dirty_for_io(page))
4389 ret = __extent_writepage(page, &wbc_writepages, &epd);
4390 else {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02004391 btrfs_writepage_endio_finish_ordered(page, start,
Nikolay Borisovc6297322018-11-08 10:18:08 +02004392 start + PAGE_SIZE - 1, 1);
Chris Mason771ed682008-11-06 22:02:51 -05004393 unlock_page(page);
4394 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004395 put_page(page);
4396 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004397 }
4398
Qu Wenruo02c6db42019-03-20 14:27:45 +08004399 ASSERT(ret <= 0);
Chris Masondbb70be2019-07-10 12:28:18 -07004400 if (ret == 0)
4401 ret = flush_write_bio(&epd);
4402 else
Qu Wenruo02c6db42019-03-20 14:27:45 +08004403 end_write_bio(&epd, ret);
Chris Masondbb70be2019-07-10 12:28:18 -07004404
4405 wbc_detach_inode(&wbc_writepages);
Chris Mason771ed682008-11-06 22:02:51 -05004406 return ret;
4407}
Chris Masond1310b22008-01-24 16:13:08 -05004408
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004409int extent_writepages(struct address_space *mapping,
Chris Masond1310b22008-01-24 16:13:08 -05004410 struct writeback_control *wbc)
4411{
4412 int ret = 0;
4413 struct extent_page_data epd = {
4414 .bio = NULL,
Chris Mason771ed682008-11-06 22:02:51 -05004415 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004416 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004417 };
4418
David Sterba935db852017-06-23 04:30:28 +02004419 ret = extent_write_cache_pages(mapping, wbc, &epd);
Qu Wenruoa2a72fb2019-03-20 14:27:48 +08004420 ASSERT(ret <= 0);
4421 if (ret < 0) {
4422 end_write_bio(&epd, ret);
4423 return ret;
4424 }
4425 ret = flush_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004426 return ret;
4427}
Chris Masond1310b22008-01-24 16:13:08 -05004428
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07004429void extent_readahead(struct readahead_control *rac)
Chris Masond1310b22008-01-24 16:13:08 -05004430{
4431 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004432 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004433 struct page *pagepool[16];
Miao Xie125bac012013-07-25 19:22:37 +08004434 struct extent_map *em_cached = NULL;
Filipe Manana808f80b2015-09-28 09:56:26 +01004435 u64 prev_em_start = (u64)-1;
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07004436 int nr;
Chris Masond1310b22008-01-24 16:13:08 -05004437
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07004438 while ((nr = readahead_page_batch(rac, pagepool))) {
4439 u64 contig_start = page_offset(pagepool[0]);
4440 u64 contig_end = page_offset(pagepool[nr - 1]) + PAGE_SIZE - 1;
Nikolay Borisove65ef212019-03-11 09:55:38 +02004441
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07004442 ASSERT(contig_start + nr * PAGE_SIZE - 1 == contig_end);
Chris Masond1310b22008-01-24 16:13:08 -05004443
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07004444 contiguous_readpages(pagepool, nr, contig_start, contig_end,
4445 &em_cached, &bio, &bio_flags, &prev_em_start);
Chris Masond1310b22008-01-24 16:13:08 -05004446 }
Liu Bo67c96842012-07-20 21:43:09 -06004447
Miao Xie125bac012013-07-25 19:22:37 +08004448 if (em_cached)
4449 free_extent_map(em_cached);
4450
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07004451 if (bio) {
4452 if (submit_one_bio(bio, 0, bio_flags))
4453 return;
4454 }
Chris Masond1310b22008-01-24 16:13:08 -05004455}
Chris Masond1310b22008-01-24 16:13:08 -05004456
4457/*
4458 * basic invalidatepage code, this waits on any locked or writeback
4459 * ranges corresponding to the page, and then deletes any extent state
4460 * records from the tree
4461 */
4462int extent_invalidatepage(struct extent_io_tree *tree,
4463 struct page *page, unsigned long offset)
4464{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004465 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004466 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004467 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004468 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4469
Qu Wenruo829ddec2020-11-13 20:51:39 +08004470 /* This function is only called for the btree inode */
4471 ASSERT(tree->owner == IO_TREE_BTREE_INODE_IO);
4472
Qu Wenruofda28322013-02-26 08:10:22 +00004473 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004474 if (start > end)
4475 return 0;
4476
David Sterbaff13db42015-12-03 14:30:40 +01004477 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004478 wait_on_page_writeback(page);
Qu Wenruo829ddec2020-11-13 20:51:39 +08004479
4480 /*
4481 * Currently for btree io tree, only EXTENT_LOCKED is utilized,
4482 * so here we only need to unlock the extent range to free any
4483 * existing extent state.
4484 */
4485 unlock_extent_cached(tree, start, end, &cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05004486 return 0;
4487}
Chris Masond1310b22008-01-24 16:13:08 -05004488
4489/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004490 * a helper for releasepage, this tests for areas of the page that
4491 * are locked or under IO and drops the related state bits if it is safe
4492 * to drop the page.
4493 */
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004494static int try_release_extent_state(struct extent_io_tree *tree,
Eric Sandeen48a3b632013-04-25 20:41:01 +00004495 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004496{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004497 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004498 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004499 int ret = 1;
4500
Nikolay Borisov88826792019-03-14 15:28:31 +02004501 if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) {
Chris Mason7b13b7b2008-04-18 10:29:50 -04004502 ret = 0;
Nikolay Borisov88826792019-03-14 15:28:31 +02004503 } else {
Chris Mason11ef1602009-09-23 20:28:46 -04004504 /*
Filipe Manana2766ff62020-11-04 11:07:34 +00004505 * At this point we can safely clear everything except the
4506 * locked bit, the nodatasum bit and the delalloc new bit.
4507 * The delalloc new bit will be cleared by ordered extent
4508 * completion.
Chris Mason11ef1602009-09-23 20:28:46 -04004509 */
David Sterba66b0c882017-10-31 16:30:47 +01004510 ret = __clear_extent_bit(tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00004511 ~(EXTENT_LOCKED | EXTENT_NODATASUM | EXTENT_DELALLOC_NEW),
4512 0, 0, NULL, mask, NULL);
Chris Masone3f24cc2011-02-14 12:52:08 -05004513
4514 /* if clear_extent_bit failed for enomem reasons,
4515 * we can't allow the release to continue.
4516 */
4517 if (ret < 0)
4518 ret = 0;
4519 else
4520 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004521 }
4522 return ret;
4523}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004524
4525/*
Chris Masond1310b22008-01-24 16:13:08 -05004526 * a helper for releasepage. As long as there are no locked extents
4527 * in the range corresponding to the page, both state records and extent
4528 * map records are removed
4529 */
Nikolay Borisov477a30b2018-04-19 10:46:34 +03004530int try_release_extent_mapping(struct page *page, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004531{
4532 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004533 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004534 u64 end = start + PAGE_SIZE - 1;
Filipe Mananabd3599a2018-07-12 01:36:43 +01004535 struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
4536 struct extent_io_tree *tree = &btrfs_inode->io_tree;
4537 struct extent_map_tree *map = &btrfs_inode->extent_tree;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004538
Mel Gormand0164ad2015-11-06 16:28:21 -08004539 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004540 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004541 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004542 while (start <= end) {
Filipe Mananafbc2bd72020-07-22 12:28:52 +01004543 struct btrfs_fs_info *fs_info;
4544 u64 cur_gen;
4545
Yan39b56372008-02-15 10:40:50 -05004546 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004547 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004548 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004549 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004550 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004551 break;
4552 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004553 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4554 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004555 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004556 free_extent_map(em);
4557 break;
4558 }
Filipe Manana3d6448e2020-07-22 12:28:37 +01004559 if (test_range_bit(tree, em->start,
4560 extent_map_end(em) - 1,
4561 EXTENT_LOCKED, 0, NULL))
4562 goto next;
4563 /*
4564 * If it's not in the list of modified extents, used
4565 * by a fast fsync, we can remove it. If it's being
4566 * logged we can safely remove it since fsync took an
4567 * extra reference on the em.
4568 */
4569 if (list_empty(&em->list) ||
Filipe Mananafbc2bd72020-07-22 12:28:52 +01004570 test_bit(EXTENT_FLAG_LOGGING, &em->flags))
4571 goto remove_em;
4572 /*
4573 * If it's in the list of modified extents, remove it
4574 * only if its generation is older then the current one,
4575 * in which case we don't need it for a fast fsync.
4576 * Otherwise don't remove it, we could be racing with an
4577 * ongoing fast fsync that could miss the new extent.
4578 */
4579 fs_info = btrfs_inode->root->fs_info;
4580 spin_lock(&fs_info->trans_lock);
4581 cur_gen = fs_info->generation;
4582 spin_unlock(&fs_info->trans_lock);
4583 if (em->generation >= cur_gen)
4584 goto next;
4585remove_em:
Filipe Manana5e548b32020-07-22 12:29:01 +01004586 /*
4587 * We only remove extent maps that are not in the list of
4588 * modified extents or that are in the list but with a
4589 * generation lower then the current generation, so there
4590 * is no need to set the full fsync flag on the inode (it
4591 * hurts the fsync performance for workloads with a data
4592 * size that exceeds or is close to the system's memory).
4593 */
Filipe Mananafbc2bd72020-07-22 12:28:52 +01004594 remove_extent_mapping(map, em);
4595 /* once for the rb tree */
4596 free_extent_map(em);
Filipe Manana3d6448e2020-07-22 12:28:37 +01004597next:
Chris Mason70dec802008-01-29 09:59:12 -05004598 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004599 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004600
4601 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004602 free_extent_map(em);
Paul E. McKenney9f47eb52020-05-08 14:15:37 -07004603
4604 cond_resched(); /* Allow large-extent preemption. */
Chris Masond1310b22008-01-24 16:13:08 -05004605 }
Chris Masond1310b22008-01-24 16:13:08 -05004606 }
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004607 return try_release_extent_state(tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004608}
Chris Masond1310b22008-01-24 16:13:08 -05004609
Chris Masonec29ed52011-02-23 16:23:20 -05004610/*
4611 * helper function for fiemap, which doesn't want to see any holes.
4612 * This maps until we find something past 'last'
4613 */
Nikolay Borisovf1bbde82020-08-31 14:42:45 +03004614static struct extent_map *get_extent_skip_holes(struct btrfs_inode *inode,
David Sterbae3350e12017-06-23 04:09:57 +02004615 u64 offset, u64 last)
Chris Masonec29ed52011-02-23 16:23:20 -05004616{
Nikolay Borisovf1bbde82020-08-31 14:42:45 +03004617 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004618 struct extent_map *em;
4619 u64 len;
4620
4621 if (offset >= last)
4622 return NULL;
4623
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304624 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004625 len = last - offset;
4626 if (len == 0)
4627 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004628 len = ALIGN(len, sectorsize);
Nikolay Borisovf1bbde82020-08-31 14:42:45 +03004629 em = btrfs_get_extent_fiemap(inode, offset, len);
David Sterbac7040052011-04-19 18:00:01 +02004630 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004631 return em;
4632
4633 /* if this isn't a hole return it */
Nikolay Borisov4a2d25c2017-11-23 10:51:43 +02004634 if (em->block_start != EXTENT_MAP_HOLE)
Chris Masonec29ed52011-02-23 16:23:20 -05004635 return em;
Chris Masonec29ed52011-02-23 16:23:20 -05004636
4637 /* this is a hole, advance to the next extent */
4638 offset = extent_map_end(em);
4639 free_extent_map(em);
4640 if (offset >= last)
4641 break;
4642 }
4643 return NULL;
4644}
4645
Qu Wenruo47518322017-04-07 10:43:15 +08004646/*
4647 * To cache previous fiemap extent
4648 *
4649 * Will be used for merging fiemap extent
4650 */
4651struct fiemap_cache {
4652 u64 offset;
4653 u64 phys;
4654 u64 len;
4655 u32 flags;
4656 bool cached;
4657};
4658
4659/*
4660 * Helper to submit fiemap extent.
4661 *
4662 * Will try to merge current fiemap extent specified by @offset, @phys,
4663 * @len and @flags with cached one.
4664 * And only when we fails to merge, cached one will be submitted as
4665 * fiemap extent.
4666 *
4667 * Return value is the same as fiemap_fill_next_extent().
4668 */
4669static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4670 struct fiemap_cache *cache,
4671 u64 offset, u64 phys, u64 len, u32 flags)
4672{
4673 int ret = 0;
4674
4675 if (!cache->cached)
4676 goto assign;
4677
4678 /*
4679 * Sanity check, extent_fiemap() should have ensured that new
Andrea Gelmini52042d82018-11-28 12:05:13 +01004680 * fiemap extent won't overlap with cached one.
Qu Wenruo47518322017-04-07 10:43:15 +08004681 * Not recoverable.
4682 *
4683 * NOTE: Physical address can overlap, due to compression
4684 */
4685 if (cache->offset + cache->len > offset) {
4686 WARN_ON(1);
4687 return -EINVAL;
4688 }
4689
4690 /*
4691 * Only merges fiemap extents if
4692 * 1) Their logical addresses are continuous
4693 *
4694 * 2) Their physical addresses are continuous
4695 * So truly compressed (physical size smaller than logical size)
4696 * extents won't get merged with each other
4697 *
4698 * 3) Share same flags except FIEMAP_EXTENT_LAST
4699 * So regular extent won't get merged with prealloc extent
4700 */
4701 if (cache->offset + cache->len == offset &&
4702 cache->phys + cache->len == phys &&
4703 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4704 (flags & ~FIEMAP_EXTENT_LAST)) {
4705 cache->len += len;
4706 cache->flags |= flags;
4707 goto try_submit_last;
4708 }
4709
4710 /* Not mergeable, need to submit cached one */
4711 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4712 cache->len, cache->flags);
4713 cache->cached = false;
4714 if (ret)
4715 return ret;
4716assign:
4717 cache->cached = true;
4718 cache->offset = offset;
4719 cache->phys = phys;
4720 cache->len = len;
4721 cache->flags = flags;
4722try_submit_last:
4723 if (cache->flags & FIEMAP_EXTENT_LAST) {
4724 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4725 cache->phys, cache->len, cache->flags);
4726 cache->cached = false;
4727 }
4728 return ret;
4729}
4730
4731/*
Qu Wenruo848c23b2017-06-22 10:01:21 +08004732 * Emit last fiemap cache
Qu Wenruo47518322017-04-07 10:43:15 +08004733 *
Qu Wenruo848c23b2017-06-22 10:01:21 +08004734 * The last fiemap cache may still be cached in the following case:
4735 * 0 4k 8k
4736 * |<- Fiemap range ->|
4737 * |<------------ First extent ----------->|
4738 *
4739 * In this case, the first extent range will be cached but not emitted.
4740 * So we must emit it before ending extent_fiemap().
Qu Wenruo47518322017-04-07 10:43:15 +08004741 */
David Sterba5c5aff92019-03-20 11:29:46 +01004742static int emit_last_fiemap_cache(struct fiemap_extent_info *fieinfo,
Qu Wenruo848c23b2017-06-22 10:01:21 +08004743 struct fiemap_cache *cache)
Qu Wenruo47518322017-04-07 10:43:15 +08004744{
4745 int ret;
4746
4747 if (!cache->cached)
4748 return 0;
4749
Qu Wenruo47518322017-04-07 10:43:15 +08004750 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4751 cache->len, cache->flags);
4752 cache->cached = false;
4753 if (ret > 0)
4754 ret = 0;
4755 return ret;
4756}
4757
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03004758int extent_fiemap(struct btrfs_inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02004759 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004760{
Josef Bacik975f84f2010-11-23 19:36:57 +00004761 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004762 u64 off = start;
4763 u64 max = start + len;
4764 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004765 u32 found_type;
4766 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004767 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004768 u64 disko = 0;
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03004769 u64 isize = i_size_read(&inode->vfs_inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004770 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004771 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004772 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004773 struct btrfs_path *path;
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03004774 struct btrfs_root *root = inode->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004775 struct fiemap_cache cache = { 0 };
David Sterba5911c8f2019-05-15 15:31:04 +02004776 struct ulist *roots;
4777 struct ulist *tmp_ulist;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004778 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004779 u64 em_start = 0;
4780 u64 em_len = 0;
4781 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004782
4783 if (len == 0)
4784 return -EINVAL;
4785
Josef Bacik975f84f2010-11-23 19:36:57 +00004786 path = btrfs_alloc_path();
4787 if (!path)
4788 return -ENOMEM;
Josef Bacik975f84f2010-11-23 19:36:57 +00004789
David Sterba5911c8f2019-05-15 15:31:04 +02004790 roots = ulist_alloc(GFP_KERNEL);
4791 tmp_ulist = ulist_alloc(GFP_KERNEL);
4792 if (!roots || !tmp_ulist) {
4793 ret = -ENOMEM;
4794 goto out_free_ulist;
4795 }
4796
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03004797 start = round_down(start, btrfs_inode_sectorsize(inode));
4798 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004799
Chris Masonec29ed52011-02-23 16:23:20 -05004800 /*
4801 * lookup the last file extent. We're not using i_size here
4802 * because there might be preallocation past i_size
4803 */
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03004804 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), -1,
4805 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004806 if (ret < 0) {
David Sterba5911c8f2019-05-15 15:31:04 +02004807 goto out_free_ulist;
Liu Bo2d324f52016-05-17 17:21:48 -07004808 } else {
4809 WARN_ON(!ret);
4810 if (ret == 1)
4811 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004812 }
Liu Bo2d324f52016-05-17 17:21:48 -07004813
Josef Bacik975f84f2010-11-23 19:36:57 +00004814 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004815 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004816 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004817
Chris Masonec29ed52011-02-23 16:23:20 -05004818 /* No extents, but there might be delalloc bits */
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03004819 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004820 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004821 /* have to trust i_size as the end */
4822 last = (u64)-1;
4823 last_for_get_extent = isize;
4824 } else {
4825 /*
4826 * remember the start of the last extent. There are a
4827 * bunch of different factors that go into the length of the
4828 * extent, so its much less complex to remember where it started
4829 */
4830 last = found_key.offset;
4831 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004832 }
Liu Bofe09e162013-09-22 12:54:23 +08004833 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004834
Chris Masonec29ed52011-02-23 16:23:20 -05004835 /*
4836 * we might have some extents allocated but more delalloc past those
4837 * extents. so, we trust isize unless the start of the last extent is
4838 * beyond isize
4839 */
4840 if (last < isize) {
4841 last = (u64)-1;
4842 last_for_get_extent = isize;
4843 }
4844
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03004845 lock_extent_bits(&inode->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004846 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004847
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03004848 em = get_extent_skip_holes(inode, start, last_for_get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004849 if (!em)
4850 goto out;
4851 if (IS_ERR(em)) {
4852 ret = PTR_ERR(em);
4853 goto out;
4854 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004855
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004856 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004857 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004858
Chris Masonea8efc72011-03-08 11:54:40 -05004859 /* break if the extent we found is outside the range */
4860 if (em->start >= max || extent_map_end(em) < off)
4861 break;
4862
4863 /*
4864 * get_extent may return an extent that starts before our
4865 * requested range. We have to make sure the ranges
4866 * we return to fiemap always move forward and don't
4867 * overlap, so adjust the offsets here
4868 */
4869 em_start = max(em->start, off);
4870
4871 /*
4872 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004873 * for adjusting the disk offset below. Only do this if the
4874 * extent isn't compressed since our in ram offset may be past
4875 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004876 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004877 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4878 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004879 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004880 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004881 flags = 0;
Filipe Mananaf0986312018-06-20 10:02:30 +01004882 if (em->block_start < EXTENT_MAP_LAST_BYTE)
4883 disko = em->block_start + offset_in_extent;
4884 else
4885 disko = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004886
Chris Masonea8efc72011-03-08 11:54:40 -05004887 /*
4888 * bump off for our next call to get_extent
4889 */
4890 off = extent_map_end(em);
4891 if (off >= max)
4892 end = 1;
4893
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004894 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004895 end = 1;
4896 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004897 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004898 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4899 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004900 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004901 flags |= (FIEMAP_EXTENT_DELALLOC |
4902 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004903 } else if (fieinfo->fi_extents_max) {
4904 u64 bytenr = em->block_start -
4905 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004906
Liu Bofe09e162013-09-22 12:54:23 +08004907 /*
4908 * As btrfs supports shared space, this information
4909 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004910 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4911 * then we're just getting a count and we can skip the
4912 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004913 */
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03004914 ret = btrfs_check_shared(root, btrfs_ino(inode),
David Sterba5911c8f2019-05-15 15:31:04 +02004915 bytenr, roots, tmp_ulist);
Josef Bacikdc046b12014-09-10 16:20:45 -04004916 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004917 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004918 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004919 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004920 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004921 }
4922 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4923 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004924 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4925 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004926
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004927 free_extent_map(em);
4928 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004929 if ((em_start >= last) || em_len == (u64)-1 ||
4930 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004931 flags |= FIEMAP_EXTENT_LAST;
4932 end = 1;
4933 }
4934
Chris Masonec29ed52011-02-23 16:23:20 -05004935 /* now scan forward to see if this is really the last extent. */
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03004936 em = get_extent_skip_holes(inode, off, last_for_get_extent);
Chris Masonec29ed52011-02-23 16:23:20 -05004937 if (IS_ERR(em)) {
4938 ret = PTR_ERR(em);
4939 goto out;
4940 }
4941 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004942 flags |= FIEMAP_EXTENT_LAST;
4943 end = 1;
4944 }
Qu Wenruo47518322017-04-07 10:43:15 +08004945 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4946 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004947 if (ret) {
4948 if (ret == 1)
4949 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004950 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004951 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004952 }
4953out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004954 if (!ret)
David Sterba5c5aff92019-03-20 11:29:46 +01004955 ret = emit_last_fiemap_cache(fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004956 free_extent_map(em);
4957out:
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03004958 unlock_extent_cached(&inode->io_tree, start, start + len - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004959 &cached_state);
David Sterba5911c8f2019-05-15 15:31:04 +02004960
4961out_free_ulist:
Colin Ian Kinge02d48e2019-07-05 08:26:24 +01004962 btrfs_free_path(path);
David Sterba5911c8f2019-05-15 15:31:04 +02004963 ulist_free(roots);
4964 ulist_free(tmp_ulist);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004965 return ret;
4966}
4967
Chris Mason727011e2010-08-06 13:21:20 -04004968static void __free_extent_buffer(struct extent_buffer *eb)
4969{
Chris Mason727011e2010-08-06 13:21:20 -04004970 kmem_cache_free(extent_buffer_cache, eb);
4971}
4972
David Sterba2b489662020-04-29 03:04:10 +02004973int extent_buffer_under_io(const struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004974{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004975 return (atomic_read(&eb->io_pages) ||
4976 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4977 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004978}
4979
Miao Xie897ca6e92010-10-26 20:57:29 -04004980/*
David Sterba55ac0132018-07-19 17:24:32 +02004981 * Release all pages attached to the extent buffer.
Miao Xie897ca6e92010-10-26 20:57:29 -04004982 */
David Sterba55ac0132018-07-19 17:24:32 +02004983static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004984{
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004985 int i;
4986 int num_pages;
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004987 int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004988
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004989 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004990
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004991 num_pages = num_extent_pages(eb);
4992 for (i = 0; i < num_pages; i++) {
4993 struct page *page = eb->pages[i];
Miao Xie897ca6e92010-10-26 20:57:29 -04004994
Forrest Liu5d2361d2015-02-09 17:31:45 +08004995 if (!page)
4996 continue;
4997 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004998 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004999 /*
5000 * We do this since we'll remove the pages after we've
5001 * removed the eb from the radix tree, so we could race
5002 * and have this page now attached to the new eb. So
5003 * only clear page_private if it's still connected to
5004 * this eb.
5005 */
5006 if (PagePrivate(page) &&
5007 page->private == (unsigned long)eb) {
5008 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
5009 BUG_ON(PageDirty(page));
5010 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005011 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08005012 * We need to make sure we haven't be attached
5013 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005014 */
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07005015 detach_page_private(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005016 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08005017
5018 if (mapped)
5019 spin_unlock(&page->mapping->private_lock);
5020
Nicholas D Steeves01327612016-05-19 21:18:45 -04005021 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005022 put_page(page);
Nikolay Borisovd64766f2018-06-27 16:38:22 +03005023 }
Miao Xie897ca6e92010-10-26 20:57:29 -04005024}
5025
5026/*
5027 * Helper for releasing the extent buffer.
5028 */
5029static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
5030{
David Sterba55ac0132018-07-19 17:24:32 +02005031 btrfs_release_extent_buffer_pages(eb);
Josef Bacik8c389382020-02-14 16:11:42 -05005032 btrfs_leak_debug_del(&eb->fs_info->eb_leak_lock, &eb->leak_list);
Miao Xie897ca6e92010-10-26 20:57:29 -04005033 __free_extent_buffer(eb);
5034}
5035
Josef Bacikf28491e2013-12-16 13:24:27 -05005036static struct extent_buffer *
5037__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02005038 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04005039{
5040 struct extent_buffer *eb = NULL;
5041
Michal Hockod1b5c562015-08-19 14:17:40 +02005042 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04005043 eb->start = start;
5044 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05005045 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04005046 eb->bflags = 0;
Josef Bacik196d59a2020-08-20 11:46:09 -04005047 init_rwsem(&eb->lock);
Josef Bacikdb7f3432013-08-07 14:54:37 -04005048
Josef Bacik3fd63722020-02-14 16:11:40 -05005049 btrfs_leak_debug_add(&fs_info->eb_leak_lock, &eb->leak_list,
5050 &fs_info->allocated_ebs);
Josef Bacikdb7f3432013-08-07 14:54:37 -04005051
5052 spin_lock_init(&eb->refs_lock);
5053 atomic_set(&eb->refs, 1);
5054 atomic_set(&eb->io_pages, 0);
5055
Qu Wenruodeb67892020-12-02 14:48:01 +08005056 ASSERT(len <= BTRFS_MAX_METADATA_BLOCKSIZE);
Josef Bacikdb7f3432013-08-07 14:54:37 -04005057
5058 return eb;
5059}
5060
David Sterba2b489662020-04-29 03:04:10 +02005061struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src)
Josef Bacikdb7f3432013-08-07 14:54:37 -04005062{
David Sterbacc5e31a2018-03-01 18:20:27 +01005063 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04005064 struct page *p;
5065 struct extent_buffer *new;
David Sterbacc5e31a2018-03-01 18:20:27 +01005066 int num_pages = num_extent_pages(src);
Josef Bacikdb7f3432013-08-07 14:54:37 -04005067
David Sterba3f556f72014-06-15 03:20:26 +02005068 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04005069 if (new == NULL)
5070 return NULL;
5071
5072 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04005073 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04005074 if (!p) {
5075 btrfs_release_extent_buffer(new);
5076 return NULL;
5077 }
5078 attach_extent_buffer_page(new, p);
5079 WARN_ON(PageDirty(p));
5080 SetPageUptodate(p);
5081 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01005082 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04005083 }
5084
Josef Bacikdb7f3432013-08-07 14:54:37 -04005085 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005086 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04005087
5088 return new;
5089}
5090
Omar Sandoval0f331222015-09-29 20:50:31 -07005091struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
5092 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04005093{
5094 struct extent_buffer *eb;
David Sterbacc5e31a2018-03-01 18:20:27 +01005095 int num_pages;
5096 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04005097
David Sterba3f556f72014-06-15 03:20:26 +02005098 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04005099 if (!eb)
5100 return NULL;
5101
David Sterba65ad0102018-06-29 10:56:49 +02005102 num_pages = num_extent_pages(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04005103 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04005104 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04005105 if (!eb->pages[i])
5106 goto err;
5107 }
5108 set_extent_buffer_uptodate(eb);
5109 btrfs_set_header_nritems(eb, 0);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005110 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04005111
5112 return eb;
5113err:
5114 for (; i > 0; i--)
5115 __free_page(eb->pages[i - 1]);
5116 __free_extent_buffer(eb);
5117 return NULL;
5118}
5119
Omar Sandoval0f331222015-09-29 20:50:31 -07005120struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005121 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07005122{
Jeff Mahoneyda170662016-06-15 09:22:56 -04005123 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07005124}
5125
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005126static void check_buffer_tree_ref(struct extent_buffer *eb)
5127{
Chris Mason242e18c2013-01-29 17:49:37 -05005128 int refs;
Boris Burkov6bf9cd22020-06-17 11:35:19 -07005129 /*
5130 * The TREE_REF bit is first set when the extent_buffer is added
5131 * to the radix tree. It is also reset, if unset, when a new reference
5132 * is created by find_extent_buffer.
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005133 *
Boris Burkov6bf9cd22020-06-17 11:35:19 -07005134 * It is only cleared in two cases: freeing the last non-tree
5135 * reference to the extent_buffer when its STALE bit is set or
5136 * calling releasepage when the tree reference is the only reference.
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005137 *
Boris Burkov6bf9cd22020-06-17 11:35:19 -07005138 * In both cases, care is taken to ensure that the extent_buffer's
5139 * pages are not under io. However, releasepage can be concurrently
5140 * called with creating new references, which is prone to race
5141 * conditions between the calls to check_buffer_tree_ref in those
5142 * codepaths and clearing TREE_REF in try_release_extent_buffer.
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005143 *
Boris Burkov6bf9cd22020-06-17 11:35:19 -07005144 * The actual lifetime of the extent_buffer in the radix tree is
5145 * adequately protected by the refcount, but the TREE_REF bit and
5146 * its corresponding reference are not. To protect against this
5147 * class of races, we call check_buffer_tree_ref from the codepaths
5148 * which trigger io after they set eb->io_pages. Note that once io is
5149 * initiated, TREE_REF can no longer be cleared, so that is the
5150 * moment at which any such race is best fixed.
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005151 */
Chris Mason242e18c2013-01-29 17:49:37 -05005152 refs = atomic_read(&eb->refs);
5153 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5154 return;
5155
Josef Bacik594831c2012-07-20 16:11:08 -04005156 spin_lock(&eb->refs_lock);
5157 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005158 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04005159 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005160}
5161
Mel Gorman2457aec2014-06-04 16:10:31 -07005162static void mark_extent_buffer_accessed(struct extent_buffer *eb,
5163 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04005164{
David Sterbacc5e31a2018-03-01 18:20:27 +01005165 int num_pages, i;
Josef Bacik5df42352012-03-15 18:24:42 -04005166
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005167 check_buffer_tree_ref(eb);
5168
David Sterba65ad0102018-06-29 10:56:49 +02005169 num_pages = num_extent_pages(eb);
Josef Bacik5df42352012-03-15 18:24:42 -04005170 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005171 struct page *p = eb->pages[i];
5172
Mel Gorman2457aec2014-06-04 16:10:31 -07005173 if (p != accessed)
5174 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04005175 }
5176}
5177
Josef Bacikf28491e2013-12-16 13:24:27 -05005178struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
5179 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005180{
5181 struct extent_buffer *eb;
5182
5183 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05005184 eb = radix_tree_lookup(&fs_info->buffer_radix,
Qu Wenruo478ef882020-10-21 14:25:05 +08005185 start >> fs_info->sectorsize_bits);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005186 if (eb && atomic_inc_not_zero(&eb->refs)) {
5187 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01005188 /*
5189 * Lock our eb's refs_lock to avoid races with
5190 * free_extent_buffer. When we get our eb it might be flagged
5191 * with EXTENT_BUFFER_STALE and another task running
5192 * free_extent_buffer might have seen that flag set,
5193 * eb->refs == 2, that the buffer isn't under IO (dirty and
5194 * writeback flags not set) and it's still in the tree (flag
5195 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
5196 * of decrementing the extent buffer's reference count twice.
5197 * So here we could race and increment the eb's reference count,
5198 * clear its stale flag, mark it as dirty and drop our reference
5199 * before the other task finishes executing free_extent_buffer,
5200 * which would later result in an attempt to free an extent
5201 * buffer that is dirty.
5202 */
5203 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
5204 spin_lock(&eb->refs_lock);
5205 spin_unlock(&eb->refs_lock);
5206 }
Mel Gorman2457aec2014-06-04 16:10:31 -07005207 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005208 return eb;
5209 }
5210 rcu_read_unlock();
5211
5212 return NULL;
5213}
5214
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005215#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
5216struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005217 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005218{
5219 struct extent_buffer *eb, *exists = NULL;
5220 int ret;
5221
5222 eb = find_extent_buffer(fs_info, start);
5223 if (eb)
5224 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04005225 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005226 if (!eb)
Dan Carpenterb6293c82019-12-03 14:24:58 +03005227 return ERR_PTR(-ENOMEM);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005228 eb->fs_info = fs_info;
5229again:
David Sterbae1860a72016-05-09 14:11:38 +02005230 ret = radix_tree_preload(GFP_NOFS);
Dan Carpenterb6293c82019-12-03 14:24:58 +03005231 if (ret) {
5232 exists = ERR_PTR(ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005233 goto free_eb;
Dan Carpenterb6293c82019-12-03 14:24:58 +03005234 }
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005235 spin_lock(&fs_info->buffer_lock);
5236 ret = radix_tree_insert(&fs_info->buffer_radix,
Qu Wenruo478ef882020-10-21 14:25:05 +08005237 start >> fs_info->sectorsize_bits, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005238 spin_unlock(&fs_info->buffer_lock);
5239 radix_tree_preload_end();
5240 if (ret == -EEXIST) {
5241 exists = find_extent_buffer(fs_info, start);
5242 if (exists)
5243 goto free_eb;
5244 else
5245 goto again;
5246 }
5247 check_buffer_tree_ref(eb);
5248 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
5249
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005250 return eb;
5251free_eb:
5252 btrfs_release_extent_buffer(eb);
5253 return exists;
5254}
5255#endif
5256
Josef Bacikf28491e2013-12-16 13:24:27 -05005257struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
Josef Bacik3fbaf252020-11-05 10:45:20 -05005258 u64 start, u64 owner_root, int level)
Chris Masond1310b22008-01-24 16:13:08 -05005259{
Jeff Mahoneyda170662016-06-15 09:22:56 -04005260 unsigned long len = fs_info->nodesize;
David Sterbacc5e31a2018-03-01 18:20:27 +01005261 int num_pages;
5262 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005263 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005264 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04005265 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05005266 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05005267 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05005268 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005269 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05005270
Jeff Mahoneyda170662016-06-15 09:22:56 -04005271 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07005272 btrfs_err(fs_info, "bad tree block start %llu", start);
5273 return ERR_PTR(-EINVAL);
5274 }
5275
Qu Wenruo1aaac382020-12-02 14:48:02 +08005276 if (fs_info->sectorsize < PAGE_SIZE &&
5277 offset_in_page(start) + len > PAGE_SIZE) {
5278 btrfs_err(fs_info,
5279 "tree block crosses page boundary, start %llu nodesize %lu",
5280 start, len);
5281 return ERR_PTR(-EINVAL);
5282 }
5283
Josef Bacikf28491e2013-12-16 13:24:27 -05005284 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005285 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04005286 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04005287
David Sterba23d79d82014-06-15 02:55:29 +02005288 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04005289 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07005290 return ERR_PTR(-ENOMEM);
Josef Bacike114c542020-11-05 10:45:21 -05005291 btrfs_set_buffer_lockdep_class(owner_root, eb, level);
Chris Masond1310b22008-01-24 16:13:08 -05005292
David Sterba65ad0102018-06-29 10:56:49 +02005293 num_pages = num_extent_pages(eb);
Chris Mason727011e2010-08-06 13:21:20 -04005294 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02005295 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07005296 if (!p) {
5297 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04005298 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005299 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005300
5301 spin_lock(&mapping->private_lock);
5302 if (PagePrivate(p)) {
5303 /*
5304 * We could have already allocated an eb for this page
5305 * and attached one so lets see if we can get a ref on
5306 * the existing eb, and if we can we know it's good and
5307 * we can just return that one, else we know we can just
5308 * overwrite page->private.
5309 */
5310 exists = (struct extent_buffer *)p->private;
5311 if (atomic_inc_not_zero(&exists->refs)) {
5312 spin_unlock(&mapping->private_lock);
5313 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005314 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07005315 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005316 goto free_eb;
5317 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005318 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005319
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005320 WARN_ON(PageDirty(p));
Qu Wenruofb22e9c2020-11-13 20:51:49 +08005321 detach_page_private(p);
Chris Masond1310b22008-01-24 16:13:08 -05005322 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005323 attach_extent_buffer_page(eb, p);
5324 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005325 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04005326 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05005327 if (!PageUptodate(p))
5328 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05005329
5330 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005331 * We can't unlock the pages just yet since the extent buffer
5332 * hasn't been properly inserted in the radix tree, this
5333 * opens a race with btree_releasepage which can free a page
5334 * while we are still filling in all pages for the buffer and
5335 * we could crash.
Chris Masoneb14ab82011-02-10 12:35:00 -05005336 */
Chris Masond1310b22008-01-24 16:13:08 -05005337 }
5338 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05005339 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05005340again:
David Sterbae1860a72016-05-09 14:11:38 +02005341 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07005342 if (ret) {
5343 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04005344 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005345 }
Miao Xie19fe0a82010-10-26 20:57:29 -04005346
Josef Bacikf28491e2013-12-16 13:24:27 -05005347 spin_lock(&fs_info->buffer_lock);
5348 ret = radix_tree_insert(&fs_info->buffer_radix,
Qu Wenruo478ef882020-10-21 14:25:05 +08005349 start >> fs_info->sectorsize_bits, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05005350 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005351 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04005352 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005353 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005354 if (exists)
5355 goto free_eb;
5356 else
Josef Bacik115391d2012-03-09 09:51:43 -05005357 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04005358 }
Chris Mason6af118ce2008-07-22 11:18:07 -04005359 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005360 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005361 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005362
5363 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005364 * Now it's safe to unlock the pages because any calls to
5365 * btree_releasepage will correctly detect that a page belongs to a
5366 * live buffer and won't free them prematurely.
Chris Masoneb14ab82011-02-10 12:35:00 -05005367 */
Nikolay Borisov28187ae2018-07-04 10:24:51 +03005368 for (i = 0; i < num_pages; i++)
5369 unlock_page(eb->pages[i]);
Chris Masond1310b22008-01-24 16:13:08 -05005370 return eb;
5371
Chris Mason6af118ce2008-07-22 11:18:07 -04005372free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005373 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005374 for (i = 0; i < num_pages; i++) {
5375 if (eb->pages[i])
5376 unlock_page(eb->pages[i]);
5377 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005378
Miao Xie897ca6e92010-10-26 20:57:29 -04005379 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005380 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005381}
Chris Masond1310b22008-01-24 16:13:08 -05005382
Josef Bacik3083ee22012-03-09 16:01:49 -05005383static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5384{
5385 struct extent_buffer *eb =
5386 container_of(head, struct extent_buffer, rcu_head);
5387
5388 __free_extent_buffer(eb);
5389}
5390
David Sterbaf7a52a42013-04-26 14:56:29 +00005391static int release_extent_buffer(struct extent_buffer *eb)
Jules Irenge5ce48d02020-02-23 23:16:42 +00005392 __releases(&eb->refs_lock)
Josef Bacik3083ee22012-03-09 16:01:49 -05005393{
Nikolay Borisov07e21c42018-06-27 16:38:23 +03005394 lockdep_assert_held(&eb->refs_lock);
5395
Josef Bacik3083ee22012-03-09 16:01:49 -05005396 WARN_ON(atomic_read(&eb->refs) == 0);
5397 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005398 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005399 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005400
Jan Schmidt815a51c2012-05-16 17:00:02 +02005401 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005402
Josef Bacikf28491e2013-12-16 13:24:27 -05005403 spin_lock(&fs_info->buffer_lock);
5404 radix_tree_delete(&fs_info->buffer_radix,
Qu Wenruo478ef882020-10-21 14:25:05 +08005405 eb->start >> fs_info->sectorsize_bits);
Josef Bacikf28491e2013-12-16 13:24:27 -05005406 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005407 } else {
5408 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005409 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005410
Josef Bacik8c389382020-02-14 16:11:42 -05005411 btrfs_leak_debug_del(&eb->fs_info->eb_leak_lock, &eb->leak_list);
Josef Bacik3083ee22012-03-09 16:01:49 -05005412 /* Should be safe to release our pages at this point */
David Sterba55ac0132018-07-19 17:24:32 +02005413 btrfs_release_extent_buffer_pages(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005414#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005415 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
Josef Bacikbcb7e442015-03-16 17:38:02 -04005416 __free_extent_buffer(eb);
5417 return 1;
5418 }
5419#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005420 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005421 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005422 }
5423 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005424
5425 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005426}
5427
Chris Masond1310b22008-01-24 16:13:08 -05005428void free_extent_buffer(struct extent_buffer *eb)
5429{
Chris Mason242e18c2013-01-29 17:49:37 -05005430 int refs;
5431 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005432 if (!eb)
5433 return;
5434
Chris Mason242e18c2013-01-29 17:49:37 -05005435 while (1) {
5436 refs = atomic_read(&eb->refs);
Nikolay Borisov46cc7752018-10-15 17:04:01 +03005437 if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
5438 || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
5439 refs == 1))
Chris Mason242e18c2013-01-29 17:49:37 -05005440 break;
5441 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5442 if (old == refs)
5443 return;
5444 }
5445
Josef Bacik3083ee22012-03-09 16:01:49 -05005446 spin_lock(&eb->refs_lock);
5447 if (atomic_read(&eb->refs) == 2 &&
5448 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005449 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005450 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5451 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005452
Josef Bacik3083ee22012-03-09 16:01:49 -05005453 /*
5454 * I know this is terrible, but it's temporary until we stop tracking
5455 * the uptodate bits and such for the extent buffers.
5456 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005457 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005458}
Chris Masond1310b22008-01-24 16:13:08 -05005459
Josef Bacik3083ee22012-03-09 16:01:49 -05005460void free_extent_buffer_stale(struct extent_buffer *eb)
5461{
5462 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005463 return;
5464
Josef Bacik3083ee22012-03-09 16:01:49 -05005465 spin_lock(&eb->refs_lock);
5466 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5467
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005468 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005469 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5470 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005471 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005472}
5473
David Sterba2b489662020-04-29 03:04:10 +02005474void clear_extent_buffer_dirty(const struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005475{
David Sterbacc5e31a2018-03-01 18:20:27 +01005476 int i;
5477 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005478 struct page *page;
5479
David Sterba65ad0102018-06-29 10:56:49 +02005480 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005481
5482 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005483 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005484 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005485 continue;
5486
Chris Masona61e6f22008-07-22 11:18:08 -04005487 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005488 WARN_ON(!PagePrivate(page));
5489
Chris Masond1310b22008-01-24 16:13:08 -05005490 clear_page_dirty_for_io(page);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005491 xa_lock_irq(&page->mapping->i_pages);
Matthew Wilcox0a943c62017-12-04 10:37:22 -05005492 if (!PageDirty(page))
5493 __xa_clear_mark(&page->mapping->i_pages,
5494 page_index(page), PAGECACHE_TAG_DIRTY);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005495 xa_unlock_irq(&page->mapping->i_pages);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005496 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005497 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005498 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005499 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005500}
Chris Masond1310b22008-01-24 16:13:08 -05005501
Liu Boabb57ef2018-09-14 01:44:42 +08005502bool set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005503{
David Sterbacc5e31a2018-03-01 18:20:27 +01005504 int i;
5505 int num_pages;
Liu Boabb57ef2018-09-14 01:44:42 +08005506 bool was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005507
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005508 check_buffer_tree_ref(eb);
5509
Chris Masonb9473432009-03-13 11:00:37 -04005510 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005511
David Sterba65ad0102018-06-29 10:56:49 +02005512 num_pages = num_extent_pages(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05005513 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005514 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5515
Liu Boabb57ef2018-09-14 01:44:42 +08005516 if (!was_dirty)
5517 for (i = 0; i < num_pages; i++)
5518 set_page_dirty(eb->pages[i]);
Liu Bo51995c32018-09-14 01:46:08 +08005519
5520#ifdef CONFIG_BTRFS_DEBUG
5521 for (i = 0; i < num_pages; i++)
5522 ASSERT(PageDirty(eb->pages[i]));
5523#endif
5524
Chris Masonb9473432009-03-13 11:00:37 -04005525 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005526}
Chris Masond1310b22008-01-24 16:13:08 -05005527
David Sterba69ba3922015-12-03 13:08:59 +01005528void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005529{
David Sterbacc5e31a2018-03-01 18:20:27 +01005530 int i;
Chris Mason1259ab72008-05-12 13:39:03 -04005531 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005532 int num_pages;
Chris Mason1259ab72008-05-12 13:39:03 -04005533
Chris Masonb4ce94d2009-02-04 09:25:08 -05005534 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005535 num_pages = num_extent_pages(eb);
Chris Mason1259ab72008-05-12 13:39:03 -04005536 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005537 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005538 if (page)
5539 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005540 }
Chris Mason1259ab72008-05-12 13:39:03 -04005541}
5542
David Sterba09c25a82015-12-03 13:08:59 +01005543void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005544{
David Sterbacc5e31a2018-03-01 18:20:27 +01005545 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005546 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005547 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005548
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005549 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005550 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005551 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005552 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005553 SetPageUptodate(page);
5554 }
Chris Masond1310b22008-01-24 16:13:08 -05005555}
Chris Masond1310b22008-01-24 16:13:08 -05005556
Nikolay Borisovc2ccfbc2019-04-10 17:24:40 +03005557int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005558{
David Sterbacc5e31a2018-03-01 18:20:27 +01005559 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005560 struct page *page;
5561 int err;
5562 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005563 int locked_pages = 0;
5564 int all_uptodate = 1;
David Sterbacc5e31a2018-03-01 18:20:27 +01005565 int num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005566 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005567 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005568 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005569
Chris Masonb4ce94d2009-02-04 09:25:08 -05005570 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005571 return 0;
5572
David Sterba65ad0102018-06-29 10:56:49 +02005573 num_pages = num_extent_pages(eb);
Josef Bacik8436ea912016-09-02 15:40:03 -04005574 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005575 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005576 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005577 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005578 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005579 } else {
5580 lock_page(page);
5581 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005582 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005583 }
5584 /*
5585 * We need to firstly lock all pages to make sure that
5586 * the uptodate bit of our pages won't be affected by
5587 * clear_extent_buffer_uptodate().
5588 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005589 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005590 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005591 if (!PageUptodate(page)) {
5592 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005593 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005594 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005595 }
Liu Bo2571e732016-08-03 12:33:01 -07005596
Chris Masonce9adaa2008-04-09 16:28:12 -04005597 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005598 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005599 goto unlock_exit;
5600 }
5601
Filipe Manana656f30d2014-09-26 12:25:56 +01005602 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005603 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005604 atomic_set(&eb->io_pages, num_reads);
Boris Burkov6bf9cd22020-06-17 11:35:19 -07005605 /*
5606 * It is possible for releasepage to clear the TREE_REF bit before we
5607 * set io_pages. See check_buffer_tree_ref for a more detailed comment.
5608 */
5609 check_buffer_tree_ref(eb);
Josef Bacik8436ea912016-09-02 15:40:03 -04005610 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005611 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005612
Chris Masonce9adaa2008-04-09 16:28:12 -04005613 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005614 if (ret) {
5615 atomic_dec(&eb->io_pages);
5616 unlock_page(page);
5617 continue;
5618 }
5619
Chris Masonf1885912008-04-09 16:28:12 -04005620 ClearPageError(page);
Nikolay Borisov04201772020-09-14 12:37:04 +03005621 err = submit_extent_page(REQ_OP_READ | REQ_META, NULL,
5622 page, page_offset(page), PAGE_SIZE, 0,
5623 &bio, end_bio_extent_readpage,
5624 mirror_num, 0, 0, false);
Liu Bobaf863b2016-07-11 10:39:07 -07005625 if (err) {
Liu Bobaf863b2016-07-11 10:39:07 -07005626 /*
Nikolay Borisov04201772020-09-14 12:37:04 +03005627 * We failed to submit the bio so it's the
5628 * caller's responsibility to perform cleanup
5629 * i.e unlock page/set error bit.
Liu Bobaf863b2016-07-11 10:39:07 -07005630 */
Nikolay Borisov04201772020-09-14 12:37:04 +03005631 ret = err;
5632 SetPageError(page);
5633 unlock_page(page);
Liu Bobaf863b2016-07-11 10:39:07 -07005634 atomic_dec(&eb->io_pages);
5635 }
Chris Masond1310b22008-01-24 16:13:08 -05005636 } else {
5637 unlock_page(page);
5638 }
5639 }
5640
Jeff Mahoney355808c2011-10-03 23:23:14 -04005641 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005642 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005643 if (err)
5644 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005645 }
Chris Masona86c12c2008-02-07 10:50:54 -05005646
Arne Jansenbb82ab82011-06-10 14:06:53 +02005647 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005648 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005649
Josef Bacik8436ea912016-09-02 15:40:03 -04005650 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005651 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005652 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005653 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005654 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005655 }
Chris Masond3977122009-01-05 21:25:51 -05005656
Chris Masond1310b22008-01-24 16:13:08 -05005657 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005658
5659unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005660 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005661 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005662 page = eb->pages[locked_pages];
5663 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005664 }
5665 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005666}
Chris Masond1310b22008-01-24 16:13:08 -05005667
Qu Wenruof98b6212020-08-19 14:35:47 +08005668static bool report_eb_range(const struct extent_buffer *eb, unsigned long start,
5669 unsigned long len)
5670{
5671 btrfs_warn(eb->fs_info,
5672 "access to eb bytenr %llu len %lu out of range start %lu len %lu",
5673 eb->start, eb->len, start, len);
5674 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
5675
5676 return true;
5677}
5678
5679/*
5680 * Check if the [start, start + len) range is valid before reading/writing
5681 * the eb.
5682 * NOTE: @start and @len are offset inside the eb, not logical address.
5683 *
5684 * Caller should not touch the dst/src memory if this function returns error.
5685 */
5686static inline int check_eb_range(const struct extent_buffer *eb,
5687 unsigned long start, unsigned long len)
5688{
5689 unsigned long offset;
5690
5691 /* start, start + len should not go beyond eb->len nor overflow */
5692 if (unlikely(check_add_overflow(start, len, &offset) || offset > eb->len))
5693 return report_eb_range(eb, start, len);
5694
5695 return false;
5696}
5697
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005698void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5699 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005700{
5701 size_t cur;
5702 size_t offset;
5703 struct page *page;
5704 char *kaddr;
5705 char *dst = (char *)dstv;
Qu Wenruo884b07d2020-12-02 14:48:04 +08005706 unsigned long i = get_eb_page_index(start);
Chris Masond1310b22008-01-24 16:13:08 -05005707
Qu Wenruof98b6212020-08-19 14:35:47 +08005708 if (check_eb_range(eb, start, len))
Liu Bof716abd2017-08-09 11:10:16 -06005709 return;
Chris Masond1310b22008-01-24 16:13:08 -05005710
Qu Wenruo884b07d2020-12-02 14:48:04 +08005711 offset = get_eb_offset_in_page(eb, start);
Chris Masond1310b22008-01-24 16:13:08 -05005712
Chris Masond3977122009-01-05 21:25:51 -05005713 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005714 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005715
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005716 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005717 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005718 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005719
5720 dst += cur;
5721 len -= cur;
5722 offset = 0;
5723 i++;
5724 }
5725}
Chris Masond1310b22008-01-24 16:13:08 -05005726
Josef Bacika48b73e2020-08-10 11:42:27 -04005727int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb,
5728 void __user *dstv,
5729 unsigned long start, unsigned long len)
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005730{
5731 size_t cur;
5732 size_t offset;
5733 struct page *page;
5734 char *kaddr;
5735 char __user *dst = (char __user *)dstv;
Qu Wenruo884b07d2020-12-02 14:48:04 +08005736 unsigned long i = get_eb_page_index(start);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005737 int ret = 0;
5738
5739 WARN_ON(start > eb->len);
5740 WARN_ON(start + len > eb->start + eb->len);
5741
Qu Wenruo884b07d2020-12-02 14:48:04 +08005742 offset = get_eb_offset_in_page(eb, start);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005743
5744 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005745 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005746
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005747 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005748 kaddr = page_address(page);
Josef Bacika48b73e2020-08-10 11:42:27 -04005749 if (copy_to_user_nofault(dst, kaddr + offset, cur)) {
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005750 ret = -EFAULT;
5751 break;
5752 }
5753
5754 dst += cur;
5755 len -= cur;
5756 offset = 0;
5757 i++;
5758 }
5759
5760 return ret;
5761}
5762
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005763int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5764 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005765{
5766 size_t cur;
5767 size_t offset;
5768 struct page *page;
5769 char *kaddr;
5770 char *ptr = (char *)ptrv;
Qu Wenruo884b07d2020-12-02 14:48:04 +08005771 unsigned long i = get_eb_page_index(start);
Chris Masond1310b22008-01-24 16:13:08 -05005772 int ret = 0;
5773
Qu Wenruof98b6212020-08-19 14:35:47 +08005774 if (check_eb_range(eb, start, len))
5775 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05005776
Qu Wenruo884b07d2020-12-02 14:48:04 +08005777 offset = get_eb_offset_in_page(eb, start);
Chris Masond1310b22008-01-24 16:13:08 -05005778
Chris Masond3977122009-01-05 21:25:51 -05005779 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005780 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005781
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005782 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005783
Chris Masona6591712011-07-19 12:04:14 -04005784 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005785 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005786 if (ret)
5787 break;
5788
5789 ptr += cur;
5790 len -= cur;
5791 offset = 0;
5792 i++;
5793 }
5794 return ret;
5795}
Chris Masond1310b22008-01-24 16:13:08 -05005796
David Sterba2b489662020-04-29 03:04:10 +02005797void write_extent_buffer_chunk_tree_uuid(const struct extent_buffer *eb,
David Sterbaf157bf72016-11-09 17:43:38 +01005798 const void *srcv)
5799{
5800 char *kaddr;
5801
5802 WARN_ON(!PageUptodate(eb->pages[0]));
Qu Wenruo884b07d2020-12-02 14:48:04 +08005803 kaddr = page_address(eb->pages[0]) + get_eb_offset_in_page(eb, 0);
David Sterbaf157bf72016-11-09 17:43:38 +01005804 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5805 BTRFS_FSID_SIZE);
5806}
5807
David Sterba2b489662020-04-29 03:04:10 +02005808void write_extent_buffer_fsid(const struct extent_buffer *eb, const void *srcv)
David Sterbaf157bf72016-11-09 17:43:38 +01005809{
5810 char *kaddr;
5811
5812 WARN_ON(!PageUptodate(eb->pages[0]));
Qu Wenruo884b07d2020-12-02 14:48:04 +08005813 kaddr = page_address(eb->pages[0]) + get_eb_offset_in_page(eb, 0);
David Sterbaf157bf72016-11-09 17:43:38 +01005814 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5815 BTRFS_FSID_SIZE);
5816}
5817
David Sterba2b489662020-04-29 03:04:10 +02005818void write_extent_buffer(const struct extent_buffer *eb, const void *srcv,
Chris Masond1310b22008-01-24 16:13:08 -05005819 unsigned long start, unsigned long len)
5820{
5821 size_t cur;
5822 size_t offset;
5823 struct page *page;
5824 char *kaddr;
5825 char *src = (char *)srcv;
Qu Wenruo884b07d2020-12-02 14:48:04 +08005826 unsigned long i = get_eb_page_index(start);
Chris Masond1310b22008-01-24 16:13:08 -05005827
Qu Wenruof98b6212020-08-19 14:35:47 +08005828 if (check_eb_range(eb, start, len))
5829 return;
Chris Masond1310b22008-01-24 16:13:08 -05005830
Qu Wenruo884b07d2020-12-02 14:48:04 +08005831 offset = get_eb_offset_in_page(eb, start);
Chris Masond1310b22008-01-24 16:13:08 -05005832
Chris Masond3977122009-01-05 21:25:51 -05005833 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005834 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005835 WARN_ON(!PageUptodate(page));
5836
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005837 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005838 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005839 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005840
5841 src += cur;
5842 len -= cur;
5843 offset = 0;
5844 i++;
5845 }
5846}
Chris Masond1310b22008-01-24 16:13:08 -05005847
David Sterba2b489662020-04-29 03:04:10 +02005848void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start,
David Sterbab159fa22016-11-08 18:09:03 +01005849 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005850{
5851 size_t cur;
5852 size_t offset;
5853 struct page *page;
5854 char *kaddr;
Qu Wenruo884b07d2020-12-02 14:48:04 +08005855 unsigned long i = get_eb_page_index(start);
Chris Masond1310b22008-01-24 16:13:08 -05005856
Qu Wenruof98b6212020-08-19 14:35:47 +08005857 if (check_eb_range(eb, start, len))
5858 return;
Chris Masond1310b22008-01-24 16:13:08 -05005859
Qu Wenruo884b07d2020-12-02 14:48:04 +08005860 offset = get_eb_offset_in_page(eb, start);
Chris Masond1310b22008-01-24 16:13:08 -05005861
Chris Masond3977122009-01-05 21:25:51 -05005862 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005863 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005864 WARN_ON(!PageUptodate(page));
5865
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005866 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005867 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005868 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005869
5870 len -= cur;
5871 offset = 0;
5872 i++;
5873 }
5874}
Chris Masond1310b22008-01-24 16:13:08 -05005875
David Sterba2b489662020-04-29 03:04:10 +02005876void copy_extent_buffer_full(const struct extent_buffer *dst,
5877 const struct extent_buffer *src)
David Sterba58e80122016-11-08 18:30:31 +01005878{
5879 int i;
David Sterbacc5e31a2018-03-01 18:20:27 +01005880 int num_pages;
David Sterba58e80122016-11-08 18:30:31 +01005881
5882 ASSERT(dst->len == src->len);
5883
Qu Wenruo884b07d2020-12-02 14:48:04 +08005884 if (dst->fs_info->sectorsize == PAGE_SIZE) {
5885 num_pages = num_extent_pages(dst);
5886 for (i = 0; i < num_pages; i++)
5887 copy_page(page_address(dst->pages[i]),
5888 page_address(src->pages[i]));
5889 } else {
5890 size_t src_offset = get_eb_offset_in_page(src, 0);
5891 size_t dst_offset = get_eb_offset_in_page(dst, 0);
5892
5893 ASSERT(src->fs_info->sectorsize < PAGE_SIZE);
5894 memcpy(page_address(dst->pages[0]) + dst_offset,
5895 page_address(src->pages[0]) + src_offset,
5896 src->len);
5897 }
David Sterba58e80122016-11-08 18:30:31 +01005898}
5899
David Sterba2b489662020-04-29 03:04:10 +02005900void copy_extent_buffer(const struct extent_buffer *dst,
5901 const struct extent_buffer *src,
Chris Masond1310b22008-01-24 16:13:08 -05005902 unsigned long dst_offset, unsigned long src_offset,
5903 unsigned long len)
5904{
5905 u64 dst_len = dst->len;
5906 size_t cur;
5907 size_t offset;
5908 struct page *page;
5909 char *kaddr;
Qu Wenruo884b07d2020-12-02 14:48:04 +08005910 unsigned long i = get_eb_page_index(dst_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005911
Qu Wenruof98b6212020-08-19 14:35:47 +08005912 if (check_eb_range(dst, dst_offset, len) ||
5913 check_eb_range(src, src_offset, len))
5914 return;
5915
Chris Masond1310b22008-01-24 16:13:08 -05005916 WARN_ON(src->len != dst_len);
5917
Qu Wenruo884b07d2020-12-02 14:48:04 +08005918 offset = get_eb_offset_in_page(dst, dst_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005919
Chris Masond3977122009-01-05 21:25:51 -05005920 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005921 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005922 WARN_ON(!PageUptodate(page));
5923
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005924 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005925
Chris Masona6591712011-07-19 12:04:14 -04005926 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005927 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005928
5929 src_offset += cur;
5930 len -= cur;
5931 offset = 0;
5932 i++;
5933 }
5934}
Chris Masond1310b22008-01-24 16:13:08 -05005935
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005936/*
5937 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5938 * given bit number
5939 * @eb: the extent buffer
5940 * @start: offset of the bitmap item in the extent buffer
5941 * @nr: bit number
5942 * @page_index: return index of the page in the extent buffer that contains the
5943 * given bit number
5944 * @page_offset: return offset into the page given by page_index
5945 *
5946 * This helper hides the ugliness of finding the byte in an extent buffer which
5947 * contains a given bit.
5948 */
David Sterba2b489662020-04-29 03:04:10 +02005949static inline void eb_bitmap_offset(const struct extent_buffer *eb,
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005950 unsigned long start, unsigned long nr,
5951 unsigned long *page_index,
5952 size_t *page_offset)
5953{
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005954 size_t byte_offset = BIT_BYTE(nr);
5955 size_t offset;
5956
5957 /*
5958 * The byte we want is the offset of the extent buffer + the offset of
5959 * the bitmap item in the extent buffer + the offset of the byte in the
5960 * bitmap item.
5961 */
Qu Wenruo884b07d2020-12-02 14:48:04 +08005962 offset = start + offset_in_page(eb->start) + byte_offset;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005963
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005964 *page_index = offset >> PAGE_SHIFT;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005965 *page_offset = offset_in_page(offset);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005966}
5967
5968/**
5969 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5970 * @eb: the extent buffer
5971 * @start: offset of the bitmap item in the extent buffer
5972 * @nr: bit number to test
5973 */
David Sterba2b489662020-04-29 03:04:10 +02005974int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start,
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005975 unsigned long nr)
5976{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005977 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005978 struct page *page;
5979 unsigned long i;
5980 size_t offset;
5981
5982 eb_bitmap_offset(eb, start, nr, &i, &offset);
5983 page = eb->pages[i];
5984 WARN_ON(!PageUptodate(page));
5985 kaddr = page_address(page);
5986 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5987}
5988
5989/**
5990 * extent_buffer_bitmap_set - set an area of a bitmap
5991 * @eb: the extent buffer
5992 * @start: offset of the bitmap item in the extent buffer
5993 * @pos: bit number of the first bit
5994 * @len: number of bits to set
5995 */
David Sterba2b489662020-04-29 03:04:10 +02005996void extent_buffer_bitmap_set(const struct extent_buffer *eb, unsigned long start,
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005997 unsigned long pos, unsigned long len)
5998{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005999 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07006000 struct page *page;
6001 unsigned long i;
6002 size_t offset;
6003 const unsigned int size = pos + len;
6004 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07006005 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07006006
6007 eb_bitmap_offset(eb, start, pos, &i, &offset);
6008 page = eb->pages[i];
6009 WARN_ON(!PageUptodate(page));
6010 kaddr = page_address(page);
6011
6012 while (len >= bits_to_set) {
6013 kaddr[offset] |= mask_to_set;
6014 len -= bits_to_set;
6015 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03006016 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006017 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07006018 offset = 0;
6019 page = eb->pages[++i];
6020 WARN_ON(!PageUptodate(page));
6021 kaddr = page_address(page);
6022 }
6023 }
6024 if (len) {
6025 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
6026 kaddr[offset] |= mask_to_set;
6027 }
6028}
6029
6030
6031/**
6032 * extent_buffer_bitmap_clear - clear an area of a bitmap
6033 * @eb: the extent buffer
6034 * @start: offset of the bitmap item in the extent buffer
6035 * @pos: bit number of the first bit
6036 * @len: number of bits to clear
6037 */
David Sterba2b489662020-04-29 03:04:10 +02006038void extent_buffer_bitmap_clear(const struct extent_buffer *eb,
6039 unsigned long start, unsigned long pos,
6040 unsigned long len)
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07006041{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07006042 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07006043 struct page *page;
6044 unsigned long i;
6045 size_t offset;
6046 const unsigned int size = pos + len;
6047 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07006048 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07006049
6050 eb_bitmap_offset(eb, start, pos, &i, &offset);
6051 page = eb->pages[i];
6052 WARN_ON(!PageUptodate(page));
6053 kaddr = page_address(page);
6054
6055 while (len >= bits_to_clear) {
6056 kaddr[offset] &= ~mask_to_clear;
6057 len -= bits_to_clear;
6058 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03006059 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006060 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07006061 offset = 0;
6062 page = eb->pages[++i];
6063 WARN_ON(!PageUptodate(page));
6064 kaddr = page_address(page);
6065 }
6066 }
6067 if (len) {
6068 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
6069 kaddr[offset] &= ~mask_to_clear;
6070 }
6071}
6072
Sergei Trofimovich33872062011-04-11 21:52:52 +00006073static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
6074{
6075 unsigned long distance = (src > dst) ? src - dst : dst - src;
6076 return distance < len;
6077}
6078
Chris Masond1310b22008-01-24 16:13:08 -05006079static void copy_pages(struct page *dst_page, struct page *src_page,
6080 unsigned long dst_off, unsigned long src_off,
6081 unsigned long len)
6082{
Chris Masona6591712011-07-19 12:04:14 -04006083 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05006084 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04006085 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05006086
Sergei Trofimovich33872062011-04-11 21:52:52 +00006087 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04006088 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00006089 } else {
Chris Masond1310b22008-01-24 16:13:08 -05006090 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04006091 if (areas_overlap(src_off, dst_off, len))
6092 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00006093 }
Chris Masond1310b22008-01-24 16:13:08 -05006094
Chris Mason727011e2010-08-06 13:21:20 -04006095 if (must_memmove)
6096 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
6097 else
6098 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05006099}
6100
David Sterba2b489662020-04-29 03:04:10 +02006101void memcpy_extent_buffer(const struct extent_buffer *dst,
6102 unsigned long dst_offset, unsigned long src_offset,
6103 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05006104{
6105 size_t cur;
6106 size_t dst_off_in_page;
6107 size_t src_off_in_page;
Chris Masond1310b22008-01-24 16:13:08 -05006108 unsigned long dst_i;
6109 unsigned long src_i;
6110
Qu Wenruof98b6212020-08-19 14:35:47 +08006111 if (check_eb_range(dst, dst_offset, len) ||
6112 check_eb_range(dst, src_offset, len))
6113 return;
Chris Masond1310b22008-01-24 16:13:08 -05006114
Chris Masond3977122009-01-05 21:25:51 -05006115 while (len > 0) {
Qu Wenruo884b07d2020-12-02 14:48:04 +08006116 dst_off_in_page = get_eb_offset_in_page(dst, dst_offset);
6117 src_off_in_page = get_eb_offset_in_page(dst, src_offset);
Chris Masond1310b22008-01-24 16:13:08 -05006118
Qu Wenruo884b07d2020-12-02 14:48:04 +08006119 dst_i = get_eb_page_index(dst_offset);
6120 src_i = get_eb_page_index(src_offset);
Chris Masond1310b22008-01-24 16:13:08 -05006121
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006122 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05006123 src_off_in_page));
6124 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006125 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05006126
David Sterbafb85fc92014-07-31 01:03:53 +02006127 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05006128 dst_off_in_page, src_off_in_page, cur);
6129
6130 src_offset += cur;
6131 dst_offset += cur;
6132 len -= cur;
6133 }
6134}
Chris Masond1310b22008-01-24 16:13:08 -05006135
David Sterba2b489662020-04-29 03:04:10 +02006136void memmove_extent_buffer(const struct extent_buffer *dst,
6137 unsigned long dst_offset, unsigned long src_offset,
6138 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05006139{
6140 size_t cur;
6141 size_t dst_off_in_page;
6142 size_t src_off_in_page;
6143 unsigned long dst_end = dst_offset + len - 1;
6144 unsigned long src_end = src_offset + len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05006145 unsigned long dst_i;
6146 unsigned long src_i;
6147
Qu Wenruof98b6212020-08-19 14:35:47 +08006148 if (check_eb_range(dst, dst_offset, len) ||
6149 check_eb_range(dst, src_offset, len))
6150 return;
Chris Mason727011e2010-08-06 13:21:20 -04006151 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05006152 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
6153 return;
6154 }
Chris Masond3977122009-01-05 21:25:51 -05006155 while (len > 0) {
Qu Wenruo884b07d2020-12-02 14:48:04 +08006156 dst_i = get_eb_page_index(dst_end);
6157 src_i = get_eb_page_index(src_end);
Chris Masond1310b22008-01-24 16:13:08 -05006158
Qu Wenruo884b07d2020-12-02 14:48:04 +08006159 dst_off_in_page = get_eb_offset_in_page(dst, dst_end);
6160 src_off_in_page = get_eb_offset_in_page(dst, src_end);
Chris Masond1310b22008-01-24 16:13:08 -05006161
6162 cur = min_t(unsigned long, len, src_off_in_page + 1);
6163 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02006164 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05006165 dst_off_in_page - cur + 1,
6166 src_off_in_page - cur + 1, cur);
6167
6168 dst_end -= cur;
6169 src_end -= cur;
6170 len -= cur;
6171 }
6172}
Chris Mason6af118ce2008-07-22 11:18:07 -04006173
David Sterbaf7a52a42013-04-26 14:56:29 +00006174int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04006175{
Chris Mason6af118ce2008-07-22 11:18:07 -04006176 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04006177
Miao Xie19fe0a82010-10-26 20:57:29 -04006178 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006179 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05006180 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04006181 */
Josef Bacik3083ee22012-03-09 16:01:49 -05006182 spin_lock(&page->mapping->private_lock);
6183 if (!PagePrivate(page)) {
6184 spin_unlock(&page->mapping->private_lock);
6185 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04006186 }
6187
Josef Bacik3083ee22012-03-09 16:01:49 -05006188 eb = (struct extent_buffer *)page->private;
6189 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04006190
Josef Bacik0b32f4b2012-03-13 09:38:00 -04006191 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05006192 * This is a little awful but should be ok, we need to make sure that
6193 * the eb doesn't disappear out from under us while we're looking at
6194 * this page.
6195 */
6196 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04006197 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05006198 spin_unlock(&eb->refs_lock);
6199 spin_unlock(&page->mapping->private_lock);
6200 return 0;
6201 }
6202 spin_unlock(&page->mapping->private_lock);
6203
Josef Bacik3083ee22012-03-09 16:01:49 -05006204 /*
6205 * If tree ref isn't set then we know the ref on this eb is a real ref,
6206 * so just return, this page will likely be freed soon anyway.
6207 */
6208 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
6209 spin_unlock(&eb->refs_lock);
6210 return 0;
6211 }
Josef Bacik3083ee22012-03-09 16:01:49 -05006212
David Sterbaf7a52a42013-04-26 14:56:29 +00006213 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04006214}
Josef Bacikbfb484d2020-11-05 10:45:09 -05006215
6216/*
6217 * btrfs_readahead_tree_block - attempt to readahead a child block
6218 * @fs_info: the fs_info
6219 * @bytenr: bytenr to read
Josef Bacik3fbaf252020-11-05 10:45:20 -05006220 * @owner_root: objectid of the root that owns this eb
Josef Bacikbfb484d2020-11-05 10:45:09 -05006221 * @gen: generation for the uptodate check, can be 0
Josef Bacik3fbaf252020-11-05 10:45:20 -05006222 * @level: level for the eb
Josef Bacikbfb484d2020-11-05 10:45:09 -05006223 *
6224 * Attempt to readahead a tree block at @bytenr. If @gen is 0 then we do a
6225 * normal uptodate check of the eb, without checking the generation. If we have
6226 * to read the block we will not block on anything.
6227 */
6228void btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info,
Josef Bacik3fbaf252020-11-05 10:45:20 -05006229 u64 bytenr, u64 owner_root, u64 gen, int level)
Josef Bacikbfb484d2020-11-05 10:45:09 -05006230{
6231 struct extent_buffer *eb;
6232 int ret;
6233
Josef Bacik3fbaf252020-11-05 10:45:20 -05006234 eb = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level);
Josef Bacikbfb484d2020-11-05 10:45:09 -05006235 if (IS_ERR(eb))
6236 return;
6237
6238 if (btrfs_buffer_uptodate(eb, gen, 1)) {
6239 free_extent_buffer(eb);
6240 return;
6241 }
6242
6243 ret = read_extent_buffer_pages(eb, WAIT_NONE, 0);
6244 if (ret < 0)
6245 free_extent_buffer_stale(eb);
6246 else
6247 free_extent_buffer(eb);
6248}
6249
6250/*
6251 * btrfs_readahead_node_child - readahead a node's child block
6252 * @node: parent node we're reading from
6253 * @slot: slot in the parent node for the child we want to read
6254 *
6255 * A helper for btrfs_readahead_tree_block, we simply read the bytenr pointed at
6256 * the slot in the node provided.
6257 */
6258void btrfs_readahead_node_child(struct extent_buffer *node, int slot)
6259{
6260 btrfs_readahead_tree_block(node->fs_info,
6261 btrfs_node_blockptr(node, slot),
Josef Bacik3fbaf252020-11-05 10:45:20 -05006262 btrfs_header_owner(node),
6263 btrfs_node_ptr_generation(node, slot),
6264 btrfs_header_level(node) - 1);
Josef Bacikbfb484d2020-11-05 10:45:09 -05006265}