blob: 38dcac0a152e499e707a29b77e498d628a39a24a [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
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003495/*
3496 * Lock eb pages and flush the bio if we can't the locks
3497 *
3498 * Return 0 if nothing went wrong
3499 * Return >0 is same as 0, except bio is not submitted
3500 * Return <0 if something went wrong, no page is locked
3501 */
Chris Mason0e378df2014-05-19 20:55:27 -07003502static noinline_for_stack int
3503lock_extent_buffer_for_io(struct extent_buffer *eb,
3504 struct btrfs_fs_info *fs_info,
3505 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003506{
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003507 int i, num_pages, failed_page_nr;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003508 int flush = 0;
3509 int ret = 0;
3510
3511 if (!btrfs_try_tree_write_lock(eb)) {
Qu Wenruof4340622019-03-20 14:27:41 +08003512 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003513 if (ret < 0)
3514 return ret;
3515 flush = 1;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003516 btrfs_tree_lock(eb);
3517 }
3518
3519 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3520 btrfs_tree_unlock(eb);
3521 if (!epd->sync_io)
3522 return 0;
3523 if (!flush) {
Qu Wenruof4340622019-03-20 14:27:41 +08003524 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003525 if (ret < 0)
3526 return ret;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003527 flush = 1;
3528 }
Chris Masona098d8e82012-03-21 12:09:56 -04003529 while (1) {
3530 wait_on_extent_buffer_writeback(eb);
3531 btrfs_tree_lock(eb);
3532 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3533 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003534 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003535 }
3536 }
3537
Josef Bacik51561ff2012-07-20 16:25:24 -04003538 /*
3539 * We need to do this to prevent races in people who check if the eb is
3540 * under IO since we can end up having no IO bits set for a short period
3541 * of time.
3542 */
3543 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003544 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3545 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003546 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003547 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Nikolay Borisov104b4e52017-06-20 21:01:20 +03003548 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3549 -eb->len,
3550 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003551 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003552 } else {
3553 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003554 }
3555
3556 btrfs_tree_unlock(eb);
3557
3558 if (!ret)
3559 return ret;
3560
David Sterba65ad0102018-06-29 10:56:49 +02003561 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003562 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003563 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003564
3565 if (!trylock_page(p)) {
3566 if (!flush) {
Qu Wenruof4340622019-03-20 14:27:41 +08003567 ret = flush_write_bio(epd);
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003568 if (ret < 0) {
3569 failed_page_nr = i;
3570 goto err_unlock;
3571 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003572 flush = 1;
3573 }
3574 lock_page(p);
3575 }
3576 }
3577
3578 return ret;
Qu Wenruo2e3c2512019-03-20 14:27:46 +08003579err_unlock:
3580 /* Unlock already locked pages */
3581 for (i = 0; i < failed_page_nr; i++)
3582 unlock_page(eb->pages[i]);
3583 return ret;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003584}
3585
3586static void end_extent_buffer_writeback(struct extent_buffer *eb)
3587{
3588 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003589 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003590 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3591}
3592
Filipe Manana656f30d2014-09-26 12:25:56 +01003593static void set_btree_ioerr(struct page *page)
3594{
3595 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Filipe Manana656f30d2014-09-26 12:25:56 +01003596
3597 SetPageError(page);
3598 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3599 return;
3600
3601 /*
3602 * If writeback for a btree extent that doesn't belong to a log tree
3603 * failed, increment the counter transaction->eb_write_errors.
3604 * We do this because while the transaction is running and before it's
3605 * committing (when we call filemap_fdata[write|wait]_range against
3606 * the btree inode), we might have
3607 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3608 * returns an error or an error happens during writeback, when we're
3609 * committing the transaction we wouldn't know about it, since the pages
3610 * can be no longer dirty nor marked anymore for writeback (if a
3611 * subsequent modification to the extent buffer didn't happen before the
3612 * transaction commit), which makes filemap_fdata[write|wait]_range not
3613 * able to find the pages tagged with SetPageError at transaction
3614 * commit time. So if this happens we must abort the transaction,
3615 * otherwise we commit a super block with btree roots that point to
3616 * btree nodes/leafs whose content on disk is invalid - either garbage
3617 * or the content of some node/leaf from a past generation that got
3618 * cowed or deleted and is no longer valid.
3619 *
3620 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3621 * not be enough - we need to distinguish between log tree extents vs
3622 * non-log tree extents, and the next filemap_fdatawait_range() call
3623 * will catch and clear such errors in the mapping - and that call might
3624 * be from a log sync and not from a transaction commit. Also, checking
3625 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3626 * not done and would not be reliable - the eb might have been released
3627 * from memory and reading it back again means that flag would not be
3628 * set (since it's a runtime flag, not persisted on disk).
3629 *
3630 * Using the flags below in the btree inode also makes us achieve the
3631 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3632 * writeback for all dirty pages and before filemap_fdatawait_range()
3633 * is called, the writeback for all dirty pages had already finished
3634 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3635 * filemap_fdatawait_range() would return success, as it could not know
3636 * that writeback errors happened (the pages were no longer tagged for
3637 * writeback).
3638 */
3639 switch (eb->log_index) {
3640 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003641 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003642 break;
3643 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003644 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003645 break;
3646 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003647 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003648 break;
3649 default:
3650 BUG(); /* unexpected, logic error */
3651 }
3652}
3653
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003654static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003655{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003656 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003657 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003658 int i, done;
Ming Lei6dc4f102019-02-15 19:13:19 +08003659 struct bvec_iter_all iter_all;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003660
David Sterbac09abff2017-07-13 18:10:07 +02003661 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08003662 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003663 struct page *page = bvec->bv_page;
3664
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003665 eb = (struct extent_buffer *)page->private;
3666 BUG_ON(!eb);
3667 done = atomic_dec_and_test(&eb->io_pages);
3668
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003669 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003670 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003671 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003672 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003673 }
3674
3675 end_page_writeback(page);
3676
3677 if (!done)
3678 continue;
3679
3680 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003681 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003682
3683 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003684}
3685
Chris Mason0e378df2014-05-19 20:55:27 -07003686static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003687 struct btrfs_fs_info *fs_info,
3688 struct writeback_control *wbc,
3689 struct extent_page_data *epd)
3690{
3691 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003692 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003693 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003694 u32 nritems;
David Sterbacc5e31a2018-03-01 18:20:27 +01003695 int i, num_pages;
Liu Bo851cd172016-09-23 13:44:44 -07003696 unsigned long start, end;
Liu Boff40adf2017-08-24 18:19:48 -06003697 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003698 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003699
Filipe Manana656f30d2014-09-26 12:25:56 +01003700 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02003701 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003702 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003703
Liu Bo851cd172016-09-23 13:44:44 -07003704 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3705 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003706 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003707 end = btrfs_node_key_ptr_offset(nritems);
3708
David Sterbab159fa22016-11-08 18:09:03 +01003709 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003710 } else {
3711 /*
3712 * leaf:
3713 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3714 */
3715 start = btrfs_item_nr_offset(nritems);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003716 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, eb);
David Sterbab159fa22016-11-08 18:09:03 +01003717 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003718 }
3719
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003720 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003721 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003722
3723 clear_page_dirty_for_io(p);
3724 set_page_writeback(p);
David Sterba4b81ba42017-06-06 19:14:26 +02003725 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003726 p, offset, PAGE_SIZE, 0, bdev,
David Sterbac2df8bb2017-02-10 19:29:38 +01003727 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003728 end_bio_extent_buffer_writepage,
Liu Bo18fdc672017-09-13 12:18:22 -06003729 0, 0, 0, false);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003730 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003731 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003732 if (PageWriteback(p))
3733 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003734 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3735 end_extent_buffer_writeback(eb);
3736 ret = -EIO;
3737 break;
3738 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003739 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003740 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003741 unlock_page(p);
3742 }
3743
3744 if (unlikely(ret)) {
3745 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003746 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003747 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003748 unlock_page(p);
3749 }
3750 }
3751
3752 return ret;
3753}
3754
3755int btree_write_cache_pages(struct address_space *mapping,
3756 struct writeback_control *wbc)
3757{
3758 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3759 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3760 struct extent_buffer *eb, *prev_eb = NULL;
3761 struct extent_page_data epd = {
3762 .bio = NULL,
3763 .tree = tree,
3764 .extent_locked = 0,
3765 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3766 };
3767 int ret = 0;
3768 int done = 0;
3769 int nr_to_write_done = 0;
3770 struct pagevec pvec;
3771 int nr_pages;
3772 pgoff_t index;
3773 pgoff_t end; /* Inclusive */
3774 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003775 xa_mark_t tag;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003776
Mel Gorman86679822017-11-15 17:37:52 -08003777 pagevec_init(&pvec);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003778 if (wbc->range_cyclic) {
3779 index = mapping->writeback_index; /* Start from prev offset */
3780 end = -1;
3781 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003782 index = wbc->range_start >> PAGE_SHIFT;
3783 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003784 scanned = 1;
3785 }
3786 if (wbc->sync_mode == WB_SYNC_ALL)
3787 tag = PAGECACHE_TAG_TOWRITE;
3788 else
3789 tag = PAGECACHE_TAG_DIRTY;
3790retry:
3791 if (wbc->sync_mode == WB_SYNC_ALL)
3792 tag_pages_for_writeback(mapping, index, end);
3793 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara4006f432017-11-15 17:34:37 -08003794 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08003795 tag))) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003796 unsigned i;
3797
3798 scanned = 1;
3799 for (i = 0; i < nr_pages; i++) {
3800 struct page *page = pvec.pages[i];
3801
3802 if (!PagePrivate(page))
3803 continue;
3804
Josef Bacikb5bae262012-09-14 13:43:01 -04003805 spin_lock(&mapping->private_lock);
3806 if (!PagePrivate(page)) {
3807 spin_unlock(&mapping->private_lock);
3808 continue;
3809 }
3810
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003811 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003812
3813 /*
3814 * Shouldn't happen and normally this would be a BUG_ON
3815 * but no sense in crashing the users box for something
3816 * we can survive anyway.
3817 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303818 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003819 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003820 continue;
3821 }
3822
Josef Bacikb5bae262012-09-14 13:43:01 -04003823 if (eb == prev_eb) {
3824 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003825 continue;
3826 }
3827
Josef Bacikb5bae262012-09-14 13:43:01 -04003828 ret = atomic_inc_not_zero(&eb->refs);
3829 spin_unlock(&mapping->private_lock);
3830 if (!ret)
3831 continue;
3832
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003833 prev_eb = eb;
3834 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3835 if (!ret) {
3836 free_extent_buffer(eb);
3837 continue;
3838 }
3839
3840 ret = write_one_eb(eb, fs_info, wbc, &epd);
3841 if (ret) {
3842 done = 1;
3843 free_extent_buffer(eb);
3844 break;
3845 }
3846 free_extent_buffer(eb);
3847
3848 /*
3849 * the filesystem may choose to bump up nr_to_write.
3850 * We have to make sure to honor the new nr_to_write
3851 * at any time
3852 */
3853 nr_to_write_done = wbc->nr_to_write <= 0;
3854 }
3855 pagevec_release(&pvec);
3856 cond_resched();
3857 }
3858 if (!scanned && !done) {
3859 /*
3860 * We hit the last page and there is more work to be done: wrap
3861 * back to the start of the file
3862 */
3863 scanned = 1;
3864 index = 0;
3865 goto retry;
3866 }
Qu Wenruo2b952ee2019-03-20 14:27:43 +08003867 ASSERT(ret <= 0);
3868 if (ret < 0) {
3869 end_write_bio(&epd, ret);
3870 return ret;
3871 }
3872 ret = flush_write_bio(&epd);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003873 return ret;
3874}
3875
Chris Masond1310b22008-01-24 16:13:08 -05003876/**
Chris Mason4bef0842008-09-08 11:18:08 -04003877 * 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 -05003878 * @mapping: address space structure to write
3879 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
David Sterba935db852017-06-23 04:30:28 +02003880 * @data: data passed to __extent_writepage function
Chris Masond1310b22008-01-24 16:13:08 -05003881 *
3882 * If a page is already under I/O, write_cache_pages() skips it, even
3883 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3884 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3885 * and msync() need to guarantee that all the data which was dirty at the time
3886 * the call was made get new I/O started against them. If wbc->sync_mode is
3887 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3888 * existing IO to complete.
3889 */
David Sterba4242b642017-02-10 19:38:24 +01003890static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04003891 struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003892 struct extent_page_data *epd)
Chris Masond1310b22008-01-24 16:13:08 -05003893{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003894 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003895 int ret = 0;
3896 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003897 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003898 struct pagevec pvec;
3899 int nr_pages;
3900 pgoff_t index;
3901 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08003902 pgoff_t done_index;
3903 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003904 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003905 xa_mark_t tag;
Chris Masond1310b22008-01-24 16:13:08 -05003906
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003907 /*
3908 * We have to hold onto the inode so that ordered extents can do their
3909 * work when the IO finishes. The alternative to this is failing to add
3910 * an ordered extent if the igrab() fails there and that is a huge pain
3911 * to deal with, so instead just hold onto the inode throughout the
3912 * writepages operation. If it fails here we are freeing up the inode
3913 * anyway and we'd rather not waste our time writing out stuff that is
3914 * going to be truncated anyway.
3915 */
3916 if (!igrab(inode))
3917 return 0;
3918
Mel Gorman86679822017-11-15 17:37:52 -08003919 pagevec_init(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05003920 if (wbc->range_cyclic) {
3921 index = mapping->writeback_index; /* Start from prev offset */
3922 end = -1;
3923 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003924 index = wbc->range_start >> PAGE_SHIFT;
3925 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08003926 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3927 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05003928 scanned = 1;
3929 }
Ethan Lien3cd24c62018-11-01 14:49:03 +08003930
3931 /*
3932 * We do the tagged writepage as long as the snapshot flush bit is set
3933 * and we are the first one who do the filemap_flush() on this inode.
3934 *
3935 * The nr_to_write == LONG_MAX is needed to make sure other flushers do
3936 * not race in and drop the bit.
3937 */
3938 if (range_whole && wbc->nr_to_write == LONG_MAX &&
3939 test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
3940 &BTRFS_I(inode)->runtime_flags))
3941 wbc->tagged_writepages = 1;
3942
3943 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00003944 tag = PAGECACHE_TAG_TOWRITE;
3945 else
3946 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003947retry:
Ethan Lien3cd24c62018-11-01 14:49:03 +08003948 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00003949 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08003950 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003951 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara67fd7072017-11-15 17:35:19 -08003952 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
3953 &index, end, tag))) {
Chris Masond1310b22008-01-24 16:13:08 -05003954 unsigned i;
3955
3956 scanned = 1;
3957 for (i = 0; i < nr_pages; i++) {
3958 struct page *page = pvec.pages[i];
3959
Liu Boa91326672016-03-07 16:56:21 -08003960 done_index = page->index;
Chris Masond1310b22008-01-24 16:13:08 -05003961 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07003962 * At this point we hold neither the i_pages lock nor
3963 * the page lock: the page may be truncated or
3964 * invalidated (changing page->mapping to NULL),
3965 * or even swizzled back from swapper_space to
3966 * tmpfs file mapping
Chris Masond1310b22008-01-24 16:13:08 -05003967 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003968 if (!trylock_page(page)) {
Qu Wenruof4340622019-03-20 14:27:41 +08003969 ret = flush_write_bio(epd);
3970 BUG_ON(ret < 0);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003971 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003972 }
Chris Masond1310b22008-01-24 16:13:08 -05003973
3974 if (unlikely(page->mapping != mapping)) {
3975 unlock_page(page);
3976 continue;
3977 }
3978
Chris Masond2c3f4f2008-11-19 12:44:22 -05003979 if (wbc->sync_mode != WB_SYNC_NONE) {
Qu Wenruof4340622019-03-20 14:27:41 +08003980 if (PageWriteback(page)) {
3981 ret = flush_write_bio(epd);
3982 BUG_ON(ret < 0);
3983 }
Chris Masond1310b22008-01-24 16:13:08 -05003984 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003985 }
Chris Masond1310b22008-01-24 16:13:08 -05003986
3987 if (PageWriteback(page) ||
3988 !clear_page_dirty_for_io(page)) {
3989 unlock_page(page);
3990 continue;
3991 }
3992
David Sterbaaab6e9e2017-11-30 18:00:02 +01003993 ret = __extent_writepage(page, wbc, epd);
Liu Boa91326672016-03-07 16:56:21 -08003994 if (ret < 0) {
3995 /*
3996 * done_index is set past this page,
3997 * so media errors will not choke
3998 * background writeout for the entire
3999 * file. This has consequences for
4000 * range_cyclic semantics (ie. it may
4001 * not be suitable for data integrity
4002 * writeout).
4003 */
4004 done_index = page->index + 1;
4005 done = 1;
4006 break;
4007 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004008
4009 /*
4010 * the filesystem may choose to bump up nr_to_write.
4011 * We have to make sure to honor the new nr_to_write
4012 * at any time
4013 */
4014 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004015 }
4016 pagevec_release(&pvec);
4017 cond_resched();
4018 }
Liu Bo894b36e2016-03-07 16:56:22 -08004019 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004020 /*
4021 * We hit the last page and there is more work to be done: wrap
4022 * back to the start of the file
4023 */
4024 scanned = 1;
4025 index = 0;
4026 goto retry;
4027 }
Liu Boa91326672016-03-07 16:56:21 -08004028
4029 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4030 mapping->writeback_index = done_index;
4031
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004032 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004033 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004034}
Chris Masond1310b22008-01-24 16:13:08 -05004035
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004036int extent_write_full_page(struct page *page, struct writeback_control *wbc)
Chris Masond1310b22008-01-24 16:13:08 -05004037{
4038 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004039 struct extent_page_data epd = {
4040 .bio = NULL,
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004041 .tree = &BTRFS_I(page->mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004042 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004043 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004044 };
Chris Masond1310b22008-01-24 16:13:08 -05004045
Chris Masond1310b22008-01-24 16:13:08 -05004046 ret = __extent_writepage(page, wbc, &epd);
Qu Wenruo30659762019-03-20 14:27:42 +08004047 ASSERT(ret <= 0);
4048 if (ret < 0) {
4049 end_write_bio(&epd, ret);
4050 return ret;
4051 }
Chris Masond1310b22008-01-24 16:13:08 -05004052
Qu Wenruo30659762019-03-20 14:27:42 +08004053 ret = flush_write_bio(&epd);
4054 ASSERT(ret <= 0);
Chris Masond1310b22008-01-24 16:13:08 -05004055 return ret;
4056}
Chris Masond1310b22008-01-24 16:13:08 -05004057
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004058int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
Chris Mason771ed682008-11-06 22:02:51 -05004059 int mode)
4060{
4061 int ret = 0;
4062 struct address_space *mapping = inode->i_mapping;
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004063 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Mason771ed682008-11-06 22:02:51 -05004064 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004065 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4066 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004067
4068 struct extent_page_data epd = {
4069 .bio = NULL,
4070 .tree = tree,
Chris Mason771ed682008-11-06 22:02:51 -05004071 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004072 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05004073 };
4074 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004075 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004076 .nr_to_write = nr_pages * 2,
4077 .range_start = start,
4078 .range_end = end + 1,
4079 };
4080
Chris Masond3977122009-01-05 21:25:51 -05004081 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004082 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004083 if (clear_page_dirty_for_io(page))
4084 ret = __extent_writepage(page, &wbc_writepages, &epd);
4085 else {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02004086 btrfs_writepage_endio_finish_ordered(page, start,
Nikolay Borisovc6297322018-11-08 10:18:08 +02004087 start + PAGE_SIZE - 1, 1);
Chris Mason771ed682008-11-06 22:02:51 -05004088 unlock_page(page);
4089 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004090 put_page(page);
4091 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004092 }
4093
Qu Wenruo02c6db42019-03-20 14:27:45 +08004094 ASSERT(ret <= 0);
4095 if (ret < 0) {
4096 end_write_bio(&epd, ret);
4097 return ret;
4098 }
4099 ret = flush_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004100 return ret;
4101}
Chris Masond1310b22008-01-24 16:13:08 -05004102
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004103int extent_writepages(struct address_space *mapping,
Chris Masond1310b22008-01-24 16:13:08 -05004104 struct writeback_control *wbc)
4105{
4106 int ret = 0;
Qu Wenruof4340622019-03-20 14:27:41 +08004107 int flush_ret;
Chris Masond1310b22008-01-24 16:13:08 -05004108 struct extent_page_data epd = {
4109 .bio = NULL,
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004110 .tree = &BTRFS_I(mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004111 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004112 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004113 };
4114
David Sterba935db852017-06-23 04:30:28 +02004115 ret = extent_write_cache_pages(mapping, wbc, &epd);
Qu Wenruof4340622019-03-20 14:27:41 +08004116 flush_ret = flush_write_bio(&epd);
4117 BUG_ON(flush_ret < 0);
Chris Masond1310b22008-01-24 16:13:08 -05004118 return ret;
4119}
Chris Masond1310b22008-01-24 16:13:08 -05004120
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004121int extent_readpages(struct address_space *mapping, struct list_head *pages,
4122 unsigned nr_pages)
Chris Masond1310b22008-01-24 16:13:08 -05004123{
4124 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004125 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004126 struct page *pagepool[16];
Miao Xie125bac012013-07-25 19:22:37 +08004127 struct extent_map *em_cached = NULL;
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004128 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Liu Bo67c96842012-07-20 21:43:09 -06004129 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004130 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004131
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004132 while (!list_empty(pages)) {
Nikolay Borisove65ef212019-03-11 09:55:38 +02004133 u64 contig_end = 0;
4134
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004135 for (nr = 0; nr < ARRAY_SIZE(pagepool) && !list_empty(pages);) {
Nikolay Borisovf86196e2019-01-03 15:29:02 -08004136 struct page *page = lru_to_page(pages);
Chris Masond1310b22008-01-24 16:13:08 -05004137
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004138 prefetchw(&page->flags);
4139 list_del(&page->lru);
4140 if (add_to_page_cache_lru(page, mapping, page->index,
4141 readahead_gfp_mask(mapping))) {
4142 put_page(page);
Nikolay Borisove65ef212019-03-11 09:55:38 +02004143 break;
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004144 }
4145
4146 pagepool[nr++] = page;
Nikolay Borisove65ef212019-03-11 09:55:38 +02004147 contig_end = page_offset(page) + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004148 }
Liu Bo67c96842012-07-20 21:43:09 -06004149
Nikolay Borisove65ef212019-03-11 09:55:38 +02004150 if (nr) {
4151 u64 contig_start = page_offset(pagepool[0]);
4152
4153 ASSERT(contig_start + nr * PAGE_SIZE - 1 == contig_end);
4154
4155 contiguous_readpages(tree, pagepool, nr, contig_start,
4156 contig_end, &em_cached, &bio, &bio_flags,
4157 &prev_em_start);
4158 }
Chris Masond1310b22008-01-24 16:13:08 -05004159 }
Liu Bo67c96842012-07-20 21:43:09 -06004160
Miao Xie125bac012013-07-25 19:22:37 +08004161 if (em_cached)
4162 free_extent_map(em_cached);
4163
Chris Masond1310b22008-01-24 16:13:08 -05004164 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004165 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004166 return 0;
4167}
Chris Masond1310b22008-01-24 16:13:08 -05004168
4169/*
4170 * basic invalidatepage code, this waits on any locked or writeback
4171 * ranges corresponding to the page, and then deletes any extent state
4172 * records from the tree
4173 */
4174int extent_invalidatepage(struct extent_io_tree *tree,
4175 struct page *page, unsigned long offset)
4176{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004177 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004178 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004179 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004180 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4181
Qu Wenruofda28322013-02-26 08:10:22 +00004182 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004183 if (start > end)
4184 return 0;
4185
David Sterbaff13db42015-12-03 14:30:40 +01004186 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004187 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004188 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004189 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4190 EXTENT_DO_ACCOUNTING,
David Sterbaae0f1622017-10-31 16:37:52 +01004191 1, 1, &cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05004192 return 0;
4193}
Chris Masond1310b22008-01-24 16:13:08 -05004194
4195/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004196 * a helper for releasepage, this tests for areas of the page that
4197 * are locked or under IO and drops the related state bits if it is safe
4198 * to drop the page.
4199 */
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004200static int try_release_extent_state(struct extent_io_tree *tree,
Eric Sandeen48a3b632013-04-25 20:41:01 +00004201 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004202{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004203 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004204 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004205 int ret = 1;
4206
Nikolay Borisov88826792019-03-14 15:28:31 +02004207 if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) {
Chris Mason7b13b7b2008-04-18 10:29:50 -04004208 ret = 0;
Nikolay Borisov88826792019-03-14 15:28:31 +02004209 } else {
Chris Mason11ef1602009-09-23 20:28:46 -04004210 /*
4211 * at this point we can safely clear everything except the
4212 * locked bit and the nodatasum bit
4213 */
David Sterba66b0c882017-10-31 16:30:47 +01004214 ret = __clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004215 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
David Sterba66b0c882017-10-31 16:30:47 +01004216 0, 0, NULL, mask, NULL);
Chris Masone3f24cc2011-02-14 12:52:08 -05004217
4218 /* if clear_extent_bit failed for enomem reasons,
4219 * we can't allow the release to continue.
4220 */
4221 if (ret < 0)
4222 ret = 0;
4223 else
4224 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004225 }
4226 return ret;
4227}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004228
4229/*
Chris Masond1310b22008-01-24 16:13:08 -05004230 * a helper for releasepage. As long as there are no locked extents
4231 * in the range corresponding to the page, both state records and extent
4232 * map records are removed
4233 */
Nikolay Borisov477a30b2018-04-19 10:46:34 +03004234int try_release_extent_mapping(struct page *page, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004235{
4236 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004237 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004238 u64 end = start + PAGE_SIZE - 1;
Filipe Mananabd3599a2018-07-12 01:36:43 +01004239 struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
4240 struct extent_io_tree *tree = &btrfs_inode->io_tree;
4241 struct extent_map_tree *map = &btrfs_inode->extent_tree;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004242
Mel Gormand0164ad2015-11-06 16:28:21 -08004243 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004244 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004245 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004246 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004247 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004248 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004249 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004250 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004251 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004252 break;
4253 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004254 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4255 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004256 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004257 free_extent_map(em);
4258 break;
4259 }
4260 if (!test_range_bit(tree, em->start,
4261 extent_map_end(em) - 1,
Nikolay Borisov4e586ca2019-03-14 15:28:30 +02004262 EXTENT_LOCKED, 0, NULL)) {
Filipe Mananabd3599a2018-07-12 01:36:43 +01004263 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4264 &btrfs_inode->runtime_flags);
Chris Mason70dec802008-01-29 09:59:12 -05004265 remove_extent_mapping(map, em);
4266 /* once for the rb tree */
4267 free_extent_map(em);
4268 }
4269 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004270 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004271
4272 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004273 free_extent_map(em);
4274 }
Chris Masond1310b22008-01-24 16:13:08 -05004275 }
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004276 return try_release_extent_state(tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004277}
Chris Masond1310b22008-01-24 16:13:08 -05004278
Chris Masonec29ed52011-02-23 16:23:20 -05004279/*
4280 * helper function for fiemap, which doesn't want to see any holes.
4281 * This maps until we find something past 'last'
4282 */
4283static struct extent_map *get_extent_skip_holes(struct inode *inode,
David Sterbae3350e12017-06-23 04:09:57 +02004284 u64 offset, u64 last)
Chris Masonec29ed52011-02-23 16:23:20 -05004285{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004286 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004287 struct extent_map *em;
4288 u64 len;
4289
4290 if (offset >= last)
4291 return NULL;
4292
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304293 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004294 len = last - offset;
4295 if (len == 0)
4296 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004297 len = ALIGN(len, sectorsize);
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02004298 em = btrfs_get_extent_fiemap(BTRFS_I(inode), offset, len);
David Sterbac7040052011-04-19 18:00:01 +02004299 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004300 return em;
4301
4302 /* if this isn't a hole return it */
Nikolay Borisov4a2d25c2017-11-23 10:51:43 +02004303 if (em->block_start != EXTENT_MAP_HOLE)
Chris Masonec29ed52011-02-23 16:23:20 -05004304 return em;
Chris Masonec29ed52011-02-23 16:23:20 -05004305
4306 /* this is a hole, advance to the next extent */
4307 offset = extent_map_end(em);
4308 free_extent_map(em);
4309 if (offset >= last)
4310 break;
4311 }
4312 return NULL;
4313}
4314
Qu Wenruo47518322017-04-07 10:43:15 +08004315/*
4316 * To cache previous fiemap extent
4317 *
4318 * Will be used for merging fiemap extent
4319 */
4320struct fiemap_cache {
4321 u64 offset;
4322 u64 phys;
4323 u64 len;
4324 u32 flags;
4325 bool cached;
4326};
4327
4328/*
4329 * Helper to submit fiemap extent.
4330 *
4331 * Will try to merge current fiemap extent specified by @offset, @phys,
4332 * @len and @flags with cached one.
4333 * And only when we fails to merge, cached one will be submitted as
4334 * fiemap extent.
4335 *
4336 * Return value is the same as fiemap_fill_next_extent().
4337 */
4338static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4339 struct fiemap_cache *cache,
4340 u64 offset, u64 phys, u64 len, u32 flags)
4341{
4342 int ret = 0;
4343
4344 if (!cache->cached)
4345 goto assign;
4346
4347 /*
4348 * Sanity check, extent_fiemap() should have ensured that new
Andrea Gelmini52042d82018-11-28 12:05:13 +01004349 * fiemap extent won't overlap with cached one.
Qu Wenruo47518322017-04-07 10:43:15 +08004350 * Not recoverable.
4351 *
4352 * NOTE: Physical address can overlap, due to compression
4353 */
4354 if (cache->offset + cache->len > offset) {
4355 WARN_ON(1);
4356 return -EINVAL;
4357 }
4358
4359 /*
4360 * Only merges fiemap extents if
4361 * 1) Their logical addresses are continuous
4362 *
4363 * 2) Their physical addresses are continuous
4364 * So truly compressed (physical size smaller than logical size)
4365 * extents won't get merged with each other
4366 *
4367 * 3) Share same flags except FIEMAP_EXTENT_LAST
4368 * So regular extent won't get merged with prealloc extent
4369 */
4370 if (cache->offset + cache->len == offset &&
4371 cache->phys + cache->len == phys &&
4372 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4373 (flags & ~FIEMAP_EXTENT_LAST)) {
4374 cache->len += len;
4375 cache->flags |= flags;
4376 goto try_submit_last;
4377 }
4378
4379 /* Not mergeable, need to submit cached one */
4380 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4381 cache->len, cache->flags);
4382 cache->cached = false;
4383 if (ret)
4384 return ret;
4385assign:
4386 cache->cached = true;
4387 cache->offset = offset;
4388 cache->phys = phys;
4389 cache->len = len;
4390 cache->flags = flags;
4391try_submit_last:
4392 if (cache->flags & FIEMAP_EXTENT_LAST) {
4393 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4394 cache->phys, cache->len, cache->flags);
4395 cache->cached = false;
4396 }
4397 return ret;
4398}
4399
4400/*
Qu Wenruo848c23b2017-06-22 10:01:21 +08004401 * Emit last fiemap cache
Qu Wenruo47518322017-04-07 10:43:15 +08004402 *
Qu Wenruo848c23b2017-06-22 10:01:21 +08004403 * The last fiemap cache may still be cached in the following case:
4404 * 0 4k 8k
4405 * |<- Fiemap range ->|
4406 * |<------------ First extent ----------->|
4407 *
4408 * In this case, the first extent range will be cached but not emitted.
4409 * So we must emit it before ending extent_fiemap().
Qu Wenruo47518322017-04-07 10:43:15 +08004410 */
Qu Wenruo848c23b2017-06-22 10:01:21 +08004411static int emit_last_fiemap_cache(struct btrfs_fs_info *fs_info,
4412 struct fiemap_extent_info *fieinfo,
4413 struct fiemap_cache *cache)
Qu Wenruo47518322017-04-07 10:43:15 +08004414{
4415 int ret;
4416
4417 if (!cache->cached)
4418 return 0;
4419
Qu Wenruo47518322017-04-07 10:43:15 +08004420 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4421 cache->len, cache->flags);
4422 cache->cached = false;
4423 if (ret > 0)
4424 ret = 0;
4425 return ret;
4426}
4427
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004428int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterba2135fb92017-06-23 04:09:57 +02004429 __u64 start, __u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004430{
Josef Bacik975f84f2010-11-23 19:36:57 +00004431 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004432 u64 off = start;
4433 u64 max = start + len;
4434 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004435 u32 found_type;
4436 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004437 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004438 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004439 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004440 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004441 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004442 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004443 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004444 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004445 struct fiemap_cache cache = { 0 };
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004446 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004447 u64 em_start = 0;
4448 u64 em_len = 0;
4449 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004450
4451 if (len == 0)
4452 return -EINVAL;
4453
Josef Bacik975f84f2010-11-23 19:36:57 +00004454 path = btrfs_alloc_path();
4455 if (!path)
4456 return -ENOMEM;
4457 path->leave_spinning = 1;
4458
Jeff Mahoneyda170662016-06-15 09:22:56 -04004459 start = round_down(start, btrfs_inode_sectorsize(inode));
4460 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004461
Chris Masonec29ed52011-02-23 16:23:20 -05004462 /*
4463 * lookup the last file extent. We're not using i_size here
4464 * because there might be preallocation past i_size
4465 */
David Sterbaf85b7372017-01-20 14:54:07 +01004466 ret = btrfs_lookup_file_extent(NULL, root, path,
4467 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004468 if (ret < 0) {
4469 btrfs_free_path(path);
4470 return ret;
Liu Bo2d324f52016-05-17 17:21:48 -07004471 } else {
4472 WARN_ON(!ret);
4473 if (ret == 1)
4474 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004475 }
Liu Bo2d324f52016-05-17 17:21:48 -07004476
Josef Bacik975f84f2010-11-23 19:36:57 +00004477 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004478 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004479 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004480
Chris Masonec29ed52011-02-23 16:23:20 -05004481 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004482 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004483 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004484 /* have to trust i_size as the end */
4485 last = (u64)-1;
4486 last_for_get_extent = isize;
4487 } else {
4488 /*
4489 * remember the start of the last extent. There are a
4490 * bunch of different factors that go into the length of the
4491 * extent, so its much less complex to remember where it started
4492 */
4493 last = found_key.offset;
4494 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004495 }
Liu Bofe09e162013-09-22 12:54:23 +08004496 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004497
Chris Masonec29ed52011-02-23 16:23:20 -05004498 /*
4499 * we might have some extents allocated but more delalloc past those
4500 * extents. so, we trust isize unless the start of the last extent is
4501 * beyond isize
4502 */
4503 if (last < isize) {
4504 last = (u64)-1;
4505 last_for_get_extent = isize;
4506 }
4507
David Sterbaff13db42015-12-03 14:30:40 +01004508 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004509 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004510
David Sterbae3350e12017-06-23 04:09:57 +02004511 em = get_extent_skip_holes(inode, start, last_for_get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004512 if (!em)
4513 goto out;
4514 if (IS_ERR(em)) {
4515 ret = PTR_ERR(em);
4516 goto out;
4517 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004518
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004519 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004520 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004521
Chris Masonea8efc72011-03-08 11:54:40 -05004522 /* break if the extent we found is outside the range */
4523 if (em->start >= max || extent_map_end(em) < off)
4524 break;
4525
4526 /*
4527 * get_extent may return an extent that starts before our
4528 * requested range. We have to make sure the ranges
4529 * we return to fiemap always move forward and don't
4530 * overlap, so adjust the offsets here
4531 */
4532 em_start = max(em->start, off);
4533
4534 /*
4535 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004536 * for adjusting the disk offset below. Only do this if the
4537 * extent isn't compressed since our in ram offset may be past
4538 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004539 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004540 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4541 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004542 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004543 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004544 flags = 0;
Filipe Mananaf0986312018-06-20 10:02:30 +01004545 if (em->block_start < EXTENT_MAP_LAST_BYTE)
4546 disko = em->block_start + offset_in_extent;
4547 else
4548 disko = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004549
Chris Masonea8efc72011-03-08 11:54:40 -05004550 /*
4551 * bump off for our next call to get_extent
4552 */
4553 off = extent_map_end(em);
4554 if (off >= max)
4555 end = 1;
4556
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004557 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004558 end = 1;
4559 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004560 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004561 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4562 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004563 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004564 flags |= (FIEMAP_EXTENT_DELALLOC |
4565 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004566 } else if (fieinfo->fi_extents_max) {
4567 u64 bytenr = em->block_start -
4568 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004569
Liu Bofe09e162013-09-22 12:54:23 +08004570 /*
4571 * As btrfs supports shared space, this information
4572 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004573 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4574 * then we're just getting a count and we can skip the
4575 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004576 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06004577 ret = btrfs_check_shared(root,
4578 btrfs_ino(BTRFS_I(inode)),
4579 bytenr);
Josef Bacikdc046b12014-09-10 16:20:45 -04004580 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004581 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004582 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004583 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004584 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004585 }
4586 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4587 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004588 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4589 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004590
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004591 free_extent_map(em);
4592 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004593 if ((em_start >= last) || em_len == (u64)-1 ||
4594 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004595 flags |= FIEMAP_EXTENT_LAST;
4596 end = 1;
4597 }
4598
Chris Masonec29ed52011-02-23 16:23:20 -05004599 /* now scan forward to see if this is really the last extent. */
David Sterbae3350e12017-06-23 04:09:57 +02004600 em = get_extent_skip_holes(inode, off, last_for_get_extent);
Chris Masonec29ed52011-02-23 16:23:20 -05004601 if (IS_ERR(em)) {
4602 ret = PTR_ERR(em);
4603 goto out;
4604 }
4605 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004606 flags |= FIEMAP_EXTENT_LAST;
4607 end = 1;
4608 }
Qu Wenruo47518322017-04-07 10:43:15 +08004609 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4610 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004611 if (ret) {
4612 if (ret == 1)
4613 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004614 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004615 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004616 }
4617out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004618 if (!ret)
Qu Wenruo848c23b2017-06-22 10:01:21 +08004619 ret = emit_last_fiemap_cache(root->fs_info, fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004620 free_extent_map(em);
4621out:
Liu Bofe09e162013-09-22 12:54:23 +08004622 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004623 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004624 &cached_state);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004625 return ret;
4626}
4627
Chris Mason727011e2010-08-06 13:21:20 -04004628static void __free_extent_buffer(struct extent_buffer *eb)
4629{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004630 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004631 kmem_cache_free(extent_buffer_cache, eb);
4632}
4633
Josef Bacika26e8c92014-03-28 17:07:27 -04004634int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004635{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004636 return (atomic_read(&eb->io_pages) ||
4637 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4638 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004639}
4640
Miao Xie897ca6e92010-10-26 20:57:29 -04004641/*
David Sterba55ac0132018-07-19 17:24:32 +02004642 * Release all pages attached to the extent buffer.
Miao Xie897ca6e92010-10-26 20:57:29 -04004643 */
David Sterba55ac0132018-07-19 17:24:32 +02004644static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004645{
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004646 int i;
4647 int num_pages;
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004648 int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004649
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004650 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004651
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004652 num_pages = num_extent_pages(eb);
4653 for (i = 0; i < num_pages; i++) {
4654 struct page *page = eb->pages[i];
Miao Xie897ca6e92010-10-26 20:57:29 -04004655
Forrest Liu5d2361d2015-02-09 17:31:45 +08004656 if (!page)
4657 continue;
4658 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004659 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004660 /*
4661 * We do this since we'll remove the pages after we've
4662 * removed the eb from the radix tree, so we could race
4663 * and have this page now attached to the new eb. So
4664 * only clear page_private if it's still connected to
4665 * this eb.
4666 */
4667 if (PagePrivate(page) &&
4668 page->private == (unsigned long)eb) {
4669 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4670 BUG_ON(PageDirty(page));
4671 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004672 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004673 * We need to make sure we haven't be attached
4674 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004675 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004676 ClearPagePrivate(page);
4677 set_page_private(page, 0);
4678 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004679 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004680 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004681
4682 if (mapped)
4683 spin_unlock(&page->mapping->private_lock);
4684
Nicholas D Steeves01327612016-05-19 21:18:45 -04004685 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004686 put_page(page);
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004687 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004688}
4689
4690/*
4691 * Helper for releasing the extent buffer.
4692 */
4693static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4694{
David Sterba55ac0132018-07-19 17:24:32 +02004695 btrfs_release_extent_buffer_pages(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004696 __free_extent_buffer(eb);
4697}
4698
Josef Bacikf28491e2013-12-16 13:24:27 -05004699static struct extent_buffer *
4700__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004701 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004702{
4703 struct extent_buffer *eb = NULL;
4704
Michal Hockod1b5c562015-08-19 14:17:40 +02004705 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004706 eb->start = start;
4707 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004708 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004709 eb->bflags = 0;
4710 rwlock_init(&eb->lock);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004711 atomic_set(&eb->blocking_readers, 0);
4712 atomic_set(&eb->blocking_writers, 0);
David Sterbaed1b4ed2018-08-24 16:31:17 +02004713 eb->lock_nested = false;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004714 init_waitqueue_head(&eb->write_lock_wq);
4715 init_waitqueue_head(&eb->read_lock_wq);
4716
4717 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4718
4719 spin_lock_init(&eb->refs_lock);
4720 atomic_set(&eb->refs, 1);
4721 atomic_set(&eb->io_pages, 0);
4722
4723 /*
4724 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4725 */
4726 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4727 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4728 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4729
David Sterba843ccf92018-08-24 14:56:28 +02004730#ifdef CONFIG_BTRFS_DEBUG
4731 atomic_set(&eb->spinning_writers, 0);
David Sterbaafd495a2018-08-24 15:57:38 +02004732 atomic_set(&eb->spinning_readers, 0);
David Sterba5c9c7992018-08-24 16:15:51 +02004733 atomic_set(&eb->read_locks, 0);
David Sterbac79adfc2018-08-24 16:24:26 +02004734 atomic_set(&eb->write_locks, 0);
David Sterba843ccf92018-08-24 14:56:28 +02004735#endif
4736
Josef Bacikdb7f3432013-08-07 14:54:37 -04004737 return eb;
4738}
4739
4740struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4741{
David Sterbacc5e31a2018-03-01 18:20:27 +01004742 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004743 struct page *p;
4744 struct extent_buffer *new;
David Sterbacc5e31a2018-03-01 18:20:27 +01004745 int num_pages = num_extent_pages(src);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004746
David Sterba3f556f72014-06-15 03:20:26 +02004747 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004748 if (new == NULL)
4749 return NULL;
4750
4751 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004752 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004753 if (!p) {
4754 btrfs_release_extent_buffer(new);
4755 return NULL;
4756 }
4757 attach_extent_buffer_page(new, p);
4758 WARN_ON(PageDirty(p));
4759 SetPageUptodate(p);
4760 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004761 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004762 }
4763
Josef Bacikdb7f3432013-08-07 14:54:37 -04004764 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004765 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004766
4767 return new;
4768}
4769
Omar Sandoval0f331222015-09-29 20:50:31 -07004770struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4771 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004772{
4773 struct extent_buffer *eb;
David Sterbacc5e31a2018-03-01 18:20:27 +01004774 int num_pages;
4775 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004776
David Sterba3f556f72014-06-15 03:20:26 +02004777 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004778 if (!eb)
4779 return NULL;
4780
David Sterba65ad0102018-06-29 10:56:49 +02004781 num_pages = num_extent_pages(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004782 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004783 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004784 if (!eb->pages[i])
4785 goto err;
4786 }
4787 set_extent_buffer_uptodate(eb);
4788 btrfs_set_header_nritems(eb, 0);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004789 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004790
4791 return eb;
4792err:
4793 for (; i > 0; i--)
4794 __free_page(eb->pages[i - 1]);
4795 __free_extent_buffer(eb);
4796 return NULL;
4797}
4798
Omar Sandoval0f331222015-09-29 20:50:31 -07004799struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004800 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004801{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004802 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004803}
4804
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004805static void check_buffer_tree_ref(struct extent_buffer *eb)
4806{
Chris Mason242e18c2013-01-29 17:49:37 -05004807 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004808 /* the ref bit is tricky. We have to make sure it is set
4809 * if we have the buffer dirty. Otherwise the
4810 * code to free a buffer can end up dropping a dirty
4811 * page
4812 *
4813 * Once the ref bit is set, it won't go away while the
4814 * buffer is dirty or in writeback, and it also won't
4815 * go away while we have the reference count on the
4816 * eb bumped.
4817 *
4818 * We can't just set the ref bit without bumping the
4819 * ref on the eb because free_extent_buffer might
4820 * see the ref bit and try to clear it. If this happens
4821 * free_extent_buffer might end up dropping our original
4822 * ref by mistake and freeing the page before we are able
4823 * to add one more ref.
4824 *
4825 * So bump the ref count first, then set the bit. If someone
4826 * beat us to it, drop the ref we added.
4827 */
Chris Mason242e18c2013-01-29 17:49:37 -05004828 refs = atomic_read(&eb->refs);
4829 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4830 return;
4831
Josef Bacik594831c2012-07-20 16:11:08 -04004832 spin_lock(&eb->refs_lock);
4833 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004834 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004835 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004836}
4837
Mel Gorman2457aec2014-06-04 16:10:31 -07004838static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4839 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004840{
David Sterbacc5e31a2018-03-01 18:20:27 +01004841 int num_pages, i;
Josef Bacik5df42352012-03-15 18:24:42 -04004842
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004843 check_buffer_tree_ref(eb);
4844
David Sterba65ad0102018-06-29 10:56:49 +02004845 num_pages = num_extent_pages(eb);
Josef Bacik5df42352012-03-15 18:24:42 -04004846 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004847 struct page *p = eb->pages[i];
4848
Mel Gorman2457aec2014-06-04 16:10:31 -07004849 if (p != accessed)
4850 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004851 }
4852}
4853
Josef Bacikf28491e2013-12-16 13:24:27 -05004854struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4855 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004856{
4857 struct extent_buffer *eb;
4858
4859 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004860 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004861 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004862 if (eb && atomic_inc_not_zero(&eb->refs)) {
4863 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004864 /*
4865 * Lock our eb's refs_lock to avoid races with
4866 * free_extent_buffer. When we get our eb it might be flagged
4867 * with EXTENT_BUFFER_STALE and another task running
4868 * free_extent_buffer might have seen that flag set,
4869 * eb->refs == 2, that the buffer isn't under IO (dirty and
4870 * writeback flags not set) and it's still in the tree (flag
4871 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4872 * of decrementing the extent buffer's reference count twice.
4873 * So here we could race and increment the eb's reference count,
4874 * clear its stale flag, mark it as dirty and drop our reference
4875 * before the other task finishes executing free_extent_buffer,
4876 * which would later result in an attempt to free an extent
4877 * buffer that is dirty.
4878 */
4879 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4880 spin_lock(&eb->refs_lock);
4881 spin_unlock(&eb->refs_lock);
4882 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004883 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004884 return eb;
4885 }
4886 rcu_read_unlock();
4887
4888 return NULL;
4889}
4890
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004891#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4892struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004893 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004894{
4895 struct extent_buffer *eb, *exists = NULL;
4896 int ret;
4897
4898 eb = find_extent_buffer(fs_info, start);
4899 if (eb)
4900 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04004901 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004902 if (!eb)
4903 return NULL;
4904 eb->fs_info = fs_info;
4905again:
David Sterbae1860a72016-05-09 14:11:38 +02004906 ret = radix_tree_preload(GFP_NOFS);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004907 if (ret)
4908 goto free_eb;
4909 spin_lock(&fs_info->buffer_lock);
4910 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004911 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004912 spin_unlock(&fs_info->buffer_lock);
4913 radix_tree_preload_end();
4914 if (ret == -EEXIST) {
4915 exists = find_extent_buffer(fs_info, start);
4916 if (exists)
4917 goto free_eb;
4918 else
4919 goto again;
4920 }
4921 check_buffer_tree_ref(eb);
4922 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
4923
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004924 return eb;
4925free_eb:
4926 btrfs_release_extent_buffer(eb);
4927 return exists;
4928}
4929#endif
4930
Josef Bacikf28491e2013-12-16 13:24:27 -05004931struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004932 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05004933{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004934 unsigned long len = fs_info->nodesize;
David Sterbacc5e31a2018-03-01 18:20:27 +01004935 int num_pages;
4936 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004937 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004938 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004939 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004940 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05004941 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05004942 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004943 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004944
Jeff Mahoneyda170662016-06-15 09:22:56 -04004945 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07004946 btrfs_err(fs_info, "bad tree block start %llu", start);
4947 return ERR_PTR(-EINVAL);
4948 }
4949
Josef Bacikf28491e2013-12-16 13:24:27 -05004950 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004951 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004952 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004953
David Sterba23d79d82014-06-15 02:55:29 +02004954 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04004955 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07004956 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05004957
David Sterba65ad0102018-06-29 10:56:49 +02004958 num_pages = num_extent_pages(eb);
Chris Mason727011e2010-08-06 13:21:20 -04004959 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02004960 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07004961 if (!p) {
4962 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04004963 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004964 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004965
4966 spin_lock(&mapping->private_lock);
4967 if (PagePrivate(p)) {
4968 /*
4969 * We could have already allocated an eb for this page
4970 * and attached one so lets see if we can get a ref on
4971 * the existing eb, and if we can we know it's good and
4972 * we can just return that one, else we know we can just
4973 * overwrite page->private.
4974 */
4975 exists = (struct extent_buffer *)p->private;
4976 if (atomic_inc_not_zero(&exists->refs)) {
4977 spin_unlock(&mapping->private_lock);
4978 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004979 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07004980 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004981 goto free_eb;
4982 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08004983 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004984
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004985 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004986 * Do this so attach doesn't complain and we need to
4987 * drop the ref the old guy had.
4988 */
4989 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004990 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004991 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05004992 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004993 attach_extent_buffer_page(eb, p);
4994 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004995 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04004996 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004997 if (!PageUptodate(p))
4998 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004999
5000 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005001 * We can't unlock the pages just yet since the extent buffer
5002 * hasn't been properly inserted in the radix tree, this
5003 * opens a race with btree_releasepage which can free a page
5004 * while we are still filling in all pages for the buffer and
5005 * we could crash.
Chris Masoneb14ab82011-02-10 12:35:00 -05005006 */
Chris Masond1310b22008-01-24 16:13:08 -05005007 }
5008 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05005009 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05005010again:
David Sterbae1860a72016-05-09 14:11:38 +02005011 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07005012 if (ret) {
5013 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04005014 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005015 }
Miao Xie19fe0a82010-10-26 20:57:29 -04005016
Josef Bacikf28491e2013-12-16 13:24:27 -05005017 spin_lock(&fs_info->buffer_lock);
5018 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005019 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05005020 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005021 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04005022 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005023 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005024 if (exists)
5025 goto free_eb;
5026 else
Josef Bacik115391d2012-03-09 09:51:43 -05005027 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04005028 }
Chris Mason6af118ce2008-07-22 11:18:07 -04005029 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005030 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005031 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005032
5033 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005034 * Now it's safe to unlock the pages because any calls to
5035 * btree_releasepage will correctly detect that a page belongs to a
5036 * live buffer and won't free them prematurely.
Chris Masoneb14ab82011-02-10 12:35:00 -05005037 */
Nikolay Borisov28187ae2018-07-04 10:24:51 +03005038 for (i = 0; i < num_pages; i++)
5039 unlock_page(eb->pages[i]);
Chris Masond1310b22008-01-24 16:13:08 -05005040 return eb;
5041
Chris Mason6af118ce2008-07-22 11:18:07 -04005042free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005043 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005044 for (i = 0; i < num_pages; i++) {
5045 if (eb->pages[i])
5046 unlock_page(eb->pages[i]);
5047 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005048
Miao Xie897ca6e92010-10-26 20:57:29 -04005049 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005050 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005051}
Chris Masond1310b22008-01-24 16:13:08 -05005052
Josef Bacik3083ee22012-03-09 16:01:49 -05005053static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5054{
5055 struct extent_buffer *eb =
5056 container_of(head, struct extent_buffer, rcu_head);
5057
5058 __free_extent_buffer(eb);
5059}
5060
David Sterbaf7a52a42013-04-26 14:56:29 +00005061static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005062{
Nikolay Borisov07e21c42018-06-27 16:38:23 +03005063 lockdep_assert_held(&eb->refs_lock);
5064
Josef Bacik3083ee22012-03-09 16:01:49 -05005065 WARN_ON(atomic_read(&eb->refs) == 0);
5066 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005067 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005068 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005069
Jan Schmidt815a51c2012-05-16 17:00:02 +02005070 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005071
Josef Bacikf28491e2013-12-16 13:24:27 -05005072 spin_lock(&fs_info->buffer_lock);
5073 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005074 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005075 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005076 } else {
5077 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005078 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005079
5080 /* Should be safe to release our pages at this point */
David Sterba55ac0132018-07-19 17:24:32 +02005081 btrfs_release_extent_buffer_pages(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005082#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005083 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
Josef Bacikbcb7e442015-03-16 17:38:02 -04005084 __free_extent_buffer(eb);
5085 return 1;
5086 }
5087#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005088 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005089 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005090 }
5091 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005092
5093 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005094}
5095
Chris Masond1310b22008-01-24 16:13:08 -05005096void free_extent_buffer(struct extent_buffer *eb)
5097{
Chris Mason242e18c2013-01-29 17:49:37 -05005098 int refs;
5099 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005100 if (!eb)
5101 return;
5102
Chris Mason242e18c2013-01-29 17:49:37 -05005103 while (1) {
5104 refs = atomic_read(&eb->refs);
Nikolay Borisov46cc7752018-10-15 17:04:01 +03005105 if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
5106 || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
5107 refs == 1))
Chris Mason242e18c2013-01-29 17:49:37 -05005108 break;
5109 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5110 if (old == refs)
5111 return;
5112 }
5113
Josef Bacik3083ee22012-03-09 16:01:49 -05005114 spin_lock(&eb->refs_lock);
5115 if (atomic_read(&eb->refs) == 2 &&
5116 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005117 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005118 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5119 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005120
Josef Bacik3083ee22012-03-09 16:01:49 -05005121 /*
5122 * I know this is terrible, but it's temporary until we stop tracking
5123 * the uptodate bits and such for the extent buffers.
5124 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005125 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005126}
Chris Masond1310b22008-01-24 16:13:08 -05005127
Josef Bacik3083ee22012-03-09 16:01:49 -05005128void free_extent_buffer_stale(struct extent_buffer *eb)
5129{
5130 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005131 return;
5132
Josef Bacik3083ee22012-03-09 16:01:49 -05005133 spin_lock(&eb->refs_lock);
5134 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5135
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005136 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005137 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5138 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005139 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005140}
5141
Chris Mason1d4284b2012-03-28 20:31:37 -04005142void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005143{
David Sterbacc5e31a2018-03-01 18:20:27 +01005144 int i;
5145 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005146 struct page *page;
5147
David Sterba65ad0102018-06-29 10:56:49 +02005148 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005149
5150 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005151 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005152 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005153 continue;
5154
Chris Masona61e6f22008-07-22 11:18:08 -04005155 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005156 WARN_ON(!PagePrivate(page));
5157
Chris Masond1310b22008-01-24 16:13:08 -05005158 clear_page_dirty_for_io(page);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005159 xa_lock_irq(&page->mapping->i_pages);
Matthew Wilcox0a943c62017-12-04 10:37:22 -05005160 if (!PageDirty(page))
5161 __xa_clear_mark(&page->mapping->i_pages,
5162 page_index(page), PAGECACHE_TAG_DIRTY);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005163 xa_unlock_irq(&page->mapping->i_pages);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005164 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005165 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005166 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005167 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005168}
Chris Masond1310b22008-01-24 16:13:08 -05005169
Liu Boabb57ef2018-09-14 01:44:42 +08005170bool set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005171{
David Sterbacc5e31a2018-03-01 18:20:27 +01005172 int i;
5173 int num_pages;
Liu Boabb57ef2018-09-14 01:44:42 +08005174 bool was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005175
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005176 check_buffer_tree_ref(eb);
5177
Chris Masonb9473432009-03-13 11:00:37 -04005178 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005179
David Sterba65ad0102018-06-29 10:56:49 +02005180 num_pages = num_extent_pages(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05005181 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005182 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5183
Liu Boabb57ef2018-09-14 01:44:42 +08005184 if (!was_dirty)
5185 for (i = 0; i < num_pages; i++)
5186 set_page_dirty(eb->pages[i]);
Liu Bo51995c32018-09-14 01:46:08 +08005187
5188#ifdef CONFIG_BTRFS_DEBUG
5189 for (i = 0; i < num_pages; i++)
5190 ASSERT(PageDirty(eb->pages[i]));
5191#endif
5192
Chris Masonb9473432009-03-13 11:00:37 -04005193 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005194}
Chris Masond1310b22008-01-24 16:13:08 -05005195
David Sterba69ba3922015-12-03 13:08:59 +01005196void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005197{
David Sterbacc5e31a2018-03-01 18:20:27 +01005198 int i;
Chris Mason1259ab72008-05-12 13:39:03 -04005199 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005200 int num_pages;
Chris Mason1259ab72008-05-12 13:39:03 -04005201
Chris Masonb4ce94d2009-02-04 09:25:08 -05005202 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005203 num_pages = num_extent_pages(eb);
Chris Mason1259ab72008-05-12 13:39:03 -04005204 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005205 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005206 if (page)
5207 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005208 }
Chris Mason1259ab72008-05-12 13:39:03 -04005209}
5210
David Sterba09c25a82015-12-03 13:08:59 +01005211void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005212{
David Sterbacc5e31a2018-03-01 18:20:27 +01005213 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005214 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005215 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005216
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005217 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005218 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005219 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005220 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005221 SetPageUptodate(page);
5222 }
Chris Masond1310b22008-01-24 16:13:08 -05005223}
Chris Masond1310b22008-01-24 16:13:08 -05005224
Chris Masond1310b22008-01-24 16:13:08 -05005225int read_extent_buffer_pages(struct extent_io_tree *tree,
David Sterba6af49db2017-06-23 04:09:57 +02005226 struct extent_buffer *eb, int wait, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005227{
David Sterbacc5e31a2018-03-01 18:20:27 +01005228 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005229 struct page *page;
5230 int err;
5231 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005232 int locked_pages = 0;
5233 int all_uptodate = 1;
David Sterbacc5e31a2018-03-01 18:20:27 +01005234 int num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005235 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005236 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005237 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005238
Chris Masonb4ce94d2009-02-04 09:25:08 -05005239 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005240 return 0;
5241
David Sterba65ad0102018-06-29 10:56:49 +02005242 num_pages = num_extent_pages(eb);
Josef Bacik8436ea912016-09-02 15:40:03 -04005243 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005244 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005245 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005246 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005247 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005248 } else {
5249 lock_page(page);
5250 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005251 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005252 }
5253 /*
5254 * We need to firstly lock all pages to make sure that
5255 * the uptodate bit of our pages won't be affected by
5256 * clear_extent_buffer_uptodate().
5257 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005258 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005259 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005260 if (!PageUptodate(page)) {
5261 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005262 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005263 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005264 }
Liu Bo2571e732016-08-03 12:33:01 -07005265
Chris Masonce9adaa2008-04-09 16:28:12 -04005266 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005267 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005268 goto unlock_exit;
5269 }
5270
Filipe Manana656f30d2014-09-26 12:25:56 +01005271 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005272 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005273 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005274 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005275 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005276
Chris Masonce9adaa2008-04-09 16:28:12 -04005277 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005278 if (ret) {
5279 atomic_dec(&eb->io_pages);
5280 unlock_page(page);
5281 continue;
5282 }
5283
Chris Masonf1885912008-04-09 16:28:12 -04005284 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005285 err = __extent_read_full_page(tree, page,
David Sterba6af49db2017-06-23 04:09:57 +02005286 btree_get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005287 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005288 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005289 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005290 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005291 /*
5292 * We use &bio in above __extent_read_full_page,
5293 * so we ensure that if it returns error, the
5294 * current page fails to add itself to bio and
5295 * it's been unlocked.
5296 *
5297 * We must dec io_pages by ourselves.
5298 */
5299 atomic_dec(&eb->io_pages);
5300 }
Chris Masond1310b22008-01-24 16:13:08 -05005301 } else {
5302 unlock_page(page);
5303 }
5304 }
5305
Jeff Mahoney355808c2011-10-03 23:23:14 -04005306 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005307 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005308 if (err)
5309 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005310 }
Chris Masona86c12c2008-02-07 10:50:54 -05005311
Arne Jansenbb82ab82011-06-10 14:06:53 +02005312 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005313 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005314
Josef Bacik8436ea912016-09-02 15:40:03 -04005315 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005316 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005317 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005318 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005319 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005320 }
Chris Masond3977122009-01-05 21:25:51 -05005321
Chris Masond1310b22008-01-24 16:13:08 -05005322 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005323
5324unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005325 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005326 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005327 page = eb->pages[locked_pages];
5328 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005329 }
5330 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005331}
Chris Masond1310b22008-01-24 16:13:08 -05005332
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005333void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5334 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005335{
5336 size_t cur;
5337 size_t offset;
5338 struct page *page;
5339 char *kaddr;
5340 char *dst = (char *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005341 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005342 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005343
Liu Bof716abd2017-08-09 11:10:16 -06005344 if (start + len > eb->len) {
5345 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5346 eb->start, eb->len, start, len);
5347 memset(dst, 0, len);
5348 return;
5349 }
Chris Masond1310b22008-01-24 16:13:08 -05005350
Johannes Thumshirn70730172018-12-05 15:23:03 +01005351 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005352
Chris Masond3977122009-01-05 21:25:51 -05005353 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005354 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005355
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005356 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005357 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005358 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005359
5360 dst += cur;
5361 len -= cur;
5362 offset = 0;
5363 i++;
5364 }
5365}
Chris Masond1310b22008-01-24 16:13:08 -05005366
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005367int read_extent_buffer_to_user(const struct extent_buffer *eb,
5368 void __user *dstv,
5369 unsigned long start, unsigned long len)
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005370{
5371 size_t cur;
5372 size_t offset;
5373 struct page *page;
5374 char *kaddr;
5375 char __user *dst = (char __user *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005376 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005377 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005378 int ret = 0;
5379
5380 WARN_ON(start > eb->len);
5381 WARN_ON(start + len > eb->start + eb->len);
5382
Johannes Thumshirn70730172018-12-05 15:23:03 +01005383 offset = offset_in_page(start_offset + start);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005384
5385 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005386 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005387
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005388 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005389 kaddr = page_address(page);
5390 if (copy_to_user(dst, kaddr + offset, cur)) {
5391 ret = -EFAULT;
5392 break;
5393 }
5394
5395 dst += cur;
5396 len -= cur;
5397 offset = 0;
5398 i++;
5399 }
5400
5401 return ret;
5402}
5403
Liu Bo415b35a2016-06-17 19:16:21 -07005404/*
5405 * return 0 if the item is found within a page.
5406 * return 1 if the item spans two pages.
5407 * return -EINVAL otherwise.
5408 */
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005409int map_private_extent_buffer(const struct extent_buffer *eb,
5410 unsigned long start, unsigned long min_len,
5411 char **map, unsigned long *map_start,
5412 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005413{
Johannes Thumshirncc2c39d2018-11-28 09:54:54 +01005414 size_t offset;
Chris Masond1310b22008-01-24 16:13:08 -05005415 char *kaddr;
5416 struct page *p;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005417 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005418 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005419 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005420 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005421
Liu Bof716abd2017-08-09 11:10:16 -06005422 if (start + min_len > eb->len) {
5423 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5424 eb->start, eb->len, start, min_len);
5425 return -EINVAL;
5426 }
5427
Chris Masond1310b22008-01-24 16:13:08 -05005428 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005429 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005430
5431 if (i == 0) {
5432 offset = start_offset;
5433 *map_start = 0;
5434 } else {
5435 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005436 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005437 }
Chris Masond3977122009-01-05 21:25:51 -05005438
David Sterbafb85fc92014-07-31 01:03:53 +02005439 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005440 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005441 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005442 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005443 return 0;
5444}
Chris Masond1310b22008-01-24 16:13:08 -05005445
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005446int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5447 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005448{
5449 size_t cur;
5450 size_t offset;
5451 struct page *page;
5452 char *kaddr;
5453 char *ptr = (char *)ptrv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005454 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005455 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005456 int ret = 0;
5457
5458 WARN_ON(start > eb->len);
5459 WARN_ON(start + len > eb->start + eb->len);
5460
Johannes Thumshirn70730172018-12-05 15:23:03 +01005461 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005462
Chris Masond3977122009-01-05 21:25:51 -05005463 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005464 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005465
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005466 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005467
Chris Masona6591712011-07-19 12:04:14 -04005468 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005469 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005470 if (ret)
5471 break;
5472
5473 ptr += cur;
5474 len -= cur;
5475 offset = 0;
5476 i++;
5477 }
5478 return ret;
5479}
Chris Masond1310b22008-01-24 16:13:08 -05005480
David Sterbaf157bf72016-11-09 17:43:38 +01005481void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5482 const void *srcv)
5483{
5484 char *kaddr;
5485
5486 WARN_ON(!PageUptodate(eb->pages[0]));
5487 kaddr = page_address(eb->pages[0]);
5488 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5489 BTRFS_FSID_SIZE);
5490}
5491
5492void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5493{
5494 char *kaddr;
5495
5496 WARN_ON(!PageUptodate(eb->pages[0]));
5497 kaddr = page_address(eb->pages[0]);
5498 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5499 BTRFS_FSID_SIZE);
5500}
5501
Chris Masond1310b22008-01-24 16:13:08 -05005502void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5503 unsigned long start, unsigned long len)
5504{
5505 size_t cur;
5506 size_t offset;
5507 struct page *page;
5508 char *kaddr;
5509 char *src = (char *)srcv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005510 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005511 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005512
5513 WARN_ON(start > eb->len);
5514 WARN_ON(start + len > eb->start + eb->len);
5515
Johannes Thumshirn70730172018-12-05 15:23:03 +01005516 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005517
Chris Masond3977122009-01-05 21:25:51 -05005518 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005519 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005520 WARN_ON(!PageUptodate(page));
5521
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005522 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005523 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005524 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005525
5526 src += cur;
5527 len -= cur;
5528 offset = 0;
5529 i++;
5530 }
5531}
Chris Masond1310b22008-01-24 16:13:08 -05005532
David Sterbab159fa22016-11-08 18:09:03 +01005533void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5534 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005535{
5536 size_t cur;
5537 size_t offset;
5538 struct page *page;
5539 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005540 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005541 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005542
5543 WARN_ON(start > eb->len);
5544 WARN_ON(start + len > eb->start + eb->len);
5545
Johannes Thumshirn70730172018-12-05 15:23:03 +01005546 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005547
Chris Masond3977122009-01-05 21:25:51 -05005548 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005549 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005550 WARN_ON(!PageUptodate(page));
5551
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005552 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005553 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005554 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005555
5556 len -= cur;
5557 offset = 0;
5558 i++;
5559 }
5560}
Chris Masond1310b22008-01-24 16:13:08 -05005561
David Sterba58e80122016-11-08 18:30:31 +01005562void copy_extent_buffer_full(struct extent_buffer *dst,
5563 struct extent_buffer *src)
5564{
5565 int i;
David Sterbacc5e31a2018-03-01 18:20:27 +01005566 int num_pages;
David Sterba58e80122016-11-08 18:30:31 +01005567
5568 ASSERT(dst->len == src->len);
5569
David Sterba65ad0102018-06-29 10:56:49 +02005570 num_pages = num_extent_pages(dst);
David Sterba58e80122016-11-08 18:30:31 +01005571 for (i = 0; i < num_pages; i++)
5572 copy_page(page_address(dst->pages[i]),
5573 page_address(src->pages[i]));
5574}
5575
Chris Masond1310b22008-01-24 16:13:08 -05005576void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5577 unsigned long dst_offset, unsigned long src_offset,
5578 unsigned long len)
5579{
5580 u64 dst_len = dst->len;
5581 size_t cur;
5582 size_t offset;
5583 struct page *page;
5584 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005585 size_t start_offset = offset_in_page(dst->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005586 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005587
5588 WARN_ON(src->len != dst_len);
5589
Johannes Thumshirn70730172018-12-05 15:23:03 +01005590 offset = offset_in_page(start_offset + dst_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005591
Chris Masond3977122009-01-05 21:25:51 -05005592 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005593 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005594 WARN_ON(!PageUptodate(page));
5595
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005596 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005597
Chris Masona6591712011-07-19 12:04:14 -04005598 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005599 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005600
5601 src_offset += cur;
5602 len -= cur;
5603 offset = 0;
5604 i++;
5605 }
5606}
Chris Masond1310b22008-01-24 16:13:08 -05005607
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005608/*
5609 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5610 * given bit number
5611 * @eb: the extent buffer
5612 * @start: offset of the bitmap item in the extent buffer
5613 * @nr: bit number
5614 * @page_index: return index of the page in the extent buffer that contains the
5615 * given bit number
5616 * @page_offset: return offset into the page given by page_index
5617 *
5618 * This helper hides the ugliness of finding the byte in an extent buffer which
5619 * contains a given bit.
5620 */
5621static inline void eb_bitmap_offset(struct extent_buffer *eb,
5622 unsigned long start, unsigned long nr,
5623 unsigned long *page_index,
5624 size_t *page_offset)
5625{
Johannes Thumshirn70730172018-12-05 15:23:03 +01005626 size_t start_offset = offset_in_page(eb->start);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005627 size_t byte_offset = BIT_BYTE(nr);
5628 size_t offset;
5629
5630 /*
5631 * The byte we want is the offset of the extent buffer + the offset of
5632 * the bitmap item in the extent buffer + the offset of the byte in the
5633 * bitmap item.
5634 */
5635 offset = start_offset + start + byte_offset;
5636
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005637 *page_index = offset >> PAGE_SHIFT;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005638 *page_offset = offset_in_page(offset);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005639}
5640
5641/**
5642 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5643 * @eb: the extent buffer
5644 * @start: offset of the bitmap item in the extent buffer
5645 * @nr: bit number to test
5646 */
5647int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5648 unsigned long nr)
5649{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005650 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005651 struct page *page;
5652 unsigned long i;
5653 size_t offset;
5654
5655 eb_bitmap_offset(eb, start, nr, &i, &offset);
5656 page = eb->pages[i];
5657 WARN_ON(!PageUptodate(page));
5658 kaddr = page_address(page);
5659 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5660}
5661
5662/**
5663 * extent_buffer_bitmap_set - set an area of a bitmap
5664 * @eb: the extent buffer
5665 * @start: offset of the bitmap item in the extent buffer
5666 * @pos: bit number of the first bit
5667 * @len: number of bits to set
5668 */
5669void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5670 unsigned long pos, unsigned long len)
5671{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005672 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005673 struct page *page;
5674 unsigned long i;
5675 size_t offset;
5676 const unsigned int size = pos + len;
5677 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005678 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005679
5680 eb_bitmap_offset(eb, start, pos, &i, &offset);
5681 page = eb->pages[i];
5682 WARN_ON(!PageUptodate(page));
5683 kaddr = page_address(page);
5684
5685 while (len >= bits_to_set) {
5686 kaddr[offset] |= mask_to_set;
5687 len -= bits_to_set;
5688 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005689 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005690 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005691 offset = 0;
5692 page = eb->pages[++i];
5693 WARN_ON(!PageUptodate(page));
5694 kaddr = page_address(page);
5695 }
5696 }
5697 if (len) {
5698 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5699 kaddr[offset] |= mask_to_set;
5700 }
5701}
5702
5703
5704/**
5705 * extent_buffer_bitmap_clear - clear an area of a bitmap
5706 * @eb: the extent buffer
5707 * @start: offset of the bitmap item in the extent buffer
5708 * @pos: bit number of the first bit
5709 * @len: number of bits to clear
5710 */
5711void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5712 unsigned long pos, unsigned long len)
5713{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005714 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005715 struct page *page;
5716 unsigned long i;
5717 size_t offset;
5718 const unsigned int size = pos + len;
5719 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005720 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005721
5722 eb_bitmap_offset(eb, start, pos, &i, &offset);
5723 page = eb->pages[i];
5724 WARN_ON(!PageUptodate(page));
5725 kaddr = page_address(page);
5726
5727 while (len >= bits_to_clear) {
5728 kaddr[offset] &= ~mask_to_clear;
5729 len -= bits_to_clear;
5730 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005731 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005732 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005733 offset = 0;
5734 page = eb->pages[++i];
5735 WARN_ON(!PageUptodate(page));
5736 kaddr = page_address(page);
5737 }
5738 }
5739 if (len) {
5740 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5741 kaddr[offset] &= ~mask_to_clear;
5742 }
5743}
5744
Sergei Trofimovich33872062011-04-11 21:52:52 +00005745static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5746{
5747 unsigned long distance = (src > dst) ? src - dst : dst - src;
5748 return distance < len;
5749}
5750
Chris Masond1310b22008-01-24 16:13:08 -05005751static void copy_pages(struct page *dst_page, struct page *src_page,
5752 unsigned long dst_off, unsigned long src_off,
5753 unsigned long len)
5754{
Chris Masona6591712011-07-19 12:04:14 -04005755 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005756 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005757 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005758
Sergei Trofimovich33872062011-04-11 21:52:52 +00005759 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005760 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005761 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005762 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005763 if (areas_overlap(src_off, dst_off, len))
5764 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005765 }
Chris Masond1310b22008-01-24 16:13:08 -05005766
Chris Mason727011e2010-08-06 13:21:20 -04005767 if (must_memmove)
5768 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5769 else
5770 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005771}
5772
5773void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5774 unsigned long src_offset, unsigned long len)
5775{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005776 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005777 size_t cur;
5778 size_t dst_off_in_page;
5779 size_t src_off_in_page;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005780 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005781 unsigned long dst_i;
5782 unsigned long src_i;
5783
5784 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005785 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005786 "memmove bogus src_offset %lu move len %lu dst len %lu",
5787 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005788 BUG_ON(1);
5789 }
5790 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005791 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005792 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5793 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005794 BUG_ON(1);
5795 }
5796
Chris Masond3977122009-01-05 21:25:51 -05005797 while (len > 0) {
Johannes Thumshirn70730172018-12-05 15:23:03 +01005798 dst_off_in_page = offset_in_page(start_offset + dst_offset);
5799 src_off_in_page = offset_in_page(start_offset + src_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005800
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005801 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5802 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005803
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005804 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005805 src_off_in_page));
5806 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005807 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005808
David Sterbafb85fc92014-07-31 01:03:53 +02005809 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005810 dst_off_in_page, src_off_in_page, cur);
5811
5812 src_offset += cur;
5813 dst_offset += cur;
5814 len -= cur;
5815 }
5816}
Chris Masond1310b22008-01-24 16:13:08 -05005817
5818void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5819 unsigned long src_offset, unsigned long len)
5820{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005821 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005822 size_t cur;
5823 size_t dst_off_in_page;
5824 size_t src_off_in_page;
5825 unsigned long dst_end = dst_offset + len - 1;
5826 unsigned long src_end = src_offset + len - 1;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005827 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005828 unsigned long dst_i;
5829 unsigned long src_i;
5830
5831 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005832 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005833 "memmove bogus src_offset %lu move len %lu len %lu",
5834 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005835 BUG_ON(1);
5836 }
5837 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005838 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005839 "memmove bogus dst_offset %lu move len %lu len %lu",
5840 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005841 BUG_ON(1);
5842 }
Chris Mason727011e2010-08-06 13:21:20 -04005843 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005844 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5845 return;
5846 }
Chris Masond3977122009-01-05 21:25:51 -05005847 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005848 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
5849 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005850
Johannes Thumshirn70730172018-12-05 15:23:03 +01005851 dst_off_in_page = offset_in_page(start_offset + dst_end);
5852 src_off_in_page = offset_in_page(start_offset + src_end);
Chris Masond1310b22008-01-24 16:13:08 -05005853
5854 cur = min_t(unsigned long, len, src_off_in_page + 1);
5855 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005856 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005857 dst_off_in_page - cur + 1,
5858 src_off_in_page - cur + 1, cur);
5859
5860 dst_end -= cur;
5861 src_end -= cur;
5862 len -= cur;
5863 }
5864}
Chris Mason6af118ce2008-07-22 11:18:07 -04005865
David Sterbaf7a52a42013-04-26 14:56:29 +00005866int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005867{
Chris Mason6af118ce2008-07-22 11:18:07 -04005868 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005869
Miao Xie19fe0a82010-10-26 20:57:29 -04005870 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005871 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05005872 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005873 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005874 spin_lock(&page->mapping->private_lock);
5875 if (!PagePrivate(page)) {
5876 spin_unlock(&page->mapping->private_lock);
5877 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005878 }
5879
Josef Bacik3083ee22012-03-09 16:01:49 -05005880 eb = (struct extent_buffer *)page->private;
5881 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005882
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005883 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005884 * This is a little awful but should be ok, we need to make sure that
5885 * the eb doesn't disappear out from under us while we're looking at
5886 * this page.
5887 */
5888 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005889 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005890 spin_unlock(&eb->refs_lock);
5891 spin_unlock(&page->mapping->private_lock);
5892 return 0;
5893 }
5894 spin_unlock(&page->mapping->private_lock);
5895
Josef Bacik3083ee22012-03-09 16:01:49 -05005896 /*
5897 * If tree ref isn't set then we know the ref on this eb is a real ref,
5898 * so just return, this page will likely be freed soon anyway.
5899 */
5900 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5901 spin_unlock(&eb->refs_lock);
5902 return 0;
5903 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005904
David Sterbaf7a52a42013-04-26 14:56:29 +00005905 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005906}