blob: c0f202741e092cfdd126ee001f4dfe93dc322030 [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);
Nikolay Borisov5750c372020-01-27 11:59:26 +02001596 if (!node && !next && !prev) {
1597 /*
1598 * Tree is completely empty, send full range and let
1599 * caller deal with it
1600 */
1601 *start_ret = 0;
1602 *end_ret = -1;
1603 goto out;
1604 } else if (!node && !next) {
1605 /*
1606 * We are past the last allocated chunk, set start at
1607 * the end of the last extent.
1608 */
1609 state = rb_entry(prev, struct extent_state, rb_node);
1610 *start_ret = state->end + 1;
1611 *end_ret = -1;
1612 goto out;
1613 } else if (!node) {
Nikolay Borisov45bfcfc2019-03-27 14:24:17 +02001614 node = next;
Nikolay Borisov45bfcfc2019-03-27 14:24:17 +02001615 }
Nikolay Borisov1eaebb32019-06-03 13:06:02 +03001616 /*
1617 * At this point 'node' either contains 'start' or start is
1618 * before 'node'
1619 */
Nikolay Borisov45bfcfc2019-03-27 14:24:17 +02001620 state = rb_entry(node, struct extent_state, rb_node);
Nikolay Borisov1eaebb32019-06-03 13:06:02 +03001621
1622 if (in_range(start, state->start, state->end - state->start + 1)) {
1623 if (state->state & bits) {
1624 /*
1625 * |--range with bits sets--|
1626 * |
1627 * start
1628 */
1629 start = state->end + 1;
1630 } else {
1631 /*
1632 * 'start' falls within a range that doesn't
1633 * have the bits set, so take its start as
1634 * the beginning of the desired range
1635 *
1636 * |--range with bits cleared----|
1637 * |
1638 * start
1639 */
1640 *start_ret = state->start;
1641 break;
1642 }
Nikolay Borisov45bfcfc2019-03-27 14:24:17 +02001643 } else {
Nikolay Borisov1eaebb32019-06-03 13:06:02 +03001644 /*
1645 * |---prev range---|---hole/unset---|---node range---|
1646 * |
1647 * start
1648 *
1649 * or
1650 *
1651 * |---hole/unset--||--first node--|
1652 * 0 |
1653 * start
1654 */
1655 if (prev) {
1656 state = rb_entry(prev, struct extent_state,
1657 rb_node);
1658 *start_ret = state->end + 1;
1659 } else {
1660 *start_ret = 0;
1661 }
Nikolay Borisov45bfcfc2019-03-27 14:24:17 +02001662 break;
1663 }
1664 }
1665
1666 /*
1667 * Find the longest stretch from start until an entry which has the
1668 * bits set
1669 */
1670 while (1) {
1671 state = rb_entry(node, struct extent_state, rb_node);
1672 if (state->end >= start && !(state->state & bits)) {
1673 *end_ret = state->end;
1674 } else {
1675 *end_ret = state->start - 1;
1676 break;
1677 }
1678
1679 node = rb_next(node);
1680 if (!node)
1681 break;
1682 }
1683out:
1684 spin_unlock(&tree->lock);
1685}
1686
Xiao Guangrong69261c42011-07-14 03:19:45 +00001687/*
Chris Masond352ac62008-09-29 15:18:18 -04001688 * find a contiguous range of bytes in the file marked as delalloc, not
1689 * more than 'max_bytes'. start and end are used to return the range,
1690 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001691 * true is returned if we find something, false if nothing was in the tree
Chris Masond352ac62008-09-29 15:18:18 -04001692 */
Josef Bacik083e75e2019-09-23 10:05:20 -04001693bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
1694 u64 *end, u64 max_bytes,
1695 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001696{
1697 struct rb_node *node;
1698 struct extent_state *state;
1699 u64 cur_start = *start;
Lu Fengqi3522e902018-11-29 11:33:38 +08001700 bool found = false;
Chris Masond1310b22008-01-24 16:13:08 -05001701 u64 total_bytes = 0;
1702
Chris Masoncad321a2008-12-17 14:51:42 -05001703 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001704
Chris Masond1310b22008-01-24 16:13:08 -05001705 /*
1706 * this search will find all the extents that end after
1707 * our range starts.
1708 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001709 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001710 if (!node) {
Lu Fengqi3522e902018-11-29 11:33:38 +08001711 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001712 goto out;
1713 }
1714
Chris Masond3977122009-01-05 21:25:51 -05001715 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001716 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001717 if (found && (state->start != cur_start ||
1718 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001719 goto out;
1720 }
1721 if (!(state->state & EXTENT_DELALLOC)) {
1722 if (!found)
1723 *end = state->end;
1724 goto out;
1725 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001726 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001727 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001728 *cached_state = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +02001729 refcount_inc(&state->refs);
Josef Bacikc2a128d2010-02-02 21:19:11 +00001730 }
Lu Fengqi3522e902018-11-29 11:33:38 +08001731 found = true;
Chris Masond1310b22008-01-24 16:13:08 -05001732 *end = state->end;
1733 cur_start = state->end + 1;
1734 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001735 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001736 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001737 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001738 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001739 break;
1740 }
1741out:
Chris Masoncad321a2008-12-17 14:51:42 -05001742 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001743 return found;
1744}
1745
Liu Boda2c7002017-02-10 16:41:05 +01001746static int __process_pages_contig(struct address_space *mapping,
1747 struct page *locked_page,
1748 pgoff_t start_index, pgoff_t end_index,
1749 unsigned long page_ops, pgoff_t *index_ret);
1750
Jeff Mahoney143bede2012-03-01 14:56:26 +01001751static noinline void __unlock_for_delalloc(struct inode *inode,
1752 struct page *locked_page,
1753 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001754{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001755 unsigned long index = start >> PAGE_SHIFT;
1756 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001757
Liu Bo76c00212017-02-10 16:42:14 +01001758 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001759 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001760 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001761
Liu Bo76c00212017-02-10 16:42:14 +01001762 __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1763 PAGE_UNLOCK, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -04001764}
1765
1766static noinline int lock_delalloc_pages(struct inode *inode,
1767 struct page *locked_page,
1768 u64 delalloc_start,
1769 u64 delalloc_end)
1770{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001771 unsigned long index = delalloc_start >> PAGE_SHIFT;
Liu Bo76c00212017-02-10 16:42:14 +01001772 unsigned long index_ret = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001773 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001774 int ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001775
Liu Bo76c00212017-02-10 16:42:14 +01001776 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001777 if (index == locked_page->index && index == end_index)
1778 return 0;
1779
Liu Bo76c00212017-02-10 16:42:14 +01001780 ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1781 end_index, PAGE_LOCK, &index_ret);
1782 if (ret == -EAGAIN)
1783 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1784 (u64)index_ret << PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001785 return ret;
1786}
1787
1788/*
Lu Fengqi3522e902018-11-29 11:33:38 +08001789 * Find and lock a contiguous range of bytes in the file marked as delalloc, no
1790 * more than @max_bytes. @Start and @end are used to return the range,
Chris Masonc8b97812008-10-29 14:49:59 -04001791 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001792 * Return: true if we find something
1793 * false if nothing was in the tree
Chris Masonc8b97812008-10-29 14:49:59 -04001794 */
Johannes Thumshirnce9f9672018-11-19 10:38:17 +01001795EXPORT_FOR_TESTS
Lu Fengqi3522e902018-11-29 11:33:38 +08001796noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
Josef Bacik294e30f2013-10-09 12:00:56 -04001797 struct page *locked_page, u64 *start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03001798 u64 *end)
Chris Masonc8b97812008-10-29 14:49:59 -04001799{
Goldwyn Rodrigues99780592019-06-21 10:02:54 -05001800 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Nikolay Borisov917aace2018-10-26 14:43:20 +03001801 u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001802 u64 delalloc_start;
1803 u64 delalloc_end;
Lu Fengqi3522e902018-11-29 11:33:38 +08001804 bool found;
Chris Mason9655d292009-09-02 15:22:30 -04001805 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001806 int ret;
1807 int loops = 0;
1808
1809again:
1810 /* step one, find a bunch of delalloc bytes starting at start */
1811 delalloc_start = *start;
1812 delalloc_end = 0;
Josef Bacik083e75e2019-09-23 10:05:20 -04001813 found = btrfs_find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1814 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001815 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001816 *start = delalloc_start;
1817 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001818 free_extent_state(cached_state);
Lu Fengqi3522e902018-11-29 11:33:38 +08001819 return false;
Chris Masonc8b97812008-10-29 14:49:59 -04001820 }
1821
1822 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001823 * start comes from the offset of locked_page. We have to lock
1824 * pages in order, so we can't process delalloc bytes before
1825 * locked_page
1826 */
Chris Masond3977122009-01-05 21:25:51 -05001827 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001828 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001829
1830 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001831 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001832 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001833 if (delalloc_end + 1 - delalloc_start > max_bytes)
1834 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001835
Chris Masonc8b97812008-10-29 14:49:59 -04001836 /* step two, lock all the pages after the page that has start */
1837 ret = lock_delalloc_pages(inode, locked_page,
1838 delalloc_start, delalloc_end);
Nikolay Borisov9bfd61d2018-10-26 14:43:21 +03001839 ASSERT(!ret || ret == -EAGAIN);
Chris Masonc8b97812008-10-29 14:49:59 -04001840 if (ret == -EAGAIN) {
1841 /* some of the pages are gone, lets avoid looping by
1842 * shortening the size of the delalloc range we're searching
1843 */
Chris Mason9655d292009-09-02 15:22:30 -04001844 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001845 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001846 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001847 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001848 loops = 1;
1849 goto again;
1850 } else {
Lu Fengqi3522e902018-11-29 11:33:38 +08001851 found = false;
Chris Masonc8b97812008-10-29 14:49:59 -04001852 goto out_failed;
1853 }
1854 }
Chris Masonc8b97812008-10-29 14:49:59 -04001855
1856 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001857 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001858
1859 /* then test to make sure it is all still delalloc */
1860 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001861 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001862 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001863 unlock_extent_cached(tree, delalloc_start, delalloc_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001864 &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001865 __unlock_for_delalloc(inode, locked_page,
1866 delalloc_start, delalloc_end);
1867 cond_resched();
1868 goto again;
1869 }
Chris Mason9655d292009-09-02 15:22:30 -04001870 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001871 *start = delalloc_start;
1872 *end = delalloc_end;
1873out_failed:
1874 return found;
1875}
1876
Liu Boda2c7002017-02-10 16:41:05 +01001877static int __process_pages_contig(struct address_space *mapping,
1878 struct page *locked_page,
1879 pgoff_t start_index, pgoff_t end_index,
1880 unsigned long page_ops, pgoff_t *index_ret)
Chris Masonc8b97812008-10-29 14:49:59 -04001881{
Liu Bo873695b2017-02-02 17:49:22 -08001882 unsigned long nr_pages = end_index - start_index + 1;
Liu Boda2c7002017-02-10 16:41:05 +01001883 unsigned long pages_locked = 0;
Liu Bo873695b2017-02-02 17:49:22 -08001884 pgoff_t index = start_index;
Chris Masonc8b97812008-10-29 14:49:59 -04001885 struct page *pages[16];
Liu Bo873695b2017-02-02 17:49:22 -08001886 unsigned ret;
Liu Boda2c7002017-02-10 16:41:05 +01001887 int err = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001888 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001889
Liu Boda2c7002017-02-10 16:41:05 +01001890 if (page_ops & PAGE_LOCK) {
1891 ASSERT(page_ops == PAGE_LOCK);
1892 ASSERT(index_ret && *index_ret == start_index);
1893 }
1894
Filipe Manana704de492014-10-06 22:14:22 +01001895 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
Liu Bo873695b2017-02-02 17:49:22 -08001896 mapping_set_error(mapping, -EIO);
Filipe Manana704de492014-10-06 22:14:22 +01001897
Chris Masond3977122009-01-05 21:25:51 -05001898 while (nr_pages > 0) {
Liu Bo873695b2017-02-02 17:49:22 -08001899 ret = find_get_pages_contig(mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001900 min_t(unsigned long,
1901 nr_pages, ARRAY_SIZE(pages)), pages);
Liu Boda2c7002017-02-10 16:41:05 +01001902 if (ret == 0) {
1903 /*
1904 * Only if we're going to lock these pages,
1905 * can we find nothing at @index.
1906 */
1907 ASSERT(page_ops & PAGE_LOCK);
Liu Bo49d4a332017-03-06 18:20:56 -08001908 err = -EAGAIN;
1909 goto out;
Liu Boda2c7002017-02-10 16:41:05 +01001910 }
Chris Mason8b62b722009-09-02 16:53:46 -04001911
Liu Boda2c7002017-02-10 16:41:05 +01001912 for (i = 0; i < ret; i++) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001913 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001914 SetPagePrivate2(pages[i]);
1915
Chris Mason1d53c9e2019-07-10 12:28:16 -07001916 if (locked_page && pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001917 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001918 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001919 continue;
1920 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001921 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001922 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001923 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001924 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001925 if (page_ops & PAGE_SET_ERROR)
1926 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001927 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001928 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001929 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001930 unlock_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001931 if (page_ops & PAGE_LOCK) {
1932 lock_page(pages[i]);
1933 if (!PageDirty(pages[i]) ||
1934 pages[i]->mapping != mapping) {
1935 unlock_page(pages[i]);
1936 put_page(pages[i]);
1937 err = -EAGAIN;
1938 goto out;
1939 }
1940 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001941 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001942 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001943 }
1944 nr_pages -= ret;
1945 index += ret;
1946 cond_resched();
1947 }
Liu Boda2c7002017-02-10 16:41:05 +01001948out:
1949 if (err && index_ret)
1950 *index_ret = start_index + pages_locked - 1;
1951 return err;
Chris Masonc8b97812008-10-29 14:49:59 -04001952}
Chris Masonc8b97812008-10-29 14:49:59 -04001953
Liu Bo873695b2017-02-02 17:49:22 -08001954void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001955 struct page *locked_page,
1956 unsigned clear_bits,
1957 unsigned long page_ops)
Liu Bo873695b2017-02-02 17:49:22 -08001958{
1959 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001960 NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001961
1962 __process_pages_contig(inode->i_mapping, locked_page,
1963 start >> PAGE_SHIFT, end >> PAGE_SHIFT,
Liu Boda2c7002017-02-10 16:41:05 +01001964 page_ops, NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001965}
1966
Chris Masond352ac62008-09-29 15:18:18 -04001967/*
1968 * count the number of bytes in the tree that have a given bit(s)
1969 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1970 * cached. The total number found is returned.
1971 */
Chris Masond1310b22008-01-24 16:13:08 -05001972u64 count_range_bits(struct extent_io_tree *tree,
1973 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001974 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001975{
1976 struct rb_node *node;
1977 struct extent_state *state;
1978 u64 cur_start = *start;
1979 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001980 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001981 int found = 0;
1982
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301983 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001984 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001985
Chris Masoncad321a2008-12-17 14:51:42 -05001986 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001987 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1988 total_bytes = tree->dirty_bytes;
1989 goto out;
1990 }
1991 /*
1992 * this search will find all the extents that end after
1993 * our range starts.
1994 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001995 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001996 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001997 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001998
Chris Masond3977122009-01-05 21:25:51 -05001999 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05002000 state = rb_entry(node, struct extent_state, rb_node);
2001 if (state->start > search_end)
2002 break;
Chris Masonec29ed52011-02-23 16:23:20 -05002003 if (contig && found && state->start > last + 1)
2004 break;
2005 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05002006 total_bytes += min(search_end, state->end) + 1 -
2007 max(cur_start, state->start);
2008 if (total_bytes >= max_bytes)
2009 break;
2010 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04002011 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05002012 found = 1;
2013 }
Chris Masonec29ed52011-02-23 16:23:20 -05002014 last = state->end;
2015 } else if (contig && found) {
2016 break;
Chris Masond1310b22008-01-24 16:13:08 -05002017 }
2018 node = rb_next(node);
2019 if (!node)
2020 break;
2021 }
2022out:
Chris Masoncad321a2008-12-17 14:51:42 -05002023 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002024 return total_bytes;
2025}
Christoph Hellwigb2950862008-12-02 09:54:17 -05002026
Chris Masond352ac62008-09-29 15:18:18 -04002027/*
2028 * set the private field for a given byte offset in the tree. If there isn't
2029 * an extent_state there already, this does nothing.
2030 */
Josef Bacikb3f167a2019-09-23 10:05:21 -04002031int set_state_failrec(struct extent_io_tree *tree, u64 start,
2032 struct io_failure_record *failrec)
Chris Masond1310b22008-01-24 16:13:08 -05002033{
2034 struct rb_node *node;
2035 struct extent_state *state;
2036 int ret = 0;
2037
Chris Masoncad321a2008-12-17 14:51:42 -05002038 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002039 /*
2040 * this search will find all the extents that end after
2041 * our range starts.
2042 */
Chris Mason80ea96b2008-02-01 14:51:59 -05002043 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04002044 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05002045 ret = -ENOENT;
2046 goto out;
2047 }
2048 state = rb_entry(node, struct extent_state, rb_node);
2049 if (state->start != start) {
2050 ret = -ENOENT;
2051 goto out;
2052 }
David Sterba47dc1962016-02-11 13:24:13 +01002053 state->failrec = failrec;
Chris Masond1310b22008-01-24 16:13:08 -05002054out:
Chris Masoncad321a2008-12-17 14:51:42 -05002055 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002056 return ret;
2057}
2058
Josef Bacikb3f167a2019-09-23 10:05:21 -04002059int get_state_failrec(struct extent_io_tree *tree, u64 start,
2060 struct io_failure_record **failrec)
Chris Masond1310b22008-01-24 16:13:08 -05002061{
2062 struct rb_node *node;
2063 struct extent_state *state;
2064 int ret = 0;
2065
Chris Masoncad321a2008-12-17 14:51:42 -05002066 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002067 /*
2068 * this search will find all the extents that end after
2069 * our range starts.
2070 */
Chris Mason80ea96b2008-02-01 14:51:59 -05002071 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04002072 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05002073 ret = -ENOENT;
2074 goto out;
2075 }
2076 state = rb_entry(node, struct extent_state, rb_node);
2077 if (state->start != start) {
2078 ret = -ENOENT;
2079 goto out;
2080 }
David Sterba47dc1962016-02-11 13:24:13 +01002081 *failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05002082out:
Chris Masoncad321a2008-12-17 14:51:42 -05002083 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002084 return ret;
2085}
2086
2087/*
2088 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05002089 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05002090 * has the bits set. Otherwise, 1 is returned if any bit in the
2091 * range is found set.
2092 */
2093int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01002094 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05002095{
2096 struct extent_state *state = NULL;
2097 struct rb_node *node;
2098 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002099
Chris Masoncad321a2008-12-17 14:51:42 -05002100 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01002101 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04002102 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04002103 node = &cached->rb_node;
2104 else
2105 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05002106 while (node && start <= end) {
2107 state = rb_entry(node, struct extent_state, rb_node);
2108
2109 if (filled && state->start > start) {
2110 bitset = 0;
2111 break;
2112 }
2113
2114 if (state->start > end)
2115 break;
2116
2117 if (state->state & bits) {
2118 bitset = 1;
2119 if (!filled)
2120 break;
2121 } else if (filled) {
2122 bitset = 0;
2123 break;
2124 }
Chris Mason46562ce2009-09-23 20:23:16 -04002125
2126 if (state->end == (u64)-1)
2127 break;
2128
Chris Masond1310b22008-01-24 16:13:08 -05002129 start = state->end + 1;
2130 if (start > end)
2131 break;
2132 node = rb_next(node);
2133 if (!node) {
2134 if (filled)
2135 bitset = 0;
2136 break;
2137 }
2138 }
Chris Masoncad321a2008-12-17 14:51:42 -05002139 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002140 return bitset;
2141}
Chris Masond1310b22008-01-24 16:13:08 -05002142
2143/*
2144 * helper function to set a given page up to date if all the
2145 * extents in the tree for that page are up to date
2146 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002147static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05002148{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002149 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002150 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04002151 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05002152 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002153}
2154
Josef Bacik7870d082017-05-05 11:57:15 -04002155int free_io_failure(struct extent_io_tree *failure_tree,
2156 struct extent_io_tree *io_tree,
2157 struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002158{
2159 int ret;
2160 int err = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002161
David Sterba47dc1962016-02-11 13:24:13 +01002162 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002163 ret = clear_extent_bits(failure_tree, rec->start,
2164 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02002165 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002166 if (ret)
2167 err = ret;
2168
Josef Bacik7870d082017-05-05 11:57:15 -04002169 ret = clear_extent_bits(io_tree, rec->start,
David Woodhouse53b381b2013-01-29 18:40:14 -05002170 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02002171 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05002172 if (ret && !err)
2173 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002174
2175 kfree(rec);
2176 return err;
2177}
2178
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002179/*
2180 * this bypasses the standard btrfs submit functions deliberately, as
2181 * the standard behavior is to write all copies in a raid setup. here we only
2182 * want to write the one bad copy. so we do the mapping for ourselves and issue
2183 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002184 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002185 * actually prevents the read that triggered the error from finishing.
2186 * currently, there can be no more than two copies of every data bit. thus,
2187 * exactly one rewrite is required.
2188 */
Josef Bacik6ec656b2017-05-05 11:57:14 -04002189int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
2190 u64 length, u64 logical, struct page *page,
2191 unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002192{
2193 struct bio *bio;
2194 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002195 u64 map_length = 0;
2196 u64 sector;
2197 struct btrfs_bio *bbio = NULL;
2198 int ret;
2199
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002200 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002201 BUG_ON(!mirror_num);
2202
David Sterbac5e4c3d2017-06-12 17:29:41 +02002203 bio = btrfs_io_bio_alloc(1);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002204 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002205 map_length = length;
2206
Filipe Mananab5de8d02016-05-27 22:21:27 +01002207 /*
2208 * Avoid races with device replace and make sure our bbio has devices
2209 * associated to its stripes that don't go away while we are doing the
2210 * read repair operation.
2211 */
2212 btrfs_bio_counter_inc_blocked(fs_info);
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03002213 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
Liu Boc7253282017-03-29 10:53:58 -07002214 /*
2215 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
2216 * to update all raid stripes, but here we just want to correct
2217 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2218 * stripe's dev and sector.
2219 */
2220 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2221 &map_length, &bbio, 0);
2222 if (ret) {
2223 btrfs_bio_counter_dec(fs_info);
2224 bio_put(bio);
2225 return -EIO;
2226 }
2227 ASSERT(bbio->mirror_num == 1);
2228 } else {
2229 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2230 &map_length, &bbio, mirror_num);
2231 if (ret) {
2232 btrfs_bio_counter_dec(fs_info);
2233 bio_put(bio);
2234 return -EIO;
2235 }
2236 BUG_ON(mirror_num != bbio->mirror_num);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002237 }
Liu Boc7253282017-03-29 10:53:58 -07002238
2239 sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002240 bio->bi_iter.bi_sector = sector;
Liu Boc7253282017-03-29 10:53:58 -07002241 dev = bbio->stripes[bbio->mirror_num - 1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002242 btrfs_put_bbio(bbio);
Anand Jainebbede42017-12-04 12:54:52 +08002243 if (!dev || !dev->bdev ||
2244 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002245 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002246 bio_put(bio);
2247 return -EIO;
2248 }
Christoph Hellwig74d46992017-08-23 19:10:32 +02002249 bio_set_dev(bio, dev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002250 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Miao Xieffdd2012014-09-12 18:44:00 +08002251 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002252
Mike Christie4e49ea42016-06-05 14:31:41 -05002253 if (btrfsic_submit_bio_wait(bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002254 /* try to remap that extent elsewhere? */
Filipe Mananab5de8d02016-05-27 22:21:27 +01002255 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002256 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002257 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002258 return -EIO;
2259 }
2260
David Sterbab14af3b2015-10-08 10:43:10 +02002261 btrfs_info_rl_in_rcu(fs_info,
2262 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Josef Bacik6ec656b2017-05-05 11:57:14 -04002263 ino, start,
Miao Xie1203b682014-09-12 18:44:01 +08002264 rcu_str_deref(dev->name), sector);
Filipe Mananab5de8d02016-05-27 22:21:27 +01002265 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002266 bio_put(bio);
2267 return 0;
2268}
2269
David Sterba20a1fbf92019-03-20 11:23:44 +01002270int btrfs_repair_eb_io_failure(struct extent_buffer *eb, int mirror_num)
Josef Bacikea466792012-03-26 21:57:36 -04002271{
David Sterba20a1fbf92019-03-20 11:23:44 +01002272 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacikea466792012-03-26 21:57:36 -04002273 u64 start = eb->start;
David Sterbacc5e31a2018-03-01 18:20:27 +01002274 int i, num_pages = num_extent_pages(eb);
Chris Masond95603b2012-04-12 15:55:15 -04002275 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002276
David Howellsbc98a422017-07-17 08:45:34 +01002277 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002278 return -EROFS;
2279
Josef Bacikea466792012-03-26 21:57:36 -04002280 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002281 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002282
Josef Bacik6ec656b2017-05-05 11:57:14 -04002283 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002284 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002285 if (ret)
2286 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002287 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002288 }
2289
2290 return ret;
2291}
2292
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002293/*
2294 * each time an IO finishes, we do a fast check in the IO failure tree
2295 * to see if we need to process or clean up an io_failure_record
2296 */
Josef Bacik7870d082017-05-05 11:57:15 -04002297int clean_io_failure(struct btrfs_fs_info *fs_info,
2298 struct extent_io_tree *failure_tree,
2299 struct extent_io_tree *io_tree, u64 start,
2300 struct page *page, u64 ino, unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002301{
2302 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002303 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002304 struct extent_state *state;
2305 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002306 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002307
2308 private = 0;
Josef Bacik7870d082017-05-05 11:57:15 -04002309 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2310 EXTENT_DIRTY, 0);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002311 if (!ret)
2312 return 0;
2313
Josef Bacik7870d082017-05-05 11:57:15 -04002314 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002315 if (ret)
2316 return 0;
2317
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002318 BUG_ON(!failrec->this_mirror);
2319
2320 if (failrec->in_validation) {
2321 /* there was no real error, just free the record */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002322 btrfs_debug(fs_info,
2323 "clean_io_failure: freeing dummy error at %llu",
2324 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002325 goto out;
2326 }
David Howellsbc98a422017-07-17 08:45:34 +01002327 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002328 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002329
Josef Bacik7870d082017-05-05 11:57:15 -04002330 spin_lock(&io_tree->lock);
2331 state = find_first_extent_bit_state(io_tree,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002332 failrec->start,
2333 EXTENT_LOCKED);
Josef Bacik7870d082017-05-05 11:57:15 -04002334 spin_unlock(&io_tree->lock);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002335
Miao Xie883d0de2013-07-25 19:22:35 +08002336 if (state && state->start <= failrec->start &&
2337 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002338 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2339 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002340 if (num_copies > 1) {
Josef Bacik7870d082017-05-05 11:57:15 -04002341 repair_io_failure(fs_info, ino, start, failrec->len,
2342 failrec->logical, page, pg_offset,
2343 failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002344 }
2345 }
2346
2347out:
Josef Bacik7870d082017-05-05 11:57:15 -04002348 free_io_failure(failure_tree, io_tree, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002349
Miao Xie454ff3d2014-09-12 18:43:58 +08002350 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002351}
2352
Miao Xief6124962014-09-12 18:44:04 +08002353/*
2354 * Can be called when
2355 * - hold extent lock
2356 * - under ordered extent
2357 * - the inode is freeing
2358 */
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002359void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
Miao Xief6124962014-09-12 18:44:04 +08002360{
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002361 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
Miao Xief6124962014-09-12 18:44:04 +08002362 struct io_failure_record *failrec;
2363 struct extent_state *state, *next;
2364
2365 if (RB_EMPTY_ROOT(&failure_tree->state))
2366 return;
2367
2368 spin_lock(&failure_tree->lock);
2369 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2370 while (state) {
2371 if (state->start > end)
2372 break;
2373
2374 ASSERT(state->end <= end);
2375
2376 next = next_state(state);
2377
David Sterba47dc1962016-02-11 13:24:13 +01002378 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002379 free_extent_state(state);
2380 kfree(failrec);
2381
2382 state = next;
2383 }
2384 spin_unlock(&failure_tree->lock);
2385}
2386
Miao Xie2fe63032014-09-12 18:43:59 +08002387int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
David Sterba47dc1962016-02-11 13:24:13 +01002388 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002389{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002390 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002391 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002392 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002393 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2394 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2395 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002396 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002397 u64 logical;
2398
David Sterba47dc1962016-02-11 13:24:13 +01002399 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002400 if (ret) {
2401 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2402 if (!failrec)
2403 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002404
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002405 failrec->start = start;
2406 failrec->len = end - start + 1;
2407 failrec->this_mirror = 0;
2408 failrec->bio_flags = 0;
2409 failrec->in_validation = 0;
2410
2411 read_lock(&em_tree->lock);
2412 em = lookup_extent_mapping(em_tree, start, failrec->len);
2413 if (!em) {
2414 read_unlock(&em_tree->lock);
2415 kfree(failrec);
2416 return -EIO;
2417 }
2418
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002419 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002420 free_extent_map(em);
2421 em = NULL;
2422 }
2423 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002424 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002425 kfree(failrec);
2426 return -EIO;
2427 }
Miao Xie2fe63032014-09-12 18:43:59 +08002428
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002429 logical = start - em->start;
2430 logical = em->block_start + logical;
2431 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2432 logical = em->block_start;
2433 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2434 extent_set_compress_type(&failrec->bio_flags,
2435 em->compress_type);
2436 }
Miao Xie2fe63032014-09-12 18:43:59 +08002437
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002438 btrfs_debug(fs_info,
2439 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2440 logical, start, failrec->len);
Miao Xie2fe63032014-09-12 18:43:59 +08002441
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002442 failrec->logical = logical;
2443 free_extent_map(em);
2444
2445 /* set the bits in the private failure tree */
2446 ret = set_extent_bits(failure_tree, start, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002447 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002448 if (ret >= 0)
David Sterba47dc1962016-02-11 13:24:13 +01002449 ret = set_state_failrec(failure_tree, start, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002450 /* set the bits in the inode's tree */
2451 if (ret >= 0)
David Sterbaceeb0ae2016-04-26 23:54:39 +02002452 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002453 if (ret < 0) {
2454 kfree(failrec);
2455 return ret;
2456 }
2457 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002458 btrfs_debug(fs_info,
2459 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2460 failrec->logical, failrec->start, failrec->len,
2461 failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002462 /*
2463 * when data can be on disk more than twice, add to failrec here
2464 * (e.g. with a list for failed_mirror) to make
2465 * clean_io_failure() clean all those errors at once.
2466 */
2467 }
Miao Xie2fe63032014-09-12 18:43:59 +08002468
2469 *failrec_ret = failrec;
2470
2471 return 0;
2472}
2473
Ming Leia0b60d72017-12-18 20:22:11 +08002474bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages,
Miao Xie2fe63032014-09-12 18:43:59 +08002475 struct io_failure_record *failrec, int failed_mirror)
2476{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002477 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002478 int num_copies;
2479
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002480 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002481 if (num_copies == 1) {
2482 /*
2483 * we only have a single copy of the data, so don't bother with
2484 * all the retry and error correction code that follows. no
2485 * matter what the error is, it is very likely to persist.
2486 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002487 btrfs_debug(fs_info,
2488 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2489 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002490 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002491 }
2492
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002493 /*
2494 * there are two premises:
2495 * a) deliver good data to the caller
2496 * b) correct the bad sectors on disk
2497 */
Ming Leia0b60d72017-12-18 20:22:11 +08002498 if (failed_bio_pages > 1) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002499 /*
2500 * to fulfill b), we need to know the exact failing sectors, as
2501 * we don't want to rewrite any more than the failed ones. thus,
2502 * we need separate read requests for the failed bio
2503 *
2504 * if the following BUG_ON triggers, our validation request got
2505 * merged. we need separate requests for our algorithm to work.
2506 */
2507 BUG_ON(failrec->in_validation);
2508 failrec->in_validation = 1;
2509 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002510 } else {
2511 /*
2512 * we're ready to fulfill a) and b) alongside. get a good copy
2513 * of the failed sector and if we succeed, we have setup
2514 * everything for repair_io_failure to do the rest for us.
2515 */
2516 if (failrec->in_validation) {
2517 BUG_ON(failrec->this_mirror != failed_mirror);
2518 failrec->in_validation = 0;
2519 failrec->this_mirror = 0;
2520 }
2521 failrec->failed_mirror = failed_mirror;
2522 failrec->this_mirror++;
2523 if (failrec->this_mirror == failed_mirror)
2524 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002525 }
2526
Miao Xiefacc8a222013-07-25 19:22:34 +08002527 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002528 btrfs_debug(fs_info,
2529 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2530 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002531 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002532 }
2533
Liu Boc3cfb652017-07-13 15:00:50 -07002534 return true;
Miao Xie2fe63032014-09-12 18:43:59 +08002535}
2536
2537
2538struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2539 struct io_failure_record *failrec,
2540 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002541 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002542{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002543 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002544 struct bio *bio;
2545 struct btrfs_io_bio *btrfs_failed_bio;
2546 struct btrfs_io_bio *btrfs_bio;
2547
David Sterbac5e4c3d2017-06-12 17:29:41 +02002548 bio = btrfs_io_bio_alloc(1);
Miao Xie2fe63032014-09-12 18:43:59 +08002549 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002550 bio->bi_iter.bi_sector = failrec->logical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002551 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002552 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002553
Miao Xiefacc8a222013-07-25 19:22:34 +08002554 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2555 if (btrfs_failed_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002556 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2557
2558 btrfs_bio = btrfs_io_bio(bio);
2559 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002560 icsum *= csum_size;
2561 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002562 csum_size);
2563 }
2564
Miao Xie2fe63032014-09-12 18:43:59 +08002565 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002566
Miao Xie2fe63032014-09-12 18:43:59 +08002567 return bio;
2568}
2569
2570/*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002571 * This is a generic handler for readpage errors. If other copies exist, read
2572 * those and write back good data to the failed position. Does not investigate
2573 * in remapping the failed extent elsewhere, hoping the device will be smart
2574 * enough to do this as needed
Miao Xie2fe63032014-09-12 18:43:59 +08002575 */
Miao Xie2fe63032014-09-12 18:43:59 +08002576static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2577 struct page *page, u64 start, u64 end,
2578 int failed_mirror)
2579{
2580 struct io_failure_record *failrec;
2581 struct inode *inode = page->mapping->host;
2582 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002583 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie2fe63032014-09-12 18:43:59 +08002584 struct bio *bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002585 int read_mode = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002586 blk_status_t status;
Miao Xie2fe63032014-09-12 18:43:59 +08002587 int ret;
Christoph Hellwig8a2ee442019-02-15 19:13:07 +08002588 unsigned failed_bio_pages = failed_bio->bi_iter.bi_size >> PAGE_SHIFT;
Miao Xie2fe63032014-09-12 18:43:59 +08002589
Mike Christie1f7ad752016-06-05 14:31:51 -05002590 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002591
2592 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2593 if (ret)
2594 return ret;
2595
Ming Leia0b60d72017-12-18 20:22:11 +08002596 if (!btrfs_check_repairable(inode, failed_bio_pages, failrec,
Liu Boc3cfb652017-07-13 15:00:50 -07002597 failed_mirror)) {
Josef Bacik7870d082017-05-05 11:57:15 -04002598 free_io_failure(failure_tree, tree, failrec);
Miao Xie2fe63032014-09-12 18:43:59 +08002599 return -EIO;
2600 }
2601
Ming Leia0b60d72017-12-18 20:22:11 +08002602 if (failed_bio_pages > 1)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002603 read_mode |= REQ_FAILFAST_DEV;
Miao Xie2fe63032014-09-12 18:43:59 +08002604
2605 phy_offset >>= inode->i_sb->s_blocksize_bits;
2606 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2607 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002608 (int)phy_offset, failed_bio->bi_end_io,
2609 NULL);
David Sterbaebcc3262018-06-29 10:56:53 +02002610 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie2fe63032014-09-12 18:43:59 +08002611
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002612 btrfs_debug(btrfs_sb(inode->i_sb),
2613 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2614 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002615
Linus Torvalds8c27cb32017-07-05 16:41:23 -07002616 status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002617 failrec->bio_flags);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002618 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04002619 free_io_failure(failure_tree, tree, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002620 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002621 ret = blk_status_to_errno(status);
Miao Xie6c387ab2014-09-12 18:43:57 +08002622 }
2623
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002624 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002625}
2626
Chris Masond1310b22008-01-24 16:13:08 -05002627/* lots and lots of room for performance fixes in the end_bio funcs */
2628
David Sterbab5227c02015-12-03 13:08:59 +01002629void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002630{
2631 int uptodate = (err == 0);
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002632 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002633
Nikolay Borisovc6297322018-11-08 10:18:08 +02002634 btrfs_writepage_endio_finish_ordered(page, start, end, uptodate);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002635
Jeff Mahoney87826df2012-02-15 16:23:57 +01002636 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002637 ClearPageUptodate(page);
2638 SetPageError(page);
Colin Ian Kingbff5baf2017-05-09 18:14:01 +01002639 ret = err < 0 ? err : -EIO;
Liu Bo5dca6ee2014-05-12 12:47:36 +08002640 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002641 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002642}
2643
Chris Masond1310b22008-01-24 16:13:08 -05002644/*
2645 * after a writepage IO is done, we need to:
2646 * clear the uptodate bits on error
2647 * clear the writeback bits in the extent tree for this IO
2648 * end_page_writeback if the page has no more pending IO
2649 *
2650 * Scheduling is not allowed, so the extent state tree is expected
2651 * to have one and only one object corresponding to this IO.
2652 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002653static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002654{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002655 int error = blk_status_to_errno(bio->bi_status);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002656 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002657 u64 start;
2658 u64 end;
Ming Lei6dc4f102019-02-15 19:13:19 +08002659 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002660
David Sterbac09abff2017-07-13 18:10:07 +02002661 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02002662 bio_for_each_segment_all(bvec, bio, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002663 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002664 struct inode *inode = page->mapping->host;
2665 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002666
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002667 /* We always issue full-page reads, but if some block
2668 * in a page fails to read, blk_update_request() will
2669 * advance bv_offset and adjust bv_len to compensate.
2670 * Print a warning for nonzero offsets, and an error
2671 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002672 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2673 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002674 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002675 "partial page write in btrfs with offset %u and length %u",
2676 bvec->bv_offset, bvec->bv_len);
2677 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002678 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002679 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002680 bvec->bv_offset, bvec->bv_len);
2681 }
Chris Masond1310b22008-01-24 16:13:08 -05002682
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002683 start = page_offset(page);
2684 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002685
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002686 end_extent_writepage(page, error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002687 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002688 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002689
Chris Masond1310b22008-01-24 16:13:08 -05002690 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002691}
2692
Miao Xie883d0de2013-07-25 19:22:35 +08002693static void
2694endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2695 int uptodate)
2696{
2697 struct extent_state *cached = NULL;
2698 u64 end = start + len - 1;
2699
2700 if (uptodate && tree->track_uptodate)
2701 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
David Sterbad810a4b2017-12-07 18:52:54 +01002702 unlock_extent_cached_atomic(tree, start, end, &cached);
Miao Xie883d0de2013-07-25 19:22:35 +08002703}
2704
Chris Masond1310b22008-01-24 16:13:08 -05002705/*
2706 * after a readpage IO is done, we need to:
2707 * clear the uptodate bits on error
2708 * set the uptodate bits if things worked
2709 * set the page up to date if all extents in the tree are uptodate
2710 * clear the lock bit in the extent tree
2711 * unlock the page if there are no other extents locked for it
2712 *
2713 * Scheduling is not allowed, so the extent state tree is expected
2714 * to have one and only one object corresponding to this IO.
2715 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002716static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002717{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002718 struct bio_vec *bvec;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002719 int uptodate = !bio->bi_status;
Miao Xiefacc8a222013-07-25 19:22:34 +08002720 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04002721 struct extent_io_tree *tree, *failure_tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002722 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002723 u64 start;
2724 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002725 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002726 u64 extent_start = 0;
2727 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002728 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002729 int ret;
Ming Lei6dc4f102019-02-15 19:13:19 +08002730 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002731
David Sterbac09abff2017-07-13 18:10:07 +02002732 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02002733 bio_for_each_segment_all(bvec, bio, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002734 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002735 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002736 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002737 bool data_inode = btrfs_ino(BTRFS_I(inode))
2738 != BTRFS_BTREE_INODE_OBJECTID;
Arne Jansen507903b2011-04-06 10:02:20 +00002739
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002740 btrfs_debug(fs_info,
2741 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002742 (u64)bio->bi_iter.bi_sector, bio->bi_status,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002743 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002744 tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002745 failure_tree = &BTRFS_I(inode)->io_failure_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002746
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002747 /* We always issue full-page reads, but if some block
2748 * in a page fails to read, blk_update_request() will
2749 * advance bv_offset and adjust bv_len to compensate.
2750 * Print a warning for nonzero offsets, and an error
2751 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002752 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2753 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002754 btrfs_err(fs_info,
2755 "partial 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 else
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002758 btrfs_info(fs_info,
2759 "incomplete page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002760 bvec->bv_offset, bvec->bv_len);
2761 }
Chris Masond1310b22008-01-24 16:13:08 -05002762
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002763 start = page_offset(page);
2764 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002765 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002766
Chris Mason9be33952013-05-17 18:30:14 -04002767 mirror = io_bio->mirror_num;
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002768 if (likely(uptodate)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002769 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2770 page, start, end,
2771 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002772 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002773 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002774 else
Josef Bacik7870d082017-05-05 11:57:15 -04002775 clean_io_failure(BTRFS_I(inode)->root->fs_info,
2776 failure_tree, tree, start,
2777 page,
2778 btrfs_ino(BTRFS_I(inode)), 0);
Chris Masond1310b22008-01-24 16:13:08 -05002779 }
Josef Bacikea466792012-03-26 21:57:36 -04002780
Miao Xief2a09da2013-07-25 19:22:33 +08002781 if (likely(uptodate))
2782 goto readpage_ok;
2783
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002784 if (data_inode) {
Liu Bo9d0d1c82017-03-24 15:04:50 -07002785
2786 /*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002787 * The generic bio_readpage_error handles errors the
2788 * following way: If possible, new read requests are
2789 * created and submitted and will end up in
2790 * end_bio_extent_readpage as well (if we're lucky,
2791 * not in the !uptodate case). In that case it returns
2792 * 0 and we just go on with the next page in our bio.
2793 * If it can't handle the error it will return -EIO and
2794 * we remain responsible for that page.
Liu Bo9d0d1c82017-03-24 15:04:50 -07002795 */
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002796 ret = bio_readpage_error(bio, offset, page, start, end,
2797 mirror);
2798 if (ret == 0) {
2799 uptodate = !bio->bi_status;
2800 offset += len;
2801 continue;
2802 }
2803 } else {
2804 struct extent_buffer *eb;
2805
2806 eb = (struct extent_buffer *)page->private;
2807 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
2808 eb->read_mirror = mirror;
2809 atomic_dec(&eb->io_pages);
2810 if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD,
2811 &eb->bflags))
2812 btree_readahead_hook(eb, -EIO);
Chris Mason7e383262008-04-09 16:28:12 -04002813 }
Miao Xief2a09da2013-07-25 19:22:33 +08002814readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002815 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002816 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002817 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002818 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002819
2820 /* Zero out the end if this page straddles i_size */
Johannes Thumshirn70730172018-12-05 15:23:03 +01002821 off = offset_in_page(i_size);
Liu Boa583c022014-08-19 23:32:22 +08002822 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002823 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002824 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002825 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002826 ClearPageUptodate(page);
2827 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002828 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002829 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002830 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002831
2832 if (unlikely(!uptodate)) {
2833 if (extent_len) {
2834 endio_readpage_release_extent(tree,
2835 extent_start,
2836 extent_len, 1);
2837 extent_start = 0;
2838 extent_len = 0;
2839 }
2840 endio_readpage_release_extent(tree, start,
2841 end - start + 1, 0);
2842 } else if (!extent_len) {
2843 extent_start = start;
2844 extent_len = end + 1 - start;
2845 } else if (extent_start + extent_len == start) {
2846 extent_len += end + 1 - start;
2847 } else {
2848 endio_readpage_release_extent(tree, extent_start,
2849 extent_len, uptodate);
2850 extent_start = start;
2851 extent_len = end + 1 - start;
2852 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002853 }
Chris Masond1310b22008-01-24 16:13:08 -05002854
Miao Xie883d0de2013-07-25 19:22:35 +08002855 if (extent_len)
2856 endio_readpage_release_extent(tree, extent_start, extent_len,
2857 uptodate);
David Sterbab3a0dd52018-11-22 17:16:49 +01002858 btrfs_io_bio_free_csum(io_bio);
Chris Masond1310b22008-01-24 16:13:08 -05002859 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002860}
2861
Chris Mason9be33952013-05-17 18:30:14 -04002862/*
David Sterba184f9992017-06-12 17:29:39 +02002863 * Initialize the members up to but not including 'bio'. Use after allocating a
2864 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
2865 * 'bio' because use of __GFP_ZERO is not supported.
Chris Mason9be33952013-05-17 18:30:14 -04002866 */
David Sterba184f9992017-06-12 17:29:39 +02002867static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
Chris Masond1310b22008-01-24 16:13:08 -05002868{
David Sterba184f9992017-06-12 17:29:39 +02002869 memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
2870}
2871
2872/*
David Sterba6e707bc2017-06-02 17:26:26 +02002873 * The following helpers allocate a bio. As it's backed by a bioset, it'll
2874 * never fail. We're returning a bio right now but you can call btrfs_io_bio
2875 * for the appropriate container_of magic
Chris Masond1310b22008-01-24 16:13:08 -05002876 */
David Sterbae749af442019-06-18 20:00:16 +02002877struct bio *btrfs_bio_alloc(u64 first_byte)
Chris Masond1310b22008-01-24 16:13:08 -05002878{
2879 struct bio *bio;
2880
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002881 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset);
David Sterbac821e7f32017-06-02 18:35:36 +02002882 bio->bi_iter.bi_sector = first_byte >> 9;
David Sterba184f9992017-06-12 17:29:39 +02002883 btrfs_io_bio_init(btrfs_io_bio(bio));
Chris Masond1310b22008-01-24 16:13:08 -05002884 return bio;
2885}
2886
David Sterba8b6c1d52017-06-02 17:48:13 +02002887struct bio *btrfs_bio_clone(struct bio *bio)
Chris Mason9be33952013-05-17 18:30:14 -04002888{
Miao Xie23ea8e52014-09-12 18:43:54 +08002889 struct btrfs_io_bio *btrfs_bio;
2890 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002891
David Sterba6e707bc2017-06-02 17:26:26 +02002892 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002893 new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset);
David Sterba6e707bc2017-06-02 17:26:26 +02002894 btrfs_bio = btrfs_io_bio(new);
David Sterba184f9992017-06-12 17:29:39 +02002895 btrfs_io_bio_init(btrfs_bio);
David Sterba6e707bc2017-06-02 17:26:26 +02002896 btrfs_bio->iter = bio->bi_iter;
Miao Xie23ea8e52014-09-12 18:43:54 +08002897 return new;
2898}
Chris Mason9be33952013-05-17 18:30:14 -04002899
David Sterbac5e4c3d2017-06-12 17:29:41 +02002900struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
Chris Mason9be33952013-05-17 18:30:14 -04002901{
Miao Xiefacc8a222013-07-25 19:22:34 +08002902 struct bio *bio;
2903
David Sterba6e707bc2017-06-02 17:26:26 +02002904 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002905 bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
David Sterba184f9992017-06-12 17:29:39 +02002906 btrfs_io_bio_init(btrfs_io_bio(bio));
Miao Xiefacc8a222013-07-25 19:22:34 +08002907 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002908}
2909
Liu Boe4770942017-05-16 10:57:14 -07002910struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
Liu Bo2f8e9142017-05-15 17:43:31 -07002911{
2912 struct bio *bio;
2913 struct btrfs_io_bio *btrfs_bio;
2914
2915 /* this will never fail when it's backed by a bioset */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002916 bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset);
Liu Bo2f8e9142017-05-15 17:43:31 -07002917 ASSERT(bio);
2918
2919 btrfs_bio = btrfs_io_bio(bio);
David Sterba184f9992017-06-12 17:29:39 +02002920 btrfs_io_bio_init(btrfs_bio);
Liu Bo2f8e9142017-05-15 17:43:31 -07002921
2922 bio_trim(bio, offset >> 9, size >> 9);
Liu Bo17347ce2017-05-15 15:33:27 -07002923 btrfs_bio->iter = bio->bi_iter;
Liu Bo2f8e9142017-05-15 17:43:31 -07002924 return bio;
2925}
Chris Mason9be33952013-05-17 18:30:14 -04002926
David Sterba4b81ba42017-06-06 19:14:26 +02002927/*
2928 * @opf: bio REQ_OP_* and REQ_* flags as one value
David Sterbab8b3d622017-06-12 19:50:41 +02002929 * @tree: tree so we can call our merge_bio hook
2930 * @wbc: optional writeback control for io accounting
2931 * @page: page to add to the bio
2932 * @pg_offset: offset of the new bio or to check whether we are adding
2933 * a contiguous page to the previous one
2934 * @size: portion of page that we want to write
2935 * @offset: starting offset in the page
David Sterba5c2b1fd2017-06-06 19:22:55 +02002936 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
David Sterbab8b3d622017-06-12 19:50:41 +02002937 * @end_io_func: end_io callback for new bio
2938 * @mirror_num: desired mirror to read/write
2939 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
2940 * @bio_flags: flags of the current bio to see if we can merge them
David Sterba4b81ba42017-06-06 19:14:26 +02002941 */
2942static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002943 struct writeback_control *wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02002944 struct page *page, u64 offset,
David Sterba6c5a4e22017-10-04 17:10:34 +02002945 size_t size, unsigned long pg_offset,
Chris Masond1310b22008-01-24 16:13:08 -05002946 struct bio **bio_ret,
Chris Masonf1885912008-04-09 16:28:12 -04002947 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002948 int mirror_num,
2949 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002950 unsigned long bio_flags,
2951 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002952{
2953 int ret = 0;
2954 struct bio *bio;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002955 size_t page_size = min_t(size_t, size, PAGE_SIZE);
David Sterba6273b7f2017-10-04 17:30:11 +02002956 sector_t sector = offset >> 9;
Chris Masond1310b22008-01-24 16:13:08 -05002957
David Sterba5c2b1fd2017-06-06 19:22:55 +02002958 ASSERT(bio_ret);
2959
2960 if (*bio_ret) {
David Sterba0c8508a2017-06-12 20:00:43 +02002961 bool contig;
2962 bool can_merge = true;
2963
Chris Masond1310b22008-01-24 16:13:08 -05002964 bio = *bio_ret;
David Sterba0c8508a2017-06-12 20:00:43 +02002965 if (prev_bio_flags & EXTENT_BIO_COMPRESSED)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002966 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002967 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002968 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002969
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002970 ASSERT(tree->ops);
2971 if (btrfs_bio_fits_in_stripe(page, page_size, bio, bio_flags))
David Sterba0c8508a2017-06-12 20:00:43 +02002972 can_merge = false;
2973
2974 if (prev_bio_flags != bio_flags || !contig || !can_merge ||
Filipe Manana005efed2015-09-14 09:09:31 +01002975 force_bio_submit ||
David Sterba6c5a4e22017-10-04 17:10:34 +02002976 bio_add_page(bio, page, page_size, pg_offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002977 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002978 if (ret < 0) {
2979 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002980 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002981 }
Chris Masond1310b22008-01-24 16:13:08 -05002982 bio = NULL;
2983 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002984 if (wbc)
Tejun Heo34e51a52019-06-27 13:39:49 -07002985 wbc_account_cgroup_owner(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002986 return 0;
2987 }
2988 }
Chris Masonc8b97812008-10-29 14:49:59 -04002989
David Sterbae749af442019-06-18 20:00:16 +02002990 bio = btrfs_bio_alloc(offset);
David Sterba6c5a4e22017-10-04 17:10:34 +02002991 bio_add_page(bio, page, page_size, pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002992 bio->bi_end_io = end_io_func;
2993 bio->bi_private = tree;
Jens Axboee6959b92017-06-27 11:51:28 -06002994 bio->bi_write_hint = page->mapping->host->i_write_hint;
David Sterba4b81ba42017-06-06 19:14:26 +02002995 bio->bi_opf = opf;
Chris Masonda2f0f72015-07-02 13:57:22 -07002996 if (wbc) {
David Sterba429aebc2019-11-18 23:27:55 +01002997 struct block_device *bdev;
2998
2999 bdev = BTRFS_I(page->mapping->host)->root->fs_info->fs_devices->latest_bdev;
3000 bio_set_dev(bio, bdev);
Chris Masonda2f0f72015-07-02 13:57:22 -07003001 wbc_init_bio(wbc, bio);
Tejun Heo34e51a52019-06-27 13:39:49 -07003002 wbc_account_cgroup_owner(wbc, page, page_size);
Chris Masonda2f0f72015-07-02 13:57:22 -07003003 }
Chris Mason70dec802008-01-29 09:59:12 -05003004
David Sterba5c2b1fd2017-06-06 19:22:55 +02003005 *bio_ret = bio;
Chris Masond1310b22008-01-24 16:13:08 -05003006
3007 return ret;
3008}
3009
Eric Sandeen48a3b632013-04-25 20:41:01 +00003010static void attach_extent_buffer_page(struct extent_buffer *eb,
3011 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003012{
3013 if (!PagePrivate(page)) {
3014 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003015 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003016 set_page_private(page, (unsigned long)eb);
3017 } else {
3018 WARN_ON(page->private != (unsigned long)eb);
3019 }
3020}
3021
Chris Masond1310b22008-01-24 16:13:08 -05003022void set_page_extent_mapped(struct page *page)
3023{
3024 if (!PagePrivate(page)) {
3025 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003026 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04003027 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05003028 }
3029}
3030
Miao Xie125bac012013-07-25 19:22:37 +08003031static struct extent_map *
3032__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
3033 u64 start, u64 len, get_extent_t *get_extent,
3034 struct extent_map **em_cached)
3035{
3036 struct extent_map *em;
3037
3038 if (em_cached && *em_cached) {
3039 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00003040 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08003041 start < extent_map_end(em)) {
Elena Reshetova490b54d2017-03-03 10:55:12 +02003042 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08003043 return em;
3044 }
3045
3046 free_extent_map(em);
3047 *em_cached = NULL;
3048 }
3049
Omar Sandoval39b07b52019-12-02 17:34:23 -08003050 em = get_extent(BTRFS_I(inode), page, pg_offset, start, len);
Miao Xie125bac012013-07-25 19:22:37 +08003051 if (em_cached && !IS_ERR_OR_NULL(em)) {
3052 BUG_ON(*em_cached);
Elena Reshetova490b54d2017-03-03 10:55:12 +02003053 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08003054 *em_cached = em;
3055 }
3056 return em;
3057}
Chris Masond1310b22008-01-24 16:13:08 -05003058/*
3059 * basic readpage implementation. Locked extent state structs are inserted
3060 * into the tree that are removed when the IO is done (by the end_io
3061 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003062 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07003063 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05003064 */
Miao Xie99740902013-07-25 19:22:36 +08003065static int __do_readpage(struct extent_io_tree *tree,
3066 struct page *page,
3067 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08003068 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003069 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003070 unsigned long *bio_flags, unsigned int read_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003071 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05003072{
3073 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003074 u64 start = page_offset(page);
David Sterba8eec8292017-06-06 19:50:13 +02003075 const u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003076 u64 cur = start;
3077 u64 extent_offset;
3078 u64 last_byte = i_size_read(inode);
3079 u64 block_start;
3080 u64 cur_end;
Chris Masond1310b22008-01-24 16:13:08 -05003081 struct extent_map *em;
Liu Bobaf863b2016-07-11 10:39:07 -07003082 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003083 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02003084 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003085 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04003086 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05003087 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00003088 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003089
3090 set_page_extent_mapped(page);
3091
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003092 if (!PageUptodate(page)) {
3093 if (cleancache_get_page(page) == 0) {
3094 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003095 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003096 goto out;
3097 }
3098 }
3099
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003100 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04003101 char *userpage;
Johannes Thumshirn70730172018-12-05 15:23:03 +01003102 size_t zero_offset = offset_in_page(last_byte);
Chris Masonc8b97812008-10-29 14:49:59 -04003103
3104 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003105 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08003106 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04003107 memset(userpage + zero_offset, 0, iosize);
3108 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003109 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04003110 }
3111 }
Chris Masond1310b22008-01-24 16:13:08 -05003112 while (cur <= end) {
Filipe Manana005efed2015-09-14 09:09:31 +01003113 bool force_bio_submit = false;
David Sterba6273b7f2017-10-04 17:30:11 +02003114 u64 offset;
Josef Bacikc8f2f242013-02-11 11:33:00 -05003115
Chris Masond1310b22008-01-24 16:13:08 -05003116 if (cur >= last_byte) {
3117 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003118 struct extent_state *cached = NULL;
3119
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003120 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08003121 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003122 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003123 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003124 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003125 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003126 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003127 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01003128 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003129 break;
3130 }
Miao Xie125bac012013-07-25 19:22:37 +08003131 em = __get_extent_map(inode, page, pg_offset, cur,
3132 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02003133 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003134 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003135 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003136 break;
3137 }
Chris Masond1310b22008-01-24 16:13:08 -05003138 extent_offset = cur - em->start;
3139 BUG_ON(extent_map_end(em) <= cur);
3140 BUG_ON(end < cur);
3141
Li Zefan261507a02010-12-17 14:21:50 +08003142 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07003143 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08003144 extent_set_compress_type(&this_bio_flag,
3145 em->compress_type);
3146 }
Chris Masonc8b97812008-10-29 14:49:59 -04003147
Chris Masond1310b22008-01-24 16:13:08 -05003148 iosize = min(extent_map_end(em) - cur, end - cur + 1);
3149 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00003150 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04003151 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
3152 disk_io_size = em->block_len;
David Sterba6273b7f2017-10-04 17:30:11 +02003153 offset = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003154 } else {
David Sterba6273b7f2017-10-04 17:30:11 +02003155 offset = em->block_start + extent_offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003156 disk_io_size = iosize;
3157 }
Chris Masond1310b22008-01-24 16:13:08 -05003158 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04003159 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
3160 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01003161
3162 /*
3163 * If we have a file range that points to a compressed extent
3164 * and it's followed by a consecutive file range that points to
3165 * to the same compressed extent (possibly with a different
3166 * offset and/or length, so it either points to the whole extent
3167 * or only part of it), we must make sure we do not submit a
3168 * single bio to populate the pages for the 2 ranges because
3169 * this makes the compressed extent read zero out the pages
3170 * belonging to the 2nd range. Imagine the following scenario:
3171 *
3172 * File layout
3173 * [0 - 8K] [8K - 24K]
3174 * | |
3175 * | |
3176 * points to extent X, points to extent X,
3177 * offset 4K, length of 8K offset 0, length 16K
3178 *
3179 * [extent X, compressed length = 4K uncompressed length = 16K]
3180 *
3181 * If the bio to read the compressed extent covers both ranges,
3182 * it will decompress extent X into the pages belonging to the
3183 * first range and then it will stop, zeroing out the remaining
3184 * pages that belong to the other range that points to extent X.
3185 * So here we make sure we submit 2 bios, one for the first
3186 * range and another one for the third range. Both will target
3187 * the same physical extent from disk, but we can't currently
3188 * make the compressed bio endio callback populate the pages
3189 * for both ranges because each compressed bio is tightly
3190 * coupled with a single extent map, and each range can have
3191 * an extent map with a different offset value relative to the
3192 * uncompressed data of our extent and different lengths. This
3193 * is a corner case so we prioritize correctness over
3194 * non-optimal behavior (submitting 2 bios for the same extent).
3195 */
3196 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3197 prev_em_start && *prev_em_start != (u64)-1 &&
Filipe Manana8e928212019-02-14 15:17:20 +00003198 *prev_em_start != em->start)
Filipe Manana005efed2015-09-14 09:09:31 +01003199 force_bio_submit = true;
3200
3201 if (prev_em_start)
Filipe Manana8e928212019-02-14 15:17:20 +00003202 *prev_em_start = em->start;
Filipe Manana005efed2015-09-14 09:09:31 +01003203
Chris Masond1310b22008-01-24 16:13:08 -05003204 free_extent_map(em);
3205 em = NULL;
3206
3207 /* we've found a hole, just zero and go on */
3208 if (block_start == EXTENT_MAP_HOLE) {
3209 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003210 struct extent_state *cached = NULL;
3211
Cong Wang7ac687d2011-11-25 23:14:28 +08003212 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003213 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003214 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003215 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003216
3217 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003218 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003219 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01003220 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003221 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003222 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003223 continue;
3224 }
3225 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003226 if (test_range_bit(tree, cur, cur_end,
3227 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003228 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003229 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003230 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003231 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003232 continue;
3233 }
Chris Mason70dec802008-01-29 09:59:12 -05003234 /* we have an inline extent but it didn't get marked up
3235 * to date. Error out
3236 */
3237 if (block_start == EXTENT_MAP_INLINE) {
3238 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003239 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003240 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003241 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003242 continue;
3243 }
Chris Masond1310b22008-01-24 16:13:08 -05003244
David Sterba4b81ba42017-06-06 19:14:26 +02003245 ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL,
David Sterba6273b7f2017-10-04 17:30:11 +02003246 page, offset, disk_io_size,
David Sterbafa17ed02019-10-03 17:29:05 +02003247 pg_offset, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003248 end_bio_extent_readpage, mirror_num,
3249 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003250 this_bio_flag,
3251 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003252 if (!ret) {
3253 nr++;
3254 *bio_flags = this_bio_flag;
3255 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003256 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003257 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003258 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003259 }
Chris Masond1310b22008-01-24 16:13:08 -05003260 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003261 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003262 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003263out:
Chris Masond1310b22008-01-24 16:13:08 -05003264 if (!nr) {
3265 if (!PageError(page))
3266 SetPageUptodate(page);
3267 unlock_page(page);
3268 }
Liu Bobaf863b2016-07-11 10:39:07 -07003269 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003270}
3271
Nikolay Borisove65ef212019-03-11 09:55:38 +02003272static inline void contiguous_readpages(struct extent_io_tree *tree,
Miao Xie99740902013-07-25 19:22:36 +08003273 struct page *pages[], int nr_pages,
3274 u64 start, u64 end,
Miao Xie125bac012013-07-25 19:22:37 +08003275 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003276 struct bio **bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003277 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003278 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003279{
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03003280 struct btrfs_inode *inode = BTRFS_I(pages[0]->mapping->host);
Miao Xie99740902013-07-25 19:22:36 +08003281 int index;
3282
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03003283 btrfs_lock_and_flush_ordered_range(tree, inode, start, end, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003284
3285 for (index = 0; index < nr_pages; index++) {
David Sterba4ef77692017-06-23 04:09:57 +02003286 __do_readpage(tree, pages[index], btrfs_get_extent, em_cached,
Jens Axboe5e9d3982018-08-17 15:45:39 -07003287 bio, 0, bio_flags, REQ_RAHEAD, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003288 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003289 }
3290}
3291
Miao Xie99740902013-07-25 19:22:36 +08003292static int __extent_read_full_page(struct extent_io_tree *tree,
3293 struct page *page,
3294 get_extent_t *get_extent,
3295 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003296 unsigned long *bio_flags,
3297 unsigned int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003298{
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03003299 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
Miao Xie99740902013-07-25 19:22:36 +08003300 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003301 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003302 int ret;
3303
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03003304 btrfs_lock_and_flush_ordered_range(tree, inode, start, end, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003305
Miao Xie125bac012013-07-25 19:22:37 +08003306 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003307 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003308 return ret;
3309}
3310
Chris Masond1310b22008-01-24 16:13:08 -05003311int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003312 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003313{
3314 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003315 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003316 int ret;
3317
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003318 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003319 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003320 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003321 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003322 return ret;
3323}
Chris Masond1310b22008-01-24 16:13:08 -05003324
David Sterba3d4b9492017-02-10 19:33:41 +01003325static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003326 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003327{
3328 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003329}
3330
Chris Masond1310b22008-01-24 16:13:08 -05003331/*
Chris Mason40f76582014-05-21 13:35:51 -07003332 * helper for __extent_writepage, doing all of the delayed allocation setup.
3333 *
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003334 * This returns 1 if btrfs_run_delalloc_range function did all the work required
Chris Mason40f76582014-05-21 13:35:51 -07003335 * to write the page (copy into inline extent). In this case the IO has
3336 * been started and the page is already unlocked.
3337 *
3338 * This returns 0 if all went well (page still locked)
3339 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003340 */
Chris Mason40f76582014-05-21 13:35:51 -07003341static noinline_for_stack int writepage_delalloc(struct inode *inode,
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003342 struct page *page, struct writeback_control *wbc,
3343 u64 delalloc_start, unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003344{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003345 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Lu Fengqi3522e902018-11-29 11:33:38 +08003346 bool found;
Chris Mason40f76582014-05-21 13:35:51 -07003347 u64 delalloc_to_write = 0;
3348 u64 delalloc_end = 0;
3349 int ret;
3350 int page_started = 0;
3351
Chris Mason40f76582014-05-21 13:35:51 -07003352
3353 while (delalloc_end < page_end) {
Goldwyn Rodrigues99780592019-06-21 10:02:54 -05003354 found = find_lock_delalloc_range(inode, page,
Chris Mason40f76582014-05-21 13:35:51 -07003355 &delalloc_start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03003356 &delalloc_end);
Lu Fengqi3522e902018-11-29 11:33:38 +08003357 if (!found) {
Chris Mason40f76582014-05-21 13:35:51 -07003358 delalloc_start = delalloc_end + 1;
3359 continue;
3360 }
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003361 ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
3362 delalloc_end, &page_started, nr_written, wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003363 if (ret) {
3364 SetPageError(page);
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003365 /*
3366 * btrfs_run_delalloc_range should return < 0 for error
3367 * but just in case, we use > 0 here meaning the IO is
3368 * started, so we don't want to return > 0 unless
3369 * things are going well.
Chris Mason40f76582014-05-21 13:35:51 -07003370 */
3371 ret = ret < 0 ? ret : -EIO;
3372 goto done;
3373 }
3374 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003375 * delalloc_end is already one less than the total length, so
3376 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003377 */
3378 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003379 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003380 delalloc_start = delalloc_end + 1;
3381 }
3382 if (wbc->nr_to_write < delalloc_to_write) {
3383 int thresh = 8192;
3384
3385 if (delalloc_to_write < thresh * 2)
3386 thresh = delalloc_to_write;
3387 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3388 thresh);
3389 }
3390
3391 /* did the fill delalloc function already unlock and start
3392 * the IO?
3393 */
3394 if (page_started) {
3395 /*
3396 * we've unlocked the page, so we can't update
3397 * the mapping's writeback index, just update
3398 * nr_to_write.
3399 */
3400 wbc->nr_to_write -= *nr_written;
3401 return 1;
3402 }
3403
3404 ret = 0;
3405
3406done:
3407 return ret;
3408}
3409
3410/*
3411 * helper for __extent_writepage. This calls the writepage start hooks,
3412 * and does the loop to map the page into extents and bios.
3413 *
3414 * We return 1 if the IO is started and the page is unlocked,
3415 * 0 if all went well (page still locked)
3416 * < 0 if there were errors (page still locked)
3417 */
3418static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3419 struct page *page,
3420 struct writeback_control *wbc,
3421 struct extent_page_data *epd,
3422 loff_t i_size,
3423 unsigned long nr_written,
David Sterba57e5ffe2019-10-29 18:28:55 +01003424 int *nr_ret)
Chris Mason40f76582014-05-21 13:35:51 -07003425{
Chris Masond1310b22008-01-24 16:13:08 -05003426 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003427 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003428 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003429 u64 end;
3430 u64 cur = start;
3431 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003432 u64 block_start;
3433 u64 iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003434 struct extent_map *em;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003435 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003436 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003437 int ret = 0;
3438 int nr = 0;
David Sterba57e5ffe2019-10-29 18:28:55 +01003439 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003440 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003441
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003442 ret = btrfs_writepage_cow_fixup(page, start, page_end);
3443 if (ret) {
3444 /* Fixup worker will requeue */
Josef Bacik5ab58052020-01-21 11:51:43 -05003445 redirty_page_for_writepage(wbc, page);
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) {
Omar Sandovalc8b04032019-12-02 17:34:24 -08003495 if (compressed)
Chris Masonc8b97812008-10-29 14:49:59 -04003496 nr++;
Omar Sandovalc8b04032019-12-02 17:34:24 -08003497 else
3498 btrfs_writepage_endio_finish_ordered(page, cur,
3499 cur + iosize - 1, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003500 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003501 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003502 continue;
3503 }
Chris Masonc8b97812008-10-29 14:49:59 -04003504
David Sterba5cdc84b2018-07-18 20:32:52 +02003505 btrfs_set_range_writeback(tree, cur, cur + iosize - 1);
David Sterba58409ed2016-05-04 11:46:10 +02003506 if (!PageWriteback(page)) {
3507 btrfs_err(BTRFS_I(inode)->root->fs_info,
3508 "page %lu not writeback, cur %llu end %llu",
3509 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003510 }
David Sterba58409ed2016-05-04 11:46:10 +02003511
David Sterba4b81ba42017-06-06 19:14:26 +02003512 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003513 page, offset, iosize, pg_offset,
David Sterbafa17ed02019-10-03 17:29:05 +02003514 &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003515 end_bio_extent_writepage,
3516 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003517 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003518 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003519 if (PageWriteback(page))
3520 end_page_writeback(page);
3521 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003522
Chris Masond1310b22008-01-24 16:13:08 -05003523 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003524 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003525 nr++;
3526 }
Chris Mason40f76582014-05-21 13:35:51 -07003527 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003528 return ret;
3529}
3530
3531/*
3532 * the writepage semantics are similar to regular writepage. extent
3533 * records are inserted to lock ranges in the tree, and as dirty areas
3534 * are found, they are marked writeback. Then the lock bits are removed
3535 * and the end_io handler clears the writeback ranges
Qu Wenruo30659762019-03-20 14:27:42 +08003536 *
3537 * Return 0 if everything goes well.
3538 * Return <0 for error.
Chris Mason40f76582014-05-21 13:35:51 -07003539 */
3540static int __extent_writepage(struct page *page, struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003541 struct extent_page_data *epd)
Chris Mason40f76582014-05-21 13:35:51 -07003542{
3543 struct inode *inode = page->mapping->host;
Chris Mason40f76582014-05-21 13:35:51 -07003544 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003545 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003546 int ret;
3547 int nr = 0;
Omar Sandovaleb70d222019-12-02 17:34:20 -08003548 size_t pg_offset;
Chris Mason40f76582014-05-21 13:35:51 -07003549 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003550 unsigned long end_index = i_size >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003551 unsigned long nr_written = 0;
3552
Chris Mason40f76582014-05-21 13:35:51 -07003553 trace___extent_writepage(page, inode, wbc);
3554
3555 WARN_ON(!PageLocked(page));
3556
3557 ClearPageError(page);
3558
Johannes Thumshirn70730172018-12-05 15:23:03 +01003559 pg_offset = offset_in_page(i_size);
Chris Mason40f76582014-05-21 13:35:51 -07003560 if (page->index > end_index ||
3561 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003562 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003563 unlock_page(page);
3564 return 0;
3565 }
3566
3567 if (page->index == end_index) {
3568 char *userpage;
3569
3570 userpage = kmap_atomic(page);
3571 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003572 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003573 kunmap_atomic(userpage);
3574 flush_dcache_page(page);
3575 }
3576
Chris Mason40f76582014-05-21 13:35:51 -07003577 set_page_extent_mapped(page);
3578
Nikolay Borisov7789a552018-11-08 10:18:06 +02003579 if (!epd->extent_locked) {
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003580 ret = writepage_delalloc(inode, page, wbc, start, &nr_written);
Nikolay Borisov7789a552018-11-08 10:18:06 +02003581 if (ret == 1)
Omar Sandoval169d2c82019-12-02 17:34:21 -08003582 return 0;
Nikolay Borisov7789a552018-11-08 10:18:06 +02003583 if (ret)
3584 goto done;
3585 }
Chris Mason40f76582014-05-21 13:35:51 -07003586
3587 ret = __extent_writepage_io(inode, page, wbc, epd,
David Sterba57e5ffe2019-10-29 18:28:55 +01003588 i_size, nr_written, &nr);
Chris Mason40f76582014-05-21 13:35:51 -07003589 if (ret == 1)
Omar Sandoval169d2c82019-12-02 17:34:21 -08003590 return 0;
Chris Mason40f76582014-05-21 13:35:51 -07003591
3592done:
Chris Masond1310b22008-01-24 16:13:08 -05003593 if (nr == 0) {
3594 /* make sure the mapping tag for page dirty gets cleared */
3595 set_page_writeback(page);
3596 end_page_writeback(page);
3597 }
Filipe Manana61391d52014-05-09 17:17:40 +01003598 if (PageError(page)) {
3599 ret = ret < 0 ? ret : -EIO;
3600 end_extent_writepage(page, ret, start, page_end);
3601 }
Chris Masond1310b22008-01-24 16:13:08 -05003602 unlock_page(page);
Qu Wenruo30659762019-03-20 14:27:42 +08003603 ASSERT(ret <= 0);
Chris Mason40f76582014-05-21 13:35:51 -07003604 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003605}
3606
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003607void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003608{
NeilBrown74316202014-07-07 15:16:04 +10003609 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3610 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003611}
3612
Filipe Manana18dfa712019-09-11 17:42:00 +01003613static void end_extent_buffer_writeback(struct extent_buffer *eb)
3614{
3615 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3616 smp_mb__after_atomic();
3617 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3618}
3619
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003620/*
3621 * Lock eb pages and flush the bio if we can't the locks
3622 *
3623 * Return 0 if nothing went wrong
3624 * Return >0 is same as 0, except bio is not submitted
3625 * Return <0 if something went wrong, no page is locked
3626 */
David Sterba9df76fb2019-03-20 11:21:41 +01003627static noinline_for_stack int lock_extent_buffer_for_io(struct extent_buffer *eb,
Chris Mason0e378df2014-05-19 20:55:27 -07003628 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003629{
David Sterba9df76fb2019-03-20 11:21:41 +01003630 struct btrfs_fs_info *fs_info = eb->fs_info;
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003631 int i, num_pages, failed_page_nr;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003632 int flush = 0;
3633 int ret = 0;
3634
3635 if (!btrfs_try_tree_write_lock(eb)) {
Qu Wenruof4340622019-03-20 14:27:41 +08003636 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003637 if (ret < 0)
3638 return ret;
3639 flush = 1;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003640 btrfs_tree_lock(eb);
3641 }
3642
3643 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3644 btrfs_tree_unlock(eb);
3645 if (!epd->sync_io)
3646 return 0;
3647 if (!flush) {
Qu Wenruof4340622019-03-20 14:27:41 +08003648 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003649 if (ret < 0)
3650 return ret;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003651 flush = 1;
3652 }
Chris Masona098d8e82012-03-21 12:09:56 -04003653 while (1) {
3654 wait_on_extent_buffer_writeback(eb);
3655 btrfs_tree_lock(eb);
3656 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3657 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003658 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003659 }
3660 }
3661
Josef Bacik51561ff2012-07-20 16:25:24 -04003662 /*
3663 * We need to do this to prevent races in people who check if the eb is
3664 * under IO since we can end up having no IO bits set for a short period
3665 * of time.
3666 */
3667 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003668 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3669 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003670 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003671 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Nikolay Borisov104b4e52017-06-20 21:01:20 +03003672 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3673 -eb->len,
3674 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003675 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003676 } else {
3677 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003678 }
3679
3680 btrfs_tree_unlock(eb);
3681
3682 if (!ret)
3683 return ret;
3684
David Sterba65ad0102018-06-29 10:56:49 +02003685 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003686 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003687 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003688
3689 if (!trylock_page(p)) {
3690 if (!flush) {
Filipe Manana18dfa712019-09-11 17:42:00 +01003691 int err;
3692
3693 err = flush_write_bio(epd);
3694 if (err < 0) {
3695 ret = err;
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003696 failed_page_nr = i;
3697 goto err_unlock;
3698 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003699 flush = 1;
3700 }
3701 lock_page(p);
3702 }
3703 }
3704
3705 return ret;
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003706err_unlock:
3707 /* Unlock already locked pages */
3708 for (i = 0; i < failed_page_nr; i++)
3709 unlock_page(eb->pages[i]);
Filipe Manana18dfa712019-09-11 17:42:00 +01003710 /*
3711 * Clear EXTENT_BUFFER_WRITEBACK and wake up anyone waiting on it.
3712 * Also set back EXTENT_BUFFER_DIRTY so future attempts to this eb can
3713 * be made and undo everything done before.
3714 */
3715 btrfs_tree_lock(eb);
3716 spin_lock(&eb->refs_lock);
3717 set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
3718 end_extent_buffer_writeback(eb);
3719 spin_unlock(&eb->refs_lock);
3720 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, eb->len,
3721 fs_info->dirty_metadata_batch);
3722 btrfs_clear_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3723 btrfs_tree_unlock(eb);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003724 return ret;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003725}
3726
Filipe Manana656f30d2014-09-26 12:25:56 +01003727static void set_btree_ioerr(struct page *page)
3728{
3729 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Dennis Zhoueb5b64f2019-09-13 14:54:07 +01003730 struct btrfs_fs_info *fs_info;
Filipe Manana656f30d2014-09-26 12:25:56 +01003731
3732 SetPageError(page);
3733 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3734 return;
3735
3736 /*
Dennis Zhoueb5b64f2019-09-13 14:54:07 +01003737 * If we error out, we should add back the dirty_metadata_bytes
3738 * to make it consistent.
3739 */
3740 fs_info = eb->fs_info;
3741 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3742 eb->len, fs_info->dirty_metadata_batch);
3743
3744 /*
Filipe Manana656f30d2014-09-26 12:25:56 +01003745 * If writeback for a btree extent that doesn't belong to a log tree
3746 * failed, increment the counter transaction->eb_write_errors.
3747 * We do this because while the transaction is running and before it's
3748 * committing (when we call filemap_fdata[write|wait]_range against
3749 * the btree inode), we might have
3750 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3751 * returns an error or an error happens during writeback, when we're
3752 * committing the transaction we wouldn't know about it, since the pages
3753 * can be no longer dirty nor marked anymore for writeback (if a
3754 * subsequent modification to the extent buffer didn't happen before the
3755 * transaction commit), which makes filemap_fdata[write|wait]_range not
3756 * able to find the pages tagged with SetPageError at transaction
3757 * commit time. So if this happens we must abort the transaction,
3758 * otherwise we commit a super block with btree roots that point to
3759 * btree nodes/leafs whose content on disk is invalid - either garbage
3760 * or the content of some node/leaf from a past generation that got
3761 * cowed or deleted and is no longer valid.
3762 *
3763 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3764 * not be enough - we need to distinguish between log tree extents vs
3765 * non-log tree extents, and the next filemap_fdatawait_range() call
3766 * will catch and clear such errors in the mapping - and that call might
3767 * be from a log sync and not from a transaction commit. Also, checking
3768 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3769 * not done and would not be reliable - the eb might have been released
3770 * from memory and reading it back again means that flag would not be
3771 * set (since it's a runtime flag, not persisted on disk).
3772 *
3773 * Using the flags below in the btree inode also makes us achieve the
3774 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3775 * writeback for all dirty pages and before filemap_fdatawait_range()
3776 * is called, the writeback for all dirty pages had already finished
3777 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3778 * filemap_fdatawait_range() would return success, as it could not know
3779 * that writeback errors happened (the pages were no longer tagged for
3780 * writeback).
3781 */
3782 switch (eb->log_index) {
3783 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003784 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003785 break;
3786 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003787 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003788 break;
3789 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003790 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003791 break;
3792 default:
3793 BUG(); /* unexpected, logic error */
3794 }
3795}
3796
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003797static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003798{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003799 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003800 struct extent_buffer *eb;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02003801 int done;
Ming Lei6dc4f102019-02-15 19:13:19 +08003802 struct bvec_iter_all iter_all;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003803
David Sterbac09abff2017-07-13 18:10:07 +02003804 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02003805 bio_for_each_segment_all(bvec, bio, iter_all) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003806 struct page *page = bvec->bv_page;
3807
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003808 eb = (struct extent_buffer *)page->private;
3809 BUG_ON(!eb);
3810 done = atomic_dec_and_test(&eb->io_pages);
3811
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003812 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003813 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003814 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003815 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003816 }
3817
3818 end_page_writeback(page);
3819
3820 if (!done)
3821 continue;
3822
3823 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003824 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003825
3826 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003827}
3828
Chris Mason0e378df2014-05-19 20:55:27 -07003829static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003830 struct writeback_control *wbc,
3831 struct extent_page_data *epd)
3832{
David Sterba0ab02062019-03-20 11:27:57 +01003833 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacikf28491e2013-12-16 13:24:27 -05003834 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003835 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003836 u32 nritems;
David Sterbacc5e31a2018-03-01 18:20:27 +01003837 int i, num_pages;
Liu Bo851cd172016-09-23 13:44:44 -07003838 unsigned long start, end;
Liu Boff40adf2017-08-24 18:19:48 -06003839 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003840 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003841
Filipe Manana656f30d2014-09-26 12:25:56 +01003842 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02003843 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003844 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003845
Liu Bo851cd172016-09-23 13:44:44 -07003846 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3847 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003848 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003849 end = btrfs_node_key_ptr_offset(nritems);
3850
David Sterbab159fa22016-11-08 18:09:03 +01003851 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003852 } else {
3853 /*
3854 * leaf:
3855 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3856 */
3857 start = btrfs_item_nr_offset(nritems);
David Sterba8f881e82019-03-20 11:33:10 +01003858 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(eb);
David Sterbab159fa22016-11-08 18:09:03 +01003859 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003860 }
3861
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003862 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003863 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003864
3865 clear_page_dirty_for_io(p);
3866 set_page_writeback(p);
David Sterba4b81ba42017-06-06 19:14:26 +02003867 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterbafa17ed02019-10-03 17:29:05 +02003868 p, offset, PAGE_SIZE, 0,
David Sterbac2df8bb2017-02-10 19:29:38 +01003869 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003870 end_bio_extent_buffer_writepage,
Liu Bo18fdc672017-09-13 12:18:22 -06003871 0, 0, 0, false);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003872 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003873 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003874 if (PageWriteback(p))
3875 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003876 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3877 end_extent_buffer_writeback(eb);
3878 ret = -EIO;
3879 break;
3880 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003881 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003882 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003883 unlock_page(p);
3884 }
3885
3886 if (unlikely(ret)) {
3887 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003888 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003889 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003890 unlock_page(p);
3891 }
3892 }
3893
3894 return ret;
3895}
3896
3897int btree_write_cache_pages(struct address_space *mapping,
3898 struct writeback_control *wbc)
3899{
3900 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003901 struct extent_buffer *eb, *prev_eb = NULL;
3902 struct extent_page_data epd = {
3903 .bio = NULL,
3904 .tree = tree,
3905 .extent_locked = 0,
3906 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3907 };
3908 int ret = 0;
3909 int done = 0;
3910 int nr_to_write_done = 0;
3911 struct pagevec pvec;
3912 int nr_pages;
3913 pgoff_t index;
3914 pgoff_t end; /* Inclusive */
3915 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003916 xa_mark_t tag;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003917
Mel Gorman86679822017-11-15 17:37:52 -08003918 pagevec_init(&pvec);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003919 if (wbc->range_cyclic) {
3920 index = mapping->writeback_index; /* Start from prev offset */
3921 end = -1;
Josef Bacik556755a2020-01-03 10:38:44 -05003922 /*
3923 * Start from the beginning does not need to cycle over the
3924 * range, mark it as scanned.
3925 */
3926 scanned = (index == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003927 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003928 index = wbc->range_start >> PAGE_SHIFT;
3929 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003930 scanned = 1;
3931 }
3932 if (wbc->sync_mode == WB_SYNC_ALL)
3933 tag = PAGECACHE_TAG_TOWRITE;
3934 else
3935 tag = PAGECACHE_TAG_DIRTY;
3936retry:
3937 if (wbc->sync_mode == WB_SYNC_ALL)
3938 tag_pages_for_writeback(mapping, index, end);
3939 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara4006f432017-11-15 17:34:37 -08003940 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08003941 tag))) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003942 unsigned i;
3943
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003944 for (i = 0; i < nr_pages; i++) {
3945 struct page *page = pvec.pages[i];
3946
3947 if (!PagePrivate(page))
3948 continue;
3949
Josef Bacikb5bae262012-09-14 13:43:01 -04003950 spin_lock(&mapping->private_lock);
3951 if (!PagePrivate(page)) {
3952 spin_unlock(&mapping->private_lock);
3953 continue;
3954 }
3955
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003956 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003957
3958 /*
3959 * Shouldn't happen and normally this would be a BUG_ON
3960 * but no sense in crashing the users box for something
3961 * we can survive anyway.
3962 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303963 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003964 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003965 continue;
3966 }
3967
Josef Bacikb5bae262012-09-14 13:43:01 -04003968 if (eb == prev_eb) {
3969 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003970 continue;
3971 }
3972
Josef Bacikb5bae262012-09-14 13:43:01 -04003973 ret = atomic_inc_not_zero(&eb->refs);
3974 spin_unlock(&mapping->private_lock);
3975 if (!ret)
3976 continue;
3977
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003978 prev_eb = eb;
David Sterba9df76fb2019-03-20 11:21:41 +01003979 ret = lock_extent_buffer_for_io(eb, &epd);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003980 if (!ret) {
3981 free_extent_buffer(eb);
3982 continue;
Filipe Manana0607eb1d2019-09-11 17:42:28 +01003983 } else if (ret < 0) {
3984 done = 1;
3985 free_extent_buffer(eb);
3986 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003987 }
3988
David Sterba0ab02062019-03-20 11:27:57 +01003989 ret = write_one_eb(eb, wbc, &epd);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003990 if (ret) {
3991 done = 1;
3992 free_extent_buffer(eb);
3993 break;
3994 }
3995 free_extent_buffer(eb);
3996
3997 /*
3998 * the filesystem may choose to bump up nr_to_write.
3999 * We have to make sure to honor the new nr_to_write
4000 * at any time
4001 */
4002 nr_to_write_done = wbc->nr_to_write <= 0;
4003 }
4004 pagevec_release(&pvec);
4005 cond_resched();
4006 }
4007 if (!scanned && !done) {
4008 /*
4009 * We hit the last page and there is more work to be done: wrap
4010 * back to the start of the file
4011 */
4012 scanned = 1;
4013 index = 0;
4014 goto retry;
4015 }
Qu Wenruo2b952ee2019-03-20 14:27:43 +08004016 ASSERT(ret <= 0);
4017 if (ret < 0) {
4018 end_write_bio(&epd, ret);
4019 return ret;
4020 }
4021 ret = flush_write_bio(&epd);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004022 return ret;
4023}
4024
Chris Masond1310b22008-01-24 16:13:08 -05004025/**
Chris Mason4bef0842008-09-08 11:18:08 -04004026 * 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 -05004027 * @mapping: address space structure to write
4028 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
David Sterba935db852017-06-23 04:30:28 +02004029 * @data: data passed to __extent_writepage function
Chris Masond1310b22008-01-24 16:13:08 -05004030 *
4031 * If a page is already under I/O, write_cache_pages() skips it, even
4032 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
4033 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
4034 * and msync() need to guarantee that all the data which was dirty at the time
4035 * the call was made get new I/O started against them. If wbc->sync_mode is
4036 * WB_SYNC_ALL then we were called for data integrity and we must wait for
4037 * existing IO to complete.
4038 */
David Sterba4242b642017-02-10 19:38:24 +01004039static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04004040 struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01004041 struct extent_page_data *epd)
Chris Masond1310b22008-01-24 16:13:08 -05004042{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004043 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05004044 int ret = 0;
4045 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004046 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004047 struct pagevec pvec;
4048 int nr_pages;
4049 pgoff_t index;
4050 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08004051 pgoff_t done_index;
4052 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004053 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05004054 xa_mark_t tag;
Chris Masond1310b22008-01-24 16:13:08 -05004055
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004056 /*
4057 * We have to hold onto the inode so that ordered extents can do their
4058 * work when the IO finishes. The alternative to this is failing to add
4059 * an ordered extent if the igrab() fails there and that is a huge pain
4060 * to deal with, so instead just hold onto the inode throughout the
4061 * writepages operation. If it fails here we are freeing up the inode
4062 * anyway and we'd rather not waste our time writing out stuff that is
4063 * going to be truncated anyway.
4064 */
4065 if (!igrab(inode))
4066 return 0;
4067
Mel Gorman86679822017-11-15 17:37:52 -08004068 pagevec_init(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05004069 if (wbc->range_cyclic) {
4070 index = mapping->writeback_index; /* Start from prev offset */
4071 end = -1;
Josef Bacik556755a2020-01-03 10:38:44 -05004072 /*
4073 * Start from the beginning does not need to cycle over the
4074 * range, mark it as scanned.
4075 */
4076 scanned = (index == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004077 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004078 index = wbc->range_start >> PAGE_SHIFT;
4079 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08004080 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
4081 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004082 scanned = 1;
4083 }
Ethan Lien3cd24c62018-11-01 14:49:03 +08004084
4085 /*
4086 * We do the tagged writepage as long as the snapshot flush bit is set
4087 * and we are the first one who do the filemap_flush() on this inode.
4088 *
4089 * The nr_to_write == LONG_MAX is needed to make sure other flushers do
4090 * not race in and drop the bit.
4091 */
4092 if (range_whole && wbc->nr_to_write == LONG_MAX &&
4093 test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
4094 &BTRFS_I(inode)->runtime_flags))
4095 wbc->tagged_writepages = 1;
4096
4097 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00004098 tag = PAGECACHE_TAG_TOWRITE;
4099 else
4100 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05004101retry:
Ethan Lien3cd24c62018-11-01 14:49:03 +08004102 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00004103 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08004104 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004105 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara67fd7072017-11-15 17:35:19 -08004106 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
4107 &index, end, tag))) {
Chris Masond1310b22008-01-24 16:13:08 -05004108 unsigned i;
4109
Chris Masond1310b22008-01-24 16:13:08 -05004110 for (i = 0; i < nr_pages; i++) {
4111 struct page *page = pvec.pages[i];
4112
Tejun Heof7bddf12019-10-03 07:27:13 -07004113 done_index = page->index + 1;
Chris Masond1310b22008-01-24 16:13:08 -05004114 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07004115 * At this point we hold neither the i_pages lock nor
4116 * the page lock: the page may be truncated or
4117 * invalidated (changing page->mapping to NULL),
4118 * or even swizzled back from swapper_space to
4119 * tmpfs file mapping
Chris Masond1310b22008-01-24 16:13:08 -05004120 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05004121 if (!trylock_page(page)) {
Qu Wenruof4340622019-03-20 14:27:41 +08004122 ret = flush_write_bio(epd);
4123 BUG_ON(ret < 0);
Josef Bacikc8f2f242013-02-11 11:33:00 -05004124 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04004125 }
Chris Masond1310b22008-01-24 16:13:08 -05004126
4127 if (unlikely(page->mapping != mapping)) {
4128 unlock_page(page);
4129 continue;
4130 }
4131
Chris Masond2c3f4f2008-11-19 12:44:22 -05004132 if (wbc->sync_mode != WB_SYNC_NONE) {
Qu Wenruof4340622019-03-20 14:27:41 +08004133 if (PageWriteback(page)) {
4134 ret = flush_write_bio(epd);
4135 BUG_ON(ret < 0);
4136 }
Chris Masond1310b22008-01-24 16:13:08 -05004137 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004138 }
Chris Masond1310b22008-01-24 16:13:08 -05004139
4140 if (PageWriteback(page) ||
4141 !clear_page_dirty_for_io(page)) {
4142 unlock_page(page);
4143 continue;
4144 }
4145
David Sterbaaab6e9e2017-11-30 18:00:02 +01004146 ret = __extent_writepage(page, wbc, epd);
Liu Boa91326672016-03-07 16:56:21 -08004147 if (ret < 0) {
Liu Boa91326672016-03-07 16:56:21 -08004148 done = 1;
4149 break;
4150 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004151
4152 /*
4153 * the filesystem may choose to bump up nr_to_write.
4154 * We have to make sure to honor the new nr_to_write
4155 * at any time
4156 */
4157 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004158 }
4159 pagevec_release(&pvec);
4160 cond_resched();
4161 }
Liu Bo894b36e2016-03-07 16:56:22 -08004162 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004163 /*
4164 * We hit the last page and there is more work to be done: wrap
4165 * back to the start of the file
4166 */
4167 scanned = 1;
4168 index = 0;
Josef Bacik42ffb0b2020-01-23 15:33:02 -05004169
4170 /*
4171 * If we're looping we could run into a page that is locked by a
4172 * writer and that writer could be waiting on writeback for a
4173 * page in our current bio, and thus deadlock, so flush the
4174 * write bio here.
4175 */
4176 ret = flush_write_bio(epd);
4177 if (!ret)
4178 goto retry;
Chris Masond1310b22008-01-24 16:13:08 -05004179 }
Liu Boa91326672016-03-07 16:56:21 -08004180
4181 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4182 mapping->writeback_index = done_index;
4183
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004184 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004185 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004186}
Chris Masond1310b22008-01-24 16:13:08 -05004187
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004188int extent_write_full_page(struct page *page, struct writeback_control *wbc)
Chris Masond1310b22008-01-24 16:13:08 -05004189{
4190 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004191 struct extent_page_data epd = {
4192 .bio = NULL,
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004193 .tree = &BTRFS_I(page->mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004194 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004195 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004196 };
Chris Masond1310b22008-01-24 16:13:08 -05004197
Chris Masond1310b22008-01-24 16:13:08 -05004198 ret = __extent_writepage(page, wbc, &epd);
Qu Wenruo30659762019-03-20 14:27:42 +08004199 ASSERT(ret <= 0);
4200 if (ret < 0) {
4201 end_write_bio(&epd, ret);
4202 return ret;
4203 }
Chris Masond1310b22008-01-24 16:13:08 -05004204
Qu Wenruo30659762019-03-20 14:27:42 +08004205 ret = flush_write_bio(&epd);
4206 ASSERT(ret <= 0);
Chris Masond1310b22008-01-24 16:13:08 -05004207 return ret;
4208}
Chris Masond1310b22008-01-24 16:13:08 -05004209
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004210int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
Chris Mason771ed682008-11-06 22:02:51 -05004211 int mode)
4212{
4213 int ret = 0;
4214 struct address_space *mapping = inode->i_mapping;
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004215 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Mason771ed682008-11-06 22:02:51 -05004216 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004217 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4218 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004219
4220 struct extent_page_data epd = {
4221 .bio = NULL,
4222 .tree = tree,
Chris Mason771ed682008-11-06 22:02:51 -05004223 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004224 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05004225 };
4226 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004227 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004228 .nr_to_write = nr_pages * 2,
4229 .range_start = start,
4230 .range_end = end + 1,
Chris Masonec39f762019-07-10 12:28:17 -07004231 /* We're called from an async helper function */
4232 .punt_to_cgroup = 1,
4233 .no_cgroup_owner = 1,
Chris Mason771ed682008-11-06 22:02:51 -05004234 };
4235
Chris Masondbb70be2019-07-10 12:28:18 -07004236 wbc_attach_fdatawrite_inode(&wbc_writepages, inode);
Chris Masond3977122009-01-05 21:25:51 -05004237 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004238 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004239 if (clear_page_dirty_for_io(page))
4240 ret = __extent_writepage(page, &wbc_writepages, &epd);
4241 else {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02004242 btrfs_writepage_endio_finish_ordered(page, start,
Nikolay Borisovc6297322018-11-08 10:18:08 +02004243 start + PAGE_SIZE - 1, 1);
Chris Mason771ed682008-11-06 22:02:51 -05004244 unlock_page(page);
4245 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004246 put_page(page);
4247 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004248 }
4249
Qu Wenruo02c6db42019-03-20 14:27:45 +08004250 ASSERT(ret <= 0);
Chris Masondbb70be2019-07-10 12:28:18 -07004251 if (ret == 0)
4252 ret = flush_write_bio(&epd);
4253 else
Qu Wenruo02c6db42019-03-20 14:27:45 +08004254 end_write_bio(&epd, ret);
Chris Masondbb70be2019-07-10 12:28:18 -07004255
4256 wbc_detach_inode(&wbc_writepages);
Chris Mason771ed682008-11-06 22:02:51 -05004257 return ret;
4258}
Chris Masond1310b22008-01-24 16:13:08 -05004259
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004260int extent_writepages(struct address_space *mapping,
Chris Masond1310b22008-01-24 16:13:08 -05004261 struct writeback_control *wbc)
4262{
4263 int ret = 0;
4264 struct extent_page_data epd = {
4265 .bio = NULL,
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004266 .tree = &BTRFS_I(mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004267 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004268 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004269 };
4270
David Sterba935db852017-06-23 04:30:28 +02004271 ret = extent_write_cache_pages(mapping, wbc, &epd);
Qu Wenruoa2a72fb2019-03-20 14:27:48 +08004272 ASSERT(ret <= 0);
4273 if (ret < 0) {
4274 end_write_bio(&epd, ret);
4275 return ret;
4276 }
4277 ret = flush_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004278 return ret;
4279}
Chris Masond1310b22008-01-24 16:13:08 -05004280
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004281int extent_readpages(struct address_space *mapping, struct list_head *pages,
4282 unsigned nr_pages)
Chris Masond1310b22008-01-24 16:13:08 -05004283{
4284 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004285 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004286 struct page *pagepool[16];
Miao Xie125bac012013-07-25 19:22:37 +08004287 struct extent_map *em_cached = NULL;
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004288 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Liu Bo67c96842012-07-20 21:43:09 -06004289 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004290 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004291
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004292 while (!list_empty(pages)) {
Nikolay Borisove65ef212019-03-11 09:55:38 +02004293 u64 contig_end = 0;
4294
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004295 for (nr = 0; nr < ARRAY_SIZE(pagepool) && !list_empty(pages);) {
Nikolay Borisovf86196e2019-01-03 15:29:02 -08004296 struct page *page = lru_to_page(pages);
Chris Masond1310b22008-01-24 16:13:08 -05004297
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004298 prefetchw(&page->flags);
4299 list_del(&page->lru);
4300 if (add_to_page_cache_lru(page, mapping, page->index,
4301 readahead_gfp_mask(mapping))) {
4302 put_page(page);
Nikolay Borisove65ef212019-03-11 09:55:38 +02004303 break;
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004304 }
4305
4306 pagepool[nr++] = page;
Nikolay Borisove65ef212019-03-11 09:55:38 +02004307 contig_end = page_offset(page) + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004308 }
Liu Bo67c96842012-07-20 21:43:09 -06004309
Nikolay Borisove65ef212019-03-11 09:55:38 +02004310 if (nr) {
4311 u64 contig_start = page_offset(pagepool[0]);
4312
4313 ASSERT(contig_start + nr * PAGE_SIZE - 1 == contig_end);
4314
4315 contiguous_readpages(tree, pagepool, nr, contig_start,
4316 contig_end, &em_cached, &bio, &bio_flags,
4317 &prev_em_start);
4318 }
Chris Masond1310b22008-01-24 16:13:08 -05004319 }
Liu Bo67c96842012-07-20 21:43:09 -06004320
Miao Xie125bac012013-07-25 19:22:37 +08004321 if (em_cached)
4322 free_extent_map(em_cached);
4323
Chris Masond1310b22008-01-24 16:13:08 -05004324 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004325 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004326 return 0;
4327}
Chris Masond1310b22008-01-24 16:13:08 -05004328
4329/*
4330 * basic invalidatepage code, this waits on any locked or writeback
4331 * ranges corresponding to the page, and then deletes any extent state
4332 * records from the tree
4333 */
4334int extent_invalidatepage(struct extent_io_tree *tree,
4335 struct page *page, unsigned long offset)
4336{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004337 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004338 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004339 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004340 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4341
Qu Wenruofda28322013-02-26 08:10:22 +00004342 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004343 if (start > end)
4344 return 0;
4345
David Sterbaff13db42015-12-03 14:30:40 +01004346 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004347 wait_on_page_writeback(page);
Omar Sandovale1821632019-08-15 14:04:04 -07004348 clear_extent_bit(tree, start, end, EXTENT_LOCKED | EXTENT_DELALLOC |
4349 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05004350 return 0;
4351}
Chris Masond1310b22008-01-24 16:13:08 -05004352
4353/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004354 * a helper for releasepage, this tests for areas of the page that
4355 * are locked or under IO and drops the related state bits if it is safe
4356 * to drop the page.
4357 */
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004358static int try_release_extent_state(struct extent_io_tree *tree,
Eric Sandeen48a3b632013-04-25 20:41:01 +00004359 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004360{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004361 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004362 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004363 int ret = 1;
4364
Nikolay Borisov88826792019-03-14 15:28:31 +02004365 if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) {
Chris Mason7b13b7b2008-04-18 10:29:50 -04004366 ret = 0;
Nikolay Borisov88826792019-03-14 15:28:31 +02004367 } else {
Chris Mason11ef1602009-09-23 20:28:46 -04004368 /*
4369 * at this point we can safely clear everything except the
4370 * locked bit and the nodatasum bit
4371 */
David Sterba66b0c882017-10-31 16:30:47 +01004372 ret = __clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004373 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
David Sterba66b0c882017-10-31 16:30:47 +01004374 0, 0, NULL, mask, NULL);
Chris Masone3f24cc2011-02-14 12:52:08 -05004375
4376 /* if clear_extent_bit failed for enomem reasons,
4377 * we can't allow the release to continue.
4378 */
4379 if (ret < 0)
4380 ret = 0;
4381 else
4382 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004383 }
4384 return ret;
4385}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004386
4387/*
Chris Masond1310b22008-01-24 16:13:08 -05004388 * a helper for releasepage. As long as there are no locked extents
4389 * in the range corresponding to the page, both state records and extent
4390 * map records are removed
4391 */
Nikolay Borisov477a30b2018-04-19 10:46:34 +03004392int try_release_extent_mapping(struct page *page, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004393{
4394 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004395 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004396 u64 end = start + PAGE_SIZE - 1;
Filipe Mananabd3599a2018-07-12 01:36:43 +01004397 struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
4398 struct extent_io_tree *tree = &btrfs_inode->io_tree;
4399 struct extent_map_tree *map = &btrfs_inode->extent_tree;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004400
Mel Gormand0164ad2015-11-06 16:28:21 -08004401 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004402 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004403 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004404 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004405 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004406 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004407 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004408 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004409 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004410 break;
4411 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004412 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4413 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004414 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004415 free_extent_map(em);
4416 break;
4417 }
4418 if (!test_range_bit(tree, em->start,
4419 extent_map_end(em) - 1,
Nikolay Borisov4e586ca2019-03-14 15:28:30 +02004420 EXTENT_LOCKED, 0, NULL)) {
Filipe Mananabd3599a2018-07-12 01:36:43 +01004421 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4422 &btrfs_inode->runtime_flags);
Chris Mason70dec802008-01-29 09:59:12 -05004423 remove_extent_mapping(map, em);
4424 /* once for the rb tree */
4425 free_extent_map(em);
4426 }
4427 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004428 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004429
4430 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004431 free_extent_map(em);
4432 }
Chris Masond1310b22008-01-24 16:13:08 -05004433 }
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004434 return try_release_extent_state(tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004435}
Chris Masond1310b22008-01-24 16:13:08 -05004436
Chris Masonec29ed52011-02-23 16:23:20 -05004437/*
4438 * helper function for fiemap, which doesn't want to see any holes.
4439 * This maps until we find something past 'last'
4440 */
4441static struct extent_map *get_extent_skip_holes(struct inode *inode,
David Sterbae3350e12017-06-23 04:09:57 +02004442 u64 offset, u64 last)
Chris Masonec29ed52011-02-23 16:23:20 -05004443{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004444 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004445 struct extent_map *em;
4446 u64 len;
4447
4448 if (offset >= last)
4449 return NULL;
4450
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304451 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004452 len = last - offset;
4453 if (len == 0)
4454 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004455 len = ALIGN(len, sectorsize);
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02004456 em = btrfs_get_extent_fiemap(BTRFS_I(inode), offset, len);
David Sterbac7040052011-04-19 18:00:01 +02004457 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004458 return em;
4459
4460 /* if this isn't a hole return it */
Nikolay Borisov4a2d25c2017-11-23 10:51:43 +02004461 if (em->block_start != EXTENT_MAP_HOLE)
Chris Masonec29ed52011-02-23 16:23:20 -05004462 return em;
Chris Masonec29ed52011-02-23 16:23:20 -05004463
4464 /* this is a hole, advance to the next extent */
4465 offset = extent_map_end(em);
4466 free_extent_map(em);
4467 if (offset >= last)
4468 break;
4469 }
4470 return NULL;
4471}
4472
Qu Wenruo47518322017-04-07 10:43:15 +08004473/*
4474 * To cache previous fiemap extent
4475 *
4476 * Will be used for merging fiemap extent
4477 */
4478struct fiemap_cache {
4479 u64 offset;
4480 u64 phys;
4481 u64 len;
4482 u32 flags;
4483 bool cached;
4484};
4485
4486/*
4487 * Helper to submit fiemap extent.
4488 *
4489 * Will try to merge current fiemap extent specified by @offset, @phys,
4490 * @len and @flags with cached one.
4491 * And only when we fails to merge, cached one will be submitted as
4492 * fiemap extent.
4493 *
4494 * Return value is the same as fiemap_fill_next_extent().
4495 */
4496static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4497 struct fiemap_cache *cache,
4498 u64 offset, u64 phys, u64 len, u32 flags)
4499{
4500 int ret = 0;
4501
4502 if (!cache->cached)
4503 goto assign;
4504
4505 /*
4506 * Sanity check, extent_fiemap() should have ensured that new
Andrea Gelmini52042d82018-11-28 12:05:13 +01004507 * fiemap extent won't overlap with cached one.
Qu Wenruo47518322017-04-07 10:43:15 +08004508 * Not recoverable.
4509 *
4510 * NOTE: Physical address can overlap, due to compression
4511 */
4512 if (cache->offset + cache->len > offset) {
4513 WARN_ON(1);
4514 return -EINVAL;
4515 }
4516
4517 /*
4518 * Only merges fiemap extents if
4519 * 1) Their logical addresses are continuous
4520 *
4521 * 2) Their physical addresses are continuous
4522 * So truly compressed (physical size smaller than logical size)
4523 * extents won't get merged with each other
4524 *
4525 * 3) Share same flags except FIEMAP_EXTENT_LAST
4526 * So regular extent won't get merged with prealloc extent
4527 */
4528 if (cache->offset + cache->len == offset &&
4529 cache->phys + cache->len == phys &&
4530 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4531 (flags & ~FIEMAP_EXTENT_LAST)) {
4532 cache->len += len;
4533 cache->flags |= flags;
4534 goto try_submit_last;
4535 }
4536
4537 /* Not mergeable, need to submit cached one */
4538 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4539 cache->len, cache->flags);
4540 cache->cached = false;
4541 if (ret)
4542 return ret;
4543assign:
4544 cache->cached = true;
4545 cache->offset = offset;
4546 cache->phys = phys;
4547 cache->len = len;
4548 cache->flags = flags;
4549try_submit_last:
4550 if (cache->flags & FIEMAP_EXTENT_LAST) {
4551 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4552 cache->phys, cache->len, cache->flags);
4553 cache->cached = false;
4554 }
4555 return ret;
4556}
4557
4558/*
Qu Wenruo848c23b2017-06-22 10:01:21 +08004559 * Emit last fiemap cache
Qu Wenruo47518322017-04-07 10:43:15 +08004560 *
Qu Wenruo848c23b2017-06-22 10:01:21 +08004561 * The last fiemap cache may still be cached in the following case:
4562 * 0 4k 8k
4563 * |<- Fiemap range ->|
4564 * |<------------ First extent ----------->|
4565 *
4566 * In this case, the first extent range will be cached but not emitted.
4567 * So we must emit it before ending extent_fiemap().
Qu Wenruo47518322017-04-07 10:43:15 +08004568 */
David Sterba5c5aff92019-03-20 11:29:46 +01004569static int emit_last_fiemap_cache(struct fiemap_extent_info *fieinfo,
Qu Wenruo848c23b2017-06-22 10:01:21 +08004570 struct fiemap_cache *cache)
Qu Wenruo47518322017-04-07 10:43:15 +08004571{
4572 int ret;
4573
4574 if (!cache->cached)
4575 return 0;
4576
Qu Wenruo47518322017-04-07 10:43:15 +08004577 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4578 cache->len, cache->flags);
4579 cache->cached = false;
4580 if (ret > 0)
4581 ret = 0;
4582 return ret;
4583}
4584
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004585int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterba2135fb92017-06-23 04:09:57 +02004586 __u64 start, __u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004587{
Josef Bacik975f84f2010-11-23 19:36:57 +00004588 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004589 u64 off = start;
4590 u64 max = start + len;
4591 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004592 u32 found_type;
4593 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004594 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004595 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004596 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004597 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004598 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004599 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004600 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004601 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004602 struct fiemap_cache cache = { 0 };
David Sterba5911c8f2019-05-15 15:31:04 +02004603 struct ulist *roots;
4604 struct ulist *tmp_ulist;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004605 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004606 u64 em_start = 0;
4607 u64 em_len = 0;
4608 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004609
4610 if (len == 0)
4611 return -EINVAL;
4612
Josef Bacik975f84f2010-11-23 19:36:57 +00004613 path = btrfs_alloc_path();
4614 if (!path)
4615 return -ENOMEM;
4616 path->leave_spinning = 1;
4617
David Sterba5911c8f2019-05-15 15:31:04 +02004618 roots = ulist_alloc(GFP_KERNEL);
4619 tmp_ulist = ulist_alloc(GFP_KERNEL);
4620 if (!roots || !tmp_ulist) {
4621 ret = -ENOMEM;
4622 goto out_free_ulist;
4623 }
4624
Jeff Mahoneyda170662016-06-15 09:22:56 -04004625 start = round_down(start, btrfs_inode_sectorsize(inode));
4626 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004627
Chris Masonec29ed52011-02-23 16:23:20 -05004628 /*
4629 * lookup the last file extent. We're not using i_size here
4630 * because there might be preallocation past i_size
4631 */
David Sterbaf85b7372017-01-20 14:54:07 +01004632 ret = btrfs_lookup_file_extent(NULL, root, path,
4633 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004634 if (ret < 0) {
David Sterba5911c8f2019-05-15 15:31:04 +02004635 goto out_free_ulist;
Liu Bo2d324f52016-05-17 17:21:48 -07004636 } else {
4637 WARN_ON(!ret);
4638 if (ret == 1)
4639 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004640 }
Liu Bo2d324f52016-05-17 17:21:48 -07004641
Josef Bacik975f84f2010-11-23 19:36:57 +00004642 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004643 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004644 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004645
Chris Masonec29ed52011-02-23 16:23:20 -05004646 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004647 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004648 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004649 /* have to trust i_size as the end */
4650 last = (u64)-1;
4651 last_for_get_extent = isize;
4652 } else {
4653 /*
4654 * remember the start of the last extent. There are a
4655 * bunch of different factors that go into the length of the
4656 * extent, so its much less complex to remember where it started
4657 */
4658 last = found_key.offset;
4659 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004660 }
Liu Bofe09e162013-09-22 12:54:23 +08004661 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004662
Chris Masonec29ed52011-02-23 16:23:20 -05004663 /*
4664 * we might have some extents allocated but more delalloc past those
4665 * extents. so, we trust isize unless the start of the last extent is
4666 * beyond isize
4667 */
4668 if (last < isize) {
4669 last = (u64)-1;
4670 last_for_get_extent = isize;
4671 }
4672
David Sterbaff13db42015-12-03 14:30:40 +01004673 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004674 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004675
David Sterbae3350e12017-06-23 04:09:57 +02004676 em = get_extent_skip_holes(inode, start, last_for_get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004677 if (!em)
4678 goto out;
4679 if (IS_ERR(em)) {
4680 ret = PTR_ERR(em);
4681 goto out;
4682 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004683
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004684 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004685 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004686
Chris Masonea8efc72011-03-08 11:54:40 -05004687 /* break if the extent we found is outside the range */
4688 if (em->start >= max || extent_map_end(em) < off)
4689 break;
4690
4691 /*
4692 * get_extent may return an extent that starts before our
4693 * requested range. We have to make sure the ranges
4694 * we return to fiemap always move forward and don't
4695 * overlap, so adjust the offsets here
4696 */
4697 em_start = max(em->start, off);
4698
4699 /*
4700 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004701 * for adjusting the disk offset below. Only do this if the
4702 * extent isn't compressed since our in ram offset may be past
4703 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004704 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004705 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4706 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004707 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004708 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004709 flags = 0;
Filipe Mananaf0986312018-06-20 10:02:30 +01004710 if (em->block_start < EXTENT_MAP_LAST_BYTE)
4711 disko = em->block_start + offset_in_extent;
4712 else
4713 disko = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004714
Chris Masonea8efc72011-03-08 11:54:40 -05004715 /*
4716 * bump off for our next call to get_extent
4717 */
4718 off = extent_map_end(em);
4719 if (off >= max)
4720 end = 1;
4721
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004722 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004723 end = 1;
4724 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004725 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004726 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4727 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004728 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004729 flags |= (FIEMAP_EXTENT_DELALLOC |
4730 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004731 } else if (fieinfo->fi_extents_max) {
4732 u64 bytenr = em->block_start -
4733 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004734
Liu Bofe09e162013-09-22 12:54:23 +08004735 /*
4736 * As btrfs supports shared space, this information
4737 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004738 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4739 * then we're just getting a count and we can skip the
4740 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004741 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06004742 ret = btrfs_check_shared(root,
4743 btrfs_ino(BTRFS_I(inode)),
David Sterba5911c8f2019-05-15 15:31:04 +02004744 bytenr, roots, tmp_ulist);
Josef Bacikdc046b12014-09-10 16:20:45 -04004745 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004746 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004747 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004748 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004749 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004750 }
4751 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4752 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004753 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4754 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004755
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004756 free_extent_map(em);
4757 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004758 if ((em_start >= last) || em_len == (u64)-1 ||
4759 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004760 flags |= FIEMAP_EXTENT_LAST;
4761 end = 1;
4762 }
4763
Chris Masonec29ed52011-02-23 16:23:20 -05004764 /* now scan forward to see if this is really the last extent. */
David Sterbae3350e12017-06-23 04:09:57 +02004765 em = get_extent_skip_holes(inode, off, last_for_get_extent);
Chris Masonec29ed52011-02-23 16:23:20 -05004766 if (IS_ERR(em)) {
4767 ret = PTR_ERR(em);
4768 goto out;
4769 }
4770 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004771 flags |= FIEMAP_EXTENT_LAST;
4772 end = 1;
4773 }
Qu Wenruo47518322017-04-07 10:43:15 +08004774 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4775 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004776 if (ret) {
4777 if (ret == 1)
4778 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004779 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004780 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004781 }
4782out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004783 if (!ret)
David Sterba5c5aff92019-03-20 11:29:46 +01004784 ret = emit_last_fiemap_cache(fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004785 free_extent_map(em);
4786out:
Liu Boa52f4cd2013-05-01 16:23:41 +00004787 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004788 &cached_state);
David Sterba5911c8f2019-05-15 15:31:04 +02004789
4790out_free_ulist:
Colin Ian Kinge02d48e2019-07-05 08:26:24 +01004791 btrfs_free_path(path);
David Sterba5911c8f2019-05-15 15:31:04 +02004792 ulist_free(roots);
4793 ulist_free(tmp_ulist);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004794 return ret;
4795}
4796
Chris Mason727011e2010-08-06 13:21:20 -04004797static void __free_extent_buffer(struct extent_buffer *eb)
4798{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004799 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004800 kmem_cache_free(extent_buffer_cache, eb);
4801}
4802
Josef Bacika26e8c92014-03-28 17:07:27 -04004803int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004804{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004805 return (atomic_read(&eb->io_pages) ||
4806 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4807 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004808}
4809
Miao Xie897ca6e92010-10-26 20:57:29 -04004810/*
David Sterba55ac0132018-07-19 17:24:32 +02004811 * Release all pages attached to the extent buffer.
Miao Xie897ca6e92010-10-26 20:57:29 -04004812 */
David Sterba55ac0132018-07-19 17:24:32 +02004813static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004814{
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004815 int i;
4816 int num_pages;
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004817 int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004818
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004819 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004820
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004821 num_pages = num_extent_pages(eb);
4822 for (i = 0; i < num_pages; i++) {
4823 struct page *page = eb->pages[i];
Miao Xie897ca6e92010-10-26 20:57:29 -04004824
Forrest Liu5d2361d2015-02-09 17:31:45 +08004825 if (!page)
4826 continue;
4827 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004828 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004829 /*
4830 * We do this since we'll remove the pages after we've
4831 * removed the eb from the radix tree, so we could race
4832 * and have this page now attached to the new eb. So
4833 * only clear page_private if it's still connected to
4834 * this eb.
4835 */
4836 if (PagePrivate(page) &&
4837 page->private == (unsigned long)eb) {
4838 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4839 BUG_ON(PageDirty(page));
4840 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004841 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004842 * We need to make sure we haven't be attached
4843 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004844 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004845 ClearPagePrivate(page);
4846 set_page_private(page, 0);
4847 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004848 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004849 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004850
4851 if (mapped)
4852 spin_unlock(&page->mapping->private_lock);
4853
Nicholas D Steeves01327612016-05-19 21:18:45 -04004854 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004855 put_page(page);
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004856 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004857}
4858
4859/*
4860 * Helper for releasing the extent buffer.
4861 */
4862static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4863{
David Sterba55ac0132018-07-19 17:24:32 +02004864 btrfs_release_extent_buffer_pages(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004865 __free_extent_buffer(eb);
4866}
4867
Josef Bacikf28491e2013-12-16 13:24:27 -05004868static struct extent_buffer *
4869__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004870 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004871{
4872 struct extent_buffer *eb = NULL;
4873
Michal Hockod1b5c562015-08-19 14:17:40 +02004874 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004875 eb->start = start;
4876 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004877 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004878 eb->bflags = 0;
4879 rwlock_init(&eb->lock);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004880 atomic_set(&eb->blocking_readers, 0);
David Sterba06297d82019-05-02 16:47:23 +02004881 eb->blocking_writers = 0;
David Sterbaed1b4ed2018-08-24 16:31:17 +02004882 eb->lock_nested = false;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004883 init_waitqueue_head(&eb->write_lock_wq);
4884 init_waitqueue_head(&eb->read_lock_wq);
4885
4886 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4887
4888 spin_lock_init(&eb->refs_lock);
4889 atomic_set(&eb->refs, 1);
4890 atomic_set(&eb->io_pages, 0);
4891
4892 /*
4893 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4894 */
4895 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4896 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4897 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4898
David Sterba843ccf92018-08-24 14:56:28 +02004899#ifdef CONFIG_BTRFS_DEBUG
David Sterbaf3dc24c2019-05-02 16:51:53 +02004900 eb->spinning_writers = 0;
David Sterbaafd495a2018-08-24 15:57:38 +02004901 atomic_set(&eb->spinning_readers, 0);
David Sterba5c9c7992018-08-24 16:15:51 +02004902 atomic_set(&eb->read_locks, 0);
David Sterba00801ae2019-05-02 16:53:47 +02004903 eb->write_locks = 0;
David Sterba843ccf92018-08-24 14:56:28 +02004904#endif
4905
Josef Bacikdb7f3432013-08-07 14:54:37 -04004906 return eb;
4907}
4908
4909struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4910{
David Sterbacc5e31a2018-03-01 18:20:27 +01004911 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004912 struct page *p;
4913 struct extent_buffer *new;
David Sterbacc5e31a2018-03-01 18:20:27 +01004914 int num_pages = num_extent_pages(src);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004915
David Sterba3f556f72014-06-15 03:20:26 +02004916 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004917 if (new == NULL)
4918 return NULL;
4919
4920 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004921 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004922 if (!p) {
4923 btrfs_release_extent_buffer(new);
4924 return NULL;
4925 }
4926 attach_extent_buffer_page(new, p);
4927 WARN_ON(PageDirty(p));
4928 SetPageUptodate(p);
4929 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004930 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004931 }
4932
Josef Bacikdb7f3432013-08-07 14:54:37 -04004933 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004934 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004935
4936 return new;
4937}
4938
Omar Sandoval0f331222015-09-29 20:50:31 -07004939struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4940 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004941{
4942 struct extent_buffer *eb;
David Sterbacc5e31a2018-03-01 18:20:27 +01004943 int num_pages;
4944 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004945
David Sterba3f556f72014-06-15 03:20:26 +02004946 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004947 if (!eb)
4948 return NULL;
4949
David Sterba65ad0102018-06-29 10:56:49 +02004950 num_pages = num_extent_pages(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004951 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004952 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004953 if (!eb->pages[i])
4954 goto err;
4955 }
4956 set_extent_buffer_uptodate(eb);
4957 btrfs_set_header_nritems(eb, 0);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004958 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004959
4960 return eb;
4961err:
4962 for (; i > 0; i--)
4963 __free_page(eb->pages[i - 1]);
4964 __free_extent_buffer(eb);
4965 return NULL;
4966}
4967
Omar Sandoval0f331222015-09-29 20:50:31 -07004968struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004969 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004970{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004971 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004972}
4973
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004974static void check_buffer_tree_ref(struct extent_buffer *eb)
4975{
Chris Mason242e18c2013-01-29 17:49:37 -05004976 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004977 /* the ref bit is tricky. We have to make sure it is set
4978 * if we have the buffer dirty. Otherwise the
4979 * code to free a buffer can end up dropping a dirty
4980 * page
4981 *
4982 * Once the ref bit is set, it won't go away while the
4983 * buffer is dirty or in writeback, and it also won't
4984 * go away while we have the reference count on the
4985 * eb bumped.
4986 *
4987 * We can't just set the ref bit without bumping the
4988 * ref on the eb because free_extent_buffer might
4989 * see the ref bit and try to clear it. If this happens
4990 * free_extent_buffer might end up dropping our original
4991 * ref by mistake and freeing the page before we are able
4992 * to add one more ref.
4993 *
4994 * So bump the ref count first, then set the bit. If someone
4995 * beat us to it, drop the ref we added.
4996 */
Chris Mason242e18c2013-01-29 17:49:37 -05004997 refs = atomic_read(&eb->refs);
4998 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4999 return;
5000
Josef Bacik594831c2012-07-20 16:11:08 -04005001 spin_lock(&eb->refs_lock);
5002 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005003 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04005004 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005005}
5006
Mel Gorman2457aec2014-06-04 16:10:31 -07005007static void mark_extent_buffer_accessed(struct extent_buffer *eb,
5008 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04005009{
David Sterbacc5e31a2018-03-01 18:20:27 +01005010 int num_pages, i;
Josef Bacik5df42352012-03-15 18:24:42 -04005011
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005012 check_buffer_tree_ref(eb);
5013
David Sterba65ad0102018-06-29 10:56:49 +02005014 num_pages = num_extent_pages(eb);
Josef Bacik5df42352012-03-15 18:24:42 -04005015 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005016 struct page *p = eb->pages[i];
5017
Mel Gorman2457aec2014-06-04 16:10:31 -07005018 if (p != accessed)
5019 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04005020 }
5021}
5022
Josef Bacikf28491e2013-12-16 13:24:27 -05005023struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
5024 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005025{
5026 struct extent_buffer *eb;
5027
5028 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05005029 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005030 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005031 if (eb && atomic_inc_not_zero(&eb->refs)) {
5032 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01005033 /*
5034 * Lock our eb's refs_lock to avoid races with
5035 * free_extent_buffer. When we get our eb it might be flagged
5036 * with EXTENT_BUFFER_STALE and another task running
5037 * free_extent_buffer might have seen that flag set,
5038 * eb->refs == 2, that the buffer isn't under IO (dirty and
5039 * writeback flags not set) and it's still in the tree (flag
5040 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
5041 * of decrementing the extent buffer's reference count twice.
5042 * So here we could race and increment the eb's reference count,
5043 * clear its stale flag, mark it as dirty and drop our reference
5044 * before the other task finishes executing free_extent_buffer,
5045 * which would later result in an attempt to free an extent
5046 * buffer that is dirty.
5047 */
5048 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
5049 spin_lock(&eb->refs_lock);
5050 spin_unlock(&eb->refs_lock);
5051 }
Mel Gorman2457aec2014-06-04 16:10:31 -07005052 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005053 return eb;
5054 }
5055 rcu_read_unlock();
5056
5057 return NULL;
5058}
5059
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005060#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
5061struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005062 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005063{
5064 struct extent_buffer *eb, *exists = NULL;
5065 int ret;
5066
5067 eb = find_extent_buffer(fs_info, start);
5068 if (eb)
5069 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04005070 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005071 if (!eb)
Dan Carpenterb6293c82019-12-03 14:24:58 +03005072 return ERR_PTR(-ENOMEM);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005073 eb->fs_info = fs_info;
5074again:
David Sterbae1860a72016-05-09 14:11:38 +02005075 ret = radix_tree_preload(GFP_NOFS);
Dan Carpenterb6293c82019-12-03 14:24:58 +03005076 if (ret) {
5077 exists = ERR_PTR(ret);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005078 goto free_eb;
Dan Carpenterb6293c82019-12-03 14:24:58 +03005079 }
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005080 spin_lock(&fs_info->buffer_lock);
5081 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005082 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005083 spin_unlock(&fs_info->buffer_lock);
5084 radix_tree_preload_end();
5085 if (ret == -EEXIST) {
5086 exists = find_extent_buffer(fs_info, start);
5087 if (exists)
5088 goto free_eb;
5089 else
5090 goto again;
5091 }
5092 check_buffer_tree_ref(eb);
5093 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
5094
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04005095 return eb;
5096free_eb:
5097 btrfs_release_extent_buffer(eb);
5098 return exists;
5099}
5100#endif
5101
Josef Bacikf28491e2013-12-16 13:24:27 -05005102struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02005103 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05005104{
Jeff Mahoneyda170662016-06-15 09:22:56 -04005105 unsigned long len = fs_info->nodesize;
David Sterbacc5e31a2018-03-01 18:20:27 +01005106 int num_pages;
5107 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005108 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005109 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04005110 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05005111 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05005112 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05005113 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005114 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05005115
Jeff Mahoneyda170662016-06-15 09:22:56 -04005116 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07005117 btrfs_err(fs_info, "bad tree block start %llu", start);
5118 return ERR_PTR(-EINVAL);
5119 }
5120
Josef Bacikf28491e2013-12-16 13:24:27 -05005121 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005122 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04005123 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04005124
David Sterba23d79d82014-06-15 02:55:29 +02005125 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04005126 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07005127 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05005128
David Sterba65ad0102018-06-29 10:56:49 +02005129 num_pages = num_extent_pages(eb);
Chris Mason727011e2010-08-06 13:21:20 -04005130 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02005131 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07005132 if (!p) {
5133 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04005134 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005135 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005136
5137 spin_lock(&mapping->private_lock);
5138 if (PagePrivate(p)) {
5139 /*
5140 * We could have already allocated an eb for this page
5141 * and attached one so lets see if we can get a ref on
5142 * the existing eb, and if we can we know it's good and
5143 * we can just return that one, else we know we can just
5144 * overwrite page->private.
5145 */
5146 exists = (struct extent_buffer *)p->private;
5147 if (atomic_inc_not_zero(&exists->refs)) {
5148 spin_unlock(&mapping->private_lock);
5149 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005150 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07005151 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005152 goto free_eb;
5153 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005154 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005155
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005156 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005157 * Do this so attach doesn't complain and we need to
5158 * drop the ref the old guy had.
5159 */
5160 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005161 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005162 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05005163 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005164 attach_extent_buffer_page(eb, p);
5165 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005166 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04005167 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05005168 if (!PageUptodate(p))
5169 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05005170
5171 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005172 * We can't unlock the pages just yet since the extent buffer
5173 * hasn't been properly inserted in the radix tree, this
5174 * opens a race with btree_releasepage which can free a page
5175 * while we are still filling in all pages for the buffer and
5176 * we could crash.
Chris Masoneb14ab82011-02-10 12:35:00 -05005177 */
Chris Masond1310b22008-01-24 16:13:08 -05005178 }
5179 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05005180 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05005181again:
David Sterbae1860a72016-05-09 14:11:38 +02005182 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07005183 if (ret) {
5184 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04005185 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005186 }
Miao Xie19fe0a82010-10-26 20:57:29 -04005187
Josef Bacikf28491e2013-12-16 13:24:27 -05005188 spin_lock(&fs_info->buffer_lock);
5189 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005190 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05005191 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005192 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04005193 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005194 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005195 if (exists)
5196 goto free_eb;
5197 else
Josef Bacik115391d2012-03-09 09:51:43 -05005198 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04005199 }
Chris Mason6af118ce2008-07-22 11:18:07 -04005200 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005201 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005202 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005203
5204 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005205 * Now it's safe to unlock the pages because any calls to
5206 * btree_releasepage will correctly detect that a page belongs to a
5207 * live buffer and won't free them prematurely.
Chris Masoneb14ab82011-02-10 12:35:00 -05005208 */
Nikolay Borisov28187ae2018-07-04 10:24:51 +03005209 for (i = 0; i < num_pages; i++)
5210 unlock_page(eb->pages[i]);
Chris Masond1310b22008-01-24 16:13:08 -05005211 return eb;
5212
Chris Mason6af118ce2008-07-22 11:18:07 -04005213free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005214 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005215 for (i = 0; i < num_pages; i++) {
5216 if (eb->pages[i])
5217 unlock_page(eb->pages[i]);
5218 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005219
Miao Xie897ca6e92010-10-26 20:57:29 -04005220 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005221 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005222}
Chris Masond1310b22008-01-24 16:13:08 -05005223
Josef Bacik3083ee22012-03-09 16:01:49 -05005224static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5225{
5226 struct extent_buffer *eb =
5227 container_of(head, struct extent_buffer, rcu_head);
5228
5229 __free_extent_buffer(eb);
5230}
5231
David Sterbaf7a52a42013-04-26 14:56:29 +00005232static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005233{
Nikolay Borisov07e21c42018-06-27 16:38:23 +03005234 lockdep_assert_held(&eb->refs_lock);
5235
Josef Bacik3083ee22012-03-09 16:01:49 -05005236 WARN_ON(atomic_read(&eb->refs) == 0);
5237 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005238 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005239 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005240
Jan Schmidt815a51c2012-05-16 17:00:02 +02005241 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005242
Josef Bacikf28491e2013-12-16 13:24:27 -05005243 spin_lock(&fs_info->buffer_lock);
5244 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005245 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005246 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005247 } else {
5248 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005249 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005250
5251 /* Should be safe to release our pages at this point */
David Sterba55ac0132018-07-19 17:24:32 +02005252 btrfs_release_extent_buffer_pages(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005253#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005254 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
Josef Bacikbcb7e442015-03-16 17:38:02 -04005255 __free_extent_buffer(eb);
5256 return 1;
5257 }
5258#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005259 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005260 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005261 }
5262 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005263
5264 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005265}
5266
Chris Masond1310b22008-01-24 16:13:08 -05005267void free_extent_buffer(struct extent_buffer *eb)
5268{
Chris Mason242e18c2013-01-29 17:49:37 -05005269 int refs;
5270 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005271 if (!eb)
5272 return;
5273
Chris Mason242e18c2013-01-29 17:49:37 -05005274 while (1) {
5275 refs = atomic_read(&eb->refs);
Nikolay Borisov46cc7752018-10-15 17:04:01 +03005276 if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
5277 || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
5278 refs == 1))
Chris Mason242e18c2013-01-29 17:49:37 -05005279 break;
5280 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5281 if (old == refs)
5282 return;
5283 }
5284
Josef Bacik3083ee22012-03-09 16:01:49 -05005285 spin_lock(&eb->refs_lock);
5286 if (atomic_read(&eb->refs) == 2 &&
5287 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005288 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005289 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5290 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005291
Josef Bacik3083ee22012-03-09 16:01:49 -05005292 /*
5293 * I know this is terrible, but it's temporary until we stop tracking
5294 * the uptodate bits and such for the extent buffers.
5295 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005296 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005297}
Chris Masond1310b22008-01-24 16:13:08 -05005298
Josef Bacik3083ee22012-03-09 16:01:49 -05005299void free_extent_buffer_stale(struct extent_buffer *eb)
5300{
5301 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005302 return;
5303
Josef Bacik3083ee22012-03-09 16:01:49 -05005304 spin_lock(&eb->refs_lock);
5305 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5306
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005307 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005308 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5309 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005310 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005311}
5312
Chris Mason1d4284b2012-03-28 20:31:37 -04005313void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005314{
David Sterbacc5e31a2018-03-01 18:20:27 +01005315 int i;
5316 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005317 struct page *page;
5318
David Sterba65ad0102018-06-29 10:56:49 +02005319 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005320
5321 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005322 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005323 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005324 continue;
5325
Chris Masona61e6f22008-07-22 11:18:08 -04005326 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005327 WARN_ON(!PagePrivate(page));
5328
Chris Masond1310b22008-01-24 16:13:08 -05005329 clear_page_dirty_for_io(page);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005330 xa_lock_irq(&page->mapping->i_pages);
Matthew Wilcox0a943c62017-12-04 10:37:22 -05005331 if (!PageDirty(page))
5332 __xa_clear_mark(&page->mapping->i_pages,
5333 page_index(page), PAGECACHE_TAG_DIRTY);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005334 xa_unlock_irq(&page->mapping->i_pages);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005335 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005336 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005337 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005338 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005339}
Chris Masond1310b22008-01-24 16:13:08 -05005340
Liu Boabb57ef2018-09-14 01:44:42 +08005341bool set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005342{
David Sterbacc5e31a2018-03-01 18:20:27 +01005343 int i;
5344 int num_pages;
Liu Boabb57ef2018-09-14 01:44:42 +08005345 bool was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005346
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005347 check_buffer_tree_ref(eb);
5348
Chris Masonb9473432009-03-13 11:00:37 -04005349 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005350
David Sterba65ad0102018-06-29 10:56:49 +02005351 num_pages = num_extent_pages(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05005352 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005353 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5354
Liu Boabb57ef2018-09-14 01:44:42 +08005355 if (!was_dirty)
5356 for (i = 0; i < num_pages; i++)
5357 set_page_dirty(eb->pages[i]);
Liu Bo51995c32018-09-14 01:46:08 +08005358
5359#ifdef CONFIG_BTRFS_DEBUG
5360 for (i = 0; i < num_pages; i++)
5361 ASSERT(PageDirty(eb->pages[i]));
5362#endif
5363
Chris Masonb9473432009-03-13 11:00:37 -04005364 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005365}
Chris Masond1310b22008-01-24 16:13:08 -05005366
David Sterba69ba3922015-12-03 13:08:59 +01005367void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005368{
David Sterbacc5e31a2018-03-01 18:20:27 +01005369 int i;
Chris Mason1259ab72008-05-12 13:39:03 -04005370 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005371 int num_pages;
Chris Mason1259ab72008-05-12 13:39:03 -04005372
Chris Masonb4ce94d2009-02-04 09:25:08 -05005373 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005374 num_pages = num_extent_pages(eb);
Chris Mason1259ab72008-05-12 13:39:03 -04005375 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005376 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005377 if (page)
5378 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005379 }
Chris Mason1259ab72008-05-12 13:39:03 -04005380}
5381
David Sterba09c25a82015-12-03 13:08:59 +01005382void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005383{
David Sterbacc5e31a2018-03-01 18:20:27 +01005384 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005385 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005386 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005387
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005388 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005389 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005390 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005391 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005392 SetPageUptodate(page);
5393 }
Chris Masond1310b22008-01-24 16:13:08 -05005394}
Chris Masond1310b22008-01-24 16:13:08 -05005395
Nikolay Borisovc2ccfbc2019-04-10 17:24:40 +03005396int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005397{
David Sterbacc5e31a2018-03-01 18:20:27 +01005398 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005399 struct page *page;
5400 int err;
5401 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005402 int locked_pages = 0;
5403 int all_uptodate = 1;
David Sterbacc5e31a2018-03-01 18:20:27 +01005404 int num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005405 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005406 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005407 unsigned long bio_flags = 0;
Nikolay Borisovc2ccfbc2019-04-10 17:24:40 +03005408 struct extent_io_tree *tree = &BTRFS_I(eb->fs_info->btree_inode)->io_tree;
Chris Masona86c12c2008-02-07 10:50:54 -05005409
Chris Masonb4ce94d2009-02-04 09:25:08 -05005410 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005411 return 0;
5412
David Sterba65ad0102018-06-29 10:56:49 +02005413 num_pages = num_extent_pages(eb);
Josef Bacik8436ea912016-09-02 15:40:03 -04005414 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005415 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005416 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005417 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005418 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005419 } else {
5420 lock_page(page);
5421 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005422 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005423 }
5424 /*
5425 * We need to firstly lock all pages to make sure that
5426 * the uptodate bit of our pages won't be affected by
5427 * clear_extent_buffer_uptodate().
5428 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005429 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005430 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005431 if (!PageUptodate(page)) {
5432 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005433 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005434 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005435 }
Liu Bo2571e732016-08-03 12:33:01 -07005436
Chris Masonce9adaa2008-04-09 16:28:12 -04005437 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005438 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005439 goto unlock_exit;
5440 }
5441
Filipe Manana656f30d2014-09-26 12:25:56 +01005442 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005443 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005444 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005445 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005446 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005447
Chris Masonce9adaa2008-04-09 16:28:12 -04005448 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005449 if (ret) {
5450 atomic_dec(&eb->io_pages);
5451 unlock_page(page);
5452 continue;
5453 }
5454
Chris Masonf1885912008-04-09 16:28:12 -04005455 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005456 err = __extent_read_full_page(tree, page,
David Sterba6af49db2017-06-23 04:09:57 +02005457 btree_get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005458 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005459 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005460 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005461 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005462 /*
5463 * We use &bio in above __extent_read_full_page,
5464 * so we ensure that if it returns error, the
5465 * current page fails to add itself to bio and
5466 * it's been unlocked.
5467 *
5468 * We must dec io_pages by ourselves.
5469 */
5470 atomic_dec(&eb->io_pages);
5471 }
Chris Masond1310b22008-01-24 16:13:08 -05005472 } else {
5473 unlock_page(page);
5474 }
5475 }
5476
Jeff Mahoney355808c2011-10-03 23:23:14 -04005477 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005478 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005479 if (err)
5480 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005481 }
Chris Masona86c12c2008-02-07 10:50:54 -05005482
Arne Jansenbb82ab82011-06-10 14:06:53 +02005483 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005484 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005485
Josef Bacik8436ea912016-09-02 15:40:03 -04005486 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005487 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005488 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005489 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005490 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005491 }
Chris Masond3977122009-01-05 21:25:51 -05005492
Chris Masond1310b22008-01-24 16:13:08 -05005493 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005494
5495unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005496 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005497 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005498 page = eb->pages[locked_pages];
5499 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005500 }
5501 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005502}
Chris Masond1310b22008-01-24 16:13:08 -05005503
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005504void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5505 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005506{
5507 size_t cur;
5508 size_t offset;
5509 struct page *page;
5510 char *kaddr;
5511 char *dst = (char *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005512 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005513 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005514
Liu Bof716abd2017-08-09 11:10:16 -06005515 if (start + len > eb->len) {
5516 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5517 eb->start, eb->len, start, len);
5518 memset(dst, 0, len);
5519 return;
5520 }
Chris Masond1310b22008-01-24 16:13:08 -05005521
Johannes Thumshirn70730172018-12-05 15:23:03 +01005522 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005523
Chris Masond3977122009-01-05 21:25:51 -05005524 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005525 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005526
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005527 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005528 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005529 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005530
5531 dst += cur;
5532 len -= cur;
5533 offset = 0;
5534 i++;
5535 }
5536}
Chris Masond1310b22008-01-24 16:13:08 -05005537
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005538int read_extent_buffer_to_user(const struct extent_buffer *eb,
5539 void __user *dstv,
5540 unsigned long start, unsigned long len)
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005541{
5542 size_t cur;
5543 size_t offset;
5544 struct page *page;
5545 char *kaddr;
5546 char __user *dst = (char __user *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005547 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005548 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005549 int ret = 0;
5550
5551 WARN_ON(start > eb->len);
5552 WARN_ON(start + len > eb->start + eb->len);
5553
Johannes Thumshirn70730172018-12-05 15:23:03 +01005554 offset = offset_in_page(start_offset + start);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005555
5556 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005557 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005558
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005559 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005560 kaddr = page_address(page);
5561 if (copy_to_user(dst, kaddr + offset, cur)) {
5562 ret = -EFAULT;
5563 break;
5564 }
5565
5566 dst += cur;
5567 len -= cur;
5568 offset = 0;
5569 i++;
5570 }
5571
5572 return ret;
5573}
5574
Liu Bo415b35a2016-06-17 19:16:21 -07005575/*
5576 * return 0 if the item is found within a page.
5577 * return 1 if the item spans two pages.
5578 * return -EINVAL otherwise.
5579 */
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005580int map_private_extent_buffer(const struct extent_buffer *eb,
5581 unsigned long start, unsigned long min_len,
5582 char **map, unsigned long *map_start,
5583 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005584{
Johannes Thumshirncc2c39d2018-11-28 09:54:54 +01005585 size_t offset;
Chris Masond1310b22008-01-24 16:13:08 -05005586 char *kaddr;
5587 struct page *p;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005588 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005589 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005590 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005591 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005592
Liu Bof716abd2017-08-09 11:10:16 -06005593 if (start + min_len > eb->len) {
5594 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5595 eb->start, eb->len, start, min_len);
5596 return -EINVAL;
5597 }
5598
Chris Masond1310b22008-01-24 16:13:08 -05005599 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005600 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005601
5602 if (i == 0) {
5603 offset = start_offset;
5604 *map_start = 0;
5605 } else {
5606 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005607 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005608 }
Chris Masond3977122009-01-05 21:25:51 -05005609
David Sterbafb85fc92014-07-31 01:03:53 +02005610 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005611 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005612 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005613 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005614 return 0;
5615}
Chris Masond1310b22008-01-24 16:13:08 -05005616
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005617int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5618 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005619{
5620 size_t cur;
5621 size_t offset;
5622 struct page *page;
5623 char *kaddr;
5624 char *ptr = (char *)ptrv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005625 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005626 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005627 int ret = 0;
5628
5629 WARN_ON(start > eb->len);
5630 WARN_ON(start + len > eb->start + eb->len);
5631
Johannes Thumshirn70730172018-12-05 15:23:03 +01005632 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005633
Chris Masond3977122009-01-05 21:25:51 -05005634 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005635 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005636
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005637 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005638
Chris Masona6591712011-07-19 12:04:14 -04005639 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005640 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005641 if (ret)
5642 break;
5643
5644 ptr += cur;
5645 len -= cur;
5646 offset = 0;
5647 i++;
5648 }
5649 return ret;
5650}
Chris Masond1310b22008-01-24 16:13:08 -05005651
David Sterbaf157bf72016-11-09 17:43:38 +01005652void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5653 const void *srcv)
5654{
5655 char *kaddr;
5656
5657 WARN_ON(!PageUptodate(eb->pages[0]));
5658 kaddr = page_address(eb->pages[0]);
5659 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5660 BTRFS_FSID_SIZE);
5661}
5662
5663void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5664{
5665 char *kaddr;
5666
5667 WARN_ON(!PageUptodate(eb->pages[0]));
5668 kaddr = page_address(eb->pages[0]);
5669 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5670 BTRFS_FSID_SIZE);
5671}
5672
Chris Masond1310b22008-01-24 16:13:08 -05005673void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5674 unsigned long start, unsigned long len)
5675{
5676 size_t cur;
5677 size_t offset;
5678 struct page *page;
5679 char *kaddr;
5680 char *src = (char *)srcv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005681 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005682 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005683
5684 WARN_ON(start > eb->len);
5685 WARN_ON(start + len > eb->start + eb->len);
5686
Johannes Thumshirn70730172018-12-05 15:23:03 +01005687 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005688
Chris Masond3977122009-01-05 21:25:51 -05005689 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005690 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005691 WARN_ON(!PageUptodate(page));
5692
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005693 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005694 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005695 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005696
5697 src += cur;
5698 len -= cur;
5699 offset = 0;
5700 i++;
5701 }
5702}
Chris Masond1310b22008-01-24 16:13:08 -05005703
David Sterbab159fa22016-11-08 18:09:03 +01005704void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5705 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005706{
5707 size_t cur;
5708 size_t offset;
5709 struct page *page;
5710 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005711 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005712 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005713
5714 WARN_ON(start > eb->len);
5715 WARN_ON(start + len > eb->start + eb->len);
5716
Johannes Thumshirn70730172018-12-05 15:23:03 +01005717 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005718
Chris Masond3977122009-01-05 21:25:51 -05005719 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005720 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005721 WARN_ON(!PageUptodate(page));
5722
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005723 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005724 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005725 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005726
5727 len -= cur;
5728 offset = 0;
5729 i++;
5730 }
5731}
Chris Masond1310b22008-01-24 16:13:08 -05005732
David Sterba58e80122016-11-08 18:30:31 +01005733void copy_extent_buffer_full(struct extent_buffer *dst,
5734 struct extent_buffer *src)
5735{
5736 int i;
David Sterbacc5e31a2018-03-01 18:20:27 +01005737 int num_pages;
David Sterba58e80122016-11-08 18:30:31 +01005738
5739 ASSERT(dst->len == src->len);
5740
David Sterba65ad0102018-06-29 10:56:49 +02005741 num_pages = num_extent_pages(dst);
David Sterba58e80122016-11-08 18:30:31 +01005742 for (i = 0; i < num_pages; i++)
5743 copy_page(page_address(dst->pages[i]),
5744 page_address(src->pages[i]));
5745}
5746
Chris Masond1310b22008-01-24 16:13:08 -05005747void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5748 unsigned long dst_offset, unsigned long src_offset,
5749 unsigned long len)
5750{
5751 u64 dst_len = dst->len;
5752 size_t cur;
5753 size_t offset;
5754 struct page *page;
5755 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005756 size_t start_offset = offset_in_page(dst->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005757 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005758
5759 WARN_ON(src->len != dst_len);
5760
Johannes Thumshirn70730172018-12-05 15:23:03 +01005761 offset = offset_in_page(start_offset + dst_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005762
Chris Masond3977122009-01-05 21:25:51 -05005763 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005764 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005765 WARN_ON(!PageUptodate(page));
5766
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005767 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005768
Chris Masona6591712011-07-19 12:04:14 -04005769 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005770 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005771
5772 src_offset += cur;
5773 len -= cur;
5774 offset = 0;
5775 i++;
5776 }
5777}
Chris Masond1310b22008-01-24 16:13:08 -05005778
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005779/*
5780 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5781 * given bit number
5782 * @eb: the extent buffer
5783 * @start: offset of the bitmap item in the extent buffer
5784 * @nr: bit number
5785 * @page_index: return index of the page in the extent buffer that contains the
5786 * given bit number
5787 * @page_offset: return offset into the page given by page_index
5788 *
5789 * This helper hides the ugliness of finding the byte in an extent buffer which
5790 * contains a given bit.
5791 */
5792static inline void eb_bitmap_offset(struct extent_buffer *eb,
5793 unsigned long start, unsigned long nr,
5794 unsigned long *page_index,
5795 size_t *page_offset)
5796{
Johannes Thumshirn70730172018-12-05 15:23:03 +01005797 size_t start_offset = offset_in_page(eb->start);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005798 size_t byte_offset = BIT_BYTE(nr);
5799 size_t offset;
5800
5801 /*
5802 * The byte we want is the offset of the extent buffer + the offset of
5803 * the bitmap item in the extent buffer + the offset of the byte in the
5804 * bitmap item.
5805 */
5806 offset = start_offset + start + byte_offset;
5807
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005808 *page_index = offset >> PAGE_SHIFT;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005809 *page_offset = offset_in_page(offset);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005810}
5811
5812/**
5813 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5814 * @eb: the extent buffer
5815 * @start: offset of the bitmap item in the extent buffer
5816 * @nr: bit number to test
5817 */
5818int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5819 unsigned long nr)
5820{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005821 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005822 struct page *page;
5823 unsigned long i;
5824 size_t offset;
5825
5826 eb_bitmap_offset(eb, start, nr, &i, &offset);
5827 page = eb->pages[i];
5828 WARN_ON(!PageUptodate(page));
5829 kaddr = page_address(page);
5830 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5831}
5832
5833/**
5834 * extent_buffer_bitmap_set - set an area of a bitmap
5835 * @eb: the extent buffer
5836 * @start: offset of the bitmap item in the extent buffer
5837 * @pos: bit number of the first bit
5838 * @len: number of bits to set
5839 */
5840void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5841 unsigned long pos, unsigned long len)
5842{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005843 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005844 struct page *page;
5845 unsigned long i;
5846 size_t offset;
5847 const unsigned int size = pos + len;
5848 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005849 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005850
5851 eb_bitmap_offset(eb, start, pos, &i, &offset);
5852 page = eb->pages[i];
5853 WARN_ON(!PageUptodate(page));
5854 kaddr = page_address(page);
5855
5856 while (len >= bits_to_set) {
5857 kaddr[offset] |= mask_to_set;
5858 len -= bits_to_set;
5859 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005860 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005861 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005862 offset = 0;
5863 page = eb->pages[++i];
5864 WARN_ON(!PageUptodate(page));
5865 kaddr = page_address(page);
5866 }
5867 }
5868 if (len) {
5869 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5870 kaddr[offset] |= mask_to_set;
5871 }
5872}
5873
5874
5875/**
5876 * extent_buffer_bitmap_clear - clear an area of a bitmap
5877 * @eb: the extent buffer
5878 * @start: offset of the bitmap item in the extent buffer
5879 * @pos: bit number of the first bit
5880 * @len: number of bits to clear
5881 */
5882void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5883 unsigned long pos, unsigned long len)
5884{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005885 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005886 struct page *page;
5887 unsigned long i;
5888 size_t offset;
5889 const unsigned int size = pos + len;
5890 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005891 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005892
5893 eb_bitmap_offset(eb, start, pos, &i, &offset);
5894 page = eb->pages[i];
5895 WARN_ON(!PageUptodate(page));
5896 kaddr = page_address(page);
5897
5898 while (len >= bits_to_clear) {
5899 kaddr[offset] &= ~mask_to_clear;
5900 len -= bits_to_clear;
5901 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005902 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005903 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005904 offset = 0;
5905 page = eb->pages[++i];
5906 WARN_ON(!PageUptodate(page));
5907 kaddr = page_address(page);
5908 }
5909 }
5910 if (len) {
5911 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5912 kaddr[offset] &= ~mask_to_clear;
5913 }
5914}
5915
Sergei Trofimovich33872062011-04-11 21:52:52 +00005916static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5917{
5918 unsigned long distance = (src > dst) ? src - dst : dst - src;
5919 return distance < len;
5920}
5921
Chris Masond1310b22008-01-24 16:13:08 -05005922static void copy_pages(struct page *dst_page, struct page *src_page,
5923 unsigned long dst_off, unsigned long src_off,
5924 unsigned long len)
5925{
Chris Masona6591712011-07-19 12:04:14 -04005926 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005927 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005928 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005929
Sergei Trofimovich33872062011-04-11 21:52:52 +00005930 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005931 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005932 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005933 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005934 if (areas_overlap(src_off, dst_off, len))
5935 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005936 }
Chris Masond1310b22008-01-24 16:13:08 -05005937
Chris Mason727011e2010-08-06 13:21:20 -04005938 if (must_memmove)
5939 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5940 else
5941 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005942}
5943
5944void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5945 unsigned long src_offset, unsigned long len)
5946{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005947 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005948 size_t cur;
5949 size_t dst_off_in_page;
5950 size_t src_off_in_page;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005951 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005952 unsigned long dst_i;
5953 unsigned long src_i;
5954
5955 if (src_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 src_offset %lu move len %lu dst len %lu",
5958 src_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01005959 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05005960 }
5961 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005962 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005963 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5964 dst_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01005965 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05005966 }
5967
Chris Masond3977122009-01-05 21:25:51 -05005968 while (len > 0) {
Johannes Thumshirn70730172018-12-05 15:23:03 +01005969 dst_off_in_page = offset_in_page(start_offset + dst_offset);
5970 src_off_in_page = offset_in_page(start_offset + src_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005971
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005972 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5973 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005974
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005975 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005976 src_off_in_page));
5977 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005978 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005979
David Sterbafb85fc92014-07-31 01:03:53 +02005980 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005981 dst_off_in_page, src_off_in_page, cur);
5982
5983 src_offset += cur;
5984 dst_offset += cur;
5985 len -= cur;
5986 }
5987}
Chris Masond1310b22008-01-24 16:13:08 -05005988
5989void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5990 unsigned long src_offset, unsigned long len)
5991{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005992 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005993 size_t cur;
5994 size_t dst_off_in_page;
5995 size_t src_off_in_page;
5996 unsigned long dst_end = dst_offset + len - 1;
5997 unsigned long src_end = src_offset + len - 1;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005998 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005999 unsigned long dst_i;
6000 unsigned long src_i;
6001
6002 if (src_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 src_offset %lu move len %lu len %lu",
6005 src_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01006006 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05006007 }
6008 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006009 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006010 "memmove bogus dst_offset %lu move len %lu len %lu",
6011 dst_offset, len, dst->len);
Arnd Bergmann290342f2019-03-25 14:02:25 +01006012 BUG();
Chris Masond1310b22008-01-24 16:13:08 -05006013 }
Chris Mason727011e2010-08-06 13:21:20 -04006014 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05006015 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
6016 return;
6017 }
Chris Masond3977122009-01-05 21:25:51 -05006018 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006019 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
6020 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05006021
Johannes Thumshirn70730172018-12-05 15:23:03 +01006022 dst_off_in_page = offset_in_page(start_offset + dst_end);
6023 src_off_in_page = offset_in_page(start_offset + src_end);
Chris Masond1310b22008-01-24 16:13:08 -05006024
6025 cur = min_t(unsigned long, len, src_off_in_page + 1);
6026 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02006027 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05006028 dst_off_in_page - cur + 1,
6029 src_off_in_page - cur + 1, cur);
6030
6031 dst_end -= cur;
6032 src_end -= cur;
6033 len -= cur;
6034 }
6035}
Chris Mason6af118ce2008-07-22 11:18:07 -04006036
David Sterbaf7a52a42013-04-26 14:56:29 +00006037int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04006038{
Chris Mason6af118ce2008-07-22 11:18:07 -04006039 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04006040
Miao Xie19fe0a82010-10-26 20:57:29 -04006041 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006042 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05006043 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04006044 */
Josef Bacik3083ee22012-03-09 16:01:49 -05006045 spin_lock(&page->mapping->private_lock);
6046 if (!PagePrivate(page)) {
6047 spin_unlock(&page->mapping->private_lock);
6048 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04006049 }
6050
Josef Bacik3083ee22012-03-09 16:01:49 -05006051 eb = (struct extent_buffer *)page->private;
6052 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04006053
Josef Bacik0b32f4b2012-03-13 09:38:00 -04006054 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05006055 * This is a little awful but should be ok, we need to make sure that
6056 * the eb doesn't disappear out from under us while we're looking at
6057 * this page.
6058 */
6059 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04006060 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05006061 spin_unlock(&eb->refs_lock);
6062 spin_unlock(&page->mapping->private_lock);
6063 return 0;
6064 }
6065 spin_unlock(&page->mapping->private_lock);
6066
Josef Bacik3083ee22012-03-09 16:01:49 -05006067 /*
6068 * If tree ref isn't set then we know the ref on this eb is a real ref,
6069 * so just return, this page will likely be freed soon anyway.
6070 */
6071 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
6072 spin_unlock(&eb->refs_lock);
6073 return 0;
6074 }
Josef Bacik3083ee22012-03-09 16:01:49 -05006075
David Sterbaf7a52a42013-04-26 14:56:29 +00006076 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04006077}