blob: 9d52f3b7873237b558b7f948c40cececac72ccd1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
David Sterbac1d7c512018-04-03 19:23:33 +02002
Chris Masond1310b22008-01-24 16:13:08 -05003#include <linux/bitops.h>
4#include <linux/slab.h>
5#include <linux/bio.h>
6#include <linux/mm.h>
Chris Masond1310b22008-01-24 16:13:08 -05007#include <linux/pagemap.h>
8#include <linux/page-flags.h>
Chris Masond1310b22008-01-24 16:13:08 -05009#include <linux/spinlock.h>
10#include <linux/blkdev.h>
11#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050012#include <linux/writeback.h>
13#include <linux/pagevec.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070014#include <linux/prefetch.h>
Dan Magenheimer90a887c2011-05-26 10:01:56 -060015#include <linux/cleancache.h>
Chris Masond1310b22008-01-24 16:13:08 -050016#include "extent_io.h"
17#include "extent_map.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040018#include "ctree.h"
19#include "btrfs_inode.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020020#include "volumes.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010021#include "check-integrity.h"
Josef Bacik0b32f4b2012-03-13 09:38:00 -040022#include "locking.h"
Josef Bacik606686e2012-06-04 14:03:51 -040023#include "rcu-string.h"
Liu Bofe09e162013-09-22 12:54:23 +080024#include "backref.h"
David Sterba6af49db2017-06-23 04:09:57 +020025#include "disk-io.h"
Chris Masond1310b22008-01-24 16:13:08 -050026
Chris Masond1310b22008-01-24 16:13:08 -050027static struct kmem_cache *extent_state_cache;
28static struct kmem_cache *extent_buffer_cache;
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -040029static struct bio_set btrfs_bioset;
Chris Masond1310b22008-01-24 16:13:08 -050030
Filipe Manana27a35072014-07-06 20:09:59 +010031static inline bool extent_state_in_tree(const struct extent_state *state)
32{
33 return !RB_EMPTY_NODE(&state->rb_node);
34}
35
Eric Sandeen6d49ba12013-04-22 16:12:31 +000036#ifdef CONFIG_BTRFS_DEBUG
Chris Masond1310b22008-01-24 16:13:08 -050037static LIST_HEAD(buffers);
38static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040039
Chris Masond3977122009-01-05 21:25:51 -050040static DEFINE_SPINLOCK(leak_lock);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000041
42static inline
43void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
44{
45 unsigned long flags;
46
47 spin_lock_irqsave(&leak_lock, flags);
48 list_add(new, head);
49 spin_unlock_irqrestore(&leak_lock, flags);
50}
51
52static inline
53void btrfs_leak_debug_del(struct list_head *entry)
54{
55 unsigned long flags;
56
57 spin_lock_irqsave(&leak_lock, flags);
58 list_del(entry);
59 spin_unlock_irqrestore(&leak_lock, flags);
60}
61
62static inline
63void btrfs_leak_debug_check(void)
64{
65 struct extent_state *state;
66 struct extent_buffer *eb;
67
68 while (!list_empty(&states)) {
69 state = list_entry(states.next, struct extent_state, leak_list);
David Sterba9ee49a042015-01-14 19:52:13 +010070 pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
Filipe Manana27a35072014-07-06 20:09:59 +010071 state->start, state->end, state->state,
72 extent_state_in_tree(state),
Elena Reshetovab7ac31b2017-03-03 10:55:19 +020073 refcount_read(&state->refs));
Eric Sandeen6d49ba12013-04-22 16:12:31 +000074 list_del(&state->leak_list);
75 kmem_cache_free(extent_state_cache, state);
76 }
77
78 while (!list_empty(&buffers)) {
79 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Liu Boaf2679e2018-01-25 11:02:48 -070080 pr_err("BTRFS: buffer leak start %llu len %lu refs %d bflags %lu\n",
81 eb->start, eb->len, atomic_read(&eb->refs), eb->bflags);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000082 list_del(&eb->leak_list);
83 kmem_cache_free(extent_buffer_cache, eb);
84 }
85}
David Sterba8d599ae2013-04-30 15:22:23 +000086
Josef Bacika5dee372013-12-13 10:02:44 -050087#define btrfs_debug_check_extent_io_range(tree, start, end) \
88 __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
David Sterba8d599ae2013-04-30 15:22:23 +000089static inline void __btrfs_debug_check_extent_io_range(const char *caller,
Josef Bacika5dee372013-12-13 10:02:44 -050090 struct extent_io_tree *tree, u64 start, u64 end)
David Sterba8d599ae2013-04-30 15:22:23 +000091{
Nikolay Borisov65a680f2018-11-01 14:09:49 +020092 struct inode *inode = tree->private_data;
93 u64 isize;
94
95 if (!inode || !is_data_inode(inode))
96 return;
97
98 isize = i_size_read(inode);
99 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
100 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
101 "%s: ino %llu isize %llu odd range [%llu,%llu]",
102 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
103 }
David Sterba8d599ae2013-04-30 15:22:23 +0000104}
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000105#else
106#define btrfs_leak_debug_add(new, head) do {} while (0)
107#define btrfs_leak_debug_del(entry) do {} while (0)
108#define btrfs_leak_debug_check() do {} while (0)
David Sterba8d599ae2013-04-30 15:22:23 +0000109#define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
Chris Mason4bef0842008-09-08 11:18:08 -0400110#endif
Chris Masond1310b22008-01-24 16:13:08 -0500111
Chris Masond1310b22008-01-24 16:13:08 -0500112#define BUFFER_LRU_MAX 64
113
114struct tree_entry {
115 u64 start;
116 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -0500117 struct rb_node rb_node;
118};
119
120struct extent_page_data {
121 struct bio *bio;
122 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -0500123 /* tells writepage not to lock the state bits for this range
124 * it still does the unlocking
125 */
Chris Masonffbd5172009-04-20 15:50:09 -0400126 unsigned int extent_locked:1;
127
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600128 /* tells the submit_bio code to use REQ_SYNC */
Chris Masonffbd5172009-04-20 15:50:09 -0400129 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -0500130};
131
David Sterba57599c72018-03-01 17:56:34 +0100132static int add_extent_changeset(struct extent_state *state, unsigned bits,
Qu Wenruod38ed272015-10-12 14:53:37 +0800133 struct extent_changeset *changeset,
134 int set)
135{
136 int ret;
137
138 if (!changeset)
David Sterba57599c72018-03-01 17:56:34 +0100139 return 0;
Qu Wenruod38ed272015-10-12 14:53:37 +0800140 if (set && (state->state & bits) == bits)
David Sterba57599c72018-03-01 17:56:34 +0100141 return 0;
Qu Wenruofefdc552015-10-12 15:35:38 +0800142 if (!set && (state->state & bits) == 0)
David Sterba57599c72018-03-01 17:56:34 +0100143 return 0;
Qu Wenruod38ed272015-10-12 14:53:37 +0800144 changeset->bytes_changed += state->end - state->start + 1;
David Sterba53d32352017-02-13 13:42:29 +0100145 ret = ulist_add(&changeset->range_changed, state->start, state->end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800146 GFP_ATOMIC);
David Sterba57599c72018-03-01 17:56:34 +0100147 return ret;
Qu Wenruod38ed272015-10-12 14:53:37 +0800148}
149
Qu Wenruobb58eb92019-01-25 13:09:15 +0800150static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
151 unsigned long bio_flags)
152{
153 blk_status_t ret = 0;
154 struct bio_vec *bvec = bio_last_bvec_all(bio);
Linus Torvalds80201fe2019-03-08 14:12:17 -0800155 struct bio_vec bv;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800156 struct extent_io_tree *tree = bio->bi_private;
157 u64 start;
158
Linus Torvalds80201fe2019-03-08 14:12:17 -0800159 mp_bvec_last_segment(bvec, &bv);
160 start = page_offset(bv.bv_page) + bv.bv_offset;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800161
162 bio->bi_private = NULL;
163
164 if (tree->ops)
165 ret = tree->ops->submit_bio_hook(tree->private_data, bio,
166 mirror_num, bio_flags, start);
167 else
168 btrfsic_submit_bio(bio);
169
170 return blk_status_to_errno(ret);
171}
172
Qu Wenruo30659762019-03-20 14:27:42 +0800173/* Cleanup unsubmitted bios */
174static void end_write_bio(struct extent_page_data *epd, int ret)
175{
176 if (epd->bio) {
177 epd->bio->bi_status = errno_to_blk_status(ret);
178 bio_endio(epd->bio);
179 epd->bio = NULL;
180 }
181}
182
Qu Wenruof4340622019-03-20 14:27:41 +0800183/*
184 * Submit bio from extent page data via submit_one_bio
185 *
186 * Return 0 if everything is OK.
187 * Return <0 for error.
188 */
189static int __must_check flush_write_bio(struct extent_page_data *epd)
Qu Wenruobb58eb92019-01-25 13:09:15 +0800190{
Qu Wenruof4340622019-03-20 14:27:41 +0800191 int ret = 0;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800192
Qu Wenruof4340622019-03-20 14:27:41 +0800193 if (epd->bio) {
Qu Wenruobb58eb92019-01-25 13:09:15 +0800194 ret = submit_one_bio(epd->bio, 0, 0);
Qu Wenruof4340622019-03-20 14:27:41 +0800195 /*
196 * Clean up of epd->bio is handled by its endio function.
197 * And endio is either triggered by successful bio execution
198 * or the error handler of submit bio hook.
199 * So at this point, no matter what happened, we don't need
200 * to clean up epd->bio.
201 */
Qu Wenruobb58eb92019-01-25 13:09:15 +0800202 epd->bio = NULL;
203 }
Qu Wenruof4340622019-03-20 14:27:41 +0800204 return ret;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800205}
David Sterbae2932ee2017-06-23 04:16:17 +0200206
Chris Masond1310b22008-01-24 16:13:08 -0500207int __init extent_io_init(void)
208{
David Sterba837e1972012-09-07 03:00:48 -0600209 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200210 sizeof(struct extent_state), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300211 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500212 if (!extent_state_cache)
213 return -ENOMEM;
214
David Sterba837e1972012-09-07 03:00:48 -0600215 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200216 sizeof(struct extent_buffer), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300217 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500218 if (!extent_buffer_cache)
219 goto free_state_cache;
Chris Mason9be33952013-05-17 18:30:14 -0400220
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400221 if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
222 offsetof(struct btrfs_io_bio, bio),
223 BIOSET_NEED_BVECS))
Chris Mason9be33952013-05-17 18:30:14 -0400224 goto free_buffer_cache;
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700225
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400226 if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE))
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700227 goto free_bioset;
228
Chris Masond1310b22008-01-24 16:13:08 -0500229 return 0;
230
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700231free_bioset:
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400232 bioset_exit(&btrfs_bioset);
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700233
Chris Mason9be33952013-05-17 18:30:14 -0400234free_buffer_cache:
235 kmem_cache_destroy(extent_buffer_cache);
236 extent_buffer_cache = NULL;
237
Chris Masond1310b22008-01-24 16:13:08 -0500238free_state_cache:
239 kmem_cache_destroy(extent_state_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400240 extent_state_cache = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500241 return -ENOMEM;
242}
243
David Sterbae67c7182018-02-19 17:24:18 +0100244void __cold extent_io_exit(void)
Chris Masond1310b22008-01-24 16:13:08 -0500245{
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000246 btrfs_leak_debug_check();
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000247
248 /*
249 * Make sure all delayed rcu free are flushed before we
250 * destroy caches.
251 */
252 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +0800253 kmem_cache_destroy(extent_state_cache);
254 kmem_cache_destroy(extent_buffer_cache);
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400255 bioset_exit(&btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -0500256}
257
Qu Wenruoc258d6e2019-03-01 10:47:58 +0800258void extent_io_tree_init(struct btrfs_fs_info *fs_info,
Qu Wenruo43eb5f22019-03-01 10:47:59 +0800259 struct extent_io_tree *tree, unsigned int owner,
260 void *private_data)
Chris Masond1310b22008-01-24 16:13:08 -0500261{
Qu Wenruoc258d6e2019-03-01 10:47:58 +0800262 tree->fs_info = fs_info;
Eric Paris6bef4d32010-02-23 19:43:04 +0000263 tree->state = RB_ROOT;
Chris Masond1310b22008-01-24 16:13:08 -0500264 tree->ops = NULL;
265 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500266 spin_lock_init(&tree->lock);
Josef Bacikc6100a42017-05-05 11:57:13 -0400267 tree->private_data = private_data;
Qu Wenruo43eb5f22019-03-01 10:47:59 +0800268 tree->owner = owner;
Chris Masond1310b22008-01-24 16:13:08 -0500269}
Chris Masond1310b22008-01-24 16:13:08 -0500270
Christoph Hellwigb2950862008-12-02 09:54:17 -0500271static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500272{
273 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500274
Michal Hocko3ba7ab22017-01-09 15:39:02 +0100275 /*
276 * The given mask might be not appropriate for the slab allocator,
277 * drop the unsupported bits
278 */
279 mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
Chris Masond1310b22008-01-24 16:13:08 -0500280 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400281 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500282 return state;
283 state->state = 0;
David Sterba47dc1962016-02-11 13:24:13 +0100284 state->failrec = NULL;
Filipe Manana27a35072014-07-06 20:09:59 +0100285 RB_CLEAR_NODE(&state->rb_node);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000286 btrfs_leak_debug_add(&state->leak_list, &states);
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200287 refcount_set(&state->refs, 1);
Chris Masond1310b22008-01-24 16:13:08 -0500288 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100289 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500290 return state;
291}
Chris Masond1310b22008-01-24 16:13:08 -0500292
Chris Mason4845e442010-05-25 20:56:50 -0400293void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500294{
Chris Masond1310b22008-01-24 16:13:08 -0500295 if (!state)
296 return;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200297 if (refcount_dec_and_test(&state->refs)) {
Filipe Manana27a35072014-07-06 20:09:59 +0100298 WARN_ON(extent_state_in_tree(state));
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000299 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100300 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500301 kmem_cache_free(extent_state_cache, state);
302 }
303}
Chris Masond1310b22008-01-24 16:13:08 -0500304
Filipe Mananaf2071b22014-02-12 15:05:53 +0000305static struct rb_node *tree_insert(struct rb_root *root,
306 struct rb_node *search_start,
307 u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000308 struct rb_node *node,
309 struct rb_node ***p_in,
310 struct rb_node **parent_in)
Chris Masond1310b22008-01-24 16:13:08 -0500311{
Filipe Mananaf2071b22014-02-12 15:05:53 +0000312 struct rb_node **p;
Chris Masond3977122009-01-05 21:25:51 -0500313 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500314 struct tree_entry *entry;
315
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000316 if (p_in && parent_in) {
317 p = *p_in;
318 parent = *parent_in;
319 goto do_insert;
320 }
321
Filipe Mananaf2071b22014-02-12 15:05:53 +0000322 p = search_start ? &search_start : &root->rb_node;
Chris Masond3977122009-01-05 21:25:51 -0500323 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500324 parent = *p;
325 entry = rb_entry(parent, struct tree_entry, rb_node);
326
327 if (offset < entry->start)
328 p = &(*p)->rb_left;
329 else if (offset > entry->end)
330 p = &(*p)->rb_right;
331 else
332 return parent;
333 }
334
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000335do_insert:
Chris Masond1310b22008-01-24 16:13:08 -0500336 rb_link_node(node, parent, p);
337 rb_insert_color(node, root);
338 return NULL;
339}
340
Chris Mason80ea96b2008-02-01 14:51:59 -0500341static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000342 struct rb_node **next_ret,
Nikolay Borisov352646c2019-01-30 16:51:00 +0200343 struct rb_node **prev_ret,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000344 struct rb_node ***p_ret,
345 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500346{
Chris Mason80ea96b2008-02-01 14:51:59 -0500347 struct rb_root *root = &tree->state;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000348 struct rb_node **n = &root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500349 struct rb_node *prev = NULL;
350 struct rb_node *orig_prev = NULL;
351 struct tree_entry *entry;
352 struct tree_entry *prev_entry = NULL;
353
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000354 while (*n) {
355 prev = *n;
356 entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500357 prev_entry = entry;
358
359 if (offset < entry->start)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000360 n = &(*n)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500361 else if (offset > entry->end)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000362 n = &(*n)->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500363 else
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000364 return *n;
Chris Masond1310b22008-01-24 16:13:08 -0500365 }
366
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000367 if (p_ret)
368 *p_ret = n;
369 if (parent_ret)
370 *parent_ret = prev;
371
Nikolay Borisov352646c2019-01-30 16:51:00 +0200372 if (next_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500373 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500374 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500375 prev = rb_next(prev);
376 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
377 }
Nikolay Borisov352646c2019-01-30 16:51:00 +0200378 *next_ret = prev;
Chris Masond1310b22008-01-24 16:13:08 -0500379 prev = orig_prev;
380 }
381
Nikolay Borisov352646c2019-01-30 16:51:00 +0200382 if (prev_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500383 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500384 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500385 prev = rb_prev(prev);
386 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
387 }
Nikolay Borisov352646c2019-01-30 16:51:00 +0200388 *prev_ret = prev;
Chris Masond1310b22008-01-24 16:13:08 -0500389 }
390 return NULL;
391}
392
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000393static inline struct rb_node *
394tree_search_for_insert(struct extent_io_tree *tree,
395 u64 offset,
396 struct rb_node ***p_ret,
397 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500398{
Nikolay Borisov352646c2019-01-30 16:51:00 +0200399 struct rb_node *next= NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500400 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500401
Nikolay Borisov352646c2019-01-30 16:51:00 +0200402 ret = __etree_search(tree, offset, &next, NULL, p_ret, parent_ret);
Chris Masond3977122009-01-05 21:25:51 -0500403 if (!ret)
Nikolay Borisov352646c2019-01-30 16:51:00 +0200404 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500405 return ret;
406}
407
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000408static inline struct rb_node *tree_search(struct extent_io_tree *tree,
409 u64 offset)
410{
411 return tree_search_for_insert(tree, offset, NULL, NULL);
412}
413
Chris Masond1310b22008-01-24 16:13:08 -0500414/*
415 * utility function to look for merge candidates inside a given range.
416 * Any extents with matching state are merged together into a single
417 * extent in the tree. Extents with EXTENT_IO in their state field
418 * are not merged because the end_io handlers need to be able to do
419 * operations on them without sleeping (or doing allocations/splits).
420 *
421 * This should be called with the tree lock held.
422 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000423static void merge_state(struct extent_io_tree *tree,
424 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500425{
426 struct extent_state *other;
427 struct rb_node *other_node;
428
Nikolay Borisov88826792019-03-14 15:28:31 +0200429 if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000430 return;
Chris Masond1310b22008-01-24 16:13:08 -0500431
432 other_node = rb_prev(&state->rb_node);
433 if (other_node) {
434 other = rb_entry(other_node, struct extent_state, rb_node);
435 if (other->end == state->start - 1 &&
436 other->state == state->state) {
Nikolay Borisov5c848192018-11-01 14:09:52 +0200437 if (tree->private_data &&
438 is_data_inode(tree->private_data))
439 btrfs_merge_delalloc_extent(tree->private_data,
440 state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500441 state->start = other->start;
Chris Masond1310b22008-01-24 16:13:08 -0500442 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100443 RB_CLEAR_NODE(&other->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500444 free_extent_state(other);
445 }
446 }
447 other_node = rb_next(&state->rb_node);
448 if (other_node) {
449 other = rb_entry(other_node, struct extent_state, rb_node);
450 if (other->start == state->end + 1 &&
451 other->state == state->state) {
Nikolay Borisov5c848192018-11-01 14:09:52 +0200452 if (tree->private_data &&
453 is_data_inode(tree->private_data))
454 btrfs_merge_delalloc_extent(tree->private_data,
455 state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400456 state->end = other->end;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400457 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100458 RB_CLEAR_NODE(&other->rb_node);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400459 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500460 }
461 }
Chris Masond1310b22008-01-24 16:13:08 -0500462}
463
Xiao Guangrong3150b692011-07-14 03:19:08 +0000464static void set_state_bits(struct extent_io_tree *tree,
Qu Wenruod38ed272015-10-12 14:53:37 +0800465 struct extent_state *state, unsigned *bits,
466 struct extent_changeset *changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000467
Chris Masond1310b22008-01-24 16:13:08 -0500468/*
469 * insert an extent_state struct into the tree. 'bits' are set on the
470 * struct before it is inserted.
471 *
472 * This may return -EEXIST if the extent is already there, in which case the
473 * state struct is freed.
474 *
475 * The tree lock is not taken internally. This is a utility function and
476 * probably isn't what you want to call (see set/clear_extent_bit).
477 */
478static int insert_state(struct extent_io_tree *tree,
479 struct extent_state *state, u64 start, u64 end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000480 struct rb_node ***p,
481 struct rb_node **parent,
Qu Wenruod38ed272015-10-12 14:53:37 +0800482 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500483{
484 struct rb_node *node;
485
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000486 if (end < start)
Frank Holtonefe120a2013-12-20 11:37:06 -0500487 WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200488 end, start);
Chris Masond1310b22008-01-24 16:13:08 -0500489 state->start = start;
490 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400491
Qu Wenruod38ed272015-10-12 14:53:37 +0800492 set_state_bits(tree, state, bits, changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000493
Filipe Mananaf2071b22014-02-12 15:05:53 +0000494 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
Chris Masond1310b22008-01-24 16:13:08 -0500495 if (node) {
496 struct extent_state *found;
497 found = rb_entry(node, struct extent_state, rb_node);
Jeff Mahoney62e85572016-09-20 10:05:01 -0400498 pr_err("BTRFS: found node %llu %llu on insert of %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200499 found->start, found->end, start, end);
Chris Masond1310b22008-01-24 16:13:08 -0500500 return -EEXIST;
501 }
502 merge_state(tree, state);
503 return 0;
504}
505
506/*
507 * split a given extent state struct in two, inserting the preallocated
508 * struct 'prealloc' as the newly created second half. 'split' indicates an
509 * offset inside 'orig' where it should be split.
510 *
511 * Before calling,
512 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
513 * are two extent state structs in the tree:
514 * prealloc: [orig->start, split - 1]
515 * orig: [ split, orig->end ]
516 *
517 * The tree locks are not taken by this function. They need to be held
518 * by the caller.
519 */
520static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
521 struct extent_state *prealloc, u64 split)
522{
523 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400524
Nikolay Borisovabbb55f2018-11-01 14:09:53 +0200525 if (tree->private_data && is_data_inode(tree->private_data))
526 btrfs_split_delalloc_extent(tree->private_data, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400527
Chris Masond1310b22008-01-24 16:13:08 -0500528 prealloc->start = orig->start;
529 prealloc->end = split - 1;
530 prealloc->state = orig->state;
531 orig->start = split;
532
Filipe Mananaf2071b22014-02-12 15:05:53 +0000533 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
534 &prealloc->rb_node, NULL, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500535 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500536 free_extent_state(prealloc);
537 return -EEXIST;
538 }
539 return 0;
540}
541
Li Zefancdc6a392012-03-12 16:39:48 +0800542static struct extent_state *next_state(struct extent_state *state)
543{
544 struct rb_node *next = rb_next(&state->rb_node);
545 if (next)
546 return rb_entry(next, struct extent_state, rb_node);
547 else
548 return NULL;
549}
550
Chris Masond1310b22008-01-24 16:13:08 -0500551/*
552 * utility function to clear some bits in an extent state struct.
Andrea Gelmini52042d82018-11-28 12:05:13 +0100553 * it will optionally wake up anyone waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500554 *
555 * If no bits are set on the state struct after clearing things, the
556 * struct is freed and removed from the tree
557 */
Li Zefancdc6a392012-03-12 16:39:48 +0800558static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
559 struct extent_state *state,
Qu Wenruofefdc552015-10-12 15:35:38 +0800560 unsigned *bits, int wake,
561 struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500562{
Li Zefancdc6a392012-03-12 16:39:48 +0800563 struct extent_state *next;
David Sterba9ee49a042015-01-14 19:52:13 +0100564 unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100565 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500566
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400567 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500568 u64 range = state->end - state->start + 1;
569 WARN_ON(range > tree->dirty_bytes);
570 tree->dirty_bytes -= range;
571 }
Nikolay Borisova36bb5f2018-11-01 14:09:51 +0200572
573 if (tree->private_data && is_data_inode(tree->private_data))
574 btrfs_clear_delalloc_extent(tree->private_data, state, bits);
575
David Sterba57599c72018-03-01 17:56:34 +0100576 ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
577 BUG_ON(ret < 0);
Josef Bacik32c00af2009-10-08 13:34:05 -0400578 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500579 if (wake)
580 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400581 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800582 next = next_state(state);
Filipe Manana27a35072014-07-06 20:09:59 +0100583 if (extent_state_in_tree(state)) {
Chris Masond1310b22008-01-24 16:13:08 -0500584 rb_erase(&state->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100585 RB_CLEAR_NODE(&state->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500586 free_extent_state(state);
587 } else {
588 WARN_ON(1);
589 }
590 } else {
591 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800592 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500593 }
Li Zefancdc6a392012-03-12 16:39:48 +0800594 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500595}
596
Xiao Guangrong82337672011-04-20 06:44:57 +0000597static struct extent_state *
598alloc_extent_state_atomic(struct extent_state *prealloc)
599{
600 if (!prealloc)
601 prealloc = alloc_extent_state(GFP_ATOMIC);
602
603 return prealloc;
604}
605
Eric Sandeen48a3b632013-04-25 20:41:01 +0000606static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400607{
David Sterba05912a32018-07-18 19:23:45 +0200608 struct inode *inode = tree->private_data;
609
610 btrfs_panic(btrfs_sb(inode->i_sb), err,
611 "locking error: extent tree was modified by another thread while locked");
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400612}
613
Chris Masond1310b22008-01-24 16:13:08 -0500614/*
615 * clear some bits on a range in the tree. This may require splitting
616 * or inserting elements in the tree, so the gfp mask is used to
617 * indicate which allocations or sleeping are allowed.
618 *
619 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
620 * the given range from the tree regardless of state (ie for truncate).
621 *
622 * the range [start, end] is inclusive.
623 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100624 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500625 */
David Sterba66b0c882017-10-31 16:30:47 +0100626int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Qu Wenruofefdc552015-10-12 15:35:38 +0800627 unsigned bits, int wake, int delete,
628 struct extent_state **cached_state,
629 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500630{
631 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400632 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500633 struct extent_state *prealloc = NULL;
634 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400635 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500636 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000637 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500638
Josef Bacika5dee372013-12-13 10:02:44 -0500639 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +0800640 trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits);
David Sterba8d599ae2013-04-30 15:22:23 +0000641
Josef Bacik7ee9e442013-06-21 16:37:03 -0400642 if (bits & EXTENT_DELALLOC)
643 bits |= EXTENT_NORESERVE;
644
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400645 if (delete)
646 bits |= ~EXTENT_CTLBITS;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400647
Nikolay Borisov88826792019-03-14 15:28:31 +0200648 if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY))
Josef Bacik2ac55d42010-02-03 19:33:23 +0000649 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500650again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800651 if (!prealloc && gfpflags_allow_blocking(mask)) {
Filipe Mananac7bc6312014-11-03 14:12:57 +0000652 /*
653 * Don't care for allocation failure here because we might end
654 * up not needing the pre-allocated extent state at all, which
655 * is the case if we only have in the tree extent states that
656 * cover our input range and don't cover too any other range.
657 * If we end up needing a new extent state we allocate it later.
658 */
Chris Masond1310b22008-01-24 16:13:08 -0500659 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500660 }
661
Chris Masoncad321a2008-12-17 14:51:42 -0500662 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400663 if (cached_state) {
664 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000665
666 if (clear) {
667 *cached_state = NULL;
668 cached_state = NULL;
669 }
670
Filipe Manana27a35072014-07-06 20:09:59 +0100671 if (cached && extent_state_in_tree(cached) &&
672 cached->start <= start && cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000673 if (clear)
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200674 refcount_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400675 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400676 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400677 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000678 if (clear)
679 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400680 }
Chris Masond1310b22008-01-24 16:13:08 -0500681 /*
682 * this search will find the extents that end after
683 * our range starts
684 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500685 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500686 if (!node)
687 goto out;
688 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400689hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500690 if (state->start > end)
691 goto out;
692 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400693 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500694
Liu Bo04493142012-02-16 18:34:37 +0800695 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800696 if (!(state->state & bits)) {
697 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800698 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800699 }
Liu Bo04493142012-02-16 18:34:37 +0800700
Chris Masond1310b22008-01-24 16:13:08 -0500701 /*
702 * | ---- desired range ---- |
703 * | state | or
704 * | ------------- state -------------- |
705 *
706 * We need to split the extent we found, and may flip
707 * bits on second half.
708 *
709 * If the extent we found extends past our range, we
710 * just split and search again. It'll get split again
711 * the next time though.
712 *
713 * If the extent we found is inside our range, we clear
714 * the desired bit on it.
715 */
716
717 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000718 prealloc = alloc_extent_state_atomic(prealloc);
719 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500720 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400721 if (err)
722 extent_io_tree_panic(tree, err);
723
Chris Masond1310b22008-01-24 16:13:08 -0500724 prealloc = NULL;
725 if (err)
726 goto out;
727 if (state->end <= end) {
Qu Wenruofefdc552015-10-12 15:35:38 +0800728 state = clear_state_bit(tree, state, &bits, wake,
729 changeset);
Liu Bod1ac6e42012-05-10 18:10:39 +0800730 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500731 }
732 goto search_again;
733 }
734 /*
735 * | ---- desired range ---- |
736 * | state |
737 * We need to split the extent, and clear the bit
738 * on the first half
739 */
740 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000741 prealloc = alloc_extent_state_atomic(prealloc);
742 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500743 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400744 if (err)
745 extent_io_tree_panic(tree, err);
746
Chris Masond1310b22008-01-24 16:13:08 -0500747 if (wake)
748 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400749
Qu Wenruofefdc552015-10-12 15:35:38 +0800750 clear_state_bit(tree, prealloc, &bits, wake, changeset);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400751
Chris Masond1310b22008-01-24 16:13:08 -0500752 prealloc = NULL;
753 goto out;
754 }
Chris Mason42daec22009-09-23 19:51:09 -0400755
Qu Wenruofefdc552015-10-12 15:35:38 +0800756 state = clear_state_bit(tree, state, &bits, wake, changeset);
Liu Bo04493142012-02-16 18:34:37 +0800757next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400758 if (last_end == (u64)-1)
759 goto out;
760 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800761 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800762 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500763
764search_again:
765 if (start > end)
766 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500767 spin_unlock(&tree->lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800768 if (gfpflags_allow_blocking(mask))
Chris Masond1310b22008-01-24 16:13:08 -0500769 cond_resched();
770 goto again;
David Sterba7ab5cb22016-04-27 01:02:15 +0200771
772out:
773 spin_unlock(&tree->lock);
774 if (prealloc)
775 free_extent_state(prealloc);
776
777 return 0;
778
Chris Masond1310b22008-01-24 16:13:08 -0500779}
Chris Masond1310b22008-01-24 16:13:08 -0500780
Jeff Mahoney143bede2012-03-01 14:56:26 +0100781static void wait_on_state(struct extent_io_tree *tree,
782 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500783 __releases(tree->lock)
784 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500785{
786 DEFINE_WAIT(wait);
787 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500788 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500789 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500790 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500791 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500792}
793
794/*
795 * waits for one or more bits to clear on a range in the state tree.
796 * The range [start, end] is inclusive.
797 * The tree lock is taken by this function
798 */
David Sterba41074882013-04-29 13:38:46 +0000799static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
800 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500801{
802 struct extent_state *state;
803 struct rb_node *node;
804
Josef Bacika5dee372013-12-13 10:02:44 -0500805 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000806
Chris Masoncad321a2008-12-17 14:51:42 -0500807 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500808again:
809 while (1) {
810 /*
811 * this search will find all the extents that end after
812 * our range starts
813 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500814 node = tree_search(tree, start);
Filipe Mananac50d3e72014-03-31 14:53:25 +0100815process_node:
Chris Masond1310b22008-01-24 16:13:08 -0500816 if (!node)
817 break;
818
819 state = rb_entry(node, struct extent_state, rb_node);
820
821 if (state->start > end)
822 goto out;
823
824 if (state->state & bits) {
825 start = state->start;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200826 refcount_inc(&state->refs);
Chris Masond1310b22008-01-24 16:13:08 -0500827 wait_on_state(tree, state);
828 free_extent_state(state);
829 goto again;
830 }
831 start = state->end + 1;
832
833 if (start > end)
834 break;
835
Filipe Mananac50d3e72014-03-31 14:53:25 +0100836 if (!cond_resched_lock(&tree->lock)) {
837 node = rb_next(node);
838 goto process_node;
839 }
Chris Masond1310b22008-01-24 16:13:08 -0500840 }
841out:
Chris Masoncad321a2008-12-17 14:51:42 -0500842 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500843}
Chris Masond1310b22008-01-24 16:13:08 -0500844
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000845static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500846 struct extent_state *state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800847 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500848{
David Sterba9ee49a042015-01-14 19:52:13 +0100849 unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100850 int ret;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400851
Nikolay Borisove06a1fc2018-11-01 14:09:50 +0200852 if (tree->private_data && is_data_inode(tree->private_data))
853 btrfs_set_delalloc_extent(tree->private_data, state, bits);
854
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400855 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500856 u64 range = state->end - state->start + 1;
857 tree->dirty_bytes += range;
858 }
David Sterba57599c72018-03-01 17:56:34 +0100859 ret = add_extent_changeset(state, bits_to_set, changeset, 1);
860 BUG_ON(ret < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400861 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500862}
863
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100864static void cache_state_if_flags(struct extent_state *state,
865 struct extent_state **cached_ptr,
David Sterba9ee49a042015-01-14 19:52:13 +0100866 unsigned flags)
Chris Mason2c64c532009-09-02 15:04:12 -0400867{
868 if (cached_ptr && !(*cached_ptr)) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100869 if (!flags || (state->state & flags)) {
Chris Mason2c64c532009-09-02 15:04:12 -0400870 *cached_ptr = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200871 refcount_inc(&state->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400872 }
873 }
874}
875
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100876static void cache_state(struct extent_state *state,
877 struct extent_state **cached_ptr)
878{
879 return cache_state_if_flags(state, cached_ptr,
Nikolay Borisov88826792019-03-14 15:28:31 +0200880 EXTENT_LOCKED | EXTENT_BOUNDARY);
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100881}
882
Chris Masond1310b22008-01-24 16:13:08 -0500883/*
Chris Mason1edbb732009-09-02 13:24:36 -0400884 * set some bits on a range in the tree. This may require allocations or
885 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500886 *
Chris Mason1edbb732009-09-02 13:24:36 -0400887 * If any of the exclusive bits are set, this will fail with -EEXIST if some
888 * part of the range already has the desired bits set. The start of the
889 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500890 *
Chris Mason1edbb732009-09-02 13:24:36 -0400891 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500892 */
Chris Mason1edbb732009-09-02 13:24:36 -0400893
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100894static int __must_check
895__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +0100896 unsigned bits, unsigned exclusive_bits,
David Sterba41074882013-04-29 13:38:46 +0000897 u64 *failed_start, struct extent_state **cached_state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800898 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500899{
900 struct extent_state *state;
901 struct extent_state *prealloc = NULL;
902 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000903 struct rb_node **p;
904 struct rb_node *parent;
Chris Masond1310b22008-01-24 16:13:08 -0500905 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500906 u64 last_start;
907 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400908
Josef Bacika5dee372013-12-13 10:02:44 -0500909 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +0800910 trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits);
David Sterba8d599ae2013-04-30 15:22:23 +0000911
Chris Masond1310b22008-01-24 16:13:08 -0500912again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800913 if (!prealloc && gfpflags_allow_blocking(mask)) {
David Sterba059f7912016-04-27 01:03:45 +0200914 /*
915 * Don't care for allocation failure here because we might end
916 * up not needing the pre-allocated extent state at all, which
917 * is the case if we only have in the tree extent states that
918 * cover our input range and don't cover too any other range.
919 * If we end up needing a new extent state we allocate it later.
920 */
Chris Masond1310b22008-01-24 16:13:08 -0500921 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500922 }
923
Chris Masoncad321a2008-12-17 14:51:42 -0500924 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400925 if (cached_state && *cached_state) {
926 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400927 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +0100928 extent_state_in_tree(state)) {
Chris Mason9655d292009-09-02 15:22:30 -0400929 node = &state->rb_node;
930 goto hit_next;
931 }
932 }
Chris Masond1310b22008-01-24 16:13:08 -0500933 /*
934 * this search will find all the extents that end after
935 * our range starts.
936 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000937 node = tree_search_for_insert(tree, start, &p, &parent);
Chris Masond1310b22008-01-24 16:13:08 -0500938 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000939 prealloc = alloc_extent_state_atomic(prealloc);
940 BUG_ON(!prealloc);
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000941 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800942 &p, &parent, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400943 if (err)
944 extent_io_tree_panic(tree, err);
945
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +0000946 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500947 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500948 goto out;
949 }
Chris Masond1310b22008-01-24 16:13:08 -0500950 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400951hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500952 last_start = state->start;
953 last_end = state->end;
954
955 /*
956 * | ---- desired range ---- |
957 * | state |
958 *
959 * Just lock what we found and keep going
960 */
961 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400962 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500963 *failed_start = state->start;
964 err = -EEXIST;
965 goto out;
966 }
Chris Mason42daec22009-09-23 19:51:09 -0400967
Qu Wenruod38ed272015-10-12 14:53:37 +0800968 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400969 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500970 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400971 if (last_end == (u64)-1)
972 goto out;
973 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800974 state = next_state(state);
975 if (start < end && state && state->start == start &&
976 !need_resched())
977 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500978 goto search_again;
979 }
980
981 /*
982 * | ---- desired range ---- |
983 * | state |
984 * or
985 * | ------------- state -------------- |
986 *
987 * We need to split the extent we found, and may flip bits on
988 * second half.
989 *
990 * If the extent we found extends past our
991 * range, we just split and search again. It'll get split
992 * again the next time though.
993 *
994 * If the extent we found is inside our range, we set the
995 * desired bit on it.
996 */
997 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400998 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500999 *failed_start = start;
1000 err = -EEXIST;
1001 goto out;
1002 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001003
1004 prealloc = alloc_extent_state_atomic(prealloc);
1005 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001006 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001007 if (err)
1008 extent_io_tree_panic(tree, err);
1009
Chris Masond1310b22008-01-24 16:13:08 -05001010 prealloc = NULL;
1011 if (err)
1012 goto out;
1013 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001014 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001015 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001016 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -04001017 if (last_end == (u64)-1)
1018 goto out;
1019 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001020 state = next_state(state);
1021 if (start < end && state && state->start == start &&
1022 !need_resched())
1023 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -05001024 }
1025 goto search_again;
1026 }
1027 /*
1028 * | ---- desired range ---- |
1029 * | state | or | state |
1030 *
1031 * There's a hole, we need to insert something in it and
1032 * ignore the extent we found.
1033 */
1034 if (state->start > start) {
1035 u64 this_end;
1036 if (end < last_start)
1037 this_end = end;
1038 else
Chris Masond3977122009-01-05 21:25:51 -05001039 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +00001040
1041 prealloc = alloc_extent_state_atomic(prealloc);
1042 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +00001043
1044 /*
1045 * Avoid to free 'prealloc' if it can be merged with
1046 * the later extent.
1047 */
Chris Masond1310b22008-01-24 16:13:08 -05001048 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001049 NULL, NULL, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001050 if (err)
1051 extent_io_tree_panic(tree, err);
1052
Chris Mason2c64c532009-09-02 15:04:12 -04001053 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001054 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001055 start = this_end + 1;
1056 goto search_again;
1057 }
1058 /*
1059 * | ---- desired range ---- |
1060 * | state |
1061 * We need to split the extent, and set the bit
1062 * on the first half
1063 */
1064 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -04001065 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001066 *failed_start = start;
1067 err = -EEXIST;
1068 goto out;
1069 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001070
1071 prealloc = alloc_extent_state_atomic(prealloc);
1072 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001073 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001074 if (err)
1075 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -05001076
Qu Wenruod38ed272015-10-12 14:53:37 +08001077 set_state_bits(tree, prealloc, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001078 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001079 merge_state(tree, prealloc);
1080 prealloc = NULL;
1081 goto out;
1082 }
1083
David Sterbab5a4ba142016-04-27 01:02:15 +02001084search_again:
1085 if (start > end)
1086 goto out;
1087 spin_unlock(&tree->lock);
1088 if (gfpflags_allow_blocking(mask))
1089 cond_resched();
1090 goto again;
Chris Masond1310b22008-01-24 16:13:08 -05001091
1092out:
Chris Masoncad321a2008-12-17 14:51:42 -05001093 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001094 if (prealloc)
1095 free_extent_state(prealloc);
1096
1097 return err;
1098
Chris Masond1310b22008-01-24 16:13:08 -05001099}
Chris Masond1310b22008-01-24 16:13:08 -05001100
David Sterba41074882013-04-29 13:38:46 +00001101int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001102 unsigned bits, u64 * failed_start,
David Sterba41074882013-04-29 13:38:46 +00001103 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001104{
1105 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001106 cached_state, mask, NULL);
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001107}
1108
1109
Josef Bacik462d6fa2011-09-26 13:56:12 -04001110/**
Liu Bo10983f22012-07-11 15:26:19 +08001111 * convert_extent_bit - convert all bits in a given range from one bit to
1112 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -04001113 * @tree: the io tree to search
1114 * @start: the start offset in bytes
1115 * @end: the end offset in bytes (inclusive)
1116 * @bits: the bits to set in this range
1117 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001118 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001119 *
1120 * This will go through and set bits for the given range. If any states exist
1121 * already in this range they are set with the given bit and cleared of the
1122 * clear_bits. This is only meant to be used by things that are mergeable, ie
1123 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1124 * boundary bits like LOCK.
David Sterba210aa272016-04-26 23:54:39 +02001125 *
1126 * All allocations are done with GFP_NOFS.
Josef Bacik462d6fa2011-09-26 13:56:12 -04001127 */
1128int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001129 unsigned bits, unsigned clear_bits,
David Sterba210aa272016-04-26 23:54:39 +02001130 struct extent_state **cached_state)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001131{
1132 struct extent_state *state;
1133 struct extent_state *prealloc = NULL;
1134 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001135 struct rb_node **p;
1136 struct rb_node *parent;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001137 int err = 0;
1138 u64 last_start;
1139 u64 last_end;
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001140 bool first_iteration = true;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001141
Josef Bacika5dee372013-12-13 10:02:44 -05001142 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +08001143 trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits,
1144 clear_bits);
David Sterba8d599ae2013-04-30 15:22:23 +00001145
Josef Bacik462d6fa2011-09-26 13:56:12 -04001146again:
David Sterba210aa272016-04-26 23:54:39 +02001147 if (!prealloc) {
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001148 /*
1149 * Best effort, don't worry if extent state allocation fails
1150 * here for the first iteration. We might have a cached state
1151 * that matches exactly the target range, in which case no
1152 * extent state allocations are needed. We'll only know this
1153 * after locking the tree.
1154 */
David Sterba210aa272016-04-26 23:54:39 +02001155 prealloc = alloc_extent_state(GFP_NOFS);
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001156 if (!prealloc && !first_iteration)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001157 return -ENOMEM;
1158 }
1159
1160 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001161 if (cached_state && *cached_state) {
1162 state = *cached_state;
1163 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +01001164 extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001165 node = &state->rb_node;
1166 goto hit_next;
1167 }
1168 }
1169
Josef Bacik462d6fa2011-09-26 13:56:12 -04001170 /*
1171 * this search will find all the extents that end after
1172 * our range starts.
1173 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001174 node = tree_search_for_insert(tree, start, &p, &parent);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001175 if (!node) {
1176 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001177 if (!prealloc) {
1178 err = -ENOMEM;
1179 goto out;
1180 }
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001181 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001182 &p, &parent, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001183 if (err)
1184 extent_io_tree_panic(tree, err);
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001185 cache_state(prealloc, cached_state);
1186 prealloc = NULL;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001187 goto out;
1188 }
1189 state = rb_entry(node, struct extent_state, rb_node);
1190hit_next:
1191 last_start = state->start;
1192 last_end = state->end;
1193
1194 /*
1195 * | ---- desired range ---- |
1196 * | state |
1197 *
1198 * Just lock what we found and keep going
1199 */
1200 if (state->start == start && state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001201 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001202 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001203 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001204 if (last_end == (u64)-1)
1205 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001206 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001207 if (start < end && state && state->start == start &&
1208 !need_resched())
1209 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001210 goto search_again;
1211 }
1212
1213 /*
1214 * | ---- desired range ---- |
1215 * | state |
1216 * or
1217 * | ------------- state -------------- |
1218 *
1219 * We need to split the extent we found, and may flip bits on
1220 * second half.
1221 *
1222 * If the extent we found extends past our
1223 * range, we just split and search again. It'll get split
1224 * again the next time though.
1225 *
1226 * If the extent we found is inside our range, we set the
1227 * desired bit on it.
1228 */
1229 if (state->start < start) {
1230 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001231 if (!prealloc) {
1232 err = -ENOMEM;
1233 goto out;
1234 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001235 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001236 if (err)
1237 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001238 prealloc = NULL;
1239 if (err)
1240 goto out;
1241 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001242 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001243 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001244 state = clear_state_bit(tree, state, &clear_bits, 0,
1245 NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001246 if (last_end == (u64)-1)
1247 goto out;
1248 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001249 if (start < end && state && state->start == start &&
1250 !need_resched())
1251 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001252 }
1253 goto search_again;
1254 }
1255 /*
1256 * | ---- desired range ---- |
1257 * | state | or | state |
1258 *
1259 * There's a hole, we need to insert something in it and
1260 * ignore the extent we found.
1261 */
1262 if (state->start > start) {
1263 u64 this_end;
1264 if (end < last_start)
1265 this_end = end;
1266 else
1267 this_end = last_start - 1;
1268
1269 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001270 if (!prealloc) {
1271 err = -ENOMEM;
1272 goto out;
1273 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001274
1275 /*
1276 * Avoid to free 'prealloc' if it can be merged with
1277 * the later extent.
1278 */
1279 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001280 NULL, NULL, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001281 if (err)
1282 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001283 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001284 prealloc = NULL;
1285 start = this_end + 1;
1286 goto search_again;
1287 }
1288 /*
1289 * | ---- desired range ---- |
1290 * | state |
1291 * We need to split the extent, and set the bit
1292 * on the first half
1293 */
1294 if (state->start <= end && state->end > end) {
1295 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001296 if (!prealloc) {
1297 err = -ENOMEM;
1298 goto out;
1299 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001300
1301 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001302 if (err)
1303 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001304
Qu Wenruod38ed272015-10-12 14:53:37 +08001305 set_state_bits(tree, prealloc, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001306 cache_state(prealloc, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001307 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001308 prealloc = NULL;
1309 goto out;
1310 }
1311
Josef Bacik462d6fa2011-09-26 13:56:12 -04001312search_again:
1313 if (start > end)
1314 goto out;
1315 spin_unlock(&tree->lock);
David Sterba210aa272016-04-26 23:54:39 +02001316 cond_resched();
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001317 first_iteration = false;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001318 goto again;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001319
1320out:
1321 spin_unlock(&tree->lock);
1322 if (prealloc)
1323 free_extent_state(prealloc);
1324
1325 return err;
1326}
1327
Chris Masond1310b22008-01-24 16:13:08 -05001328/* wrappers around set/clear extent bit */
Qu Wenruod38ed272015-10-12 14:53:37 +08001329int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba2c53b912016-04-26 23:54:39 +02001330 unsigned bits, struct extent_changeset *changeset)
Qu Wenruod38ed272015-10-12 14:53:37 +08001331{
1332 /*
1333 * We don't support EXTENT_LOCKED yet, as current changeset will
1334 * record any bits changed, so for EXTENT_LOCKED case, it will
1335 * either fail with -EEXIST or changeset will record the whole
1336 * range.
1337 */
1338 BUG_ON(bits & EXTENT_LOCKED);
1339
David Sterba2c53b912016-04-26 23:54:39 +02001340 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
Qu Wenruod38ed272015-10-12 14:53:37 +08001341 changeset);
1342}
1343
Qu Wenruofefdc552015-10-12 15:35:38 +08001344int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1345 unsigned bits, int wake, int delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001346 struct extent_state **cached)
Qu Wenruofefdc552015-10-12 15:35:38 +08001347{
1348 return __clear_extent_bit(tree, start, end, bits, wake, delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001349 cached, GFP_NOFS, NULL);
Qu Wenruofefdc552015-10-12 15:35:38 +08001350}
1351
Qu Wenruofefdc552015-10-12 15:35:38 +08001352int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaf734c442016-04-26 23:54:39 +02001353 unsigned bits, struct extent_changeset *changeset)
Qu Wenruofefdc552015-10-12 15:35:38 +08001354{
1355 /*
1356 * Don't support EXTENT_LOCKED case, same reason as
1357 * set_record_extent_bits().
1358 */
1359 BUG_ON(bits & EXTENT_LOCKED);
1360
David Sterbaf734c442016-04-26 23:54:39 +02001361 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
Qu Wenruofefdc552015-10-12 15:35:38 +08001362 changeset);
1363}
1364
Chris Masond352ac62008-09-29 15:18:18 -04001365/*
1366 * either insert or lock state struct between start and end use mask to tell
1367 * us if waiting is desired.
1368 */
Chris Mason1edbb732009-09-02 13:24:36 -04001369int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaff13db42015-12-03 14:30:40 +01001370 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001371{
1372 int err;
1373 u64 failed_start;
David Sterba9ee49a042015-01-14 19:52:13 +01001374
Chris Masond1310b22008-01-24 16:13:08 -05001375 while (1) {
David Sterbaff13db42015-12-03 14:30:40 +01001376 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001377 EXTENT_LOCKED, &failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001378 cached_state, GFP_NOFS, NULL);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001379 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001380 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1381 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001382 } else
Chris Masond1310b22008-01-24 16:13:08 -05001383 break;
Chris Masond1310b22008-01-24 16:13:08 -05001384 WARN_ON(start > end);
1385 }
1386 return err;
1387}
Chris Masond1310b22008-01-24 16:13:08 -05001388
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001389int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001390{
1391 int err;
1392 u64 failed_start;
1393
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001394 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
Qu Wenruod38ed272015-10-12 14:53:37 +08001395 &failed_start, NULL, GFP_NOFS, NULL);
Yan Zheng66435582008-10-30 14:19:50 -04001396 if (err == -EEXIST) {
1397 if (failed_start > start)
1398 clear_extent_bit(tree, start, failed_start - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001399 EXTENT_LOCKED, 1, 0, NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001400 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001401 }
Josef Bacik25179202008-10-29 14:49:05 -04001402 return 1;
1403}
Josef Bacik25179202008-10-29 14:49:05 -04001404
David Sterbabd1fa4f2015-12-03 13:08:59 +01001405void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001406{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001407 unsigned long index = start >> PAGE_SHIFT;
1408 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001409 struct page *page;
1410
1411 while (index <= end_index) {
1412 page = find_get_page(inode->i_mapping, index);
1413 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1414 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001415 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001416 index++;
1417 }
Chris Mason4adaa612013-03-26 13:07:00 -04001418}
1419
David Sterbaf6311572015-12-03 13:08:59 +01001420void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001421{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001422 unsigned long index = start >> PAGE_SHIFT;
1423 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001424 struct page *page;
1425
1426 while (index <= end_index) {
1427 page = find_get_page(inode->i_mapping, index);
1428 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Mason4adaa612013-03-26 13:07:00 -04001429 __set_page_dirty_nobuffers(page);
Konstantin Khebnikov8d386332015-02-11 15:26:55 -08001430 account_page_redirty(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001431 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001432 index++;
1433 }
Chris Mason4adaa612013-03-26 13:07:00 -04001434}
1435
Chris Masond352ac62008-09-29 15:18:18 -04001436/* find the first state struct with 'bits' set after 'start', and
1437 * return it. tree->lock must be held. NULL will returned if
1438 * nothing was found after 'start'
1439 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001440static struct extent_state *
1441find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +01001442 u64 start, unsigned bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001443{
1444 struct rb_node *node;
1445 struct extent_state *state;
1446
1447 /*
1448 * this search will find all the extents that end after
1449 * our range starts.
1450 */
1451 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001452 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001453 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001454
Chris Masond3977122009-01-05 21:25:51 -05001455 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001456 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001457 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001458 return state;
Chris Masond3977122009-01-05 21:25:51 -05001459
Chris Masond7fc6402008-02-18 12:12:38 -05001460 node = rb_next(node);
1461 if (!node)
1462 break;
1463 }
1464out:
1465 return NULL;
1466}
Chris Masond7fc6402008-02-18 12:12:38 -05001467
Chris Masond352ac62008-09-29 15:18:18 -04001468/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001469 * find the first offset in the io tree with 'bits' set. zero is
1470 * returned if we find something, and *start_ret and *end_ret are
1471 * set to reflect the state struct that was found.
1472 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001473 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001474 */
1475int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba9ee49a042015-01-14 19:52:13 +01001476 u64 *start_ret, u64 *end_ret, unsigned bits,
Josef Bacike6138872012-09-27 17:07:30 -04001477 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001478{
1479 struct extent_state *state;
1480 int ret = 1;
1481
1482 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001483 if (cached_state && *cached_state) {
1484 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001485 if (state->end == start - 1 && extent_state_in_tree(state)) {
Liu Bo9688e9a2018-08-23 03:14:53 +08001486 while ((state = next_state(state)) != NULL) {
Josef Bacike6138872012-09-27 17:07:30 -04001487 if (state->state & bits)
1488 goto got_it;
Josef Bacike6138872012-09-27 17:07:30 -04001489 }
1490 free_extent_state(*cached_state);
1491 *cached_state = NULL;
1492 goto out;
1493 }
1494 free_extent_state(*cached_state);
1495 *cached_state = NULL;
1496 }
1497
Xiao Guangrong69261c42011-07-14 03:19:45 +00001498 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001499got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001500 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001501 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001502 *start_ret = state->start;
1503 *end_ret = state->end;
1504 ret = 0;
1505 }
Josef Bacike6138872012-09-27 17:07:30 -04001506out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001507 spin_unlock(&tree->lock);
1508 return ret;
1509}
1510
1511/*
Chris Masond352ac62008-09-29 15:18:18 -04001512 * find a contiguous range of bytes in the file marked as delalloc, not
1513 * more than 'max_bytes'. start and end are used to return the range,
1514 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001515 * true is returned if we find something, false if nothing was in the tree
Chris Masond352ac62008-09-29 15:18:18 -04001516 */
Lu Fengqi3522e902018-11-29 11:33:38 +08001517static noinline bool find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001518 u64 *start, u64 *end, u64 max_bytes,
1519 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001520{
1521 struct rb_node *node;
1522 struct extent_state *state;
1523 u64 cur_start = *start;
Lu Fengqi3522e902018-11-29 11:33:38 +08001524 bool found = false;
Chris Masond1310b22008-01-24 16:13:08 -05001525 u64 total_bytes = 0;
1526
Chris Masoncad321a2008-12-17 14:51:42 -05001527 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001528
Chris Masond1310b22008-01-24 16:13:08 -05001529 /*
1530 * this search will find all the extents that end after
1531 * our range starts.
1532 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001533 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001534 if (!node) {
Lu Fengqi3522e902018-11-29 11:33:38 +08001535 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001536 goto out;
1537 }
1538
Chris Masond3977122009-01-05 21:25:51 -05001539 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001540 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001541 if (found && (state->start != cur_start ||
1542 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001543 goto out;
1544 }
1545 if (!(state->state & EXTENT_DELALLOC)) {
1546 if (!found)
1547 *end = state->end;
1548 goto out;
1549 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001550 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001551 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001552 *cached_state = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +02001553 refcount_inc(&state->refs);
Josef Bacikc2a128d2010-02-02 21:19:11 +00001554 }
Lu Fengqi3522e902018-11-29 11:33:38 +08001555 found = true;
Chris Masond1310b22008-01-24 16:13:08 -05001556 *end = state->end;
1557 cur_start = state->end + 1;
1558 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001559 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001560 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001561 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001562 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001563 break;
1564 }
1565out:
Chris Masoncad321a2008-12-17 14:51:42 -05001566 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001567 return found;
1568}
1569
Liu Boda2c7002017-02-10 16:41:05 +01001570static int __process_pages_contig(struct address_space *mapping,
1571 struct page *locked_page,
1572 pgoff_t start_index, pgoff_t end_index,
1573 unsigned long page_ops, pgoff_t *index_ret);
1574
Jeff Mahoney143bede2012-03-01 14:56:26 +01001575static noinline void __unlock_for_delalloc(struct inode *inode,
1576 struct page *locked_page,
1577 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001578{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001579 unsigned long index = start >> PAGE_SHIFT;
1580 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001581
Liu Bo76c00212017-02-10 16:42:14 +01001582 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001583 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001584 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001585
Liu Bo76c00212017-02-10 16:42:14 +01001586 __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1587 PAGE_UNLOCK, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -04001588}
1589
1590static noinline int lock_delalloc_pages(struct inode *inode,
1591 struct page *locked_page,
1592 u64 delalloc_start,
1593 u64 delalloc_end)
1594{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001595 unsigned long index = delalloc_start >> PAGE_SHIFT;
Liu Bo76c00212017-02-10 16:42:14 +01001596 unsigned long index_ret = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001597 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001598 int ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001599
Liu Bo76c00212017-02-10 16:42:14 +01001600 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001601 if (index == locked_page->index && index == end_index)
1602 return 0;
1603
Liu Bo76c00212017-02-10 16:42:14 +01001604 ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1605 end_index, PAGE_LOCK, &index_ret);
1606 if (ret == -EAGAIN)
1607 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1608 (u64)index_ret << PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001609 return ret;
1610}
1611
1612/*
Lu Fengqi3522e902018-11-29 11:33:38 +08001613 * Find and lock a contiguous range of bytes in the file marked as delalloc, no
1614 * more than @max_bytes. @Start and @end are used to return the range,
Chris Masonc8b97812008-10-29 14:49:59 -04001615 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001616 * Return: true if we find something
1617 * false if nothing was in the tree
Chris Masonc8b97812008-10-29 14:49:59 -04001618 */
Johannes Thumshirnce9f9672018-11-19 10:38:17 +01001619EXPORT_FOR_TESTS
Lu Fengqi3522e902018-11-29 11:33:38 +08001620noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
Josef Bacik294e30f2013-10-09 12:00:56 -04001621 struct extent_io_tree *tree,
1622 struct page *locked_page, u64 *start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03001623 u64 *end)
Chris Masonc8b97812008-10-29 14:49:59 -04001624{
Nikolay Borisov917aace2018-10-26 14:43:20 +03001625 u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001626 u64 delalloc_start;
1627 u64 delalloc_end;
Lu Fengqi3522e902018-11-29 11:33:38 +08001628 bool found;
Chris Mason9655d292009-09-02 15:22:30 -04001629 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001630 int ret;
1631 int loops = 0;
1632
1633again:
1634 /* step one, find a bunch of delalloc bytes starting at start */
1635 delalloc_start = *start;
1636 delalloc_end = 0;
1637 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001638 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001639 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001640 *start = delalloc_start;
1641 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001642 free_extent_state(cached_state);
Lu Fengqi3522e902018-11-29 11:33:38 +08001643 return false;
Chris Masonc8b97812008-10-29 14:49:59 -04001644 }
1645
1646 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001647 * start comes from the offset of locked_page. We have to lock
1648 * pages in order, so we can't process delalloc bytes before
1649 * locked_page
1650 */
Chris Masond3977122009-01-05 21:25:51 -05001651 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001652 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001653
1654 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001655 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001656 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001657 if (delalloc_end + 1 - delalloc_start > max_bytes)
1658 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001659
Chris Masonc8b97812008-10-29 14:49:59 -04001660 /* step two, lock all the pages after the page that has start */
1661 ret = lock_delalloc_pages(inode, locked_page,
1662 delalloc_start, delalloc_end);
Nikolay Borisov9bfd61d2018-10-26 14:43:21 +03001663 ASSERT(!ret || ret == -EAGAIN);
Chris Masonc8b97812008-10-29 14:49:59 -04001664 if (ret == -EAGAIN) {
1665 /* some of the pages are gone, lets avoid looping by
1666 * shortening the size of the delalloc range we're searching
1667 */
Chris Mason9655d292009-09-02 15:22:30 -04001668 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001669 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001670 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001671 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001672 loops = 1;
1673 goto again;
1674 } else {
Lu Fengqi3522e902018-11-29 11:33:38 +08001675 found = false;
Chris Masonc8b97812008-10-29 14:49:59 -04001676 goto out_failed;
1677 }
1678 }
Chris Masonc8b97812008-10-29 14:49:59 -04001679
1680 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001681 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001682
1683 /* then test to make sure it is all still delalloc */
1684 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001685 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001686 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001687 unlock_extent_cached(tree, delalloc_start, delalloc_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001688 &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001689 __unlock_for_delalloc(inode, locked_page,
1690 delalloc_start, delalloc_end);
1691 cond_resched();
1692 goto again;
1693 }
Chris Mason9655d292009-09-02 15:22:30 -04001694 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001695 *start = delalloc_start;
1696 *end = delalloc_end;
1697out_failed:
1698 return found;
1699}
1700
Liu Boda2c7002017-02-10 16:41:05 +01001701static int __process_pages_contig(struct address_space *mapping,
1702 struct page *locked_page,
1703 pgoff_t start_index, pgoff_t end_index,
1704 unsigned long page_ops, pgoff_t *index_ret)
Chris Masonc8b97812008-10-29 14:49:59 -04001705{
Liu Bo873695b2017-02-02 17:49:22 -08001706 unsigned long nr_pages = end_index - start_index + 1;
Liu Boda2c7002017-02-10 16:41:05 +01001707 unsigned long pages_locked = 0;
Liu Bo873695b2017-02-02 17:49:22 -08001708 pgoff_t index = start_index;
Chris Masonc8b97812008-10-29 14:49:59 -04001709 struct page *pages[16];
Liu Bo873695b2017-02-02 17:49:22 -08001710 unsigned ret;
Liu Boda2c7002017-02-10 16:41:05 +01001711 int err = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001712 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001713
Liu Boda2c7002017-02-10 16:41:05 +01001714 if (page_ops & PAGE_LOCK) {
1715 ASSERT(page_ops == PAGE_LOCK);
1716 ASSERT(index_ret && *index_ret == start_index);
1717 }
1718
Filipe Manana704de492014-10-06 22:14:22 +01001719 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
Liu Bo873695b2017-02-02 17:49:22 -08001720 mapping_set_error(mapping, -EIO);
Filipe Manana704de492014-10-06 22:14:22 +01001721
Chris Masond3977122009-01-05 21:25:51 -05001722 while (nr_pages > 0) {
Liu Bo873695b2017-02-02 17:49:22 -08001723 ret = find_get_pages_contig(mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001724 min_t(unsigned long,
1725 nr_pages, ARRAY_SIZE(pages)), pages);
Liu Boda2c7002017-02-10 16:41:05 +01001726 if (ret == 0) {
1727 /*
1728 * Only if we're going to lock these pages,
1729 * can we find nothing at @index.
1730 */
1731 ASSERT(page_ops & PAGE_LOCK);
Liu Bo49d4a332017-03-06 18:20:56 -08001732 err = -EAGAIN;
1733 goto out;
Liu Boda2c7002017-02-10 16:41:05 +01001734 }
Chris Mason8b62b722009-09-02 16:53:46 -04001735
Liu Boda2c7002017-02-10 16:41:05 +01001736 for (i = 0; i < ret; i++) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001737 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001738 SetPagePrivate2(pages[i]);
1739
Chris Masonc8b97812008-10-29 14:49:59 -04001740 if (pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001741 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001742 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001743 continue;
1744 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001745 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001746 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001747 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001748 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001749 if (page_ops & PAGE_SET_ERROR)
1750 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001751 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001752 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001753 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001754 unlock_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001755 if (page_ops & PAGE_LOCK) {
1756 lock_page(pages[i]);
1757 if (!PageDirty(pages[i]) ||
1758 pages[i]->mapping != mapping) {
1759 unlock_page(pages[i]);
1760 put_page(pages[i]);
1761 err = -EAGAIN;
1762 goto out;
1763 }
1764 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001765 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001766 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001767 }
1768 nr_pages -= ret;
1769 index += ret;
1770 cond_resched();
1771 }
Liu Boda2c7002017-02-10 16:41:05 +01001772out:
1773 if (err && index_ret)
1774 *index_ret = start_index + pages_locked - 1;
1775 return err;
Chris Masonc8b97812008-10-29 14:49:59 -04001776}
Chris Masonc8b97812008-10-29 14:49:59 -04001777
Liu Bo873695b2017-02-02 17:49:22 -08001778void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1779 u64 delalloc_end, struct page *locked_page,
1780 unsigned clear_bits,
1781 unsigned long page_ops)
1782{
1783 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001784 NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001785
1786 __process_pages_contig(inode->i_mapping, locked_page,
1787 start >> PAGE_SHIFT, end >> PAGE_SHIFT,
Liu Boda2c7002017-02-10 16:41:05 +01001788 page_ops, NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001789}
1790
Chris Masond352ac62008-09-29 15:18:18 -04001791/*
1792 * count the number of bytes in the tree that have a given bit(s)
1793 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1794 * cached. The total number found is returned.
1795 */
Chris Masond1310b22008-01-24 16:13:08 -05001796u64 count_range_bits(struct extent_io_tree *tree,
1797 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001798 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001799{
1800 struct rb_node *node;
1801 struct extent_state *state;
1802 u64 cur_start = *start;
1803 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001804 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001805 int found = 0;
1806
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301807 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001808 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001809
Chris Masoncad321a2008-12-17 14:51:42 -05001810 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001811 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1812 total_bytes = tree->dirty_bytes;
1813 goto out;
1814 }
1815 /*
1816 * this search will find all the extents that end after
1817 * our range starts.
1818 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001819 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001820 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001821 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001822
Chris Masond3977122009-01-05 21:25:51 -05001823 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001824 state = rb_entry(node, struct extent_state, rb_node);
1825 if (state->start > search_end)
1826 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001827 if (contig && found && state->start > last + 1)
1828 break;
1829 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001830 total_bytes += min(search_end, state->end) + 1 -
1831 max(cur_start, state->start);
1832 if (total_bytes >= max_bytes)
1833 break;
1834 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001835 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001836 found = 1;
1837 }
Chris Masonec29ed52011-02-23 16:23:20 -05001838 last = state->end;
1839 } else if (contig && found) {
1840 break;
Chris Masond1310b22008-01-24 16:13:08 -05001841 }
1842 node = rb_next(node);
1843 if (!node)
1844 break;
1845 }
1846out:
Chris Masoncad321a2008-12-17 14:51:42 -05001847 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001848 return total_bytes;
1849}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001850
Chris Masond352ac62008-09-29 15:18:18 -04001851/*
1852 * set the private field for a given byte offset in the tree. If there isn't
1853 * an extent_state there already, this does nothing.
1854 */
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001855static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001856 struct io_failure_record *failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001857{
1858 struct rb_node *node;
1859 struct extent_state *state;
1860 int ret = 0;
1861
Chris Masoncad321a2008-12-17 14:51:42 -05001862 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001863 /*
1864 * this search will find all the extents that end after
1865 * our range starts.
1866 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001867 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001868 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001869 ret = -ENOENT;
1870 goto out;
1871 }
1872 state = rb_entry(node, struct extent_state, rb_node);
1873 if (state->start != start) {
1874 ret = -ENOENT;
1875 goto out;
1876 }
David Sterba47dc1962016-02-11 13:24:13 +01001877 state->failrec = failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001878out:
Chris Masoncad321a2008-12-17 14:51:42 -05001879 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001880 return ret;
1881}
1882
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001883static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001884 struct io_failure_record **failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001885{
1886 struct rb_node *node;
1887 struct extent_state *state;
1888 int ret = 0;
1889
Chris Masoncad321a2008-12-17 14:51:42 -05001890 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001891 /*
1892 * this search will find all the extents that end after
1893 * our range starts.
1894 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001895 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001896 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001897 ret = -ENOENT;
1898 goto out;
1899 }
1900 state = rb_entry(node, struct extent_state, rb_node);
1901 if (state->start != start) {
1902 ret = -ENOENT;
1903 goto out;
1904 }
David Sterba47dc1962016-02-11 13:24:13 +01001905 *failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001906out:
Chris Masoncad321a2008-12-17 14:51:42 -05001907 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001908 return ret;
1909}
1910
1911/*
1912 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001913 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001914 * has the bits set. Otherwise, 1 is returned if any bit in the
1915 * range is found set.
1916 */
1917int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001918 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001919{
1920 struct extent_state *state = NULL;
1921 struct rb_node *node;
1922 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001923
Chris Masoncad321a2008-12-17 14:51:42 -05001924 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01001925 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001926 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001927 node = &cached->rb_node;
1928 else
1929 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001930 while (node && start <= end) {
1931 state = rb_entry(node, struct extent_state, rb_node);
1932
1933 if (filled && state->start > start) {
1934 bitset = 0;
1935 break;
1936 }
1937
1938 if (state->start > end)
1939 break;
1940
1941 if (state->state & bits) {
1942 bitset = 1;
1943 if (!filled)
1944 break;
1945 } else if (filled) {
1946 bitset = 0;
1947 break;
1948 }
Chris Mason46562ce2009-09-23 20:23:16 -04001949
1950 if (state->end == (u64)-1)
1951 break;
1952
Chris Masond1310b22008-01-24 16:13:08 -05001953 start = state->end + 1;
1954 if (start > end)
1955 break;
1956 node = rb_next(node);
1957 if (!node) {
1958 if (filled)
1959 bitset = 0;
1960 break;
1961 }
1962 }
Chris Masoncad321a2008-12-17 14:51:42 -05001963 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001964 return bitset;
1965}
Chris Masond1310b22008-01-24 16:13:08 -05001966
1967/*
1968 * helper function to set a given page up to date if all the
1969 * extents in the tree for that page are up to date
1970 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001971static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001972{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001973 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001974 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001975 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001976 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001977}
1978
Josef Bacik7870d082017-05-05 11:57:15 -04001979int free_io_failure(struct extent_io_tree *failure_tree,
1980 struct extent_io_tree *io_tree,
1981 struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001982{
1983 int ret;
1984 int err = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001985
David Sterba47dc1962016-02-11 13:24:13 +01001986 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001987 ret = clear_extent_bits(failure_tree, rec->start,
1988 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001989 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001990 if (ret)
1991 err = ret;
1992
Josef Bacik7870d082017-05-05 11:57:15 -04001993 ret = clear_extent_bits(io_tree, rec->start,
David Woodhouse53b381b2013-01-29 18:40:14 -05001994 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001995 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05001996 if (ret && !err)
1997 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001998
1999 kfree(rec);
2000 return err;
2001}
2002
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002003/*
2004 * this bypasses the standard btrfs submit functions deliberately, as
2005 * the standard behavior is to write all copies in a raid setup. here we only
2006 * want to write the one bad copy. so we do the mapping for ourselves and issue
2007 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002008 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002009 * actually prevents the read that triggered the error from finishing.
2010 * currently, there can be no more than two copies of every data bit. thus,
2011 * exactly one rewrite is required.
2012 */
Josef Bacik6ec656b2017-05-05 11:57:14 -04002013int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
2014 u64 length, u64 logical, struct page *page,
2015 unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002016{
2017 struct bio *bio;
2018 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002019 u64 map_length = 0;
2020 u64 sector;
2021 struct btrfs_bio *bbio = NULL;
2022 int ret;
2023
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002024 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002025 BUG_ON(!mirror_num);
2026
David Sterbac5e4c3d2017-06-12 17:29:41 +02002027 bio = btrfs_io_bio_alloc(1);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002028 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002029 map_length = length;
2030
Filipe Mananab5de8d02016-05-27 22:21:27 +01002031 /*
2032 * Avoid races with device replace and make sure our bbio has devices
2033 * associated to its stripes that don't go away while we are doing the
2034 * read repair operation.
2035 */
2036 btrfs_bio_counter_inc_blocked(fs_info);
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03002037 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
Liu Boc7253282017-03-29 10:53:58 -07002038 /*
2039 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
2040 * to update all raid stripes, but here we just want to correct
2041 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2042 * stripe's dev and sector.
2043 */
2044 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2045 &map_length, &bbio, 0);
2046 if (ret) {
2047 btrfs_bio_counter_dec(fs_info);
2048 bio_put(bio);
2049 return -EIO;
2050 }
2051 ASSERT(bbio->mirror_num == 1);
2052 } else {
2053 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2054 &map_length, &bbio, mirror_num);
2055 if (ret) {
2056 btrfs_bio_counter_dec(fs_info);
2057 bio_put(bio);
2058 return -EIO;
2059 }
2060 BUG_ON(mirror_num != bbio->mirror_num);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002061 }
Liu Boc7253282017-03-29 10:53:58 -07002062
2063 sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002064 bio->bi_iter.bi_sector = sector;
Liu Boc7253282017-03-29 10:53:58 -07002065 dev = bbio->stripes[bbio->mirror_num - 1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002066 btrfs_put_bbio(bbio);
Anand Jainebbede42017-12-04 12:54:52 +08002067 if (!dev || !dev->bdev ||
2068 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002069 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002070 bio_put(bio);
2071 return -EIO;
2072 }
Christoph Hellwig74d46992017-08-23 19:10:32 +02002073 bio_set_dev(bio, dev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002074 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Miao Xieffdd2012014-09-12 18:44:00 +08002075 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002076
Mike Christie4e49ea42016-06-05 14:31:41 -05002077 if (btrfsic_submit_bio_wait(bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002078 /* try to remap that extent elsewhere? */
Filipe Mananab5de8d02016-05-27 22:21:27 +01002079 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002080 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002081 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002082 return -EIO;
2083 }
2084
David Sterbab14af3b2015-10-08 10:43:10 +02002085 btrfs_info_rl_in_rcu(fs_info,
2086 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Josef Bacik6ec656b2017-05-05 11:57:14 -04002087 ino, start,
Miao Xie1203b682014-09-12 18:44:01 +08002088 rcu_str_deref(dev->name), sector);
Filipe Mananab5de8d02016-05-27 22:21:27 +01002089 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002090 bio_put(bio);
2091 return 0;
2092}
2093
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002094int repair_eb_io_failure(struct btrfs_fs_info *fs_info,
2095 struct extent_buffer *eb, int mirror_num)
Josef Bacikea466792012-03-26 21:57:36 -04002096{
Josef Bacikea466792012-03-26 21:57:36 -04002097 u64 start = eb->start;
David Sterbacc5e31a2018-03-01 18:20:27 +01002098 int i, num_pages = num_extent_pages(eb);
Chris Masond95603b2012-04-12 15:55:15 -04002099 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002100
David Howellsbc98a422017-07-17 08:45:34 +01002101 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002102 return -EROFS;
2103
Josef Bacikea466792012-03-26 21:57:36 -04002104 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002105 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002106
Josef Bacik6ec656b2017-05-05 11:57:14 -04002107 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002108 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002109 if (ret)
2110 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002111 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002112 }
2113
2114 return ret;
2115}
2116
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002117/*
2118 * each time an IO finishes, we do a fast check in the IO failure tree
2119 * to see if we need to process or clean up an io_failure_record
2120 */
Josef Bacik7870d082017-05-05 11:57:15 -04002121int clean_io_failure(struct btrfs_fs_info *fs_info,
2122 struct extent_io_tree *failure_tree,
2123 struct extent_io_tree *io_tree, u64 start,
2124 struct page *page, u64 ino, unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002125{
2126 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002127 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002128 struct extent_state *state;
2129 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002130 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002131
2132 private = 0;
Josef Bacik7870d082017-05-05 11:57:15 -04002133 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2134 EXTENT_DIRTY, 0);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002135 if (!ret)
2136 return 0;
2137
Josef Bacik7870d082017-05-05 11:57:15 -04002138 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002139 if (ret)
2140 return 0;
2141
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002142 BUG_ON(!failrec->this_mirror);
2143
2144 if (failrec->in_validation) {
2145 /* there was no real error, just free the record */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002146 btrfs_debug(fs_info,
2147 "clean_io_failure: freeing dummy error at %llu",
2148 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002149 goto out;
2150 }
David Howellsbc98a422017-07-17 08:45:34 +01002151 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002152 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002153
Josef Bacik7870d082017-05-05 11:57:15 -04002154 spin_lock(&io_tree->lock);
2155 state = find_first_extent_bit_state(io_tree,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002156 failrec->start,
2157 EXTENT_LOCKED);
Josef Bacik7870d082017-05-05 11:57:15 -04002158 spin_unlock(&io_tree->lock);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002159
Miao Xie883d0de2013-07-25 19:22:35 +08002160 if (state && state->start <= failrec->start &&
2161 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002162 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2163 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002164 if (num_copies > 1) {
Josef Bacik7870d082017-05-05 11:57:15 -04002165 repair_io_failure(fs_info, ino, start, failrec->len,
2166 failrec->logical, page, pg_offset,
2167 failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002168 }
2169 }
2170
2171out:
Josef Bacik7870d082017-05-05 11:57:15 -04002172 free_io_failure(failure_tree, io_tree, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002173
Miao Xie454ff3d2014-09-12 18:43:58 +08002174 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002175}
2176
Miao Xief6124962014-09-12 18:44:04 +08002177/*
2178 * Can be called when
2179 * - hold extent lock
2180 * - under ordered extent
2181 * - the inode is freeing
2182 */
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002183void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
Miao Xief6124962014-09-12 18:44:04 +08002184{
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002185 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
Miao Xief6124962014-09-12 18:44:04 +08002186 struct io_failure_record *failrec;
2187 struct extent_state *state, *next;
2188
2189 if (RB_EMPTY_ROOT(&failure_tree->state))
2190 return;
2191
2192 spin_lock(&failure_tree->lock);
2193 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2194 while (state) {
2195 if (state->start > end)
2196 break;
2197
2198 ASSERT(state->end <= end);
2199
2200 next = next_state(state);
2201
David Sterba47dc1962016-02-11 13:24:13 +01002202 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002203 free_extent_state(state);
2204 kfree(failrec);
2205
2206 state = next;
2207 }
2208 spin_unlock(&failure_tree->lock);
2209}
2210
Miao Xie2fe63032014-09-12 18:43:59 +08002211int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
David Sterba47dc1962016-02-11 13:24:13 +01002212 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002213{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002214 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002215 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002216 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002217 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2218 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2219 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002220 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002221 u64 logical;
2222
David Sterba47dc1962016-02-11 13:24:13 +01002223 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002224 if (ret) {
2225 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2226 if (!failrec)
2227 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002228
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002229 failrec->start = start;
2230 failrec->len = end - start + 1;
2231 failrec->this_mirror = 0;
2232 failrec->bio_flags = 0;
2233 failrec->in_validation = 0;
2234
2235 read_lock(&em_tree->lock);
2236 em = lookup_extent_mapping(em_tree, start, failrec->len);
2237 if (!em) {
2238 read_unlock(&em_tree->lock);
2239 kfree(failrec);
2240 return -EIO;
2241 }
2242
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002243 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002244 free_extent_map(em);
2245 em = NULL;
2246 }
2247 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002248 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002249 kfree(failrec);
2250 return -EIO;
2251 }
Miao Xie2fe63032014-09-12 18:43:59 +08002252
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002253 logical = start - em->start;
2254 logical = em->block_start + logical;
2255 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2256 logical = em->block_start;
2257 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2258 extent_set_compress_type(&failrec->bio_flags,
2259 em->compress_type);
2260 }
Miao Xie2fe63032014-09-12 18:43:59 +08002261
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002262 btrfs_debug(fs_info,
2263 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2264 logical, start, failrec->len);
Miao Xie2fe63032014-09-12 18:43:59 +08002265
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002266 failrec->logical = logical;
2267 free_extent_map(em);
2268
2269 /* set the bits in the private failure tree */
2270 ret = set_extent_bits(failure_tree, start, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002271 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002272 if (ret >= 0)
David Sterba47dc1962016-02-11 13:24:13 +01002273 ret = set_state_failrec(failure_tree, start, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002274 /* set the bits in the inode's tree */
2275 if (ret >= 0)
David Sterbaceeb0ae2016-04-26 23:54:39 +02002276 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002277 if (ret < 0) {
2278 kfree(failrec);
2279 return ret;
2280 }
2281 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002282 btrfs_debug(fs_info,
2283 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2284 failrec->logical, failrec->start, failrec->len,
2285 failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002286 /*
2287 * when data can be on disk more than twice, add to failrec here
2288 * (e.g. with a list for failed_mirror) to make
2289 * clean_io_failure() clean all those errors at once.
2290 */
2291 }
Miao Xie2fe63032014-09-12 18:43:59 +08002292
2293 *failrec_ret = failrec;
2294
2295 return 0;
2296}
2297
Ming Leia0b60d72017-12-18 20:22:11 +08002298bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages,
Miao Xie2fe63032014-09-12 18:43:59 +08002299 struct io_failure_record *failrec, int failed_mirror)
2300{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002301 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002302 int num_copies;
2303
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002304 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002305 if (num_copies == 1) {
2306 /*
2307 * we only have a single copy of the data, so don't bother with
2308 * all the retry and error correction code that follows. no
2309 * matter what the error is, it is very likely to persist.
2310 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002311 btrfs_debug(fs_info,
2312 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2313 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002314 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002315 }
2316
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002317 /*
2318 * there are two premises:
2319 * a) deliver good data to the caller
2320 * b) correct the bad sectors on disk
2321 */
Ming Leia0b60d72017-12-18 20:22:11 +08002322 if (failed_bio_pages > 1) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002323 /*
2324 * to fulfill b), we need to know the exact failing sectors, as
2325 * we don't want to rewrite any more than the failed ones. thus,
2326 * we need separate read requests for the failed bio
2327 *
2328 * if the following BUG_ON triggers, our validation request got
2329 * merged. we need separate requests for our algorithm to work.
2330 */
2331 BUG_ON(failrec->in_validation);
2332 failrec->in_validation = 1;
2333 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002334 } else {
2335 /*
2336 * we're ready to fulfill a) and b) alongside. get a good copy
2337 * of the failed sector and if we succeed, we have setup
2338 * everything for repair_io_failure to do the rest for us.
2339 */
2340 if (failrec->in_validation) {
2341 BUG_ON(failrec->this_mirror != failed_mirror);
2342 failrec->in_validation = 0;
2343 failrec->this_mirror = 0;
2344 }
2345 failrec->failed_mirror = failed_mirror;
2346 failrec->this_mirror++;
2347 if (failrec->this_mirror == failed_mirror)
2348 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002349 }
2350
Miao Xiefacc8a222013-07-25 19:22:34 +08002351 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002352 btrfs_debug(fs_info,
2353 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2354 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002355 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002356 }
2357
Liu Boc3cfb652017-07-13 15:00:50 -07002358 return true;
Miao Xie2fe63032014-09-12 18:43:59 +08002359}
2360
2361
2362struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2363 struct io_failure_record *failrec,
2364 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002365 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002366{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002367 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002368 struct bio *bio;
2369 struct btrfs_io_bio *btrfs_failed_bio;
2370 struct btrfs_io_bio *btrfs_bio;
2371
David Sterbac5e4c3d2017-06-12 17:29:41 +02002372 bio = btrfs_io_bio_alloc(1);
Miao Xie2fe63032014-09-12 18:43:59 +08002373 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002374 bio->bi_iter.bi_sector = failrec->logical >> 9;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002375 bio_set_dev(bio, fs_info->fs_devices->latest_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002376 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002377 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002378
Miao Xiefacc8a222013-07-25 19:22:34 +08002379 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2380 if (btrfs_failed_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002381 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2382
2383 btrfs_bio = btrfs_io_bio(bio);
2384 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002385 icsum *= csum_size;
2386 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002387 csum_size);
2388 }
2389
Miao Xie2fe63032014-09-12 18:43:59 +08002390 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002391
Miao Xie2fe63032014-09-12 18:43:59 +08002392 return bio;
2393}
2394
2395/*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002396 * This is a generic handler for readpage errors. If other copies exist, read
2397 * those and write back good data to the failed position. Does not investigate
2398 * in remapping the failed extent elsewhere, hoping the device will be smart
2399 * enough to do this as needed
Miao Xie2fe63032014-09-12 18:43:59 +08002400 */
Miao Xie2fe63032014-09-12 18:43:59 +08002401static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2402 struct page *page, u64 start, u64 end,
2403 int failed_mirror)
2404{
2405 struct io_failure_record *failrec;
2406 struct inode *inode = page->mapping->host;
2407 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002408 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie2fe63032014-09-12 18:43:59 +08002409 struct bio *bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002410 int read_mode = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002411 blk_status_t status;
Miao Xie2fe63032014-09-12 18:43:59 +08002412 int ret;
Christoph Hellwig8a2ee442019-02-15 19:13:07 +08002413 unsigned failed_bio_pages = failed_bio->bi_iter.bi_size >> PAGE_SHIFT;
Miao Xie2fe63032014-09-12 18:43:59 +08002414
Mike Christie1f7ad752016-06-05 14:31:51 -05002415 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002416
2417 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2418 if (ret)
2419 return ret;
2420
Ming Leia0b60d72017-12-18 20:22:11 +08002421 if (!btrfs_check_repairable(inode, failed_bio_pages, failrec,
Liu Boc3cfb652017-07-13 15:00:50 -07002422 failed_mirror)) {
Josef Bacik7870d082017-05-05 11:57:15 -04002423 free_io_failure(failure_tree, tree, failrec);
Miao Xie2fe63032014-09-12 18:43:59 +08002424 return -EIO;
2425 }
2426
Ming Leia0b60d72017-12-18 20:22:11 +08002427 if (failed_bio_pages > 1)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002428 read_mode |= REQ_FAILFAST_DEV;
Miao Xie2fe63032014-09-12 18:43:59 +08002429
2430 phy_offset >>= inode->i_sb->s_blocksize_bits;
2431 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2432 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002433 (int)phy_offset, failed_bio->bi_end_io,
2434 NULL);
David Sterbaebcc3262018-06-29 10:56:53 +02002435 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie2fe63032014-09-12 18:43:59 +08002436
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002437 btrfs_debug(btrfs_sb(inode->i_sb),
2438 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2439 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002440
Linus Torvalds8c27cb32017-07-05 16:41:23 -07002441 status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002442 failrec->bio_flags, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002443 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04002444 free_io_failure(failure_tree, tree, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002445 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002446 ret = blk_status_to_errno(status);
Miao Xie6c387ab2014-09-12 18:43:57 +08002447 }
2448
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002449 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002450}
2451
Chris Masond1310b22008-01-24 16:13:08 -05002452/* lots and lots of room for performance fixes in the end_bio funcs */
2453
David Sterbab5227c02015-12-03 13:08:59 +01002454void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002455{
2456 int uptodate = (err == 0);
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002457 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002458
Nikolay Borisovc6297322018-11-08 10:18:08 +02002459 btrfs_writepage_endio_finish_ordered(page, start, end, uptodate);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002460
Jeff Mahoney87826df2012-02-15 16:23:57 +01002461 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002462 ClearPageUptodate(page);
2463 SetPageError(page);
Colin Ian Kingbff5baf2017-05-09 18:14:01 +01002464 ret = err < 0 ? err : -EIO;
Liu Bo5dca6ee2014-05-12 12:47:36 +08002465 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002466 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002467}
2468
Chris Masond1310b22008-01-24 16:13:08 -05002469/*
2470 * after a writepage IO is done, we need to:
2471 * clear the uptodate bits on error
2472 * clear the writeback bits in the extent tree for this IO
2473 * end_page_writeback if the page has no more pending IO
2474 *
2475 * Scheduling is not allowed, so the extent state tree is expected
2476 * to have one and only one object corresponding to this IO.
2477 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002478static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002479{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002480 int error = blk_status_to_errno(bio->bi_status);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002481 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002482 u64 start;
2483 u64 end;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002484 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08002485 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002486
David Sterbac09abff2017-07-13 18:10:07 +02002487 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08002488 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002489 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002490 struct inode *inode = page->mapping->host;
2491 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002492
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002493 /* We always issue full-page reads, but if some block
2494 * in a page fails to read, blk_update_request() will
2495 * advance bv_offset and adjust bv_len to compensate.
2496 * Print a warning for nonzero offsets, and an error
2497 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002498 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2499 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002500 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002501 "partial page write in btrfs with offset %u and length %u",
2502 bvec->bv_offset, bvec->bv_len);
2503 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002504 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002505 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002506 bvec->bv_offset, bvec->bv_len);
2507 }
Chris Masond1310b22008-01-24 16:13:08 -05002508
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002509 start = page_offset(page);
2510 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002511
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002512 end_extent_writepage(page, error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002513 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002514 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002515
Chris Masond1310b22008-01-24 16:13:08 -05002516 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002517}
2518
Miao Xie883d0de2013-07-25 19:22:35 +08002519static void
2520endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2521 int uptodate)
2522{
2523 struct extent_state *cached = NULL;
2524 u64 end = start + len - 1;
2525
2526 if (uptodate && tree->track_uptodate)
2527 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
David Sterbad810a4b2017-12-07 18:52:54 +01002528 unlock_extent_cached_atomic(tree, start, end, &cached);
Miao Xie883d0de2013-07-25 19:22:35 +08002529}
2530
Chris Masond1310b22008-01-24 16:13:08 -05002531/*
2532 * after a readpage IO is done, we need to:
2533 * clear the uptodate bits on error
2534 * set the uptodate bits if things worked
2535 * set the page up to date if all extents in the tree are uptodate
2536 * clear the lock bit in the extent tree
2537 * unlock the page if there are no other extents locked for it
2538 *
2539 * Scheduling is not allowed, so the extent state tree is expected
2540 * to have one and only one object corresponding to this IO.
2541 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002542static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002543{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002544 struct bio_vec *bvec;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002545 int uptodate = !bio->bi_status;
Miao Xiefacc8a222013-07-25 19:22:34 +08002546 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04002547 struct extent_io_tree *tree, *failure_tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002548 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002549 u64 start;
2550 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002551 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002552 u64 extent_start = 0;
2553 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002554 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002555 int ret;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002556 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08002557 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002558
David Sterbac09abff2017-07-13 18:10:07 +02002559 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08002560 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002561 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002562 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002563 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002564 bool data_inode = btrfs_ino(BTRFS_I(inode))
2565 != BTRFS_BTREE_INODE_OBJECTID;
Arne Jansen507903b2011-04-06 10:02:20 +00002566
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002567 btrfs_debug(fs_info,
2568 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002569 (u64)bio->bi_iter.bi_sector, bio->bi_status,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002570 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002571 tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002572 failure_tree = &BTRFS_I(inode)->io_failure_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002573
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002574 /* We always issue full-page reads, but if some block
2575 * in a page fails to read, blk_update_request() will
2576 * advance bv_offset and adjust bv_len to compensate.
2577 * Print a warning for nonzero offsets, and an error
2578 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002579 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2580 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002581 btrfs_err(fs_info,
2582 "partial page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002583 bvec->bv_offset, bvec->bv_len);
2584 else
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002585 btrfs_info(fs_info,
2586 "incomplete page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002587 bvec->bv_offset, bvec->bv_len);
2588 }
Chris Masond1310b22008-01-24 16:13:08 -05002589
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002590 start = page_offset(page);
2591 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002592 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002593
Chris Mason9be33952013-05-17 18:30:14 -04002594 mirror = io_bio->mirror_num;
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002595 if (likely(uptodate)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002596 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2597 page, start, end,
2598 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002599 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002600 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002601 else
Josef Bacik7870d082017-05-05 11:57:15 -04002602 clean_io_failure(BTRFS_I(inode)->root->fs_info,
2603 failure_tree, tree, start,
2604 page,
2605 btrfs_ino(BTRFS_I(inode)), 0);
Chris Masond1310b22008-01-24 16:13:08 -05002606 }
Josef Bacikea466792012-03-26 21:57:36 -04002607
Miao Xief2a09da2013-07-25 19:22:33 +08002608 if (likely(uptodate))
2609 goto readpage_ok;
2610
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002611 if (data_inode) {
Liu Bo9d0d1c82017-03-24 15:04:50 -07002612
2613 /*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002614 * The generic bio_readpage_error handles errors the
2615 * following way: If possible, new read requests are
2616 * created and submitted and will end up in
2617 * end_bio_extent_readpage as well (if we're lucky,
2618 * not in the !uptodate case). In that case it returns
2619 * 0 and we just go on with the next page in our bio.
2620 * If it can't handle the error it will return -EIO and
2621 * we remain responsible for that page.
Liu Bo9d0d1c82017-03-24 15:04:50 -07002622 */
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002623 ret = bio_readpage_error(bio, offset, page, start, end,
2624 mirror);
2625 if (ret == 0) {
2626 uptodate = !bio->bi_status;
2627 offset += len;
2628 continue;
2629 }
2630 } else {
2631 struct extent_buffer *eb;
2632
2633 eb = (struct extent_buffer *)page->private;
2634 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
2635 eb->read_mirror = mirror;
2636 atomic_dec(&eb->io_pages);
2637 if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD,
2638 &eb->bflags))
2639 btree_readahead_hook(eb, -EIO);
Chris Mason7e383262008-04-09 16:28:12 -04002640 }
Miao Xief2a09da2013-07-25 19:22:33 +08002641readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002642 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002643 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002644 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002645 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002646
2647 /* Zero out the end if this page straddles i_size */
Johannes Thumshirn70730172018-12-05 15:23:03 +01002648 off = offset_in_page(i_size);
Liu Boa583c022014-08-19 23:32:22 +08002649 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002650 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002651 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002652 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002653 ClearPageUptodate(page);
2654 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002655 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002656 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002657 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002658
2659 if (unlikely(!uptodate)) {
2660 if (extent_len) {
2661 endio_readpage_release_extent(tree,
2662 extent_start,
2663 extent_len, 1);
2664 extent_start = 0;
2665 extent_len = 0;
2666 }
2667 endio_readpage_release_extent(tree, start,
2668 end - start + 1, 0);
2669 } else if (!extent_len) {
2670 extent_start = start;
2671 extent_len = end + 1 - start;
2672 } else if (extent_start + extent_len == start) {
2673 extent_len += end + 1 - start;
2674 } else {
2675 endio_readpage_release_extent(tree, extent_start,
2676 extent_len, uptodate);
2677 extent_start = start;
2678 extent_len = end + 1 - start;
2679 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002680 }
Chris Masond1310b22008-01-24 16:13:08 -05002681
Miao Xie883d0de2013-07-25 19:22:35 +08002682 if (extent_len)
2683 endio_readpage_release_extent(tree, extent_start, extent_len,
2684 uptodate);
David Sterbab3a0dd52018-11-22 17:16:49 +01002685 btrfs_io_bio_free_csum(io_bio);
Chris Masond1310b22008-01-24 16:13:08 -05002686 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002687}
2688
Chris Mason9be33952013-05-17 18:30:14 -04002689/*
David Sterba184f9992017-06-12 17:29:39 +02002690 * Initialize the members up to but not including 'bio'. Use after allocating a
2691 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
2692 * 'bio' because use of __GFP_ZERO is not supported.
Chris Mason9be33952013-05-17 18:30:14 -04002693 */
David Sterba184f9992017-06-12 17:29:39 +02002694static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
Chris Masond1310b22008-01-24 16:13:08 -05002695{
David Sterba184f9992017-06-12 17:29:39 +02002696 memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
2697}
2698
2699/*
David Sterba6e707bc2017-06-02 17:26:26 +02002700 * The following helpers allocate a bio. As it's backed by a bioset, it'll
2701 * never fail. We're returning a bio right now but you can call btrfs_io_bio
2702 * for the appropriate container_of magic
Chris Masond1310b22008-01-24 16:13:08 -05002703 */
David Sterbac821e7f32017-06-02 18:35:36 +02002704struct bio *btrfs_bio_alloc(struct block_device *bdev, u64 first_byte)
Chris Masond1310b22008-01-24 16:13:08 -05002705{
2706 struct bio *bio;
2707
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002708 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset);
Christoph Hellwig74d46992017-08-23 19:10:32 +02002709 bio_set_dev(bio, bdev);
David Sterbac821e7f32017-06-02 18:35:36 +02002710 bio->bi_iter.bi_sector = first_byte >> 9;
David Sterba184f9992017-06-12 17:29:39 +02002711 btrfs_io_bio_init(btrfs_io_bio(bio));
Chris Masond1310b22008-01-24 16:13:08 -05002712 return bio;
2713}
2714
David Sterba8b6c1d52017-06-02 17:48:13 +02002715struct bio *btrfs_bio_clone(struct bio *bio)
Chris Mason9be33952013-05-17 18:30:14 -04002716{
Miao Xie23ea8e52014-09-12 18:43:54 +08002717 struct btrfs_io_bio *btrfs_bio;
2718 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002719
David Sterba6e707bc2017-06-02 17:26:26 +02002720 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002721 new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset);
David Sterba6e707bc2017-06-02 17:26:26 +02002722 btrfs_bio = btrfs_io_bio(new);
David Sterba184f9992017-06-12 17:29:39 +02002723 btrfs_io_bio_init(btrfs_bio);
David Sterba6e707bc2017-06-02 17:26:26 +02002724 btrfs_bio->iter = bio->bi_iter;
Miao Xie23ea8e52014-09-12 18:43:54 +08002725 return new;
2726}
Chris Mason9be33952013-05-17 18:30:14 -04002727
David Sterbac5e4c3d2017-06-12 17:29:41 +02002728struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
Chris Mason9be33952013-05-17 18:30:14 -04002729{
Miao Xiefacc8a222013-07-25 19:22:34 +08002730 struct bio *bio;
2731
David Sterba6e707bc2017-06-02 17:26:26 +02002732 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002733 bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
David Sterba184f9992017-06-12 17:29:39 +02002734 btrfs_io_bio_init(btrfs_io_bio(bio));
Miao Xiefacc8a222013-07-25 19:22:34 +08002735 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002736}
2737
Liu Boe4770942017-05-16 10:57:14 -07002738struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
Liu Bo2f8e9142017-05-15 17:43:31 -07002739{
2740 struct bio *bio;
2741 struct btrfs_io_bio *btrfs_bio;
2742
2743 /* this will never fail when it's backed by a bioset */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002744 bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset);
Liu Bo2f8e9142017-05-15 17:43:31 -07002745 ASSERT(bio);
2746
2747 btrfs_bio = btrfs_io_bio(bio);
David Sterba184f9992017-06-12 17:29:39 +02002748 btrfs_io_bio_init(btrfs_bio);
Liu Bo2f8e9142017-05-15 17:43:31 -07002749
2750 bio_trim(bio, offset >> 9, size >> 9);
Liu Bo17347ce2017-05-15 15:33:27 -07002751 btrfs_bio->iter = bio->bi_iter;
Liu Bo2f8e9142017-05-15 17:43:31 -07002752 return bio;
2753}
Chris Mason9be33952013-05-17 18:30:14 -04002754
David Sterba4b81ba42017-06-06 19:14:26 +02002755/*
2756 * @opf: bio REQ_OP_* and REQ_* flags as one value
David Sterbab8b3d622017-06-12 19:50:41 +02002757 * @tree: tree so we can call our merge_bio hook
2758 * @wbc: optional writeback control for io accounting
2759 * @page: page to add to the bio
2760 * @pg_offset: offset of the new bio or to check whether we are adding
2761 * a contiguous page to the previous one
2762 * @size: portion of page that we want to write
2763 * @offset: starting offset in the page
2764 * @bdev: attach newly created bios to this bdev
David Sterba5c2b1fd2017-06-06 19:22:55 +02002765 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
David Sterbab8b3d622017-06-12 19:50:41 +02002766 * @end_io_func: end_io callback for new bio
2767 * @mirror_num: desired mirror to read/write
2768 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
2769 * @bio_flags: flags of the current bio to see if we can merge them
David Sterba4b81ba42017-06-06 19:14:26 +02002770 */
2771static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002772 struct writeback_control *wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02002773 struct page *page, u64 offset,
David Sterba6c5a4e22017-10-04 17:10:34 +02002774 size_t size, unsigned long pg_offset,
Chris Masond1310b22008-01-24 16:13:08 -05002775 struct block_device *bdev,
2776 struct bio **bio_ret,
Chris Masonf1885912008-04-09 16:28:12 -04002777 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002778 int mirror_num,
2779 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002780 unsigned long bio_flags,
2781 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002782{
2783 int ret = 0;
2784 struct bio *bio;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002785 size_t page_size = min_t(size_t, size, PAGE_SIZE);
David Sterba6273b7f2017-10-04 17:30:11 +02002786 sector_t sector = offset >> 9;
Chris Masond1310b22008-01-24 16:13:08 -05002787
David Sterba5c2b1fd2017-06-06 19:22:55 +02002788 ASSERT(bio_ret);
2789
2790 if (*bio_ret) {
David Sterba0c8508a2017-06-12 20:00:43 +02002791 bool contig;
2792 bool can_merge = true;
2793
Chris Masond1310b22008-01-24 16:13:08 -05002794 bio = *bio_ret;
David Sterba0c8508a2017-06-12 20:00:43 +02002795 if (prev_bio_flags & EXTENT_BIO_COMPRESSED)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002796 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002797 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002798 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002799
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002800 ASSERT(tree->ops);
2801 if (btrfs_bio_fits_in_stripe(page, page_size, bio, bio_flags))
David Sterba0c8508a2017-06-12 20:00:43 +02002802 can_merge = false;
2803
2804 if (prev_bio_flags != bio_flags || !contig || !can_merge ||
Filipe Manana005efed2015-09-14 09:09:31 +01002805 force_bio_submit ||
David Sterba6c5a4e22017-10-04 17:10:34 +02002806 bio_add_page(bio, page, page_size, pg_offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002807 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002808 if (ret < 0) {
2809 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002810 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002811 }
Chris Masond1310b22008-01-24 16:13:08 -05002812 bio = NULL;
2813 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002814 if (wbc)
2815 wbc_account_io(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002816 return 0;
2817 }
2818 }
Chris Masonc8b97812008-10-29 14:49:59 -04002819
David Sterba6273b7f2017-10-04 17:30:11 +02002820 bio = btrfs_bio_alloc(bdev, offset);
David Sterba6c5a4e22017-10-04 17:10:34 +02002821 bio_add_page(bio, page, page_size, pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002822 bio->bi_end_io = end_io_func;
2823 bio->bi_private = tree;
Jens Axboee6959b92017-06-27 11:51:28 -06002824 bio->bi_write_hint = page->mapping->host->i_write_hint;
David Sterba4b81ba42017-06-06 19:14:26 +02002825 bio->bi_opf = opf;
Chris Masonda2f0f72015-07-02 13:57:22 -07002826 if (wbc) {
2827 wbc_init_bio(wbc, bio);
2828 wbc_account_io(wbc, page, page_size);
2829 }
Chris Mason70dec802008-01-29 09:59:12 -05002830
David Sterba5c2b1fd2017-06-06 19:22:55 +02002831 *bio_ret = bio;
Chris Masond1310b22008-01-24 16:13:08 -05002832
2833 return ret;
2834}
2835
Eric Sandeen48a3b632013-04-25 20:41:01 +00002836static void attach_extent_buffer_page(struct extent_buffer *eb,
2837 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002838{
2839 if (!PagePrivate(page)) {
2840 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002841 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002842 set_page_private(page, (unsigned long)eb);
2843 } else {
2844 WARN_ON(page->private != (unsigned long)eb);
2845 }
2846}
2847
Chris Masond1310b22008-01-24 16:13:08 -05002848void set_page_extent_mapped(struct page *page)
2849{
2850 if (!PagePrivate(page)) {
2851 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002852 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002853 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002854 }
2855}
2856
Miao Xie125bac012013-07-25 19:22:37 +08002857static struct extent_map *
2858__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2859 u64 start, u64 len, get_extent_t *get_extent,
2860 struct extent_map **em_cached)
2861{
2862 struct extent_map *em;
2863
2864 if (em_cached && *em_cached) {
2865 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002866 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002867 start < extent_map_end(em)) {
Elena Reshetova490b54d2017-03-03 10:55:12 +02002868 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002869 return em;
2870 }
2871
2872 free_extent_map(em);
2873 *em_cached = NULL;
2874 }
2875
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02002876 em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
Miao Xie125bac012013-07-25 19:22:37 +08002877 if (em_cached && !IS_ERR_OR_NULL(em)) {
2878 BUG_ON(*em_cached);
Elena Reshetova490b54d2017-03-03 10:55:12 +02002879 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002880 *em_cached = em;
2881 }
2882 return em;
2883}
Chris Masond1310b22008-01-24 16:13:08 -05002884/*
2885 * basic readpage implementation. Locked extent state structs are inserted
2886 * into the tree that are removed when the IO is done (by the end_io
2887 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002888 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07002889 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05002890 */
Miao Xie99740902013-07-25 19:22:36 +08002891static int __do_readpage(struct extent_io_tree *tree,
2892 struct page *page,
2893 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002894 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002895 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02002896 unsigned long *bio_flags, unsigned int read_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002897 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05002898{
2899 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002900 u64 start = page_offset(page);
David Sterba8eec8292017-06-06 19:50:13 +02002901 const u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002902 u64 cur = start;
2903 u64 extent_offset;
2904 u64 last_byte = i_size_read(inode);
2905 u64 block_start;
2906 u64 cur_end;
Chris Masond1310b22008-01-24 16:13:08 -05002907 struct extent_map *em;
2908 struct block_device *bdev;
Liu Bobaf863b2016-07-11 10:39:07 -07002909 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002910 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002911 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002912 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002913 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002914 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00002915 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002916
2917 set_page_extent_mapped(page);
2918
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002919 if (!PageUptodate(page)) {
2920 if (cleancache_get_page(page) == 0) {
2921 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08002922 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002923 goto out;
2924 }
2925 }
2926
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002927 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04002928 char *userpage;
Johannes Thumshirn70730172018-12-05 15:23:03 +01002929 size_t zero_offset = offset_in_page(last_byte);
Chris Masonc8b97812008-10-29 14:49:59 -04002930
2931 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002932 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002933 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002934 memset(userpage + zero_offset, 0, iosize);
2935 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002936 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002937 }
2938 }
Chris Masond1310b22008-01-24 16:13:08 -05002939 while (cur <= end) {
Filipe Manana005efed2015-09-14 09:09:31 +01002940 bool force_bio_submit = false;
David Sterba6273b7f2017-10-04 17:30:11 +02002941 u64 offset;
Josef Bacikc8f2f242013-02-11 11:33:00 -05002942
Chris Masond1310b22008-01-24 16:13:08 -05002943 if (cur >= last_byte) {
2944 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002945 struct extent_state *cached = NULL;
2946
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002947 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002948 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002949 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002950 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002951 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002952 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002953 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00002954 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01002955 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05002956 break;
2957 }
Miao Xie125bac012013-07-25 19:22:37 +08002958 em = __get_extent_map(inode, page, pg_offset, cur,
2959 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02002960 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002961 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00002962 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002963 break;
2964 }
Chris Masond1310b22008-01-24 16:13:08 -05002965 extent_offset = cur - em->start;
2966 BUG_ON(extent_map_end(em) <= cur);
2967 BUG_ON(end < cur);
2968
Li Zefan261507a02010-12-17 14:21:50 +08002969 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07002970 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002971 extent_set_compress_type(&this_bio_flag,
2972 em->compress_type);
2973 }
Chris Masonc8b97812008-10-29 14:49:59 -04002974
Chris Masond1310b22008-01-24 16:13:08 -05002975 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2976 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002977 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002978 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2979 disk_io_size = em->block_len;
David Sterba6273b7f2017-10-04 17:30:11 +02002980 offset = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002981 } else {
David Sterba6273b7f2017-10-04 17:30:11 +02002982 offset = em->block_start + extent_offset;
Chris Masonc8b97812008-10-29 14:49:59 -04002983 disk_io_size = iosize;
2984 }
Chris Masond1310b22008-01-24 16:13:08 -05002985 bdev = em->bdev;
2986 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002987 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2988 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01002989
2990 /*
2991 * If we have a file range that points to a compressed extent
2992 * and it's followed by a consecutive file range that points to
2993 * to the same compressed extent (possibly with a different
2994 * offset and/or length, so it either points to the whole extent
2995 * or only part of it), we must make sure we do not submit a
2996 * single bio to populate the pages for the 2 ranges because
2997 * this makes the compressed extent read zero out the pages
2998 * belonging to the 2nd range. Imagine the following scenario:
2999 *
3000 * File layout
3001 * [0 - 8K] [8K - 24K]
3002 * | |
3003 * | |
3004 * points to extent X, points to extent X,
3005 * offset 4K, length of 8K offset 0, length 16K
3006 *
3007 * [extent X, compressed length = 4K uncompressed length = 16K]
3008 *
3009 * If the bio to read the compressed extent covers both ranges,
3010 * it will decompress extent X into the pages belonging to the
3011 * first range and then it will stop, zeroing out the remaining
3012 * pages that belong to the other range that points to extent X.
3013 * So here we make sure we submit 2 bios, one for the first
3014 * range and another one for the third range. Both will target
3015 * the same physical extent from disk, but we can't currently
3016 * make the compressed bio endio callback populate the pages
3017 * for both ranges because each compressed bio is tightly
3018 * coupled with a single extent map, and each range can have
3019 * an extent map with a different offset value relative to the
3020 * uncompressed data of our extent and different lengths. This
3021 * is a corner case so we prioritize correctness over
3022 * non-optimal behavior (submitting 2 bios for the same extent).
3023 */
3024 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3025 prev_em_start && *prev_em_start != (u64)-1 &&
Filipe Manana8e928212019-02-14 15:17:20 +00003026 *prev_em_start != em->start)
Filipe Manana005efed2015-09-14 09:09:31 +01003027 force_bio_submit = true;
3028
3029 if (prev_em_start)
Filipe Manana8e928212019-02-14 15:17:20 +00003030 *prev_em_start = em->start;
Filipe Manana005efed2015-09-14 09:09:31 +01003031
Chris Masond1310b22008-01-24 16:13:08 -05003032 free_extent_map(em);
3033 em = NULL;
3034
3035 /* we've found a hole, just zero and go on */
3036 if (block_start == EXTENT_MAP_HOLE) {
3037 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003038 struct extent_state *cached = NULL;
3039
Cong Wang7ac687d2011-11-25 23:14:28 +08003040 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003041 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003042 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003043 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003044
3045 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003046 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003047 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01003048 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003049 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003050 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003051 continue;
3052 }
3053 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003054 if (test_range_bit(tree, cur, cur_end,
3055 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003056 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003057 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003058 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003059 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003060 continue;
3061 }
Chris Mason70dec802008-01-29 09:59:12 -05003062 /* we have an inline extent but it didn't get marked up
3063 * to date. Error out
3064 */
3065 if (block_start == EXTENT_MAP_INLINE) {
3066 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003067 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003068 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003069 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003070 continue;
3071 }
Chris Masond1310b22008-01-24 16:13:08 -05003072
David Sterba4b81ba42017-06-06 19:14:26 +02003073 ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL,
David Sterba6273b7f2017-10-04 17:30:11 +02003074 page, offset, disk_io_size,
3075 pg_offset, bdev, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003076 end_bio_extent_readpage, mirror_num,
3077 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003078 this_bio_flag,
3079 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003080 if (!ret) {
3081 nr++;
3082 *bio_flags = this_bio_flag;
3083 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003084 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003085 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003086 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003087 }
Chris Masond1310b22008-01-24 16:13:08 -05003088 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003089 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003090 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003091out:
Chris Masond1310b22008-01-24 16:13:08 -05003092 if (!nr) {
3093 if (!PageError(page))
3094 SetPageUptodate(page);
3095 unlock_page(page);
3096 }
Liu Bobaf863b2016-07-11 10:39:07 -07003097 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003098}
3099
Nikolay Borisove65ef212019-03-11 09:55:38 +02003100static inline void contiguous_readpages(struct extent_io_tree *tree,
Miao Xie99740902013-07-25 19:22:36 +08003101 struct page *pages[], int nr_pages,
3102 u64 start, u64 end,
Miao Xie125bac012013-07-25 19:22:37 +08003103 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003104 struct bio **bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003105 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003106 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003107{
3108 struct inode *inode;
3109 struct btrfs_ordered_extent *ordered;
3110 int index;
3111
3112 inode = pages[0]->mapping->host;
3113 while (1) {
3114 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003115 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Miao Xie99740902013-07-25 19:22:36 +08003116 end - start + 1);
3117 if (!ordered)
3118 break;
3119 unlock_extent(tree, start, end);
3120 btrfs_start_ordered_extent(inode, ordered, 1);
3121 btrfs_put_ordered_extent(ordered);
3122 }
3123
3124 for (index = 0; index < nr_pages; index++) {
David Sterba4ef77692017-06-23 04:09:57 +02003125 __do_readpage(tree, pages[index], btrfs_get_extent, em_cached,
Jens Axboe5e9d3982018-08-17 15:45:39 -07003126 bio, 0, bio_flags, REQ_RAHEAD, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003127 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003128 }
3129}
3130
Miao Xie99740902013-07-25 19:22:36 +08003131static int __extent_read_full_page(struct extent_io_tree *tree,
3132 struct page *page,
3133 get_extent_t *get_extent,
3134 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003135 unsigned long *bio_flags,
3136 unsigned int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003137{
3138 struct inode *inode = page->mapping->host;
3139 struct btrfs_ordered_extent *ordered;
3140 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003141 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003142 int ret;
3143
3144 while (1) {
3145 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003146 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003147 PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003148 if (!ordered)
3149 break;
3150 unlock_extent(tree, start, end);
3151 btrfs_start_ordered_extent(inode, ordered, 1);
3152 btrfs_put_ordered_extent(ordered);
3153 }
3154
Miao Xie125bac012013-07-25 19:22:37 +08003155 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003156 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003157 return ret;
3158}
3159
Chris Masond1310b22008-01-24 16:13:08 -05003160int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003161 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003162{
3163 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003164 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003165 int ret;
3166
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003167 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003168 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003169 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003170 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003171 return ret;
3172}
Chris Masond1310b22008-01-24 16:13:08 -05003173
David Sterba3d4b9492017-02-10 19:33:41 +01003174static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003175 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003176{
3177 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003178}
3179
Chris Masond1310b22008-01-24 16:13:08 -05003180/*
Chris Mason40f76582014-05-21 13:35:51 -07003181 * helper for __extent_writepage, doing all of the delayed allocation setup.
3182 *
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003183 * This returns 1 if btrfs_run_delalloc_range function did all the work required
Chris Mason40f76582014-05-21 13:35:51 -07003184 * to write the page (copy into inline extent). In this case the IO has
3185 * been started and the page is already unlocked.
3186 *
3187 * This returns 0 if all went well (page still locked)
3188 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003189 */
Chris Mason40f76582014-05-21 13:35:51 -07003190static noinline_for_stack int writepage_delalloc(struct inode *inode,
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003191 struct page *page, struct writeback_control *wbc,
3192 u64 delalloc_start, unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003193{
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003194 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003195 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Lu Fengqi3522e902018-11-29 11:33:38 +08003196 bool found;
Chris Mason40f76582014-05-21 13:35:51 -07003197 u64 delalloc_to_write = 0;
3198 u64 delalloc_end = 0;
3199 int ret;
3200 int page_started = 0;
3201
Chris Mason40f76582014-05-21 13:35:51 -07003202
3203 while (delalloc_end < page_end) {
Lu Fengqi3522e902018-11-29 11:33:38 +08003204 found = find_lock_delalloc_range(inode, tree,
Chris Mason40f76582014-05-21 13:35:51 -07003205 page,
3206 &delalloc_start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03003207 &delalloc_end);
Lu Fengqi3522e902018-11-29 11:33:38 +08003208 if (!found) {
Chris Mason40f76582014-05-21 13:35:51 -07003209 delalloc_start = delalloc_end + 1;
3210 continue;
3211 }
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003212 ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
3213 delalloc_end, &page_started, nr_written, wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003214 /* File system has been set read-only */
3215 if (ret) {
3216 SetPageError(page);
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003217 /*
3218 * btrfs_run_delalloc_range should return < 0 for error
3219 * but just in case, we use > 0 here meaning the IO is
3220 * started, so we don't want to return > 0 unless
3221 * things are going well.
Chris Mason40f76582014-05-21 13:35:51 -07003222 */
3223 ret = ret < 0 ? ret : -EIO;
3224 goto done;
3225 }
3226 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003227 * delalloc_end is already one less than the total length, so
3228 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003229 */
3230 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003231 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003232 delalloc_start = delalloc_end + 1;
3233 }
3234 if (wbc->nr_to_write < delalloc_to_write) {
3235 int thresh = 8192;
3236
3237 if (delalloc_to_write < thresh * 2)
3238 thresh = delalloc_to_write;
3239 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3240 thresh);
3241 }
3242
3243 /* did the fill delalloc function already unlock and start
3244 * the IO?
3245 */
3246 if (page_started) {
3247 /*
3248 * we've unlocked the page, so we can't update
3249 * the mapping's writeback index, just update
3250 * nr_to_write.
3251 */
3252 wbc->nr_to_write -= *nr_written;
3253 return 1;
3254 }
3255
3256 ret = 0;
3257
3258done:
3259 return ret;
3260}
3261
3262/*
3263 * helper for __extent_writepage. This calls the writepage start hooks,
3264 * and does the loop to map the page into extents and bios.
3265 *
3266 * We return 1 if the IO is started and the page is unlocked,
3267 * 0 if all went well (page still locked)
3268 * < 0 if there were errors (page still locked)
3269 */
3270static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3271 struct page *page,
3272 struct writeback_control *wbc,
3273 struct extent_page_data *epd,
3274 loff_t i_size,
3275 unsigned long nr_written,
David Sterbaf1c77c52017-06-06 19:03:49 +02003276 unsigned int write_flags, int *nr_ret)
Chris Mason40f76582014-05-21 13:35:51 -07003277{
Chris Masond1310b22008-01-24 16:13:08 -05003278 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003279 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003280 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003281 u64 end;
3282 u64 cur = start;
3283 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003284 u64 block_start;
3285 u64 iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003286 struct extent_map *em;
3287 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003288 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003289 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003290 int ret = 0;
3291 int nr = 0;
3292 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003293
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003294 ret = btrfs_writepage_cow_fixup(page, start, page_end);
3295 if (ret) {
3296 /* Fixup worker will requeue */
3297 if (ret == -EBUSY)
3298 wbc->pages_skipped++;
3299 else
3300 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003301
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003302 update_nr_written(wbc, nr_written);
3303 unlock_page(page);
3304 return 1;
Chris Mason247e7432008-07-17 12:53:51 -04003305 }
3306
Chris Mason11c83492009-04-20 15:50:09 -04003307 /*
3308 * we don't want to touch the inode after unlocking the page,
3309 * so we update the mapping writeback index now
3310 */
David Sterba3d4b9492017-02-10 19:33:41 +01003311 update_nr_written(wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003312
Chris Masond1310b22008-01-24 16:13:08 -05003313 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003314 if (i_size <= start) {
Nikolay Borisovc6297322018-11-08 10:18:08 +02003315 btrfs_writepage_endio_finish_ordered(page, start, page_end, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003316 goto done;
3317 }
3318
Chris Masond1310b22008-01-24 16:13:08 -05003319 blocksize = inode->i_sb->s_blocksize;
3320
3321 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003322 u64 em_end;
David Sterba6273b7f2017-10-04 17:30:11 +02003323 u64 offset;
David Sterba58409ed2016-05-04 11:46:10 +02003324
Chris Mason40f76582014-05-21 13:35:51 -07003325 if (cur >= i_size) {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003326 btrfs_writepage_endio_finish_ordered(page, cur,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003327 page_end, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003328 break;
3329 }
David Sterba3c98c622017-06-23 04:01:08 +02003330 em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003331 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003332 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003333 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003334 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003335 break;
3336 }
3337
3338 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003339 em_end = extent_map_end(em);
3340 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003341 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003342 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003343 iosize = ALIGN(iosize, blocksize);
David Sterba6273b7f2017-10-04 17:30:11 +02003344 offset = em->block_start + extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003345 bdev = em->bdev;
3346 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003347 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003348 free_extent_map(em);
3349 em = NULL;
3350
Chris Masonc8b97812008-10-29 14:49:59 -04003351 /*
3352 * compressed and inline extents are written through other
3353 * paths in the FS
3354 */
3355 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003356 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003357 /*
3358 * end_io notification does not happen here for
3359 * compressed extents
3360 */
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003361 if (!compressed)
3362 btrfs_writepage_endio_finish_ordered(page, cur,
3363 cur + iosize - 1,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003364 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003365 else if (compressed) {
3366 /* we don't want to end_page_writeback on
3367 * a compressed extent. this happens
3368 * elsewhere
3369 */
3370 nr++;
3371 }
3372
3373 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003374 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003375 continue;
3376 }
Chris Masonc8b97812008-10-29 14:49:59 -04003377
David Sterba5cdc84b2018-07-18 20:32:52 +02003378 btrfs_set_range_writeback(tree, cur, cur + iosize - 1);
David Sterba58409ed2016-05-04 11:46:10 +02003379 if (!PageWriteback(page)) {
3380 btrfs_err(BTRFS_I(inode)->root->fs_info,
3381 "page %lu not writeback, cur %llu end %llu",
3382 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003383 }
David Sterba58409ed2016-05-04 11:46:10 +02003384
David Sterba4b81ba42017-06-06 19:14:26 +02003385 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003386 page, offset, iosize, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003387 bdev, &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003388 end_bio_extent_writepage,
3389 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003390 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003391 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003392 if (PageWriteback(page))
3393 end_page_writeback(page);
3394 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003395
Chris Masond1310b22008-01-24 16:13:08 -05003396 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003397 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003398 nr++;
3399 }
3400done:
Chris Mason40f76582014-05-21 13:35:51 -07003401 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003402 return ret;
3403}
3404
3405/*
3406 * the writepage semantics are similar to regular writepage. extent
3407 * records are inserted to lock ranges in the tree, and as dirty areas
3408 * are found, they are marked writeback. Then the lock bits are removed
3409 * and the end_io handler clears the writeback ranges
Qu Wenruo30659762019-03-20 14:27:42 +08003410 *
3411 * Return 0 if everything goes well.
3412 * Return <0 for error.
Chris Mason40f76582014-05-21 13:35:51 -07003413 */
3414static int __extent_writepage(struct page *page, struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003415 struct extent_page_data *epd)
Chris Mason40f76582014-05-21 13:35:51 -07003416{
3417 struct inode *inode = page->mapping->host;
Chris Mason40f76582014-05-21 13:35:51 -07003418 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003419 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003420 int ret;
3421 int nr = 0;
3422 size_t pg_offset = 0;
3423 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003424 unsigned long end_index = i_size >> PAGE_SHIFT;
David Sterbaf1c77c52017-06-06 19:03:49 +02003425 unsigned int write_flags = 0;
Chris Mason40f76582014-05-21 13:35:51 -07003426 unsigned long nr_written = 0;
3427
Liu Boff40adf2017-08-24 18:19:48 -06003428 write_flags = wbc_to_write_flags(wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003429
3430 trace___extent_writepage(page, inode, wbc);
3431
3432 WARN_ON(!PageLocked(page));
3433
3434 ClearPageError(page);
3435
Johannes Thumshirn70730172018-12-05 15:23:03 +01003436 pg_offset = offset_in_page(i_size);
Chris Mason40f76582014-05-21 13:35:51 -07003437 if (page->index > end_index ||
3438 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003439 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003440 unlock_page(page);
3441 return 0;
3442 }
3443
3444 if (page->index == end_index) {
3445 char *userpage;
3446
3447 userpage = kmap_atomic(page);
3448 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003449 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003450 kunmap_atomic(userpage);
3451 flush_dcache_page(page);
3452 }
3453
3454 pg_offset = 0;
3455
3456 set_page_extent_mapped(page);
3457
Nikolay Borisov7789a552018-11-08 10:18:06 +02003458 if (!epd->extent_locked) {
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003459 ret = writepage_delalloc(inode, page, wbc, start, &nr_written);
Nikolay Borisov7789a552018-11-08 10:18:06 +02003460 if (ret == 1)
3461 goto done_unlocked;
3462 if (ret)
3463 goto done;
3464 }
Chris Mason40f76582014-05-21 13:35:51 -07003465
3466 ret = __extent_writepage_io(inode, page, wbc, epd,
3467 i_size, nr_written, write_flags, &nr);
3468 if (ret == 1)
3469 goto done_unlocked;
3470
3471done:
Chris Masond1310b22008-01-24 16:13:08 -05003472 if (nr == 0) {
3473 /* make sure the mapping tag for page dirty gets cleared */
3474 set_page_writeback(page);
3475 end_page_writeback(page);
3476 }
Filipe Manana61391d52014-05-09 17:17:40 +01003477 if (PageError(page)) {
3478 ret = ret < 0 ? ret : -EIO;
3479 end_extent_writepage(page, ret, start, page_end);
3480 }
Chris Masond1310b22008-01-24 16:13:08 -05003481 unlock_page(page);
Qu Wenruo30659762019-03-20 14:27:42 +08003482 ASSERT(ret <= 0);
Chris Mason40f76582014-05-21 13:35:51 -07003483 return ret;
Chris Mason771ed682008-11-06 22:02:51 -05003484
Chris Mason11c83492009-04-20 15:50:09 -04003485done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003486 return 0;
3487}
3488
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003489void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003490{
NeilBrown74316202014-07-07 15:16:04 +10003491 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3492 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003493}
3494
Chris Mason0e378df2014-05-19 20:55:27 -07003495static noinline_for_stack int
3496lock_extent_buffer_for_io(struct extent_buffer *eb,
3497 struct btrfs_fs_info *fs_info,
3498 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003499{
David Sterbacc5e31a2018-03-01 18:20:27 +01003500 int i, num_pages;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003501 int flush = 0;
3502 int ret = 0;
3503
3504 if (!btrfs_try_tree_write_lock(eb)) {
3505 flush = 1;
Qu Wenruof4340622019-03-20 14:27:41 +08003506 ret = flush_write_bio(epd);
3507 BUG_ON(ret < 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003508 btrfs_tree_lock(eb);
3509 }
3510
3511 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3512 btrfs_tree_unlock(eb);
3513 if (!epd->sync_io)
3514 return 0;
3515 if (!flush) {
Qu Wenruof4340622019-03-20 14:27:41 +08003516 ret = flush_write_bio(epd);
3517 BUG_ON(ret < 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003518 flush = 1;
3519 }
Chris Masona098d8e82012-03-21 12:09:56 -04003520 while (1) {
3521 wait_on_extent_buffer_writeback(eb);
3522 btrfs_tree_lock(eb);
3523 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3524 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003525 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003526 }
3527 }
3528
Josef Bacik51561ff2012-07-20 16:25:24 -04003529 /*
3530 * We need to do this to prevent races in people who check if the eb is
3531 * under IO since we can end up having no IO bits set for a short period
3532 * of time.
3533 */
3534 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003535 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3536 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003537 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003538 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Nikolay Borisov104b4e52017-06-20 21:01:20 +03003539 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3540 -eb->len,
3541 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003542 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003543 } else {
3544 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003545 }
3546
3547 btrfs_tree_unlock(eb);
3548
3549 if (!ret)
3550 return ret;
3551
David Sterba65ad0102018-06-29 10:56:49 +02003552 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003553 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003554 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003555
3556 if (!trylock_page(p)) {
3557 if (!flush) {
Qu Wenruof4340622019-03-20 14:27:41 +08003558 ret = flush_write_bio(epd);
3559 BUG_ON(ret < 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003560 flush = 1;
3561 }
3562 lock_page(p);
3563 }
3564 }
3565
3566 return ret;
3567}
3568
3569static void end_extent_buffer_writeback(struct extent_buffer *eb)
3570{
3571 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003572 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003573 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3574}
3575
Filipe Manana656f30d2014-09-26 12:25:56 +01003576static void set_btree_ioerr(struct page *page)
3577{
3578 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Filipe Manana656f30d2014-09-26 12:25:56 +01003579
3580 SetPageError(page);
3581 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3582 return;
3583
3584 /*
3585 * If writeback for a btree extent that doesn't belong to a log tree
3586 * failed, increment the counter transaction->eb_write_errors.
3587 * We do this because while the transaction is running and before it's
3588 * committing (when we call filemap_fdata[write|wait]_range against
3589 * the btree inode), we might have
3590 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3591 * returns an error or an error happens during writeback, when we're
3592 * committing the transaction we wouldn't know about it, since the pages
3593 * can be no longer dirty nor marked anymore for writeback (if a
3594 * subsequent modification to the extent buffer didn't happen before the
3595 * transaction commit), which makes filemap_fdata[write|wait]_range not
3596 * able to find the pages tagged with SetPageError at transaction
3597 * commit time. So if this happens we must abort the transaction,
3598 * otherwise we commit a super block with btree roots that point to
3599 * btree nodes/leafs whose content on disk is invalid - either garbage
3600 * or the content of some node/leaf from a past generation that got
3601 * cowed or deleted and is no longer valid.
3602 *
3603 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3604 * not be enough - we need to distinguish between log tree extents vs
3605 * non-log tree extents, and the next filemap_fdatawait_range() call
3606 * will catch and clear such errors in the mapping - and that call might
3607 * be from a log sync and not from a transaction commit. Also, checking
3608 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3609 * not done and would not be reliable - the eb might have been released
3610 * from memory and reading it back again means that flag would not be
3611 * set (since it's a runtime flag, not persisted on disk).
3612 *
3613 * Using the flags below in the btree inode also makes us achieve the
3614 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3615 * writeback for all dirty pages and before filemap_fdatawait_range()
3616 * is called, the writeback for all dirty pages had already finished
3617 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3618 * filemap_fdatawait_range() would return success, as it could not know
3619 * that writeback errors happened (the pages were no longer tagged for
3620 * writeback).
3621 */
3622 switch (eb->log_index) {
3623 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003624 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003625 break;
3626 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003627 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003628 break;
3629 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003630 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003631 break;
3632 default:
3633 BUG(); /* unexpected, logic error */
3634 }
3635}
3636
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003637static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003638{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003639 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003640 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003641 int i, done;
Ming Lei6dc4f102019-02-15 19:13:19 +08003642 struct bvec_iter_all iter_all;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003643
David Sterbac09abff2017-07-13 18:10:07 +02003644 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08003645 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003646 struct page *page = bvec->bv_page;
3647
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003648 eb = (struct extent_buffer *)page->private;
3649 BUG_ON(!eb);
3650 done = atomic_dec_and_test(&eb->io_pages);
3651
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003652 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003653 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003654 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003655 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003656 }
3657
3658 end_page_writeback(page);
3659
3660 if (!done)
3661 continue;
3662
3663 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003664 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003665
3666 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003667}
3668
Chris Mason0e378df2014-05-19 20:55:27 -07003669static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003670 struct btrfs_fs_info *fs_info,
3671 struct writeback_control *wbc,
3672 struct extent_page_data *epd)
3673{
3674 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003675 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003676 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003677 u32 nritems;
David Sterbacc5e31a2018-03-01 18:20:27 +01003678 int i, num_pages;
Liu Bo851cd172016-09-23 13:44:44 -07003679 unsigned long start, end;
Liu Boff40adf2017-08-24 18:19:48 -06003680 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003681 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003682
Filipe Manana656f30d2014-09-26 12:25:56 +01003683 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02003684 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003685 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003686
Liu Bo851cd172016-09-23 13:44:44 -07003687 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3688 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003689 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003690 end = btrfs_node_key_ptr_offset(nritems);
3691
David Sterbab159fa22016-11-08 18:09:03 +01003692 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003693 } else {
3694 /*
3695 * leaf:
3696 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3697 */
3698 start = btrfs_item_nr_offset(nritems);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003699 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, eb);
David Sterbab159fa22016-11-08 18:09:03 +01003700 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003701 }
3702
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003703 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003704 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003705
3706 clear_page_dirty_for_io(p);
3707 set_page_writeback(p);
David Sterba4b81ba42017-06-06 19:14:26 +02003708 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003709 p, offset, PAGE_SIZE, 0, bdev,
David Sterbac2df8bb2017-02-10 19:29:38 +01003710 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003711 end_bio_extent_buffer_writepage,
Liu Bo18fdc672017-09-13 12:18:22 -06003712 0, 0, 0, false);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003713 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003714 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003715 if (PageWriteback(p))
3716 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003717 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3718 end_extent_buffer_writeback(eb);
3719 ret = -EIO;
3720 break;
3721 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003722 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003723 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003724 unlock_page(p);
3725 }
3726
3727 if (unlikely(ret)) {
3728 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003729 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003730 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003731 unlock_page(p);
3732 }
3733 }
3734
3735 return ret;
3736}
3737
3738int btree_write_cache_pages(struct address_space *mapping,
3739 struct writeback_control *wbc)
3740{
3741 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3742 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3743 struct extent_buffer *eb, *prev_eb = NULL;
3744 struct extent_page_data epd = {
3745 .bio = NULL,
3746 .tree = tree,
3747 .extent_locked = 0,
3748 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3749 };
3750 int ret = 0;
Qu Wenruof4340622019-03-20 14:27:41 +08003751 int flush_ret;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003752 int done = 0;
3753 int nr_to_write_done = 0;
3754 struct pagevec pvec;
3755 int nr_pages;
3756 pgoff_t index;
3757 pgoff_t end; /* Inclusive */
3758 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003759 xa_mark_t tag;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003760
Mel Gorman86679822017-11-15 17:37:52 -08003761 pagevec_init(&pvec);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003762 if (wbc->range_cyclic) {
3763 index = mapping->writeback_index; /* Start from prev offset */
3764 end = -1;
3765 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003766 index = wbc->range_start >> PAGE_SHIFT;
3767 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003768 scanned = 1;
3769 }
3770 if (wbc->sync_mode == WB_SYNC_ALL)
3771 tag = PAGECACHE_TAG_TOWRITE;
3772 else
3773 tag = PAGECACHE_TAG_DIRTY;
3774retry:
3775 if (wbc->sync_mode == WB_SYNC_ALL)
3776 tag_pages_for_writeback(mapping, index, end);
3777 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara4006f432017-11-15 17:34:37 -08003778 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08003779 tag))) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003780 unsigned i;
3781
3782 scanned = 1;
3783 for (i = 0; i < nr_pages; i++) {
3784 struct page *page = pvec.pages[i];
3785
3786 if (!PagePrivate(page))
3787 continue;
3788
Josef Bacikb5bae262012-09-14 13:43:01 -04003789 spin_lock(&mapping->private_lock);
3790 if (!PagePrivate(page)) {
3791 spin_unlock(&mapping->private_lock);
3792 continue;
3793 }
3794
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003795 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003796
3797 /*
3798 * Shouldn't happen and normally this would be a BUG_ON
3799 * but no sense in crashing the users box for something
3800 * we can survive anyway.
3801 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303802 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003803 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003804 continue;
3805 }
3806
Josef Bacikb5bae262012-09-14 13:43:01 -04003807 if (eb == prev_eb) {
3808 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003809 continue;
3810 }
3811
Josef Bacikb5bae262012-09-14 13:43:01 -04003812 ret = atomic_inc_not_zero(&eb->refs);
3813 spin_unlock(&mapping->private_lock);
3814 if (!ret)
3815 continue;
3816
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003817 prev_eb = eb;
3818 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3819 if (!ret) {
3820 free_extent_buffer(eb);
3821 continue;
3822 }
3823
3824 ret = write_one_eb(eb, fs_info, wbc, &epd);
3825 if (ret) {
3826 done = 1;
3827 free_extent_buffer(eb);
3828 break;
3829 }
3830 free_extent_buffer(eb);
3831
3832 /*
3833 * the filesystem may choose to bump up nr_to_write.
3834 * We have to make sure to honor the new nr_to_write
3835 * at any time
3836 */
3837 nr_to_write_done = wbc->nr_to_write <= 0;
3838 }
3839 pagevec_release(&pvec);
3840 cond_resched();
3841 }
3842 if (!scanned && !done) {
3843 /*
3844 * We hit the last page and there is more work to be done: wrap
3845 * back to the start of the file
3846 */
3847 scanned = 1;
3848 index = 0;
3849 goto retry;
3850 }
Qu Wenruof4340622019-03-20 14:27:41 +08003851 flush_ret = flush_write_bio(&epd);
3852 BUG_ON(flush_ret < 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003853 return ret;
3854}
3855
Chris Masond1310b22008-01-24 16:13:08 -05003856/**
Chris Mason4bef0842008-09-08 11:18:08 -04003857 * 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 -05003858 * @mapping: address space structure to write
3859 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
David Sterba935db852017-06-23 04:30:28 +02003860 * @data: data passed to __extent_writepage function
Chris Masond1310b22008-01-24 16:13:08 -05003861 *
3862 * If a page is already under I/O, write_cache_pages() skips it, even
3863 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3864 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3865 * and msync() need to guarantee that all the data which was dirty at the time
3866 * the call was made get new I/O started against them. If wbc->sync_mode is
3867 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3868 * existing IO to complete.
3869 */
David Sterba4242b642017-02-10 19:38:24 +01003870static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04003871 struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003872 struct extent_page_data *epd)
Chris Masond1310b22008-01-24 16:13:08 -05003873{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003874 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003875 int ret = 0;
3876 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003877 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003878 struct pagevec pvec;
3879 int nr_pages;
3880 pgoff_t index;
3881 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08003882 pgoff_t done_index;
3883 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003884 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003885 xa_mark_t tag;
Chris Masond1310b22008-01-24 16:13:08 -05003886
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003887 /*
3888 * We have to hold onto the inode so that ordered extents can do their
3889 * work when the IO finishes. The alternative to this is failing to add
3890 * an ordered extent if the igrab() fails there and that is a huge pain
3891 * to deal with, so instead just hold onto the inode throughout the
3892 * writepages operation. If it fails here we are freeing up the inode
3893 * anyway and we'd rather not waste our time writing out stuff that is
3894 * going to be truncated anyway.
3895 */
3896 if (!igrab(inode))
3897 return 0;
3898
Mel Gorman86679822017-11-15 17:37:52 -08003899 pagevec_init(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05003900 if (wbc->range_cyclic) {
3901 index = mapping->writeback_index; /* Start from prev offset */
3902 end = -1;
3903 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003904 index = wbc->range_start >> PAGE_SHIFT;
3905 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08003906 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3907 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05003908 scanned = 1;
3909 }
Ethan Lien3cd24c62018-11-01 14:49:03 +08003910
3911 /*
3912 * We do the tagged writepage as long as the snapshot flush bit is set
3913 * and we are the first one who do the filemap_flush() on this inode.
3914 *
3915 * The nr_to_write == LONG_MAX is needed to make sure other flushers do
3916 * not race in and drop the bit.
3917 */
3918 if (range_whole && wbc->nr_to_write == LONG_MAX &&
3919 test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
3920 &BTRFS_I(inode)->runtime_flags))
3921 wbc->tagged_writepages = 1;
3922
3923 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00003924 tag = PAGECACHE_TAG_TOWRITE;
3925 else
3926 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003927retry:
Ethan Lien3cd24c62018-11-01 14:49:03 +08003928 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00003929 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08003930 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003931 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara67fd7072017-11-15 17:35:19 -08003932 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
3933 &index, end, tag))) {
Chris Masond1310b22008-01-24 16:13:08 -05003934 unsigned i;
3935
3936 scanned = 1;
3937 for (i = 0; i < nr_pages; i++) {
3938 struct page *page = pvec.pages[i];
3939
Liu Boa91326672016-03-07 16:56:21 -08003940 done_index = page->index;
Chris Masond1310b22008-01-24 16:13:08 -05003941 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07003942 * At this point we hold neither the i_pages lock nor
3943 * the page lock: the page may be truncated or
3944 * invalidated (changing page->mapping to NULL),
3945 * or even swizzled back from swapper_space to
3946 * tmpfs file mapping
Chris Masond1310b22008-01-24 16:13:08 -05003947 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003948 if (!trylock_page(page)) {
Qu Wenruof4340622019-03-20 14:27:41 +08003949 ret = flush_write_bio(epd);
3950 BUG_ON(ret < 0);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003951 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003952 }
Chris Masond1310b22008-01-24 16:13:08 -05003953
3954 if (unlikely(page->mapping != mapping)) {
3955 unlock_page(page);
3956 continue;
3957 }
3958
Chris Masond2c3f4f2008-11-19 12:44:22 -05003959 if (wbc->sync_mode != WB_SYNC_NONE) {
Qu Wenruof4340622019-03-20 14:27:41 +08003960 if (PageWriteback(page)) {
3961 ret = flush_write_bio(epd);
3962 BUG_ON(ret < 0);
3963 }
Chris Masond1310b22008-01-24 16:13:08 -05003964 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003965 }
Chris Masond1310b22008-01-24 16:13:08 -05003966
3967 if (PageWriteback(page) ||
3968 !clear_page_dirty_for_io(page)) {
3969 unlock_page(page);
3970 continue;
3971 }
3972
David Sterbaaab6e9e2017-11-30 18:00:02 +01003973 ret = __extent_writepage(page, wbc, epd);
Chris Masond1310b22008-01-24 16:13:08 -05003974
3975 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3976 unlock_page(page);
3977 ret = 0;
3978 }
Liu Boa91326672016-03-07 16:56:21 -08003979 if (ret < 0) {
3980 /*
3981 * done_index is set past this page,
3982 * so media errors will not choke
3983 * background writeout for the entire
3984 * file. This has consequences for
3985 * range_cyclic semantics (ie. it may
3986 * not be suitable for data integrity
3987 * writeout).
3988 */
3989 done_index = page->index + 1;
3990 done = 1;
3991 break;
3992 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003993
3994 /*
3995 * the filesystem may choose to bump up nr_to_write.
3996 * We have to make sure to honor the new nr_to_write
3997 * at any time
3998 */
3999 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004000 }
4001 pagevec_release(&pvec);
4002 cond_resched();
4003 }
Liu Bo894b36e2016-03-07 16:56:22 -08004004 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004005 /*
4006 * We hit the last page and there is more work to be done: wrap
4007 * back to the start of the file
4008 */
4009 scanned = 1;
4010 index = 0;
4011 goto retry;
4012 }
Liu Boa91326672016-03-07 16:56:21 -08004013
4014 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4015 mapping->writeback_index = done_index;
4016
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004017 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004018 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004019}
Chris Masond1310b22008-01-24 16:13:08 -05004020
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004021int extent_write_full_page(struct page *page, struct writeback_control *wbc)
Chris Masond1310b22008-01-24 16:13:08 -05004022{
4023 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004024 struct extent_page_data epd = {
4025 .bio = NULL,
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004026 .tree = &BTRFS_I(page->mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004027 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004028 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004029 };
Chris Masond1310b22008-01-24 16:13:08 -05004030
Chris Masond1310b22008-01-24 16:13:08 -05004031 ret = __extent_writepage(page, wbc, &epd);
Qu Wenruo30659762019-03-20 14:27:42 +08004032 ASSERT(ret <= 0);
4033 if (ret < 0) {
4034 end_write_bio(&epd, ret);
4035 return ret;
4036 }
Chris Masond1310b22008-01-24 16:13:08 -05004037
Qu Wenruo30659762019-03-20 14:27:42 +08004038 ret = flush_write_bio(&epd);
4039 ASSERT(ret <= 0);
Chris Masond1310b22008-01-24 16:13:08 -05004040 return ret;
4041}
Chris Masond1310b22008-01-24 16:13:08 -05004042
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004043int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
Chris Mason771ed682008-11-06 22:02:51 -05004044 int mode)
4045{
4046 int ret = 0;
Qu Wenruof4340622019-03-20 14:27:41 +08004047 int flush_ret;
Chris Mason771ed682008-11-06 22:02:51 -05004048 struct address_space *mapping = inode->i_mapping;
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004049 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Mason771ed682008-11-06 22:02:51 -05004050 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004051 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4052 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004053
4054 struct extent_page_data epd = {
4055 .bio = NULL,
4056 .tree = tree,
Chris Mason771ed682008-11-06 22:02:51 -05004057 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004058 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05004059 };
4060 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004061 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004062 .nr_to_write = nr_pages * 2,
4063 .range_start = start,
4064 .range_end = end + 1,
4065 };
4066
Chris Masond3977122009-01-05 21:25:51 -05004067 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004068 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004069 if (clear_page_dirty_for_io(page))
4070 ret = __extent_writepage(page, &wbc_writepages, &epd);
4071 else {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02004072 btrfs_writepage_endio_finish_ordered(page, start,
Nikolay Borisovc6297322018-11-08 10:18:08 +02004073 start + PAGE_SIZE - 1, 1);
Chris Mason771ed682008-11-06 22:02:51 -05004074 unlock_page(page);
4075 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004076 put_page(page);
4077 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004078 }
4079
Qu Wenruof4340622019-03-20 14:27:41 +08004080 flush_ret = flush_write_bio(&epd);
4081 BUG_ON(flush_ret < 0);
Chris Mason771ed682008-11-06 22:02:51 -05004082 return ret;
4083}
Chris Masond1310b22008-01-24 16:13:08 -05004084
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004085int extent_writepages(struct address_space *mapping,
Chris Masond1310b22008-01-24 16:13:08 -05004086 struct writeback_control *wbc)
4087{
4088 int ret = 0;
Qu Wenruof4340622019-03-20 14:27:41 +08004089 int flush_ret;
Chris Masond1310b22008-01-24 16:13:08 -05004090 struct extent_page_data epd = {
4091 .bio = NULL,
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004092 .tree = &BTRFS_I(mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004093 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004094 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004095 };
4096
David Sterba935db852017-06-23 04:30:28 +02004097 ret = extent_write_cache_pages(mapping, wbc, &epd);
Qu Wenruof4340622019-03-20 14:27:41 +08004098 flush_ret = flush_write_bio(&epd);
4099 BUG_ON(flush_ret < 0);
Chris Masond1310b22008-01-24 16:13:08 -05004100 return ret;
4101}
Chris Masond1310b22008-01-24 16:13:08 -05004102
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004103int extent_readpages(struct address_space *mapping, struct list_head *pages,
4104 unsigned nr_pages)
Chris Masond1310b22008-01-24 16:13:08 -05004105{
4106 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004107 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004108 struct page *pagepool[16];
Miao Xie125bac012013-07-25 19:22:37 +08004109 struct extent_map *em_cached = NULL;
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004110 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Liu Bo67c96842012-07-20 21:43:09 -06004111 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004112 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004113
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004114 while (!list_empty(pages)) {
Nikolay Borisove65ef212019-03-11 09:55:38 +02004115 u64 contig_end = 0;
4116
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004117 for (nr = 0; nr < ARRAY_SIZE(pagepool) && !list_empty(pages);) {
Nikolay Borisovf86196e2019-01-03 15:29:02 -08004118 struct page *page = lru_to_page(pages);
Chris Masond1310b22008-01-24 16:13:08 -05004119
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004120 prefetchw(&page->flags);
4121 list_del(&page->lru);
4122 if (add_to_page_cache_lru(page, mapping, page->index,
4123 readahead_gfp_mask(mapping))) {
4124 put_page(page);
Nikolay Borisove65ef212019-03-11 09:55:38 +02004125 break;
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004126 }
4127
4128 pagepool[nr++] = page;
Nikolay Borisove65ef212019-03-11 09:55:38 +02004129 contig_end = page_offset(page) + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004130 }
Liu Bo67c96842012-07-20 21:43:09 -06004131
Nikolay Borisove65ef212019-03-11 09:55:38 +02004132 if (nr) {
4133 u64 contig_start = page_offset(pagepool[0]);
4134
4135 ASSERT(contig_start + nr * PAGE_SIZE - 1 == contig_end);
4136
4137 contiguous_readpages(tree, pagepool, nr, contig_start,
4138 contig_end, &em_cached, &bio, &bio_flags,
4139 &prev_em_start);
4140 }
Chris Masond1310b22008-01-24 16:13:08 -05004141 }
Liu Bo67c96842012-07-20 21:43:09 -06004142
Miao Xie125bac012013-07-25 19:22:37 +08004143 if (em_cached)
4144 free_extent_map(em_cached);
4145
Chris Masond1310b22008-01-24 16:13:08 -05004146 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004147 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004148 return 0;
4149}
Chris Masond1310b22008-01-24 16:13:08 -05004150
4151/*
4152 * basic invalidatepage code, this waits on any locked or writeback
4153 * ranges corresponding to the page, and then deletes any extent state
4154 * records from the tree
4155 */
4156int extent_invalidatepage(struct extent_io_tree *tree,
4157 struct page *page, unsigned long offset)
4158{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004159 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004160 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004161 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004162 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4163
Qu Wenruofda28322013-02-26 08:10:22 +00004164 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004165 if (start > end)
4166 return 0;
4167
David Sterbaff13db42015-12-03 14:30:40 +01004168 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004169 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004170 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004171 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4172 EXTENT_DO_ACCOUNTING,
David Sterbaae0f1622017-10-31 16:37:52 +01004173 1, 1, &cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05004174 return 0;
4175}
Chris Masond1310b22008-01-24 16:13:08 -05004176
4177/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004178 * a helper for releasepage, this tests for areas of the page that
4179 * are locked or under IO and drops the related state bits if it is safe
4180 * to drop the page.
4181 */
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004182static int try_release_extent_state(struct extent_io_tree *tree,
Eric Sandeen48a3b632013-04-25 20:41:01 +00004183 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004184{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004185 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004186 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004187 int ret = 1;
4188
Nikolay Borisov88826792019-03-14 15:28:31 +02004189 if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) {
Chris Mason7b13b7b2008-04-18 10:29:50 -04004190 ret = 0;
Nikolay Borisov88826792019-03-14 15:28:31 +02004191 } else {
Chris Mason11ef1602009-09-23 20:28:46 -04004192 /*
4193 * at this point we can safely clear everything except the
4194 * locked bit and the nodatasum bit
4195 */
David Sterba66b0c882017-10-31 16:30:47 +01004196 ret = __clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004197 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
David Sterba66b0c882017-10-31 16:30:47 +01004198 0, 0, NULL, mask, NULL);
Chris Masone3f24cc2011-02-14 12:52:08 -05004199
4200 /* if clear_extent_bit failed for enomem reasons,
4201 * we can't allow the release to continue.
4202 */
4203 if (ret < 0)
4204 ret = 0;
4205 else
4206 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004207 }
4208 return ret;
4209}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004210
4211/*
Chris Masond1310b22008-01-24 16:13:08 -05004212 * a helper for releasepage. As long as there are no locked extents
4213 * in the range corresponding to the page, both state records and extent
4214 * map records are removed
4215 */
Nikolay Borisov477a30b2018-04-19 10:46:34 +03004216int try_release_extent_mapping(struct page *page, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004217{
4218 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004219 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004220 u64 end = start + PAGE_SIZE - 1;
Filipe Mananabd3599a2018-07-12 01:36:43 +01004221 struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
4222 struct extent_io_tree *tree = &btrfs_inode->io_tree;
4223 struct extent_map_tree *map = &btrfs_inode->extent_tree;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004224
Mel Gormand0164ad2015-11-06 16:28:21 -08004225 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004226 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004227 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004228 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004229 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004230 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004231 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004232 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004233 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004234 break;
4235 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004236 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4237 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004238 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004239 free_extent_map(em);
4240 break;
4241 }
4242 if (!test_range_bit(tree, em->start,
4243 extent_map_end(em) - 1,
Nikolay Borisov4e586ca2019-03-14 15:28:30 +02004244 EXTENT_LOCKED, 0, NULL)) {
Filipe Mananabd3599a2018-07-12 01:36:43 +01004245 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4246 &btrfs_inode->runtime_flags);
Chris Mason70dec802008-01-29 09:59:12 -05004247 remove_extent_mapping(map, em);
4248 /* once for the rb tree */
4249 free_extent_map(em);
4250 }
4251 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004252 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004253
4254 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004255 free_extent_map(em);
4256 }
Chris Masond1310b22008-01-24 16:13:08 -05004257 }
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004258 return try_release_extent_state(tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004259}
Chris Masond1310b22008-01-24 16:13:08 -05004260
Chris Masonec29ed52011-02-23 16:23:20 -05004261/*
4262 * helper function for fiemap, which doesn't want to see any holes.
4263 * This maps until we find something past 'last'
4264 */
4265static struct extent_map *get_extent_skip_holes(struct inode *inode,
David Sterbae3350e12017-06-23 04:09:57 +02004266 u64 offset, u64 last)
Chris Masonec29ed52011-02-23 16:23:20 -05004267{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004268 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004269 struct extent_map *em;
4270 u64 len;
4271
4272 if (offset >= last)
4273 return NULL;
4274
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304275 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004276 len = last - offset;
4277 if (len == 0)
4278 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004279 len = ALIGN(len, sectorsize);
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02004280 em = btrfs_get_extent_fiemap(BTRFS_I(inode), offset, len);
David Sterbac7040052011-04-19 18:00:01 +02004281 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004282 return em;
4283
4284 /* if this isn't a hole return it */
Nikolay Borisov4a2d25c2017-11-23 10:51:43 +02004285 if (em->block_start != EXTENT_MAP_HOLE)
Chris Masonec29ed52011-02-23 16:23:20 -05004286 return em;
Chris Masonec29ed52011-02-23 16:23:20 -05004287
4288 /* this is a hole, advance to the next extent */
4289 offset = extent_map_end(em);
4290 free_extent_map(em);
4291 if (offset >= last)
4292 break;
4293 }
4294 return NULL;
4295}
4296
Qu Wenruo47518322017-04-07 10:43:15 +08004297/*
4298 * To cache previous fiemap extent
4299 *
4300 * Will be used for merging fiemap extent
4301 */
4302struct fiemap_cache {
4303 u64 offset;
4304 u64 phys;
4305 u64 len;
4306 u32 flags;
4307 bool cached;
4308};
4309
4310/*
4311 * Helper to submit fiemap extent.
4312 *
4313 * Will try to merge current fiemap extent specified by @offset, @phys,
4314 * @len and @flags with cached one.
4315 * And only when we fails to merge, cached one will be submitted as
4316 * fiemap extent.
4317 *
4318 * Return value is the same as fiemap_fill_next_extent().
4319 */
4320static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4321 struct fiemap_cache *cache,
4322 u64 offset, u64 phys, u64 len, u32 flags)
4323{
4324 int ret = 0;
4325
4326 if (!cache->cached)
4327 goto assign;
4328
4329 /*
4330 * Sanity check, extent_fiemap() should have ensured that new
Andrea Gelmini52042d82018-11-28 12:05:13 +01004331 * fiemap extent won't overlap with cached one.
Qu Wenruo47518322017-04-07 10:43:15 +08004332 * Not recoverable.
4333 *
4334 * NOTE: Physical address can overlap, due to compression
4335 */
4336 if (cache->offset + cache->len > offset) {
4337 WARN_ON(1);
4338 return -EINVAL;
4339 }
4340
4341 /*
4342 * Only merges fiemap extents if
4343 * 1) Their logical addresses are continuous
4344 *
4345 * 2) Their physical addresses are continuous
4346 * So truly compressed (physical size smaller than logical size)
4347 * extents won't get merged with each other
4348 *
4349 * 3) Share same flags except FIEMAP_EXTENT_LAST
4350 * So regular extent won't get merged with prealloc extent
4351 */
4352 if (cache->offset + cache->len == offset &&
4353 cache->phys + cache->len == phys &&
4354 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4355 (flags & ~FIEMAP_EXTENT_LAST)) {
4356 cache->len += len;
4357 cache->flags |= flags;
4358 goto try_submit_last;
4359 }
4360
4361 /* Not mergeable, need to submit cached one */
4362 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4363 cache->len, cache->flags);
4364 cache->cached = false;
4365 if (ret)
4366 return ret;
4367assign:
4368 cache->cached = true;
4369 cache->offset = offset;
4370 cache->phys = phys;
4371 cache->len = len;
4372 cache->flags = flags;
4373try_submit_last:
4374 if (cache->flags & FIEMAP_EXTENT_LAST) {
4375 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4376 cache->phys, cache->len, cache->flags);
4377 cache->cached = false;
4378 }
4379 return ret;
4380}
4381
4382/*
Qu Wenruo848c23b2017-06-22 10:01:21 +08004383 * Emit last fiemap cache
Qu Wenruo47518322017-04-07 10:43:15 +08004384 *
Qu Wenruo848c23b2017-06-22 10:01:21 +08004385 * The last fiemap cache may still be cached in the following case:
4386 * 0 4k 8k
4387 * |<- Fiemap range ->|
4388 * |<------------ First extent ----------->|
4389 *
4390 * In this case, the first extent range will be cached but not emitted.
4391 * So we must emit it before ending extent_fiemap().
Qu Wenruo47518322017-04-07 10:43:15 +08004392 */
Qu Wenruo848c23b2017-06-22 10:01:21 +08004393static int emit_last_fiemap_cache(struct btrfs_fs_info *fs_info,
4394 struct fiemap_extent_info *fieinfo,
4395 struct fiemap_cache *cache)
Qu Wenruo47518322017-04-07 10:43:15 +08004396{
4397 int ret;
4398
4399 if (!cache->cached)
4400 return 0;
4401
Qu Wenruo47518322017-04-07 10:43:15 +08004402 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4403 cache->len, cache->flags);
4404 cache->cached = false;
4405 if (ret > 0)
4406 ret = 0;
4407 return ret;
4408}
4409
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004410int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterba2135fb92017-06-23 04:09:57 +02004411 __u64 start, __u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004412{
Josef Bacik975f84f2010-11-23 19:36:57 +00004413 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004414 u64 off = start;
4415 u64 max = start + len;
4416 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004417 u32 found_type;
4418 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004419 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004420 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004421 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004422 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004423 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004424 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004425 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004426 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004427 struct fiemap_cache cache = { 0 };
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004428 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004429 u64 em_start = 0;
4430 u64 em_len = 0;
4431 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004432
4433 if (len == 0)
4434 return -EINVAL;
4435
Josef Bacik975f84f2010-11-23 19:36:57 +00004436 path = btrfs_alloc_path();
4437 if (!path)
4438 return -ENOMEM;
4439 path->leave_spinning = 1;
4440
Jeff Mahoneyda170662016-06-15 09:22:56 -04004441 start = round_down(start, btrfs_inode_sectorsize(inode));
4442 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004443
Chris Masonec29ed52011-02-23 16:23:20 -05004444 /*
4445 * lookup the last file extent. We're not using i_size here
4446 * because there might be preallocation past i_size
4447 */
David Sterbaf85b7372017-01-20 14:54:07 +01004448 ret = btrfs_lookup_file_extent(NULL, root, path,
4449 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004450 if (ret < 0) {
4451 btrfs_free_path(path);
4452 return ret;
Liu Bo2d324f52016-05-17 17:21:48 -07004453 } else {
4454 WARN_ON(!ret);
4455 if (ret == 1)
4456 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004457 }
Liu Bo2d324f52016-05-17 17:21:48 -07004458
Josef Bacik975f84f2010-11-23 19:36:57 +00004459 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004460 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004461 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004462
Chris Masonec29ed52011-02-23 16:23:20 -05004463 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004464 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004465 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004466 /* have to trust i_size as the end */
4467 last = (u64)-1;
4468 last_for_get_extent = isize;
4469 } else {
4470 /*
4471 * remember the start of the last extent. There are a
4472 * bunch of different factors that go into the length of the
4473 * extent, so its much less complex to remember where it started
4474 */
4475 last = found_key.offset;
4476 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004477 }
Liu Bofe09e162013-09-22 12:54:23 +08004478 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004479
Chris Masonec29ed52011-02-23 16:23:20 -05004480 /*
4481 * we might have some extents allocated but more delalloc past those
4482 * extents. so, we trust isize unless the start of the last extent is
4483 * beyond isize
4484 */
4485 if (last < isize) {
4486 last = (u64)-1;
4487 last_for_get_extent = isize;
4488 }
4489
David Sterbaff13db42015-12-03 14:30:40 +01004490 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004491 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004492
David Sterbae3350e12017-06-23 04:09:57 +02004493 em = get_extent_skip_holes(inode, start, last_for_get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004494 if (!em)
4495 goto out;
4496 if (IS_ERR(em)) {
4497 ret = PTR_ERR(em);
4498 goto out;
4499 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004500
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004501 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004502 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004503
Chris Masonea8efc72011-03-08 11:54:40 -05004504 /* break if the extent we found is outside the range */
4505 if (em->start >= max || extent_map_end(em) < off)
4506 break;
4507
4508 /*
4509 * get_extent may return an extent that starts before our
4510 * requested range. We have to make sure the ranges
4511 * we return to fiemap always move forward and don't
4512 * overlap, so adjust the offsets here
4513 */
4514 em_start = max(em->start, off);
4515
4516 /*
4517 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004518 * for adjusting the disk offset below. Only do this if the
4519 * extent isn't compressed since our in ram offset may be past
4520 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004521 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004522 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4523 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004524 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004525 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004526 flags = 0;
Filipe Mananaf0986312018-06-20 10:02:30 +01004527 if (em->block_start < EXTENT_MAP_LAST_BYTE)
4528 disko = em->block_start + offset_in_extent;
4529 else
4530 disko = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004531
Chris Masonea8efc72011-03-08 11:54:40 -05004532 /*
4533 * bump off for our next call to get_extent
4534 */
4535 off = extent_map_end(em);
4536 if (off >= max)
4537 end = 1;
4538
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004539 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004540 end = 1;
4541 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004542 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004543 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4544 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004545 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004546 flags |= (FIEMAP_EXTENT_DELALLOC |
4547 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004548 } else if (fieinfo->fi_extents_max) {
4549 u64 bytenr = em->block_start -
4550 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004551
Liu Bofe09e162013-09-22 12:54:23 +08004552 /*
4553 * As btrfs supports shared space, this information
4554 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004555 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4556 * then we're just getting a count and we can skip the
4557 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004558 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06004559 ret = btrfs_check_shared(root,
4560 btrfs_ino(BTRFS_I(inode)),
4561 bytenr);
Josef Bacikdc046b12014-09-10 16:20:45 -04004562 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004563 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004564 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004565 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004566 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004567 }
4568 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4569 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004570 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4571 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004572
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004573 free_extent_map(em);
4574 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004575 if ((em_start >= last) || em_len == (u64)-1 ||
4576 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004577 flags |= FIEMAP_EXTENT_LAST;
4578 end = 1;
4579 }
4580
Chris Masonec29ed52011-02-23 16:23:20 -05004581 /* now scan forward to see if this is really the last extent. */
David Sterbae3350e12017-06-23 04:09:57 +02004582 em = get_extent_skip_holes(inode, off, last_for_get_extent);
Chris Masonec29ed52011-02-23 16:23:20 -05004583 if (IS_ERR(em)) {
4584 ret = PTR_ERR(em);
4585 goto out;
4586 }
4587 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004588 flags |= FIEMAP_EXTENT_LAST;
4589 end = 1;
4590 }
Qu Wenruo47518322017-04-07 10:43:15 +08004591 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4592 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004593 if (ret) {
4594 if (ret == 1)
4595 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004596 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004597 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004598 }
4599out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004600 if (!ret)
Qu Wenruo848c23b2017-06-22 10:01:21 +08004601 ret = emit_last_fiemap_cache(root->fs_info, fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004602 free_extent_map(em);
4603out:
Liu Bofe09e162013-09-22 12:54:23 +08004604 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004605 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004606 &cached_state);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004607 return ret;
4608}
4609
Chris Mason727011e2010-08-06 13:21:20 -04004610static void __free_extent_buffer(struct extent_buffer *eb)
4611{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004612 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004613 kmem_cache_free(extent_buffer_cache, eb);
4614}
4615
Josef Bacika26e8c92014-03-28 17:07:27 -04004616int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004617{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004618 return (atomic_read(&eb->io_pages) ||
4619 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4620 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004621}
4622
Miao Xie897ca6e92010-10-26 20:57:29 -04004623/*
David Sterba55ac0132018-07-19 17:24:32 +02004624 * Release all pages attached to the extent buffer.
Miao Xie897ca6e92010-10-26 20:57:29 -04004625 */
David Sterba55ac0132018-07-19 17:24:32 +02004626static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004627{
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004628 int i;
4629 int num_pages;
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004630 int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004631
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004632 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004633
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004634 num_pages = num_extent_pages(eb);
4635 for (i = 0; i < num_pages; i++) {
4636 struct page *page = eb->pages[i];
Miao Xie897ca6e92010-10-26 20:57:29 -04004637
Forrest Liu5d2361d2015-02-09 17:31:45 +08004638 if (!page)
4639 continue;
4640 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004641 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004642 /*
4643 * We do this since we'll remove the pages after we've
4644 * removed the eb from the radix tree, so we could race
4645 * and have this page now attached to the new eb. So
4646 * only clear page_private if it's still connected to
4647 * this eb.
4648 */
4649 if (PagePrivate(page) &&
4650 page->private == (unsigned long)eb) {
4651 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4652 BUG_ON(PageDirty(page));
4653 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004654 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004655 * We need to make sure we haven't be attached
4656 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004657 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004658 ClearPagePrivate(page);
4659 set_page_private(page, 0);
4660 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004661 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004662 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004663
4664 if (mapped)
4665 spin_unlock(&page->mapping->private_lock);
4666
Nicholas D Steeves01327612016-05-19 21:18:45 -04004667 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004668 put_page(page);
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004669 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004670}
4671
4672/*
4673 * Helper for releasing the extent buffer.
4674 */
4675static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4676{
David Sterba55ac0132018-07-19 17:24:32 +02004677 btrfs_release_extent_buffer_pages(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004678 __free_extent_buffer(eb);
4679}
4680
Josef Bacikf28491e2013-12-16 13:24:27 -05004681static struct extent_buffer *
4682__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004683 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004684{
4685 struct extent_buffer *eb = NULL;
4686
Michal Hockod1b5c562015-08-19 14:17:40 +02004687 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004688 eb->start = start;
4689 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004690 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004691 eb->bflags = 0;
4692 rwlock_init(&eb->lock);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004693 atomic_set(&eb->blocking_readers, 0);
4694 atomic_set(&eb->blocking_writers, 0);
David Sterbaed1b4ed2018-08-24 16:31:17 +02004695 eb->lock_nested = false;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004696 init_waitqueue_head(&eb->write_lock_wq);
4697 init_waitqueue_head(&eb->read_lock_wq);
4698
4699 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4700
4701 spin_lock_init(&eb->refs_lock);
4702 atomic_set(&eb->refs, 1);
4703 atomic_set(&eb->io_pages, 0);
4704
4705 /*
4706 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4707 */
4708 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4709 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4710 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4711
David Sterba843ccf92018-08-24 14:56:28 +02004712#ifdef CONFIG_BTRFS_DEBUG
4713 atomic_set(&eb->spinning_writers, 0);
David Sterbaafd495a2018-08-24 15:57:38 +02004714 atomic_set(&eb->spinning_readers, 0);
David Sterba5c9c7992018-08-24 16:15:51 +02004715 atomic_set(&eb->read_locks, 0);
David Sterbac79adfc2018-08-24 16:24:26 +02004716 atomic_set(&eb->write_locks, 0);
David Sterba843ccf92018-08-24 14:56:28 +02004717#endif
4718
Josef Bacikdb7f3432013-08-07 14:54:37 -04004719 return eb;
4720}
4721
4722struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4723{
David Sterbacc5e31a2018-03-01 18:20:27 +01004724 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004725 struct page *p;
4726 struct extent_buffer *new;
David Sterbacc5e31a2018-03-01 18:20:27 +01004727 int num_pages = num_extent_pages(src);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004728
David Sterba3f556f72014-06-15 03:20:26 +02004729 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004730 if (new == NULL)
4731 return NULL;
4732
4733 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004734 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004735 if (!p) {
4736 btrfs_release_extent_buffer(new);
4737 return NULL;
4738 }
4739 attach_extent_buffer_page(new, p);
4740 WARN_ON(PageDirty(p));
4741 SetPageUptodate(p);
4742 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004743 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004744 }
4745
Josef Bacikdb7f3432013-08-07 14:54:37 -04004746 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004747 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004748
4749 return new;
4750}
4751
Omar Sandoval0f331222015-09-29 20:50:31 -07004752struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4753 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004754{
4755 struct extent_buffer *eb;
David Sterbacc5e31a2018-03-01 18:20:27 +01004756 int num_pages;
4757 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004758
David Sterba3f556f72014-06-15 03:20:26 +02004759 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004760 if (!eb)
4761 return NULL;
4762
David Sterba65ad0102018-06-29 10:56:49 +02004763 num_pages = num_extent_pages(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004764 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004765 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004766 if (!eb->pages[i])
4767 goto err;
4768 }
4769 set_extent_buffer_uptodate(eb);
4770 btrfs_set_header_nritems(eb, 0);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004771 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004772
4773 return eb;
4774err:
4775 for (; i > 0; i--)
4776 __free_page(eb->pages[i - 1]);
4777 __free_extent_buffer(eb);
4778 return NULL;
4779}
4780
Omar Sandoval0f331222015-09-29 20:50:31 -07004781struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004782 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004783{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004784 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004785}
4786
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004787static void check_buffer_tree_ref(struct extent_buffer *eb)
4788{
Chris Mason242e18c2013-01-29 17:49:37 -05004789 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004790 /* the ref bit is tricky. We have to make sure it is set
4791 * if we have the buffer dirty. Otherwise the
4792 * code to free a buffer can end up dropping a dirty
4793 * page
4794 *
4795 * Once the ref bit is set, it won't go away while the
4796 * buffer is dirty or in writeback, and it also won't
4797 * go away while we have the reference count on the
4798 * eb bumped.
4799 *
4800 * We can't just set the ref bit without bumping the
4801 * ref on the eb because free_extent_buffer might
4802 * see the ref bit and try to clear it. If this happens
4803 * free_extent_buffer might end up dropping our original
4804 * ref by mistake and freeing the page before we are able
4805 * to add one more ref.
4806 *
4807 * So bump the ref count first, then set the bit. If someone
4808 * beat us to it, drop the ref we added.
4809 */
Chris Mason242e18c2013-01-29 17:49:37 -05004810 refs = atomic_read(&eb->refs);
4811 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4812 return;
4813
Josef Bacik594831c2012-07-20 16:11:08 -04004814 spin_lock(&eb->refs_lock);
4815 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004816 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004817 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004818}
4819
Mel Gorman2457aec2014-06-04 16:10:31 -07004820static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4821 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004822{
David Sterbacc5e31a2018-03-01 18:20:27 +01004823 int num_pages, i;
Josef Bacik5df42352012-03-15 18:24:42 -04004824
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004825 check_buffer_tree_ref(eb);
4826
David Sterba65ad0102018-06-29 10:56:49 +02004827 num_pages = num_extent_pages(eb);
Josef Bacik5df42352012-03-15 18:24:42 -04004828 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004829 struct page *p = eb->pages[i];
4830
Mel Gorman2457aec2014-06-04 16:10:31 -07004831 if (p != accessed)
4832 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004833 }
4834}
4835
Josef Bacikf28491e2013-12-16 13:24:27 -05004836struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4837 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004838{
4839 struct extent_buffer *eb;
4840
4841 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004842 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004843 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004844 if (eb && atomic_inc_not_zero(&eb->refs)) {
4845 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004846 /*
4847 * Lock our eb's refs_lock to avoid races with
4848 * free_extent_buffer. When we get our eb it might be flagged
4849 * with EXTENT_BUFFER_STALE and another task running
4850 * free_extent_buffer might have seen that flag set,
4851 * eb->refs == 2, that the buffer isn't under IO (dirty and
4852 * writeback flags not set) and it's still in the tree (flag
4853 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4854 * of decrementing the extent buffer's reference count twice.
4855 * So here we could race and increment the eb's reference count,
4856 * clear its stale flag, mark it as dirty and drop our reference
4857 * before the other task finishes executing free_extent_buffer,
4858 * which would later result in an attempt to free an extent
4859 * buffer that is dirty.
4860 */
4861 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4862 spin_lock(&eb->refs_lock);
4863 spin_unlock(&eb->refs_lock);
4864 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004865 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004866 return eb;
4867 }
4868 rcu_read_unlock();
4869
4870 return NULL;
4871}
4872
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004873#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4874struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004875 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004876{
4877 struct extent_buffer *eb, *exists = NULL;
4878 int ret;
4879
4880 eb = find_extent_buffer(fs_info, start);
4881 if (eb)
4882 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04004883 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004884 if (!eb)
4885 return NULL;
4886 eb->fs_info = fs_info;
4887again:
David Sterbae1860a72016-05-09 14:11:38 +02004888 ret = radix_tree_preload(GFP_NOFS);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004889 if (ret)
4890 goto free_eb;
4891 spin_lock(&fs_info->buffer_lock);
4892 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004893 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004894 spin_unlock(&fs_info->buffer_lock);
4895 radix_tree_preload_end();
4896 if (ret == -EEXIST) {
4897 exists = find_extent_buffer(fs_info, start);
4898 if (exists)
4899 goto free_eb;
4900 else
4901 goto again;
4902 }
4903 check_buffer_tree_ref(eb);
4904 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
4905
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004906 return eb;
4907free_eb:
4908 btrfs_release_extent_buffer(eb);
4909 return exists;
4910}
4911#endif
4912
Josef Bacikf28491e2013-12-16 13:24:27 -05004913struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004914 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05004915{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004916 unsigned long len = fs_info->nodesize;
David Sterbacc5e31a2018-03-01 18:20:27 +01004917 int num_pages;
4918 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004919 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004920 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004921 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004922 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05004923 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05004924 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004925 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004926
Jeff Mahoneyda170662016-06-15 09:22:56 -04004927 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07004928 btrfs_err(fs_info, "bad tree block start %llu", start);
4929 return ERR_PTR(-EINVAL);
4930 }
4931
Josef Bacikf28491e2013-12-16 13:24:27 -05004932 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004933 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004934 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004935
David Sterba23d79d82014-06-15 02:55:29 +02004936 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04004937 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07004938 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05004939
David Sterba65ad0102018-06-29 10:56:49 +02004940 num_pages = num_extent_pages(eb);
Chris Mason727011e2010-08-06 13:21:20 -04004941 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02004942 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07004943 if (!p) {
4944 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04004945 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004946 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004947
4948 spin_lock(&mapping->private_lock);
4949 if (PagePrivate(p)) {
4950 /*
4951 * We could have already allocated an eb for this page
4952 * and attached one so lets see if we can get a ref on
4953 * the existing eb, and if we can we know it's good and
4954 * we can just return that one, else we know we can just
4955 * overwrite page->private.
4956 */
4957 exists = (struct extent_buffer *)p->private;
4958 if (atomic_inc_not_zero(&exists->refs)) {
4959 spin_unlock(&mapping->private_lock);
4960 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004961 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07004962 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004963 goto free_eb;
4964 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08004965 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004966
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004967 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004968 * Do this so attach doesn't complain and we need to
4969 * drop the ref the old guy had.
4970 */
4971 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004972 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004973 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05004974 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004975 attach_extent_buffer_page(eb, p);
4976 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004977 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04004978 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004979 if (!PageUptodate(p))
4980 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004981
4982 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03004983 * We can't unlock the pages just yet since the extent buffer
4984 * hasn't been properly inserted in the radix tree, this
4985 * opens a race with btree_releasepage which can free a page
4986 * while we are still filling in all pages for the buffer and
4987 * we could crash.
Chris Masoneb14ab82011-02-10 12:35:00 -05004988 */
Chris Masond1310b22008-01-24 16:13:08 -05004989 }
4990 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004991 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004992again:
David Sterbae1860a72016-05-09 14:11:38 +02004993 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07004994 if (ret) {
4995 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04004996 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004997 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004998
Josef Bacikf28491e2013-12-16 13:24:27 -05004999 spin_lock(&fs_info->buffer_lock);
5000 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005001 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05005002 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005003 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04005004 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005005 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005006 if (exists)
5007 goto free_eb;
5008 else
Josef Bacik115391d2012-03-09 09:51:43 -05005009 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04005010 }
Chris Mason6af118ce2008-07-22 11:18:07 -04005011 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005012 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005013 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005014
5015 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005016 * Now it's safe to unlock the pages because any calls to
5017 * btree_releasepage will correctly detect that a page belongs to a
5018 * live buffer and won't free them prematurely.
Chris Masoneb14ab82011-02-10 12:35:00 -05005019 */
Nikolay Borisov28187ae2018-07-04 10:24:51 +03005020 for (i = 0; i < num_pages; i++)
5021 unlock_page(eb->pages[i]);
Chris Masond1310b22008-01-24 16:13:08 -05005022 return eb;
5023
Chris Mason6af118ce2008-07-22 11:18:07 -04005024free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005025 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005026 for (i = 0; i < num_pages; i++) {
5027 if (eb->pages[i])
5028 unlock_page(eb->pages[i]);
5029 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005030
Miao Xie897ca6e92010-10-26 20:57:29 -04005031 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005032 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005033}
Chris Masond1310b22008-01-24 16:13:08 -05005034
Josef Bacik3083ee22012-03-09 16:01:49 -05005035static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5036{
5037 struct extent_buffer *eb =
5038 container_of(head, struct extent_buffer, rcu_head);
5039
5040 __free_extent_buffer(eb);
5041}
5042
David Sterbaf7a52a42013-04-26 14:56:29 +00005043static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005044{
Nikolay Borisov07e21c42018-06-27 16:38:23 +03005045 lockdep_assert_held(&eb->refs_lock);
5046
Josef Bacik3083ee22012-03-09 16:01:49 -05005047 WARN_ON(atomic_read(&eb->refs) == 0);
5048 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005049 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005050 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005051
Jan Schmidt815a51c2012-05-16 17:00:02 +02005052 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005053
Josef Bacikf28491e2013-12-16 13:24:27 -05005054 spin_lock(&fs_info->buffer_lock);
5055 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005056 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005057 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005058 } else {
5059 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005060 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005061
5062 /* Should be safe to release our pages at this point */
David Sterba55ac0132018-07-19 17:24:32 +02005063 btrfs_release_extent_buffer_pages(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005064#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005065 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
Josef Bacikbcb7e442015-03-16 17:38:02 -04005066 __free_extent_buffer(eb);
5067 return 1;
5068 }
5069#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005070 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005071 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005072 }
5073 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005074
5075 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005076}
5077
Chris Masond1310b22008-01-24 16:13:08 -05005078void free_extent_buffer(struct extent_buffer *eb)
5079{
Chris Mason242e18c2013-01-29 17:49:37 -05005080 int refs;
5081 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005082 if (!eb)
5083 return;
5084
Chris Mason242e18c2013-01-29 17:49:37 -05005085 while (1) {
5086 refs = atomic_read(&eb->refs);
Nikolay Borisov46cc7752018-10-15 17:04:01 +03005087 if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
5088 || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
5089 refs == 1))
Chris Mason242e18c2013-01-29 17:49:37 -05005090 break;
5091 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5092 if (old == refs)
5093 return;
5094 }
5095
Josef Bacik3083ee22012-03-09 16:01:49 -05005096 spin_lock(&eb->refs_lock);
5097 if (atomic_read(&eb->refs) == 2 &&
5098 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005099 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005100 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5101 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005102
Josef Bacik3083ee22012-03-09 16:01:49 -05005103 /*
5104 * I know this is terrible, but it's temporary until we stop tracking
5105 * the uptodate bits and such for the extent buffers.
5106 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005107 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005108}
Chris Masond1310b22008-01-24 16:13:08 -05005109
Josef Bacik3083ee22012-03-09 16:01:49 -05005110void free_extent_buffer_stale(struct extent_buffer *eb)
5111{
5112 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005113 return;
5114
Josef Bacik3083ee22012-03-09 16:01:49 -05005115 spin_lock(&eb->refs_lock);
5116 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5117
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005118 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005119 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5120 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005121 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005122}
5123
Chris Mason1d4284b2012-03-28 20:31:37 -04005124void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005125{
David Sterbacc5e31a2018-03-01 18:20:27 +01005126 int i;
5127 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005128 struct page *page;
5129
David Sterba65ad0102018-06-29 10:56:49 +02005130 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005131
5132 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005133 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005134 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005135 continue;
5136
Chris Masona61e6f22008-07-22 11:18:08 -04005137 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005138 WARN_ON(!PagePrivate(page));
5139
Chris Masond1310b22008-01-24 16:13:08 -05005140 clear_page_dirty_for_io(page);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005141 xa_lock_irq(&page->mapping->i_pages);
Matthew Wilcox0a943c62017-12-04 10:37:22 -05005142 if (!PageDirty(page))
5143 __xa_clear_mark(&page->mapping->i_pages,
5144 page_index(page), PAGECACHE_TAG_DIRTY);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005145 xa_unlock_irq(&page->mapping->i_pages);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005146 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005147 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005148 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005149 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005150}
Chris Masond1310b22008-01-24 16:13:08 -05005151
Liu Boabb57ef2018-09-14 01:44:42 +08005152bool set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005153{
David Sterbacc5e31a2018-03-01 18:20:27 +01005154 int i;
5155 int num_pages;
Liu Boabb57ef2018-09-14 01:44:42 +08005156 bool was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005157
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005158 check_buffer_tree_ref(eb);
5159
Chris Masonb9473432009-03-13 11:00:37 -04005160 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005161
David Sterba65ad0102018-06-29 10:56:49 +02005162 num_pages = num_extent_pages(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05005163 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005164 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5165
Liu Boabb57ef2018-09-14 01:44:42 +08005166 if (!was_dirty)
5167 for (i = 0; i < num_pages; i++)
5168 set_page_dirty(eb->pages[i]);
Liu Bo51995c32018-09-14 01:46:08 +08005169
5170#ifdef CONFIG_BTRFS_DEBUG
5171 for (i = 0; i < num_pages; i++)
5172 ASSERT(PageDirty(eb->pages[i]));
5173#endif
5174
Chris Masonb9473432009-03-13 11:00:37 -04005175 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005176}
Chris Masond1310b22008-01-24 16:13:08 -05005177
David Sterba69ba3922015-12-03 13:08:59 +01005178void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005179{
David Sterbacc5e31a2018-03-01 18:20:27 +01005180 int i;
Chris Mason1259ab72008-05-12 13:39:03 -04005181 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005182 int num_pages;
Chris Mason1259ab72008-05-12 13:39:03 -04005183
Chris Masonb4ce94d2009-02-04 09:25:08 -05005184 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005185 num_pages = num_extent_pages(eb);
Chris Mason1259ab72008-05-12 13:39:03 -04005186 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005187 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005188 if (page)
5189 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005190 }
Chris Mason1259ab72008-05-12 13:39:03 -04005191}
5192
David Sterba09c25a82015-12-03 13:08:59 +01005193void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005194{
David Sterbacc5e31a2018-03-01 18:20:27 +01005195 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005196 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005197 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005198
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005199 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005200 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005201 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005202 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005203 SetPageUptodate(page);
5204 }
Chris Masond1310b22008-01-24 16:13:08 -05005205}
Chris Masond1310b22008-01-24 16:13:08 -05005206
Chris Masond1310b22008-01-24 16:13:08 -05005207int read_extent_buffer_pages(struct extent_io_tree *tree,
David Sterba6af49db2017-06-23 04:09:57 +02005208 struct extent_buffer *eb, int wait, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005209{
David Sterbacc5e31a2018-03-01 18:20:27 +01005210 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005211 struct page *page;
5212 int err;
5213 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005214 int locked_pages = 0;
5215 int all_uptodate = 1;
David Sterbacc5e31a2018-03-01 18:20:27 +01005216 int num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005217 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005218 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005219 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005220
Chris Masonb4ce94d2009-02-04 09:25:08 -05005221 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005222 return 0;
5223
David Sterba65ad0102018-06-29 10:56:49 +02005224 num_pages = num_extent_pages(eb);
Josef Bacik8436ea912016-09-02 15:40:03 -04005225 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005226 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005227 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005228 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005229 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005230 } else {
5231 lock_page(page);
5232 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005233 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005234 }
5235 /*
5236 * We need to firstly lock all pages to make sure that
5237 * the uptodate bit of our pages won't be affected by
5238 * clear_extent_buffer_uptodate().
5239 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005240 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005241 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005242 if (!PageUptodate(page)) {
5243 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005244 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005245 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005246 }
Liu Bo2571e732016-08-03 12:33:01 -07005247
Chris Masonce9adaa2008-04-09 16:28:12 -04005248 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005249 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005250 goto unlock_exit;
5251 }
5252
Filipe Manana656f30d2014-09-26 12:25:56 +01005253 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005254 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005255 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005256 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005257 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005258
Chris Masonce9adaa2008-04-09 16:28:12 -04005259 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005260 if (ret) {
5261 atomic_dec(&eb->io_pages);
5262 unlock_page(page);
5263 continue;
5264 }
5265
Chris Masonf1885912008-04-09 16:28:12 -04005266 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005267 err = __extent_read_full_page(tree, page,
David Sterba6af49db2017-06-23 04:09:57 +02005268 btree_get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005269 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005270 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005271 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005272 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005273 /*
5274 * We use &bio in above __extent_read_full_page,
5275 * so we ensure that if it returns error, the
5276 * current page fails to add itself to bio and
5277 * it's been unlocked.
5278 *
5279 * We must dec io_pages by ourselves.
5280 */
5281 atomic_dec(&eb->io_pages);
5282 }
Chris Masond1310b22008-01-24 16:13:08 -05005283 } else {
5284 unlock_page(page);
5285 }
5286 }
5287
Jeff Mahoney355808c2011-10-03 23:23:14 -04005288 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005289 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005290 if (err)
5291 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005292 }
Chris Masona86c12c2008-02-07 10:50:54 -05005293
Arne Jansenbb82ab82011-06-10 14:06:53 +02005294 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005295 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005296
Josef Bacik8436ea912016-09-02 15:40:03 -04005297 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005298 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005299 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005300 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005301 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005302 }
Chris Masond3977122009-01-05 21:25:51 -05005303
Chris Masond1310b22008-01-24 16:13:08 -05005304 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005305
5306unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005307 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005308 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005309 page = eb->pages[locked_pages];
5310 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005311 }
5312 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005313}
Chris Masond1310b22008-01-24 16:13:08 -05005314
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005315void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5316 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005317{
5318 size_t cur;
5319 size_t offset;
5320 struct page *page;
5321 char *kaddr;
5322 char *dst = (char *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005323 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005324 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005325
Liu Bof716abd2017-08-09 11:10:16 -06005326 if (start + len > eb->len) {
5327 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5328 eb->start, eb->len, start, len);
5329 memset(dst, 0, len);
5330 return;
5331 }
Chris Masond1310b22008-01-24 16:13:08 -05005332
Johannes Thumshirn70730172018-12-05 15:23:03 +01005333 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005334
Chris Masond3977122009-01-05 21:25:51 -05005335 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005336 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005337
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005338 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005339 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005340 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005341
5342 dst += cur;
5343 len -= cur;
5344 offset = 0;
5345 i++;
5346 }
5347}
Chris Masond1310b22008-01-24 16:13:08 -05005348
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005349int read_extent_buffer_to_user(const struct extent_buffer *eb,
5350 void __user *dstv,
5351 unsigned long start, unsigned long len)
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005352{
5353 size_t cur;
5354 size_t offset;
5355 struct page *page;
5356 char *kaddr;
5357 char __user *dst = (char __user *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005358 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005359 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005360 int ret = 0;
5361
5362 WARN_ON(start > eb->len);
5363 WARN_ON(start + len > eb->start + eb->len);
5364
Johannes Thumshirn70730172018-12-05 15:23:03 +01005365 offset = offset_in_page(start_offset + start);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005366
5367 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005368 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005369
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005370 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005371 kaddr = page_address(page);
5372 if (copy_to_user(dst, kaddr + offset, cur)) {
5373 ret = -EFAULT;
5374 break;
5375 }
5376
5377 dst += cur;
5378 len -= cur;
5379 offset = 0;
5380 i++;
5381 }
5382
5383 return ret;
5384}
5385
Liu Bo415b35a2016-06-17 19:16:21 -07005386/*
5387 * return 0 if the item is found within a page.
5388 * return 1 if the item spans two pages.
5389 * return -EINVAL otherwise.
5390 */
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005391int map_private_extent_buffer(const struct extent_buffer *eb,
5392 unsigned long start, unsigned long min_len,
5393 char **map, unsigned long *map_start,
5394 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005395{
Johannes Thumshirncc2c39d2018-11-28 09:54:54 +01005396 size_t offset;
Chris Masond1310b22008-01-24 16:13:08 -05005397 char *kaddr;
5398 struct page *p;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005399 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005400 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005401 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005402 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005403
Liu Bof716abd2017-08-09 11:10:16 -06005404 if (start + min_len > eb->len) {
5405 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5406 eb->start, eb->len, start, min_len);
5407 return -EINVAL;
5408 }
5409
Chris Masond1310b22008-01-24 16:13:08 -05005410 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005411 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005412
5413 if (i == 0) {
5414 offset = start_offset;
5415 *map_start = 0;
5416 } else {
5417 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005418 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005419 }
Chris Masond3977122009-01-05 21:25:51 -05005420
David Sterbafb85fc92014-07-31 01:03:53 +02005421 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005422 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005423 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005424 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005425 return 0;
5426}
Chris Masond1310b22008-01-24 16:13:08 -05005427
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005428int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5429 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005430{
5431 size_t cur;
5432 size_t offset;
5433 struct page *page;
5434 char *kaddr;
5435 char *ptr = (char *)ptrv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005436 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005437 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005438 int ret = 0;
5439
5440 WARN_ON(start > eb->len);
5441 WARN_ON(start + len > eb->start + eb->len);
5442
Johannes Thumshirn70730172018-12-05 15:23:03 +01005443 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005444
Chris Masond3977122009-01-05 21:25:51 -05005445 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005446 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005447
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005448 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005449
Chris Masona6591712011-07-19 12:04:14 -04005450 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005451 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005452 if (ret)
5453 break;
5454
5455 ptr += cur;
5456 len -= cur;
5457 offset = 0;
5458 i++;
5459 }
5460 return ret;
5461}
Chris Masond1310b22008-01-24 16:13:08 -05005462
David Sterbaf157bf72016-11-09 17:43:38 +01005463void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5464 const void *srcv)
5465{
5466 char *kaddr;
5467
5468 WARN_ON(!PageUptodate(eb->pages[0]));
5469 kaddr = page_address(eb->pages[0]);
5470 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5471 BTRFS_FSID_SIZE);
5472}
5473
5474void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5475{
5476 char *kaddr;
5477
5478 WARN_ON(!PageUptodate(eb->pages[0]));
5479 kaddr = page_address(eb->pages[0]);
5480 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5481 BTRFS_FSID_SIZE);
5482}
5483
Chris Masond1310b22008-01-24 16:13:08 -05005484void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5485 unsigned long start, unsigned long len)
5486{
5487 size_t cur;
5488 size_t offset;
5489 struct page *page;
5490 char *kaddr;
5491 char *src = (char *)srcv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005492 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005493 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005494
5495 WARN_ON(start > eb->len);
5496 WARN_ON(start + len > eb->start + eb->len);
5497
Johannes Thumshirn70730172018-12-05 15:23:03 +01005498 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005499
Chris Masond3977122009-01-05 21:25:51 -05005500 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005501 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005502 WARN_ON(!PageUptodate(page));
5503
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005504 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005505 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005506 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005507
5508 src += cur;
5509 len -= cur;
5510 offset = 0;
5511 i++;
5512 }
5513}
Chris Masond1310b22008-01-24 16:13:08 -05005514
David Sterbab159fa22016-11-08 18:09:03 +01005515void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5516 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005517{
5518 size_t cur;
5519 size_t offset;
5520 struct page *page;
5521 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005522 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005523 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005524
5525 WARN_ON(start > eb->len);
5526 WARN_ON(start + len > eb->start + eb->len);
5527
Johannes Thumshirn70730172018-12-05 15:23:03 +01005528 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005529
Chris Masond3977122009-01-05 21:25:51 -05005530 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005531 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005532 WARN_ON(!PageUptodate(page));
5533
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005534 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005535 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005536 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005537
5538 len -= cur;
5539 offset = 0;
5540 i++;
5541 }
5542}
Chris Masond1310b22008-01-24 16:13:08 -05005543
David Sterba58e80122016-11-08 18:30:31 +01005544void copy_extent_buffer_full(struct extent_buffer *dst,
5545 struct extent_buffer *src)
5546{
5547 int i;
David Sterbacc5e31a2018-03-01 18:20:27 +01005548 int num_pages;
David Sterba58e80122016-11-08 18:30:31 +01005549
5550 ASSERT(dst->len == src->len);
5551
David Sterba65ad0102018-06-29 10:56:49 +02005552 num_pages = num_extent_pages(dst);
David Sterba58e80122016-11-08 18:30:31 +01005553 for (i = 0; i < num_pages; i++)
5554 copy_page(page_address(dst->pages[i]),
5555 page_address(src->pages[i]));
5556}
5557
Chris Masond1310b22008-01-24 16:13:08 -05005558void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5559 unsigned long dst_offset, unsigned long src_offset,
5560 unsigned long len)
5561{
5562 u64 dst_len = dst->len;
5563 size_t cur;
5564 size_t offset;
5565 struct page *page;
5566 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005567 size_t start_offset = offset_in_page(dst->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005568 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005569
5570 WARN_ON(src->len != dst_len);
5571
Johannes Thumshirn70730172018-12-05 15:23:03 +01005572 offset = offset_in_page(start_offset + dst_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005573
Chris Masond3977122009-01-05 21:25:51 -05005574 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005575 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005576 WARN_ON(!PageUptodate(page));
5577
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005578 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005579
Chris Masona6591712011-07-19 12:04:14 -04005580 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005581 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005582
5583 src_offset += cur;
5584 len -= cur;
5585 offset = 0;
5586 i++;
5587 }
5588}
Chris Masond1310b22008-01-24 16:13:08 -05005589
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005590/*
5591 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5592 * given bit number
5593 * @eb: the extent buffer
5594 * @start: offset of the bitmap item in the extent buffer
5595 * @nr: bit number
5596 * @page_index: return index of the page in the extent buffer that contains the
5597 * given bit number
5598 * @page_offset: return offset into the page given by page_index
5599 *
5600 * This helper hides the ugliness of finding the byte in an extent buffer which
5601 * contains a given bit.
5602 */
5603static inline void eb_bitmap_offset(struct extent_buffer *eb,
5604 unsigned long start, unsigned long nr,
5605 unsigned long *page_index,
5606 size_t *page_offset)
5607{
Johannes Thumshirn70730172018-12-05 15:23:03 +01005608 size_t start_offset = offset_in_page(eb->start);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005609 size_t byte_offset = BIT_BYTE(nr);
5610 size_t offset;
5611
5612 /*
5613 * The byte we want is the offset of the extent buffer + the offset of
5614 * the bitmap item in the extent buffer + the offset of the byte in the
5615 * bitmap item.
5616 */
5617 offset = start_offset + start + byte_offset;
5618
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005619 *page_index = offset >> PAGE_SHIFT;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005620 *page_offset = offset_in_page(offset);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005621}
5622
5623/**
5624 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5625 * @eb: the extent buffer
5626 * @start: offset of the bitmap item in the extent buffer
5627 * @nr: bit number to test
5628 */
5629int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5630 unsigned long nr)
5631{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005632 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005633 struct page *page;
5634 unsigned long i;
5635 size_t offset;
5636
5637 eb_bitmap_offset(eb, start, nr, &i, &offset);
5638 page = eb->pages[i];
5639 WARN_ON(!PageUptodate(page));
5640 kaddr = page_address(page);
5641 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5642}
5643
5644/**
5645 * extent_buffer_bitmap_set - set an area of a bitmap
5646 * @eb: the extent buffer
5647 * @start: offset of the bitmap item in the extent buffer
5648 * @pos: bit number of the first bit
5649 * @len: number of bits to set
5650 */
5651void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5652 unsigned long pos, unsigned long len)
5653{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005654 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005655 struct page *page;
5656 unsigned long i;
5657 size_t offset;
5658 const unsigned int size = pos + len;
5659 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005660 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005661
5662 eb_bitmap_offset(eb, start, pos, &i, &offset);
5663 page = eb->pages[i];
5664 WARN_ON(!PageUptodate(page));
5665 kaddr = page_address(page);
5666
5667 while (len >= bits_to_set) {
5668 kaddr[offset] |= mask_to_set;
5669 len -= bits_to_set;
5670 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005671 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005672 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005673 offset = 0;
5674 page = eb->pages[++i];
5675 WARN_ON(!PageUptodate(page));
5676 kaddr = page_address(page);
5677 }
5678 }
5679 if (len) {
5680 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5681 kaddr[offset] |= mask_to_set;
5682 }
5683}
5684
5685
5686/**
5687 * extent_buffer_bitmap_clear - clear an area of a bitmap
5688 * @eb: the extent buffer
5689 * @start: offset of the bitmap item in the extent buffer
5690 * @pos: bit number of the first bit
5691 * @len: number of bits to clear
5692 */
5693void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5694 unsigned long pos, unsigned long len)
5695{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005696 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005697 struct page *page;
5698 unsigned long i;
5699 size_t offset;
5700 const unsigned int size = pos + len;
5701 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005702 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005703
5704 eb_bitmap_offset(eb, start, pos, &i, &offset);
5705 page = eb->pages[i];
5706 WARN_ON(!PageUptodate(page));
5707 kaddr = page_address(page);
5708
5709 while (len >= bits_to_clear) {
5710 kaddr[offset] &= ~mask_to_clear;
5711 len -= bits_to_clear;
5712 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005713 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005714 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005715 offset = 0;
5716 page = eb->pages[++i];
5717 WARN_ON(!PageUptodate(page));
5718 kaddr = page_address(page);
5719 }
5720 }
5721 if (len) {
5722 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5723 kaddr[offset] &= ~mask_to_clear;
5724 }
5725}
5726
Sergei Trofimovich33872062011-04-11 21:52:52 +00005727static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5728{
5729 unsigned long distance = (src > dst) ? src - dst : dst - src;
5730 return distance < len;
5731}
5732
Chris Masond1310b22008-01-24 16:13:08 -05005733static void copy_pages(struct page *dst_page, struct page *src_page,
5734 unsigned long dst_off, unsigned long src_off,
5735 unsigned long len)
5736{
Chris Masona6591712011-07-19 12:04:14 -04005737 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005738 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005739 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005740
Sergei Trofimovich33872062011-04-11 21:52:52 +00005741 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005742 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005743 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005744 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005745 if (areas_overlap(src_off, dst_off, len))
5746 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005747 }
Chris Masond1310b22008-01-24 16:13:08 -05005748
Chris Mason727011e2010-08-06 13:21:20 -04005749 if (must_memmove)
5750 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5751 else
5752 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005753}
5754
5755void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5756 unsigned long src_offset, unsigned long len)
5757{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005758 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005759 size_t cur;
5760 size_t dst_off_in_page;
5761 size_t src_off_in_page;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005762 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005763 unsigned long dst_i;
5764 unsigned long src_i;
5765
5766 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005767 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005768 "memmove bogus src_offset %lu move len %lu dst len %lu",
5769 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005770 BUG_ON(1);
5771 }
5772 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005773 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005774 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5775 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005776 BUG_ON(1);
5777 }
5778
Chris Masond3977122009-01-05 21:25:51 -05005779 while (len > 0) {
Johannes Thumshirn70730172018-12-05 15:23:03 +01005780 dst_off_in_page = offset_in_page(start_offset + dst_offset);
5781 src_off_in_page = offset_in_page(start_offset + src_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005782
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005783 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5784 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005785
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005786 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005787 src_off_in_page));
5788 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005789 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005790
David Sterbafb85fc92014-07-31 01:03:53 +02005791 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005792 dst_off_in_page, src_off_in_page, cur);
5793
5794 src_offset += cur;
5795 dst_offset += cur;
5796 len -= cur;
5797 }
5798}
Chris Masond1310b22008-01-24 16:13:08 -05005799
5800void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5801 unsigned long src_offset, unsigned long len)
5802{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005803 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005804 size_t cur;
5805 size_t dst_off_in_page;
5806 size_t src_off_in_page;
5807 unsigned long dst_end = dst_offset + len - 1;
5808 unsigned long src_end = src_offset + len - 1;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005809 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005810 unsigned long dst_i;
5811 unsigned long src_i;
5812
5813 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005814 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005815 "memmove bogus src_offset %lu move len %lu len %lu",
5816 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005817 BUG_ON(1);
5818 }
5819 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005820 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005821 "memmove bogus dst_offset %lu move len %lu len %lu",
5822 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005823 BUG_ON(1);
5824 }
Chris Mason727011e2010-08-06 13:21:20 -04005825 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005826 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5827 return;
5828 }
Chris Masond3977122009-01-05 21:25:51 -05005829 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005830 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
5831 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005832
Johannes Thumshirn70730172018-12-05 15:23:03 +01005833 dst_off_in_page = offset_in_page(start_offset + dst_end);
5834 src_off_in_page = offset_in_page(start_offset + src_end);
Chris Masond1310b22008-01-24 16:13:08 -05005835
5836 cur = min_t(unsigned long, len, src_off_in_page + 1);
5837 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005838 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005839 dst_off_in_page - cur + 1,
5840 src_off_in_page - cur + 1, cur);
5841
5842 dst_end -= cur;
5843 src_end -= cur;
5844 len -= cur;
5845 }
5846}
Chris Mason6af118ce2008-07-22 11:18:07 -04005847
David Sterbaf7a52a42013-04-26 14:56:29 +00005848int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005849{
Chris Mason6af118ce2008-07-22 11:18:07 -04005850 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005851
Miao Xie19fe0a82010-10-26 20:57:29 -04005852 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005853 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05005854 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005855 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005856 spin_lock(&page->mapping->private_lock);
5857 if (!PagePrivate(page)) {
5858 spin_unlock(&page->mapping->private_lock);
5859 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005860 }
5861
Josef Bacik3083ee22012-03-09 16:01:49 -05005862 eb = (struct extent_buffer *)page->private;
5863 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005864
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005865 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005866 * This is a little awful but should be ok, we need to make sure that
5867 * the eb doesn't disappear out from under us while we're looking at
5868 * this page.
5869 */
5870 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005871 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005872 spin_unlock(&eb->refs_lock);
5873 spin_unlock(&page->mapping->private_lock);
5874 return 0;
5875 }
5876 spin_unlock(&page->mapping->private_lock);
5877
Josef Bacik3083ee22012-03-09 16:01:49 -05005878 /*
5879 * If tree ref isn't set then we know the ref on this eb is a real ref,
5880 * so just return, this page will likely be freed soon anyway.
5881 */
5882 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5883 spin_unlock(&eb->refs_lock);
5884 return 0;
5885 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005886
David Sterbaf7a52a42013-04-26 14:56:29 +00005887 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005888}