blob: 4e550a3e7e34a84563561aa2bfdec4d7502124e1 [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(buffers);
39static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040040
Chris Masond3977122009-01-05 21:25:51 -050041static DEFINE_SPINLOCK(leak_lock);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000042
43static inline
44void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
45{
46 unsigned long flags;
47
48 spin_lock_irqsave(&leak_lock, flags);
49 list_add(new, head);
50 spin_unlock_irqrestore(&leak_lock, flags);
51}
52
53static inline
54void btrfs_leak_debug_del(struct list_head *entry)
55{
56 unsigned long flags;
57
58 spin_lock_irqsave(&leak_lock, flags);
59 list_del(entry);
60 spin_unlock_irqrestore(&leak_lock, flags);
61}
62
Josef Bacik33ca8322019-09-23 10:05:17 -040063static inline void btrfs_extent_buffer_leak_debug_check(void)
64{
65 struct extent_buffer *eb;
66
67 while (!list_empty(&buffers)) {
68 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
69 pr_err("BTRFS: buffer leak start %llu len %lu refs %d bflags %lu\n",
70 eb->start, eb->len, atomic_read(&eb->refs), eb->bflags);
71 list_del(&eb->leak_list);
72 kmem_cache_free(extent_buffer_cache, eb);
73 }
74}
75
76static inline void btrfs_extent_state_leak_debug_check(void)
Eric Sandeen6d49ba12013-04-22 16:12:31 +000077{
78 struct extent_state *state;
Eric Sandeen6d49ba12013-04-22 16:12:31 +000079
80 while (!list_empty(&states)) {
81 state = list_entry(states.next, struct extent_state, leak_list);
David Sterba9ee49a042015-01-14 19:52:13 +010082 pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
Filipe Manana27a35072014-07-06 20:09:59 +010083 state->start, state->end, state->state,
84 extent_state_in_tree(state),
Elena Reshetovab7ac31b2017-03-03 10:55:19 +020085 refcount_read(&state->refs));
Eric Sandeen6d49ba12013-04-22 16:12:31 +000086 list_del(&state->leak_list);
87 kmem_cache_free(extent_state_cache, state);
88 }
Eric Sandeen6d49ba12013-04-22 16:12:31 +000089}
David Sterba8d599ae2013-04-30 15:22:23 +000090
Josef Bacika5dee372013-12-13 10:02:44 -050091#define btrfs_debug_check_extent_io_range(tree, start, end) \
92 __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
David Sterba8d599ae2013-04-30 15:22:23 +000093static inline void __btrfs_debug_check_extent_io_range(const char *caller,
Josef Bacika5dee372013-12-13 10:02:44 -050094 struct extent_io_tree *tree, u64 start, u64 end)
David Sterba8d599ae2013-04-30 15:22:23 +000095{
Nikolay Borisov65a680f2018-11-01 14:09:49 +020096 struct inode *inode = tree->private_data;
97 u64 isize;
98
99 if (!inode || !is_data_inode(inode))
100 return;
101
102 isize = i_size_read(inode);
103 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
104 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
105 "%s: ino %llu isize %llu odd range [%llu,%llu]",
106 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
107 }
David Sterba8d599ae2013-04-30 15:22:23 +0000108}
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000109#else
110#define btrfs_leak_debug_add(new, head) do {} while (0)
111#define btrfs_leak_debug_del(entry) do {} while (0)
Josef Bacik33ca8322019-09-23 10:05:17 -0400112#define btrfs_extent_buffer_leak_debug_check() do {} while (0)
113#define btrfs_extent_state_leak_debug_check() do {} while (0)
David Sterba8d599ae2013-04-30 15:22:23 +0000114#define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
Chris Mason4bef0842008-09-08 11:18:08 -0400115#endif
Chris Masond1310b22008-01-24 16:13:08 -0500116
Chris Masond1310b22008-01-24 16:13:08 -0500117struct tree_entry {
118 u64 start;
119 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -0500120 struct rb_node rb_node;
121};
122
123struct extent_page_data {
124 struct bio *bio;
125 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -0500126 /* tells writepage not to lock the state bits for this range
127 * it still does the unlocking
128 */
Chris Masonffbd5172009-04-20 15:50:09 -0400129 unsigned int extent_locked:1;
130
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600131 /* tells the submit_bio code to use REQ_SYNC */
Chris Masonffbd5172009-04-20 15:50:09 -0400132 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -0500133};
134
David Sterba57599c72018-03-01 17:56:34 +0100135static int add_extent_changeset(struct extent_state *state, unsigned bits,
Qu Wenruod38ed272015-10-12 14:53:37 +0800136 struct extent_changeset *changeset,
137 int set)
138{
139 int ret;
140
141 if (!changeset)
David Sterba57599c72018-03-01 17:56:34 +0100142 return 0;
Qu Wenruod38ed272015-10-12 14:53:37 +0800143 if (set && (state->state & bits) == bits)
David Sterba57599c72018-03-01 17:56:34 +0100144 return 0;
Qu Wenruofefdc552015-10-12 15:35:38 +0800145 if (!set && (state->state & bits) == 0)
David Sterba57599c72018-03-01 17:56:34 +0100146 return 0;
Qu Wenruod38ed272015-10-12 14:53:37 +0800147 changeset->bytes_changed += state->end - state->start + 1;
David Sterba53d32352017-02-13 13:42:29 +0100148 ret = ulist_add(&changeset->range_changed, state->start, state->end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800149 GFP_ATOMIC);
David Sterba57599c72018-03-01 17:56:34 +0100150 return ret;
Qu Wenruod38ed272015-10-12 14:53:37 +0800151}
152
Qu Wenruobb58eb92019-01-25 13:09:15 +0800153static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
154 unsigned long bio_flags)
155{
156 blk_status_t ret = 0;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800157 struct extent_io_tree *tree = bio->bi_private;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800158
159 bio->bi_private = NULL;
160
161 if (tree->ops)
162 ret = tree->ops->submit_bio_hook(tree->private_data, bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +0300163 mirror_num, bio_flags);
Qu Wenruobb58eb92019-01-25 13:09:15 +0800164 else
165 btrfsic_submit_bio(bio);
166
167 return blk_status_to_errno(ret);
168}
169
Qu Wenruo30659762019-03-20 14:27:42 +0800170/* Cleanup unsubmitted bios */
171static void end_write_bio(struct extent_page_data *epd, int ret)
172{
173 if (epd->bio) {
174 epd->bio->bi_status = errno_to_blk_status(ret);
175 bio_endio(epd->bio);
176 epd->bio = NULL;
177 }
178}
179
Qu Wenruof4340622019-03-20 14:27:41 +0800180/*
181 * Submit bio from extent page data via submit_one_bio
182 *
183 * Return 0 if everything is OK.
184 * Return <0 for error.
185 */
186static int __must_check flush_write_bio(struct extent_page_data *epd)
Qu Wenruobb58eb92019-01-25 13:09:15 +0800187{
Qu Wenruof4340622019-03-20 14:27:41 +0800188 int ret = 0;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800189
Qu Wenruof4340622019-03-20 14:27:41 +0800190 if (epd->bio) {
Qu Wenruobb58eb92019-01-25 13:09:15 +0800191 ret = submit_one_bio(epd->bio, 0, 0);
Qu Wenruof4340622019-03-20 14:27:41 +0800192 /*
193 * Clean up of epd->bio is handled by its endio function.
194 * And endio is either triggered by successful bio execution
195 * or the error handler of submit bio hook.
196 * So at this point, no matter what happened, we don't need
197 * to clean up epd->bio.
198 */
Qu Wenruobb58eb92019-01-25 13:09:15 +0800199 epd->bio = NULL;
200 }
Qu Wenruof4340622019-03-20 14:27:41 +0800201 return ret;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800202}
David Sterbae2932ee2017-06-23 04:16:17 +0200203
Josef Bacik6f0d04f2019-09-23 10:05:18 -0400204int __init extent_state_cache_init(void)
Chris Masond1310b22008-01-24 16:13:08 -0500205{
David Sterba837e1972012-09-07 03:00:48 -0600206 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200207 sizeof(struct extent_state), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300208 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500209 if (!extent_state_cache)
210 return -ENOMEM;
Josef Bacik6f0d04f2019-09-23 10:05:18 -0400211 return 0;
212}
Chris Masond1310b22008-01-24 16:13:08 -0500213
Josef Bacik6f0d04f2019-09-23 10:05:18 -0400214int __init extent_io_init(void)
215{
David Sterba837e1972012-09-07 03:00:48 -0600216 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200217 sizeof(struct extent_buffer), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300218 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500219 if (!extent_buffer_cache)
Josef Bacik6f0d04f2019-09-23 10:05:18 -0400220 return -ENOMEM;
Chris Mason9be33952013-05-17 18:30:14 -0400221
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400222 if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
223 offsetof(struct btrfs_io_bio, bio),
224 BIOSET_NEED_BVECS))
Chris Mason9be33952013-05-17 18:30:14 -0400225 goto free_buffer_cache;
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700226
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400227 if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE))
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700228 goto free_bioset;
229
Chris Masond1310b22008-01-24 16:13:08 -0500230 return 0;
231
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700232free_bioset:
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400233 bioset_exit(&btrfs_bioset);
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700234
Chris Mason9be33952013-05-17 18:30:14 -0400235free_buffer_cache:
236 kmem_cache_destroy(extent_buffer_cache);
237 extent_buffer_cache = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500238 return -ENOMEM;
239}
240
Josef Bacik6f0d04f2019-09-23 10:05:18 -0400241void __cold extent_state_cache_exit(void)
242{
243 btrfs_extent_state_leak_debug_check();
244 kmem_cache_destroy(extent_state_cache);
245}
246
David Sterbae67c7182018-02-19 17:24:18 +0100247void __cold extent_io_exit(void)
Chris Masond1310b22008-01-24 16:13:08 -0500248{
Josef Bacik33ca8322019-09-23 10:05:17 -0400249 btrfs_extent_buffer_leak_debug_check();
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000250
251 /*
252 * Make sure all delayed rcu free are flushed before we
253 * destroy caches.
254 */
255 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +0800256 kmem_cache_destroy(extent_buffer_cache);
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400257 bioset_exit(&btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -0500258}
259
Qu Wenruoc258d6e2019-03-01 10:47:58 +0800260void extent_io_tree_init(struct btrfs_fs_info *fs_info,
Qu Wenruo43eb5f22019-03-01 10:47:59 +0800261 struct extent_io_tree *tree, unsigned int owner,
262 void *private_data)
Chris Masond1310b22008-01-24 16:13:08 -0500263{
Qu Wenruoc258d6e2019-03-01 10:47:58 +0800264 tree->fs_info = fs_info;
Eric Paris6bef4d32010-02-23 19:43:04 +0000265 tree->state = RB_ROOT;
Chris Masond1310b22008-01-24 16:13:08 -0500266 tree->ops = NULL;
267 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500268 spin_lock_init(&tree->lock);
Josef Bacikc6100a42017-05-05 11:57:13 -0400269 tree->private_data = private_data;
Qu Wenruo43eb5f22019-03-01 10:47:59 +0800270 tree->owner = owner;
Chris Masond1310b22008-01-24 16:13:08 -0500271}
Chris Masond1310b22008-01-24 16:13:08 -0500272
Nikolay Borisov41e7acd2019-03-25 14:31:24 +0200273void extent_io_tree_release(struct extent_io_tree *tree)
274{
275 spin_lock(&tree->lock);
276 /*
277 * Do a single barrier for the waitqueue_active check here, the state
278 * of the waitqueue should not change once extent_io_tree_release is
279 * called.
280 */
281 smp_mb();
282 while (!RB_EMPTY_ROOT(&tree->state)) {
283 struct rb_node *node;
284 struct extent_state *state;
285
286 node = rb_first(&tree->state);
287 state = rb_entry(node, struct extent_state, rb_node);
288 rb_erase(&state->rb_node, &tree->state);
289 RB_CLEAR_NODE(&state->rb_node);
290 /*
291 * btree io trees aren't supposed to have tasks waiting for
292 * changes in the flags of extent states ever.
293 */
294 ASSERT(!waitqueue_active(&state->wq));
295 free_extent_state(state);
296
297 cond_resched_lock(&tree->lock);
298 }
299 spin_unlock(&tree->lock);
300}
301
Christoph Hellwigb2950862008-12-02 09:54:17 -0500302static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500303{
304 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500305
Michal Hocko3ba7ab22017-01-09 15:39:02 +0100306 /*
307 * The given mask might be not appropriate for the slab allocator,
308 * drop the unsupported bits
309 */
310 mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
Chris Masond1310b22008-01-24 16:13:08 -0500311 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400312 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500313 return state;
314 state->state = 0;
David Sterba47dc1962016-02-11 13:24:13 +0100315 state->failrec = NULL;
Filipe Manana27a35072014-07-06 20:09:59 +0100316 RB_CLEAR_NODE(&state->rb_node);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000317 btrfs_leak_debug_add(&state->leak_list, &states);
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200318 refcount_set(&state->refs, 1);
Chris Masond1310b22008-01-24 16:13:08 -0500319 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100320 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500321 return state;
322}
Chris Masond1310b22008-01-24 16:13:08 -0500323
Chris Mason4845e442010-05-25 20:56:50 -0400324void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500325{
Chris Masond1310b22008-01-24 16:13:08 -0500326 if (!state)
327 return;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200328 if (refcount_dec_and_test(&state->refs)) {
Filipe Manana27a35072014-07-06 20:09:59 +0100329 WARN_ON(extent_state_in_tree(state));
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000330 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100331 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500332 kmem_cache_free(extent_state_cache, state);
333 }
334}
Chris Masond1310b22008-01-24 16:13:08 -0500335
Filipe Mananaf2071b22014-02-12 15:05:53 +0000336static struct rb_node *tree_insert(struct rb_root *root,
337 struct rb_node *search_start,
338 u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000339 struct rb_node *node,
340 struct rb_node ***p_in,
341 struct rb_node **parent_in)
Chris Masond1310b22008-01-24 16:13:08 -0500342{
Filipe Mananaf2071b22014-02-12 15:05:53 +0000343 struct rb_node **p;
Chris Masond3977122009-01-05 21:25:51 -0500344 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500345 struct tree_entry *entry;
346
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000347 if (p_in && parent_in) {
348 p = *p_in;
349 parent = *parent_in;
350 goto do_insert;
351 }
352
Filipe Mananaf2071b22014-02-12 15:05:53 +0000353 p = search_start ? &search_start : &root->rb_node;
Chris Masond3977122009-01-05 21:25:51 -0500354 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500355 parent = *p;
356 entry = rb_entry(parent, struct tree_entry, rb_node);
357
358 if (offset < entry->start)
359 p = &(*p)->rb_left;
360 else if (offset > entry->end)
361 p = &(*p)->rb_right;
362 else
363 return parent;
364 }
365
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000366do_insert:
Chris Masond1310b22008-01-24 16:13:08 -0500367 rb_link_node(node, parent, p);
368 rb_insert_color(node, root);
369 return NULL;
370}
371
Nikolay Borisov8666e632019-06-05 14:50:04 +0300372/**
373 * __etree_search - searche @tree for an entry that contains @offset. Such
374 * entry would have entry->start <= offset && entry->end >= offset.
375 *
376 * @tree - the tree to search
377 * @offset - offset that should fall within an entry in @tree
378 * @next_ret - pointer to the first entry whose range ends after @offset
379 * @prev - pointer to the first entry whose range begins before @offset
380 * @p_ret - pointer where new node should be anchored (used when inserting an
381 * entry in the tree)
382 * @parent_ret - points to entry which would have been the parent of the entry,
383 * containing @offset
384 *
385 * This function returns a pointer to the entry that contains @offset byte
386 * address. If no such entry exists, then NULL is returned and the other
387 * pointer arguments to the function are filled, otherwise the found entry is
388 * returned and other pointers are left untouched.
389 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500390static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000391 struct rb_node **next_ret,
Nikolay Borisov352646c2019-01-30 16:51:00 +0200392 struct rb_node **prev_ret,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000393 struct rb_node ***p_ret,
394 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500395{
Chris Mason80ea96b2008-02-01 14:51:59 -0500396 struct rb_root *root = &tree->state;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000397 struct rb_node **n = &root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500398 struct rb_node *prev = NULL;
399 struct rb_node *orig_prev = NULL;
400 struct tree_entry *entry;
401 struct tree_entry *prev_entry = NULL;
402
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000403 while (*n) {
404 prev = *n;
405 entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500406 prev_entry = entry;
407
408 if (offset < entry->start)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000409 n = &(*n)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500410 else if (offset > entry->end)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000411 n = &(*n)->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500412 else
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000413 return *n;
Chris Masond1310b22008-01-24 16:13:08 -0500414 }
415
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000416 if (p_ret)
417 *p_ret = n;
418 if (parent_ret)
419 *parent_ret = prev;
420
Nikolay Borisov352646c2019-01-30 16:51:00 +0200421 if (next_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500422 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500423 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500424 prev = rb_next(prev);
425 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
426 }
Nikolay Borisov352646c2019-01-30 16:51:00 +0200427 *next_ret = prev;
Chris Masond1310b22008-01-24 16:13:08 -0500428 prev = orig_prev;
429 }
430
Nikolay Borisov352646c2019-01-30 16:51:00 +0200431 if (prev_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500432 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500433 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500434 prev = rb_prev(prev);
435 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
436 }
Nikolay Borisov352646c2019-01-30 16:51:00 +0200437 *prev_ret = prev;
Chris Masond1310b22008-01-24 16:13:08 -0500438 }
439 return NULL;
440}
441
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000442static inline struct rb_node *
443tree_search_for_insert(struct extent_io_tree *tree,
444 u64 offset,
445 struct rb_node ***p_ret,
446 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500447{
Nikolay Borisov352646c2019-01-30 16:51:00 +0200448 struct rb_node *next= NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500449 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500450
Nikolay Borisov352646c2019-01-30 16:51:00 +0200451 ret = __etree_search(tree, offset, &next, NULL, p_ret, parent_ret);
Chris Masond3977122009-01-05 21:25:51 -0500452 if (!ret)
Nikolay Borisov352646c2019-01-30 16:51:00 +0200453 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500454 return ret;
455}
456
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000457static inline struct rb_node *tree_search(struct extent_io_tree *tree,
458 u64 offset)
459{
460 return tree_search_for_insert(tree, offset, NULL, NULL);
461}
462
Chris Masond1310b22008-01-24 16:13:08 -0500463/*
464 * utility function to look for merge candidates inside a given range.
465 * Any extents with matching state are merged together into a single
466 * extent in the tree. Extents with EXTENT_IO in their state field
467 * are not merged because the end_io handlers need to be able to do
468 * operations on them without sleeping (or doing allocations/splits).
469 *
470 * This should be called with the tree lock held.
471 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000472static void merge_state(struct extent_io_tree *tree,
473 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500474{
475 struct extent_state *other;
476 struct rb_node *other_node;
477
Nikolay Borisov88826792019-03-14 15:28:31 +0200478 if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000479 return;
Chris Masond1310b22008-01-24 16:13:08 -0500480
481 other_node = rb_prev(&state->rb_node);
482 if (other_node) {
483 other = rb_entry(other_node, struct extent_state, rb_node);
484 if (other->end == state->start - 1 &&
485 other->state == state->state) {
Nikolay Borisov5c848192018-11-01 14:09:52 +0200486 if (tree->private_data &&
487 is_data_inode(tree->private_data))
488 btrfs_merge_delalloc_extent(tree->private_data,
489 state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500490 state->start = other->start;
Chris Masond1310b22008-01-24 16:13:08 -0500491 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100492 RB_CLEAR_NODE(&other->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500493 free_extent_state(other);
494 }
495 }
496 other_node = rb_next(&state->rb_node);
497 if (other_node) {
498 other = rb_entry(other_node, struct extent_state, rb_node);
499 if (other->start == state->end + 1 &&
500 other->state == state->state) {
Nikolay Borisov5c848192018-11-01 14:09:52 +0200501 if (tree->private_data &&
502 is_data_inode(tree->private_data))
503 btrfs_merge_delalloc_extent(tree->private_data,
504 state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400505 state->end = other->end;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400506 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100507 RB_CLEAR_NODE(&other->rb_node);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400508 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500509 }
510 }
Chris Masond1310b22008-01-24 16:13:08 -0500511}
512
Xiao Guangrong3150b692011-07-14 03:19:08 +0000513static void set_state_bits(struct extent_io_tree *tree,
Qu Wenruod38ed272015-10-12 14:53:37 +0800514 struct extent_state *state, unsigned *bits,
515 struct extent_changeset *changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000516
Chris Masond1310b22008-01-24 16:13:08 -0500517/*
518 * insert an extent_state struct into the tree. 'bits' are set on the
519 * struct before it is inserted.
520 *
521 * This may return -EEXIST if the extent is already there, in which case the
522 * state struct is freed.
523 *
524 * The tree lock is not taken internally. This is a utility function and
525 * probably isn't what you want to call (see set/clear_extent_bit).
526 */
527static int insert_state(struct extent_io_tree *tree,
528 struct extent_state *state, u64 start, u64 end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000529 struct rb_node ***p,
530 struct rb_node **parent,
Qu Wenruod38ed272015-10-12 14:53:37 +0800531 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500532{
533 struct rb_node *node;
534
David Sterba27922372019-06-18 20:00:05 +0200535 if (end < start) {
536 btrfs_err(tree->fs_info,
537 "insert state: end < start %llu %llu", end, start);
538 WARN_ON(1);
539 }
Chris Masond1310b22008-01-24 16:13:08 -0500540 state->start = start;
541 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400542
Qu Wenruod38ed272015-10-12 14:53:37 +0800543 set_state_bits(tree, state, bits, changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000544
Filipe Mananaf2071b22014-02-12 15:05:53 +0000545 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
Chris Masond1310b22008-01-24 16:13:08 -0500546 if (node) {
547 struct extent_state *found;
548 found = rb_entry(node, struct extent_state, rb_node);
David Sterba27922372019-06-18 20:00:05 +0200549 btrfs_err(tree->fs_info,
550 "found node %llu %llu on insert of %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200551 found->start, found->end, start, end);
Chris Masond1310b22008-01-24 16:13:08 -0500552 return -EEXIST;
553 }
554 merge_state(tree, state);
555 return 0;
556}
557
558/*
559 * split a given extent state struct in two, inserting the preallocated
560 * struct 'prealloc' as the newly created second half. 'split' indicates an
561 * offset inside 'orig' where it should be split.
562 *
563 * Before calling,
564 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
565 * are two extent state structs in the tree:
566 * prealloc: [orig->start, split - 1]
567 * orig: [ split, orig->end ]
568 *
569 * The tree locks are not taken by this function. They need to be held
570 * by the caller.
571 */
572static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
573 struct extent_state *prealloc, u64 split)
574{
575 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400576
Nikolay Borisovabbb55f2018-11-01 14:09:53 +0200577 if (tree->private_data && is_data_inode(tree->private_data))
578 btrfs_split_delalloc_extent(tree->private_data, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400579
Chris Masond1310b22008-01-24 16:13:08 -0500580 prealloc->start = orig->start;
581 prealloc->end = split - 1;
582 prealloc->state = orig->state;
583 orig->start = split;
584
Filipe Mananaf2071b22014-02-12 15:05:53 +0000585 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
586 &prealloc->rb_node, NULL, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500587 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500588 free_extent_state(prealloc);
589 return -EEXIST;
590 }
591 return 0;
592}
593
Li Zefancdc6a392012-03-12 16:39:48 +0800594static struct extent_state *next_state(struct extent_state *state)
595{
596 struct rb_node *next = rb_next(&state->rb_node);
597 if (next)
598 return rb_entry(next, struct extent_state, rb_node);
599 else
600 return NULL;
601}
602
Chris Masond1310b22008-01-24 16:13:08 -0500603/*
604 * utility function to clear some bits in an extent state struct.
Andrea Gelmini52042d82018-11-28 12:05:13 +0100605 * it will optionally wake up anyone waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500606 *
607 * If no bits are set on the state struct after clearing things, the
608 * struct is freed and removed from the tree
609 */
Li Zefancdc6a392012-03-12 16:39:48 +0800610static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
611 struct extent_state *state,
Qu Wenruofefdc552015-10-12 15:35:38 +0800612 unsigned *bits, int wake,
613 struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500614{
Li Zefancdc6a392012-03-12 16:39:48 +0800615 struct extent_state *next;
David Sterba9ee49a042015-01-14 19:52:13 +0100616 unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100617 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500618
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400619 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500620 u64 range = state->end - state->start + 1;
621 WARN_ON(range > tree->dirty_bytes);
622 tree->dirty_bytes -= range;
623 }
Nikolay Borisova36bb5f2018-11-01 14:09:51 +0200624
625 if (tree->private_data && is_data_inode(tree->private_data))
626 btrfs_clear_delalloc_extent(tree->private_data, state, bits);
627
David Sterba57599c72018-03-01 17:56:34 +0100628 ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
629 BUG_ON(ret < 0);
Josef Bacik32c00af2009-10-08 13:34:05 -0400630 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500631 if (wake)
632 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400633 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800634 next = next_state(state);
Filipe Manana27a35072014-07-06 20:09:59 +0100635 if (extent_state_in_tree(state)) {
Chris Masond1310b22008-01-24 16:13:08 -0500636 rb_erase(&state->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100637 RB_CLEAR_NODE(&state->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500638 free_extent_state(state);
639 } else {
640 WARN_ON(1);
641 }
642 } else {
643 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800644 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500645 }
Li Zefancdc6a392012-03-12 16:39:48 +0800646 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500647}
648
Xiao Guangrong82337672011-04-20 06:44:57 +0000649static struct extent_state *
650alloc_extent_state_atomic(struct extent_state *prealloc)
651{
652 if (!prealloc)
653 prealloc = alloc_extent_state(GFP_ATOMIC);
654
655 return prealloc;
656}
657
Eric Sandeen48a3b632013-04-25 20:41:01 +0000658static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400659{
David Sterba05912a32018-07-18 19:23:45 +0200660 struct inode *inode = tree->private_data;
661
662 btrfs_panic(btrfs_sb(inode->i_sb), err,
663 "locking error: extent tree was modified by another thread while locked");
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400664}
665
Chris Masond1310b22008-01-24 16:13:08 -0500666/*
667 * clear some bits on a range in the tree. This may require splitting
668 * or inserting elements in the tree, so the gfp mask is used to
669 * indicate which allocations or sleeping are allowed.
670 *
671 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
672 * the given range from the tree regardless of state (ie for truncate).
673 *
674 * the range [start, end] is inclusive.
675 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100676 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500677 */
David Sterba66b0c882017-10-31 16:30:47 +0100678int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Qu Wenruofefdc552015-10-12 15:35:38 +0800679 unsigned bits, int wake, int delete,
680 struct extent_state **cached_state,
681 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500682{
683 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400684 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500685 struct extent_state *prealloc = NULL;
686 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400687 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500688 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000689 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500690
Josef Bacika5dee372013-12-13 10:02:44 -0500691 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +0800692 trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits);
David Sterba8d599ae2013-04-30 15:22:23 +0000693
Josef Bacik7ee9e442013-06-21 16:37:03 -0400694 if (bits & EXTENT_DELALLOC)
695 bits |= EXTENT_NORESERVE;
696
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400697 if (delete)
698 bits |= ~EXTENT_CTLBITS;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400699
Nikolay Borisov88826792019-03-14 15:28:31 +0200700 if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY))
Josef Bacik2ac55d42010-02-03 19:33:23 +0000701 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500702again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800703 if (!prealloc && gfpflags_allow_blocking(mask)) {
Filipe Mananac7bc6312014-11-03 14:12:57 +0000704 /*
705 * Don't care for allocation failure here because we might end
706 * up not needing the pre-allocated extent state at all, which
707 * is the case if we only have in the tree extent states that
708 * cover our input range and don't cover too any other range.
709 * If we end up needing a new extent state we allocate it later.
710 */
Chris Masond1310b22008-01-24 16:13:08 -0500711 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500712 }
713
Chris Masoncad321a2008-12-17 14:51:42 -0500714 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400715 if (cached_state) {
716 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000717
718 if (clear) {
719 *cached_state = NULL;
720 cached_state = NULL;
721 }
722
Filipe Manana27a35072014-07-06 20:09:59 +0100723 if (cached && extent_state_in_tree(cached) &&
724 cached->start <= start && cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000725 if (clear)
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200726 refcount_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400727 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400728 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400729 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000730 if (clear)
731 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400732 }
Chris Masond1310b22008-01-24 16:13:08 -0500733 /*
734 * this search will find the extents that end after
735 * our range starts
736 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500737 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500738 if (!node)
739 goto out;
740 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400741hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500742 if (state->start > end)
743 goto out;
744 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400745 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500746
Liu Bo04493142012-02-16 18:34:37 +0800747 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800748 if (!(state->state & bits)) {
749 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800750 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800751 }
Liu Bo04493142012-02-16 18:34:37 +0800752
Chris Masond1310b22008-01-24 16:13:08 -0500753 /*
754 * | ---- desired range ---- |
755 * | state | or
756 * | ------------- state -------------- |
757 *
758 * We need to split the extent we found, and may flip
759 * bits on second half.
760 *
761 * If the extent we found extends past our range, we
762 * just split and search again. It'll get split again
763 * the next time though.
764 *
765 * If the extent we found is inside our range, we clear
766 * the desired bit on it.
767 */
768
769 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000770 prealloc = alloc_extent_state_atomic(prealloc);
771 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500772 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400773 if (err)
774 extent_io_tree_panic(tree, err);
775
Chris Masond1310b22008-01-24 16:13:08 -0500776 prealloc = NULL;
777 if (err)
778 goto out;
779 if (state->end <= end) {
Qu Wenruofefdc552015-10-12 15:35:38 +0800780 state = clear_state_bit(tree, state, &bits, wake,
781 changeset);
Liu Bod1ac6e42012-05-10 18:10:39 +0800782 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500783 }
784 goto search_again;
785 }
786 /*
787 * | ---- desired range ---- |
788 * | state |
789 * We need to split the extent, and clear the bit
790 * on the first half
791 */
792 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000793 prealloc = alloc_extent_state_atomic(prealloc);
794 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500795 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400796 if (err)
797 extent_io_tree_panic(tree, err);
798
Chris Masond1310b22008-01-24 16:13:08 -0500799 if (wake)
800 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400801
Qu Wenruofefdc552015-10-12 15:35:38 +0800802 clear_state_bit(tree, prealloc, &bits, wake, changeset);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400803
Chris Masond1310b22008-01-24 16:13:08 -0500804 prealloc = NULL;
805 goto out;
806 }
Chris Mason42daec22009-09-23 19:51:09 -0400807
Qu Wenruofefdc552015-10-12 15:35:38 +0800808 state = clear_state_bit(tree, state, &bits, wake, changeset);
Liu Bo04493142012-02-16 18:34:37 +0800809next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400810 if (last_end == (u64)-1)
811 goto out;
812 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800813 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800814 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500815
816search_again:
817 if (start > end)
818 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500819 spin_unlock(&tree->lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800820 if (gfpflags_allow_blocking(mask))
Chris Masond1310b22008-01-24 16:13:08 -0500821 cond_resched();
822 goto again;
David Sterba7ab5cb22016-04-27 01:02:15 +0200823
824out:
825 spin_unlock(&tree->lock);
826 if (prealloc)
827 free_extent_state(prealloc);
828
829 return 0;
830
Chris Masond1310b22008-01-24 16:13:08 -0500831}
Chris Masond1310b22008-01-24 16:13:08 -0500832
Jeff Mahoney143bede2012-03-01 14:56:26 +0100833static void wait_on_state(struct extent_io_tree *tree,
834 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500835 __releases(tree->lock)
836 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500837{
838 DEFINE_WAIT(wait);
839 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500840 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500841 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500842 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500843 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500844}
845
846/*
847 * waits for one or more bits to clear on a range in the state tree.
848 * The range [start, end] is inclusive.
849 * The tree lock is taken by this function
850 */
David Sterba41074882013-04-29 13:38:46 +0000851static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
852 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500853{
854 struct extent_state *state;
855 struct rb_node *node;
856
Josef Bacika5dee372013-12-13 10:02:44 -0500857 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000858
Chris Masoncad321a2008-12-17 14:51:42 -0500859 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500860again:
861 while (1) {
862 /*
863 * this search will find all the extents that end after
864 * our range starts
865 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500866 node = tree_search(tree, start);
Filipe Mananac50d3e72014-03-31 14:53:25 +0100867process_node:
Chris Masond1310b22008-01-24 16:13:08 -0500868 if (!node)
869 break;
870
871 state = rb_entry(node, struct extent_state, rb_node);
872
873 if (state->start > end)
874 goto out;
875
876 if (state->state & bits) {
877 start = state->start;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200878 refcount_inc(&state->refs);
Chris Masond1310b22008-01-24 16:13:08 -0500879 wait_on_state(tree, state);
880 free_extent_state(state);
881 goto again;
882 }
883 start = state->end + 1;
884
885 if (start > end)
886 break;
887
Filipe Mananac50d3e72014-03-31 14:53:25 +0100888 if (!cond_resched_lock(&tree->lock)) {
889 node = rb_next(node);
890 goto process_node;
891 }
Chris Masond1310b22008-01-24 16:13:08 -0500892 }
893out:
Chris Masoncad321a2008-12-17 14:51:42 -0500894 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500895}
Chris Masond1310b22008-01-24 16:13:08 -0500896
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000897static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500898 struct extent_state *state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800899 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500900{
David Sterba9ee49a042015-01-14 19:52:13 +0100901 unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100902 int ret;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400903
Nikolay Borisove06a1fc2018-11-01 14:09:50 +0200904 if (tree->private_data && is_data_inode(tree->private_data))
905 btrfs_set_delalloc_extent(tree->private_data, state, bits);
906
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400907 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500908 u64 range = state->end - state->start + 1;
909 tree->dirty_bytes += range;
910 }
David Sterba57599c72018-03-01 17:56:34 +0100911 ret = add_extent_changeset(state, bits_to_set, changeset, 1);
912 BUG_ON(ret < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400913 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500914}
915
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100916static void cache_state_if_flags(struct extent_state *state,
917 struct extent_state **cached_ptr,
David Sterba9ee49a042015-01-14 19:52:13 +0100918 unsigned flags)
Chris Mason2c64c532009-09-02 15:04:12 -0400919{
920 if (cached_ptr && !(*cached_ptr)) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100921 if (!flags || (state->state & flags)) {
Chris Mason2c64c532009-09-02 15:04:12 -0400922 *cached_ptr = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200923 refcount_inc(&state->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400924 }
925 }
926}
927
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100928static void cache_state(struct extent_state *state,
929 struct extent_state **cached_ptr)
930{
931 return cache_state_if_flags(state, cached_ptr,
Nikolay Borisov88826792019-03-14 15:28:31 +0200932 EXTENT_LOCKED | EXTENT_BOUNDARY);
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100933}
934
Chris Masond1310b22008-01-24 16:13:08 -0500935/*
Chris Mason1edbb732009-09-02 13:24:36 -0400936 * set some bits on a range in the tree. This may require allocations or
937 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500938 *
Chris Mason1edbb732009-09-02 13:24:36 -0400939 * If any of the exclusive bits are set, this will fail with -EEXIST if some
940 * part of the range already has the desired bits set. The start of the
941 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500942 *
Chris Mason1edbb732009-09-02 13:24:36 -0400943 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500944 */
Chris Mason1edbb732009-09-02 13:24:36 -0400945
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100946static int __must_check
947__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +0100948 unsigned bits, unsigned exclusive_bits,
David Sterba41074882013-04-29 13:38:46 +0000949 u64 *failed_start, struct extent_state **cached_state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800950 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500951{
952 struct extent_state *state;
953 struct extent_state *prealloc = NULL;
954 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000955 struct rb_node **p;
956 struct rb_node *parent;
Chris Masond1310b22008-01-24 16:13:08 -0500957 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500958 u64 last_start;
959 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400960
Josef Bacika5dee372013-12-13 10:02:44 -0500961 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +0800962 trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits);
David Sterba8d599ae2013-04-30 15:22:23 +0000963
Chris Masond1310b22008-01-24 16:13:08 -0500964again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800965 if (!prealloc && gfpflags_allow_blocking(mask)) {
David Sterba059f7912016-04-27 01:03:45 +0200966 /*
967 * Don't care for allocation failure here because we might end
968 * up not needing the pre-allocated extent state at all, which
969 * is the case if we only have in the tree extent states that
970 * cover our input range and don't cover too any other range.
971 * If we end up needing a new extent state we allocate it later.
972 */
Chris Masond1310b22008-01-24 16:13:08 -0500973 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500974 }
975
Chris Masoncad321a2008-12-17 14:51:42 -0500976 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400977 if (cached_state && *cached_state) {
978 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400979 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +0100980 extent_state_in_tree(state)) {
Chris Mason9655d292009-09-02 15:22:30 -0400981 node = &state->rb_node;
982 goto hit_next;
983 }
984 }
Chris Masond1310b22008-01-24 16:13:08 -0500985 /*
986 * this search will find all the extents that end after
987 * our range starts.
988 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000989 node = tree_search_for_insert(tree, start, &p, &parent);
Chris Masond1310b22008-01-24 16:13:08 -0500990 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000991 prealloc = alloc_extent_state_atomic(prealloc);
992 BUG_ON(!prealloc);
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000993 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800994 &p, &parent, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400995 if (err)
996 extent_io_tree_panic(tree, err);
997
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +0000998 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500999 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001000 goto out;
1001 }
Chris Masond1310b22008-01-24 16:13:08 -05001002 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -04001003hit_next:
Chris Masond1310b22008-01-24 16:13:08 -05001004 last_start = state->start;
1005 last_end = state->end;
1006
1007 /*
1008 * | ---- desired range ---- |
1009 * | state |
1010 *
1011 * Just lock what we found and keep going
1012 */
1013 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -04001014 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001015 *failed_start = state->start;
1016 err = -EEXIST;
1017 goto out;
1018 }
Chris Mason42daec22009-09-23 19:51:09 -04001019
Qu Wenruod38ed272015-10-12 14:53:37 +08001020 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001021 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001022 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -04001023 if (last_end == (u64)-1)
1024 goto out;
1025 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001026 state = next_state(state);
1027 if (start < end && state && state->start == start &&
1028 !need_resched())
1029 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -05001030 goto search_again;
1031 }
1032
1033 /*
1034 * | ---- desired range ---- |
1035 * | state |
1036 * or
1037 * | ------------- state -------------- |
1038 *
1039 * We need to split the extent we found, and may flip bits on
1040 * second half.
1041 *
1042 * If the extent we found extends past our
1043 * range, we just split and search again. It'll get split
1044 * again the next time though.
1045 *
1046 * If the extent we found is inside our range, we set the
1047 * desired bit on it.
1048 */
1049 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -04001050 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001051 *failed_start = start;
1052 err = -EEXIST;
1053 goto out;
1054 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001055
1056 prealloc = alloc_extent_state_atomic(prealloc);
1057 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001058 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001059 if (err)
1060 extent_io_tree_panic(tree, err);
1061
Chris Masond1310b22008-01-24 16:13:08 -05001062 prealloc = NULL;
1063 if (err)
1064 goto out;
1065 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001066 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001067 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001068 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -04001069 if (last_end == (u64)-1)
1070 goto out;
1071 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001072 state = next_state(state);
1073 if (start < end && state && state->start == start &&
1074 !need_resched())
1075 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -05001076 }
1077 goto search_again;
1078 }
1079 /*
1080 * | ---- desired range ---- |
1081 * | state | or | state |
1082 *
1083 * There's a hole, we need to insert something in it and
1084 * ignore the extent we found.
1085 */
1086 if (state->start > start) {
1087 u64 this_end;
1088 if (end < last_start)
1089 this_end = end;
1090 else
Chris Masond3977122009-01-05 21:25:51 -05001091 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +00001092
1093 prealloc = alloc_extent_state_atomic(prealloc);
1094 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +00001095
1096 /*
1097 * Avoid to free 'prealloc' if it can be merged with
1098 * the later extent.
1099 */
Chris Masond1310b22008-01-24 16:13:08 -05001100 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001101 NULL, NULL, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001102 if (err)
1103 extent_io_tree_panic(tree, err);
1104
Chris Mason2c64c532009-09-02 15:04:12 -04001105 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001106 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001107 start = this_end + 1;
1108 goto search_again;
1109 }
1110 /*
1111 * | ---- desired range ---- |
1112 * | state |
1113 * We need to split the extent, and set the bit
1114 * on the first half
1115 */
1116 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -04001117 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001118 *failed_start = start;
1119 err = -EEXIST;
1120 goto out;
1121 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001122
1123 prealloc = alloc_extent_state_atomic(prealloc);
1124 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001125 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001126 if (err)
1127 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -05001128
Qu Wenruod38ed272015-10-12 14:53:37 +08001129 set_state_bits(tree, prealloc, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001130 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001131 merge_state(tree, prealloc);
1132 prealloc = NULL;
1133 goto out;
1134 }
1135
David Sterbab5a4ba142016-04-27 01:02:15 +02001136search_again:
1137 if (start > end)
1138 goto out;
1139 spin_unlock(&tree->lock);
1140 if (gfpflags_allow_blocking(mask))
1141 cond_resched();
1142 goto again;
Chris Masond1310b22008-01-24 16:13:08 -05001143
1144out:
Chris Masoncad321a2008-12-17 14:51:42 -05001145 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001146 if (prealloc)
1147 free_extent_state(prealloc);
1148
1149 return err;
1150
Chris Masond1310b22008-01-24 16:13:08 -05001151}
Chris Masond1310b22008-01-24 16:13:08 -05001152
David Sterba41074882013-04-29 13:38:46 +00001153int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001154 unsigned bits, u64 * failed_start,
David Sterba41074882013-04-29 13:38:46 +00001155 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001156{
1157 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001158 cached_state, mask, NULL);
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001159}
1160
1161
Josef Bacik462d6fa2011-09-26 13:56:12 -04001162/**
Liu Bo10983f22012-07-11 15:26:19 +08001163 * convert_extent_bit - convert all bits in a given range from one bit to
1164 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -04001165 * @tree: the io tree to search
1166 * @start: the start offset in bytes
1167 * @end: the end offset in bytes (inclusive)
1168 * @bits: the bits to set in this range
1169 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001170 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001171 *
1172 * This will go through and set bits for the given range. If any states exist
1173 * already in this range they are set with the given bit and cleared of the
1174 * clear_bits. This is only meant to be used by things that are mergeable, ie
1175 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1176 * boundary bits like LOCK.
David Sterba210aa272016-04-26 23:54:39 +02001177 *
1178 * All allocations are done with GFP_NOFS.
Josef Bacik462d6fa2011-09-26 13:56:12 -04001179 */
1180int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001181 unsigned bits, unsigned clear_bits,
David Sterba210aa272016-04-26 23:54:39 +02001182 struct extent_state **cached_state)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001183{
1184 struct extent_state *state;
1185 struct extent_state *prealloc = NULL;
1186 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001187 struct rb_node **p;
1188 struct rb_node *parent;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001189 int err = 0;
1190 u64 last_start;
1191 u64 last_end;
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001192 bool first_iteration = true;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001193
Josef Bacika5dee372013-12-13 10:02:44 -05001194 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +08001195 trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits,
1196 clear_bits);
David Sterba8d599ae2013-04-30 15:22:23 +00001197
Josef Bacik462d6fa2011-09-26 13:56:12 -04001198again:
David Sterba210aa272016-04-26 23:54:39 +02001199 if (!prealloc) {
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001200 /*
1201 * Best effort, don't worry if extent state allocation fails
1202 * here for the first iteration. We might have a cached state
1203 * that matches exactly the target range, in which case no
1204 * extent state allocations are needed. We'll only know this
1205 * after locking the tree.
1206 */
David Sterba210aa272016-04-26 23:54:39 +02001207 prealloc = alloc_extent_state(GFP_NOFS);
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001208 if (!prealloc && !first_iteration)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001209 return -ENOMEM;
1210 }
1211
1212 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001213 if (cached_state && *cached_state) {
1214 state = *cached_state;
1215 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +01001216 extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001217 node = &state->rb_node;
1218 goto hit_next;
1219 }
1220 }
1221
Josef Bacik462d6fa2011-09-26 13:56:12 -04001222 /*
1223 * this search will find all the extents that end after
1224 * our range starts.
1225 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001226 node = tree_search_for_insert(tree, start, &p, &parent);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001227 if (!node) {
1228 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001229 if (!prealloc) {
1230 err = -ENOMEM;
1231 goto out;
1232 }
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001233 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001234 &p, &parent, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001235 if (err)
1236 extent_io_tree_panic(tree, err);
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001237 cache_state(prealloc, cached_state);
1238 prealloc = NULL;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001239 goto out;
1240 }
1241 state = rb_entry(node, struct extent_state, rb_node);
1242hit_next:
1243 last_start = state->start;
1244 last_end = state->end;
1245
1246 /*
1247 * | ---- desired range ---- |
1248 * | state |
1249 *
1250 * Just lock what we found and keep going
1251 */
1252 if (state->start == start && state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001253 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001254 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001255 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001256 if (last_end == (u64)-1)
1257 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001258 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001259 if (start < end && state && state->start == start &&
1260 !need_resched())
1261 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001262 goto search_again;
1263 }
1264
1265 /*
1266 * | ---- desired range ---- |
1267 * | state |
1268 * or
1269 * | ------------- state -------------- |
1270 *
1271 * We need to split the extent we found, and may flip bits on
1272 * second half.
1273 *
1274 * If the extent we found extends past our
1275 * range, we just split and search again. It'll get split
1276 * again the next time though.
1277 *
1278 * If the extent we found is inside our range, we set the
1279 * desired bit on it.
1280 */
1281 if (state->start < start) {
1282 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001283 if (!prealloc) {
1284 err = -ENOMEM;
1285 goto out;
1286 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001287 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001288 if (err)
1289 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001290 prealloc = NULL;
1291 if (err)
1292 goto out;
1293 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001294 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001295 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001296 state = clear_state_bit(tree, state, &clear_bits, 0,
1297 NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001298 if (last_end == (u64)-1)
1299 goto out;
1300 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001301 if (start < end && state && state->start == start &&
1302 !need_resched())
1303 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001304 }
1305 goto search_again;
1306 }
1307 /*
1308 * | ---- desired range ---- |
1309 * | state | or | state |
1310 *
1311 * There's a hole, we need to insert something in it and
1312 * ignore the extent we found.
1313 */
1314 if (state->start > start) {
1315 u64 this_end;
1316 if (end < last_start)
1317 this_end = end;
1318 else
1319 this_end = last_start - 1;
1320
1321 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001322 if (!prealloc) {
1323 err = -ENOMEM;
1324 goto out;
1325 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001326
1327 /*
1328 * Avoid to free 'prealloc' if it can be merged with
1329 * the later extent.
1330 */
1331 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001332 NULL, NULL, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001333 if (err)
1334 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001335 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001336 prealloc = NULL;
1337 start = this_end + 1;
1338 goto search_again;
1339 }
1340 /*
1341 * | ---- desired range ---- |
1342 * | state |
1343 * We need to split the extent, and set the bit
1344 * on the first half
1345 */
1346 if (state->start <= end && state->end > end) {
1347 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001348 if (!prealloc) {
1349 err = -ENOMEM;
1350 goto out;
1351 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001352
1353 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001354 if (err)
1355 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001356
Qu Wenruod38ed272015-10-12 14:53:37 +08001357 set_state_bits(tree, prealloc, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001358 cache_state(prealloc, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001359 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001360 prealloc = NULL;
1361 goto out;
1362 }
1363
Josef Bacik462d6fa2011-09-26 13:56:12 -04001364search_again:
1365 if (start > end)
1366 goto out;
1367 spin_unlock(&tree->lock);
David Sterba210aa272016-04-26 23:54:39 +02001368 cond_resched();
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001369 first_iteration = false;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001370 goto again;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001371
1372out:
1373 spin_unlock(&tree->lock);
1374 if (prealloc)
1375 free_extent_state(prealloc);
1376
1377 return err;
1378}
1379
Chris Masond1310b22008-01-24 16:13:08 -05001380/* wrappers around set/clear extent bit */
Qu Wenruod38ed272015-10-12 14:53:37 +08001381int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba2c53b912016-04-26 23:54:39 +02001382 unsigned bits, struct extent_changeset *changeset)
Qu Wenruod38ed272015-10-12 14:53:37 +08001383{
1384 /*
1385 * We don't support EXTENT_LOCKED yet, as current changeset will
1386 * record any bits changed, so for EXTENT_LOCKED case, it will
1387 * either fail with -EEXIST or changeset will record the whole
1388 * range.
1389 */
1390 BUG_ON(bits & EXTENT_LOCKED);
1391
David Sterba2c53b912016-04-26 23:54:39 +02001392 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
Qu Wenruod38ed272015-10-12 14:53:37 +08001393 changeset);
1394}
1395
Nikolay Borisov4ca73652019-03-27 14:24:10 +02001396int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start, u64 end,
1397 unsigned bits)
1398{
1399 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL,
1400 GFP_NOWAIT, NULL);
1401}
1402
Qu Wenruofefdc552015-10-12 15:35:38 +08001403int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1404 unsigned bits, int wake, int delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001405 struct extent_state **cached)
Qu Wenruofefdc552015-10-12 15:35:38 +08001406{
1407 return __clear_extent_bit(tree, start, end, bits, wake, delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001408 cached, GFP_NOFS, NULL);
Qu Wenruofefdc552015-10-12 15:35:38 +08001409}
1410
Qu Wenruofefdc552015-10-12 15:35:38 +08001411int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaf734c442016-04-26 23:54:39 +02001412 unsigned bits, struct extent_changeset *changeset)
Qu Wenruofefdc552015-10-12 15:35:38 +08001413{
1414 /*
1415 * Don't support EXTENT_LOCKED case, same reason as
1416 * set_record_extent_bits().
1417 */
1418 BUG_ON(bits & EXTENT_LOCKED);
1419
David Sterbaf734c442016-04-26 23:54:39 +02001420 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
Qu Wenruofefdc552015-10-12 15:35:38 +08001421 changeset);
1422}
1423
Chris Masond352ac62008-09-29 15:18:18 -04001424/*
1425 * either insert or lock state struct between start and end use mask to tell
1426 * us if waiting is desired.
1427 */
Chris Mason1edbb732009-09-02 13:24:36 -04001428int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaff13db42015-12-03 14:30:40 +01001429 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001430{
1431 int err;
1432 u64 failed_start;
David Sterba9ee49a042015-01-14 19:52:13 +01001433
Chris Masond1310b22008-01-24 16:13:08 -05001434 while (1) {
David Sterbaff13db42015-12-03 14:30:40 +01001435 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001436 EXTENT_LOCKED, &failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001437 cached_state, GFP_NOFS, NULL);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001438 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001439 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1440 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001441 } else
Chris Masond1310b22008-01-24 16:13:08 -05001442 break;
Chris Masond1310b22008-01-24 16:13:08 -05001443 WARN_ON(start > end);
1444 }
1445 return err;
1446}
Chris Masond1310b22008-01-24 16:13:08 -05001447
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001448int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001449{
1450 int err;
1451 u64 failed_start;
1452
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001453 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
Qu Wenruod38ed272015-10-12 14:53:37 +08001454 &failed_start, NULL, GFP_NOFS, NULL);
Yan Zheng66435582008-10-30 14:19:50 -04001455 if (err == -EEXIST) {
1456 if (failed_start > start)
1457 clear_extent_bit(tree, start, failed_start - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001458 EXTENT_LOCKED, 1, 0, NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001459 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001460 }
Josef Bacik25179202008-10-29 14:49:05 -04001461 return 1;
1462}
Josef Bacik25179202008-10-29 14:49:05 -04001463
David Sterbabd1fa4f2015-12-03 13:08:59 +01001464void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001465{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001466 unsigned long index = start >> PAGE_SHIFT;
1467 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001468 struct page *page;
1469
1470 while (index <= end_index) {
1471 page = find_get_page(inode->i_mapping, index);
1472 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1473 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001474 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001475 index++;
1476 }
Chris Mason4adaa612013-03-26 13:07:00 -04001477}
1478
David Sterbaf6311572015-12-03 13:08:59 +01001479void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001480{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001481 unsigned long index = start >> PAGE_SHIFT;
1482 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001483 struct page *page;
1484
1485 while (index <= end_index) {
1486 page = find_get_page(inode->i_mapping, index);
1487 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Mason4adaa612013-03-26 13:07:00 -04001488 __set_page_dirty_nobuffers(page);
Konstantin Khebnikov8d386332015-02-11 15:26:55 -08001489 account_page_redirty(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001490 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001491 index++;
1492 }
Chris Mason4adaa612013-03-26 13:07:00 -04001493}
1494
Chris Masond352ac62008-09-29 15:18:18 -04001495/* find the first state struct with 'bits' set after 'start', and
1496 * return it. tree->lock must be held. NULL will returned if
1497 * nothing was found after 'start'
1498 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001499static struct extent_state *
1500find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +01001501 u64 start, unsigned bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001502{
1503 struct rb_node *node;
1504 struct extent_state *state;
1505
1506 /*
1507 * this search will find all the extents that end after
1508 * our range starts.
1509 */
1510 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001511 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001512 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001513
Chris Masond3977122009-01-05 21:25:51 -05001514 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001515 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001516 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001517 return state;
Chris Masond3977122009-01-05 21:25:51 -05001518
Chris Masond7fc6402008-02-18 12:12:38 -05001519 node = rb_next(node);
1520 if (!node)
1521 break;
1522 }
1523out:
1524 return NULL;
1525}
Chris Masond7fc6402008-02-18 12:12:38 -05001526
Chris Masond352ac62008-09-29 15:18:18 -04001527/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001528 * find the first offset in the io tree with 'bits' set. zero is
1529 * returned if we find something, and *start_ret and *end_ret are
1530 * set to reflect the state struct that was found.
1531 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001532 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001533 */
1534int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba9ee49a042015-01-14 19:52:13 +01001535 u64 *start_ret, u64 *end_ret, unsigned bits,
Josef Bacike6138872012-09-27 17:07:30 -04001536 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001537{
1538 struct extent_state *state;
1539 int ret = 1;
1540
1541 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001542 if (cached_state && *cached_state) {
1543 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001544 if (state->end == start - 1 && extent_state_in_tree(state)) {
Liu Bo9688e9a2018-08-23 03:14:53 +08001545 while ((state = next_state(state)) != NULL) {
Josef Bacike6138872012-09-27 17:07:30 -04001546 if (state->state & bits)
1547 goto got_it;
Josef Bacike6138872012-09-27 17:07:30 -04001548 }
1549 free_extent_state(*cached_state);
1550 *cached_state = NULL;
1551 goto out;
1552 }
1553 free_extent_state(*cached_state);
1554 *cached_state = NULL;
1555 }
1556
Xiao Guangrong69261c42011-07-14 03:19:45 +00001557 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001558got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001559 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001560 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001561 *start_ret = state->start;
1562 *end_ret = state->end;
1563 ret = 0;
1564 }
Josef Bacike6138872012-09-27 17:07:30 -04001565out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001566 spin_unlock(&tree->lock);
1567 return ret;
1568}
1569
Nikolay Borisov45bfcfc2019-03-27 14:24:17 +02001570/**
Nikolay Borisov1eaebb32019-06-03 13:06:02 +03001571 * find_first_clear_extent_bit - find the first range that has @bits not set.
1572 * This range could start before @start.
Nikolay Borisov45bfcfc2019-03-27 14:24:17 +02001573 *
1574 * @tree - the tree to search
1575 * @start - the offset at/after which the found extent should start
1576 * @start_ret - records the beginning of the range
1577 * @end_ret - records the end of the range (inclusive)
1578 * @bits - the set of bits which must be unset
1579 *
1580 * Since unallocated range is also considered one which doesn't have the bits
1581 * set it's possible that @end_ret contains -1, this happens in case the range
1582 * spans (last_range_end, end of device]. In this case it's up to the caller to
1583 * trim @end_ret to the appropriate size.
1584 */
1585void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
1586 u64 *start_ret, u64 *end_ret, unsigned bits)
1587{
1588 struct extent_state *state;
1589 struct rb_node *node, *prev = NULL, *next;
1590
1591 spin_lock(&tree->lock);
1592
1593 /* Find first extent with bits cleared */
1594 while (1) {
1595 node = __etree_search(tree, start, &next, &prev, NULL, NULL);
1596 if (!node) {
1597 node = next;
1598 if (!node) {
1599 /*
1600 * We are past the last allocated chunk,
1601 * set start at the end of the last extent. The
1602 * device alloc tree should never be empty so
1603 * prev is always set.
1604 */
1605 ASSERT(prev);
1606 state = rb_entry(prev, struct extent_state, rb_node);
1607 *start_ret = state->end + 1;
1608 *end_ret = -1;
1609 goto out;
1610 }
1611 }
Nikolay Borisov1eaebb32019-06-03 13:06:02 +03001612 /*
1613 * At this point 'node' either contains 'start' or start is
1614 * before 'node'
1615 */
Nikolay Borisov45bfcfc2019-03-27 14:24:17 +02001616 state = rb_entry(node, struct extent_state, rb_node);
Nikolay Borisov1eaebb32019-06-03 13:06:02 +03001617
1618 if (in_range(start, state->start, state->end - state->start + 1)) {
1619 if (state->state & bits) {
1620 /*
1621 * |--range with bits sets--|
1622 * |
1623 * start
1624 */
1625 start = state->end + 1;
1626 } else {
1627 /*
1628 * 'start' falls within a range that doesn't
1629 * have the bits set, so take its start as
1630 * the beginning of the desired range
1631 *
1632 * |--range with bits cleared----|
1633 * |
1634 * start
1635 */
1636 *start_ret = state->start;
1637 break;
1638 }
Nikolay Borisov45bfcfc2019-03-27 14:24:17 +02001639 } else {
Nikolay Borisov1eaebb32019-06-03 13:06:02 +03001640 /*
1641 * |---prev range---|---hole/unset---|---node range---|
1642 * |
1643 * start
1644 *
1645 * or
1646 *
1647 * |---hole/unset--||--first node--|
1648 * 0 |
1649 * start
1650 */
1651 if (prev) {
1652 state = rb_entry(prev, struct extent_state,
1653 rb_node);
1654 *start_ret = state->end + 1;
1655 } else {
1656 *start_ret = 0;
1657 }
Nikolay Borisov45bfcfc2019-03-27 14:24:17 +02001658 break;
1659 }
1660 }
1661
1662 /*
1663 * Find the longest stretch from start until an entry which has the
1664 * bits set
1665 */
1666 while (1) {
1667 state = rb_entry(node, struct extent_state, rb_node);
1668 if (state->end >= start && !(state->state & bits)) {
1669 *end_ret = state->end;
1670 } else {
1671 *end_ret = state->start - 1;
1672 break;
1673 }
1674
1675 node = rb_next(node);
1676 if (!node)
1677 break;
1678 }
1679out:
1680 spin_unlock(&tree->lock);
1681}
1682
Xiao Guangrong69261c42011-07-14 03:19:45 +00001683/*
Chris Masond352ac62008-09-29 15:18:18 -04001684 * find a contiguous range of bytes in the file marked as delalloc, not
1685 * more than 'max_bytes'. start and end are used to return the range,
1686 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001687 * true is returned if we find something, false if nothing was in the tree
Chris Masond352ac62008-09-29 15:18:18 -04001688 */
Josef Bacik083e75e2019-09-23 10:05:20 -04001689bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
1690 u64 *end, u64 max_bytes,
1691 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001692{
1693 struct rb_node *node;
1694 struct extent_state *state;
1695 u64 cur_start = *start;
Lu Fengqi3522e902018-11-29 11:33:38 +08001696 bool found = false;
Chris Masond1310b22008-01-24 16:13:08 -05001697 u64 total_bytes = 0;
1698
Chris Masoncad321a2008-12-17 14:51:42 -05001699 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001700
Chris Masond1310b22008-01-24 16:13:08 -05001701 /*
1702 * this search will find all the extents that end after
1703 * our range starts.
1704 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001705 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001706 if (!node) {
Lu Fengqi3522e902018-11-29 11:33:38 +08001707 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001708 goto out;
1709 }
1710
Chris Masond3977122009-01-05 21:25:51 -05001711 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001712 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001713 if (found && (state->start != cur_start ||
1714 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001715 goto out;
1716 }
1717 if (!(state->state & EXTENT_DELALLOC)) {
1718 if (!found)
1719 *end = state->end;
1720 goto out;
1721 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001722 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001723 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001724 *cached_state = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +02001725 refcount_inc(&state->refs);
Josef Bacikc2a128d2010-02-02 21:19:11 +00001726 }
Lu Fengqi3522e902018-11-29 11:33:38 +08001727 found = true;
Chris Masond1310b22008-01-24 16:13:08 -05001728 *end = state->end;
1729 cur_start = state->end + 1;
1730 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001731 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001732 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001733 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001734 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001735 break;
1736 }
1737out:
Chris Masoncad321a2008-12-17 14:51:42 -05001738 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001739 return found;
1740}
1741
Liu Boda2c7002017-02-10 16:41:05 +01001742static int __process_pages_contig(struct address_space *mapping,
1743 struct page *locked_page,
1744 pgoff_t start_index, pgoff_t end_index,
1745 unsigned long page_ops, pgoff_t *index_ret);
1746
Jeff Mahoney143bede2012-03-01 14:56:26 +01001747static noinline void __unlock_for_delalloc(struct inode *inode,
1748 struct page *locked_page,
1749 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001750{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001751 unsigned long index = start >> PAGE_SHIFT;
1752 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001753
Liu Bo76c00212017-02-10 16:42:14 +01001754 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001755 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001756 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001757
Liu Bo76c00212017-02-10 16:42:14 +01001758 __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1759 PAGE_UNLOCK, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -04001760}
1761
1762static noinline int lock_delalloc_pages(struct inode *inode,
1763 struct page *locked_page,
1764 u64 delalloc_start,
1765 u64 delalloc_end)
1766{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001767 unsigned long index = delalloc_start >> PAGE_SHIFT;
Liu Bo76c00212017-02-10 16:42:14 +01001768 unsigned long index_ret = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001769 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001770 int ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001771
Liu Bo76c00212017-02-10 16:42:14 +01001772 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001773 if (index == locked_page->index && index == end_index)
1774 return 0;
1775
Liu Bo76c00212017-02-10 16:42:14 +01001776 ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1777 end_index, PAGE_LOCK, &index_ret);
1778 if (ret == -EAGAIN)
1779 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1780 (u64)index_ret << PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001781 return ret;
1782}
1783
1784/*
Lu Fengqi3522e902018-11-29 11:33:38 +08001785 * Find and lock a contiguous range of bytes in the file marked as delalloc, no
1786 * more than @max_bytes. @Start and @end are used to return the range,
Chris Masonc8b97812008-10-29 14:49:59 -04001787 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001788 * Return: true if we find something
1789 * false if nothing was in the tree
Chris Masonc8b97812008-10-29 14:49:59 -04001790 */
Johannes Thumshirnce9f9672018-11-19 10:38:17 +01001791EXPORT_FOR_TESTS
Lu Fengqi3522e902018-11-29 11:33:38 +08001792noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
Josef Bacik294e30f2013-10-09 12:00:56 -04001793 struct page *locked_page, u64 *start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03001794 u64 *end)
Chris Masonc8b97812008-10-29 14:49:59 -04001795{
Goldwyn Rodrigues99780592019-06-21 10:02:54 -05001796 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Nikolay Borisov917aace2018-10-26 14:43:20 +03001797 u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001798 u64 delalloc_start;
1799 u64 delalloc_end;
Lu Fengqi3522e902018-11-29 11:33:38 +08001800 bool found;
Chris Mason9655d292009-09-02 15:22:30 -04001801 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001802 int ret;
1803 int loops = 0;
1804
1805again:
1806 /* step one, find a bunch of delalloc bytes starting at start */
1807 delalloc_start = *start;
1808 delalloc_end = 0;
Josef Bacik083e75e2019-09-23 10:05:20 -04001809 found = btrfs_find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1810 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001811 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001812 *start = delalloc_start;
1813 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001814 free_extent_state(cached_state);
Lu Fengqi3522e902018-11-29 11:33:38 +08001815 return false;
Chris Masonc8b97812008-10-29 14:49:59 -04001816 }
1817
1818 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001819 * start comes from the offset of locked_page. We have to lock
1820 * pages in order, so we can't process delalloc bytes before
1821 * locked_page
1822 */
Chris Masond3977122009-01-05 21:25:51 -05001823 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001824 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001825
1826 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001827 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001828 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001829 if (delalloc_end + 1 - delalloc_start > max_bytes)
1830 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001831
Chris Masonc8b97812008-10-29 14:49:59 -04001832 /* step two, lock all the pages after the page that has start */
1833 ret = lock_delalloc_pages(inode, locked_page,
1834 delalloc_start, delalloc_end);
Nikolay Borisov9bfd61d2018-10-26 14:43:21 +03001835 ASSERT(!ret || ret == -EAGAIN);
Chris Masonc8b97812008-10-29 14:49:59 -04001836 if (ret == -EAGAIN) {
1837 /* some of the pages are gone, lets avoid looping by
1838 * shortening the size of the delalloc range we're searching
1839 */
Chris Mason9655d292009-09-02 15:22:30 -04001840 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001841 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001842 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001843 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001844 loops = 1;
1845 goto again;
1846 } else {
Lu Fengqi3522e902018-11-29 11:33:38 +08001847 found = false;
Chris Masonc8b97812008-10-29 14:49:59 -04001848 goto out_failed;
1849 }
1850 }
Chris Masonc8b97812008-10-29 14:49:59 -04001851
1852 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001853 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001854
1855 /* then test to make sure it is all still delalloc */
1856 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001857 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001858 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001859 unlock_extent_cached(tree, delalloc_start, delalloc_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001860 &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001861 __unlock_for_delalloc(inode, locked_page,
1862 delalloc_start, delalloc_end);
1863 cond_resched();
1864 goto again;
1865 }
Chris Mason9655d292009-09-02 15:22:30 -04001866 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001867 *start = delalloc_start;
1868 *end = delalloc_end;
1869out_failed:
1870 return found;
1871}
1872
Liu Boda2c7002017-02-10 16:41:05 +01001873static int __process_pages_contig(struct address_space *mapping,
1874 struct page *locked_page,
1875 pgoff_t start_index, pgoff_t end_index,
1876 unsigned long page_ops, pgoff_t *index_ret)
Chris Masonc8b97812008-10-29 14:49:59 -04001877{
Liu Bo873695b2017-02-02 17:49:22 -08001878 unsigned long nr_pages = end_index - start_index + 1;
Liu Boda2c7002017-02-10 16:41:05 +01001879 unsigned long pages_locked = 0;
Liu Bo873695b2017-02-02 17:49:22 -08001880 pgoff_t index = start_index;
Chris Masonc8b97812008-10-29 14:49:59 -04001881 struct page *pages[16];
Liu Bo873695b2017-02-02 17:49:22 -08001882 unsigned ret;
Liu Boda2c7002017-02-10 16:41:05 +01001883 int err = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001884 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001885
Liu Boda2c7002017-02-10 16:41:05 +01001886 if (page_ops & PAGE_LOCK) {
1887 ASSERT(page_ops == PAGE_LOCK);
1888 ASSERT(index_ret && *index_ret == start_index);
1889 }
1890
Filipe Manana704de492014-10-06 22:14:22 +01001891 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
Liu Bo873695b2017-02-02 17:49:22 -08001892 mapping_set_error(mapping, -EIO);
Filipe Manana704de492014-10-06 22:14:22 +01001893
Chris Masond3977122009-01-05 21:25:51 -05001894 while (nr_pages > 0) {
Liu Bo873695b2017-02-02 17:49:22 -08001895 ret = find_get_pages_contig(mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001896 min_t(unsigned long,
1897 nr_pages, ARRAY_SIZE(pages)), pages);
Liu Boda2c7002017-02-10 16:41:05 +01001898 if (ret == 0) {
1899 /*
1900 * Only if we're going to lock these pages,
1901 * can we find nothing at @index.
1902 */
1903 ASSERT(page_ops & PAGE_LOCK);
Liu Bo49d4a332017-03-06 18:20:56 -08001904 err = -EAGAIN;
1905 goto out;
Liu Boda2c7002017-02-10 16:41:05 +01001906 }
Chris Mason8b62b722009-09-02 16:53:46 -04001907
Liu Boda2c7002017-02-10 16:41:05 +01001908 for (i = 0; i < ret; i++) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001909 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001910 SetPagePrivate2(pages[i]);
1911
Chris Mason1d53c9e2019-07-10 12:28:16 -07001912 if (locked_page && pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001913 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001914 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001915 continue;
1916 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001917 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001918 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001919 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001920 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001921 if (page_ops & PAGE_SET_ERROR)
1922 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001923 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001924 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001925 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001926 unlock_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001927 if (page_ops & PAGE_LOCK) {
1928 lock_page(pages[i]);
1929 if (!PageDirty(pages[i]) ||
1930 pages[i]->mapping != mapping) {
1931 unlock_page(pages[i]);
1932 put_page(pages[i]);
1933 err = -EAGAIN;
1934 goto out;
1935 }
1936 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001937 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001938 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001939 }
1940 nr_pages -= ret;
1941 index += ret;
1942 cond_resched();
1943 }
Liu Boda2c7002017-02-10 16:41:05 +01001944out:
1945 if (err && index_ret)
1946 *index_ret = start_index + pages_locked - 1;
1947 return err;
Chris Masonc8b97812008-10-29 14:49:59 -04001948}
Chris Masonc8b97812008-10-29 14:49:59 -04001949
Liu Bo873695b2017-02-02 17:49:22 -08001950void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001951 struct page *locked_page,
1952 unsigned clear_bits,
1953 unsigned long page_ops)
Liu Bo873695b2017-02-02 17:49:22 -08001954{
1955 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001956 NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001957
1958 __process_pages_contig(inode->i_mapping, locked_page,
1959 start >> PAGE_SHIFT, end >> PAGE_SHIFT,
Liu Boda2c7002017-02-10 16:41:05 +01001960 page_ops, NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001961}
1962
Chris Masond352ac62008-09-29 15:18:18 -04001963/*
1964 * count the number of bytes in the tree that have a given bit(s)
1965 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1966 * cached. The total number found is returned.
1967 */
Chris Masond1310b22008-01-24 16:13:08 -05001968u64 count_range_bits(struct extent_io_tree *tree,
1969 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001970 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001971{
1972 struct rb_node *node;
1973 struct extent_state *state;
1974 u64 cur_start = *start;
1975 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001976 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001977 int found = 0;
1978
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301979 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001980 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001981
Chris Masoncad321a2008-12-17 14:51:42 -05001982 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001983 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1984 total_bytes = tree->dirty_bytes;
1985 goto out;
1986 }
1987 /*
1988 * this search will find all the extents that end after
1989 * our range starts.
1990 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001991 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001992 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001993 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001994
Chris Masond3977122009-01-05 21:25:51 -05001995 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001996 state = rb_entry(node, struct extent_state, rb_node);
1997 if (state->start > search_end)
1998 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001999 if (contig && found && state->start > last + 1)
2000 break;
2001 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05002002 total_bytes += min(search_end, state->end) + 1 -
2003 max(cur_start, state->start);
2004 if (total_bytes >= max_bytes)
2005 break;
2006 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04002007 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05002008 found = 1;
2009 }
Chris Masonec29ed52011-02-23 16:23:20 -05002010 last = state->end;
2011 } else if (contig && found) {
2012 break;
Chris Masond1310b22008-01-24 16:13:08 -05002013 }
2014 node = rb_next(node);
2015 if (!node)
2016 break;
2017 }
2018out:
Chris Masoncad321a2008-12-17 14:51:42 -05002019 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002020 return total_bytes;
2021}
Christoph Hellwigb2950862008-12-02 09:54:17 -05002022
Chris Masond352ac62008-09-29 15:18:18 -04002023/*
2024 * set the private field for a given byte offset in the tree. If there isn't
2025 * an extent_state there already, this does nothing.
2026 */
Josef Bacikb3f167a2019-09-23 10:05:21 -04002027int set_state_failrec(struct extent_io_tree *tree, u64 start,
2028 struct io_failure_record *failrec)
Chris Masond1310b22008-01-24 16:13:08 -05002029{
2030 struct rb_node *node;
2031 struct extent_state *state;
2032 int ret = 0;
2033
Chris Masoncad321a2008-12-17 14:51:42 -05002034 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002035 /*
2036 * this search will find all the extents that end after
2037 * our range starts.
2038 */
Chris Mason80ea96b2008-02-01 14:51:59 -05002039 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04002040 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05002041 ret = -ENOENT;
2042 goto out;
2043 }
2044 state = rb_entry(node, struct extent_state, rb_node);
2045 if (state->start != start) {
2046 ret = -ENOENT;
2047 goto out;
2048 }
David Sterba47dc1962016-02-11 13:24:13 +01002049 state->failrec = failrec;
Chris Masond1310b22008-01-24 16:13:08 -05002050out:
Chris Masoncad321a2008-12-17 14:51:42 -05002051 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002052 return ret;
2053}
2054
Josef Bacikb3f167a2019-09-23 10:05:21 -04002055int get_state_failrec(struct extent_io_tree *tree, u64 start,
2056 struct io_failure_record **failrec)
Chris Masond1310b22008-01-24 16:13:08 -05002057{
2058 struct rb_node *node;
2059 struct extent_state *state;
2060 int ret = 0;
2061
Chris Masoncad321a2008-12-17 14:51:42 -05002062 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002063 /*
2064 * this search will find all the extents that end after
2065 * our range starts.
2066 */
Chris Mason80ea96b2008-02-01 14:51:59 -05002067 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04002068 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05002069 ret = -ENOENT;
2070 goto out;
2071 }
2072 state = rb_entry(node, struct extent_state, rb_node);
2073 if (state->start != start) {
2074 ret = -ENOENT;
2075 goto out;
2076 }
David Sterba47dc1962016-02-11 13:24:13 +01002077 *failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05002078out:
Chris Masoncad321a2008-12-17 14:51:42 -05002079 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002080 return ret;
2081}
2082
2083/*
2084 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05002085 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05002086 * has the bits set. Otherwise, 1 is returned if any bit in the
2087 * range is found set.
2088 */
2089int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01002090 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05002091{
2092 struct extent_state *state = NULL;
2093 struct rb_node *node;
2094 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002095
Chris Masoncad321a2008-12-17 14:51:42 -05002096 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01002097 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04002098 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04002099 node = &cached->rb_node;
2100 else
2101 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05002102 while (node && start <= end) {
2103 state = rb_entry(node, struct extent_state, rb_node);
2104
2105 if (filled && state->start > start) {
2106 bitset = 0;
2107 break;
2108 }
2109
2110 if (state->start > end)
2111 break;
2112
2113 if (state->state & bits) {
2114 bitset = 1;
2115 if (!filled)
2116 break;
2117 } else if (filled) {
2118 bitset = 0;
2119 break;
2120 }
Chris Mason46562ce2009-09-23 20:23:16 -04002121
2122 if (state->end == (u64)-1)
2123 break;
2124
Chris Masond1310b22008-01-24 16:13:08 -05002125 start = state->end + 1;
2126 if (start > end)
2127 break;
2128 node = rb_next(node);
2129 if (!node) {
2130 if (filled)
2131 bitset = 0;
2132 break;
2133 }
2134 }
Chris Masoncad321a2008-12-17 14:51:42 -05002135 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002136 return bitset;
2137}
Chris Masond1310b22008-01-24 16:13:08 -05002138
2139/*
2140 * helper function to set a given page up to date if all the
2141 * extents in the tree for that page are up to date
2142 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002143static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05002144{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002145 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002146 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04002147 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05002148 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002149}
2150
Josef Bacik7870d082017-05-05 11:57:15 -04002151int free_io_failure(struct extent_io_tree *failure_tree,
2152 struct extent_io_tree *io_tree,
2153 struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002154{
2155 int ret;
2156 int err = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002157
David Sterba47dc1962016-02-11 13:24:13 +01002158 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002159 ret = clear_extent_bits(failure_tree, rec->start,
2160 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02002161 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002162 if (ret)
2163 err = ret;
2164
Josef Bacik7870d082017-05-05 11:57:15 -04002165 ret = clear_extent_bits(io_tree, rec->start,
David Woodhouse53b381b2013-01-29 18:40:14 -05002166 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02002167 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05002168 if (ret && !err)
2169 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002170
2171 kfree(rec);
2172 return err;
2173}
2174
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002175/*
2176 * this bypasses the standard btrfs submit functions deliberately, as
2177 * the standard behavior is to write all copies in a raid setup. here we only
2178 * want to write the one bad copy. so we do the mapping for ourselves and issue
2179 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002180 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002181 * actually prevents the read that triggered the error from finishing.
2182 * currently, there can be no more than two copies of every data bit. thus,
2183 * exactly one rewrite is required.
2184 */
Josef Bacik6ec656b2017-05-05 11:57:14 -04002185int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
2186 u64 length, u64 logical, struct page *page,
2187 unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002188{
2189 struct bio *bio;
2190 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002191 u64 map_length = 0;
2192 u64 sector;
2193 struct btrfs_bio *bbio = NULL;
2194 int ret;
2195
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002196 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002197 BUG_ON(!mirror_num);
2198
David Sterbac5e4c3d2017-06-12 17:29:41 +02002199 bio = btrfs_io_bio_alloc(1);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002200 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002201 map_length = length;
2202
Filipe Mananab5de8d02016-05-27 22:21:27 +01002203 /*
2204 * Avoid races with device replace and make sure our bbio has devices
2205 * associated to its stripes that don't go away while we are doing the
2206 * read repair operation.
2207 */
2208 btrfs_bio_counter_inc_blocked(fs_info);
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03002209 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
Liu Boc7253282017-03-29 10:53:58 -07002210 /*
2211 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
2212 * to update all raid stripes, but here we just want to correct
2213 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2214 * stripe's dev and sector.
2215 */
2216 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2217 &map_length, &bbio, 0);
2218 if (ret) {
2219 btrfs_bio_counter_dec(fs_info);
2220 bio_put(bio);
2221 return -EIO;
2222 }
2223 ASSERT(bbio->mirror_num == 1);
2224 } else {
2225 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2226 &map_length, &bbio, mirror_num);
2227 if (ret) {
2228 btrfs_bio_counter_dec(fs_info);
2229 bio_put(bio);
2230 return -EIO;
2231 }
2232 BUG_ON(mirror_num != bbio->mirror_num);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002233 }
Liu Boc7253282017-03-29 10:53:58 -07002234
2235 sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002236 bio->bi_iter.bi_sector = sector;
Liu Boc7253282017-03-29 10:53:58 -07002237 dev = bbio->stripes[bbio->mirror_num - 1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002238 btrfs_put_bbio(bbio);
Anand Jainebbede42017-12-04 12:54:52 +08002239 if (!dev || !dev->bdev ||
2240 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002241 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002242 bio_put(bio);
2243 return -EIO;
2244 }
Christoph Hellwig74d46992017-08-23 19:10:32 +02002245 bio_set_dev(bio, dev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002246 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Miao Xieffdd2012014-09-12 18:44:00 +08002247 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002248
Mike Christie4e49ea42016-06-05 14:31:41 -05002249 if (btrfsic_submit_bio_wait(bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002250 /* try to remap that extent elsewhere? */
Filipe Mananab5de8d02016-05-27 22:21:27 +01002251 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002252 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002253 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002254 return -EIO;
2255 }
2256
David Sterbab14af3b2015-10-08 10:43:10 +02002257 btrfs_info_rl_in_rcu(fs_info,
2258 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Josef Bacik6ec656b2017-05-05 11:57:14 -04002259 ino, start,
Miao Xie1203b682014-09-12 18:44:01 +08002260 rcu_str_deref(dev->name), sector);
Filipe Mananab5de8d02016-05-27 22:21:27 +01002261 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002262 bio_put(bio);
2263 return 0;
2264}
2265
David Sterba20a1fbf92019-03-20 11:23:44 +01002266int btrfs_repair_eb_io_failure(struct extent_buffer *eb, int mirror_num)
Josef Bacikea466792012-03-26 21:57:36 -04002267{
David Sterba20a1fbf92019-03-20 11:23:44 +01002268 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacikea466792012-03-26 21:57:36 -04002269 u64 start = eb->start;
David Sterbacc5e31a2018-03-01 18:20:27 +01002270 int i, num_pages = num_extent_pages(eb);
Chris Masond95603b2012-04-12 15:55:15 -04002271 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002272
David Howellsbc98a422017-07-17 08:45:34 +01002273 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002274 return -EROFS;
2275
Josef Bacikea466792012-03-26 21:57:36 -04002276 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002277 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002278
Josef Bacik6ec656b2017-05-05 11:57:14 -04002279 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002280 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002281 if (ret)
2282 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002283 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002284 }
2285
2286 return ret;
2287}
2288
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002289/*
2290 * each time an IO finishes, we do a fast check in the IO failure tree
2291 * to see if we need to process or clean up an io_failure_record
2292 */
Josef Bacik7870d082017-05-05 11:57:15 -04002293int clean_io_failure(struct btrfs_fs_info *fs_info,
2294 struct extent_io_tree *failure_tree,
2295 struct extent_io_tree *io_tree, u64 start,
2296 struct page *page, u64 ino, unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002297{
2298 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002299 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002300 struct extent_state *state;
2301 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002302 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002303
2304 private = 0;
Josef Bacik7870d082017-05-05 11:57:15 -04002305 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2306 EXTENT_DIRTY, 0);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002307 if (!ret)
2308 return 0;
2309
Josef Bacik7870d082017-05-05 11:57:15 -04002310 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002311 if (ret)
2312 return 0;
2313
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002314 BUG_ON(!failrec->this_mirror);
2315
2316 if (failrec->in_validation) {
2317 /* there was no real error, just free the record */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002318 btrfs_debug(fs_info,
2319 "clean_io_failure: freeing dummy error at %llu",
2320 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002321 goto out;
2322 }
David Howellsbc98a422017-07-17 08:45:34 +01002323 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002324 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002325
Josef Bacik7870d082017-05-05 11:57:15 -04002326 spin_lock(&io_tree->lock);
2327 state = find_first_extent_bit_state(io_tree,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002328 failrec->start,
2329 EXTENT_LOCKED);
Josef Bacik7870d082017-05-05 11:57:15 -04002330 spin_unlock(&io_tree->lock);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002331
Miao Xie883d0de2013-07-25 19:22:35 +08002332 if (state && state->start <= failrec->start &&
2333 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002334 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2335 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002336 if (num_copies > 1) {
Josef Bacik7870d082017-05-05 11:57:15 -04002337 repair_io_failure(fs_info, ino, start, failrec->len,
2338 failrec->logical, page, pg_offset,
2339 failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002340 }
2341 }
2342
2343out:
Josef Bacik7870d082017-05-05 11:57:15 -04002344 free_io_failure(failure_tree, io_tree, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002345
Miao Xie454ff3d2014-09-12 18:43:58 +08002346 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002347}
2348
Miao Xief6124962014-09-12 18:44:04 +08002349/*
2350 * Can be called when
2351 * - hold extent lock
2352 * - under ordered extent
2353 * - the inode is freeing
2354 */
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002355void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
Miao Xief6124962014-09-12 18:44:04 +08002356{
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002357 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
Miao Xief6124962014-09-12 18:44:04 +08002358 struct io_failure_record *failrec;
2359 struct extent_state *state, *next;
2360
2361 if (RB_EMPTY_ROOT(&failure_tree->state))
2362 return;
2363
2364 spin_lock(&failure_tree->lock);
2365 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2366 while (state) {
2367 if (state->start > end)
2368 break;
2369
2370 ASSERT(state->end <= end);
2371
2372 next = next_state(state);
2373
David Sterba47dc1962016-02-11 13:24:13 +01002374 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002375 free_extent_state(state);
2376 kfree(failrec);
2377
2378 state = next;
2379 }
2380 spin_unlock(&failure_tree->lock);
2381}
2382
Miao Xie2fe63032014-09-12 18:43:59 +08002383int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
David Sterba47dc1962016-02-11 13:24:13 +01002384 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002385{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002386 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002387 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002388 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002389 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2390 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2391 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002392 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002393 u64 logical;
2394
David Sterba47dc1962016-02-11 13:24:13 +01002395 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002396 if (ret) {
2397 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2398 if (!failrec)
2399 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002400
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002401 failrec->start = start;
2402 failrec->len = end - start + 1;
2403 failrec->this_mirror = 0;
2404 failrec->bio_flags = 0;
2405 failrec->in_validation = 0;
2406
2407 read_lock(&em_tree->lock);
2408 em = lookup_extent_mapping(em_tree, start, failrec->len);
2409 if (!em) {
2410 read_unlock(&em_tree->lock);
2411 kfree(failrec);
2412 return -EIO;
2413 }
2414
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002415 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002416 free_extent_map(em);
2417 em = NULL;
2418 }
2419 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002420 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002421 kfree(failrec);
2422 return -EIO;
2423 }
Miao Xie2fe63032014-09-12 18:43:59 +08002424
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002425 logical = start - em->start;
2426 logical = em->block_start + logical;
2427 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2428 logical = em->block_start;
2429 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2430 extent_set_compress_type(&failrec->bio_flags,
2431 em->compress_type);
2432 }
Miao Xie2fe63032014-09-12 18:43:59 +08002433
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002434 btrfs_debug(fs_info,
2435 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2436 logical, start, failrec->len);
Miao Xie2fe63032014-09-12 18:43:59 +08002437
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002438 failrec->logical = logical;
2439 free_extent_map(em);
2440
2441 /* set the bits in the private failure tree */
2442 ret = set_extent_bits(failure_tree, start, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002443 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002444 if (ret >= 0)
David Sterba47dc1962016-02-11 13:24:13 +01002445 ret = set_state_failrec(failure_tree, start, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002446 /* set the bits in the inode's tree */
2447 if (ret >= 0)
David Sterbaceeb0ae2016-04-26 23:54:39 +02002448 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002449 if (ret < 0) {
2450 kfree(failrec);
2451 return ret;
2452 }
2453 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002454 btrfs_debug(fs_info,
2455 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2456 failrec->logical, failrec->start, failrec->len,
2457 failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002458 /*
2459 * when data can be on disk more than twice, add to failrec here
2460 * (e.g. with a list for failed_mirror) to make
2461 * clean_io_failure() clean all those errors at once.
2462 */
2463 }
Miao Xie2fe63032014-09-12 18:43:59 +08002464
2465 *failrec_ret = failrec;
2466
2467 return 0;
2468}
2469
Ming Leia0b60d72017-12-18 20:22:11 +08002470bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages,
Miao Xie2fe63032014-09-12 18:43:59 +08002471 struct io_failure_record *failrec, int failed_mirror)
2472{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002473 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002474 int num_copies;
2475
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002476 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002477 if (num_copies == 1) {
2478 /*
2479 * we only have a single copy of the data, so don't bother with
2480 * all the retry and error correction code that follows. no
2481 * matter what the error is, it is very likely to persist.
2482 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002483 btrfs_debug(fs_info,
2484 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2485 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002486 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002487 }
2488
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002489 /*
2490 * there are two premises:
2491 * a) deliver good data to the caller
2492 * b) correct the bad sectors on disk
2493 */
Ming Leia0b60d72017-12-18 20:22:11 +08002494 if (failed_bio_pages > 1) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002495 /*
2496 * to fulfill b), we need to know the exact failing sectors, as
2497 * we don't want to rewrite any more than the failed ones. thus,
2498 * we need separate read requests for the failed bio
2499 *
2500 * if the following BUG_ON triggers, our validation request got
2501 * merged. we need separate requests for our algorithm to work.
2502 */
2503 BUG_ON(failrec->in_validation);
2504 failrec->in_validation = 1;
2505 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002506 } else {
2507 /*
2508 * we're ready to fulfill a) and b) alongside. get a good copy
2509 * of the failed sector and if we succeed, we have setup
2510 * everything for repair_io_failure to do the rest for us.
2511 */
2512 if (failrec->in_validation) {
2513 BUG_ON(failrec->this_mirror != failed_mirror);
2514 failrec->in_validation = 0;
2515 failrec->this_mirror = 0;
2516 }
2517 failrec->failed_mirror = failed_mirror;
2518 failrec->this_mirror++;
2519 if (failrec->this_mirror == failed_mirror)
2520 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002521 }
2522
Miao Xiefacc8a222013-07-25 19:22:34 +08002523 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002524 btrfs_debug(fs_info,
2525 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2526 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002527 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002528 }
2529
Liu Boc3cfb652017-07-13 15:00:50 -07002530 return true;
Miao Xie2fe63032014-09-12 18:43:59 +08002531}
2532
2533
2534struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2535 struct io_failure_record *failrec,
2536 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002537 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002538{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002539 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002540 struct bio *bio;
2541 struct btrfs_io_bio *btrfs_failed_bio;
2542 struct btrfs_io_bio *btrfs_bio;
2543
David Sterbac5e4c3d2017-06-12 17:29:41 +02002544 bio = btrfs_io_bio_alloc(1);
Miao Xie2fe63032014-09-12 18:43:59 +08002545 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002546 bio->bi_iter.bi_sector = failrec->logical >> 9;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002547 bio_set_dev(bio, fs_info->fs_devices->latest_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002548 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002549 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002550
Miao Xiefacc8a222013-07-25 19:22:34 +08002551 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2552 if (btrfs_failed_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002553 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2554
2555 btrfs_bio = btrfs_io_bio(bio);
2556 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002557 icsum *= csum_size;
2558 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002559 csum_size);
2560 }
2561
Miao Xie2fe63032014-09-12 18:43:59 +08002562 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002563
Miao Xie2fe63032014-09-12 18:43:59 +08002564 return bio;
2565}
2566
2567/*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002568 * This is a generic handler for readpage errors. If other copies exist, read
2569 * those and write back good data to the failed position. Does not investigate
2570 * in remapping the failed extent elsewhere, hoping the device will be smart
2571 * enough to do this as needed
Miao Xie2fe63032014-09-12 18:43:59 +08002572 */
Miao Xie2fe63032014-09-12 18:43:59 +08002573static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2574 struct page *page, u64 start, u64 end,
2575 int failed_mirror)
2576{
2577 struct io_failure_record *failrec;
2578 struct inode *inode = page->mapping->host;
2579 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002580 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie2fe63032014-09-12 18:43:59 +08002581 struct bio *bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002582 int read_mode = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002583 blk_status_t status;
Miao Xie2fe63032014-09-12 18:43:59 +08002584 int ret;
Christoph Hellwig8a2ee442019-02-15 19:13:07 +08002585 unsigned failed_bio_pages = failed_bio->bi_iter.bi_size >> PAGE_SHIFT;
Miao Xie2fe63032014-09-12 18:43:59 +08002586
Mike Christie1f7ad752016-06-05 14:31:51 -05002587 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002588
2589 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2590 if (ret)
2591 return ret;
2592
Ming Leia0b60d72017-12-18 20:22:11 +08002593 if (!btrfs_check_repairable(inode, failed_bio_pages, failrec,
Liu Boc3cfb652017-07-13 15:00:50 -07002594 failed_mirror)) {
Josef Bacik7870d082017-05-05 11:57:15 -04002595 free_io_failure(failure_tree, tree, failrec);
Miao Xie2fe63032014-09-12 18:43:59 +08002596 return -EIO;
2597 }
2598
Ming Leia0b60d72017-12-18 20:22:11 +08002599 if (failed_bio_pages > 1)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002600 read_mode |= REQ_FAILFAST_DEV;
Miao Xie2fe63032014-09-12 18:43:59 +08002601
2602 phy_offset >>= inode->i_sb->s_blocksize_bits;
2603 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2604 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002605 (int)phy_offset, failed_bio->bi_end_io,
2606 NULL);
David Sterbaebcc3262018-06-29 10:56:53 +02002607 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie2fe63032014-09-12 18:43:59 +08002608
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002609 btrfs_debug(btrfs_sb(inode->i_sb),
2610 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2611 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002612
Linus Torvalds8c27cb32017-07-05 16:41:23 -07002613 status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002614 failrec->bio_flags);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002615 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04002616 free_io_failure(failure_tree, tree, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002617 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002618 ret = blk_status_to_errno(status);
Miao Xie6c387ab2014-09-12 18:43:57 +08002619 }
2620
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002621 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002622}
2623
Chris Masond1310b22008-01-24 16:13:08 -05002624/* lots and lots of room for performance fixes in the end_bio funcs */
2625
David Sterbab5227c02015-12-03 13:08:59 +01002626void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002627{
2628 int uptodate = (err == 0);
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002629 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002630
Nikolay Borisovc6297322018-11-08 10:18:08 +02002631 btrfs_writepage_endio_finish_ordered(page, start, end, uptodate);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002632
Jeff Mahoney87826df2012-02-15 16:23:57 +01002633 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002634 ClearPageUptodate(page);
2635 SetPageError(page);
Colin Ian Kingbff5baf2017-05-09 18:14:01 +01002636 ret = err < 0 ? err : -EIO;
Liu Bo5dca6ee2014-05-12 12:47:36 +08002637 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002638 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002639}
2640
Chris Masond1310b22008-01-24 16:13:08 -05002641/*
2642 * after a writepage IO is done, we need to:
2643 * clear the uptodate bits on error
2644 * clear the writeback bits in the extent tree for this IO
2645 * end_page_writeback if the page has no more pending IO
2646 *
2647 * Scheduling is not allowed, so the extent state tree is expected
2648 * to have one and only one object corresponding to this IO.
2649 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002650static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002651{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002652 int error = blk_status_to_errno(bio->bi_status);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002653 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002654 u64 start;
2655 u64 end;
Ming Lei6dc4f102019-02-15 19:13:19 +08002656 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002657
David Sterbac09abff2017-07-13 18:10:07 +02002658 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02002659 bio_for_each_segment_all(bvec, bio, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002660 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002661 struct inode *inode = page->mapping->host;
2662 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002663
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002664 /* We always issue full-page reads, but if some block
2665 * in a page fails to read, blk_update_request() will
2666 * advance bv_offset and adjust bv_len to compensate.
2667 * Print a warning for nonzero offsets, and an error
2668 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002669 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2670 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002671 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002672 "partial page write in btrfs with offset %u and length %u",
2673 bvec->bv_offset, bvec->bv_len);
2674 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002675 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002676 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002677 bvec->bv_offset, bvec->bv_len);
2678 }
Chris Masond1310b22008-01-24 16:13:08 -05002679
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002680 start = page_offset(page);
2681 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002682
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002683 end_extent_writepage(page, error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002684 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002685 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002686
Chris Masond1310b22008-01-24 16:13:08 -05002687 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002688}
2689
Miao Xie883d0de2013-07-25 19:22:35 +08002690static void
2691endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2692 int uptodate)
2693{
2694 struct extent_state *cached = NULL;
2695 u64 end = start + len - 1;
2696
2697 if (uptodate && tree->track_uptodate)
2698 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
David Sterbad810a4b2017-12-07 18:52:54 +01002699 unlock_extent_cached_atomic(tree, start, end, &cached);
Miao Xie883d0de2013-07-25 19:22:35 +08002700}
2701
Chris Masond1310b22008-01-24 16:13:08 -05002702/*
2703 * after a readpage IO is done, we need to:
2704 * clear the uptodate bits on error
2705 * set the uptodate bits if things worked
2706 * set the page up to date if all extents in the tree are uptodate
2707 * clear the lock bit in the extent tree
2708 * unlock the page if there are no other extents locked for it
2709 *
2710 * Scheduling is not allowed, so the extent state tree is expected
2711 * to have one and only one object corresponding to this IO.
2712 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002713static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002714{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002715 struct bio_vec *bvec;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002716 int uptodate = !bio->bi_status;
Miao Xiefacc8a222013-07-25 19:22:34 +08002717 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04002718 struct extent_io_tree *tree, *failure_tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002719 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002720 u64 start;
2721 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002722 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002723 u64 extent_start = 0;
2724 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002725 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002726 int ret;
Ming Lei6dc4f102019-02-15 19:13:19 +08002727 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002728
David Sterbac09abff2017-07-13 18:10:07 +02002729 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02002730 bio_for_each_segment_all(bvec, bio, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002731 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002732 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002733 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002734 bool data_inode = btrfs_ino(BTRFS_I(inode))
2735 != BTRFS_BTREE_INODE_OBJECTID;
Arne Jansen507903b2011-04-06 10:02:20 +00002736
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002737 btrfs_debug(fs_info,
2738 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002739 (u64)bio->bi_iter.bi_sector, bio->bi_status,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002740 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002741 tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002742 failure_tree = &BTRFS_I(inode)->io_failure_tree;
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 Mahoneyab8d0fc2016-09-20 10:05:02 -04002751 btrfs_err(fs_info,
2752 "partial page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002753 bvec->bv_offset, bvec->bv_len);
2754 else
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002755 btrfs_info(fs_info,
2756 "incomplete page read 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;
Miao Xiefacc8a222013-07-25 19:22:34 +08002762 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002763
Chris Mason9be33952013-05-17 18:30:14 -04002764 mirror = io_bio->mirror_num;
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002765 if (likely(uptodate)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002766 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2767 page, start, end,
2768 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002769 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002770 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002771 else
Josef Bacik7870d082017-05-05 11:57:15 -04002772 clean_io_failure(BTRFS_I(inode)->root->fs_info,
2773 failure_tree, tree, start,
2774 page,
2775 btrfs_ino(BTRFS_I(inode)), 0);
Chris Masond1310b22008-01-24 16:13:08 -05002776 }
Josef Bacikea466792012-03-26 21:57:36 -04002777
Miao Xief2a09da2013-07-25 19:22:33 +08002778 if (likely(uptodate))
2779 goto readpage_ok;
2780
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002781 if (data_inode) {
Liu Bo9d0d1c82017-03-24 15:04:50 -07002782
2783 /*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002784 * The generic bio_readpage_error handles errors the
2785 * following way: If possible, new read requests are
2786 * created and submitted and will end up in
2787 * end_bio_extent_readpage as well (if we're lucky,
2788 * not in the !uptodate case). In that case it returns
2789 * 0 and we just go on with the next page in our bio.
2790 * If it can't handle the error it will return -EIO and
2791 * we remain responsible for that page.
Liu Bo9d0d1c82017-03-24 15:04:50 -07002792 */
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002793 ret = bio_readpage_error(bio, offset, page, start, end,
2794 mirror);
2795 if (ret == 0) {
2796 uptodate = !bio->bi_status;
2797 offset += len;
2798 continue;
2799 }
2800 } else {
2801 struct extent_buffer *eb;
2802
2803 eb = (struct extent_buffer *)page->private;
2804 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
2805 eb->read_mirror = mirror;
2806 atomic_dec(&eb->io_pages);
2807 if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD,
2808 &eb->bflags))
2809 btree_readahead_hook(eb, -EIO);
Chris Mason7e383262008-04-09 16:28:12 -04002810 }
Miao Xief2a09da2013-07-25 19:22:33 +08002811readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002812 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002813 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002814 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002815 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002816
2817 /* Zero out the end if this page straddles i_size */
Johannes Thumshirn70730172018-12-05 15:23:03 +01002818 off = offset_in_page(i_size);
Liu Boa583c022014-08-19 23:32:22 +08002819 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002820 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002821 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002822 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002823 ClearPageUptodate(page);
2824 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002825 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002826 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002827 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002828
2829 if (unlikely(!uptodate)) {
2830 if (extent_len) {
2831 endio_readpage_release_extent(tree,
2832 extent_start,
2833 extent_len, 1);
2834 extent_start = 0;
2835 extent_len = 0;
2836 }
2837 endio_readpage_release_extent(tree, start,
2838 end - start + 1, 0);
2839 } else if (!extent_len) {
2840 extent_start = start;
2841 extent_len = end + 1 - start;
2842 } else if (extent_start + extent_len == start) {
2843 extent_len += end + 1 - start;
2844 } else {
2845 endio_readpage_release_extent(tree, extent_start,
2846 extent_len, uptodate);
2847 extent_start = start;
2848 extent_len = end + 1 - start;
2849 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002850 }
Chris Masond1310b22008-01-24 16:13:08 -05002851
Miao Xie883d0de2013-07-25 19:22:35 +08002852 if (extent_len)
2853 endio_readpage_release_extent(tree, extent_start, extent_len,
2854 uptodate);
David Sterbab3a0dd52018-11-22 17:16:49 +01002855 btrfs_io_bio_free_csum(io_bio);
Chris Masond1310b22008-01-24 16:13:08 -05002856 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002857}
2858
Chris Mason9be33952013-05-17 18:30:14 -04002859/*
David Sterba184f9992017-06-12 17:29:39 +02002860 * Initialize the members up to but not including 'bio'. Use after allocating a
2861 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
2862 * 'bio' because use of __GFP_ZERO is not supported.
Chris Mason9be33952013-05-17 18:30:14 -04002863 */
David Sterba184f9992017-06-12 17:29:39 +02002864static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
Chris Masond1310b22008-01-24 16:13:08 -05002865{
David Sterba184f9992017-06-12 17:29:39 +02002866 memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
2867}
2868
2869/*
David Sterba6e707bc2017-06-02 17:26:26 +02002870 * The following helpers allocate a bio. As it's backed by a bioset, it'll
2871 * never fail. We're returning a bio right now but you can call btrfs_io_bio
2872 * for the appropriate container_of magic
Chris Masond1310b22008-01-24 16:13:08 -05002873 */
David Sterbae749af442019-06-18 20:00:16 +02002874struct bio *btrfs_bio_alloc(u64 first_byte)
Chris Masond1310b22008-01-24 16:13:08 -05002875{
2876 struct bio *bio;
2877
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002878 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset);
David Sterbac821e7f32017-06-02 18:35:36 +02002879 bio->bi_iter.bi_sector = first_byte >> 9;
David Sterba184f9992017-06-12 17:29:39 +02002880 btrfs_io_bio_init(btrfs_io_bio(bio));
Chris Masond1310b22008-01-24 16:13:08 -05002881 return bio;
2882}
2883
David Sterba8b6c1d52017-06-02 17:48:13 +02002884struct bio *btrfs_bio_clone(struct bio *bio)
Chris Mason9be33952013-05-17 18:30:14 -04002885{
Miao Xie23ea8e52014-09-12 18:43:54 +08002886 struct btrfs_io_bio *btrfs_bio;
2887 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002888
David Sterba6e707bc2017-06-02 17:26:26 +02002889 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002890 new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset);
David Sterba6e707bc2017-06-02 17:26:26 +02002891 btrfs_bio = btrfs_io_bio(new);
David Sterba184f9992017-06-12 17:29:39 +02002892 btrfs_io_bio_init(btrfs_bio);
David Sterba6e707bc2017-06-02 17:26:26 +02002893 btrfs_bio->iter = bio->bi_iter;
Miao Xie23ea8e52014-09-12 18:43:54 +08002894 return new;
2895}
Chris Mason9be33952013-05-17 18:30:14 -04002896
David Sterbac5e4c3d2017-06-12 17:29:41 +02002897struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
Chris Mason9be33952013-05-17 18:30:14 -04002898{
Miao Xiefacc8a222013-07-25 19:22:34 +08002899 struct bio *bio;
2900
David Sterba6e707bc2017-06-02 17:26:26 +02002901 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002902 bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
David Sterba184f9992017-06-12 17:29:39 +02002903 btrfs_io_bio_init(btrfs_io_bio(bio));
Miao Xiefacc8a222013-07-25 19:22:34 +08002904 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002905}
2906
Liu Boe4770942017-05-16 10:57:14 -07002907struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
Liu Bo2f8e9142017-05-15 17:43:31 -07002908{
2909 struct bio *bio;
2910 struct btrfs_io_bio *btrfs_bio;
2911
2912 /* this will never fail when it's backed by a bioset */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002913 bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset);
Liu Bo2f8e9142017-05-15 17:43:31 -07002914 ASSERT(bio);
2915
2916 btrfs_bio = btrfs_io_bio(bio);
David Sterba184f9992017-06-12 17:29:39 +02002917 btrfs_io_bio_init(btrfs_bio);
Liu Bo2f8e9142017-05-15 17:43:31 -07002918
2919 bio_trim(bio, offset >> 9, size >> 9);
Liu Bo17347ce2017-05-15 15:33:27 -07002920 btrfs_bio->iter = bio->bi_iter;
Liu Bo2f8e9142017-05-15 17:43:31 -07002921 return bio;
2922}
Chris Mason9be33952013-05-17 18:30:14 -04002923
David Sterba4b81ba42017-06-06 19:14:26 +02002924/*
2925 * @opf: bio REQ_OP_* and REQ_* flags as one value
David Sterbab8b3d622017-06-12 19:50:41 +02002926 * @tree: tree so we can call our merge_bio hook
2927 * @wbc: optional writeback control for io accounting
2928 * @page: page to add to the bio
2929 * @pg_offset: offset of the new bio or to check whether we are adding
2930 * a contiguous page to the previous one
2931 * @size: portion of page that we want to write
2932 * @offset: starting offset in the page
2933 * @bdev: attach newly created bios to this bdev
David Sterba5c2b1fd2017-06-06 19:22:55 +02002934 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
David Sterbab8b3d622017-06-12 19:50:41 +02002935 * @end_io_func: end_io callback for new bio
2936 * @mirror_num: desired mirror to read/write
2937 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
2938 * @bio_flags: flags of the current bio to see if we can merge them
David Sterba4b81ba42017-06-06 19:14:26 +02002939 */
2940static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002941 struct writeback_control *wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02002942 struct page *page, u64 offset,
David Sterba6c5a4e22017-10-04 17:10:34 +02002943 size_t size, unsigned long pg_offset,
Chris Masond1310b22008-01-24 16:13:08 -05002944 struct block_device *bdev,
2945 struct bio **bio_ret,
Chris Masonf1885912008-04-09 16:28:12 -04002946 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002947 int mirror_num,
2948 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002949 unsigned long bio_flags,
2950 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002951{
2952 int ret = 0;
2953 struct bio *bio;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002954 size_t page_size = min_t(size_t, size, PAGE_SIZE);
David Sterba6273b7f2017-10-04 17:30:11 +02002955 sector_t sector = offset >> 9;
Chris Masond1310b22008-01-24 16:13:08 -05002956
David Sterba5c2b1fd2017-06-06 19:22:55 +02002957 ASSERT(bio_ret);
2958
2959 if (*bio_ret) {
David Sterba0c8508a2017-06-12 20:00:43 +02002960 bool contig;
2961 bool can_merge = true;
2962
Chris Masond1310b22008-01-24 16:13:08 -05002963 bio = *bio_ret;
David Sterba0c8508a2017-06-12 20:00:43 +02002964 if (prev_bio_flags & EXTENT_BIO_COMPRESSED)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002965 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002966 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002967 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002968
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002969 ASSERT(tree->ops);
2970 if (btrfs_bio_fits_in_stripe(page, page_size, bio, bio_flags))
David Sterba0c8508a2017-06-12 20:00:43 +02002971 can_merge = false;
2972
2973 if (prev_bio_flags != bio_flags || !contig || !can_merge ||
Filipe Manana005efed2015-09-14 09:09:31 +01002974 force_bio_submit ||
David Sterba6c5a4e22017-10-04 17:10:34 +02002975 bio_add_page(bio, page, page_size, pg_offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002976 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002977 if (ret < 0) {
2978 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002979 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002980 }
Chris Masond1310b22008-01-24 16:13:08 -05002981 bio = NULL;
2982 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002983 if (wbc)
Tejun Heo34e51a52019-06-27 13:39:49 -07002984 wbc_account_cgroup_owner(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002985 return 0;
2986 }
2987 }
Chris Masonc8b97812008-10-29 14:49:59 -04002988
David Sterbae749af442019-06-18 20:00:16 +02002989 bio = btrfs_bio_alloc(offset);
2990 bio_set_dev(bio, bdev);
David Sterba6c5a4e22017-10-04 17:10:34 +02002991 bio_add_page(bio, page, page_size, pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002992 bio->bi_end_io = end_io_func;
2993 bio->bi_private = tree;
Jens Axboee6959b92017-06-27 11:51:28 -06002994 bio->bi_write_hint = page->mapping->host->i_write_hint;
David Sterba4b81ba42017-06-06 19:14:26 +02002995 bio->bi_opf = opf;
Chris Masonda2f0f72015-07-02 13:57:22 -07002996 if (wbc) {
2997 wbc_init_bio(wbc, bio);
Tejun Heo34e51a52019-06-27 13:39:49 -07002998 wbc_account_cgroup_owner(wbc, page, page_size);
Chris Masonda2f0f72015-07-02 13:57:22 -07002999 }
Chris Mason70dec802008-01-29 09:59:12 -05003000
David Sterba5c2b1fd2017-06-06 19:22:55 +02003001 *bio_ret = bio;
Chris Masond1310b22008-01-24 16:13:08 -05003002
3003 return ret;
3004}
3005
Eric Sandeen48a3b632013-04-25 20:41:01 +00003006static void attach_extent_buffer_page(struct extent_buffer *eb,
3007 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003008{
3009 if (!PagePrivate(page)) {
3010 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003011 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003012 set_page_private(page, (unsigned long)eb);
3013 } else {
3014 WARN_ON(page->private != (unsigned long)eb);
3015 }
3016}
3017
Chris Masond1310b22008-01-24 16:13:08 -05003018void set_page_extent_mapped(struct page *page)
3019{
3020 if (!PagePrivate(page)) {
3021 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003022 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04003023 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05003024 }
3025}
3026
Miao Xie125bac012013-07-25 19:22:37 +08003027static struct extent_map *
3028__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
3029 u64 start, u64 len, get_extent_t *get_extent,
3030 struct extent_map **em_cached)
3031{
3032 struct extent_map *em;
3033
3034 if (em_cached && *em_cached) {
3035 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00003036 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08003037 start < extent_map_end(em)) {
Elena Reshetova490b54d2017-03-03 10:55:12 +02003038 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08003039 return em;
3040 }
3041
3042 free_extent_map(em);
3043 *em_cached = NULL;
3044 }
3045
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02003046 em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
Miao Xie125bac012013-07-25 19:22:37 +08003047 if (em_cached && !IS_ERR_OR_NULL(em)) {
3048 BUG_ON(*em_cached);
Elena Reshetova490b54d2017-03-03 10:55:12 +02003049 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08003050 *em_cached = em;
3051 }
3052 return em;
3053}
Chris Masond1310b22008-01-24 16:13:08 -05003054/*
3055 * basic readpage implementation. Locked extent state structs are inserted
3056 * into the tree that are removed when the IO is done (by the end_io
3057 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003058 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07003059 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05003060 */
Miao Xie99740902013-07-25 19:22:36 +08003061static int __do_readpage(struct extent_io_tree *tree,
3062 struct page *page,
3063 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08003064 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003065 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003066 unsigned long *bio_flags, unsigned int read_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003067 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05003068{
3069 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003070 u64 start = page_offset(page);
David Sterba8eec8292017-06-06 19:50:13 +02003071 const u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003072 u64 cur = start;
3073 u64 extent_offset;
3074 u64 last_byte = i_size_read(inode);
3075 u64 block_start;
3076 u64 cur_end;
Chris Masond1310b22008-01-24 16:13:08 -05003077 struct extent_map *em;
3078 struct block_device *bdev;
Liu Bobaf863b2016-07-11 10:39:07 -07003079 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003080 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02003081 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003082 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04003083 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05003084 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00003085 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003086
3087 set_page_extent_mapped(page);
3088
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003089 if (!PageUptodate(page)) {
3090 if (cleancache_get_page(page) == 0) {
3091 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003092 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003093 goto out;
3094 }
3095 }
3096
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003097 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04003098 char *userpage;
Johannes Thumshirn70730172018-12-05 15:23:03 +01003099 size_t zero_offset = offset_in_page(last_byte);
Chris Masonc8b97812008-10-29 14:49:59 -04003100
3101 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003102 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08003103 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04003104 memset(userpage + zero_offset, 0, iosize);
3105 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003106 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04003107 }
3108 }
Chris Masond1310b22008-01-24 16:13:08 -05003109 while (cur <= end) {
Filipe Manana005efed2015-09-14 09:09:31 +01003110 bool force_bio_submit = false;
David Sterba6273b7f2017-10-04 17:30:11 +02003111 u64 offset;
Josef Bacikc8f2f242013-02-11 11:33:00 -05003112
Chris Masond1310b22008-01-24 16:13:08 -05003113 if (cur >= last_byte) {
3114 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003115 struct extent_state *cached = NULL;
3116
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003117 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08003118 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003119 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003120 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003121 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003122 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003123 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003124 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01003125 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003126 break;
3127 }
Miao Xie125bac012013-07-25 19:22:37 +08003128 em = __get_extent_map(inode, page, pg_offset, cur,
3129 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02003130 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003131 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003132 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003133 break;
3134 }
Chris Masond1310b22008-01-24 16:13:08 -05003135 extent_offset = cur - em->start;
3136 BUG_ON(extent_map_end(em) <= cur);
3137 BUG_ON(end < cur);
3138
Li Zefan261507a02010-12-17 14:21:50 +08003139 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07003140 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08003141 extent_set_compress_type(&this_bio_flag,
3142 em->compress_type);
3143 }
Chris Masonc8b97812008-10-29 14:49:59 -04003144
Chris Masond1310b22008-01-24 16:13:08 -05003145 iosize = min(extent_map_end(em) - cur, end - cur + 1);
3146 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00003147 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04003148 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
3149 disk_io_size = em->block_len;
David Sterba6273b7f2017-10-04 17:30:11 +02003150 offset = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003151 } else {
David Sterba6273b7f2017-10-04 17:30:11 +02003152 offset = em->block_start + extent_offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003153 disk_io_size = iosize;
3154 }
Chris Masond1310b22008-01-24 16:13:08 -05003155 bdev = em->bdev;
3156 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04003157 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
3158 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01003159
3160 /*
3161 * If we have a file range that points to a compressed extent
3162 * and it's followed by a consecutive file range that points to
3163 * to the same compressed extent (possibly with a different
3164 * offset and/or length, so it either points to the whole extent
3165 * or only part of it), we must make sure we do not submit a
3166 * single bio to populate the pages for the 2 ranges because
3167 * this makes the compressed extent read zero out the pages
3168 * belonging to the 2nd range. Imagine the following scenario:
3169 *
3170 * File layout
3171 * [0 - 8K] [8K - 24K]
3172 * | |
3173 * | |
3174 * points to extent X, points to extent X,
3175 * offset 4K, length of 8K offset 0, length 16K
3176 *
3177 * [extent X, compressed length = 4K uncompressed length = 16K]
3178 *
3179 * If the bio to read the compressed extent covers both ranges,
3180 * it will decompress extent X into the pages belonging to the
3181 * first range and then it will stop, zeroing out the remaining
3182 * pages that belong to the other range that points to extent X.
3183 * So here we make sure we submit 2 bios, one for the first
3184 * range and another one for the third range. Both will target
3185 * the same physical extent from disk, but we can't currently
3186 * make the compressed bio endio callback populate the pages
3187 * for both ranges because each compressed bio is tightly
3188 * coupled with a single extent map, and each range can have
3189 * an extent map with a different offset value relative to the
3190 * uncompressed data of our extent and different lengths. This
3191 * is a corner case so we prioritize correctness over
3192 * non-optimal behavior (submitting 2 bios for the same extent).
3193 */
3194 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3195 prev_em_start && *prev_em_start != (u64)-1 &&
Filipe Manana8e928212019-02-14 15:17:20 +00003196 *prev_em_start != em->start)
Filipe Manana005efed2015-09-14 09:09:31 +01003197 force_bio_submit = true;
3198
3199 if (prev_em_start)
Filipe Manana8e928212019-02-14 15:17:20 +00003200 *prev_em_start = em->start;
Filipe Manana005efed2015-09-14 09:09:31 +01003201
Chris Masond1310b22008-01-24 16:13:08 -05003202 free_extent_map(em);
3203 em = NULL;
3204
3205 /* we've found a hole, just zero and go on */
3206 if (block_start == EXTENT_MAP_HOLE) {
3207 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003208 struct extent_state *cached = NULL;
3209
Cong Wang7ac687d2011-11-25 23:14:28 +08003210 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003211 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003212 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003213 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003214
3215 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003216 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003217 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01003218 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003219 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003220 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003221 continue;
3222 }
3223 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003224 if (test_range_bit(tree, cur, cur_end,
3225 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003226 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003227 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003228 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003229 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003230 continue;
3231 }
Chris Mason70dec802008-01-29 09:59:12 -05003232 /* we have an inline extent but it didn't get marked up
3233 * to date. Error out
3234 */
3235 if (block_start == EXTENT_MAP_INLINE) {
3236 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003237 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003238 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003239 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003240 continue;
3241 }
Chris Masond1310b22008-01-24 16:13:08 -05003242
David Sterba4b81ba42017-06-06 19:14:26 +02003243 ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL,
David Sterba6273b7f2017-10-04 17:30:11 +02003244 page, offset, disk_io_size,
3245 pg_offset, bdev, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003246 end_bio_extent_readpage, mirror_num,
3247 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003248 this_bio_flag,
3249 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003250 if (!ret) {
3251 nr++;
3252 *bio_flags = this_bio_flag;
3253 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003254 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003255 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003256 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003257 }
Chris Masond1310b22008-01-24 16:13:08 -05003258 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003259 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003260 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003261out:
Chris Masond1310b22008-01-24 16:13:08 -05003262 if (!nr) {
3263 if (!PageError(page))
3264 SetPageUptodate(page);
3265 unlock_page(page);
3266 }
Liu Bobaf863b2016-07-11 10:39:07 -07003267 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003268}
3269
Nikolay Borisove65ef212019-03-11 09:55:38 +02003270static inline void contiguous_readpages(struct extent_io_tree *tree,
Miao Xie99740902013-07-25 19:22:36 +08003271 struct page *pages[], int nr_pages,
3272 u64 start, u64 end,
Miao Xie125bac012013-07-25 19:22:37 +08003273 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003274 struct bio **bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003275 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003276 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003277{
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03003278 struct btrfs_inode *inode = BTRFS_I(pages[0]->mapping->host);
Miao Xie99740902013-07-25 19:22:36 +08003279 int index;
3280
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03003281 btrfs_lock_and_flush_ordered_range(tree, inode, start, end, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003282
3283 for (index = 0; index < nr_pages; index++) {
David Sterba4ef77692017-06-23 04:09:57 +02003284 __do_readpage(tree, pages[index], btrfs_get_extent, em_cached,
Jens Axboe5e9d3982018-08-17 15:45:39 -07003285 bio, 0, bio_flags, REQ_RAHEAD, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003286 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003287 }
3288}
3289
Miao Xie99740902013-07-25 19:22:36 +08003290static int __extent_read_full_page(struct extent_io_tree *tree,
3291 struct page *page,
3292 get_extent_t *get_extent,
3293 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003294 unsigned long *bio_flags,
3295 unsigned int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003296{
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03003297 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
Miao Xie99740902013-07-25 19:22:36 +08003298 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003299 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003300 int ret;
3301
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03003302 btrfs_lock_and_flush_ordered_range(tree, inode, start, end, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003303
Miao Xie125bac012013-07-25 19:22:37 +08003304 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003305 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003306 return ret;
3307}
3308
Chris Masond1310b22008-01-24 16:13:08 -05003309int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003310 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003311{
3312 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003313 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003314 int ret;
3315
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003316 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003317 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003318 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003319 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003320 return ret;
3321}
Chris Masond1310b22008-01-24 16:13:08 -05003322
David Sterba3d4b9492017-02-10 19:33:41 +01003323static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003324 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003325{
3326 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003327}
3328
Chris Masond1310b22008-01-24 16:13:08 -05003329/*
Chris Mason40f76582014-05-21 13:35:51 -07003330 * helper for __extent_writepage, doing all of the delayed allocation setup.
3331 *
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003332 * This returns 1 if btrfs_run_delalloc_range function did all the work required
Chris Mason40f76582014-05-21 13:35:51 -07003333 * to write the page (copy into inline extent). In this case the IO has
3334 * been started and the page is already unlocked.
3335 *
3336 * This returns 0 if all went well (page still locked)
3337 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003338 */
Chris Mason40f76582014-05-21 13:35:51 -07003339static noinline_for_stack int writepage_delalloc(struct inode *inode,
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003340 struct page *page, struct writeback_control *wbc,
3341 u64 delalloc_start, unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003342{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003343 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Lu Fengqi3522e902018-11-29 11:33:38 +08003344 bool found;
Chris Mason40f76582014-05-21 13:35:51 -07003345 u64 delalloc_to_write = 0;
3346 u64 delalloc_end = 0;
3347 int ret;
3348 int page_started = 0;
3349
Chris Mason40f76582014-05-21 13:35:51 -07003350
3351 while (delalloc_end < page_end) {
Goldwyn Rodrigues99780592019-06-21 10:02:54 -05003352 found = find_lock_delalloc_range(inode, page,
Chris Mason40f76582014-05-21 13:35:51 -07003353 &delalloc_start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03003354 &delalloc_end);
Lu Fengqi3522e902018-11-29 11:33:38 +08003355 if (!found) {
Chris Mason40f76582014-05-21 13:35:51 -07003356 delalloc_start = delalloc_end + 1;
3357 continue;
3358 }
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003359 ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
3360 delalloc_end, &page_started, nr_written, wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003361 if (ret) {
3362 SetPageError(page);
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003363 /*
3364 * btrfs_run_delalloc_range should return < 0 for error
3365 * but just in case, we use > 0 here meaning the IO is
3366 * started, so we don't want to return > 0 unless
3367 * things are going well.
Chris Mason40f76582014-05-21 13:35:51 -07003368 */
3369 ret = ret < 0 ? ret : -EIO;
3370 goto done;
3371 }
3372 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003373 * delalloc_end is already one less than the total length, so
3374 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003375 */
3376 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003377 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003378 delalloc_start = delalloc_end + 1;
3379 }
3380 if (wbc->nr_to_write < delalloc_to_write) {
3381 int thresh = 8192;
3382
3383 if (delalloc_to_write < thresh * 2)
3384 thresh = delalloc_to_write;
3385 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3386 thresh);
3387 }
3388
3389 /* did the fill delalloc function already unlock and start
3390 * the IO?
3391 */
3392 if (page_started) {
3393 /*
3394 * we've unlocked the page, so we can't update
3395 * the mapping's writeback index, just update
3396 * nr_to_write.
3397 */
3398 wbc->nr_to_write -= *nr_written;
3399 return 1;
3400 }
3401
3402 ret = 0;
3403
3404done:
3405 return ret;
3406}
3407
3408/*
3409 * helper for __extent_writepage. This calls the writepage start hooks,
3410 * and does the loop to map the page into extents and bios.
3411 *
3412 * We return 1 if the IO is started and the page is unlocked,
3413 * 0 if all went well (page still locked)
3414 * < 0 if there were errors (page still locked)
3415 */
3416static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3417 struct page *page,
3418 struct writeback_control *wbc,
3419 struct extent_page_data *epd,
3420 loff_t i_size,
3421 unsigned long nr_written,
David Sterbaf1c77c52017-06-06 19:03:49 +02003422 unsigned int write_flags, int *nr_ret)
Chris Mason40f76582014-05-21 13:35:51 -07003423{
Chris Masond1310b22008-01-24 16:13:08 -05003424 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003425 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003426 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003427 u64 end;
3428 u64 cur = start;
3429 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003430 u64 block_start;
3431 u64 iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003432 struct extent_map *em;
3433 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003434 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003435 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003436 int ret = 0;
3437 int nr = 0;
3438 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003439
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003440 ret = btrfs_writepage_cow_fixup(page, start, page_end);
3441 if (ret) {
3442 /* Fixup worker will requeue */
3443 if (ret == -EBUSY)
3444 wbc->pages_skipped++;
3445 else
3446 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003447
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003448 update_nr_written(wbc, nr_written);
3449 unlock_page(page);
3450 return 1;
Chris Mason247e7432008-07-17 12:53:51 -04003451 }
3452
Chris Mason11c83492009-04-20 15:50:09 -04003453 /*
3454 * we don't want to touch the inode after unlocking the page,
3455 * so we update the mapping writeback index now
3456 */
David Sterba3d4b9492017-02-10 19:33:41 +01003457 update_nr_written(wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003458
Chris Masond1310b22008-01-24 16:13:08 -05003459 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003460 if (i_size <= start) {
Nikolay Borisovc6297322018-11-08 10:18:08 +02003461 btrfs_writepage_endio_finish_ordered(page, start, page_end, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003462 goto done;
3463 }
3464
Chris Masond1310b22008-01-24 16:13:08 -05003465 blocksize = inode->i_sb->s_blocksize;
3466
3467 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003468 u64 em_end;
David Sterba6273b7f2017-10-04 17:30:11 +02003469 u64 offset;
David Sterba58409ed2016-05-04 11:46:10 +02003470
Chris Mason40f76582014-05-21 13:35:51 -07003471 if (cur >= i_size) {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003472 btrfs_writepage_endio_finish_ordered(page, cur,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003473 page_end, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003474 break;
3475 }
David Sterba3c98c622017-06-23 04:01:08 +02003476 em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003477 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003478 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003479 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003480 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003481 break;
3482 }
3483
3484 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003485 em_end = extent_map_end(em);
3486 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003487 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003488 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003489 iosize = ALIGN(iosize, blocksize);
David Sterba6273b7f2017-10-04 17:30:11 +02003490 offset = em->block_start + extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003491 bdev = em->bdev;
3492 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003493 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003494 free_extent_map(em);
3495 em = NULL;
3496
Chris Masonc8b97812008-10-29 14:49:59 -04003497 /*
3498 * compressed and inline extents are written through other
3499 * paths in the FS
3500 */
3501 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003502 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003503 /*
3504 * end_io notification does not happen here for
3505 * compressed extents
3506 */
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003507 if (!compressed)
3508 btrfs_writepage_endio_finish_ordered(page, cur,
3509 cur + iosize - 1,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003510 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003511 else if (compressed) {
3512 /* we don't want to end_page_writeback on
3513 * a compressed extent. this happens
3514 * elsewhere
3515 */
3516 nr++;
3517 }
3518
3519 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003520 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003521 continue;
3522 }
Chris Masonc8b97812008-10-29 14:49:59 -04003523
David Sterba5cdc84b2018-07-18 20:32:52 +02003524 btrfs_set_range_writeback(tree, cur, cur + iosize - 1);
David Sterba58409ed2016-05-04 11:46:10 +02003525 if (!PageWriteback(page)) {
3526 btrfs_err(BTRFS_I(inode)->root->fs_info,
3527 "page %lu not writeback, cur %llu end %llu",
3528 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003529 }
David Sterba58409ed2016-05-04 11:46:10 +02003530
David Sterba4b81ba42017-06-06 19:14:26 +02003531 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003532 page, offset, iosize, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003533 bdev, &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003534 end_bio_extent_writepage,
3535 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003536 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003537 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003538 if (PageWriteback(page))
3539 end_page_writeback(page);
3540 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003541
Chris Masond1310b22008-01-24 16:13:08 -05003542 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003543 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003544 nr++;
3545 }
3546done:
Chris Mason40f76582014-05-21 13:35:51 -07003547 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003548 return ret;
3549}
3550
3551/*
3552 * the writepage semantics are similar to regular writepage. extent
3553 * records are inserted to lock ranges in the tree, and as dirty areas
3554 * are found, they are marked writeback. Then the lock bits are removed
3555 * and the end_io handler clears the writeback ranges
Qu Wenruo30659762019-03-20 14:27:42 +08003556 *
3557 * Return 0 if everything goes well.
3558 * Return <0 for error.
Chris Mason40f76582014-05-21 13:35:51 -07003559 */
3560static int __extent_writepage(struct page *page, struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003561 struct extent_page_data *epd)
Chris Mason40f76582014-05-21 13:35:51 -07003562{
3563 struct inode *inode = page->mapping->host;
Chris Mason40f76582014-05-21 13:35:51 -07003564 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003565 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003566 int ret;
3567 int nr = 0;
3568 size_t pg_offset = 0;
3569 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003570 unsigned long end_index = i_size >> PAGE_SHIFT;
David Sterbaf1c77c52017-06-06 19:03:49 +02003571 unsigned int write_flags = 0;
Chris Mason40f76582014-05-21 13:35:51 -07003572 unsigned long nr_written = 0;
3573
Liu Boff40adf2017-08-24 18:19:48 -06003574 write_flags = wbc_to_write_flags(wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003575
3576 trace___extent_writepage(page, inode, wbc);
3577
3578 WARN_ON(!PageLocked(page));
3579
3580 ClearPageError(page);
3581
Johannes Thumshirn70730172018-12-05 15:23:03 +01003582 pg_offset = offset_in_page(i_size);
Chris Mason40f76582014-05-21 13:35:51 -07003583 if (page->index > end_index ||
3584 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003585 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003586 unlock_page(page);
3587 return 0;
3588 }
3589
3590 if (page->index == end_index) {
3591 char *userpage;
3592
3593 userpage = kmap_atomic(page);
3594 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003595 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003596 kunmap_atomic(userpage);
3597 flush_dcache_page(page);
3598 }
3599
3600 pg_offset = 0;
3601
3602 set_page_extent_mapped(page);
3603
Nikolay Borisov7789a552018-11-08 10:18:06 +02003604 if (!epd->extent_locked) {
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003605 ret = writepage_delalloc(inode, page, wbc, start, &nr_written);
Nikolay Borisov7789a552018-11-08 10:18:06 +02003606 if (ret == 1)
3607 goto done_unlocked;
3608 if (ret)
3609 goto done;
3610 }
Chris Mason40f76582014-05-21 13:35:51 -07003611
3612 ret = __extent_writepage_io(inode, page, wbc, epd,
3613 i_size, nr_written, write_flags, &nr);
3614 if (ret == 1)
3615 goto done_unlocked;
3616
3617done:
Chris Masond1310b22008-01-24 16:13:08 -05003618 if (nr == 0) {
3619 /* make sure the mapping tag for page dirty gets cleared */
3620 set_page_writeback(page);
3621 end_page_writeback(page);
3622 }
Filipe Manana61391d52014-05-09 17:17:40 +01003623 if (PageError(page)) {
3624 ret = ret < 0 ? ret : -EIO;
3625 end_extent_writepage(page, ret, start, page_end);
3626 }
Chris Masond1310b22008-01-24 16:13:08 -05003627 unlock_page(page);
Qu Wenruo30659762019-03-20 14:27:42 +08003628 ASSERT(ret <= 0);
Chris Mason40f76582014-05-21 13:35:51 -07003629 return ret;
Chris Mason771ed682008-11-06 22:02:51 -05003630
Chris Mason11c83492009-04-20 15:50:09 -04003631done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003632 return 0;
3633}
3634
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003635void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003636{
NeilBrown74316202014-07-07 15:16:04 +10003637 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3638 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003639}
3640
Filipe Manana18dfa712019-09-11 17:42:00 +01003641static void end_extent_buffer_writeback(struct extent_buffer *eb)
3642{
3643 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3644 smp_mb__after_atomic();
3645 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3646}
3647
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003648/*
3649 * Lock eb pages and flush the bio if we can't the locks
3650 *
3651 * Return 0 if nothing went wrong
3652 * Return >0 is same as 0, except bio is not submitted
3653 * Return <0 if something went wrong, no page is locked
3654 */
David Sterba9df76fb2019-03-20 11:21:41 +01003655static noinline_for_stack int lock_extent_buffer_for_io(struct extent_buffer *eb,
Chris Mason0e378df2014-05-19 20:55:27 -07003656 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003657{
David Sterba9df76fb2019-03-20 11:21:41 +01003658 struct btrfs_fs_info *fs_info = eb->fs_info;
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003659 int i, num_pages, failed_page_nr;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003660 int flush = 0;
3661 int ret = 0;
3662
3663 if (!btrfs_try_tree_write_lock(eb)) {
Qu Wenruof4340622019-03-20 14:27:41 +08003664 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003665 if (ret < 0)
3666 return ret;
3667 flush = 1;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003668 btrfs_tree_lock(eb);
3669 }
3670
3671 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3672 btrfs_tree_unlock(eb);
3673 if (!epd->sync_io)
3674 return 0;
3675 if (!flush) {
Qu Wenruof4340622019-03-20 14:27:41 +08003676 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003677 if (ret < 0)
3678 return ret;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003679 flush = 1;
3680 }
Chris Masona098d8e82012-03-21 12:09:56 -04003681 while (1) {
3682 wait_on_extent_buffer_writeback(eb);
3683 btrfs_tree_lock(eb);
3684 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3685 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003686 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003687 }
3688 }
3689
Josef Bacik51561ff2012-07-20 16:25:24 -04003690 /*
3691 * We need to do this to prevent races in people who check if the eb is
3692 * under IO since we can end up having no IO bits set for a short period
3693 * of time.
3694 */
3695 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003696 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3697 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003698 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003699 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Nikolay Borisov104b4e52017-06-20 21:01:20 +03003700 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3701 -eb->len,
3702 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003703 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003704 } else {
3705 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003706 }
3707
3708 btrfs_tree_unlock(eb);
3709
3710 if (!ret)
3711 return ret;
3712
David Sterba65ad0102018-06-29 10:56:49 +02003713 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003714 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003715 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003716
3717 if (!trylock_page(p)) {
3718 if (!flush) {
Filipe Manana18dfa712019-09-11 17:42:00 +01003719 int err;
3720
3721 err = flush_write_bio(epd);
3722 if (err < 0) {
3723 ret = err;
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003724 failed_page_nr = i;
3725 goto err_unlock;
3726 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003727 flush = 1;
3728 }
3729 lock_page(p);
3730 }
3731 }
3732
3733 return ret;
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003734err_unlock:
3735 /* Unlock already locked pages */
3736 for (i = 0; i < failed_page_nr; i++)
3737 unlock_page(eb->pages[i]);
Filipe Manana18dfa712019-09-11 17:42:00 +01003738 /*
3739 * Clear EXTENT_BUFFER_WRITEBACK and wake up anyone waiting on it.
3740 * Also set back EXTENT_BUFFER_DIRTY so future attempts to this eb can
3741 * be made and undo everything done before.
3742 */
3743 btrfs_tree_lock(eb);
3744 spin_lock(&eb->refs_lock);
3745 set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
3746 end_extent_buffer_writeback(eb);
3747 spin_unlock(&eb->refs_lock);
3748 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, eb->len,
3749 fs_info->dirty_metadata_batch);
3750 btrfs_clear_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3751 btrfs_tree_unlock(eb);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003752 return ret;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003753}
3754
Filipe Manana656f30d2014-09-26 12:25:56 +01003755static void set_btree_ioerr(struct page *page)
3756{
3757 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Dennis Zhoueb5b64f2019-09-13 14:54:07 +01003758 struct btrfs_fs_info *fs_info;
Filipe Manana656f30d2014-09-26 12:25:56 +01003759
3760 SetPageError(page);
3761 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3762 return;
3763
3764 /*
Dennis Zhoueb5b64f2019-09-13 14:54:07 +01003765 * If we error out, we should add back the dirty_metadata_bytes
3766 * to make it consistent.
3767 */
3768 fs_info = eb->fs_info;
3769 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3770 eb->len, fs_info->dirty_metadata_batch);
3771
3772 /*
Filipe Manana656f30d2014-09-26 12:25:56 +01003773 * If writeback for a btree extent that doesn't belong to a log tree
3774 * failed, increment the counter transaction->eb_write_errors.
3775 * We do this because while the transaction is running and before it's
3776 * committing (when we call filemap_fdata[write|wait]_range against
3777 * the btree inode), we might have
3778 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3779 * returns an error or an error happens during writeback, when we're
3780 * committing the transaction we wouldn't know about it, since the pages
3781 * can be no longer dirty nor marked anymore for writeback (if a
3782 * subsequent modification to the extent buffer didn't happen before the
3783 * transaction commit), which makes filemap_fdata[write|wait]_range not
3784 * able to find the pages tagged with SetPageError at transaction
3785 * commit time. So if this happens we must abort the transaction,
3786 * otherwise we commit a super block with btree roots that point to
3787 * btree nodes/leafs whose content on disk is invalid - either garbage
3788 * or the content of some node/leaf from a past generation that got
3789 * cowed or deleted and is no longer valid.
3790 *
3791 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3792 * not be enough - we need to distinguish between log tree extents vs
3793 * non-log tree extents, and the next filemap_fdatawait_range() call
3794 * will catch and clear such errors in the mapping - and that call might
3795 * be from a log sync and not from a transaction commit. Also, checking
3796 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3797 * not done and would not be reliable - the eb might have been released
3798 * from memory and reading it back again means that flag would not be
3799 * set (since it's a runtime flag, not persisted on disk).
3800 *
3801 * Using the flags below in the btree inode also makes us achieve the
3802 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3803 * writeback for all dirty pages and before filemap_fdatawait_range()
3804 * is called, the writeback for all dirty pages had already finished
3805 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3806 * filemap_fdatawait_range() would return success, as it could not know
3807 * that writeback errors happened (the pages were no longer tagged for
3808 * writeback).
3809 */
3810 switch (eb->log_index) {
3811 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003812 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003813 break;
3814 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003815 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003816 break;
3817 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003818 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003819 break;
3820 default:
3821 BUG(); /* unexpected, logic error */
3822 }
3823}
3824
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003825static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003826{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003827 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003828 struct extent_buffer *eb;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02003829 int done;
Ming Lei6dc4f102019-02-15 19:13:19 +08003830 struct bvec_iter_all iter_all;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003831
David Sterbac09abff2017-07-13 18:10:07 +02003832 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02003833 bio_for_each_segment_all(bvec, bio, iter_all) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003834 struct page *page = bvec->bv_page;
3835
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003836 eb = (struct extent_buffer *)page->private;
3837 BUG_ON(!eb);
3838 done = atomic_dec_and_test(&eb->io_pages);
3839
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003840 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003841 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003842 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003843 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003844 }
3845
3846 end_page_writeback(page);
3847
3848 if (!done)
3849 continue;
3850
3851 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003852 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003853
3854 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003855}
3856
Chris Mason0e378df2014-05-19 20:55:27 -07003857static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003858 struct writeback_control *wbc,
3859 struct extent_page_data *epd)
3860{
David Sterba0ab02062019-03-20 11:27:57 +01003861 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003862 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003863 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003864 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003865 u32 nritems;
David Sterbacc5e31a2018-03-01 18:20:27 +01003866 int i, num_pages;
Liu Bo851cd172016-09-23 13:44:44 -07003867 unsigned long start, end;
Liu Boff40adf2017-08-24 18:19:48 -06003868 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003869 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003870
Filipe Manana656f30d2014-09-26 12:25:56 +01003871 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02003872 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003873 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003874
Liu Bo851cd172016-09-23 13:44:44 -07003875 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3876 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003877 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003878 end = btrfs_node_key_ptr_offset(nritems);
3879
David Sterbab159fa22016-11-08 18:09:03 +01003880 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003881 } else {
3882 /*
3883 * leaf:
3884 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3885 */
3886 start = btrfs_item_nr_offset(nritems);
David Sterba8f881e82019-03-20 11:33:10 +01003887 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(eb);
David Sterbab159fa22016-11-08 18:09:03 +01003888 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003889 }
3890
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003891 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003892 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003893
3894 clear_page_dirty_for_io(p);
3895 set_page_writeback(p);
David Sterba4b81ba42017-06-06 19:14:26 +02003896 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003897 p, offset, PAGE_SIZE, 0, bdev,
David Sterbac2df8bb2017-02-10 19:29:38 +01003898 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003899 end_bio_extent_buffer_writepage,
Liu Bo18fdc672017-09-13 12:18:22 -06003900 0, 0, 0, false);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003901 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003902 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003903 if (PageWriteback(p))
3904 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003905 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3906 end_extent_buffer_writeback(eb);
3907 ret = -EIO;
3908 break;
3909 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003910 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003911 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003912 unlock_page(p);
3913 }
3914
3915 if (unlikely(ret)) {
3916 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003917 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003918 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003919 unlock_page(p);
3920 }
3921 }
3922
3923 return ret;
3924}
3925
3926int btree_write_cache_pages(struct address_space *mapping,
3927 struct writeback_control *wbc)
3928{
3929 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003930 struct extent_buffer *eb, *prev_eb = NULL;
3931 struct extent_page_data epd = {
3932 .bio = NULL,
3933 .tree = tree,
3934 .extent_locked = 0,
3935 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3936 };
3937 int ret = 0;
3938 int done = 0;
3939 int nr_to_write_done = 0;
3940 struct pagevec pvec;
3941 int nr_pages;
3942 pgoff_t index;
3943 pgoff_t end; /* Inclusive */
3944 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003945 xa_mark_t tag;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003946
Mel Gorman86679822017-11-15 17:37:52 -08003947 pagevec_init(&pvec);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003948 if (wbc->range_cyclic) {
3949 index = mapping->writeback_index; /* Start from prev offset */
3950 end = -1;
3951 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003952 index = wbc->range_start >> PAGE_SHIFT;
3953 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003954 scanned = 1;
3955 }
3956 if (wbc->sync_mode == WB_SYNC_ALL)
3957 tag = PAGECACHE_TAG_TOWRITE;
3958 else
3959 tag = PAGECACHE_TAG_DIRTY;
3960retry:
3961 if (wbc->sync_mode == WB_SYNC_ALL)
3962 tag_pages_for_writeback(mapping, index, end);
3963 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara4006f432017-11-15 17:34:37 -08003964 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08003965 tag))) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003966 unsigned i;
3967
3968 scanned = 1;
3969 for (i = 0; i < nr_pages; i++) {
3970 struct page *page = pvec.pages[i];
3971
3972 if (!PagePrivate(page))
3973 continue;
3974
Josef Bacikb5bae262012-09-14 13:43:01 -04003975 spin_lock(&mapping->private_lock);
3976 if (!PagePrivate(page)) {
3977 spin_unlock(&mapping->private_lock);
3978 continue;
3979 }
3980
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003981 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003982
3983 /*
3984 * Shouldn't happen and normally this would be a BUG_ON
3985 * but no sense in crashing the users box for something
3986 * we can survive anyway.
3987 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303988 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003989 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003990 continue;
3991 }
3992
Josef Bacikb5bae262012-09-14 13:43:01 -04003993 if (eb == prev_eb) {
3994 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003995 continue;
3996 }
3997
Josef Bacikb5bae262012-09-14 13:43:01 -04003998 ret = atomic_inc_not_zero(&eb->refs);
3999 spin_unlock(&mapping->private_lock);
4000 if (!ret)
4001 continue;
4002
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004003 prev_eb = eb;
David Sterba9df76fb2019-03-20 11:21:41 +01004004 ret = lock_extent_buffer_for_io(eb, &epd);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004005 if (!ret) {
4006 free_extent_buffer(eb);
4007 continue;
Filipe Manana0607eb1d2019-09-11 17:42:28 +01004008 } else if (ret < 0) {
4009 done = 1;
4010 free_extent_buffer(eb);
4011 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004012 }
4013
David Sterba0ab02062019-03-20 11:27:57 +01004014 ret = write_one_eb(eb, wbc, &epd);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004015 if (ret) {
4016 done = 1;
4017 free_extent_buffer(eb);
4018 break;
4019 }
4020 free_extent_buffer(eb);
4021
4022 /*
4023 * the filesystem may choose to bump up nr_to_write.
4024 * We have to make sure to honor the new nr_to_write
4025 * at any time
4026 */
4027 nr_to_write_done = wbc->nr_to_write <= 0;
4028 }
4029 pagevec_release(&pvec);
4030 cond_resched();
4031 }
4032 if (!scanned && !done) {
4033 /*
4034 * We hit the last page and there is more work to be done: wrap
4035 * back to the start of the file
4036 */
4037 scanned = 1;
4038 index = 0;
4039 goto retry;
4040 }
Qu Wenruo2b952ee2019-03-20 14:27:43 +08004041 ASSERT(ret <= 0);
4042 if (ret < 0) {
4043 end_write_bio(&epd, ret);
4044 return ret;
4045 }
4046 ret = flush_write_bio(&epd);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004047 return ret;
4048}
4049
Chris Masond1310b22008-01-24 16:13:08 -05004050/**
Chris Mason4bef0842008-09-08 11:18:08 -04004051 * 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 -05004052 * @mapping: address space structure to write
4053 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
David Sterba935db852017-06-23 04:30:28 +02004054 * @data: data passed to __extent_writepage function
Chris Masond1310b22008-01-24 16:13:08 -05004055 *
4056 * If a page is already under I/O, write_cache_pages() skips it, even
4057 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
4058 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
4059 * and msync() need to guarantee that all the data which was dirty at the time
4060 * the call was made get new I/O started against them. If wbc->sync_mode is
4061 * WB_SYNC_ALL then we were called for data integrity and we must wait for
4062 * existing IO to complete.
4063 */
David Sterba4242b642017-02-10 19:38:24 +01004064static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04004065 struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01004066 struct extent_page_data *epd)
Chris Masond1310b22008-01-24 16:13:08 -05004067{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004068 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05004069 int ret = 0;
4070 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004071 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004072 struct pagevec pvec;
4073 int nr_pages;
4074 pgoff_t index;
4075 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08004076 pgoff_t done_index;
4077 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004078 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05004079 xa_mark_t tag;
Chris Masond1310b22008-01-24 16:13:08 -05004080
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004081 /*
4082 * We have to hold onto the inode so that ordered extents can do their
4083 * work when the IO finishes. The alternative to this is failing to add
4084 * an ordered extent if the igrab() fails there and that is a huge pain
4085 * to deal with, so instead just hold onto the inode throughout the
4086 * writepages operation. If it fails here we are freeing up the inode
4087 * anyway and we'd rather not waste our time writing out stuff that is
4088 * going to be truncated anyway.
4089 */
4090 if (!igrab(inode))
4091 return 0;
4092
Mel Gorman86679822017-11-15 17:37:52 -08004093 pagevec_init(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05004094 if (wbc->range_cyclic) {
4095 index = mapping->writeback_index; /* Start from prev offset */
4096 end = -1;
4097 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004098 index = wbc->range_start >> PAGE_SHIFT;
4099 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08004100 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
4101 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004102 scanned = 1;
4103 }
Ethan Lien3cd24c62018-11-01 14:49:03 +08004104
4105 /*
4106 * We do the tagged writepage as long as the snapshot flush bit is set
4107 * and we are the first one who do the filemap_flush() on this inode.
4108 *
4109 * The nr_to_write == LONG_MAX is needed to make sure other flushers do
4110 * not race in and drop the bit.
4111 */
4112 if (range_whole && wbc->nr_to_write == LONG_MAX &&
4113 test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
4114 &BTRFS_I(inode)->runtime_flags))
4115 wbc->tagged_writepages = 1;
4116
4117 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00004118 tag = PAGECACHE_TAG_TOWRITE;
4119 else
4120 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05004121retry:
Ethan Lien3cd24c62018-11-01 14:49:03 +08004122 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00004123 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08004124 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004125 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara67fd7072017-11-15 17:35:19 -08004126 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
4127 &index, end, tag))) {
Chris Masond1310b22008-01-24 16:13:08 -05004128 unsigned i;
4129
4130 scanned = 1;
4131 for (i = 0; i < nr_pages; i++) {
4132 struct page *page = pvec.pages[i];
4133
Liu Boa91326672016-03-07 16:56:21 -08004134 done_index = page->index;
Chris Masond1310b22008-01-24 16:13:08 -05004135 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07004136 * At this point we hold neither the i_pages lock nor
4137 * the page lock: the page may be truncated or
4138 * invalidated (changing page->mapping to NULL),
4139 * or even swizzled back from swapper_space to
4140 * tmpfs file mapping
Chris Masond1310b22008-01-24 16:13:08 -05004141 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05004142 if (!trylock_page(page)) {
Qu Wenruof4340622019-03-20 14:27:41 +08004143 ret = flush_write_bio(epd);
4144 BUG_ON(ret < 0);
Josef Bacikc8f2f242013-02-11 11:33:00 -05004145 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04004146 }
Chris Masond1310b22008-01-24 16:13:08 -05004147
4148 if (unlikely(page->mapping != mapping)) {
4149 unlock_page(page);
4150 continue;
4151 }
4152
Chris Masond2c3f4f2008-11-19 12:44:22 -05004153 if (wbc->sync_mode != WB_SYNC_NONE) {
Qu Wenruof4340622019-03-20 14:27:41 +08004154 if (PageWriteback(page)) {
4155 ret = flush_write_bio(epd);
4156 BUG_ON(ret < 0);
4157 }
Chris Masond1310b22008-01-24 16:13:08 -05004158 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004159 }
Chris Masond1310b22008-01-24 16:13:08 -05004160
4161 if (PageWriteback(page) ||
4162 !clear_page_dirty_for_io(page)) {
4163 unlock_page(page);
4164 continue;
4165 }
4166
David Sterbaaab6e9e2017-11-30 18:00:02 +01004167 ret = __extent_writepage(page, wbc, epd);
Liu Boa91326672016-03-07 16:56:21 -08004168 if (ret < 0) {
4169 /*
4170 * done_index is set past this page,
4171 * so media errors will not choke
4172 * background writeout for the entire
4173 * file. This has consequences for
4174 * range_cyclic semantics (ie. it may
4175 * not be suitable for data integrity
4176 * writeout).
4177 */
4178 done_index = page->index + 1;
4179 done = 1;
4180 break;
4181 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004182
4183 /*
4184 * the filesystem may choose to bump up nr_to_write.
4185 * We have to make sure to honor the new nr_to_write
4186 * at any time
4187 */
4188 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004189 }
4190 pagevec_release(&pvec);
4191 cond_resched();
4192 }
Liu Bo894b36e2016-03-07 16:56:22 -08004193 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004194 /*
4195 * We hit the last page and there is more work to be done: wrap
4196 * back to the start of the file
4197 */
4198 scanned = 1;
4199 index = 0;
4200 goto retry;
4201 }
Liu Boa91326672016-03-07 16:56:21 -08004202
4203 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4204 mapping->writeback_index = done_index;
4205
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004206 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004207 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004208}
Chris Masond1310b22008-01-24 16:13:08 -05004209
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004210int extent_write_full_page(struct page *page, struct writeback_control *wbc)
Chris Masond1310b22008-01-24 16:13:08 -05004211{
4212 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004213 struct extent_page_data epd = {
4214 .bio = NULL,
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004215 .tree = &BTRFS_I(page->mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004216 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004217 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004218 };
Chris Masond1310b22008-01-24 16:13:08 -05004219
Chris Masond1310b22008-01-24 16:13:08 -05004220 ret = __extent_writepage(page, wbc, &epd);
Qu Wenruo30659762019-03-20 14:27:42 +08004221 ASSERT(ret <= 0);
4222 if (ret < 0) {
4223 end_write_bio(&epd, ret);
4224 return ret;
4225 }
Chris Masond1310b22008-01-24 16:13:08 -05004226
Qu Wenruo30659762019-03-20 14:27:42 +08004227 ret = flush_write_bio(&epd);
4228 ASSERT(ret <= 0);
Chris Masond1310b22008-01-24 16:13:08 -05004229 return ret;
4230}
Chris Masond1310b22008-01-24 16:13:08 -05004231
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004232int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
Chris Mason771ed682008-11-06 22:02:51 -05004233 int mode)
4234{
4235 int ret = 0;
4236 struct address_space *mapping = inode->i_mapping;
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004237 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Mason771ed682008-11-06 22:02:51 -05004238 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004239 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4240 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004241
4242 struct extent_page_data epd = {
4243 .bio = NULL,
4244 .tree = tree,
Chris Mason771ed682008-11-06 22:02:51 -05004245 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004246 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05004247 };
4248 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004249 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004250 .nr_to_write = nr_pages * 2,
4251 .range_start = start,
4252 .range_end = end + 1,
4253 };
4254
Chris Masond3977122009-01-05 21:25:51 -05004255 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004256 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004257 if (clear_page_dirty_for_io(page))
4258 ret = __extent_writepage(page, &wbc_writepages, &epd);
4259 else {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02004260 btrfs_writepage_endio_finish_ordered(page, start,
Nikolay Borisovc6297322018-11-08 10:18:08 +02004261 start + PAGE_SIZE - 1, 1);
Chris Mason771ed682008-11-06 22:02:51 -05004262 unlock_page(page);
4263 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004264 put_page(page);
4265 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004266 }
4267
Qu Wenruo02c6db42019-03-20 14:27:45 +08004268 ASSERT(ret <= 0);
4269 if (ret < 0) {
4270 end_write_bio(&epd, ret);
4271 return ret;
4272 }
4273 ret = flush_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004274 return ret;
4275}
Chris Masond1310b22008-01-24 16:13:08 -05004276
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004277int extent_writepages(struct address_space *mapping,
Chris Masond1310b22008-01-24 16:13:08 -05004278 struct writeback_control *wbc)
4279{
4280 int ret = 0;
4281 struct extent_page_data epd = {
4282 .bio = NULL,
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004283 .tree = &BTRFS_I(mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004284 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004285 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004286 };
4287
David Sterba935db852017-06-23 04:30:28 +02004288 ret = extent_write_cache_pages(mapping, wbc, &epd);
Qu Wenruoa2a72fb2019-03-20 14:27:48 +08004289 ASSERT(ret <= 0);
4290 if (ret < 0) {
4291 end_write_bio(&epd, ret);
4292 return ret;
4293 }
4294 ret = flush_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004295 return ret;
4296}
Chris Masond1310b22008-01-24 16:13:08 -05004297
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004298int extent_readpages(struct address_space *mapping, struct list_head *pages,
4299 unsigned nr_pages)
Chris Masond1310b22008-01-24 16:13:08 -05004300{
4301 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004302 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004303 struct page *pagepool[16];
Miao Xie125bac012013-07-25 19:22:37 +08004304 struct extent_map *em_cached = NULL;
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004305 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Liu Bo67c96842012-07-20 21:43:09 -06004306 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004307 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004308
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004309 while (!list_empty(pages)) {
Nikolay Borisove65ef212019-03-11 09:55:38 +02004310 u64 contig_end = 0;
4311
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004312 for (nr = 0; nr < ARRAY_SIZE(pagepool) && !list_empty(pages);) {
Nikolay Borisovf86196e2019-01-03 15:29:02 -08004313 struct page *page = lru_to_page(pages);
Chris Masond1310b22008-01-24 16:13:08 -05004314
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004315 prefetchw(&page->flags);
4316 list_del(&page->lru);
4317 if (add_to_page_cache_lru(page, mapping, page->index,
4318 readahead_gfp_mask(mapping))) {
4319 put_page(page);
Nikolay Borisove65ef212019-03-11 09:55:38 +02004320 break;
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004321 }
4322
4323 pagepool[nr++] = page;
Nikolay Borisove65ef212019-03-11 09:55:38 +02004324 contig_end = page_offset(page) + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004325 }
Liu Bo67c96842012-07-20 21:43:09 -06004326
Nikolay Borisove65ef212019-03-11 09:55:38 +02004327 if (nr) {
4328 u64 contig_start = page_offset(pagepool[0]);
4329
4330 ASSERT(contig_start + nr * PAGE_SIZE - 1 == contig_end);
4331
4332 contiguous_readpages(tree, pagepool, nr, contig_start,
4333 contig_end, &em_cached, &bio, &bio_flags,
4334 &prev_em_start);
4335 }
Chris Masond1310b22008-01-24 16:13:08 -05004336 }
Liu Bo67c96842012-07-20 21:43:09 -06004337
Miao Xie125bac012013-07-25 19:22:37 +08004338 if (em_cached)
4339 free_extent_map(em_cached);
4340
Chris Masond1310b22008-01-24 16:13:08 -05004341 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004342 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004343 return 0;
4344}
Chris Masond1310b22008-01-24 16:13:08 -05004345
4346/*
4347 * basic invalidatepage code, this waits on any locked or writeback
4348 * ranges corresponding to the page, and then deletes any extent state
4349 * records from the tree
4350 */
4351int extent_invalidatepage(struct extent_io_tree *tree,
4352 struct page *page, unsigned long offset)
4353{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004354 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004355 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004356 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004357 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4358
Qu Wenruofda28322013-02-26 08:10:22 +00004359 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004360 if (start > end)
4361 return 0;
4362
David Sterbaff13db42015-12-03 14:30:40 +01004363 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004364 wait_on_page_writeback(page);
Omar Sandovale1821632019-08-15 14:04:04 -07004365 clear_extent_bit(tree, start, end, EXTENT_LOCKED | EXTENT_DELALLOC |
4366 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05004367 return 0;
4368}
Chris Masond1310b22008-01-24 16:13:08 -05004369
4370/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004371 * a helper for releasepage, this tests for areas of the page that
4372 * are locked or under IO and drops the related state bits if it is safe
4373 * to drop the page.
4374 */
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004375static int try_release_extent_state(struct extent_io_tree *tree,
Eric Sandeen48a3b632013-04-25 20:41:01 +00004376 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004377{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004378 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004379 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004380 int ret = 1;
4381
Nikolay Borisov88826792019-03-14 15:28:31 +02004382 if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) {
Chris Mason7b13b7b2008-04-18 10:29:50 -04004383 ret = 0;
Nikolay Borisov88826792019-03-14 15:28:31 +02004384 } else {
Chris Mason11ef1602009-09-23 20:28:46 -04004385 /*
4386 * at this point we can safely clear everything except the
4387 * locked bit and the nodatasum bit
4388 */
David Sterba66b0c882017-10-31 16:30:47 +01004389 ret = __clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004390 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
David Sterba66b0c882017-10-31 16:30:47 +01004391 0, 0, NULL, mask, NULL);
Chris Masone3f24cc2011-02-14 12:52:08 -05004392
4393 /* if clear_extent_bit failed for enomem reasons,
4394 * we can't allow the release to continue.
4395 */
4396 if (ret < 0)
4397 ret = 0;
4398 else
4399 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004400 }
4401 return ret;
4402}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004403
4404/*
Chris Masond1310b22008-01-24 16:13:08 -05004405 * a helper for releasepage. As long as there are no locked extents
4406 * in the range corresponding to the page, both state records and extent
4407 * map records are removed
4408 */
Nikolay Borisov477a30b2018-04-19 10:46:34 +03004409int try_release_extent_mapping(struct page *page, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004410{
4411 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004412 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004413 u64 end = start + PAGE_SIZE - 1;
Filipe Mananabd3599a2018-07-12 01:36:43 +01004414 struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
4415 struct extent_io_tree *tree = &btrfs_inode->io_tree;
4416 struct extent_map_tree *map = &btrfs_inode->extent_tree;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004417
Mel Gormand0164ad2015-11-06 16:28:21 -08004418 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004419 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004420 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004421 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004422 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004423 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004424 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004425 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004426 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004427 break;
4428 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004429 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4430 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004431 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004432 free_extent_map(em);
4433 break;
4434 }
4435 if (!test_range_bit(tree, em->start,
4436 extent_map_end(em) - 1,
Nikolay Borisov4e586ca2019-03-14 15:28:30 +02004437 EXTENT_LOCKED, 0, NULL)) {
Filipe Mananabd3599a2018-07-12 01:36:43 +01004438 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4439 &btrfs_inode->runtime_flags);
Chris Mason70dec802008-01-29 09:59:12 -05004440 remove_extent_mapping(map, em);
4441 /* once for the rb tree */
4442 free_extent_map(em);
4443 }
4444 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004445 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004446
4447 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004448 free_extent_map(em);
4449 }
Chris Masond1310b22008-01-24 16:13:08 -05004450 }
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004451 return try_release_extent_state(tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004452}
Chris Masond1310b22008-01-24 16:13:08 -05004453
Chris Masonec29ed52011-02-23 16:23:20 -05004454/*
4455 * helper function for fiemap, which doesn't want to see any holes.
4456 * This maps until we find something past 'last'
4457 */
4458static struct extent_map *get_extent_skip_holes(struct inode *inode,
David Sterbae3350e12017-06-23 04:09:57 +02004459 u64 offset, u64 last)
Chris Masonec29ed52011-02-23 16:23:20 -05004460{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004461 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004462 struct extent_map *em;
4463 u64 len;
4464
4465 if (offset >= last)
4466 return NULL;
4467
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304468 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004469 len = last - offset;
4470 if (len == 0)
4471 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004472 len = ALIGN(len, sectorsize);
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02004473 em = btrfs_get_extent_fiemap(BTRFS_I(inode), offset, len);
David Sterbac7040052011-04-19 18:00:01 +02004474 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004475 return em;
4476
4477 /* if this isn't a hole return it */
Nikolay Borisov4a2d25c2017-11-23 10:51:43 +02004478 if (em->block_start != EXTENT_MAP_HOLE)
Chris Masonec29ed52011-02-23 16:23:20 -05004479 return em;
Chris Masonec29ed52011-02-23 16:23:20 -05004480
4481 /* this is a hole, advance to the next extent */
4482 offset = extent_map_end(em);
4483 free_extent_map(em);
4484 if (offset >= last)
4485 break;
4486 }
4487 return NULL;
4488}
4489
Qu Wenruo47518322017-04-07 10:43:15 +08004490/*
4491 * To cache previous fiemap extent
4492 *
4493 * Will be used for merging fiemap extent
4494 */
4495struct fiemap_cache {
4496 u64 offset;
4497 u64 phys;
4498 u64 len;
4499 u32 flags;
4500 bool cached;
4501};
4502
4503/*
4504 * Helper to submit fiemap extent.
4505 *
4506 * Will try to merge current fiemap extent specified by @offset, @phys,
4507 * @len and @flags with cached one.
4508 * And only when we fails to merge, cached one will be submitted as
4509 * fiemap extent.
4510 *
4511 * Return value is the same as fiemap_fill_next_extent().
4512 */
4513static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4514 struct fiemap_cache *cache,
4515 u64 offset, u64 phys, u64 len, u32 flags)
4516{
4517 int ret = 0;
4518
4519 if (!cache->cached)
4520 goto assign;
4521
4522 /*
4523 * Sanity check, extent_fiemap() should have ensured that new
Andrea Gelmini52042d82018-11-28 12:05:13 +01004524 * fiemap extent won't overlap with cached one.
Qu Wenruo47518322017-04-07 10:43:15 +08004525 * Not recoverable.
4526 *
4527 * NOTE: Physical address can overlap, due to compression
4528 */
4529 if (cache->offset + cache->len > offset) {
4530 WARN_ON(1);
4531 return -EINVAL;
4532 }
4533
4534 /*
4535 * Only merges fiemap extents if
4536 * 1) Their logical addresses are continuous
4537 *
4538 * 2) Their physical addresses are continuous
4539 * So truly compressed (physical size smaller than logical size)
4540 * extents won't get merged with each other
4541 *
4542 * 3) Share same flags except FIEMAP_EXTENT_LAST
4543 * So regular extent won't get merged with prealloc extent
4544 */
4545 if (cache->offset + cache->len == offset &&
4546 cache->phys + cache->len == phys &&
4547 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4548 (flags & ~FIEMAP_EXTENT_LAST)) {
4549 cache->len += len;
4550 cache->flags |= flags;
4551 goto try_submit_last;
4552 }
4553
4554 /* Not mergeable, need to submit cached one */
4555 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4556 cache->len, cache->flags);
4557 cache->cached = false;
4558 if (ret)
4559 return ret;
4560assign:
4561 cache->cached = true;
4562 cache->offset = offset;
4563 cache->phys = phys;
4564 cache->len = len;
4565 cache->flags = flags;
4566try_submit_last:
4567 if (cache->flags & FIEMAP_EXTENT_LAST) {
4568 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4569 cache->phys, cache->len, cache->flags);
4570 cache->cached = false;
4571 }
4572 return ret;
4573}
4574
4575/*
Qu Wenruo848c23b2017-06-22 10:01:21 +08004576 * Emit last fiemap cache
Qu Wenruo47518322017-04-07 10:43:15 +08004577 *
Qu Wenruo848c23b2017-06-22 10:01:21 +08004578 * The last fiemap cache may still be cached in the following case:
4579 * 0 4k 8k
4580 * |<- Fiemap range ->|
4581 * |<------------ First extent ----------->|
4582 *
4583 * In this case, the first extent range will be cached but not emitted.
4584 * So we must emit it before ending extent_fiemap().
Qu Wenruo47518322017-04-07 10:43:15 +08004585 */
David Sterba5c5aff92019-03-20 11:29:46 +01004586static int emit_last_fiemap_cache(struct fiemap_extent_info *fieinfo,
Qu Wenruo848c23b2017-06-22 10:01:21 +08004587 struct fiemap_cache *cache)
Qu Wenruo47518322017-04-07 10:43:15 +08004588{
4589 int ret;
4590
4591 if (!cache->cached)
4592 return 0;
4593
Qu Wenruo47518322017-04-07 10:43:15 +08004594 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4595 cache->len, cache->flags);
4596 cache->cached = false;
4597 if (ret > 0)
4598 ret = 0;
4599 return ret;
4600}
4601
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004602int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterba2135fb92017-06-23 04:09:57 +02004603 __u64 start, __u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004604{
Josef Bacik975f84f2010-11-23 19:36:57 +00004605 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004606 u64 off = start;
4607 u64 max = start + len;
4608 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004609 u32 found_type;
4610 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004611 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004612 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004613 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004614 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004615 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004616 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004617 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004618 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004619 struct fiemap_cache cache = { 0 };
David Sterba5911c8f2019-05-15 15:31:04 +02004620 struct ulist *roots;
4621 struct ulist *tmp_ulist;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004622 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004623 u64 em_start = 0;
4624 u64 em_len = 0;
4625 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004626
4627 if (len == 0)
4628 return -EINVAL;
4629
Josef Bacik975f84f2010-11-23 19:36:57 +00004630 path = btrfs_alloc_path();
4631 if (!path)
4632 return -ENOMEM;
4633 path->leave_spinning = 1;
4634
David Sterba5911c8f2019-05-15 15:31:04 +02004635 roots = ulist_alloc(GFP_KERNEL);
4636 tmp_ulist = ulist_alloc(GFP_KERNEL);
4637 if (!roots || !tmp_ulist) {
4638 ret = -ENOMEM;
4639 goto out_free_ulist;
4640 }
4641
Jeff Mahoneyda170662016-06-15 09:22:56 -04004642 start = round_down(start, btrfs_inode_sectorsize(inode));
4643 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004644
Chris Masonec29ed52011-02-23 16:23:20 -05004645 /*
4646 * lookup the last file extent. We're not using i_size here
4647 * because there might be preallocation past i_size
4648 */
David Sterbaf85b7372017-01-20 14:54:07 +01004649 ret = btrfs_lookup_file_extent(NULL, root, path,
4650 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004651 if (ret < 0) {
David Sterba5911c8f2019-05-15 15:31:04 +02004652 goto out_free_ulist;
Liu Bo2d324f52016-05-17 17:21:48 -07004653 } else {
4654 WARN_ON(!ret);
4655 if (ret == 1)
4656 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004657 }
Liu Bo2d324f52016-05-17 17:21:48 -07004658
Josef Bacik975f84f2010-11-23 19:36:57 +00004659 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004660 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004661 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004662
Chris Masonec29ed52011-02-23 16:23:20 -05004663 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004664 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004665 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004666 /* have to trust i_size as the end */
4667 last = (u64)-1;
4668 last_for_get_extent = isize;
4669 } else {
4670 /*
4671 * remember the start of the last extent. There are a
4672 * bunch of different factors that go into the length of the
4673 * extent, so its much less complex to remember where it started
4674 */
4675 last = found_key.offset;
4676 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004677 }
Liu Bofe09e162013-09-22 12:54:23 +08004678 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004679
Chris Masonec29ed52011-02-23 16:23:20 -05004680 /*
4681 * we might have some extents allocated but more delalloc past those
4682 * extents. so, we trust isize unless the start of the last extent is
4683 * beyond isize
4684 */
4685 if (last < isize) {
4686 last = (u64)-1;
4687 last_for_get_extent = isize;
4688 }
4689
David Sterbaff13db42015-12-03 14:30:40 +01004690 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004691 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004692
David Sterbae3350e12017-06-23 04:09:57 +02004693 em = get_extent_skip_holes(inode, start, last_for_get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004694 if (!em)
4695 goto out;
4696 if (IS_ERR(em)) {
4697 ret = PTR_ERR(em);
4698 goto out;
4699 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004700
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004701 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004702 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004703
Chris Masonea8efc72011-03-08 11:54:40 -05004704 /* break if the extent we found is outside the range */
4705 if (em->start >= max || extent_map_end(em) < off)
4706 break;
4707
4708 /*
4709 * get_extent may return an extent that starts before our
4710 * requested range. We have to make sure the ranges
4711 * we return to fiemap always move forward and don't
4712 * overlap, so adjust the offsets here
4713 */
4714 em_start = max(em->start, off);
4715
4716 /*
4717 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004718 * for adjusting the disk offset below. Only do this if the
4719 * extent isn't compressed since our in ram offset may be past
4720 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004721 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004722 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4723 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004724 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004725 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004726 flags = 0;
Filipe Mananaf0986312018-06-20 10:02:30 +01004727 if (em->block_start < EXTENT_MAP_LAST_BYTE)
4728 disko = em->block_start + offset_in_extent;
4729 else
4730 disko = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004731
Chris Masonea8efc72011-03-08 11:54:40 -05004732 /*
4733 * bump off for our next call to get_extent
4734 */
4735 off = extent_map_end(em);
4736 if (off >= max)
4737 end = 1;
4738
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004739 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004740 end = 1;
4741 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004742 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004743 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4744 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004745 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004746 flags |= (FIEMAP_EXTENT_DELALLOC |
4747 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004748 } else if (fieinfo->fi_extents_max) {
4749 u64 bytenr = em->block_start -
4750 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004751
Liu Bofe09e162013-09-22 12:54:23 +08004752 /*
4753 * As btrfs supports shared space, this information
4754 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004755 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4756 * then we're just getting a count and we can skip the
4757 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004758 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06004759 ret = btrfs_check_shared(root,
4760 btrfs_ino(BTRFS_I(inode)),
David Sterba5911c8f2019-05-15 15:31:04 +02004761 bytenr, roots, tmp_ulist);
Josef Bacikdc046b12014-09-10 16:20:45 -04004762 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004763 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004764 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004765 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004766 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004767 }
4768 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4769 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004770 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4771 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004772
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004773 free_extent_map(em);
4774 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004775 if ((em_start >= last) || em_len == (u64)-1 ||
4776 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004777 flags |= FIEMAP_EXTENT_LAST;
4778 end = 1;
4779 }
4780
Chris Masonec29ed52011-02-23 16:23:20 -05004781 /* now scan forward to see if this is really the last extent. */
David Sterbae3350e12017-06-23 04:09:57 +02004782 em = get_extent_skip_holes(inode, off, last_for_get_extent);
Chris Masonec29ed52011-02-23 16:23:20 -05004783 if (IS_ERR(em)) {
4784 ret = PTR_ERR(em);
4785 goto out;
4786 }
4787 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004788 flags |= FIEMAP_EXTENT_LAST;
4789 end = 1;
4790 }
Qu Wenruo47518322017-04-07 10:43:15 +08004791 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4792 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004793 if (ret) {
4794 if (ret == 1)
4795 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004796 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004797 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004798 }
4799out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004800 if (!ret)
David Sterba5c5aff92019-03-20 11:29:46 +01004801 ret = emit_last_fiemap_cache(fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004802 free_extent_map(em);
4803out:
Liu Boa52f4cd2013-05-01 16:23:41 +00004804 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004805 &cached_state);
David Sterba5911c8f2019-05-15 15:31:04 +02004806
4807out_free_ulist:
Colin Ian Kinge02d48e2019-07-05 08:26:24 +01004808 btrfs_free_path(path);
David Sterba5911c8f2019-05-15 15:31:04 +02004809 ulist_free(roots);
4810 ulist_free(tmp_ulist);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004811 return ret;
4812}
4813
Chris Mason727011e2010-08-06 13:21:20 -04004814static void __free_extent_buffer(struct extent_buffer *eb)
4815{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004816 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004817 kmem_cache_free(extent_buffer_cache, eb);
4818}
4819
Josef Bacika26e8c92014-03-28 17:07:27 -04004820int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004821{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004822 return (atomic_read(&eb->io_pages) ||
4823 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4824 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004825}
4826
Miao Xie897ca6e92010-10-26 20:57:29 -04004827/*
David Sterba55ac0132018-07-19 17:24:32 +02004828 * Release all pages attached to the extent buffer.
Miao Xie897ca6e92010-10-26 20:57:29 -04004829 */
David Sterba55ac0132018-07-19 17:24:32 +02004830static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004831{
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004832 int i;
4833 int num_pages;
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004834 int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004835
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004836 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004837
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004838 num_pages = num_extent_pages(eb);
4839 for (i = 0; i < num_pages; i++) {
4840 struct page *page = eb->pages[i];
Miao Xie897ca6e92010-10-26 20:57:29 -04004841
Forrest Liu5d2361d2015-02-09 17:31:45 +08004842 if (!page)
4843 continue;
4844 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004845 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004846 /*
4847 * We do this since we'll remove the pages after we've
4848 * removed the eb from the radix tree, so we could race
4849 * and have this page now attached to the new eb. So
4850 * only clear page_private if it's still connected to
4851 * this eb.
4852 */
4853 if (PagePrivate(page) &&
4854 page->private == (unsigned long)eb) {
4855 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4856 BUG_ON(PageDirty(page));
4857 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004858 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004859 * We need to make sure we haven't be attached
4860 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004861 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004862 ClearPagePrivate(page);
4863 set_page_private(page, 0);
4864 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004865 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004866 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004867
4868 if (mapped)
4869 spin_unlock(&page->mapping->private_lock);
4870
Nicholas D Steeves01327612016-05-19 21:18:45 -04004871 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004872 put_page(page);
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004873 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004874}
4875
4876/*
4877 * Helper for releasing the extent buffer.
4878 */
4879static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4880{
David Sterba55ac0132018-07-19 17:24:32 +02004881 btrfs_release_extent_buffer_pages(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004882 __free_extent_buffer(eb);
4883}
4884
Josef Bacikf28491e2013-12-16 13:24:27 -05004885static struct extent_buffer *
4886__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004887 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004888{
4889 struct extent_buffer *eb = NULL;
4890
Michal Hockod1b5c562015-08-19 14:17:40 +02004891 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004892 eb->start = start;
4893 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004894 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004895 eb->bflags = 0;
4896 rwlock_init(&eb->lock);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004897 atomic_set(&eb->blocking_readers, 0);
David Sterba06297d82019-05-02 16:47:23 +02004898 eb->blocking_writers = 0;
David Sterbaed1b4ed2018-08-24 16:31:17 +02004899 eb->lock_nested = false;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004900 init_waitqueue_head(&eb->write_lock_wq);
4901 init_waitqueue_head(&eb->read_lock_wq);
4902
4903 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4904
4905 spin_lock_init(&eb->refs_lock);
4906 atomic_set(&eb->refs, 1);
4907 atomic_set(&eb->io_pages, 0);
4908
4909 /*
4910 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4911 */
4912 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4913 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4914 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4915
David Sterba843ccf92018-08-24 14:56:28 +02004916#ifdef CONFIG_BTRFS_DEBUG
David Sterbaf3dc24c2019-05-02 16:51:53 +02004917 eb->spinning_writers = 0;
David Sterbaafd495a2018-08-24 15:57:38 +02004918 atomic_set(&eb->spinning_readers, 0);
David Sterba5c9c7992018-08-24 16:15:51 +02004919 atomic_set(&eb->read_locks, 0);
David Sterba00801ae2019-05-02 16:53:47 +02004920 eb->write_locks = 0;
David Sterba843ccf92018-08-24 14:56:28 +02004921#endif
4922
Josef Bacikdb7f3432013-08-07 14:54:37 -04004923 return eb;
4924}
4925
4926struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4927{
David Sterbacc5e31a2018-03-01 18:20:27 +01004928 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004929 struct page *p;
4930 struct extent_buffer *new;
David Sterbacc5e31a2018-03-01 18:20:27 +01004931 int num_pages = num_extent_pages(src);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004932
David Sterba3f556f72014-06-15 03:20:26 +02004933 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004934 if (new == NULL)
4935 return NULL;
4936
4937 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004938 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004939 if (!p) {
4940 btrfs_release_extent_buffer(new);
4941 return NULL;
4942 }
4943 attach_extent_buffer_page(new, p);
4944 WARN_ON(PageDirty(p));
4945 SetPageUptodate(p);
4946 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004947 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004948 }
4949
Josef Bacikdb7f3432013-08-07 14:54:37 -04004950 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004951 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004952
4953 return new;
4954}
4955
Omar Sandoval0f331222015-09-29 20:50:31 -07004956struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4957 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004958{
4959 struct extent_buffer *eb;
David Sterbacc5e31a2018-03-01 18:20:27 +01004960 int num_pages;
4961 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004962
David Sterba3f556f72014-06-15 03:20:26 +02004963 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004964 if (!eb)
4965 return NULL;
4966
David Sterba65ad0102018-06-29 10:56:49 +02004967 num_pages = num_extent_pages(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004968 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004969 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004970 if (!eb->pages[i])
4971 goto err;
4972 }
4973 set_extent_buffer_uptodate(eb);
4974 btrfs_set_header_nritems(eb, 0);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004975 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004976
4977 return eb;
4978err:
4979 for (; i > 0; i--)
4980 __free_page(eb->pages[i - 1]);
4981 __free_extent_buffer(eb);
4982 return NULL;
4983}
4984
Omar Sandoval0f331222015-09-29 20:50:31 -07004985struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004986 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004987{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004988 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004989}
4990
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004991static void check_buffer_tree_ref(struct extent_buffer *eb)
4992{
Chris Mason242e18c2013-01-29 17:49:37 -05004993 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004994 /* the ref bit is tricky. We have to make sure it is set
4995 * if we have the buffer dirty. Otherwise the
4996 * code to free a buffer can end up dropping a dirty
4997 * page
4998 *
4999 * Once the ref bit is set, it won't go away while the
5000 * buffer is dirty or in writeback, and it also won't
5001 * go away while we have the reference count on the
5002 * eb bumped.
5003 *
5004 * We can't just set the ref bit without bumping the
5005 * ref on the eb because free_extent_buffer might
5006 * see the ref bit and try to clear it. If this happens
5007 * free_extent_buffer might end up dropping our original
5008 * ref by mistake and freeing the page before we are able
5009 * to add one more ref.
5010 *
5011 * So bump the ref count first, then set the bit. If someone
5012 * beat us to it, drop the ref we added.
5013 */
Chris Mason242e18c2013-01-29 17:49:37 -05005014 refs = atomic_read(&eb->refs);
5015 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5016 return;
5017
Josef Bacik594831c2012-07-20 16:11:08 -04005018 spin_lock(&eb->refs_lock);
5019 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005020 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04005021 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005022}
5023
Mel Gorman2457aec2014-06-04 16:10:31 -07005024static void mark_extent_buffer_accessed(struct extent_buffer *eb,
5025 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04005026{
David Sterbacc5e31a2018-03-01 18:20:27 +01005027 int num_pages, i;
Josef Bacik5df42352012-03-15 18:24:42 -04005028
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005029 check_buffer_tree_ref(eb);
5030
David Sterba65ad0102018-06-29 10:56:49 +02005031 num_pages = num_extent_pages(eb);
Josef Bacik5df42352012-03-15 18:24:42 -04005032 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005033 struct page *p = eb->pages[i];
5034
Mel Gorman2457aec2014-06-04 16:10:31 -07005035 if (p != accessed)
5036 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04005037 }
5038}
5039
Josef Bacikf28491e2013-12-16 13:24:27 -05005040struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
5041 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005042{
5043 struct extent_buffer *eb;
5044
5045 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05005046 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005047 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005048 if (eb && atomic_inc_not_zero(&eb->refs)) {
5049 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01005050 /*
5051 * Lock our eb's refs_lock to avoid races with
5052 * free_extent_buffer. When we get our eb it might be flagged
5053 * with EXTENT_BUFFER_STALE and another task running
5054 * free_extent_buffer might have seen that flag set,
5055 * eb->refs == 2, that the buffer isn't under IO (dirty and
5056 * writeback flags not set) and it's still in the tree (flag
5057 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
5058 * of decrementing the extent buffer's reference count twice.
5059 * So here we could race and increment the eb's reference count,
5060 * clear its stale flag, mark it as dirty and drop our reference
5061 * before the other task finishes executing free_extent_buffer,
5062 * which would later result in an attempt to free an extent
5063 * buffer that is dirty.
5064 */
5065 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
5066 spin_lock(&eb->refs_lock);
5067 spin_unlock(&eb->refs_lock);
5068 }
Mel Gorman2457aec2014-06-04 16:10:31 -07005069 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005070 return eb;
5071 }
5072 rcu_read_unlock();
5073
5074 return NULL;
5075}
5076
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005077#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
5078struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005079 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005080{
5081 struct extent_buffer *eb, *exists = NULL;
5082 int ret;
5083
5084 eb = find_extent_buffer(fs_info, start);
5085 if (eb)
5086 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04005087 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005088 if (!eb)
5089 return NULL;
5090 eb->fs_info = fs_info;
5091again:
David Sterbae1860a72016-05-09 14:11:38 +02005092 ret = radix_tree_preload(GFP_NOFS);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005093 if (ret)
5094 goto free_eb;
5095 spin_lock(&fs_info->buffer_lock);
5096 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005097 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005098 spin_unlock(&fs_info->buffer_lock);
5099 radix_tree_preload_end();
5100 if (ret == -EEXIST) {
5101 exists = find_extent_buffer(fs_info, start);
5102 if (exists)
5103 goto free_eb;
5104 else
5105 goto again;
5106 }
5107 check_buffer_tree_ref(eb);
5108 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
5109
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005110 return eb;
5111free_eb:
5112 btrfs_release_extent_buffer(eb);
5113 return exists;
5114}
5115#endif
5116
Josef Bacikf28491e2013-12-16 13:24:27 -05005117struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02005118 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05005119{
Jeff Mahoneyda170662016-06-15 09:22:56 -04005120 unsigned long len = fs_info->nodesize;
David Sterbacc5e31a2018-03-01 18:20:27 +01005121 int num_pages;
5122 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005123 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005124 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04005125 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05005126 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05005127 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05005128 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005129 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05005130
Jeff Mahoneyda170662016-06-15 09:22:56 -04005131 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07005132 btrfs_err(fs_info, "bad tree block start %llu", start);
5133 return ERR_PTR(-EINVAL);
5134 }
5135
Josef Bacikf28491e2013-12-16 13:24:27 -05005136 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005137 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04005138 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04005139
David Sterba23d79d82014-06-15 02:55:29 +02005140 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04005141 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07005142 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05005143
David Sterba65ad0102018-06-29 10:56:49 +02005144 num_pages = num_extent_pages(eb);
Chris Mason727011e2010-08-06 13:21:20 -04005145 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02005146 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07005147 if (!p) {
5148 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04005149 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005150 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005151
5152 spin_lock(&mapping->private_lock);
5153 if (PagePrivate(p)) {
5154 /*
5155 * We could have already allocated an eb for this page
5156 * and attached one so lets see if we can get a ref on
5157 * the existing eb, and if we can we know it's good and
5158 * we can just return that one, else we know we can just
5159 * overwrite page->private.
5160 */
5161 exists = (struct extent_buffer *)p->private;
5162 if (atomic_inc_not_zero(&exists->refs)) {
5163 spin_unlock(&mapping->private_lock);
5164 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005165 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07005166 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005167 goto free_eb;
5168 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005169 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005170
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005171 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005172 * Do this so attach doesn't complain and we need to
5173 * drop the ref the old guy had.
5174 */
5175 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005176 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005177 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05005178 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005179 attach_extent_buffer_page(eb, p);
5180 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005181 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04005182 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05005183 if (!PageUptodate(p))
5184 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05005185
5186 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005187 * We can't unlock the pages just yet since the extent buffer
5188 * hasn't been properly inserted in the radix tree, this
5189 * opens a race with btree_releasepage which can free a page
5190 * while we are still filling in all pages for the buffer and
5191 * we could crash.
Chris Masoneb14ab82011-02-10 12:35:00 -05005192 */
Chris Masond1310b22008-01-24 16:13:08 -05005193 }
5194 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05005195 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05005196again:
David Sterbae1860a72016-05-09 14:11:38 +02005197 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07005198 if (ret) {
5199 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04005200 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005201 }
Miao Xie19fe0a82010-10-26 20:57:29 -04005202
Josef Bacikf28491e2013-12-16 13:24:27 -05005203 spin_lock(&fs_info->buffer_lock);
5204 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005205 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05005206 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005207 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04005208 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005209 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005210 if (exists)
5211 goto free_eb;
5212 else
Josef Bacik115391d2012-03-09 09:51:43 -05005213 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04005214 }
Chris Mason6af118ce2008-07-22 11:18:07 -04005215 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005216 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005217 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005218
5219 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005220 * Now it's safe to unlock the pages because any calls to
5221 * btree_releasepage will correctly detect that a page belongs to a
5222 * live buffer and won't free them prematurely.
Chris Masoneb14ab82011-02-10 12:35:00 -05005223 */
Nikolay Borisov28187ae2018-07-04 10:24:51 +03005224 for (i = 0; i < num_pages; i++)
5225 unlock_page(eb->pages[i]);
Chris Masond1310b22008-01-24 16:13:08 -05005226 return eb;
5227
Chris Mason6af118ce2008-07-22 11:18:07 -04005228free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005229 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005230 for (i = 0; i < num_pages; i++) {
5231 if (eb->pages[i])
5232 unlock_page(eb->pages[i]);
5233 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005234
Miao Xie897ca6e92010-10-26 20:57:29 -04005235 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005236 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005237}
Chris Masond1310b22008-01-24 16:13:08 -05005238
Josef Bacik3083ee22012-03-09 16:01:49 -05005239static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5240{
5241 struct extent_buffer *eb =
5242 container_of(head, struct extent_buffer, rcu_head);
5243
5244 __free_extent_buffer(eb);
5245}
5246
David Sterbaf7a52a42013-04-26 14:56:29 +00005247static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005248{
Nikolay Borisov07e21c42018-06-27 16:38:23 +03005249 lockdep_assert_held(&eb->refs_lock);
5250
Josef Bacik3083ee22012-03-09 16:01:49 -05005251 WARN_ON(atomic_read(&eb->refs) == 0);
5252 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005253 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005254 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005255
Jan Schmidt815a51c2012-05-16 17:00:02 +02005256 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005257
Josef Bacikf28491e2013-12-16 13:24:27 -05005258 spin_lock(&fs_info->buffer_lock);
5259 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005260 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005261 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005262 } else {
5263 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005264 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005265
5266 /* Should be safe to release our pages at this point */
David Sterba55ac0132018-07-19 17:24:32 +02005267 btrfs_release_extent_buffer_pages(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005268#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005269 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
Josef Bacikbcb7e442015-03-16 17:38:02 -04005270 __free_extent_buffer(eb);
5271 return 1;
5272 }
5273#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005274 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005275 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005276 }
5277 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005278
5279 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005280}
5281
Chris Masond1310b22008-01-24 16:13:08 -05005282void free_extent_buffer(struct extent_buffer *eb)
5283{
Chris Mason242e18c2013-01-29 17:49:37 -05005284 int refs;
5285 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005286 if (!eb)
5287 return;
5288
Chris Mason242e18c2013-01-29 17:49:37 -05005289 while (1) {
5290 refs = atomic_read(&eb->refs);
Nikolay Borisov46cc7752018-10-15 17:04:01 +03005291 if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
5292 || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
5293 refs == 1))
Chris Mason242e18c2013-01-29 17:49:37 -05005294 break;
5295 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5296 if (old == refs)
5297 return;
5298 }
5299
Josef Bacik3083ee22012-03-09 16:01:49 -05005300 spin_lock(&eb->refs_lock);
5301 if (atomic_read(&eb->refs) == 2 &&
5302 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005303 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005304 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5305 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005306
Josef Bacik3083ee22012-03-09 16:01:49 -05005307 /*
5308 * I know this is terrible, but it's temporary until we stop tracking
5309 * the uptodate bits and such for the extent buffers.
5310 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005311 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005312}
Chris Masond1310b22008-01-24 16:13:08 -05005313
Josef Bacik3083ee22012-03-09 16:01:49 -05005314void free_extent_buffer_stale(struct extent_buffer *eb)
5315{
5316 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005317 return;
5318
Josef Bacik3083ee22012-03-09 16:01:49 -05005319 spin_lock(&eb->refs_lock);
5320 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5321
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005322 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005323 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5324 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005325 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005326}
5327
Chris Mason1d4284b2012-03-28 20:31:37 -04005328void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005329{
David Sterbacc5e31a2018-03-01 18:20:27 +01005330 int i;
5331 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005332 struct page *page;
5333
David Sterba65ad0102018-06-29 10:56:49 +02005334 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005335
5336 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005337 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005338 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005339 continue;
5340
Chris Masona61e6f22008-07-22 11:18:08 -04005341 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005342 WARN_ON(!PagePrivate(page));
5343
Chris Masond1310b22008-01-24 16:13:08 -05005344 clear_page_dirty_for_io(page);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005345 xa_lock_irq(&page->mapping->i_pages);
Matthew Wilcox0a943c62017-12-04 10:37:22 -05005346 if (!PageDirty(page))
5347 __xa_clear_mark(&page->mapping->i_pages,
5348 page_index(page), PAGECACHE_TAG_DIRTY);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005349 xa_unlock_irq(&page->mapping->i_pages);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005350 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005351 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005352 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005353 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005354}
Chris Masond1310b22008-01-24 16:13:08 -05005355
Liu Boabb57ef2018-09-14 01:44:42 +08005356bool set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005357{
David Sterbacc5e31a2018-03-01 18:20:27 +01005358 int i;
5359 int num_pages;
Liu Boabb57ef2018-09-14 01:44:42 +08005360 bool was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005361
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005362 check_buffer_tree_ref(eb);
5363
Chris Masonb9473432009-03-13 11:00:37 -04005364 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005365
David Sterba65ad0102018-06-29 10:56:49 +02005366 num_pages = num_extent_pages(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05005367 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005368 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5369
Liu Boabb57ef2018-09-14 01:44:42 +08005370 if (!was_dirty)
5371 for (i = 0; i < num_pages; i++)
5372 set_page_dirty(eb->pages[i]);
Liu Bo51995c32018-09-14 01:46:08 +08005373
5374#ifdef CONFIG_BTRFS_DEBUG
5375 for (i = 0; i < num_pages; i++)
5376 ASSERT(PageDirty(eb->pages[i]));
5377#endif
5378
Chris Masonb9473432009-03-13 11:00:37 -04005379 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005380}
Chris Masond1310b22008-01-24 16:13:08 -05005381
David Sterba69ba3922015-12-03 13:08:59 +01005382void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005383{
David Sterbacc5e31a2018-03-01 18:20:27 +01005384 int i;
Chris Mason1259ab72008-05-12 13:39:03 -04005385 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005386 int num_pages;
Chris Mason1259ab72008-05-12 13:39:03 -04005387
Chris Masonb4ce94d2009-02-04 09:25:08 -05005388 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005389 num_pages = num_extent_pages(eb);
Chris Mason1259ab72008-05-12 13:39:03 -04005390 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005391 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005392 if (page)
5393 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005394 }
Chris Mason1259ab72008-05-12 13:39:03 -04005395}
5396
David Sterba09c25a82015-12-03 13:08:59 +01005397void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005398{
David Sterbacc5e31a2018-03-01 18:20:27 +01005399 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005400 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005401 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005402
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005403 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005404 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005405 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005406 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005407 SetPageUptodate(page);
5408 }
Chris Masond1310b22008-01-24 16:13:08 -05005409}
Chris Masond1310b22008-01-24 16:13:08 -05005410
Nikolay Borisovc2ccfbc2019-04-10 17:24:40 +03005411int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005412{
David Sterbacc5e31a2018-03-01 18:20:27 +01005413 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005414 struct page *page;
5415 int err;
5416 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005417 int locked_pages = 0;
5418 int all_uptodate = 1;
David Sterbacc5e31a2018-03-01 18:20:27 +01005419 int num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005420 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005421 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005422 unsigned long bio_flags = 0;
Nikolay Borisovc2ccfbc2019-04-10 17:24:40 +03005423 struct extent_io_tree *tree = &BTRFS_I(eb->fs_info->btree_inode)->io_tree;
Chris Masona86c12c2008-02-07 10:50:54 -05005424
Chris Masonb4ce94d2009-02-04 09:25:08 -05005425 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005426 return 0;
5427
David Sterba65ad0102018-06-29 10:56:49 +02005428 num_pages = num_extent_pages(eb);
Josef Bacik8436ea912016-09-02 15:40:03 -04005429 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005430 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005431 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005432 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005433 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005434 } else {
5435 lock_page(page);
5436 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005437 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005438 }
5439 /*
5440 * We need to firstly lock all pages to make sure that
5441 * the uptodate bit of our pages won't be affected by
5442 * clear_extent_buffer_uptodate().
5443 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005444 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005445 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005446 if (!PageUptodate(page)) {
5447 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005448 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005449 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005450 }
Liu Bo2571e732016-08-03 12:33:01 -07005451
Chris Masonce9adaa2008-04-09 16:28:12 -04005452 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005453 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005454 goto unlock_exit;
5455 }
5456
Filipe Manana656f30d2014-09-26 12:25:56 +01005457 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005458 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005459 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005460 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005461 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005462
Chris Masonce9adaa2008-04-09 16:28:12 -04005463 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005464 if (ret) {
5465 atomic_dec(&eb->io_pages);
5466 unlock_page(page);
5467 continue;
5468 }
5469
Chris Masonf1885912008-04-09 16:28:12 -04005470 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005471 err = __extent_read_full_page(tree, page,
David Sterba6af49db2017-06-23 04:09:57 +02005472 btree_get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005473 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005474 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005475 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005476 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005477 /*
5478 * We use &bio in above __extent_read_full_page,
5479 * so we ensure that if it returns error, the
5480 * current page fails to add itself to bio and
5481 * it's been unlocked.
5482 *
5483 * We must dec io_pages by ourselves.
5484 */
5485 atomic_dec(&eb->io_pages);
5486 }
Chris Masond1310b22008-01-24 16:13:08 -05005487 } else {
5488 unlock_page(page);
5489 }
5490 }
5491
Jeff Mahoney355808c2011-10-03 23:23:14 -04005492 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005493 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005494 if (err)
5495 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005496 }
Chris Masona86c12c2008-02-07 10:50:54 -05005497
Arne Jansenbb82ab82011-06-10 14:06:53 +02005498 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005499 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005500
Josef Bacik8436ea912016-09-02 15:40:03 -04005501 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005502 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005503 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005504 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005505 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005506 }
Chris Masond3977122009-01-05 21:25:51 -05005507
Chris Masond1310b22008-01-24 16:13:08 -05005508 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005509
5510unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005511 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005512 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005513 page = eb->pages[locked_pages];
5514 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005515 }
5516 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005517}
Chris Masond1310b22008-01-24 16:13:08 -05005518
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005519void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5520 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005521{
5522 size_t cur;
5523 size_t offset;
5524 struct page *page;
5525 char *kaddr;
5526 char *dst = (char *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005527 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005528 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005529
Liu Bof716abd2017-08-09 11:10:16 -06005530 if (start + len > eb->len) {
5531 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5532 eb->start, eb->len, start, len);
5533 memset(dst, 0, len);
5534 return;
5535 }
Chris Masond1310b22008-01-24 16:13:08 -05005536
Johannes Thumshirn70730172018-12-05 15:23:03 +01005537 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005538
Chris Masond3977122009-01-05 21:25:51 -05005539 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005540 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005541
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005542 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005543 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005544 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005545
5546 dst += cur;
5547 len -= cur;
5548 offset = 0;
5549 i++;
5550 }
5551}
Chris Masond1310b22008-01-24 16:13:08 -05005552
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005553int read_extent_buffer_to_user(const struct extent_buffer *eb,
5554 void __user *dstv,
5555 unsigned long start, unsigned long len)
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005556{
5557 size_t cur;
5558 size_t offset;
5559 struct page *page;
5560 char *kaddr;
5561 char __user *dst = (char __user *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005562 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005563 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005564 int ret = 0;
5565
5566 WARN_ON(start > eb->len);
5567 WARN_ON(start + len > eb->start + eb->len);
5568
Johannes Thumshirn70730172018-12-05 15:23:03 +01005569 offset = offset_in_page(start_offset + start);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005570
5571 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005572 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005573
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005574 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005575 kaddr = page_address(page);
5576 if (copy_to_user(dst, kaddr + offset, cur)) {
5577 ret = -EFAULT;
5578 break;
5579 }
5580
5581 dst += cur;
5582 len -= cur;
5583 offset = 0;
5584 i++;
5585 }
5586
5587 return ret;
5588}
5589
Liu Bo415b35a2016-06-17 19:16:21 -07005590/*
5591 * return 0 if the item is found within a page.
5592 * return 1 if the item spans two pages.
5593 * return -EINVAL otherwise.
5594 */
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005595int map_private_extent_buffer(const struct extent_buffer *eb,
5596 unsigned long start, unsigned long min_len,
5597 char **map, unsigned long *map_start,
5598 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005599{
Johannes Thumshirncc2c39d2018-11-28 09:54:54 +01005600 size_t offset;
Chris Masond1310b22008-01-24 16:13:08 -05005601 char *kaddr;
5602 struct page *p;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005603 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005604 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005605 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005606 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005607
Liu Bof716abd2017-08-09 11:10:16 -06005608 if (start + min_len > eb->len) {
5609 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5610 eb->start, eb->len, start, min_len);
5611 return -EINVAL;
5612 }
5613
Chris Masond1310b22008-01-24 16:13:08 -05005614 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005615 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005616
5617 if (i == 0) {
5618 offset = start_offset;
5619 *map_start = 0;
5620 } else {
5621 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005622 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005623 }
Chris Masond3977122009-01-05 21:25:51 -05005624
David Sterbafb85fc92014-07-31 01:03:53 +02005625 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005626 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005627 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005628 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005629 return 0;
5630}
Chris Masond1310b22008-01-24 16:13:08 -05005631
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005632int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5633 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005634{
5635 size_t cur;
5636 size_t offset;
5637 struct page *page;
5638 char *kaddr;
5639 char *ptr = (char *)ptrv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005640 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005641 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005642 int ret = 0;
5643
5644 WARN_ON(start > eb->len);
5645 WARN_ON(start + len > eb->start + eb->len);
5646
Johannes Thumshirn70730172018-12-05 15:23:03 +01005647 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005648
Chris Masond3977122009-01-05 21:25:51 -05005649 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005650 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005651
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005652 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005653
Chris Masona6591712011-07-19 12:04:14 -04005654 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005655 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005656 if (ret)
5657 break;
5658
5659 ptr += cur;
5660 len -= cur;
5661 offset = 0;
5662 i++;
5663 }
5664 return ret;
5665}
Chris Masond1310b22008-01-24 16:13:08 -05005666
David Sterbaf157bf72016-11-09 17:43:38 +01005667void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5668 const void *srcv)
5669{
5670 char *kaddr;
5671
5672 WARN_ON(!PageUptodate(eb->pages[0]));
5673 kaddr = page_address(eb->pages[0]);
5674 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5675 BTRFS_FSID_SIZE);
5676}
5677
5678void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5679{
5680 char *kaddr;
5681
5682 WARN_ON(!PageUptodate(eb->pages[0]));
5683 kaddr = page_address(eb->pages[0]);
5684 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5685 BTRFS_FSID_SIZE);
5686}
5687
Chris Masond1310b22008-01-24 16:13:08 -05005688void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5689 unsigned long start, unsigned long len)
5690{
5691 size_t cur;
5692 size_t offset;
5693 struct page *page;
5694 char *kaddr;
5695 char *src = (char *)srcv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005696 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005697 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005698
5699 WARN_ON(start > eb->len);
5700 WARN_ON(start + len > eb->start + eb->len);
5701
Johannes Thumshirn70730172018-12-05 15:23:03 +01005702 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005703
Chris Masond3977122009-01-05 21:25:51 -05005704 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005705 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005706 WARN_ON(!PageUptodate(page));
5707
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005708 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005709 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005710 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005711
5712 src += cur;
5713 len -= cur;
5714 offset = 0;
5715 i++;
5716 }
5717}
Chris Masond1310b22008-01-24 16:13:08 -05005718
David Sterbab159fa22016-11-08 18:09:03 +01005719void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5720 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005721{
5722 size_t cur;
5723 size_t offset;
5724 struct page *page;
5725 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005726 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005727 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005728
5729 WARN_ON(start > eb->len);
5730 WARN_ON(start + len > eb->start + eb->len);
5731
Johannes Thumshirn70730172018-12-05 15:23:03 +01005732 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005733
Chris Masond3977122009-01-05 21:25:51 -05005734 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005735 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005736 WARN_ON(!PageUptodate(page));
5737
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005738 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005739 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005740 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005741
5742 len -= cur;
5743 offset = 0;
5744 i++;
5745 }
5746}
Chris Masond1310b22008-01-24 16:13:08 -05005747
David Sterba58e80122016-11-08 18:30:31 +01005748void copy_extent_buffer_full(struct extent_buffer *dst,
5749 struct extent_buffer *src)
5750{
5751 int i;
David Sterbacc5e31a2018-03-01 18:20:27 +01005752 int num_pages;
David Sterba58e80122016-11-08 18:30:31 +01005753
5754 ASSERT(dst->len == src->len);
5755
David Sterba65ad0102018-06-29 10:56:49 +02005756 num_pages = num_extent_pages(dst);
David Sterba58e80122016-11-08 18:30:31 +01005757 for (i = 0; i < num_pages; i++)
5758 copy_page(page_address(dst->pages[i]),
5759 page_address(src->pages[i]));
5760}
5761
Chris Masond1310b22008-01-24 16:13:08 -05005762void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5763 unsigned long dst_offset, unsigned long src_offset,
5764 unsigned long len)
5765{
5766 u64 dst_len = dst->len;
5767 size_t cur;
5768 size_t offset;
5769 struct page *page;
5770 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005771 size_t start_offset = offset_in_page(dst->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005772 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005773
5774 WARN_ON(src->len != dst_len);
5775
Johannes Thumshirn70730172018-12-05 15:23:03 +01005776 offset = offset_in_page(start_offset + dst_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005777
Chris Masond3977122009-01-05 21:25:51 -05005778 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005779 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005780 WARN_ON(!PageUptodate(page));
5781
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005782 cur = min(len, (unsigned long)(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 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005786
5787 src_offset += cur;
5788 len -= cur;
5789 offset = 0;
5790 i++;
5791 }
5792}
Chris Masond1310b22008-01-24 16:13:08 -05005793
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005794/*
5795 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5796 * given bit number
5797 * @eb: the extent buffer
5798 * @start: offset of the bitmap item in the extent buffer
5799 * @nr: bit number
5800 * @page_index: return index of the page in the extent buffer that contains the
5801 * given bit number
5802 * @page_offset: return offset into the page given by page_index
5803 *
5804 * This helper hides the ugliness of finding the byte in an extent buffer which
5805 * contains a given bit.
5806 */
5807static inline void eb_bitmap_offset(struct extent_buffer *eb,
5808 unsigned long start, unsigned long nr,
5809 unsigned long *page_index,
5810 size_t *page_offset)
5811{
Johannes Thumshirn70730172018-12-05 15:23:03 +01005812 size_t start_offset = offset_in_page(eb->start);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005813 size_t byte_offset = BIT_BYTE(nr);
5814 size_t offset;
5815
5816 /*
5817 * The byte we want is the offset of the extent buffer + the offset of
5818 * the bitmap item in the extent buffer + the offset of the byte in the
5819 * bitmap item.
5820 */
5821 offset = start_offset + start + byte_offset;
5822
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005823 *page_index = offset >> PAGE_SHIFT;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005824 *page_offset = offset_in_page(offset);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005825}
5826
5827/**
5828 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5829 * @eb: the extent buffer
5830 * @start: offset of the bitmap item in the extent buffer
5831 * @nr: bit number to test
5832 */
5833int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5834 unsigned long nr)
5835{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005836 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005837 struct page *page;
5838 unsigned long i;
5839 size_t offset;
5840
5841 eb_bitmap_offset(eb, start, nr, &i, &offset);
5842 page = eb->pages[i];
5843 WARN_ON(!PageUptodate(page));
5844 kaddr = page_address(page);
5845 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5846}
5847
5848/**
5849 * extent_buffer_bitmap_set - set an area of a bitmap
5850 * @eb: the extent buffer
5851 * @start: offset of the bitmap item in the extent buffer
5852 * @pos: bit number of the first bit
5853 * @len: number of bits to set
5854 */
5855void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5856 unsigned long pos, unsigned long len)
5857{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005858 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005859 struct page *page;
5860 unsigned long i;
5861 size_t offset;
5862 const unsigned int size = pos + len;
5863 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005864 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005865
5866 eb_bitmap_offset(eb, start, pos, &i, &offset);
5867 page = eb->pages[i];
5868 WARN_ON(!PageUptodate(page));
5869 kaddr = page_address(page);
5870
5871 while (len >= bits_to_set) {
5872 kaddr[offset] |= mask_to_set;
5873 len -= bits_to_set;
5874 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005875 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005876 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005877 offset = 0;
5878 page = eb->pages[++i];
5879 WARN_ON(!PageUptodate(page));
5880 kaddr = page_address(page);
5881 }
5882 }
5883 if (len) {
5884 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5885 kaddr[offset] |= mask_to_set;
5886 }
5887}
5888
5889
5890/**
5891 * extent_buffer_bitmap_clear - clear an area of a bitmap
5892 * @eb: the extent buffer
5893 * @start: offset of the bitmap item in the extent buffer
5894 * @pos: bit number of the first bit
5895 * @len: number of bits to clear
5896 */
5897void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5898 unsigned long pos, unsigned long len)
5899{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005900 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005901 struct page *page;
5902 unsigned long i;
5903 size_t offset;
5904 const unsigned int size = pos + len;
5905 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005906 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005907
5908 eb_bitmap_offset(eb, start, pos, &i, &offset);
5909 page = eb->pages[i];
5910 WARN_ON(!PageUptodate(page));
5911 kaddr = page_address(page);
5912
5913 while (len >= bits_to_clear) {
5914 kaddr[offset] &= ~mask_to_clear;
5915 len -= bits_to_clear;
5916 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005917 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005918 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005919 offset = 0;
5920 page = eb->pages[++i];
5921 WARN_ON(!PageUptodate(page));
5922 kaddr = page_address(page);
5923 }
5924 }
5925 if (len) {
5926 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5927 kaddr[offset] &= ~mask_to_clear;
5928 }
5929}
5930
Sergei Trofimovich33872062011-04-11 21:52:52 +00005931static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5932{
5933 unsigned long distance = (src > dst) ? src - dst : dst - src;
5934 return distance < len;
5935}
5936
Chris Masond1310b22008-01-24 16:13:08 -05005937static void copy_pages(struct page *dst_page, struct page *src_page,
5938 unsigned long dst_off, unsigned long src_off,
5939 unsigned long len)
5940{
Chris Masona6591712011-07-19 12:04:14 -04005941 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005942 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005943 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005944
Sergei Trofimovich33872062011-04-11 21:52:52 +00005945 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005946 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005947 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005948 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005949 if (areas_overlap(src_off, dst_off, len))
5950 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005951 }
Chris Masond1310b22008-01-24 16:13:08 -05005952
Chris Mason727011e2010-08-06 13:21:20 -04005953 if (must_memmove)
5954 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5955 else
5956 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005957}
5958
5959void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5960 unsigned long src_offset, unsigned long len)
5961{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005962 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005963 size_t cur;
5964 size_t dst_off_in_page;
5965 size_t src_off_in_page;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005966 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005967 unsigned long dst_i;
5968 unsigned long src_i;
5969
5970 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005971 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005972 "memmove bogus src_offset %lu move len %lu dst len %lu",
5973 src_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01005974 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05005975 }
5976 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005977 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005978 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5979 dst_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01005980 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05005981 }
5982
Chris Masond3977122009-01-05 21:25:51 -05005983 while (len > 0) {
Johannes Thumshirn70730172018-12-05 15:23:03 +01005984 dst_off_in_page = offset_in_page(start_offset + dst_offset);
5985 src_off_in_page = offset_in_page(start_offset + src_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005986
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005987 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5988 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005989
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005990 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005991 src_off_in_page));
5992 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005993 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005994
David Sterbafb85fc92014-07-31 01:03:53 +02005995 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005996 dst_off_in_page, src_off_in_page, cur);
5997
5998 src_offset += cur;
5999 dst_offset += cur;
6000 len -= cur;
6001 }
6002}
Chris Masond1310b22008-01-24 16:13:08 -05006003
6004void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
6005 unsigned long src_offset, unsigned long len)
6006{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006007 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05006008 size_t cur;
6009 size_t dst_off_in_page;
6010 size_t src_off_in_page;
6011 unsigned long dst_end = dst_offset + len - 1;
6012 unsigned long src_end = src_offset + len - 1;
Johannes Thumshirn70730172018-12-05 15:23:03 +01006013 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05006014 unsigned long dst_i;
6015 unsigned long src_i;
6016
6017 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006018 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006019 "memmove bogus src_offset %lu move len %lu len %lu",
6020 src_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01006021 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05006022 }
6023 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006024 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006025 "memmove bogus dst_offset %lu move len %lu len %lu",
6026 dst_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01006027 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05006028 }
Chris Mason727011e2010-08-06 13:21:20 -04006029 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05006030 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
6031 return;
6032 }
Chris Masond3977122009-01-05 21:25:51 -05006033 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006034 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
6035 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05006036
Johannes Thumshirn70730172018-12-05 15:23:03 +01006037 dst_off_in_page = offset_in_page(start_offset + dst_end);
6038 src_off_in_page = offset_in_page(start_offset + src_end);
Chris Masond1310b22008-01-24 16:13:08 -05006039
6040 cur = min_t(unsigned long, len, src_off_in_page + 1);
6041 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02006042 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05006043 dst_off_in_page - cur + 1,
6044 src_off_in_page - cur + 1, cur);
6045
6046 dst_end -= cur;
6047 src_end -= cur;
6048 len -= cur;
6049 }
6050}
Chris Mason6af118ce2008-07-22 11:18:07 -04006051
David Sterbaf7a52a42013-04-26 14:56:29 +00006052int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04006053{
Chris Mason6af118ce2008-07-22 11:18:07 -04006054 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04006055
Miao Xie19fe0a82010-10-26 20:57:29 -04006056 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006057 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05006058 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04006059 */
Josef Bacik3083ee22012-03-09 16:01:49 -05006060 spin_lock(&page->mapping->private_lock);
6061 if (!PagePrivate(page)) {
6062 spin_unlock(&page->mapping->private_lock);
6063 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04006064 }
6065
Josef Bacik3083ee22012-03-09 16:01:49 -05006066 eb = (struct extent_buffer *)page->private;
6067 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04006068
Josef Bacik0b32f4b2012-03-13 09:38:00 -04006069 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05006070 * This is a little awful but should be ok, we need to make sure that
6071 * the eb doesn't disappear out from under us while we're looking at
6072 * this page.
6073 */
6074 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04006075 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05006076 spin_unlock(&eb->refs_lock);
6077 spin_unlock(&page->mapping->private_lock);
6078 return 0;
6079 }
6080 spin_unlock(&page->mapping->private_lock);
6081
Josef Bacik3083ee22012-03-09 16:01:49 -05006082 /*
6083 * If tree ref isn't set then we know the ref on this eb is a real ref,
6084 * so just return, this page will likely be freed soon anyway.
6085 */
6086 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
6087 spin_unlock(&eb->refs_lock);
6088 return 0;
6089 }
Josef Bacik3083ee22012-03-09 16:01:49 -05006090
David Sterbaf7a52a42013-04-26 14:56:29 +00006091 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04006092}