blob: bbfb102d65b86f7f80fdfc59aa5bbf7e370ed128 [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;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002547 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002548 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002549
Miao Xiefacc8a222013-07-25 19:22:34 +08002550 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2551 if (btrfs_failed_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002552 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2553
2554 btrfs_bio = btrfs_io_bio(bio);
2555 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002556 icsum *= csum_size;
2557 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002558 csum_size);
2559 }
2560
Miao Xie2fe63032014-09-12 18:43:59 +08002561 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002562
Miao Xie2fe63032014-09-12 18:43:59 +08002563 return bio;
2564}
2565
2566/*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002567 * This is a generic handler for readpage errors. If other copies exist, read
2568 * those and write back good data to the failed position. Does not investigate
2569 * in remapping the failed extent elsewhere, hoping the device will be smart
2570 * enough to do this as needed
Miao Xie2fe63032014-09-12 18:43:59 +08002571 */
Miao Xie2fe63032014-09-12 18:43:59 +08002572static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2573 struct page *page, u64 start, u64 end,
2574 int failed_mirror)
2575{
2576 struct io_failure_record *failrec;
2577 struct inode *inode = page->mapping->host;
2578 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002579 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie2fe63032014-09-12 18:43:59 +08002580 struct bio *bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002581 int read_mode = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002582 blk_status_t status;
Miao Xie2fe63032014-09-12 18:43:59 +08002583 int ret;
Christoph Hellwig8a2ee442019-02-15 19:13:07 +08002584 unsigned failed_bio_pages = failed_bio->bi_iter.bi_size >> PAGE_SHIFT;
Miao Xie2fe63032014-09-12 18:43:59 +08002585
Mike Christie1f7ad752016-06-05 14:31:51 -05002586 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002587
2588 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2589 if (ret)
2590 return ret;
2591
Ming Leia0b60d72017-12-18 20:22:11 +08002592 if (!btrfs_check_repairable(inode, failed_bio_pages, failrec,
Liu Boc3cfb652017-07-13 15:00:50 -07002593 failed_mirror)) {
Josef Bacik7870d082017-05-05 11:57:15 -04002594 free_io_failure(failure_tree, tree, failrec);
Miao Xie2fe63032014-09-12 18:43:59 +08002595 return -EIO;
2596 }
2597
Ming Leia0b60d72017-12-18 20:22:11 +08002598 if (failed_bio_pages > 1)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002599 read_mode |= REQ_FAILFAST_DEV;
Miao Xie2fe63032014-09-12 18:43:59 +08002600
2601 phy_offset >>= inode->i_sb->s_blocksize_bits;
2602 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2603 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002604 (int)phy_offset, failed_bio->bi_end_io,
2605 NULL);
David Sterbaebcc3262018-06-29 10:56:53 +02002606 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie2fe63032014-09-12 18:43:59 +08002607
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002608 btrfs_debug(btrfs_sb(inode->i_sb),
2609 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2610 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002611
Linus Torvalds8c27cb32017-07-05 16:41:23 -07002612 status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002613 failrec->bio_flags);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002614 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04002615 free_io_failure(failure_tree, tree, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002616 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002617 ret = blk_status_to_errno(status);
Miao Xie6c387ab2014-09-12 18:43:57 +08002618 }
2619
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002620 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002621}
2622
Chris Masond1310b22008-01-24 16:13:08 -05002623/* lots and lots of room for performance fixes in the end_bio funcs */
2624
David Sterbab5227c02015-12-03 13:08:59 +01002625void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002626{
2627 int uptodate = (err == 0);
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002628 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002629
Nikolay Borisovc6297322018-11-08 10:18:08 +02002630 btrfs_writepage_endio_finish_ordered(page, start, end, uptodate);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002631
Jeff Mahoney87826df2012-02-15 16:23:57 +01002632 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002633 ClearPageUptodate(page);
2634 SetPageError(page);
Colin Ian Kingbff5baf2017-05-09 18:14:01 +01002635 ret = err < 0 ? err : -EIO;
Liu Bo5dca6ee2014-05-12 12:47:36 +08002636 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002637 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002638}
2639
Chris Masond1310b22008-01-24 16:13:08 -05002640/*
2641 * after a writepage IO is done, we need to:
2642 * clear the uptodate bits on error
2643 * clear the writeback bits in the extent tree for this IO
2644 * end_page_writeback if the page has no more pending IO
2645 *
2646 * Scheduling is not allowed, so the extent state tree is expected
2647 * to have one and only one object corresponding to this IO.
2648 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002649static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002650{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002651 int error = blk_status_to_errno(bio->bi_status);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002652 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002653 u64 start;
2654 u64 end;
Ming Lei6dc4f102019-02-15 19:13:19 +08002655 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002656
David Sterbac09abff2017-07-13 18:10:07 +02002657 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02002658 bio_for_each_segment_all(bvec, bio, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002659 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002660 struct inode *inode = page->mapping->host;
2661 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002662
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002663 /* We always issue full-page reads, but if some block
2664 * in a page fails to read, blk_update_request() will
2665 * advance bv_offset and adjust bv_len to compensate.
2666 * Print a warning for nonzero offsets, and an error
2667 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002668 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2669 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002670 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002671 "partial page write in btrfs with offset %u and length %u",
2672 bvec->bv_offset, bvec->bv_len);
2673 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002674 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002675 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002676 bvec->bv_offset, bvec->bv_len);
2677 }
Chris Masond1310b22008-01-24 16:13:08 -05002678
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002679 start = page_offset(page);
2680 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002681
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002682 end_extent_writepage(page, error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002683 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002684 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002685
Chris Masond1310b22008-01-24 16:13:08 -05002686 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002687}
2688
Miao Xie883d0de2013-07-25 19:22:35 +08002689static void
2690endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2691 int uptodate)
2692{
2693 struct extent_state *cached = NULL;
2694 u64 end = start + len - 1;
2695
2696 if (uptodate && tree->track_uptodate)
2697 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
David Sterbad810a4b2017-12-07 18:52:54 +01002698 unlock_extent_cached_atomic(tree, start, end, &cached);
Miao Xie883d0de2013-07-25 19:22:35 +08002699}
2700
Chris Masond1310b22008-01-24 16:13:08 -05002701/*
2702 * after a readpage IO is done, we need to:
2703 * clear the uptodate bits on error
2704 * set the uptodate bits if things worked
2705 * set the page up to date if all extents in the tree are uptodate
2706 * clear the lock bit in the extent tree
2707 * unlock the page if there are no other extents locked for it
2708 *
2709 * Scheduling is not allowed, so the extent state tree is expected
2710 * to have one and only one object corresponding to this IO.
2711 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002712static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002713{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002714 struct bio_vec *bvec;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002715 int uptodate = !bio->bi_status;
Miao Xiefacc8a222013-07-25 19:22:34 +08002716 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04002717 struct extent_io_tree *tree, *failure_tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002718 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002719 u64 start;
2720 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002721 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002722 u64 extent_start = 0;
2723 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002724 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002725 int ret;
Ming Lei6dc4f102019-02-15 19:13:19 +08002726 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002727
David Sterbac09abff2017-07-13 18:10:07 +02002728 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02002729 bio_for_each_segment_all(bvec, bio, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002730 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002731 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002732 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002733 bool data_inode = btrfs_ino(BTRFS_I(inode))
2734 != BTRFS_BTREE_INODE_OBJECTID;
Arne Jansen507903b2011-04-06 10:02:20 +00002735
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002736 btrfs_debug(fs_info,
2737 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002738 (u64)bio->bi_iter.bi_sector, bio->bi_status,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002739 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002740 tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002741 failure_tree = &BTRFS_I(inode)->io_failure_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002742
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002743 /* We always issue full-page reads, but if some block
2744 * in a page fails to read, blk_update_request() will
2745 * advance bv_offset and adjust bv_len to compensate.
2746 * Print a warning for nonzero offsets, and an error
2747 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002748 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2749 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002750 btrfs_err(fs_info,
2751 "partial page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002752 bvec->bv_offset, bvec->bv_len);
2753 else
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002754 btrfs_info(fs_info,
2755 "incomplete page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002756 bvec->bv_offset, bvec->bv_len);
2757 }
Chris Masond1310b22008-01-24 16:13:08 -05002758
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002759 start = page_offset(page);
2760 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002761 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002762
Chris Mason9be33952013-05-17 18:30:14 -04002763 mirror = io_bio->mirror_num;
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002764 if (likely(uptodate)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002765 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2766 page, start, end,
2767 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002768 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002769 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002770 else
Josef Bacik7870d082017-05-05 11:57:15 -04002771 clean_io_failure(BTRFS_I(inode)->root->fs_info,
2772 failure_tree, tree, start,
2773 page,
2774 btrfs_ino(BTRFS_I(inode)), 0);
Chris Masond1310b22008-01-24 16:13:08 -05002775 }
Josef Bacikea466792012-03-26 21:57:36 -04002776
Miao Xief2a09da2013-07-25 19:22:33 +08002777 if (likely(uptodate))
2778 goto readpage_ok;
2779
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002780 if (data_inode) {
Liu Bo9d0d1c82017-03-24 15:04:50 -07002781
2782 /*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002783 * The generic bio_readpage_error handles errors the
2784 * following way: If possible, new read requests are
2785 * created and submitted and will end up in
2786 * end_bio_extent_readpage as well (if we're lucky,
2787 * not in the !uptodate case). In that case it returns
2788 * 0 and we just go on with the next page in our bio.
2789 * If it can't handle the error it will return -EIO and
2790 * we remain responsible for that page.
Liu Bo9d0d1c82017-03-24 15:04:50 -07002791 */
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002792 ret = bio_readpage_error(bio, offset, page, start, end,
2793 mirror);
2794 if (ret == 0) {
2795 uptodate = !bio->bi_status;
2796 offset += len;
2797 continue;
2798 }
2799 } else {
2800 struct extent_buffer *eb;
2801
2802 eb = (struct extent_buffer *)page->private;
2803 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
2804 eb->read_mirror = mirror;
2805 atomic_dec(&eb->io_pages);
2806 if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD,
2807 &eb->bflags))
2808 btree_readahead_hook(eb, -EIO);
Chris Mason7e383262008-04-09 16:28:12 -04002809 }
Miao Xief2a09da2013-07-25 19:22:33 +08002810readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002811 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002812 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002813 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002814 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002815
2816 /* Zero out the end if this page straddles i_size */
Johannes Thumshirn70730172018-12-05 15:23:03 +01002817 off = offset_in_page(i_size);
Liu Boa583c022014-08-19 23:32:22 +08002818 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002819 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002820 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002821 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002822 ClearPageUptodate(page);
2823 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002824 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002825 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002826 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002827
2828 if (unlikely(!uptodate)) {
2829 if (extent_len) {
2830 endio_readpage_release_extent(tree,
2831 extent_start,
2832 extent_len, 1);
2833 extent_start = 0;
2834 extent_len = 0;
2835 }
2836 endio_readpage_release_extent(tree, start,
2837 end - start + 1, 0);
2838 } else if (!extent_len) {
2839 extent_start = start;
2840 extent_len = end + 1 - start;
2841 } else if (extent_start + extent_len == start) {
2842 extent_len += end + 1 - start;
2843 } else {
2844 endio_readpage_release_extent(tree, extent_start,
2845 extent_len, uptodate);
2846 extent_start = start;
2847 extent_len = end + 1 - start;
2848 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002849 }
Chris Masond1310b22008-01-24 16:13:08 -05002850
Miao Xie883d0de2013-07-25 19:22:35 +08002851 if (extent_len)
2852 endio_readpage_release_extent(tree, extent_start, extent_len,
2853 uptodate);
David Sterbab3a0dd52018-11-22 17:16:49 +01002854 btrfs_io_bio_free_csum(io_bio);
Chris Masond1310b22008-01-24 16:13:08 -05002855 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002856}
2857
Chris Mason9be33952013-05-17 18:30:14 -04002858/*
David Sterba184f9992017-06-12 17:29:39 +02002859 * Initialize the members up to but not including 'bio'. Use after allocating a
2860 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
2861 * 'bio' because use of __GFP_ZERO is not supported.
Chris Mason9be33952013-05-17 18:30:14 -04002862 */
David Sterba184f9992017-06-12 17:29:39 +02002863static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
Chris Masond1310b22008-01-24 16:13:08 -05002864{
David Sterba184f9992017-06-12 17:29:39 +02002865 memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
2866}
2867
2868/*
David Sterba6e707bc2017-06-02 17:26:26 +02002869 * The following helpers allocate a bio. As it's backed by a bioset, it'll
2870 * never fail. We're returning a bio right now but you can call btrfs_io_bio
2871 * for the appropriate container_of magic
Chris Masond1310b22008-01-24 16:13:08 -05002872 */
David Sterbae749af442019-06-18 20:00:16 +02002873struct bio *btrfs_bio_alloc(u64 first_byte)
Chris Masond1310b22008-01-24 16:13:08 -05002874{
2875 struct bio *bio;
2876
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002877 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset);
David Sterbac821e7f32017-06-02 18:35:36 +02002878 bio->bi_iter.bi_sector = first_byte >> 9;
David Sterba184f9992017-06-12 17:29:39 +02002879 btrfs_io_bio_init(btrfs_io_bio(bio));
Chris Masond1310b22008-01-24 16:13:08 -05002880 return bio;
2881}
2882
David Sterba8b6c1d52017-06-02 17:48:13 +02002883struct bio *btrfs_bio_clone(struct bio *bio)
Chris Mason9be33952013-05-17 18:30:14 -04002884{
Miao Xie23ea8e52014-09-12 18:43:54 +08002885 struct btrfs_io_bio *btrfs_bio;
2886 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002887
David Sterba6e707bc2017-06-02 17:26:26 +02002888 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002889 new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset);
David Sterba6e707bc2017-06-02 17:26:26 +02002890 btrfs_bio = btrfs_io_bio(new);
David Sterba184f9992017-06-12 17:29:39 +02002891 btrfs_io_bio_init(btrfs_bio);
David Sterba6e707bc2017-06-02 17:26:26 +02002892 btrfs_bio->iter = bio->bi_iter;
Miao Xie23ea8e52014-09-12 18:43:54 +08002893 return new;
2894}
Chris Mason9be33952013-05-17 18:30:14 -04002895
David Sterbac5e4c3d2017-06-12 17:29:41 +02002896struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
Chris Mason9be33952013-05-17 18:30:14 -04002897{
Miao Xiefacc8a222013-07-25 19:22:34 +08002898 struct bio *bio;
2899
David Sterba6e707bc2017-06-02 17:26:26 +02002900 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002901 bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
David Sterba184f9992017-06-12 17:29:39 +02002902 btrfs_io_bio_init(btrfs_io_bio(bio));
Miao Xiefacc8a222013-07-25 19:22:34 +08002903 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002904}
2905
Liu Boe4770942017-05-16 10:57:14 -07002906struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
Liu Bo2f8e9142017-05-15 17:43:31 -07002907{
2908 struct bio *bio;
2909 struct btrfs_io_bio *btrfs_bio;
2910
2911 /* this will never fail when it's backed by a bioset */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002912 bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset);
Liu Bo2f8e9142017-05-15 17:43:31 -07002913 ASSERT(bio);
2914
2915 btrfs_bio = btrfs_io_bio(bio);
David Sterba184f9992017-06-12 17:29:39 +02002916 btrfs_io_bio_init(btrfs_bio);
Liu Bo2f8e9142017-05-15 17:43:31 -07002917
2918 bio_trim(bio, offset >> 9, size >> 9);
Liu Bo17347ce2017-05-15 15:33:27 -07002919 btrfs_bio->iter = bio->bi_iter;
Liu Bo2f8e9142017-05-15 17:43:31 -07002920 return bio;
2921}
Chris Mason9be33952013-05-17 18:30:14 -04002922
David Sterba4b81ba42017-06-06 19:14:26 +02002923/*
2924 * @opf: bio REQ_OP_* and REQ_* flags as one value
David Sterbab8b3d622017-06-12 19:50:41 +02002925 * @tree: tree so we can call our merge_bio hook
2926 * @wbc: optional writeback control for io accounting
2927 * @page: page to add to the bio
2928 * @pg_offset: offset of the new bio or to check whether we are adding
2929 * a contiguous page to the previous one
2930 * @size: portion of page that we want to write
2931 * @offset: starting offset in the page
David Sterba5c2b1fd2017-06-06 19:22:55 +02002932 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
David Sterbab8b3d622017-06-12 19:50:41 +02002933 * @end_io_func: end_io callback for new bio
2934 * @mirror_num: desired mirror to read/write
2935 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
2936 * @bio_flags: flags of the current bio to see if we can merge them
David Sterba4b81ba42017-06-06 19:14:26 +02002937 */
2938static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002939 struct writeback_control *wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02002940 struct page *page, u64 offset,
David Sterba6c5a4e22017-10-04 17:10:34 +02002941 size_t size, unsigned long pg_offset,
Chris Masond1310b22008-01-24 16:13:08 -05002942 struct bio **bio_ret,
Chris Masonf1885912008-04-09 16:28:12 -04002943 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002944 int mirror_num,
2945 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002946 unsigned long bio_flags,
2947 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002948{
2949 int ret = 0;
2950 struct bio *bio;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002951 size_t page_size = min_t(size_t, size, PAGE_SIZE);
David Sterba6273b7f2017-10-04 17:30:11 +02002952 sector_t sector = offset >> 9;
Chris Masond1310b22008-01-24 16:13:08 -05002953
David Sterba5c2b1fd2017-06-06 19:22:55 +02002954 ASSERT(bio_ret);
2955
2956 if (*bio_ret) {
David Sterba0c8508a2017-06-12 20:00:43 +02002957 bool contig;
2958 bool can_merge = true;
2959
Chris Masond1310b22008-01-24 16:13:08 -05002960 bio = *bio_ret;
David Sterba0c8508a2017-06-12 20:00:43 +02002961 if (prev_bio_flags & EXTENT_BIO_COMPRESSED)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002962 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002963 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002964 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002965
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002966 ASSERT(tree->ops);
2967 if (btrfs_bio_fits_in_stripe(page, page_size, bio, bio_flags))
David Sterba0c8508a2017-06-12 20:00:43 +02002968 can_merge = false;
2969
2970 if (prev_bio_flags != bio_flags || !contig || !can_merge ||
Filipe Manana005efed2015-09-14 09:09:31 +01002971 force_bio_submit ||
David Sterba6c5a4e22017-10-04 17:10:34 +02002972 bio_add_page(bio, page, page_size, pg_offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002973 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002974 if (ret < 0) {
2975 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002976 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002977 }
Chris Masond1310b22008-01-24 16:13:08 -05002978 bio = NULL;
2979 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002980 if (wbc)
Tejun Heo34e51a52019-06-27 13:39:49 -07002981 wbc_account_cgroup_owner(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002982 return 0;
2983 }
2984 }
Chris Masonc8b97812008-10-29 14:49:59 -04002985
David Sterbae749af442019-06-18 20:00:16 +02002986 bio = btrfs_bio_alloc(offset);
David Sterba6c5a4e22017-10-04 17:10:34 +02002987 bio_add_page(bio, page, page_size, pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002988 bio->bi_end_io = end_io_func;
2989 bio->bi_private = tree;
Jens Axboee6959b92017-06-27 11:51:28 -06002990 bio->bi_write_hint = page->mapping->host->i_write_hint;
David Sterba4b81ba42017-06-06 19:14:26 +02002991 bio->bi_opf = opf;
Chris Masonda2f0f72015-07-02 13:57:22 -07002992 if (wbc) {
David Sterba429aebc2019-11-18 23:27:55 +01002993 struct block_device *bdev;
2994
2995 bdev = BTRFS_I(page->mapping->host)->root->fs_info->fs_devices->latest_bdev;
2996 bio_set_dev(bio, bdev);
Chris Masonda2f0f72015-07-02 13:57:22 -07002997 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
Omar Sandoval39b07b52019-12-02 17:34:23 -08003046 em = get_extent(BTRFS_I(inode), page, pg_offset, start, len);
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;
Liu Bobaf863b2016-07-11 10:39:07 -07003078 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003079 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02003080 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003081 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04003082 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05003083 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00003084 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003085
3086 set_page_extent_mapped(page);
3087
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003088 if (!PageUptodate(page)) {
3089 if (cleancache_get_page(page) == 0) {
3090 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003091 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003092 goto out;
3093 }
3094 }
3095
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003096 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04003097 char *userpage;
Johannes Thumshirn70730172018-12-05 15:23:03 +01003098 size_t zero_offset = offset_in_page(last_byte);
Chris Masonc8b97812008-10-29 14:49:59 -04003099
3100 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003101 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08003102 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04003103 memset(userpage + zero_offset, 0, iosize);
3104 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003105 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04003106 }
3107 }
Chris Masond1310b22008-01-24 16:13:08 -05003108 while (cur <= end) {
Filipe Manana005efed2015-09-14 09:09:31 +01003109 bool force_bio_submit = false;
David Sterba6273b7f2017-10-04 17:30:11 +02003110 u64 offset;
Josef Bacikc8f2f242013-02-11 11:33:00 -05003111
Chris Masond1310b22008-01-24 16:13:08 -05003112 if (cur >= last_byte) {
3113 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003114 struct extent_state *cached = NULL;
3115
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003116 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08003117 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003118 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003119 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003120 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003121 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003122 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003123 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01003124 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003125 break;
3126 }
Miao Xie125bac012013-07-25 19:22:37 +08003127 em = __get_extent_map(inode, page, pg_offset, cur,
3128 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02003129 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003130 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003131 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003132 break;
3133 }
Chris Masond1310b22008-01-24 16:13:08 -05003134 extent_offset = cur - em->start;
3135 BUG_ON(extent_map_end(em) <= cur);
3136 BUG_ON(end < cur);
3137
Li Zefan261507a02010-12-17 14:21:50 +08003138 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07003139 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08003140 extent_set_compress_type(&this_bio_flag,
3141 em->compress_type);
3142 }
Chris Masonc8b97812008-10-29 14:49:59 -04003143
Chris Masond1310b22008-01-24 16:13:08 -05003144 iosize = min(extent_map_end(em) - cur, end - cur + 1);
3145 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00003146 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04003147 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
3148 disk_io_size = em->block_len;
David Sterba6273b7f2017-10-04 17:30:11 +02003149 offset = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003150 } else {
David Sterba6273b7f2017-10-04 17:30:11 +02003151 offset = em->block_start + extent_offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003152 disk_io_size = iosize;
3153 }
Chris Masond1310b22008-01-24 16:13:08 -05003154 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04003155 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
3156 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01003157
3158 /*
3159 * If we have a file range that points to a compressed extent
3160 * and it's followed by a consecutive file range that points to
3161 * to the same compressed extent (possibly with a different
3162 * offset and/or length, so it either points to the whole extent
3163 * or only part of it), we must make sure we do not submit a
3164 * single bio to populate the pages for the 2 ranges because
3165 * this makes the compressed extent read zero out the pages
3166 * belonging to the 2nd range. Imagine the following scenario:
3167 *
3168 * File layout
3169 * [0 - 8K] [8K - 24K]
3170 * | |
3171 * | |
3172 * points to extent X, points to extent X,
3173 * offset 4K, length of 8K offset 0, length 16K
3174 *
3175 * [extent X, compressed length = 4K uncompressed length = 16K]
3176 *
3177 * If the bio to read the compressed extent covers both ranges,
3178 * it will decompress extent X into the pages belonging to the
3179 * first range and then it will stop, zeroing out the remaining
3180 * pages that belong to the other range that points to extent X.
3181 * So here we make sure we submit 2 bios, one for the first
3182 * range and another one for the third range. Both will target
3183 * the same physical extent from disk, but we can't currently
3184 * make the compressed bio endio callback populate the pages
3185 * for both ranges because each compressed bio is tightly
3186 * coupled with a single extent map, and each range can have
3187 * an extent map with a different offset value relative to the
3188 * uncompressed data of our extent and different lengths. This
3189 * is a corner case so we prioritize correctness over
3190 * non-optimal behavior (submitting 2 bios for the same extent).
3191 */
3192 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3193 prev_em_start && *prev_em_start != (u64)-1 &&
Filipe Manana8e928212019-02-14 15:17:20 +00003194 *prev_em_start != em->start)
Filipe Manana005efed2015-09-14 09:09:31 +01003195 force_bio_submit = true;
3196
3197 if (prev_em_start)
Filipe Manana8e928212019-02-14 15:17:20 +00003198 *prev_em_start = em->start;
Filipe Manana005efed2015-09-14 09:09:31 +01003199
Chris Masond1310b22008-01-24 16:13:08 -05003200 free_extent_map(em);
3201 em = NULL;
3202
3203 /* we've found a hole, just zero and go on */
3204 if (block_start == EXTENT_MAP_HOLE) {
3205 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003206 struct extent_state *cached = NULL;
3207
Cong Wang7ac687d2011-11-25 23:14:28 +08003208 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003209 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003210 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003211 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003212
3213 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003214 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003215 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01003216 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003217 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003218 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003219 continue;
3220 }
3221 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003222 if (test_range_bit(tree, cur, cur_end,
3223 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003224 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003225 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003226 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003227 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003228 continue;
3229 }
Chris Mason70dec802008-01-29 09:59:12 -05003230 /* we have an inline extent but it didn't get marked up
3231 * to date. Error out
3232 */
3233 if (block_start == EXTENT_MAP_INLINE) {
3234 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003235 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003236 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003237 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003238 continue;
3239 }
Chris Masond1310b22008-01-24 16:13:08 -05003240
David Sterba4b81ba42017-06-06 19:14:26 +02003241 ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL,
David Sterba6273b7f2017-10-04 17:30:11 +02003242 page, offset, disk_io_size,
David Sterbafa17ed02019-10-03 17:29:05 +02003243 pg_offset, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003244 end_bio_extent_readpage, mirror_num,
3245 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003246 this_bio_flag,
3247 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003248 if (!ret) {
3249 nr++;
3250 *bio_flags = this_bio_flag;
3251 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003252 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003253 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003254 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003255 }
Chris Masond1310b22008-01-24 16:13:08 -05003256 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003257 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003258 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003259out:
Chris Masond1310b22008-01-24 16:13:08 -05003260 if (!nr) {
3261 if (!PageError(page))
3262 SetPageUptodate(page);
3263 unlock_page(page);
3264 }
Liu Bobaf863b2016-07-11 10:39:07 -07003265 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003266}
3267
Nikolay Borisove65ef212019-03-11 09:55:38 +02003268static inline void contiguous_readpages(struct extent_io_tree *tree,
Miao Xie99740902013-07-25 19:22:36 +08003269 struct page *pages[], int nr_pages,
3270 u64 start, u64 end,
Miao Xie125bac012013-07-25 19:22:37 +08003271 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003272 struct bio **bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003273 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003274 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003275{
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03003276 struct btrfs_inode *inode = BTRFS_I(pages[0]->mapping->host);
Miao Xie99740902013-07-25 19:22:36 +08003277 int index;
3278
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03003279 btrfs_lock_and_flush_ordered_range(tree, inode, start, end, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003280
3281 for (index = 0; index < nr_pages; index++) {
David Sterba4ef77692017-06-23 04:09:57 +02003282 __do_readpage(tree, pages[index], btrfs_get_extent, em_cached,
Jens Axboe5e9d3982018-08-17 15:45:39 -07003283 bio, 0, bio_flags, REQ_RAHEAD, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003284 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003285 }
3286}
3287
Miao Xie99740902013-07-25 19:22:36 +08003288static int __extent_read_full_page(struct extent_io_tree *tree,
3289 struct page *page,
3290 get_extent_t *get_extent,
3291 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003292 unsigned long *bio_flags,
3293 unsigned int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003294{
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03003295 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
Miao Xie99740902013-07-25 19:22:36 +08003296 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003297 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003298 int ret;
3299
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03003300 btrfs_lock_and_flush_ordered_range(tree, inode, start, end, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003301
Miao Xie125bac012013-07-25 19:22:37 +08003302 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003303 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003304 return ret;
3305}
3306
Chris Masond1310b22008-01-24 16:13:08 -05003307int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003308 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003309{
3310 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003311 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003312 int ret;
3313
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003314 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003315 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003316 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003317 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003318 return ret;
3319}
Chris Masond1310b22008-01-24 16:13:08 -05003320
David Sterba3d4b9492017-02-10 19:33:41 +01003321static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003322 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003323{
3324 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003325}
3326
Chris Masond1310b22008-01-24 16:13:08 -05003327/*
Chris Mason40f76582014-05-21 13:35:51 -07003328 * helper for __extent_writepage, doing all of the delayed allocation setup.
3329 *
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003330 * This returns 1 if btrfs_run_delalloc_range function did all the work required
Chris Mason40f76582014-05-21 13:35:51 -07003331 * to write the page (copy into inline extent). In this case the IO has
3332 * been started and the page is already unlocked.
3333 *
3334 * This returns 0 if all went well (page still locked)
3335 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003336 */
Chris Mason40f76582014-05-21 13:35:51 -07003337static noinline_for_stack int writepage_delalloc(struct inode *inode,
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003338 struct page *page, struct writeback_control *wbc,
3339 u64 delalloc_start, unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003340{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003341 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Lu Fengqi3522e902018-11-29 11:33:38 +08003342 bool found;
Chris Mason40f76582014-05-21 13:35:51 -07003343 u64 delalloc_to_write = 0;
3344 u64 delalloc_end = 0;
3345 int ret;
3346 int page_started = 0;
3347
Chris Mason40f76582014-05-21 13:35:51 -07003348
3349 while (delalloc_end < page_end) {
Goldwyn Rodrigues99780592019-06-21 10:02:54 -05003350 found = find_lock_delalloc_range(inode, page,
Chris Mason40f76582014-05-21 13:35:51 -07003351 &delalloc_start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03003352 &delalloc_end);
Lu Fengqi3522e902018-11-29 11:33:38 +08003353 if (!found) {
Chris Mason40f76582014-05-21 13:35:51 -07003354 delalloc_start = delalloc_end + 1;
3355 continue;
3356 }
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003357 ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
3358 delalloc_end, &page_started, nr_written, wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003359 if (ret) {
3360 SetPageError(page);
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003361 /*
3362 * btrfs_run_delalloc_range should return < 0 for error
3363 * but just in case, we use > 0 here meaning the IO is
3364 * started, so we don't want to return > 0 unless
3365 * things are going well.
Chris Mason40f76582014-05-21 13:35:51 -07003366 */
3367 ret = ret < 0 ? ret : -EIO;
3368 goto done;
3369 }
3370 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003371 * delalloc_end is already one less than the total length, so
3372 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003373 */
3374 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003375 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003376 delalloc_start = delalloc_end + 1;
3377 }
3378 if (wbc->nr_to_write < delalloc_to_write) {
3379 int thresh = 8192;
3380
3381 if (delalloc_to_write < thresh * 2)
3382 thresh = delalloc_to_write;
3383 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3384 thresh);
3385 }
3386
3387 /* did the fill delalloc function already unlock and start
3388 * the IO?
3389 */
3390 if (page_started) {
3391 /*
3392 * we've unlocked the page, so we can't update
3393 * the mapping's writeback index, just update
3394 * nr_to_write.
3395 */
3396 wbc->nr_to_write -= *nr_written;
3397 return 1;
3398 }
3399
3400 ret = 0;
3401
3402done:
3403 return ret;
3404}
3405
3406/*
3407 * helper for __extent_writepage. This calls the writepage start hooks,
3408 * and does the loop to map the page into extents and bios.
3409 *
3410 * We return 1 if the IO is started and the page is unlocked,
3411 * 0 if all went well (page still locked)
3412 * < 0 if there were errors (page still locked)
3413 */
3414static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3415 struct page *page,
3416 struct writeback_control *wbc,
3417 struct extent_page_data *epd,
3418 loff_t i_size,
3419 unsigned long nr_written,
David Sterba57e5ffe2019-10-29 18:28:55 +01003420 int *nr_ret)
Chris Mason40f76582014-05-21 13:35:51 -07003421{
Chris Masond1310b22008-01-24 16:13:08 -05003422 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003423 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003424 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003425 u64 end;
3426 u64 cur = start;
3427 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003428 u64 block_start;
3429 u64 iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003430 struct extent_map *em;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003431 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003432 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003433 int ret = 0;
3434 int nr = 0;
David Sterba57e5ffe2019-10-29 18:28:55 +01003435 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003436 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003437
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003438 ret = btrfs_writepage_cow_fixup(page, start, page_end);
3439 if (ret) {
3440 /* Fixup worker will requeue */
3441 if (ret == -EBUSY)
3442 wbc->pages_skipped++;
3443 else
3444 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003445
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003446 update_nr_written(wbc, nr_written);
3447 unlock_page(page);
3448 return 1;
Chris Mason247e7432008-07-17 12:53:51 -04003449 }
3450
Chris Mason11c83492009-04-20 15:50:09 -04003451 /*
3452 * we don't want to touch the inode after unlocking the page,
3453 * so we update the mapping writeback index now
3454 */
David Sterba3d4b9492017-02-10 19:33:41 +01003455 update_nr_written(wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003456
Chris Masond1310b22008-01-24 16:13:08 -05003457 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05003458 blocksize = inode->i_sb->s_blocksize;
3459
3460 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003461 u64 em_end;
David Sterba6273b7f2017-10-04 17:30:11 +02003462 u64 offset;
David Sterba58409ed2016-05-04 11:46:10 +02003463
Chris Mason40f76582014-05-21 13:35:51 -07003464 if (cur >= i_size) {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003465 btrfs_writepage_endio_finish_ordered(page, cur,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003466 page_end, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003467 break;
3468 }
Omar Sandoval39b07b52019-12-02 17:34:23 -08003469 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur,
3470 end - cur + 1);
David Sterbac7040052011-04-19 18:00:01 +02003471 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003472 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003473 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003474 break;
3475 }
3476
3477 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003478 em_end = extent_map_end(em);
3479 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003480 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003481 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003482 iosize = ALIGN(iosize, blocksize);
David Sterba6273b7f2017-10-04 17:30:11 +02003483 offset = em->block_start + extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003484 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003485 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003486 free_extent_map(em);
3487 em = NULL;
3488
Chris Masonc8b97812008-10-29 14:49:59 -04003489 /*
3490 * compressed and inline extents are written through other
3491 * paths in the FS
3492 */
3493 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003494 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003495 /*
3496 * end_io notification does not happen here for
3497 * compressed extents
3498 */
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003499 if (!compressed)
3500 btrfs_writepage_endio_finish_ordered(page, cur,
3501 cur + iosize - 1,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003502 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003503 else if (compressed) {
3504 /* we don't want to end_page_writeback on
3505 * a compressed extent. this happens
3506 * elsewhere
3507 */
3508 nr++;
3509 }
3510
3511 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003512 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003513 continue;
3514 }
Chris Masonc8b97812008-10-29 14:49:59 -04003515
David Sterba5cdc84b2018-07-18 20:32:52 +02003516 btrfs_set_range_writeback(tree, cur, cur + iosize - 1);
David Sterba58409ed2016-05-04 11:46:10 +02003517 if (!PageWriteback(page)) {
3518 btrfs_err(BTRFS_I(inode)->root->fs_info,
3519 "page %lu not writeback, cur %llu end %llu",
3520 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003521 }
David Sterba58409ed2016-05-04 11:46:10 +02003522
David Sterba4b81ba42017-06-06 19:14:26 +02003523 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003524 page, offset, iosize, pg_offset,
David Sterbafa17ed02019-10-03 17:29:05 +02003525 &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003526 end_bio_extent_writepage,
3527 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003528 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003529 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003530 if (PageWriteback(page))
3531 end_page_writeback(page);
3532 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003533
Chris Masond1310b22008-01-24 16:13:08 -05003534 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003535 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003536 nr++;
3537 }
Chris Mason40f76582014-05-21 13:35:51 -07003538 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003539 return ret;
3540}
3541
3542/*
3543 * the writepage semantics are similar to regular writepage. extent
3544 * records are inserted to lock ranges in the tree, and as dirty areas
3545 * are found, they are marked writeback. Then the lock bits are removed
3546 * and the end_io handler clears the writeback ranges
Qu Wenruo30659762019-03-20 14:27:42 +08003547 *
3548 * Return 0 if everything goes well.
3549 * Return <0 for error.
Chris Mason40f76582014-05-21 13:35:51 -07003550 */
3551static int __extent_writepage(struct page *page, struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003552 struct extent_page_data *epd)
Chris Mason40f76582014-05-21 13:35:51 -07003553{
3554 struct inode *inode = page->mapping->host;
Chris Mason40f76582014-05-21 13:35:51 -07003555 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003556 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003557 int ret;
3558 int nr = 0;
Omar Sandovaleb70d222019-12-02 17:34:20 -08003559 size_t pg_offset;
Chris Mason40f76582014-05-21 13:35:51 -07003560 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003561 unsigned long end_index = i_size >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003562 unsigned long nr_written = 0;
3563
Chris Mason40f76582014-05-21 13:35:51 -07003564 trace___extent_writepage(page, inode, wbc);
3565
3566 WARN_ON(!PageLocked(page));
3567
3568 ClearPageError(page);
3569
Johannes Thumshirn70730172018-12-05 15:23:03 +01003570 pg_offset = offset_in_page(i_size);
Chris Mason40f76582014-05-21 13:35:51 -07003571 if (page->index > end_index ||
3572 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003573 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003574 unlock_page(page);
3575 return 0;
3576 }
3577
3578 if (page->index == end_index) {
3579 char *userpage;
3580
3581 userpage = kmap_atomic(page);
3582 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003583 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003584 kunmap_atomic(userpage);
3585 flush_dcache_page(page);
3586 }
3587
Chris Mason40f76582014-05-21 13:35:51 -07003588 set_page_extent_mapped(page);
3589
Nikolay Borisov7789a552018-11-08 10:18:06 +02003590 if (!epd->extent_locked) {
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003591 ret = writepage_delalloc(inode, page, wbc, start, &nr_written);
Nikolay Borisov7789a552018-11-08 10:18:06 +02003592 if (ret == 1)
Omar Sandoval169d2c82019-12-02 17:34:21 -08003593 return 0;
Nikolay Borisov7789a552018-11-08 10:18:06 +02003594 if (ret)
3595 goto done;
3596 }
Chris Mason40f76582014-05-21 13:35:51 -07003597
3598 ret = __extent_writepage_io(inode, page, wbc, epd,
David Sterba57e5ffe2019-10-29 18:28:55 +01003599 i_size, nr_written, &nr);
Chris Mason40f76582014-05-21 13:35:51 -07003600 if (ret == 1)
Omar Sandoval169d2c82019-12-02 17:34:21 -08003601 return 0;
Chris Mason40f76582014-05-21 13:35:51 -07003602
3603done:
Chris Masond1310b22008-01-24 16:13:08 -05003604 if (nr == 0) {
3605 /* make sure the mapping tag for page dirty gets cleared */
3606 set_page_writeback(page);
3607 end_page_writeback(page);
3608 }
Filipe Manana61391d52014-05-09 17:17:40 +01003609 if (PageError(page)) {
3610 ret = ret < 0 ? ret : -EIO;
3611 end_extent_writepage(page, ret, start, page_end);
3612 }
Chris Masond1310b22008-01-24 16:13:08 -05003613 unlock_page(page);
Qu Wenruo30659762019-03-20 14:27:42 +08003614 ASSERT(ret <= 0);
Chris Mason40f76582014-05-21 13:35:51 -07003615 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003616}
3617
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003618void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003619{
NeilBrown74316202014-07-07 15:16:04 +10003620 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3621 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003622}
3623
Filipe Manana18dfa712019-09-11 17:42:00 +01003624static void end_extent_buffer_writeback(struct extent_buffer *eb)
3625{
3626 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3627 smp_mb__after_atomic();
3628 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3629}
3630
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003631/*
3632 * Lock eb pages and flush the bio if we can't the locks
3633 *
3634 * Return 0 if nothing went wrong
3635 * Return >0 is same as 0, except bio is not submitted
3636 * Return <0 if something went wrong, no page is locked
3637 */
David Sterba9df76fb2019-03-20 11:21:41 +01003638static noinline_for_stack int lock_extent_buffer_for_io(struct extent_buffer *eb,
Chris Mason0e378df2014-05-19 20:55:27 -07003639 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003640{
David Sterba9df76fb2019-03-20 11:21:41 +01003641 struct btrfs_fs_info *fs_info = eb->fs_info;
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003642 int i, num_pages, failed_page_nr;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003643 int flush = 0;
3644 int ret = 0;
3645
3646 if (!btrfs_try_tree_write_lock(eb)) {
Qu Wenruof4340622019-03-20 14:27:41 +08003647 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003648 if (ret < 0)
3649 return ret;
3650 flush = 1;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003651 btrfs_tree_lock(eb);
3652 }
3653
3654 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3655 btrfs_tree_unlock(eb);
3656 if (!epd->sync_io)
3657 return 0;
3658 if (!flush) {
Qu Wenruof4340622019-03-20 14:27:41 +08003659 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003660 if (ret < 0)
3661 return ret;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003662 flush = 1;
3663 }
Chris Masona098d8e82012-03-21 12:09:56 -04003664 while (1) {
3665 wait_on_extent_buffer_writeback(eb);
3666 btrfs_tree_lock(eb);
3667 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3668 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003669 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003670 }
3671 }
3672
Josef Bacik51561ff2012-07-20 16:25:24 -04003673 /*
3674 * We need to do this to prevent races in people who check if the eb is
3675 * under IO since we can end up having no IO bits set for a short period
3676 * of time.
3677 */
3678 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003679 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3680 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003681 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003682 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Nikolay Borisov104b4e52017-06-20 21:01:20 +03003683 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3684 -eb->len,
3685 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003686 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003687 } else {
3688 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003689 }
3690
3691 btrfs_tree_unlock(eb);
3692
3693 if (!ret)
3694 return ret;
3695
David Sterba65ad0102018-06-29 10:56:49 +02003696 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003697 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003698 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003699
3700 if (!trylock_page(p)) {
3701 if (!flush) {
Filipe Manana18dfa712019-09-11 17:42:00 +01003702 int err;
3703
3704 err = flush_write_bio(epd);
3705 if (err < 0) {
3706 ret = err;
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003707 failed_page_nr = i;
3708 goto err_unlock;
3709 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003710 flush = 1;
3711 }
3712 lock_page(p);
3713 }
3714 }
3715
3716 return ret;
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003717err_unlock:
3718 /* Unlock already locked pages */
3719 for (i = 0; i < failed_page_nr; i++)
3720 unlock_page(eb->pages[i]);
Filipe Manana18dfa712019-09-11 17:42:00 +01003721 /*
3722 * Clear EXTENT_BUFFER_WRITEBACK and wake up anyone waiting on it.
3723 * Also set back EXTENT_BUFFER_DIRTY so future attempts to this eb can
3724 * be made and undo everything done before.
3725 */
3726 btrfs_tree_lock(eb);
3727 spin_lock(&eb->refs_lock);
3728 set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
3729 end_extent_buffer_writeback(eb);
3730 spin_unlock(&eb->refs_lock);
3731 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, eb->len,
3732 fs_info->dirty_metadata_batch);
3733 btrfs_clear_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3734 btrfs_tree_unlock(eb);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003735 return ret;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003736}
3737
Filipe Manana656f30d2014-09-26 12:25:56 +01003738static void set_btree_ioerr(struct page *page)
3739{
3740 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Dennis Zhoueb5b64f2019-09-13 14:54:07 +01003741 struct btrfs_fs_info *fs_info;
Filipe Manana656f30d2014-09-26 12:25:56 +01003742
3743 SetPageError(page);
3744 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3745 return;
3746
3747 /*
Dennis Zhoueb5b64f2019-09-13 14:54:07 +01003748 * If we error out, we should add back the dirty_metadata_bytes
3749 * to make it consistent.
3750 */
3751 fs_info = eb->fs_info;
3752 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3753 eb->len, fs_info->dirty_metadata_batch);
3754
3755 /*
Filipe Manana656f30d2014-09-26 12:25:56 +01003756 * If writeback for a btree extent that doesn't belong to a log tree
3757 * failed, increment the counter transaction->eb_write_errors.
3758 * We do this because while the transaction is running and before it's
3759 * committing (when we call filemap_fdata[write|wait]_range against
3760 * the btree inode), we might have
3761 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3762 * returns an error or an error happens during writeback, when we're
3763 * committing the transaction we wouldn't know about it, since the pages
3764 * can be no longer dirty nor marked anymore for writeback (if a
3765 * subsequent modification to the extent buffer didn't happen before the
3766 * transaction commit), which makes filemap_fdata[write|wait]_range not
3767 * able to find the pages tagged with SetPageError at transaction
3768 * commit time. So if this happens we must abort the transaction,
3769 * otherwise we commit a super block with btree roots that point to
3770 * btree nodes/leafs whose content on disk is invalid - either garbage
3771 * or the content of some node/leaf from a past generation that got
3772 * cowed or deleted and is no longer valid.
3773 *
3774 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3775 * not be enough - we need to distinguish between log tree extents vs
3776 * non-log tree extents, and the next filemap_fdatawait_range() call
3777 * will catch and clear such errors in the mapping - and that call might
3778 * be from a log sync and not from a transaction commit. Also, checking
3779 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3780 * not done and would not be reliable - the eb might have been released
3781 * from memory and reading it back again means that flag would not be
3782 * set (since it's a runtime flag, not persisted on disk).
3783 *
3784 * Using the flags below in the btree inode also makes us achieve the
3785 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3786 * writeback for all dirty pages and before filemap_fdatawait_range()
3787 * is called, the writeback for all dirty pages had already finished
3788 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3789 * filemap_fdatawait_range() would return success, as it could not know
3790 * that writeback errors happened (the pages were no longer tagged for
3791 * writeback).
3792 */
3793 switch (eb->log_index) {
3794 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003795 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003796 break;
3797 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003798 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003799 break;
3800 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003801 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003802 break;
3803 default:
3804 BUG(); /* unexpected, logic error */
3805 }
3806}
3807
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003808static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003809{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003810 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003811 struct extent_buffer *eb;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02003812 int done;
Ming Lei6dc4f102019-02-15 19:13:19 +08003813 struct bvec_iter_all iter_all;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003814
David Sterbac09abff2017-07-13 18:10:07 +02003815 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02003816 bio_for_each_segment_all(bvec, bio, iter_all) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003817 struct page *page = bvec->bv_page;
3818
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003819 eb = (struct extent_buffer *)page->private;
3820 BUG_ON(!eb);
3821 done = atomic_dec_and_test(&eb->io_pages);
3822
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003823 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003824 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003825 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003826 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003827 }
3828
3829 end_page_writeback(page);
3830
3831 if (!done)
3832 continue;
3833
3834 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003835 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003836
3837 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003838}
3839
Chris Mason0e378df2014-05-19 20:55:27 -07003840static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003841 struct writeback_control *wbc,
3842 struct extent_page_data *epd)
3843{
David Sterba0ab02062019-03-20 11:27:57 +01003844 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacikf28491e2013-12-16 13:24:27 -05003845 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003846 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003847 u32 nritems;
David Sterbacc5e31a2018-03-01 18:20:27 +01003848 int i, num_pages;
Liu Bo851cd172016-09-23 13:44:44 -07003849 unsigned long start, end;
Liu Boff40adf2017-08-24 18:19:48 -06003850 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003851 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003852
Filipe Manana656f30d2014-09-26 12:25:56 +01003853 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02003854 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003855 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003856
Liu Bo851cd172016-09-23 13:44:44 -07003857 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3858 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003859 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003860 end = btrfs_node_key_ptr_offset(nritems);
3861
David Sterbab159fa22016-11-08 18:09:03 +01003862 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003863 } else {
3864 /*
3865 * leaf:
3866 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3867 */
3868 start = btrfs_item_nr_offset(nritems);
David Sterba8f881e82019-03-20 11:33:10 +01003869 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(eb);
David Sterbab159fa22016-11-08 18:09:03 +01003870 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003871 }
3872
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003873 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003874 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003875
3876 clear_page_dirty_for_io(p);
3877 set_page_writeback(p);
David Sterba4b81ba42017-06-06 19:14:26 +02003878 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterbafa17ed02019-10-03 17:29:05 +02003879 p, offset, PAGE_SIZE, 0,
David Sterbac2df8bb2017-02-10 19:29:38 +01003880 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003881 end_bio_extent_buffer_writepage,
Liu Bo18fdc672017-09-13 12:18:22 -06003882 0, 0, 0, false);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003883 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003884 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003885 if (PageWriteback(p))
3886 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003887 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3888 end_extent_buffer_writeback(eb);
3889 ret = -EIO;
3890 break;
3891 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003892 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003893 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003894 unlock_page(p);
3895 }
3896
3897 if (unlikely(ret)) {
3898 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003899 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003900 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003901 unlock_page(p);
3902 }
3903 }
3904
3905 return ret;
3906}
3907
3908int btree_write_cache_pages(struct address_space *mapping,
3909 struct writeback_control *wbc)
3910{
3911 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003912 struct extent_buffer *eb, *prev_eb = NULL;
3913 struct extent_page_data epd = {
3914 .bio = NULL,
3915 .tree = tree,
3916 .extent_locked = 0,
3917 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3918 };
3919 int ret = 0;
3920 int done = 0;
3921 int nr_to_write_done = 0;
3922 struct pagevec pvec;
3923 int nr_pages;
3924 pgoff_t index;
3925 pgoff_t end; /* Inclusive */
3926 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003927 xa_mark_t tag;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003928
Mel Gorman86679822017-11-15 17:37:52 -08003929 pagevec_init(&pvec);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003930 if (wbc->range_cyclic) {
3931 index = mapping->writeback_index; /* Start from prev offset */
3932 end = -1;
3933 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003934 index = wbc->range_start >> PAGE_SHIFT;
3935 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003936 scanned = 1;
3937 }
3938 if (wbc->sync_mode == WB_SYNC_ALL)
3939 tag = PAGECACHE_TAG_TOWRITE;
3940 else
3941 tag = PAGECACHE_TAG_DIRTY;
3942retry:
3943 if (wbc->sync_mode == WB_SYNC_ALL)
3944 tag_pages_for_writeback(mapping, index, end);
3945 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara4006f432017-11-15 17:34:37 -08003946 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08003947 tag))) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003948 unsigned i;
3949
3950 scanned = 1;
3951 for (i = 0; i < nr_pages; i++) {
3952 struct page *page = pvec.pages[i];
3953
3954 if (!PagePrivate(page))
3955 continue;
3956
Josef Bacikb5bae262012-09-14 13:43:01 -04003957 spin_lock(&mapping->private_lock);
3958 if (!PagePrivate(page)) {
3959 spin_unlock(&mapping->private_lock);
3960 continue;
3961 }
3962
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003963 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003964
3965 /*
3966 * Shouldn't happen and normally this would be a BUG_ON
3967 * but no sense in crashing the users box for something
3968 * we can survive anyway.
3969 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303970 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003971 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003972 continue;
3973 }
3974
Josef Bacikb5bae262012-09-14 13:43:01 -04003975 if (eb == prev_eb) {
3976 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003977 continue;
3978 }
3979
Josef Bacikb5bae262012-09-14 13:43:01 -04003980 ret = atomic_inc_not_zero(&eb->refs);
3981 spin_unlock(&mapping->private_lock);
3982 if (!ret)
3983 continue;
3984
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003985 prev_eb = eb;
David Sterba9df76fb2019-03-20 11:21:41 +01003986 ret = lock_extent_buffer_for_io(eb, &epd);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003987 if (!ret) {
3988 free_extent_buffer(eb);
3989 continue;
Filipe Manana0607eb1d2019-09-11 17:42:28 +01003990 } else if (ret < 0) {
3991 done = 1;
3992 free_extent_buffer(eb);
3993 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003994 }
3995
David Sterba0ab02062019-03-20 11:27:57 +01003996 ret = write_one_eb(eb, wbc, &epd);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003997 if (ret) {
3998 done = 1;
3999 free_extent_buffer(eb);
4000 break;
4001 }
4002 free_extent_buffer(eb);
4003
4004 /*
4005 * the filesystem may choose to bump up nr_to_write.
4006 * We have to make sure to honor the new nr_to_write
4007 * at any time
4008 */
4009 nr_to_write_done = wbc->nr_to_write <= 0;
4010 }
4011 pagevec_release(&pvec);
4012 cond_resched();
4013 }
4014 if (!scanned && !done) {
4015 /*
4016 * We hit the last page and there is more work to be done: wrap
4017 * back to the start of the file
4018 */
4019 scanned = 1;
4020 index = 0;
4021 goto retry;
4022 }
Qu Wenruo2b952ee2019-03-20 14:27:43 +08004023 ASSERT(ret <= 0);
4024 if (ret < 0) {
4025 end_write_bio(&epd, ret);
4026 return ret;
4027 }
4028 ret = flush_write_bio(&epd);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004029 return ret;
4030}
4031
Chris Masond1310b22008-01-24 16:13:08 -05004032/**
Chris Mason4bef0842008-09-08 11:18:08 -04004033 * 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 -05004034 * @mapping: address space structure to write
4035 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
David Sterba935db852017-06-23 04:30:28 +02004036 * @data: data passed to __extent_writepage function
Chris Masond1310b22008-01-24 16:13:08 -05004037 *
4038 * If a page is already under I/O, write_cache_pages() skips it, even
4039 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
4040 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
4041 * and msync() need to guarantee that all the data which was dirty at the time
4042 * the call was made get new I/O started against them. If wbc->sync_mode is
4043 * WB_SYNC_ALL then we were called for data integrity and we must wait for
4044 * existing IO to complete.
4045 */
David Sterba4242b642017-02-10 19:38:24 +01004046static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04004047 struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01004048 struct extent_page_data *epd)
Chris Masond1310b22008-01-24 16:13:08 -05004049{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004050 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05004051 int ret = 0;
4052 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004053 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004054 struct pagevec pvec;
4055 int nr_pages;
4056 pgoff_t index;
4057 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08004058 pgoff_t done_index;
4059 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004060 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05004061 xa_mark_t tag;
Chris Masond1310b22008-01-24 16:13:08 -05004062
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004063 /*
4064 * We have to hold onto the inode so that ordered extents can do their
4065 * work when the IO finishes. The alternative to this is failing to add
4066 * an ordered extent if the igrab() fails there and that is a huge pain
4067 * to deal with, so instead just hold onto the inode throughout the
4068 * writepages operation. If it fails here we are freeing up the inode
4069 * anyway and we'd rather not waste our time writing out stuff that is
4070 * going to be truncated anyway.
4071 */
4072 if (!igrab(inode))
4073 return 0;
4074
Mel Gorman86679822017-11-15 17:37:52 -08004075 pagevec_init(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05004076 if (wbc->range_cyclic) {
4077 index = mapping->writeback_index; /* Start from prev offset */
4078 end = -1;
4079 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004080 index = wbc->range_start >> PAGE_SHIFT;
4081 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08004082 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
4083 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004084 scanned = 1;
4085 }
Ethan Lien3cd24c62018-11-01 14:49:03 +08004086
4087 /*
4088 * We do the tagged writepage as long as the snapshot flush bit is set
4089 * and we are the first one who do the filemap_flush() on this inode.
4090 *
4091 * The nr_to_write == LONG_MAX is needed to make sure other flushers do
4092 * not race in and drop the bit.
4093 */
4094 if (range_whole && wbc->nr_to_write == LONG_MAX &&
4095 test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
4096 &BTRFS_I(inode)->runtime_flags))
4097 wbc->tagged_writepages = 1;
4098
4099 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00004100 tag = PAGECACHE_TAG_TOWRITE;
4101 else
4102 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05004103retry:
Ethan Lien3cd24c62018-11-01 14:49:03 +08004104 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00004105 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08004106 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004107 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara67fd7072017-11-15 17:35:19 -08004108 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
4109 &index, end, tag))) {
Chris Masond1310b22008-01-24 16:13:08 -05004110 unsigned i;
4111
4112 scanned = 1;
4113 for (i = 0; i < nr_pages; i++) {
4114 struct page *page = pvec.pages[i];
4115
Tejun Heof7bddf12019-10-03 07:27:13 -07004116 done_index = page->index + 1;
Chris Masond1310b22008-01-24 16:13:08 -05004117 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07004118 * At this point we hold neither the i_pages lock nor
4119 * the page lock: the page may be truncated or
4120 * invalidated (changing page->mapping to NULL),
4121 * or even swizzled back from swapper_space to
4122 * tmpfs file mapping
Chris Masond1310b22008-01-24 16:13:08 -05004123 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05004124 if (!trylock_page(page)) {
Qu Wenruof4340622019-03-20 14:27:41 +08004125 ret = flush_write_bio(epd);
4126 BUG_ON(ret < 0);
Josef Bacikc8f2f242013-02-11 11:33:00 -05004127 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04004128 }
Chris Masond1310b22008-01-24 16:13:08 -05004129
4130 if (unlikely(page->mapping != mapping)) {
4131 unlock_page(page);
4132 continue;
4133 }
4134
Chris Masond2c3f4f2008-11-19 12:44:22 -05004135 if (wbc->sync_mode != WB_SYNC_NONE) {
Qu Wenruof4340622019-03-20 14:27:41 +08004136 if (PageWriteback(page)) {
4137 ret = flush_write_bio(epd);
4138 BUG_ON(ret < 0);
4139 }
Chris Masond1310b22008-01-24 16:13:08 -05004140 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004141 }
Chris Masond1310b22008-01-24 16:13:08 -05004142
4143 if (PageWriteback(page) ||
4144 !clear_page_dirty_for_io(page)) {
4145 unlock_page(page);
4146 continue;
4147 }
4148
David Sterbaaab6e9e2017-11-30 18:00:02 +01004149 ret = __extent_writepage(page, wbc, epd);
Liu Boa91326672016-03-07 16:56:21 -08004150 if (ret < 0) {
Liu Boa91326672016-03-07 16:56:21 -08004151 done = 1;
4152 break;
4153 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004154
4155 /*
4156 * the filesystem may choose to bump up nr_to_write.
4157 * We have to make sure to honor the new nr_to_write
4158 * at any time
4159 */
4160 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004161 }
4162 pagevec_release(&pvec);
4163 cond_resched();
4164 }
Liu Bo894b36e2016-03-07 16:56:22 -08004165 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004166 /*
4167 * We hit the last page and there is more work to be done: wrap
4168 * back to the start of the file
4169 */
4170 scanned = 1;
4171 index = 0;
4172 goto retry;
4173 }
Liu Boa91326672016-03-07 16:56:21 -08004174
4175 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4176 mapping->writeback_index = done_index;
4177
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004178 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004179 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004180}
Chris Masond1310b22008-01-24 16:13:08 -05004181
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004182int extent_write_full_page(struct page *page, struct writeback_control *wbc)
Chris Masond1310b22008-01-24 16:13:08 -05004183{
4184 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004185 struct extent_page_data epd = {
4186 .bio = NULL,
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004187 .tree = &BTRFS_I(page->mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004188 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004189 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004190 };
Chris Masond1310b22008-01-24 16:13:08 -05004191
Chris Masond1310b22008-01-24 16:13:08 -05004192 ret = __extent_writepage(page, wbc, &epd);
Qu Wenruo30659762019-03-20 14:27:42 +08004193 ASSERT(ret <= 0);
4194 if (ret < 0) {
4195 end_write_bio(&epd, ret);
4196 return ret;
4197 }
Chris Masond1310b22008-01-24 16:13:08 -05004198
Qu Wenruo30659762019-03-20 14:27:42 +08004199 ret = flush_write_bio(&epd);
4200 ASSERT(ret <= 0);
Chris Masond1310b22008-01-24 16:13:08 -05004201 return ret;
4202}
Chris Masond1310b22008-01-24 16:13:08 -05004203
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004204int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
Chris Mason771ed682008-11-06 22:02:51 -05004205 int mode)
4206{
4207 int ret = 0;
4208 struct address_space *mapping = inode->i_mapping;
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004209 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Mason771ed682008-11-06 22:02:51 -05004210 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004211 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4212 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004213
4214 struct extent_page_data epd = {
4215 .bio = NULL,
4216 .tree = tree,
Chris Mason771ed682008-11-06 22:02:51 -05004217 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004218 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05004219 };
4220 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004221 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004222 .nr_to_write = nr_pages * 2,
4223 .range_start = start,
4224 .range_end = end + 1,
Chris Masonec39f762019-07-10 12:28:17 -07004225 /* We're called from an async helper function */
4226 .punt_to_cgroup = 1,
4227 .no_cgroup_owner = 1,
Chris Mason771ed682008-11-06 22:02:51 -05004228 };
4229
Chris Masondbb70be2019-07-10 12:28:18 -07004230 wbc_attach_fdatawrite_inode(&wbc_writepages, inode);
Chris Masond3977122009-01-05 21:25:51 -05004231 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004232 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004233 if (clear_page_dirty_for_io(page))
4234 ret = __extent_writepage(page, &wbc_writepages, &epd);
4235 else {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02004236 btrfs_writepage_endio_finish_ordered(page, start,
Nikolay Borisovc6297322018-11-08 10:18:08 +02004237 start + PAGE_SIZE - 1, 1);
Chris Mason771ed682008-11-06 22:02:51 -05004238 unlock_page(page);
4239 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004240 put_page(page);
4241 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004242 }
4243
Qu Wenruo02c6db42019-03-20 14:27:45 +08004244 ASSERT(ret <= 0);
Chris Masondbb70be2019-07-10 12:28:18 -07004245 if (ret == 0)
4246 ret = flush_write_bio(&epd);
4247 else
Qu Wenruo02c6db42019-03-20 14:27:45 +08004248 end_write_bio(&epd, ret);
Chris Masondbb70be2019-07-10 12:28:18 -07004249
4250 wbc_detach_inode(&wbc_writepages);
Chris Mason771ed682008-11-06 22:02:51 -05004251 return ret;
4252}
Chris Masond1310b22008-01-24 16:13:08 -05004253
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004254int extent_writepages(struct address_space *mapping,
Chris Masond1310b22008-01-24 16:13:08 -05004255 struct writeback_control *wbc)
4256{
4257 int ret = 0;
4258 struct extent_page_data epd = {
4259 .bio = NULL,
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004260 .tree = &BTRFS_I(mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004261 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004262 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004263 };
4264
David Sterba935db852017-06-23 04:30:28 +02004265 ret = extent_write_cache_pages(mapping, wbc, &epd);
Qu Wenruoa2a72fb2019-03-20 14:27:48 +08004266 ASSERT(ret <= 0);
4267 if (ret < 0) {
4268 end_write_bio(&epd, ret);
4269 return ret;
4270 }
4271 ret = flush_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004272 return ret;
4273}
Chris Masond1310b22008-01-24 16:13:08 -05004274
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004275int extent_readpages(struct address_space *mapping, struct list_head *pages,
4276 unsigned nr_pages)
Chris Masond1310b22008-01-24 16:13:08 -05004277{
4278 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004279 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004280 struct page *pagepool[16];
Miao Xie125bac012013-07-25 19:22:37 +08004281 struct extent_map *em_cached = NULL;
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004282 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Liu Bo67c96842012-07-20 21:43:09 -06004283 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004284 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004285
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004286 while (!list_empty(pages)) {
Nikolay Borisove65ef212019-03-11 09:55:38 +02004287 u64 contig_end = 0;
4288
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004289 for (nr = 0; nr < ARRAY_SIZE(pagepool) && !list_empty(pages);) {
Nikolay Borisovf86196e2019-01-03 15:29:02 -08004290 struct page *page = lru_to_page(pages);
Chris Masond1310b22008-01-24 16:13:08 -05004291
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004292 prefetchw(&page->flags);
4293 list_del(&page->lru);
4294 if (add_to_page_cache_lru(page, mapping, page->index,
4295 readahead_gfp_mask(mapping))) {
4296 put_page(page);
Nikolay Borisove65ef212019-03-11 09:55:38 +02004297 break;
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004298 }
4299
4300 pagepool[nr++] = page;
Nikolay Borisove65ef212019-03-11 09:55:38 +02004301 contig_end = page_offset(page) + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004302 }
Liu Bo67c96842012-07-20 21:43:09 -06004303
Nikolay Borisove65ef212019-03-11 09:55:38 +02004304 if (nr) {
4305 u64 contig_start = page_offset(pagepool[0]);
4306
4307 ASSERT(contig_start + nr * PAGE_SIZE - 1 == contig_end);
4308
4309 contiguous_readpages(tree, pagepool, nr, contig_start,
4310 contig_end, &em_cached, &bio, &bio_flags,
4311 &prev_em_start);
4312 }
Chris Masond1310b22008-01-24 16:13:08 -05004313 }
Liu Bo67c96842012-07-20 21:43:09 -06004314
Miao Xie125bac012013-07-25 19:22:37 +08004315 if (em_cached)
4316 free_extent_map(em_cached);
4317
Chris Masond1310b22008-01-24 16:13:08 -05004318 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004319 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004320 return 0;
4321}
Chris Masond1310b22008-01-24 16:13:08 -05004322
4323/*
4324 * basic invalidatepage code, this waits on any locked or writeback
4325 * ranges corresponding to the page, and then deletes any extent state
4326 * records from the tree
4327 */
4328int extent_invalidatepage(struct extent_io_tree *tree,
4329 struct page *page, unsigned long offset)
4330{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004331 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004332 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004333 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004334 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4335
Qu Wenruofda28322013-02-26 08:10:22 +00004336 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004337 if (start > end)
4338 return 0;
4339
David Sterbaff13db42015-12-03 14:30:40 +01004340 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004341 wait_on_page_writeback(page);
Omar Sandovale1821632019-08-15 14:04:04 -07004342 clear_extent_bit(tree, start, end, EXTENT_LOCKED | EXTENT_DELALLOC |
4343 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05004344 return 0;
4345}
Chris Masond1310b22008-01-24 16:13:08 -05004346
4347/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004348 * a helper for releasepage, this tests for areas of the page that
4349 * are locked or under IO and drops the related state bits if it is safe
4350 * to drop the page.
4351 */
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004352static int try_release_extent_state(struct extent_io_tree *tree,
Eric Sandeen48a3b632013-04-25 20:41:01 +00004353 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004354{
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 Mason7b13b7b2008-04-18 10:29:50 -04004357 int ret = 1;
4358
Nikolay Borisov88826792019-03-14 15:28:31 +02004359 if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) {
Chris Mason7b13b7b2008-04-18 10:29:50 -04004360 ret = 0;
Nikolay Borisov88826792019-03-14 15:28:31 +02004361 } else {
Chris Mason11ef1602009-09-23 20:28:46 -04004362 /*
4363 * at this point we can safely clear everything except the
4364 * locked bit and the nodatasum bit
4365 */
David Sterba66b0c882017-10-31 16:30:47 +01004366 ret = __clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004367 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
David Sterba66b0c882017-10-31 16:30:47 +01004368 0, 0, NULL, mask, NULL);
Chris Masone3f24cc2011-02-14 12:52:08 -05004369
4370 /* if clear_extent_bit failed for enomem reasons,
4371 * we can't allow the release to continue.
4372 */
4373 if (ret < 0)
4374 ret = 0;
4375 else
4376 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004377 }
4378 return ret;
4379}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004380
4381/*
Chris Masond1310b22008-01-24 16:13:08 -05004382 * a helper for releasepage. As long as there are no locked extents
4383 * in the range corresponding to the page, both state records and extent
4384 * map records are removed
4385 */
Nikolay Borisov477a30b2018-04-19 10:46:34 +03004386int try_release_extent_mapping(struct page *page, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004387{
4388 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004389 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004390 u64 end = start + PAGE_SIZE - 1;
Filipe Mananabd3599a2018-07-12 01:36:43 +01004391 struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
4392 struct extent_io_tree *tree = &btrfs_inode->io_tree;
4393 struct extent_map_tree *map = &btrfs_inode->extent_tree;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004394
Mel Gormand0164ad2015-11-06 16:28:21 -08004395 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004396 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004397 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004398 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004399 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004400 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004401 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004402 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004403 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004404 break;
4405 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004406 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4407 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004408 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004409 free_extent_map(em);
4410 break;
4411 }
4412 if (!test_range_bit(tree, em->start,
4413 extent_map_end(em) - 1,
Nikolay Borisov4e586ca2019-03-14 15:28:30 +02004414 EXTENT_LOCKED, 0, NULL)) {
Filipe Mananabd3599a2018-07-12 01:36:43 +01004415 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4416 &btrfs_inode->runtime_flags);
Chris Mason70dec802008-01-29 09:59:12 -05004417 remove_extent_mapping(map, em);
4418 /* once for the rb tree */
4419 free_extent_map(em);
4420 }
4421 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004422 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004423
4424 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004425 free_extent_map(em);
4426 }
Chris Masond1310b22008-01-24 16:13:08 -05004427 }
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004428 return try_release_extent_state(tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004429}
Chris Masond1310b22008-01-24 16:13:08 -05004430
Chris Masonec29ed52011-02-23 16:23:20 -05004431/*
4432 * helper function for fiemap, which doesn't want to see any holes.
4433 * This maps until we find something past 'last'
4434 */
4435static struct extent_map *get_extent_skip_holes(struct inode *inode,
David Sterbae3350e12017-06-23 04:09:57 +02004436 u64 offset, u64 last)
Chris Masonec29ed52011-02-23 16:23:20 -05004437{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004438 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004439 struct extent_map *em;
4440 u64 len;
4441
4442 if (offset >= last)
4443 return NULL;
4444
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304445 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004446 len = last - offset;
4447 if (len == 0)
4448 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004449 len = ALIGN(len, sectorsize);
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02004450 em = btrfs_get_extent_fiemap(BTRFS_I(inode), offset, len);
David Sterbac7040052011-04-19 18:00:01 +02004451 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004452 return em;
4453
4454 /* if this isn't a hole return it */
Nikolay Borisov4a2d25c2017-11-23 10:51:43 +02004455 if (em->block_start != EXTENT_MAP_HOLE)
Chris Masonec29ed52011-02-23 16:23:20 -05004456 return em;
Chris Masonec29ed52011-02-23 16:23:20 -05004457
4458 /* this is a hole, advance to the next extent */
4459 offset = extent_map_end(em);
4460 free_extent_map(em);
4461 if (offset >= last)
4462 break;
4463 }
4464 return NULL;
4465}
4466
Qu Wenruo47518322017-04-07 10:43:15 +08004467/*
4468 * To cache previous fiemap extent
4469 *
4470 * Will be used for merging fiemap extent
4471 */
4472struct fiemap_cache {
4473 u64 offset;
4474 u64 phys;
4475 u64 len;
4476 u32 flags;
4477 bool cached;
4478};
4479
4480/*
4481 * Helper to submit fiemap extent.
4482 *
4483 * Will try to merge current fiemap extent specified by @offset, @phys,
4484 * @len and @flags with cached one.
4485 * And only when we fails to merge, cached one will be submitted as
4486 * fiemap extent.
4487 *
4488 * Return value is the same as fiemap_fill_next_extent().
4489 */
4490static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4491 struct fiemap_cache *cache,
4492 u64 offset, u64 phys, u64 len, u32 flags)
4493{
4494 int ret = 0;
4495
4496 if (!cache->cached)
4497 goto assign;
4498
4499 /*
4500 * Sanity check, extent_fiemap() should have ensured that new
Andrea Gelmini52042d82018-11-28 12:05:13 +01004501 * fiemap extent won't overlap with cached one.
Qu Wenruo47518322017-04-07 10:43:15 +08004502 * Not recoverable.
4503 *
4504 * NOTE: Physical address can overlap, due to compression
4505 */
4506 if (cache->offset + cache->len > offset) {
4507 WARN_ON(1);
4508 return -EINVAL;
4509 }
4510
4511 /*
4512 * Only merges fiemap extents if
4513 * 1) Their logical addresses are continuous
4514 *
4515 * 2) Their physical addresses are continuous
4516 * So truly compressed (physical size smaller than logical size)
4517 * extents won't get merged with each other
4518 *
4519 * 3) Share same flags except FIEMAP_EXTENT_LAST
4520 * So regular extent won't get merged with prealloc extent
4521 */
4522 if (cache->offset + cache->len == offset &&
4523 cache->phys + cache->len == phys &&
4524 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4525 (flags & ~FIEMAP_EXTENT_LAST)) {
4526 cache->len += len;
4527 cache->flags |= flags;
4528 goto try_submit_last;
4529 }
4530
4531 /* Not mergeable, need to submit cached one */
4532 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4533 cache->len, cache->flags);
4534 cache->cached = false;
4535 if (ret)
4536 return ret;
4537assign:
4538 cache->cached = true;
4539 cache->offset = offset;
4540 cache->phys = phys;
4541 cache->len = len;
4542 cache->flags = flags;
4543try_submit_last:
4544 if (cache->flags & FIEMAP_EXTENT_LAST) {
4545 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4546 cache->phys, cache->len, cache->flags);
4547 cache->cached = false;
4548 }
4549 return ret;
4550}
4551
4552/*
Qu Wenruo848c23b2017-06-22 10:01:21 +08004553 * Emit last fiemap cache
Qu Wenruo47518322017-04-07 10:43:15 +08004554 *
Qu Wenruo848c23b2017-06-22 10:01:21 +08004555 * The last fiemap cache may still be cached in the following case:
4556 * 0 4k 8k
4557 * |<- Fiemap range ->|
4558 * |<------------ First extent ----------->|
4559 *
4560 * In this case, the first extent range will be cached but not emitted.
4561 * So we must emit it before ending extent_fiemap().
Qu Wenruo47518322017-04-07 10:43:15 +08004562 */
David Sterba5c5aff92019-03-20 11:29:46 +01004563static int emit_last_fiemap_cache(struct fiemap_extent_info *fieinfo,
Qu Wenruo848c23b2017-06-22 10:01:21 +08004564 struct fiemap_cache *cache)
Qu Wenruo47518322017-04-07 10:43:15 +08004565{
4566 int ret;
4567
4568 if (!cache->cached)
4569 return 0;
4570
Qu Wenruo47518322017-04-07 10:43:15 +08004571 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4572 cache->len, cache->flags);
4573 cache->cached = false;
4574 if (ret > 0)
4575 ret = 0;
4576 return ret;
4577}
4578
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004579int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterba2135fb92017-06-23 04:09:57 +02004580 __u64 start, __u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004581{
Josef Bacik975f84f2010-11-23 19:36:57 +00004582 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004583 u64 off = start;
4584 u64 max = start + len;
4585 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004586 u32 found_type;
4587 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004588 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004589 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004590 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004591 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004592 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004593 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004594 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004595 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004596 struct fiemap_cache cache = { 0 };
David Sterba5911c8f2019-05-15 15:31:04 +02004597 struct ulist *roots;
4598 struct ulist *tmp_ulist;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004599 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004600 u64 em_start = 0;
4601 u64 em_len = 0;
4602 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004603
4604 if (len == 0)
4605 return -EINVAL;
4606
Josef Bacik975f84f2010-11-23 19:36:57 +00004607 path = btrfs_alloc_path();
4608 if (!path)
4609 return -ENOMEM;
4610 path->leave_spinning = 1;
4611
David Sterba5911c8f2019-05-15 15:31:04 +02004612 roots = ulist_alloc(GFP_KERNEL);
4613 tmp_ulist = ulist_alloc(GFP_KERNEL);
4614 if (!roots || !tmp_ulist) {
4615 ret = -ENOMEM;
4616 goto out_free_ulist;
4617 }
4618
Jeff Mahoneyda170662016-06-15 09:22:56 -04004619 start = round_down(start, btrfs_inode_sectorsize(inode));
4620 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004621
Chris Masonec29ed52011-02-23 16:23:20 -05004622 /*
4623 * lookup the last file extent. We're not using i_size here
4624 * because there might be preallocation past i_size
4625 */
David Sterbaf85b7372017-01-20 14:54:07 +01004626 ret = btrfs_lookup_file_extent(NULL, root, path,
4627 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004628 if (ret < 0) {
David Sterba5911c8f2019-05-15 15:31:04 +02004629 goto out_free_ulist;
Liu Bo2d324f52016-05-17 17:21:48 -07004630 } else {
4631 WARN_ON(!ret);
4632 if (ret == 1)
4633 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004634 }
Liu Bo2d324f52016-05-17 17:21:48 -07004635
Josef Bacik975f84f2010-11-23 19:36:57 +00004636 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004637 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004638 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004639
Chris Masonec29ed52011-02-23 16:23:20 -05004640 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004641 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004642 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004643 /* have to trust i_size as the end */
4644 last = (u64)-1;
4645 last_for_get_extent = isize;
4646 } else {
4647 /*
4648 * remember the start of the last extent. There are a
4649 * bunch of different factors that go into the length of the
4650 * extent, so its much less complex to remember where it started
4651 */
4652 last = found_key.offset;
4653 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004654 }
Liu Bofe09e162013-09-22 12:54:23 +08004655 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004656
Chris Masonec29ed52011-02-23 16:23:20 -05004657 /*
4658 * we might have some extents allocated but more delalloc past those
4659 * extents. so, we trust isize unless the start of the last extent is
4660 * beyond isize
4661 */
4662 if (last < isize) {
4663 last = (u64)-1;
4664 last_for_get_extent = isize;
4665 }
4666
David Sterbaff13db42015-12-03 14:30:40 +01004667 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004668 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004669
David Sterbae3350e12017-06-23 04:09:57 +02004670 em = get_extent_skip_holes(inode, start, last_for_get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004671 if (!em)
4672 goto out;
4673 if (IS_ERR(em)) {
4674 ret = PTR_ERR(em);
4675 goto out;
4676 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004677
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004678 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004679 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004680
Chris Masonea8efc72011-03-08 11:54:40 -05004681 /* break if the extent we found is outside the range */
4682 if (em->start >= max || extent_map_end(em) < off)
4683 break;
4684
4685 /*
4686 * get_extent may return an extent that starts before our
4687 * requested range. We have to make sure the ranges
4688 * we return to fiemap always move forward and don't
4689 * overlap, so adjust the offsets here
4690 */
4691 em_start = max(em->start, off);
4692
4693 /*
4694 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004695 * for adjusting the disk offset below. Only do this if the
4696 * extent isn't compressed since our in ram offset may be past
4697 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004698 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004699 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4700 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004701 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004702 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004703 flags = 0;
Filipe Mananaf0986312018-06-20 10:02:30 +01004704 if (em->block_start < EXTENT_MAP_LAST_BYTE)
4705 disko = em->block_start + offset_in_extent;
4706 else
4707 disko = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004708
Chris Masonea8efc72011-03-08 11:54:40 -05004709 /*
4710 * bump off for our next call to get_extent
4711 */
4712 off = extent_map_end(em);
4713 if (off >= max)
4714 end = 1;
4715
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004716 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004717 end = 1;
4718 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004719 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004720 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4721 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004722 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004723 flags |= (FIEMAP_EXTENT_DELALLOC |
4724 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004725 } else if (fieinfo->fi_extents_max) {
4726 u64 bytenr = em->block_start -
4727 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004728
Liu Bofe09e162013-09-22 12:54:23 +08004729 /*
4730 * As btrfs supports shared space, this information
4731 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004732 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4733 * then we're just getting a count and we can skip the
4734 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004735 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06004736 ret = btrfs_check_shared(root,
4737 btrfs_ino(BTRFS_I(inode)),
David Sterba5911c8f2019-05-15 15:31:04 +02004738 bytenr, roots, tmp_ulist);
Josef Bacikdc046b12014-09-10 16:20:45 -04004739 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004740 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004741 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004742 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004743 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004744 }
4745 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4746 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004747 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4748 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004749
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004750 free_extent_map(em);
4751 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004752 if ((em_start >= last) || em_len == (u64)-1 ||
4753 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004754 flags |= FIEMAP_EXTENT_LAST;
4755 end = 1;
4756 }
4757
Chris Masonec29ed52011-02-23 16:23:20 -05004758 /* now scan forward to see if this is really the last extent. */
David Sterbae3350e12017-06-23 04:09:57 +02004759 em = get_extent_skip_holes(inode, off, last_for_get_extent);
Chris Masonec29ed52011-02-23 16:23:20 -05004760 if (IS_ERR(em)) {
4761 ret = PTR_ERR(em);
4762 goto out;
4763 }
4764 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004765 flags |= FIEMAP_EXTENT_LAST;
4766 end = 1;
4767 }
Qu Wenruo47518322017-04-07 10:43:15 +08004768 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4769 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004770 if (ret) {
4771 if (ret == 1)
4772 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004773 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004774 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004775 }
4776out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004777 if (!ret)
David Sterba5c5aff92019-03-20 11:29:46 +01004778 ret = emit_last_fiemap_cache(fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004779 free_extent_map(em);
4780out:
Liu Boa52f4cd2013-05-01 16:23:41 +00004781 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004782 &cached_state);
David Sterba5911c8f2019-05-15 15:31:04 +02004783
4784out_free_ulist:
Colin Ian Kinge02d48e2019-07-05 08:26:24 +01004785 btrfs_free_path(path);
David Sterba5911c8f2019-05-15 15:31:04 +02004786 ulist_free(roots);
4787 ulist_free(tmp_ulist);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004788 return ret;
4789}
4790
Chris Mason727011e2010-08-06 13:21:20 -04004791static void __free_extent_buffer(struct extent_buffer *eb)
4792{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004793 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004794 kmem_cache_free(extent_buffer_cache, eb);
4795}
4796
Josef Bacika26e8c92014-03-28 17:07:27 -04004797int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004798{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004799 return (atomic_read(&eb->io_pages) ||
4800 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4801 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004802}
4803
Miao Xie897ca6e92010-10-26 20:57:29 -04004804/*
David Sterba55ac0132018-07-19 17:24:32 +02004805 * Release all pages attached to the extent buffer.
Miao Xie897ca6e92010-10-26 20:57:29 -04004806 */
David Sterba55ac0132018-07-19 17:24:32 +02004807static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004808{
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004809 int i;
4810 int num_pages;
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004811 int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004812
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004813 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004814
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004815 num_pages = num_extent_pages(eb);
4816 for (i = 0; i < num_pages; i++) {
4817 struct page *page = eb->pages[i];
Miao Xie897ca6e92010-10-26 20:57:29 -04004818
Forrest Liu5d2361d2015-02-09 17:31:45 +08004819 if (!page)
4820 continue;
4821 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004822 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004823 /*
4824 * We do this since we'll remove the pages after we've
4825 * removed the eb from the radix tree, so we could race
4826 * and have this page now attached to the new eb. So
4827 * only clear page_private if it's still connected to
4828 * this eb.
4829 */
4830 if (PagePrivate(page) &&
4831 page->private == (unsigned long)eb) {
4832 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4833 BUG_ON(PageDirty(page));
4834 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004835 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004836 * We need to make sure we haven't be attached
4837 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004838 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004839 ClearPagePrivate(page);
4840 set_page_private(page, 0);
4841 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004842 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004843 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004844
4845 if (mapped)
4846 spin_unlock(&page->mapping->private_lock);
4847
Nicholas D Steeves01327612016-05-19 21:18:45 -04004848 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004849 put_page(page);
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004850 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004851}
4852
4853/*
4854 * Helper for releasing the extent buffer.
4855 */
4856static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4857{
David Sterba55ac0132018-07-19 17:24:32 +02004858 btrfs_release_extent_buffer_pages(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004859 __free_extent_buffer(eb);
4860}
4861
Josef Bacikf28491e2013-12-16 13:24:27 -05004862static struct extent_buffer *
4863__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004864 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004865{
4866 struct extent_buffer *eb = NULL;
4867
Michal Hockod1b5c562015-08-19 14:17:40 +02004868 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004869 eb->start = start;
4870 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004871 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004872 eb->bflags = 0;
4873 rwlock_init(&eb->lock);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004874 atomic_set(&eb->blocking_readers, 0);
David Sterba06297d82019-05-02 16:47:23 +02004875 eb->blocking_writers = 0;
David Sterbaed1b4ed2018-08-24 16:31:17 +02004876 eb->lock_nested = false;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004877 init_waitqueue_head(&eb->write_lock_wq);
4878 init_waitqueue_head(&eb->read_lock_wq);
4879
4880 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4881
4882 spin_lock_init(&eb->refs_lock);
4883 atomic_set(&eb->refs, 1);
4884 atomic_set(&eb->io_pages, 0);
4885
4886 /*
4887 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4888 */
4889 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4890 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4891 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4892
David Sterba843ccf92018-08-24 14:56:28 +02004893#ifdef CONFIG_BTRFS_DEBUG
David Sterbaf3dc24c2019-05-02 16:51:53 +02004894 eb->spinning_writers = 0;
David Sterbaafd495a2018-08-24 15:57:38 +02004895 atomic_set(&eb->spinning_readers, 0);
David Sterba5c9c7992018-08-24 16:15:51 +02004896 atomic_set(&eb->read_locks, 0);
David Sterba00801ae2019-05-02 16:53:47 +02004897 eb->write_locks = 0;
David Sterba843ccf92018-08-24 14:56:28 +02004898#endif
4899
Josef Bacikdb7f3432013-08-07 14:54:37 -04004900 return eb;
4901}
4902
4903struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4904{
David Sterbacc5e31a2018-03-01 18:20:27 +01004905 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004906 struct page *p;
4907 struct extent_buffer *new;
David Sterbacc5e31a2018-03-01 18:20:27 +01004908 int num_pages = num_extent_pages(src);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004909
David Sterba3f556f72014-06-15 03:20:26 +02004910 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004911 if (new == NULL)
4912 return NULL;
4913
4914 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004915 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004916 if (!p) {
4917 btrfs_release_extent_buffer(new);
4918 return NULL;
4919 }
4920 attach_extent_buffer_page(new, p);
4921 WARN_ON(PageDirty(p));
4922 SetPageUptodate(p);
4923 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004924 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004925 }
4926
Josef Bacikdb7f3432013-08-07 14:54:37 -04004927 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004928 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004929
4930 return new;
4931}
4932
Omar Sandoval0f331222015-09-29 20:50:31 -07004933struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4934 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004935{
4936 struct extent_buffer *eb;
David Sterbacc5e31a2018-03-01 18:20:27 +01004937 int num_pages;
4938 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004939
David Sterba3f556f72014-06-15 03:20:26 +02004940 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004941 if (!eb)
4942 return NULL;
4943
David Sterba65ad0102018-06-29 10:56:49 +02004944 num_pages = num_extent_pages(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004945 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004946 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004947 if (!eb->pages[i])
4948 goto err;
4949 }
4950 set_extent_buffer_uptodate(eb);
4951 btrfs_set_header_nritems(eb, 0);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004952 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004953
4954 return eb;
4955err:
4956 for (; i > 0; i--)
4957 __free_page(eb->pages[i - 1]);
4958 __free_extent_buffer(eb);
4959 return NULL;
4960}
4961
Omar Sandoval0f331222015-09-29 20:50:31 -07004962struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004963 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004964{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004965 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004966}
4967
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004968static void check_buffer_tree_ref(struct extent_buffer *eb)
4969{
Chris Mason242e18c2013-01-29 17:49:37 -05004970 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004971 /* the ref bit is tricky. We have to make sure it is set
4972 * if we have the buffer dirty. Otherwise the
4973 * code to free a buffer can end up dropping a dirty
4974 * page
4975 *
4976 * Once the ref bit is set, it won't go away while the
4977 * buffer is dirty or in writeback, and it also won't
4978 * go away while we have the reference count on the
4979 * eb bumped.
4980 *
4981 * We can't just set the ref bit without bumping the
4982 * ref on the eb because free_extent_buffer might
4983 * see the ref bit and try to clear it. If this happens
4984 * free_extent_buffer might end up dropping our original
4985 * ref by mistake and freeing the page before we are able
4986 * to add one more ref.
4987 *
4988 * So bump the ref count first, then set the bit. If someone
4989 * beat us to it, drop the ref we added.
4990 */
Chris Mason242e18c2013-01-29 17:49:37 -05004991 refs = atomic_read(&eb->refs);
4992 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4993 return;
4994
Josef Bacik594831c2012-07-20 16:11:08 -04004995 spin_lock(&eb->refs_lock);
4996 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004997 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004998 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004999}
5000
Mel Gorman2457aec2014-06-04 16:10:31 -07005001static void mark_extent_buffer_accessed(struct extent_buffer *eb,
5002 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04005003{
David Sterbacc5e31a2018-03-01 18:20:27 +01005004 int num_pages, i;
Josef Bacik5df42352012-03-15 18:24:42 -04005005
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005006 check_buffer_tree_ref(eb);
5007
David Sterba65ad0102018-06-29 10:56:49 +02005008 num_pages = num_extent_pages(eb);
Josef Bacik5df42352012-03-15 18:24:42 -04005009 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005010 struct page *p = eb->pages[i];
5011
Mel Gorman2457aec2014-06-04 16:10:31 -07005012 if (p != accessed)
5013 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04005014 }
5015}
5016
Josef Bacikf28491e2013-12-16 13:24:27 -05005017struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
5018 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005019{
5020 struct extent_buffer *eb;
5021
5022 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05005023 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005024 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005025 if (eb && atomic_inc_not_zero(&eb->refs)) {
5026 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01005027 /*
5028 * Lock our eb's refs_lock to avoid races with
5029 * free_extent_buffer. When we get our eb it might be flagged
5030 * with EXTENT_BUFFER_STALE and another task running
5031 * free_extent_buffer might have seen that flag set,
5032 * eb->refs == 2, that the buffer isn't under IO (dirty and
5033 * writeback flags not set) and it's still in the tree (flag
5034 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
5035 * of decrementing the extent buffer's reference count twice.
5036 * So here we could race and increment the eb's reference count,
5037 * clear its stale flag, mark it as dirty and drop our reference
5038 * before the other task finishes executing free_extent_buffer,
5039 * which would later result in an attempt to free an extent
5040 * buffer that is dirty.
5041 */
5042 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
5043 spin_lock(&eb->refs_lock);
5044 spin_unlock(&eb->refs_lock);
5045 }
Mel Gorman2457aec2014-06-04 16:10:31 -07005046 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005047 return eb;
5048 }
5049 rcu_read_unlock();
5050
5051 return NULL;
5052}
5053
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005054#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
5055struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005056 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005057{
5058 struct extent_buffer *eb, *exists = NULL;
5059 int ret;
5060
5061 eb = find_extent_buffer(fs_info, start);
5062 if (eb)
5063 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04005064 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005065 if (!eb)
Dan Carpenterb6293c82019-12-03 14:24:58 +03005066 return ERR_PTR(-ENOMEM);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005067 eb->fs_info = fs_info;
5068again:
David Sterbae1860a72016-05-09 14:11:38 +02005069 ret = radix_tree_preload(GFP_NOFS);
Dan Carpenterb6293c82019-12-03 14:24:58 +03005070 if (ret) {
5071 exists = ERR_PTR(ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005072 goto free_eb;
Dan Carpenterb6293c82019-12-03 14:24:58 +03005073 }
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005074 spin_lock(&fs_info->buffer_lock);
5075 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005076 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005077 spin_unlock(&fs_info->buffer_lock);
5078 radix_tree_preload_end();
5079 if (ret == -EEXIST) {
5080 exists = find_extent_buffer(fs_info, start);
5081 if (exists)
5082 goto free_eb;
5083 else
5084 goto again;
5085 }
5086 check_buffer_tree_ref(eb);
5087 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
5088
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005089 return eb;
5090free_eb:
5091 btrfs_release_extent_buffer(eb);
5092 return exists;
5093}
5094#endif
5095
Josef Bacikf28491e2013-12-16 13:24:27 -05005096struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02005097 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05005098{
Jeff Mahoneyda170662016-06-15 09:22:56 -04005099 unsigned long len = fs_info->nodesize;
David Sterbacc5e31a2018-03-01 18:20:27 +01005100 int num_pages;
5101 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005102 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005103 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04005104 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05005105 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05005106 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05005107 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005108 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05005109
Jeff Mahoneyda170662016-06-15 09:22:56 -04005110 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07005111 btrfs_err(fs_info, "bad tree block start %llu", start);
5112 return ERR_PTR(-EINVAL);
5113 }
5114
Josef Bacikf28491e2013-12-16 13:24:27 -05005115 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005116 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04005117 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04005118
David Sterba23d79d82014-06-15 02:55:29 +02005119 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04005120 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07005121 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05005122
David Sterba65ad0102018-06-29 10:56:49 +02005123 num_pages = num_extent_pages(eb);
Chris Mason727011e2010-08-06 13:21:20 -04005124 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02005125 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07005126 if (!p) {
5127 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04005128 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005129 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005130
5131 spin_lock(&mapping->private_lock);
5132 if (PagePrivate(p)) {
5133 /*
5134 * We could have already allocated an eb for this page
5135 * and attached one so lets see if we can get a ref on
5136 * the existing eb, and if we can we know it's good and
5137 * we can just return that one, else we know we can just
5138 * overwrite page->private.
5139 */
5140 exists = (struct extent_buffer *)p->private;
5141 if (atomic_inc_not_zero(&exists->refs)) {
5142 spin_unlock(&mapping->private_lock);
5143 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005144 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07005145 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005146 goto free_eb;
5147 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005148 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005149
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005150 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005151 * Do this so attach doesn't complain and we need to
5152 * drop the ref the old guy had.
5153 */
5154 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005155 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005156 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05005157 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005158 attach_extent_buffer_page(eb, p);
5159 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005160 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04005161 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05005162 if (!PageUptodate(p))
5163 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05005164
5165 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005166 * We can't unlock the pages just yet since the extent buffer
5167 * hasn't been properly inserted in the radix tree, this
5168 * opens a race with btree_releasepage which can free a page
5169 * while we are still filling in all pages for the buffer and
5170 * we could crash.
Chris Masoneb14ab82011-02-10 12:35:00 -05005171 */
Chris Masond1310b22008-01-24 16:13:08 -05005172 }
5173 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05005174 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05005175again:
David Sterbae1860a72016-05-09 14:11:38 +02005176 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07005177 if (ret) {
5178 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04005179 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005180 }
Miao Xie19fe0a82010-10-26 20:57:29 -04005181
Josef Bacikf28491e2013-12-16 13:24:27 -05005182 spin_lock(&fs_info->buffer_lock);
5183 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005184 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05005185 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005186 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04005187 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005188 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005189 if (exists)
5190 goto free_eb;
5191 else
Josef Bacik115391d2012-03-09 09:51:43 -05005192 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04005193 }
Chris Mason6af118ce2008-07-22 11:18:07 -04005194 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005195 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005196 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005197
5198 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005199 * Now it's safe to unlock the pages because any calls to
5200 * btree_releasepage will correctly detect that a page belongs to a
5201 * live buffer and won't free them prematurely.
Chris Masoneb14ab82011-02-10 12:35:00 -05005202 */
Nikolay Borisov28187ae2018-07-04 10:24:51 +03005203 for (i = 0; i < num_pages; i++)
5204 unlock_page(eb->pages[i]);
Chris Masond1310b22008-01-24 16:13:08 -05005205 return eb;
5206
Chris Mason6af118ce2008-07-22 11:18:07 -04005207free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005208 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005209 for (i = 0; i < num_pages; i++) {
5210 if (eb->pages[i])
5211 unlock_page(eb->pages[i]);
5212 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005213
Miao Xie897ca6e92010-10-26 20:57:29 -04005214 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005215 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005216}
Chris Masond1310b22008-01-24 16:13:08 -05005217
Josef Bacik3083ee22012-03-09 16:01:49 -05005218static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5219{
5220 struct extent_buffer *eb =
5221 container_of(head, struct extent_buffer, rcu_head);
5222
5223 __free_extent_buffer(eb);
5224}
5225
David Sterbaf7a52a42013-04-26 14:56:29 +00005226static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005227{
Nikolay Borisov07e21c42018-06-27 16:38:23 +03005228 lockdep_assert_held(&eb->refs_lock);
5229
Josef Bacik3083ee22012-03-09 16:01:49 -05005230 WARN_ON(atomic_read(&eb->refs) == 0);
5231 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005232 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005233 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005234
Jan Schmidt815a51c2012-05-16 17:00:02 +02005235 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005236
Josef Bacikf28491e2013-12-16 13:24:27 -05005237 spin_lock(&fs_info->buffer_lock);
5238 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005239 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005240 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005241 } else {
5242 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005243 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005244
5245 /* Should be safe to release our pages at this point */
David Sterba55ac0132018-07-19 17:24:32 +02005246 btrfs_release_extent_buffer_pages(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005247#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005248 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
Josef Bacikbcb7e442015-03-16 17:38:02 -04005249 __free_extent_buffer(eb);
5250 return 1;
5251 }
5252#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005253 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005254 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005255 }
5256 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005257
5258 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005259}
5260
Chris Masond1310b22008-01-24 16:13:08 -05005261void free_extent_buffer(struct extent_buffer *eb)
5262{
Chris Mason242e18c2013-01-29 17:49:37 -05005263 int refs;
5264 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005265 if (!eb)
5266 return;
5267
Chris Mason242e18c2013-01-29 17:49:37 -05005268 while (1) {
5269 refs = atomic_read(&eb->refs);
Nikolay Borisov46cc7752018-10-15 17:04:01 +03005270 if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
5271 || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
5272 refs == 1))
Chris Mason242e18c2013-01-29 17:49:37 -05005273 break;
5274 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5275 if (old == refs)
5276 return;
5277 }
5278
Josef Bacik3083ee22012-03-09 16:01:49 -05005279 spin_lock(&eb->refs_lock);
5280 if (atomic_read(&eb->refs) == 2 &&
5281 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005282 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005283 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5284 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005285
Josef Bacik3083ee22012-03-09 16:01:49 -05005286 /*
5287 * I know this is terrible, but it's temporary until we stop tracking
5288 * the uptodate bits and such for the extent buffers.
5289 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005290 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005291}
Chris Masond1310b22008-01-24 16:13:08 -05005292
Josef Bacik3083ee22012-03-09 16:01:49 -05005293void free_extent_buffer_stale(struct extent_buffer *eb)
5294{
5295 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005296 return;
5297
Josef Bacik3083ee22012-03-09 16:01:49 -05005298 spin_lock(&eb->refs_lock);
5299 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5300
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005301 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005302 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5303 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005304 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005305}
5306
Chris Mason1d4284b2012-03-28 20:31:37 -04005307void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005308{
David Sterbacc5e31a2018-03-01 18:20:27 +01005309 int i;
5310 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005311 struct page *page;
5312
David Sterba65ad0102018-06-29 10:56:49 +02005313 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005314
5315 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005316 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005317 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005318 continue;
5319
Chris Masona61e6f22008-07-22 11:18:08 -04005320 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005321 WARN_ON(!PagePrivate(page));
5322
Chris Masond1310b22008-01-24 16:13:08 -05005323 clear_page_dirty_for_io(page);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005324 xa_lock_irq(&page->mapping->i_pages);
Matthew Wilcox0a943c62017-12-04 10:37:22 -05005325 if (!PageDirty(page))
5326 __xa_clear_mark(&page->mapping->i_pages,
5327 page_index(page), PAGECACHE_TAG_DIRTY);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005328 xa_unlock_irq(&page->mapping->i_pages);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005329 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005330 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005331 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005332 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005333}
Chris Masond1310b22008-01-24 16:13:08 -05005334
Liu Boabb57ef2018-09-14 01:44:42 +08005335bool set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005336{
David Sterbacc5e31a2018-03-01 18:20:27 +01005337 int i;
5338 int num_pages;
Liu Boabb57ef2018-09-14 01:44:42 +08005339 bool was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005340
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005341 check_buffer_tree_ref(eb);
5342
Chris Masonb9473432009-03-13 11:00:37 -04005343 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005344
David Sterba65ad0102018-06-29 10:56:49 +02005345 num_pages = num_extent_pages(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05005346 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005347 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5348
Liu Boabb57ef2018-09-14 01:44:42 +08005349 if (!was_dirty)
5350 for (i = 0; i < num_pages; i++)
5351 set_page_dirty(eb->pages[i]);
Liu Bo51995c32018-09-14 01:46:08 +08005352
5353#ifdef CONFIG_BTRFS_DEBUG
5354 for (i = 0; i < num_pages; i++)
5355 ASSERT(PageDirty(eb->pages[i]));
5356#endif
5357
Chris Masonb9473432009-03-13 11:00:37 -04005358 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005359}
Chris Masond1310b22008-01-24 16:13:08 -05005360
David Sterba69ba3922015-12-03 13:08:59 +01005361void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005362{
David Sterbacc5e31a2018-03-01 18:20:27 +01005363 int i;
Chris Mason1259ab72008-05-12 13:39:03 -04005364 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005365 int num_pages;
Chris Mason1259ab72008-05-12 13:39:03 -04005366
Chris Masonb4ce94d2009-02-04 09:25:08 -05005367 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005368 num_pages = num_extent_pages(eb);
Chris Mason1259ab72008-05-12 13:39:03 -04005369 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005370 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005371 if (page)
5372 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005373 }
Chris Mason1259ab72008-05-12 13:39:03 -04005374}
5375
David Sterba09c25a82015-12-03 13:08:59 +01005376void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005377{
David Sterbacc5e31a2018-03-01 18:20:27 +01005378 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005379 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005380 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005381
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005382 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005383 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005384 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005385 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005386 SetPageUptodate(page);
5387 }
Chris Masond1310b22008-01-24 16:13:08 -05005388}
Chris Masond1310b22008-01-24 16:13:08 -05005389
Nikolay Borisovc2ccfbc2019-04-10 17:24:40 +03005390int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005391{
David Sterbacc5e31a2018-03-01 18:20:27 +01005392 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005393 struct page *page;
5394 int err;
5395 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005396 int locked_pages = 0;
5397 int all_uptodate = 1;
David Sterbacc5e31a2018-03-01 18:20:27 +01005398 int num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005399 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005400 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005401 unsigned long bio_flags = 0;
Nikolay Borisovc2ccfbc2019-04-10 17:24:40 +03005402 struct extent_io_tree *tree = &BTRFS_I(eb->fs_info->btree_inode)->io_tree;
Chris Masona86c12c2008-02-07 10:50:54 -05005403
Chris Masonb4ce94d2009-02-04 09:25:08 -05005404 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005405 return 0;
5406
David Sterba65ad0102018-06-29 10:56:49 +02005407 num_pages = num_extent_pages(eb);
Josef Bacik8436ea912016-09-02 15:40:03 -04005408 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005409 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005410 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005411 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005412 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005413 } else {
5414 lock_page(page);
5415 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005416 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005417 }
5418 /*
5419 * We need to firstly lock all pages to make sure that
5420 * the uptodate bit of our pages won't be affected by
5421 * clear_extent_buffer_uptodate().
5422 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005423 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005424 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005425 if (!PageUptodate(page)) {
5426 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005427 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005428 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005429 }
Liu Bo2571e732016-08-03 12:33:01 -07005430
Chris Masonce9adaa2008-04-09 16:28:12 -04005431 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005432 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005433 goto unlock_exit;
5434 }
5435
Filipe Manana656f30d2014-09-26 12:25:56 +01005436 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005437 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005438 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005439 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005440 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005441
Chris Masonce9adaa2008-04-09 16:28:12 -04005442 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005443 if (ret) {
5444 atomic_dec(&eb->io_pages);
5445 unlock_page(page);
5446 continue;
5447 }
5448
Chris Masonf1885912008-04-09 16:28:12 -04005449 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005450 err = __extent_read_full_page(tree, page,
David Sterba6af49db2017-06-23 04:09:57 +02005451 btree_get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005452 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005453 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005454 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005455 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005456 /*
5457 * We use &bio in above __extent_read_full_page,
5458 * so we ensure that if it returns error, the
5459 * current page fails to add itself to bio and
5460 * it's been unlocked.
5461 *
5462 * We must dec io_pages by ourselves.
5463 */
5464 atomic_dec(&eb->io_pages);
5465 }
Chris Masond1310b22008-01-24 16:13:08 -05005466 } else {
5467 unlock_page(page);
5468 }
5469 }
5470
Jeff Mahoney355808c2011-10-03 23:23:14 -04005471 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005472 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005473 if (err)
5474 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005475 }
Chris Masona86c12c2008-02-07 10:50:54 -05005476
Arne Jansenbb82ab82011-06-10 14:06:53 +02005477 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005478 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005479
Josef Bacik8436ea912016-09-02 15:40:03 -04005480 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005481 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005482 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005483 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005484 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005485 }
Chris Masond3977122009-01-05 21:25:51 -05005486
Chris Masond1310b22008-01-24 16:13:08 -05005487 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005488
5489unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005490 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005491 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005492 page = eb->pages[locked_pages];
5493 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005494 }
5495 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005496}
Chris Masond1310b22008-01-24 16:13:08 -05005497
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005498void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5499 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005500{
5501 size_t cur;
5502 size_t offset;
5503 struct page *page;
5504 char *kaddr;
5505 char *dst = (char *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005506 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005507 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005508
Liu Bof716abd2017-08-09 11:10:16 -06005509 if (start + len > eb->len) {
5510 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5511 eb->start, eb->len, start, len);
5512 memset(dst, 0, len);
5513 return;
5514 }
Chris Masond1310b22008-01-24 16:13:08 -05005515
Johannes Thumshirn70730172018-12-05 15:23:03 +01005516 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005517
Chris Masond3977122009-01-05 21:25:51 -05005518 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005519 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005520
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005521 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005522 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005523 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005524
5525 dst += cur;
5526 len -= cur;
5527 offset = 0;
5528 i++;
5529 }
5530}
Chris Masond1310b22008-01-24 16:13:08 -05005531
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005532int read_extent_buffer_to_user(const struct extent_buffer *eb,
5533 void __user *dstv,
5534 unsigned long start, unsigned long len)
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005535{
5536 size_t cur;
5537 size_t offset;
5538 struct page *page;
5539 char *kaddr;
5540 char __user *dst = (char __user *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005541 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005542 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005543 int ret = 0;
5544
5545 WARN_ON(start > eb->len);
5546 WARN_ON(start + len > eb->start + eb->len);
5547
Johannes Thumshirn70730172018-12-05 15:23:03 +01005548 offset = offset_in_page(start_offset + start);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005549
5550 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005551 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005552
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005553 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005554 kaddr = page_address(page);
5555 if (copy_to_user(dst, kaddr + offset, cur)) {
5556 ret = -EFAULT;
5557 break;
5558 }
5559
5560 dst += cur;
5561 len -= cur;
5562 offset = 0;
5563 i++;
5564 }
5565
5566 return ret;
5567}
5568
Liu Bo415b35a2016-06-17 19:16:21 -07005569/*
5570 * return 0 if the item is found within a page.
5571 * return 1 if the item spans two pages.
5572 * return -EINVAL otherwise.
5573 */
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005574int map_private_extent_buffer(const struct extent_buffer *eb,
5575 unsigned long start, unsigned long min_len,
5576 char **map, unsigned long *map_start,
5577 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005578{
Johannes Thumshirncc2c39d2018-11-28 09:54:54 +01005579 size_t offset;
Chris Masond1310b22008-01-24 16:13:08 -05005580 char *kaddr;
5581 struct page *p;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005582 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005583 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005584 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005585 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005586
Liu Bof716abd2017-08-09 11:10:16 -06005587 if (start + min_len > eb->len) {
5588 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5589 eb->start, eb->len, start, min_len);
5590 return -EINVAL;
5591 }
5592
Chris Masond1310b22008-01-24 16:13:08 -05005593 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005594 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005595
5596 if (i == 0) {
5597 offset = start_offset;
5598 *map_start = 0;
5599 } else {
5600 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005601 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005602 }
Chris Masond3977122009-01-05 21:25:51 -05005603
David Sterbafb85fc92014-07-31 01:03:53 +02005604 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005605 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005606 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005607 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005608 return 0;
5609}
Chris Masond1310b22008-01-24 16:13:08 -05005610
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005611int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5612 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005613{
5614 size_t cur;
5615 size_t offset;
5616 struct page *page;
5617 char *kaddr;
5618 char *ptr = (char *)ptrv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005619 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005620 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005621 int ret = 0;
5622
5623 WARN_ON(start > eb->len);
5624 WARN_ON(start + len > eb->start + eb->len);
5625
Johannes Thumshirn70730172018-12-05 15:23:03 +01005626 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005627
Chris Masond3977122009-01-05 21:25:51 -05005628 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005629 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005630
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005631 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005632
Chris Masona6591712011-07-19 12:04:14 -04005633 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005634 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005635 if (ret)
5636 break;
5637
5638 ptr += cur;
5639 len -= cur;
5640 offset = 0;
5641 i++;
5642 }
5643 return ret;
5644}
Chris Masond1310b22008-01-24 16:13:08 -05005645
David Sterbaf157bf72016-11-09 17:43:38 +01005646void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5647 const void *srcv)
5648{
5649 char *kaddr;
5650
5651 WARN_ON(!PageUptodate(eb->pages[0]));
5652 kaddr = page_address(eb->pages[0]);
5653 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5654 BTRFS_FSID_SIZE);
5655}
5656
5657void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5658{
5659 char *kaddr;
5660
5661 WARN_ON(!PageUptodate(eb->pages[0]));
5662 kaddr = page_address(eb->pages[0]);
5663 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5664 BTRFS_FSID_SIZE);
5665}
5666
Chris Masond1310b22008-01-24 16:13:08 -05005667void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5668 unsigned long start, unsigned long len)
5669{
5670 size_t cur;
5671 size_t offset;
5672 struct page *page;
5673 char *kaddr;
5674 char *src = (char *)srcv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005675 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005676 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005677
5678 WARN_ON(start > eb->len);
5679 WARN_ON(start + len > eb->start + eb->len);
5680
Johannes Thumshirn70730172018-12-05 15:23:03 +01005681 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005682
Chris Masond3977122009-01-05 21:25:51 -05005683 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005684 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005685 WARN_ON(!PageUptodate(page));
5686
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005687 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005688 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005689 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005690
5691 src += cur;
5692 len -= cur;
5693 offset = 0;
5694 i++;
5695 }
5696}
Chris Masond1310b22008-01-24 16:13:08 -05005697
David Sterbab159fa22016-11-08 18:09:03 +01005698void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5699 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005700{
5701 size_t cur;
5702 size_t offset;
5703 struct page *page;
5704 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005705 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005706 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005707
5708 WARN_ON(start > eb->len);
5709 WARN_ON(start + len > eb->start + eb->len);
5710
Johannes Thumshirn70730172018-12-05 15:23:03 +01005711 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005712
Chris Masond3977122009-01-05 21:25:51 -05005713 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005714 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005715 WARN_ON(!PageUptodate(page));
5716
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005717 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005718 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005719 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005720
5721 len -= cur;
5722 offset = 0;
5723 i++;
5724 }
5725}
Chris Masond1310b22008-01-24 16:13:08 -05005726
David Sterba58e80122016-11-08 18:30:31 +01005727void copy_extent_buffer_full(struct extent_buffer *dst,
5728 struct extent_buffer *src)
5729{
5730 int i;
David Sterbacc5e31a2018-03-01 18:20:27 +01005731 int num_pages;
David Sterba58e80122016-11-08 18:30:31 +01005732
5733 ASSERT(dst->len == src->len);
5734
David Sterba65ad0102018-06-29 10:56:49 +02005735 num_pages = num_extent_pages(dst);
David Sterba58e80122016-11-08 18:30:31 +01005736 for (i = 0; i < num_pages; i++)
5737 copy_page(page_address(dst->pages[i]),
5738 page_address(src->pages[i]));
5739}
5740
Chris Masond1310b22008-01-24 16:13:08 -05005741void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5742 unsigned long dst_offset, unsigned long src_offset,
5743 unsigned long len)
5744{
5745 u64 dst_len = dst->len;
5746 size_t cur;
5747 size_t offset;
5748 struct page *page;
5749 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005750 size_t start_offset = offset_in_page(dst->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005751 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005752
5753 WARN_ON(src->len != dst_len);
5754
Johannes Thumshirn70730172018-12-05 15:23:03 +01005755 offset = offset_in_page(start_offset + dst_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005756
Chris Masond3977122009-01-05 21:25:51 -05005757 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005758 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005759 WARN_ON(!PageUptodate(page));
5760
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005761 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005762
Chris Masona6591712011-07-19 12:04:14 -04005763 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005764 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005765
5766 src_offset += cur;
5767 len -= cur;
5768 offset = 0;
5769 i++;
5770 }
5771}
Chris Masond1310b22008-01-24 16:13:08 -05005772
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005773/*
5774 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5775 * given bit number
5776 * @eb: the extent buffer
5777 * @start: offset of the bitmap item in the extent buffer
5778 * @nr: bit number
5779 * @page_index: return index of the page in the extent buffer that contains the
5780 * given bit number
5781 * @page_offset: return offset into the page given by page_index
5782 *
5783 * This helper hides the ugliness of finding the byte in an extent buffer which
5784 * contains a given bit.
5785 */
5786static inline void eb_bitmap_offset(struct extent_buffer *eb,
5787 unsigned long start, unsigned long nr,
5788 unsigned long *page_index,
5789 size_t *page_offset)
5790{
Johannes Thumshirn70730172018-12-05 15:23:03 +01005791 size_t start_offset = offset_in_page(eb->start);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005792 size_t byte_offset = BIT_BYTE(nr);
5793 size_t offset;
5794
5795 /*
5796 * The byte we want is the offset of the extent buffer + the offset of
5797 * the bitmap item in the extent buffer + the offset of the byte in the
5798 * bitmap item.
5799 */
5800 offset = start_offset + start + byte_offset;
5801
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005802 *page_index = offset >> PAGE_SHIFT;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005803 *page_offset = offset_in_page(offset);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005804}
5805
5806/**
5807 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5808 * @eb: the extent buffer
5809 * @start: offset of the bitmap item in the extent buffer
5810 * @nr: bit number to test
5811 */
5812int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5813 unsigned long nr)
5814{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005815 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005816 struct page *page;
5817 unsigned long i;
5818 size_t offset;
5819
5820 eb_bitmap_offset(eb, start, nr, &i, &offset);
5821 page = eb->pages[i];
5822 WARN_ON(!PageUptodate(page));
5823 kaddr = page_address(page);
5824 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5825}
5826
5827/**
5828 * extent_buffer_bitmap_set - set an area of a bitmap
5829 * @eb: the extent buffer
5830 * @start: offset of the bitmap item in the extent buffer
5831 * @pos: bit number of the first bit
5832 * @len: number of bits to set
5833 */
5834void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5835 unsigned long pos, unsigned long len)
5836{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005837 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005838 struct page *page;
5839 unsigned long i;
5840 size_t offset;
5841 const unsigned int size = pos + len;
5842 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005843 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005844
5845 eb_bitmap_offset(eb, start, pos, &i, &offset);
5846 page = eb->pages[i];
5847 WARN_ON(!PageUptodate(page));
5848 kaddr = page_address(page);
5849
5850 while (len >= bits_to_set) {
5851 kaddr[offset] |= mask_to_set;
5852 len -= bits_to_set;
5853 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005854 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005855 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005856 offset = 0;
5857 page = eb->pages[++i];
5858 WARN_ON(!PageUptodate(page));
5859 kaddr = page_address(page);
5860 }
5861 }
5862 if (len) {
5863 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5864 kaddr[offset] |= mask_to_set;
5865 }
5866}
5867
5868
5869/**
5870 * extent_buffer_bitmap_clear - clear an area of a bitmap
5871 * @eb: the extent buffer
5872 * @start: offset of the bitmap item in the extent buffer
5873 * @pos: bit number of the first bit
5874 * @len: number of bits to clear
5875 */
5876void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5877 unsigned long pos, unsigned long len)
5878{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005879 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005880 struct page *page;
5881 unsigned long i;
5882 size_t offset;
5883 const unsigned int size = pos + len;
5884 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005885 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005886
5887 eb_bitmap_offset(eb, start, pos, &i, &offset);
5888 page = eb->pages[i];
5889 WARN_ON(!PageUptodate(page));
5890 kaddr = page_address(page);
5891
5892 while (len >= bits_to_clear) {
5893 kaddr[offset] &= ~mask_to_clear;
5894 len -= bits_to_clear;
5895 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005896 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005897 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005898 offset = 0;
5899 page = eb->pages[++i];
5900 WARN_ON(!PageUptodate(page));
5901 kaddr = page_address(page);
5902 }
5903 }
5904 if (len) {
5905 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5906 kaddr[offset] &= ~mask_to_clear;
5907 }
5908}
5909
Sergei Trofimovich33872062011-04-11 21:52:52 +00005910static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5911{
5912 unsigned long distance = (src > dst) ? src - dst : dst - src;
5913 return distance < len;
5914}
5915
Chris Masond1310b22008-01-24 16:13:08 -05005916static void copy_pages(struct page *dst_page, struct page *src_page,
5917 unsigned long dst_off, unsigned long src_off,
5918 unsigned long len)
5919{
Chris Masona6591712011-07-19 12:04:14 -04005920 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005921 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005922 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005923
Sergei Trofimovich33872062011-04-11 21:52:52 +00005924 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005925 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005926 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005927 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005928 if (areas_overlap(src_off, dst_off, len))
5929 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005930 }
Chris Masond1310b22008-01-24 16:13:08 -05005931
Chris Mason727011e2010-08-06 13:21:20 -04005932 if (must_memmove)
5933 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5934 else
5935 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005936}
5937
5938void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5939 unsigned long src_offset, unsigned long len)
5940{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005941 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005942 size_t cur;
5943 size_t dst_off_in_page;
5944 size_t src_off_in_page;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005945 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005946 unsigned long dst_i;
5947 unsigned long src_i;
5948
5949 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005950 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005951 "memmove bogus src_offset %lu move len %lu dst len %lu",
5952 src_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01005953 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05005954 }
5955 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005956 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005957 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5958 dst_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01005959 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05005960 }
5961
Chris Masond3977122009-01-05 21:25:51 -05005962 while (len > 0) {
Johannes Thumshirn70730172018-12-05 15:23:03 +01005963 dst_off_in_page = offset_in_page(start_offset + dst_offset);
5964 src_off_in_page = offset_in_page(start_offset + src_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005965
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005966 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5967 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005968
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005969 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005970 src_off_in_page));
5971 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005972 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005973
David Sterbafb85fc92014-07-31 01:03:53 +02005974 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005975 dst_off_in_page, src_off_in_page, cur);
5976
5977 src_offset += cur;
5978 dst_offset += cur;
5979 len -= cur;
5980 }
5981}
Chris Masond1310b22008-01-24 16:13:08 -05005982
5983void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5984 unsigned long src_offset, unsigned long len)
5985{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005986 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005987 size_t cur;
5988 size_t dst_off_in_page;
5989 size_t src_off_in_page;
5990 unsigned long dst_end = dst_offset + len - 1;
5991 unsigned long src_end = src_offset + len - 1;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005992 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005993 unsigned long dst_i;
5994 unsigned long src_i;
5995
5996 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005997 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005998 "memmove bogus src_offset %lu move len %lu len %lu",
5999 src_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01006000 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05006001 }
6002 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006003 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006004 "memmove bogus dst_offset %lu move len %lu len %lu",
6005 dst_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01006006 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05006007 }
Chris Mason727011e2010-08-06 13:21:20 -04006008 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05006009 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
6010 return;
6011 }
Chris Masond3977122009-01-05 21:25:51 -05006012 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006013 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
6014 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05006015
Johannes Thumshirn70730172018-12-05 15:23:03 +01006016 dst_off_in_page = offset_in_page(start_offset + dst_end);
6017 src_off_in_page = offset_in_page(start_offset + src_end);
Chris Masond1310b22008-01-24 16:13:08 -05006018
6019 cur = min_t(unsigned long, len, src_off_in_page + 1);
6020 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02006021 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05006022 dst_off_in_page - cur + 1,
6023 src_off_in_page - cur + 1, cur);
6024
6025 dst_end -= cur;
6026 src_end -= cur;
6027 len -= cur;
6028 }
6029}
Chris Mason6af118ce2008-07-22 11:18:07 -04006030
David Sterbaf7a52a42013-04-26 14:56:29 +00006031int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04006032{
Chris Mason6af118ce2008-07-22 11:18:07 -04006033 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04006034
Miao Xie19fe0a82010-10-26 20:57:29 -04006035 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006036 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05006037 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04006038 */
Josef Bacik3083ee22012-03-09 16:01:49 -05006039 spin_lock(&page->mapping->private_lock);
6040 if (!PagePrivate(page)) {
6041 spin_unlock(&page->mapping->private_lock);
6042 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04006043 }
6044
Josef Bacik3083ee22012-03-09 16:01:49 -05006045 eb = (struct extent_buffer *)page->private;
6046 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04006047
Josef Bacik0b32f4b2012-03-13 09:38:00 -04006048 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05006049 * This is a little awful but should be ok, we need to make sure that
6050 * the eb doesn't disappear out from under us while we're looking at
6051 * this page.
6052 */
6053 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04006054 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05006055 spin_unlock(&eb->refs_lock);
6056 spin_unlock(&page->mapping->private_lock);
6057 return 0;
6058 }
6059 spin_unlock(&page->mapping->private_lock);
6060
Josef Bacik3083ee22012-03-09 16:01:49 -05006061 /*
6062 * If tree ref isn't set then we know the ref on this eb is a real ref,
6063 * so just return, this page will likely be freed soon anyway.
6064 */
6065 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
6066 spin_unlock(&eb->refs_lock);
6067 return 0;
6068 }
Josef Bacik3083ee22012-03-09 16:01:49 -05006069
David Sterbaf7a52a42013-04-26 14:56:29 +00006070 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04006071}