blob: 33d761ff5bd3894eb00201a5092b51a4db2cf4b2 [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{
Josef Bacikc6100a42017-05-05 11:57:13 -040092 if (tree->ops && tree->ops->check_extent_io_range)
93 tree->ops->check_extent_io_range(tree->private_data, caller,
94 start, end);
David Sterba8d599ae2013-04-30 15:22:23 +000095}
Eric Sandeen6d49ba12013-04-22 16:12:31 +000096#else
97#define btrfs_leak_debug_add(new, head) do {} while (0)
98#define btrfs_leak_debug_del(entry) do {} while (0)
99#define btrfs_leak_debug_check() do {} while (0)
David Sterba8d599ae2013-04-30 15:22:23 +0000100#define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
Chris Mason4bef0842008-09-08 11:18:08 -0400101#endif
Chris Masond1310b22008-01-24 16:13:08 -0500102
Chris Masond1310b22008-01-24 16:13:08 -0500103#define BUFFER_LRU_MAX 64
104
105struct tree_entry {
106 u64 start;
107 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -0500108 struct rb_node rb_node;
109};
110
111struct extent_page_data {
112 struct bio *bio;
113 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -0500114 /* tells writepage not to lock the state bits for this range
115 * it still does the unlocking
116 */
Chris Masonffbd5172009-04-20 15:50:09 -0400117 unsigned int extent_locked:1;
118
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600119 /* tells the submit_bio code to use REQ_SYNC */
Chris Masonffbd5172009-04-20 15:50:09 -0400120 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -0500121};
122
David Sterba57599c72018-03-01 17:56:34 +0100123static int add_extent_changeset(struct extent_state *state, unsigned bits,
Qu Wenruod38ed272015-10-12 14:53:37 +0800124 struct extent_changeset *changeset,
125 int set)
126{
127 int ret;
128
129 if (!changeset)
David Sterba57599c72018-03-01 17:56:34 +0100130 return 0;
Qu Wenruod38ed272015-10-12 14:53:37 +0800131 if (set && (state->state & bits) == bits)
David Sterba57599c72018-03-01 17:56:34 +0100132 return 0;
Qu Wenruofefdc552015-10-12 15:35:38 +0800133 if (!set && (state->state & bits) == 0)
David Sterba57599c72018-03-01 17:56:34 +0100134 return 0;
Qu Wenruod38ed272015-10-12 14:53:37 +0800135 changeset->bytes_changed += state->end - state->start + 1;
David Sterba53d32352017-02-13 13:42:29 +0100136 ret = ulist_add(&changeset->range_changed, state->start, state->end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800137 GFP_ATOMIC);
David Sterba57599c72018-03-01 17:56:34 +0100138 return ret;
Qu Wenruod38ed272015-10-12 14:53:37 +0800139}
140
David Sterbaaab6e9e2017-11-30 18:00:02 +0100141static void flush_write_bio(struct extent_page_data *epd);
David Sterbae2932ee2017-06-23 04:16:17 +0200142
Chris Masond1310b22008-01-24 16:13:08 -0500143int __init extent_io_init(void)
144{
David Sterba837e1972012-09-07 03:00:48 -0600145 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200146 sizeof(struct extent_state), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300147 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500148 if (!extent_state_cache)
149 return -ENOMEM;
150
David Sterba837e1972012-09-07 03:00:48 -0600151 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200152 sizeof(struct extent_buffer), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300153 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500154 if (!extent_buffer_cache)
155 goto free_state_cache;
Chris Mason9be33952013-05-17 18:30:14 -0400156
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400157 if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
158 offsetof(struct btrfs_io_bio, bio),
159 BIOSET_NEED_BVECS))
Chris Mason9be33952013-05-17 18:30:14 -0400160 goto free_buffer_cache;
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700161
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400162 if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE))
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700163 goto free_bioset;
164
Chris Masond1310b22008-01-24 16:13:08 -0500165 return 0;
166
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700167free_bioset:
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400168 bioset_exit(&btrfs_bioset);
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700169
Chris Mason9be33952013-05-17 18:30:14 -0400170free_buffer_cache:
171 kmem_cache_destroy(extent_buffer_cache);
172 extent_buffer_cache = NULL;
173
Chris Masond1310b22008-01-24 16:13:08 -0500174free_state_cache:
175 kmem_cache_destroy(extent_state_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400176 extent_state_cache = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500177 return -ENOMEM;
178}
179
David Sterbae67c7182018-02-19 17:24:18 +0100180void __cold extent_io_exit(void)
Chris Masond1310b22008-01-24 16:13:08 -0500181{
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000182 btrfs_leak_debug_check();
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000183
184 /*
185 * Make sure all delayed rcu free are flushed before we
186 * destroy caches.
187 */
188 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +0800189 kmem_cache_destroy(extent_state_cache);
190 kmem_cache_destroy(extent_buffer_cache);
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400191 bioset_exit(&btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -0500192}
193
194void extent_io_tree_init(struct extent_io_tree *tree,
Josef Bacikc6100a42017-05-05 11:57:13 -0400195 void *private_data)
Chris Masond1310b22008-01-24 16:13:08 -0500196{
Eric Paris6bef4d32010-02-23 19:43:04 +0000197 tree->state = RB_ROOT;
Chris Masond1310b22008-01-24 16:13:08 -0500198 tree->ops = NULL;
199 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500200 spin_lock_init(&tree->lock);
Josef Bacikc6100a42017-05-05 11:57:13 -0400201 tree->private_data = private_data;
Chris Masond1310b22008-01-24 16:13:08 -0500202}
Chris Masond1310b22008-01-24 16:13:08 -0500203
Christoph Hellwigb2950862008-12-02 09:54:17 -0500204static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500205{
206 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500207
Michal Hocko3ba7ab22017-01-09 15:39:02 +0100208 /*
209 * The given mask might be not appropriate for the slab allocator,
210 * drop the unsupported bits
211 */
212 mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
Chris Masond1310b22008-01-24 16:13:08 -0500213 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400214 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500215 return state;
216 state->state = 0;
David Sterba47dc1962016-02-11 13:24:13 +0100217 state->failrec = NULL;
Filipe Manana27a35072014-07-06 20:09:59 +0100218 RB_CLEAR_NODE(&state->rb_node);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000219 btrfs_leak_debug_add(&state->leak_list, &states);
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200220 refcount_set(&state->refs, 1);
Chris Masond1310b22008-01-24 16:13:08 -0500221 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100222 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500223 return state;
224}
Chris Masond1310b22008-01-24 16:13:08 -0500225
Chris Mason4845e442010-05-25 20:56:50 -0400226void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500227{
Chris Masond1310b22008-01-24 16:13:08 -0500228 if (!state)
229 return;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200230 if (refcount_dec_and_test(&state->refs)) {
Filipe Manana27a35072014-07-06 20:09:59 +0100231 WARN_ON(extent_state_in_tree(state));
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000232 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100233 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500234 kmem_cache_free(extent_state_cache, state);
235 }
236}
Chris Masond1310b22008-01-24 16:13:08 -0500237
Filipe Mananaf2071b22014-02-12 15:05:53 +0000238static struct rb_node *tree_insert(struct rb_root *root,
239 struct rb_node *search_start,
240 u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000241 struct rb_node *node,
242 struct rb_node ***p_in,
243 struct rb_node **parent_in)
Chris Masond1310b22008-01-24 16:13:08 -0500244{
Filipe Mananaf2071b22014-02-12 15:05:53 +0000245 struct rb_node **p;
Chris Masond3977122009-01-05 21:25:51 -0500246 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500247 struct tree_entry *entry;
248
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000249 if (p_in && parent_in) {
250 p = *p_in;
251 parent = *parent_in;
252 goto do_insert;
253 }
254
Filipe Mananaf2071b22014-02-12 15:05:53 +0000255 p = search_start ? &search_start : &root->rb_node;
Chris Masond3977122009-01-05 21:25:51 -0500256 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500257 parent = *p;
258 entry = rb_entry(parent, struct tree_entry, rb_node);
259
260 if (offset < entry->start)
261 p = &(*p)->rb_left;
262 else if (offset > entry->end)
263 p = &(*p)->rb_right;
264 else
265 return parent;
266 }
267
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000268do_insert:
Chris Masond1310b22008-01-24 16:13:08 -0500269 rb_link_node(node, parent, p);
270 rb_insert_color(node, root);
271 return NULL;
272}
273
Chris Mason80ea96b2008-02-01 14:51:59 -0500274static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000275 struct rb_node **prev_ret,
276 struct rb_node **next_ret,
277 struct rb_node ***p_ret,
278 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500279{
Chris Mason80ea96b2008-02-01 14:51:59 -0500280 struct rb_root *root = &tree->state;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000281 struct rb_node **n = &root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500282 struct rb_node *prev = NULL;
283 struct rb_node *orig_prev = NULL;
284 struct tree_entry *entry;
285 struct tree_entry *prev_entry = NULL;
286
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000287 while (*n) {
288 prev = *n;
289 entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500290 prev_entry = entry;
291
292 if (offset < entry->start)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000293 n = &(*n)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500294 else if (offset > entry->end)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000295 n = &(*n)->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500296 else
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000297 return *n;
Chris Masond1310b22008-01-24 16:13:08 -0500298 }
299
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000300 if (p_ret)
301 *p_ret = n;
302 if (parent_ret)
303 *parent_ret = prev;
304
Chris Masond1310b22008-01-24 16:13:08 -0500305 if (prev_ret) {
306 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500307 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500308 prev = rb_next(prev);
309 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
310 }
311 *prev_ret = prev;
312 prev = orig_prev;
313 }
314
315 if (next_ret) {
316 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500317 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500318 prev = rb_prev(prev);
319 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
320 }
321 *next_ret = prev;
322 }
323 return NULL;
324}
325
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000326static inline struct rb_node *
327tree_search_for_insert(struct extent_io_tree *tree,
328 u64 offset,
329 struct rb_node ***p_ret,
330 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500331{
Chris Mason70dec802008-01-29 09:59:12 -0500332 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500333 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500334
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000335 ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret);
Chris Masond3977122009-01-05 21:25:51 -0500336 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500337 return prev;
338 return ret;
339}
340
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000341static inline struct rb_node *tree_search(struct extent_io_tree *tree,
342 u64 offset)
343{
344 return tree_search_for_insert(tree, offset, NULL, NULL);
345}
346
Josef Bacik9ed74f22009-09-11 16:12:44 -0400347static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
348 struct extent_state *other)
349{
350 if (tree->ops && tree->ops->merge_extent_hook)
Josef Bacikc6100a42017-05-05 11:57:13 -0400351 tree->ops->merge_extent_hook(tree->private_data, new, other);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400352}
353
Chris Masond1310b22008-01-24 16:13:08 -0500354/*
355 * utility function to look for merge candidates inside a given range.
356 * Any extents with matching state are merged together into a single
357 * extent in the tree. Extents with EXTENT_IO in their state field
358 * are not merged because the end_io handlers need to be able to do
359 * operations on them without sleeping (or doing allocations/splits).
360 *
361 * This should be called with the tree lock held.
362 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000363static void merge_state(struct extent_io_tree *tree,
364 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500365{
366 struct extent_state *other;
367 struct rb_node *other_node;
368
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400369 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000370 return;
Chris Masond1310b22008-01-24 16:13:08 -0500371
372 other_node = rb_prev(&state->rb_node);
373 if (other_node) {
374 other = rb_entry(other_node, struct extent_state, rb_node);
375 if (other->end == state->start - 1 &&
376 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400377 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500378 state->start = other->start;
Chris Masond1310b22008-01-24 16:13:08 -0500379 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100380 RB_CLEAR_NODE(&other->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500381 free_extent_state(other);
382 }
383 }
384 other_node = rb_next(&state->rb_node);
385 if (other_node) {
386 other = rb_entry(other_node, struct extent_state, rb_node);
387 if (other->start == state->end + 1 &&
388 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400389 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400390 state->end = other->end;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400391 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100392 RB_CLEAR_NODE(&other->rb_node);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400393 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500394 }
395 }
Chris Masond1310b22008-01-24 16:13:08 -0500396}
397
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000398static void set_state_cb(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +0100399 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500400{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000401 if (tree->ops && tree->ops->set_bit_hook)
Josef Bacikc6100a42017-05-05 11:57:13 -0400402 tree->ops->set_bit_hook(tree->private_data, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500403}
404
405static void clear_state_cb(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +0100406 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500407{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400408 if (tree->ops && tree->ops->clear_bit_hook)
Josef Bacikc6100a42017-05-05 11:57:13 -0400409 tree->ops->clear_bit_hook(tree->private_data, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500410}
411
Xiao Guangrong3150b692011-07-14 03:19:08 +0000412static void set_state_bits(struct extent_io_tree *tree,
Qu Wenruod38ed272015-10-12 14:53:37 +0800413 struct extent_state *state, unsigned *bits,
414 struct extent_changeset *changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000415
Chris Masond1310b22008-01-24 16:13:08 -0500416/*
417 * insert an extent_state struct into the tree. 'bits' are set on the
418 * struct before it is inserted.
419 *
420 * This may return -EEXIST if the extent is already there, in which case the
421 * state struct is freed.
422 *
423 * The tree lock is not taken internally. This is a utility function and
424 * probably isn't what you want to call (see set/clear_extent_bit).
425 */
426static int insert_state(struct extent_io_tree *tree,
427 struct extent_state *state, u64 start, u64 end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000428 struct rb_node ***p,
429 struct rb_node **parent,
Qu Wenruod38ed272015-10-12 14:53:37 +0800430 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500431{
432 struct rb_node *node;
433
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000434 if (end < start)
Frank Holtonefe120a2013-12-20 11:37:06 -0500435 WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200436 end, start);
Chris Masond1310b22008-01-24 16:13:08 -0500437 state->start = start;
438 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400439
Qu Wenruod38ed272015-10-12 14:53:37 +0800440 set_state_bits(tree, state, bits, changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000441
Filipe Mananaf2071b22014-02-12 15:05:53 +0000442 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
Chris Masond1310b22008-01-24 16:13:08 -0500443 if (node) {
444 struct extent_state *found;
445 found = rb_entry(node, struct extent_state, rb_node);
Jeff Mahoney62e85572016-09-20 10:05:01 -0400446 pr_err("BTRFS: found node %llu %llu on insert of %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200447 found->start, found->end, start, end);
Chris Masond1310b22008-01-24 16:13:08 -0500448 return -EEXIST;
449 }
450 merge_state(tree, state);
451 return 0;
452}
453
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000454static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400455 u64 split)
456{
457 if (tree->ops && tree->ops->split_extent_hook)
Josef Bacikc6100a42017-05-05 11:57:13 -0400458 tree->ops->split_extent_hook(tree->private_data, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400459}
460
Chris Masond1310b22008-01-24 16:13:08 -0500461/*
462 * split a given extent state struct in two, inserting the preallocated
463 * struct 'prealloc' as the newly created second half. 'split' indicates an
464 * offset inside 'orig' where it should be split.
465 *
466 * Before calling,
467 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
468 * are two extent state structs in the tree:
469 * prealloc: [orig->start, split - 1]
470 * orig: [ split, orig->end ]
471 *
472 * The tree locks are not taken by this function. They need to be held
473 * by the caller.
474 */
475static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
476 struct extent_state *prealloc, u64 split)
477{
478 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400479
480 split_cb(tree, orig, split);
481
Chris Masond1310b22008-01-24 16:13:08 -0500482 prealloc->start = orig->start;
483 prealloc->end = split - 1;
484 prealloc->state = orig->state;
485 orig->start = split;
486
Filipe Mananaf2071b22014-02-12 15:05:53 +0000487 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
488 &prealloc->rb_node, NULL, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500489 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500490 free_extent_state(prealloc);
491 return -EEXIST;
492 }
493 return 0;
494}
495
Li Zefancdc6a392012-03-12 16:39:48 +0800496static struct extent_state *next_state(struct extent_state *state)
497{
498 struct rb_node *next = rb_next(&state->rb_node);
499 if (next)
500 return rb_entry(next, struct extent_state, rb_node);
501 else
502 return NULL;
503}
504
Chris Masond1310b22008-01-24 16:13:08 -0500505/*
506 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800507 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500508 *
509 * If no bits are set on the state struct after clearing things, the
510 * struct is freed and removed from the tree
511 */
Li Zefancdc6a392012-03-12 16:39:48 +0800512static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
513 struct extent_state *state,
Qu Wenruofefdc552015-10-12 15:35:38 +0800514 unsigned *bits, int wake,
515 struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500516{
Li Zefancdc6a392012-03-12 16:39:48 +0800517 struct extent_state *next;
David Sterba9ee49a042015-01-14 19:52:13 +0100518 unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100519 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500520
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400521 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500522 u64 range = state->end - state->start + 1;
523 WARN_ON(range > tree->dirty_bytes);
524 tree->dirty_bytes -= range;
525 }
Chris Mason291d6732008-01-29 15:55:23 -0500526 clear_state_cb(tree, state, bits);
David Sterba57599c72018-03-01 17:56:34 +0100527 ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
528 BUG_ON(ret < 0);
Josef Bacik32c00af2009-10-08 13:34:05 -0400529 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500530 if (wake)
531 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400532 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800533 next = next_state(state);
Filipe Manana27a35072014-07-06 20:09:59 +0100534 if (extent_state_in_tree(state)) {
Chris Masond1310b22008-01-24 16:13:08 -0500535 rb_erase(&state->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100536 RB_CLEAR_NODE(&state->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500537 free_extent_state(state);
538 } else {
539 WARN_ON(1);
540 }
541 } else {
542 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800543 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500544 }
Li Zefancdc6a392012-03-12 16:39:48 +0800545 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500546}
547
Xiao Guangrong82337672011-04-20 06:44:57 +0000548static struct extent_state *
549alloc_extent_state_atomic(struct extent_state *prealloc)
550{
551 if (!prealloc)
552 prealloc = alloc_extent_state(GFP_ATOMIC);
553
554 return prealloc;
555}
556
Eric Sandeen48a3b632013-04-25 20:41:01 +0000557static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400558{
David Sterba05912a32018-07-18 19:23:45 +0200559 struct inode *inode = tree->private_data;
560
561 btrfs_panic(btrfs_sb(inode->i_sb), err,
562 "locking error: extent tree was modified by another thread while locked");
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400563}
564
Chris Masond1310b22008-01-24 16:13:08 -0500565/*
566 * clear some bits on a range in the tree. This may require splitting
567 * or inserting elements in the tree, so the gfp mask is used to
568 * indicate which allocations or sleeping are allowed.
569 *
570 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
571 * the given range from the tree regardless of state (ie for truncate).
572 *
573 * the range [start, end] is inclusive.
574 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100575 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500576 */
David Sterba66b0c882017-10-31 16:30:47 +0100577int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Qu Wenruofefdc552015-10-12 15:35:38 +0800578 unsigned bits, int wake, int delete,
579 struct extent_state **cached_state,
580 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500581{
582 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400583 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500584 struct extent_state *prealloc = NULL;
585 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400586 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500587 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000588 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500589
Josef Bacika5dee372013-12-13 10:02:44 -0500590 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000591
Josef Bacik7ee9e442013-06-21 16:37:03 -0400592 if (bits & EXTENT_DELALLOC)
593 bits |= EXTENT_NORESERVE;
594
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400595 if (delete)
596 bits |= ~EXTENT_CTLBITS;
597 bits |= EXTENT_FIRST_DELALLOC;
598
Josef Bacik2ac55d42010-02-03 19:33:23 +0000599 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
600 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500601again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800602 if (!prealloc && gfpflags_allow_blocking(mask)) {
Filipe Mananac7bc6312014-11-03 14:12:57 +0000603 /*
604 * Don't care for allocation failure here because we might end
605 * up not needing the pre-allocated extent state at all, which
606 * is the case if we only have in the tree extent states that
607 * cover our input range and don't cover too any other range.
608 * If we end up needing a new extent state we allocate it later.
609 */
Chris Masond1310b22008-01-24 16:13:08 -0500610 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500611 }
612
Chris Masoncad321a2008-12-17 14:51:42 -0500613 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400614 if (cached_state) {
615 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000616
617 if (clear) {
618 *cached_state = NULL;
619 cached_state = NULL;
620 }
621
Filipe Manana27a35072014-07-06 20:09:59 +0100622 if (cached && extent_state_in_tree(cached) &&
623 cached->start <= start && cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000624 if (clear)
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200625 refcount_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400626 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400627 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400628 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000629 if (clear)
630 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400631 }
Chris Masond1310b22008-01-24 16:13:08 -0500632 /*
633 * this search will find the extents that end after
634 * our range starts
635 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500636 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500637 if (!node)
638 goto out;
639 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400640hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500641 if (state->start > end)
642 goto out;
643 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400644 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500645
Liu Bo04493142012-02-16 18:34:37 +0800646 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800647 if (!(state->state & bits)) {
648 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800649 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800650 }
Liu Bo04493142012-02-16 18:34:37 +0800651
Chris Masond1310b22008-01-24 16:13:08 -0500652 /*
653 * | ---- desired range ---- |
654 * | state | or
655 * | ------------- state -------------- |
656 *
657 * We need to split the extent we found, and may flip
658 * bits on second half.
659 *
660 * If the extent we found extends past our range, we
661 * just split and search again. It'll get split again
662 * the next time though.
663 *
664 * If the extent we found is inside our range, we clear
665 * the desired bit on it.
666 */
667
668 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000669 prealloc = alloc_extent_state_atomic(prealloc);
670 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500671 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400672 if (err)
673 extent_io_tree_panic(tree, err);
674
Chris Masond1310b22008-01-24 16:13:08 -0500675 prealloc = NULL;
676 if (err)
677 goto out;
678 if (state->end <= end) {
Qu Wenruofefdc552015-10-12 15:35:38 +0800679 state = clear_state_bit(tree, state, &bits, wake,
680 changeset);
Liu Bod1ac6e42012-05-10 18:10:39 +0800681 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500682 }
683 goto search_again;
684 }
685 /*
686 * | ---- desired range ---- |
687 * | state |
688 * We need to split the extent, and clear the bit
689 * on the first half
690 */
691 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000692 prealloc = alloc_extent_state_atomic(prealloc);
693 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500694 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400695 if (err)
696 extent_io_tree_panic(tree, err);
697
Chris Masond1310b22008-01-24 16:13:08 -0500698 if (wake)
699 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400700
Qu Wenruofefdc552015-10-12 15:35:38 +0800701 clear_state_bit(tree, prealloc, &bits, wake, changeset);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400702
Chris Masond1310b22008-01-24 16:13:08 -0500703 prealloc = NULL;
704 goto out;
705 }
Chris Mason42daec22009-09-23 19:51:09 -0400706
Qu Wenruofefdc552015-10-12 15:35:38 +0800707 state = clear_state_bit(tree, state, &bits, wake, changeset);
Liu Bo04493142012-02-16 18:34:37 +0800708next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400709 if (last_end == (u64)-1)
710 goto out;
711 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800712 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800713 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500714
715search_again:
716 if (start > end)
717 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500718 spin_unlock(&tree->lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800719 if (gfpflags_allow_blocking(mask))
Chris Masond1310b22008-01-24 16:13:08 -0500720 cond_resched();
721 goto again;
David Sterba7ab5cb22016-04-27 01:02:15 +0200722
723out:
724 spin_unlock(&tree->lock);
725 if (prealloc)
726 free_extent_state(prealloc);
727
728 return 0;
729
Chris Masond1310b22008-01-24 16:13:08 -0500730}
Chris Masond1310b22008-01-24 16:13:08 -0500731
Jeff Mahoney143bede2012-03-01 14:56:26 +0100732static void wait_on_state(struct extent_io_tree *tree,
733 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500734 __releases(tree->lock)
735 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500736{
737 DEFINE_WAIT(wait);
738 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500739 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500740 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500741 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500742 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500743}
744
745/*
746 * waits for one or more bits to clear on a range in the state tree.
747 * The range [start, end] is inclusive.
748 * The tree lock is taken by this function
749 */
David Sterba41074882013-04-29 13:38:46 +0000750static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
751 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500752{
753 struct extent_state *state;
754 struct rb_node *node;
755
Josef Bacika5dee372013-12-13 10:02:44 -0500756 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000757
Chris Masoncad321a2008-12-17 14:51:42 -0500758 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500759again:
760 while (1) {
761 /*
762 * this search will find all the extents that end after
763 * our range starts
764 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500765 node = tree_search(tree, start);
Filipe Mananac50d3e72014-03-31 14:53:25 +0100766process_node:
Chris Masond1310b22008-01-24 16:13:08 -0500767 if (!node)
768 break;
769
770 state = rb_entry(node, struct extent_state, rb_node);
771
772 if (state->start > end)
773 goto out;
774
775 if (state->state & bits) {
776 start = state->start;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200777 refcount_inc(&state->refs);
Chris Masond1310b22008-01-24 16:13:08 -0500778 wait_on_state(tree, state);
779 free_extent_state(state);
780 goto again;
781 }
782 start = state->end + 1;
783
784 if (start > end)
785 break;
786
Filipe Mananac50d3e72014-03-31 14:53:25 +0100787 if (!cond_resched_lock(&tree->lock)) {
788 node = rb_next(node);
789 goto process_node;
790 }
Chris Masond1310b22008-01-24 16:13:08 -0500791 }
792out:
Chris Masoncad321a2008-12-17 14:51:42 -0500793 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500794}
Chris Masond1310b22008-01-24 16:13:08 -0500795
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000796static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500797 struct extent_state *state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800798 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500799{
David Sterba9ee49a042015-01-14 19:52:13 +0100800 unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100801 int ret;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400802
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000803 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400804 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500805 u64 range = state->end - state->start + 1;
806 tree->dirty_bytes += range;
807 }
David Sterba57599c72018-03-01 17:56:34 +0100808 ret = add_extent_changeset(state, bits_to_set, changeset, 1);
809 BUG_ON(ret < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400810 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500811}
812
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100813static void cache_state_if_flags(struct extent_state *state,
814 struct extent_state **cached_ptr,
David Sterba9ee49a042015-01-14 19:52:13 +0100815 unsigned flags)
Chris Mason2c64c532009-09-02 15:04:12 -0400816{
817 if (cached_ptr && !(*cached_ptr)) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100818 if (!flags || (state->state & flags)) {
Chris Mason2c64c532009-09-02 15:04:12 -0400819 *cached_ptr = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200820 refcount_inc(&state->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400821 }
822 }
823}
824
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100825static void cache_state(struct extent_state *state,
826 struct extent_state **cached_ptr)
827{
828 return cache_state_if_flags(state, cached_ptr,
829 EXTENT_IOBITS | EXTENT_BOUNDARY);
830}
831
Chris Masond1310b22008-01-24 16:13:08 -0500832/*
Chris Mason1edbb732009-09-02 13:24:36 -0400833 * set some bits on a range in the tree. This may require allocations or
834 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500835 *
Chris Mason1edbb732009-09-02 13:24:36 -0400836 * If any of the exclusive bits are set, this will fail with -EEXIST if some
837 * part of the range already has the desired bits set. The start of the
838 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500839 *
Chris Mason1edbb732009-09-02 13:24:36 -0400840 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500841 */
Chris Mason1edbb732009-09-02 13:24:36 -0400842
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100843static int __must_check
844__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +0100845 unsigned bits, unsigned exclusive_bits,
David Sterba41074882013-04-29 13:38:46 +0000846 u64 *failed_start, struct extent_state **cached_state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800847 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500848{
849 struct extent_state *state;
850 struct extent_state *prealloc = NULL;
851 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000852 struct rb_node **p;
853 struct rb_node *parent;
Chris Masond1310b22008-01-24 16:13:08 -0500854 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500855 u64 last_start;
856 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400857
Josef Bacika5dee372013-12-13 10:02:44 -0500858 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000859
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400860 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500861again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800862 if (!prealloc && gfpflags_allow_blocking(mask)) {
David Sterba059f7912016-04-27 01:03:45 +0200863 /*
864 * Don't care for allocation failure here because we might end
865 * up not needing the pre-allocated extent state at all, which
866 * is the case if we only have in the tree extent states that
867 * cover our input range and don't cover too any other range.
868 * If we end up needing a new extent state we allocate it later.
869 */
Chris Masond1310b22008-01-24 16:13:08 -0500870 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500871 }
872
Chris Masoncad321a2008-12-17 14:51:42 -0500873 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400874 if (cached_state && *cached_state) {
875 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400876 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +0100877 extent_state_in_tree(state)) {
Chris Mason9655d292009-09-02 15:22:30 -0400878 node = &state->rb_node;
879 goto hit_next;
880 }
881 }
Chris Masond1310b22008-01-24 16:13:08 -0500882 /*
883 * this search will find all the extents that end after
884 * our range starts.
885 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000886 node = tree_search_for_insert(tree, start, &p, &parent);
Chris Masond1310b22008-01-24 16:13:08 -0500887 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000888 prealloc = alloc_extent_state_atomic(prealloc);
889 BUG_ON(!prealloc);
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000890 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800891 &p, &parent, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400892 if (err)
893 extent_io_tree_panic(tree, err);
894
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +0000895 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500896 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500897 goto out;
898 }
Chris Masond1310b22008-01-24 16:13:08 -0500899 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400900hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500901 last_start = state->start;
902 last_end = state->end;
903
904 /*
905 * | ---- desired range ---- |
906 * | state |
907 *
908 * Just lock what we found and keep going
909 */
910 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400911 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500912 *failed_start = state->start;
913 err = -EEXIST;
914 goto out;
915 }
Chris Mason42daec22009-09-23 19:51:09 -0400916
Qu Wenruod38ed272015-10-12 14:53:37 +0800917 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400918 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500919 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400920 if (last_end == (u64)-1)
921 goto out;
922 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800923 state = next_state(state);
924 if (start < end && state && state->start == start &&
925 !need_resched())
926 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500927 goto search_again;
928 }
929
930 /*
931 * | ---- desired range ---- |
932 * | state |
933 * or
934 * | ------------- state -------------- |
935 *
936 * We need to split the extent we found, and may flip bits on
937 * second half.
938 *
939 * If the extent we found extends past our
940 * range, we just split and search again. It'll get split
941 * again the next time though.
942 *
943 * If the extent we found is inside our range, we set the
944 * desired bit on it.
945 */
946 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400947 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500948 *failed_start = start;
949 err = -EEXIST;
950 goto out;
951 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000952
953 prealloc = alloc_extent_state_atomic(prealloc);
954 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500955 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400956 if (err)
957 extent_io_tree_panic(tree, err);
958
Chris Masond1310b22008-01-24 16:13:08 -0500959 prealloc = NULL;
960 if (err)
961 goto out;
962 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +0800963 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400964 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500965 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400966 if (last_end == (u64)-1)
967 goto out;
968 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800969 state = next_state(state);
970 if (start < end && state && state->start == start &&
971 !need_resched())
972 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500973 }
974 goto search_again;
975 }
976 /*
977 * | ---- desired range ---- |
978 * | state | or | state |
979 *
980 * There's a hole, we need to insert something in it and
981 * ignore the extent we found.
982 */
983 if (state->start > start) {
984 u64 this_end;
985 if (end < last_start)
986 this_end = end;
987 else
Chris Masond3977122009-01-05 21:25:51 -0500988 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000989
990 prealloc = alloc_extent_state_atomic(prealloc);
991 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000992
993 /*
994 * Avoid to free 'prealloc' if it can be merged with
995 * the later extent.
996 */
Chris Masond1310b22008-01-24 16:13:08 -0500997 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800998 NULL, NULL, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400999 if (err)
1000 extent_io_tree_panic(tree, err);
1001
Chris Mason2c64c532009-09-02 15:04:12 -04001002 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001003 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001004 start = this_end + 1;
1005 goto search_again;
1006 }
1007 /*
1008 * | ---- desired range ---- |
1009 * | state |
1010 * We need to split the extent, and set the bit
1011 * on the first half
1012 */
1013 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -04001014 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001015 *failed_start = start;
1016 err = -EEXIST;
1017 goto out;
1018 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001019
1020 prealloc = alloc_extent_state_atomic(prealloc);
1021 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001022 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001023 if (err)
1024 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -05001025
Qu Wenruod38ed272015-10-12 14:53:37 +08001026 set_state_bits(tree, prealloc, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001027 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001028 merge_state(tree, prealloc);
1029 prealloc = NULL;
1030 goto out;
1031 }
1032
David Sterbab5a4ba142016-04-27 01:02:15 +02001033search_again:
1034 if (start > end)
1035 goto out;
1036 spin_unlock(&tree->lock);
1037 if (gfpflags_allow_blocking(mask))
1038 cond_resched();
1039 goto again;
Chris Masond1310b22008-01-24 16:13:08 -05001040
1041out:
Chris Masoncad321a2008-12-17 14:51:42 -05001042 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001043 if (prealloc)
1044 free_extent_state(prealloc);
1045
1046 return err;
1047
Chris Masond1310b22008-01-24 16:13:08 -05001048}
Chris Masond1310b22008-01-24 16:13:08 -05001049
David Sterba41074882013-04-29 13:38:46 +00001050int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001051 unsigned bits, u64 * failed_start,
David Sterba41074882013-04-29 13:38:46 +00001052 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001053{
1054 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001055 cached_state, mask, NULL);
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001056}
1057
1058
Josef Bacik462d6fa2011-09-26 13:56:12 -04001059/**
Liu Bo10983f22012-07-11 15:26:19 +08001060 * convert_extent_bit - convert all bits in a given range from one bit to
1061 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -04001062 * @tree: the io tree to search
1063 * @start: the start offset in bytes
1064 * @end: the end offset in bytes (inclusive)
1065 * @bits: the bits to set in this range
1066 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001067 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001068 *
1069 * This will go through and set bits for the given range. If any states exist
1070 * already in this range they are set with the given bit and cleared of the
1071 * clear_bits. This is only meant to be used by things that are mergeable, ie
1072 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1073 * boundary bits like LOCK.
David Sterba210aa272016-04-26 23:54:39 +02001074 *
1075 * All allocations are done with GFP_NOFS.
Josef Bacik462d6fa2011-09-26 13:56:12 -04001076 */
1077int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001078 unsigned bits, unsigned clear_bits,
David Sterba210aa272016-04-26 23:54:39 +02001079 struct extent_state **cached_state)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001080{
1081 struct extent_state *state;
1082 struct extent_state *prealloc = NULL;
1083 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001084 struct rb_node **p;
1085 struct rb_node *parent;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001086 int err = 0;
1087 u64 last_start;
1088 u64 last_end;
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001089 bool first_iteration = true;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001090
Josef Bacika5dee372013-12-13 10:02:44 -05001091 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +00001092
Josef Bacik462d6fa2011-09-26 13:56:12 -04001093again:
David Sterba210aa272016-04-26 23:54:39 +02001094 if (!prealloc) {
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001095 /*
1096 * Best effort, don't worry if extent state allocation fails
1097 * here for the first iteration. We might have a cached state
1098 * that matches exactly the target range, in which case no
1099 * extent state allocations are needed. We'll only know this
1100 * after locking the tree.
1101 */
David Sterba210aa272016-04-26 23:54:39 +02001102 prealloc = alloc_extent_state(GFP_NOFS);
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001103 if (!prealloc && !first_iteration)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001104 return -ENOMEM;
1105 }
1106
1107 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001108 if (cached_state && *cached_state) {
1109 state = *cached_state;
1110 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +01001111 extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001112 node = &state->rb_node;
1113 goto hit_next;
1114 }
1115 }
1116
Josef Bacik462d6fa2011-09-26 13:56:12 -04001117 /*
1118 * this search will find all the extents that end after
1119 * our range starts.
1120 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001121 node = tree_search_for_insert(tree, start, &p, &parent);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001122 if (!node) {
1123 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001124 if (!prealloc) {
1125 err = -ENOMEM;
1126 goto out;
1127 }
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001128 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001129 &p, &parent, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001130 if (err)
1131 extent_io_tree_panic(tree, err);
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001132 cache_state(prealloc, cached_state);
1133 prealloc = NULL;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001134 goto out;
1135 }
1136 state = rb_entry(node, struct extent_state, rb_node);
1137hit_next:
1138 last_start = state->start;
1139 last_end = state->end;
1140
1141 /*
1142 * | ---- desired range ---- |
1143 * | state |
1144 *
1145 * Just lock what we found and keep going
1146 */
1147 if (state->start == start && state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001148 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001149 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001150 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001151 if (last_end == (u64)-1)
1152 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001153 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001154 if (start < end && state && state->start == start &&
1155 !need_resched())
1156 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001157 goto search_again;
1158 }
1159
1160 /*
1161 * | ---- desired range ---- |
1162 * | state |
1163 * or
1164 * | ------------- state -------------- |
1165 *
1166 * We need to split the extent we found, and may flip bits on
1167 * second half.
1168 *
1169 * If the extent we found extends past our
1170 * range, we just split and search again. It'll get split
1171 * again the next time though.
1172 *
1173 * If the extent we found is inside our range, we set the
1174 * desired bit on it.
1175 */
1176 if (state->start < start) {
1177 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001178 if (!prealloc) {
1179 err = -ENOMEM;
1180 goto out;
1181 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001182 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001183 if (err)
1184 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001185 prealloc = NULL;
1186 if (err)
1187 goto out;
1188 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001189 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001190 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001191 state = clear_state_bit(tree, state, &clear_bits, 0,
1192 NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001193 if (last_end == (u64)-1)
1194 goto out;
1195 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001196 if (start < end && state && state->start == start &&
1197 !need_resched())
1198 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001199 }
1200 goto search_again;
1201 }
1202 /*
1203 * | ---- desired range ---- |
1204 * | state | or | state |
1205 *
1206 * There's a hole, we need to insert something in it and
1207 * ignore the extent we found.
1208 */
1209 if (state->start > start) {
1210 u64 this_end;
1211 if (end < last_start)
1212 this_end = end;
1213 else
1214 this_end = last_start - 1;
1215
1216 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001217 if (!prealloc) {
1218 err = -ENOMEM;
1219 goto out;
1220 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001221
1222 /*
1223 * Avoid to free 'prealloc' if it can be merged with
1224 * the later extent.
1225 */
1226 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001227 NULL, NULL, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001228 if (err)
1229 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001230 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001231 prealloc = NULL;
1232 start = this_end + 1;
1233 goto search_again;
1234 }
1235 /*
1236 * | ---- desired range ---- |
1237 * | state |
1238 * We need to split the extent, and set the bit
1239 * on the first half
1240 */
1241 if (state->start <= end && state->end > end) {
1242 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001243 if (!prealloc) {
1244 err = -ENOMEM;
1245 goto out;
1246 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001247
1248 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001249 if (err)
1250 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001251
Qu Wenruod38ed272015-10-12 14:53:37 +08001252 set_state_bits(tree, prealloc, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001253 cache_state(prealloc, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001254 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001255 prealloc = NULL;
1256 goto out;
1257 }
1258
Josef Bacik462d6fa2011-09-26 13:56:12 -04001259search_again:
1260 if (start > end)
1261 goto out;
1262 spin_unlock(&tree->lock);
David Sterba210aa272016-04-26 23:54:39 +02001263 cond_resched();
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001264 first_iteration = false;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001265 goto again;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001266
1267out:
1268 spin_unlock(&tree->lock);
1269 if (prealloc)
1270 free_extent_state(prealloc);
1271
1272 return err;
1273}
1274
Chris Masond1310b22008-01-24 16:13:08 -05001275/* wrappers around set/clear extent bit */
Qu Wenruod38ed272015-10-12 14:53:37 +08001276int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba2c53b912016-04-26 23:54:39 +02001277 unsigned bits, struct extent_changeset *changeset)
Qu Wenruod38ed272015-10-12 14:53:37 +08001278{
1279 /*
1280 * We don't support EXTENT_LOCKED yet, as current changeset will
1281 * record any bits changed, so for EXTENT_LOCKED case, it will
1282 * either fail with -EEXIST or changeset will record the whole
1283 * range.
1284 */
1285 BUG_ON(bits & EXTENT_LOCKED);
1286
David Sterba2c53b912016-04-26 23:54:39 +02001287 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
Qu Wenruod38ed272015-10-12 14:53:37 +08001288 changeset);
1289}
1290
Qu Wenruofefdc552015-10-12 15:35:38 +08001291int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1292 unsigned bits, int wake, int delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001293 struct extent_state **cached)
Qu Wenruofefdc552015-10-12 15:35:38 +08001294{
1295 return __clear_extent_bit(tree, start, end, bits, wake, delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001296 cached, GFP_NOFS, NULL);
Qu Wenruofefdc552015-10-12 15:35:38 +08001297}
1298
Qu Wenruofefdc552015-10-12 15:35:38 +08001299int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaf734c442016-04-26 23:54:39 +02001300 unsigned bits, struct extent_changeset *changeset)
Qu Wenruofefdc552015-10-12 15:35:38 +08001301{
1302 /*
1303 * Don't support EXTENT_LOCKED case, same reason as
1304 * set_record_extent_bits().
1305 */
1306 BUG_ON(bits & EXTENT_LOCKED);
1307
David Sterbaf734c442016-04-26 23:54:39 +02001308 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
Qu Wenruofefdc552015-10-12 15:35:38 +08001309 changeset);
1310}
1311
Chris Masond352ac62008-09-29 15:18:18 -04001312/*
1313 * either insert or lock state struct between start and end use mask to tell
1314 * us if waiting is desired.
1315 */
Chris Mason1edbb732009-09-02 13:24:36 -04001316int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaff13db42015-12-03 14:30:40 +01001317 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001318{
1319 int err;
1320 u64 failed_start;
David Sterba9ee49a042015-01-14 19:52:13 +01001321
Chris Masond1310b22008-01-24 16:13:08 -05001322 while (1) {
David Sterbaff13db42015-12-03 14:30:40 +01001323 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001324 EXTENT_LOCKED, &failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001325 cached_state, GFP_NOFS, NULL);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001326 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001327 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1328 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001329 } else
Chris Masond1310b22008-01-24 16:13:08 -05001330 break;
Chris Masond1310b22008-01-24 16:13:08 -05001331 WARN_ON(start > end);
1332 }
1333 return err;
1334}
Chris Masond1310b22008-01-24 16:13:08 -05001335
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001336int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001337{
1338 int err;
1339 u64 failed_start;
1340
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001341 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
Qu Wenruod38ed272015-10-12 14:53:37 +08001342 &failed_start, NULL, GFP_NOFS, NULL);
Yan Zheng66435582008-10-30 14:19:50 -04001343 if (err == -EEXIST) {
1344 if (failed_start > start)
1345 clear_extent_bit(tree, start, failed_start - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001346 EXTENT_LOCKED, 1, 0, NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001347 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001348 }
Josef Bacik25179202008-10-29 14:49:05 -04001349 return 1;
1350}
Josef Bacik25179202008-10-29 14:49:05 -04001351
David Sterbabd1fa4f2015-12-03 13:08:59 +01001352void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001353{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001354 unsigned long index = start >> PAGE_SHIFT;
1355 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001356 struct page *page;
1357
1358 while (index <= end_index) {
1359 page = find_get_page(inode->i_mapping, index);
1360 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1361 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001362 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001363 index++;
1364 }
Chris Mason4adaa612013-03-26 13:07:00 -04001365}
1366
David Sterbaf6311572015-12-03 13:08:59 +01001367void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001368{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001369 unsigned long index = start >> PAGE_SHIFT;
1370 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001371 struct page *page;
1372
1373 while (index <= end_index) {
1374 page = find_get_page(inode->i_mapping, index);
1375 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Mason4adaa612013-03-26 13:07:00 -04001376 __set_page_dirty_nobuffers(page);
Konstantin Khebnikov8d386332015-02-11 15:26:55 -08001377 account_page_redirty(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001378 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001379 index++;
1380 }
Chris Mason4adaa612013-03-26 13:07:00 -04001381}
1382
Chris Masond352ac62008-09-29 15:18:18 -04001383/* find the first state struct with 'bits' set after 'start', and
1384 * return it. tree->lock must be held. NULL will returned if
1385 * nothing was found after 'start'
1386 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001387static struct extent_state *
1388find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +01001389 u64 start, unsigned bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001390{
1391 struct rb_node *node;
1392 struct extent_state *state;
1393
1394 /*
1395 * this search will find all the extents that end after
1396 * our range starts.
1397 */
1398 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001399 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001400 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001401
Chris Masond3977122009-01-05 21:25:51 -05001402 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001403 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001404 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001405 return state;
Chris Masond3977122009-01-05 21:25:51 -05001406
Chris Masond7fc6402008-02-18 12:12:38 -05001407 node = rb_next(node);
1408 if (!node)
1409 break;
1410 }
1411out:
1412 return NULL;
1413}
Chris Masond7fc6402008-02-18 12:12:38 -05001414
Chris Masond352ac62008-09-29 15:18:18 -04001415/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001416 * find the first offset in the io tree with 'bits' set. zero is
1417 * returned if we find something, and *start_ret and *end_ret are
1418 * set to reflect the state struct that was found.
1419 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001420 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001421 */
1422int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba9ee49a042015-01-14 19:52:13 +01001423 u64 *start_ret, u64 *end_ret, unsigned bits,
Josef Bacike6138872012-09-27 17:07:30 -04001424 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001425{
1426 struct extent_state *state;
1427 int ret = 1;
1428
1429 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001430 if (cached_state && *cached_state) {
1431 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001432 if (state->end == start - 1 && extent_state_in_tree(state)) {
Liu Bo9688e9a2018-08-23 03:14:53 +08001433 while ((state = next_state(state)) != NULL) {
Josef Bacike6138872012-09-27 17:07:30 -04001434 if (state->state & bits)
1435 goto got_it;
Josef Bacike6138872012-09-27 17:07:30 -04001436 }
1437 free_extent_state(*cached_state);
1438 *cached_state = NULL;
1439 goto out;
1440 }
1441 free_extent_state(*cached_state);
1442 *cached_state = NULL;
1443 }
1444
Xiao Guangrong69261c42011-07-14 03:19:45 +00001445 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001446got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001447 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001448 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001449 *start_ret = state->start;
1450 *end_ret = state->end;
1451 ret = 0;
1452 }
Josef Bacike6138872012-09-27 17:07:30 -04001453out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001454 spin_unlock(&tree->lock);
1455 return ret;
1456}
1457
1458/*
Chris Masond352ac62008-09-29 15:18:18 -04001459 * find a contiguous range of bytes in the file marked as delalloc, not
1460 * more than 'max_bytes'. start and end are used to return the range,
1461 *
1462 * 1 is returned if we find something, 0 if nothing was in the tree
1463 */
Chris Masonc8b97812008-10-29 14:49:59 -04001464static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001465 u64 *start, u64 *end, u64 max_bytes,
1466 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001467{
1468 struct rb_node *node;
1469 struct extent_state *state;
1470 u64 cur_start = *start;
1471 u64 found = 0;
1472 u64 total_bytes = 0;
1473
Chris Masoncad321a2008-12-17 14:51:42 -05001474 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001475
Chris Masond1310b22008-01-24 16:13:08 -05001476 /*
1477 * this search will find all the extents that end after
1478 * our range starts.
1479 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001480 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001481 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001482 if (!found)
1483 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001484 goto out;
1485 }
1486
Chris Masond3977122009-01-05 21:25:51 -05001487 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001488 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001489 if (found && (state->start != cur_start ||
1490 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001491 goto out;
1492 }
1493 if (!(state->state & EXTENT_DELALLOC)) {
1494 if (!found)
1495 *end = state->end;
1496 goto out;
1497 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001498 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001499 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001500 *cached_state = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +02001501 refcount_inc(&state->refs);
Josef Bacikc2a128d2010-02-02 21:19:11 +00001502 }
Chris Masond1310b22008-01-24 16:13:08 -05001503 found++;
1504 *end = state->end;
1505 cur_start = state->end + 1;
1506 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001507 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001508 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001509 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001510 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001511 break;
1512 }
1513out:
Chris Masoncad321a2008-12-17 14:51:42 -05001514 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001515 return found;
1516}
1517
Liu Boda2c7002017-02-10 16:41:05 +01001518static int __process_pages_contig(struct address_space *mapping,
1519 struct page *locked_page,
1520 pgoff_t start_index, pgoff_t end_index,
1521 unsigned long page_ops, pgoff_t *index_ret);
1522
Jeff Mahoney143bede2012-03-01 14:56:26 +01001523static noinline void __unlock_for_delalloc(struct inode *inode,
1524 struct page *locked_page,
1525 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001526{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001527 unsigned long index = start >> PAGE_SHIFT;
1528 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001529
Liu Bo76c00212017-02-10 16:42:14 +01001530 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001531 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001532 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001533
Liu Bo76c00212017-02-10 16:42:14 +01001534 __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1535 PAGE_UNLOCK, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -04001536}
1537
1538static noinline int lock_delalloc_pages(struct inode *inode,
1539 struct page *locked_page,
1540 u64 delalloc_start,
1541 u64 delalloc_end)
1542{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001543 unsigned long index = delalloc_start >> PAGE_SHIFT;
Liu Bo76c00212017-02-10 16:42:14 +01001544 unsigned long index_ret = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001545 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001546 int ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001547
Liu Bo76c00212017-02-10 16:42:14 +01001548 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001549 if (index == locked_page->index && index == end_index)
1550 return 0;
1551
Liu Bo76c00212017-02-10 16:42:14 +01001552 ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1553 end_index, PAGE_LOCK, &index_ret);
1554 if (ret == -EAGAIN)
1555 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1556 (u64)index_ret << PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001557 return ret;
1558}
1559
1560/*
1561 * find a contiguous range of bytes in the file marked as delalloc, not
1562 * more than 'max_bytes'. start and end are used to return the range,
1563 *
1564 * 1 is returned if we find something, 0 if nothing was in the tree
1565 */
David Sterba9c363962018-08-17 17:28:06 +02001566static noinline_for_stack u64 find_lock_delalloc_range(struct inode *inode,
Josef Bacik294e30f2013-10-09 12:00:56 -04001567 struct extent_io_tree *tree,
1568 struct page *locked_page, u64 *start,
1569 u64 *end, u64 max_bytes)
Chris Masonc8b97812008-10-29 14:49:59 -04001570{
1571 u64 delalloc_start;
1572 u64 delalloc_end;
1573 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001574 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001575 int ret;
1576 int loops = 0;
1577
1578again:
1579 /* step one, find a bunch of delalloc bytes starting at start */
1580 delalloc_start = *start;
1581 delalloc_end = 0;
1582 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001583 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001584 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001585 *start = delalloc_start;
1586 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001587 free_extent_state(cached_state);
Liu Bo385fe0b2013-10-01 23:49:49 +08001588 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001589 }
1590
1591 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001592 * start comes from the offset of locked_page. We have to lock
1593 * pages in order, so we can't process delalloc bytes before
1594 * locked_page
1595 */
Chris Masond3977122009-01-05 21:25:51 -05001596 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001597 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001598
1599 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001600 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001601 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001602 if (delalloc_end + 1 - delalloc_start > max_bytes)
1603 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001604
Chris Masonc8b97812008-10-29 14:49:59 -04001605 /* step two, lock all the pages after the page that has start */
1606 ret = lock_delalloc_pages(inode, locked_page,
1607 delalloc_start, delalloc_end);
1608 if (ret == -EAGAIN) {
1609 /* some of the pages are gone, lets avoid looping by
1610 * shortening the size of the delalloc range we're searching
1611 */
Chris Mason9655d292009-09-02 15:22:30 -04001612 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001613 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001614 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001615 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001616 loops = 1;
1617 goto again;
1618 } else {
1619 found = 0;
1620 goto out_failed;
1621 }
1622 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001623 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001624
1625 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001626 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001627
1628 /* then test to make sure it is all still delalloc */
1629 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001630 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001631 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001632 unlock_extent_cached(tree, delalloc_start, delalloc_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001633 &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001634 __unlock_for_delalloc(inode, locked_page,
1635 delalloc_start, delalloc_end);
1636 cond_resched();
1637 goto again;
1638 }
Chris Mason9655d292009-09-02 15:22:30 -04001639 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001640 *start = delalloc_start;
1641 *end = delalloc_end;
1642out_failed:
1643 return found;
1644}
1645
David Sterba9c363962018-08-17 17:28:06 +02001646#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1647u64 btrfs_find_lock_delalloc_range(struct inode *inode,
1648 struct extent_io_tree *tree,
1649 struct page *locked_page, u64 *start,
1650 u64 *end, u64 max_bytes)
1651{
1652 return find_lock_delalloc_range(inode, tree, locked_page, start, end,
1653 max_bytes);
1654}
1655#endif
1656
Liu Boda2c7002017-02-10 16:41:05 +01001657static int __process_pages_contig(struct address_space *mapping,
1658 struct page *locked_page,
1659 pgoff_t start_index, pgoff_t end_index,
1660 unsigned long page_ops, pgoff_t *index_ret)
Chris Masonc8b97812008-10-29 14:49:59 -04001661{
Liu Bo873695b2017-02-02 17:49:22 -08001662 unsigned long nr_pages = end_index - start_index + 1;
Liu Boda2c7002017-02-10 16:41:05 +01001663 unsigned long pages_locked = 0;
Liu Bo873695b2017-02-02 17:49:22 -08001664 pgoff_t index = start_index;
Chris Masonc8b97812008-10-29 14:49:59 -04001665 struct page *pages[16];
Liu Bo873695b2017-02-02 17:49:22 -08001666 unsigned ret;
Liu Boda2c7002017-02-10 16:41:05 +01001667 int err = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001668 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001669
Liu Boda2c7002017-02-10 16:41:05 +01001670 if (page_ops & PAGE_LOCK) {
1671 ASSERT(page_ops == PAGE_LOCK);
1672 ASSERT(index_ret && *index_ret == start_index);
1673 }
1674
Filipe Manana704de492014-10-06 22:14:22 +01001675 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
Liu Bo873695b2017-02-02 17:49:22 -08001676 mapping_set_error(mapping, -EIO);
Filipe Manana704de492014-10-06 22:14:22 +01001677
Chris Masond3977122009-01-05 21:25:51 -05001678 while (nr_pages > 0) {
Liu Bo873695b2017-02-02 17:49:22 -08001679 ret = find_get_pages_contig(mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001680 min_t(unsigned long,
1681 nr_pages, ARRAY_SIZE(pages)), pages);
Liu Boda2c7002017-02-10 16:41:05 +01001682 if (ret == 0) {
1683 /*
1684 * Only if we're going to lock these pages,
1685 * can we find nothing at @index.
1686 */
1687 ASSERT(page_ops & PAGE_LOCK);
Liu Bo49d4a332017-03-06 18:20:56 -08001688 err = -EAGAIN;
1689 goto out;
Liu Boda2c7002017-02-10 16:41:05 +01001690 }
Chris Mason8b62b722009-09-02 16:53:46 -04001691
Liu Boda2c7002017-02-10 16:41:05 +01001692 for (i = 0; i < ret; i++) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001693 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001694 SetPagePrivate2(pages[i]);
1695
Chris Masonc8b97812008-10-29 14:49:59 -04001696 if (pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001697 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001698 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001699 continue;
1700 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001701 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001702 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001703 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001704 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001705 if (page_ops & PAGE_SET_ERROR)
1706 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001707 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001708 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001709 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001710 unlock_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001711 if (page_ops & PAGE_LOCK) {
1712 lock_page(pages[i]);
1713 if (!PageDirty(pages[i]) ||
1714 pages[i]->mapping != mapping) {
1715 unlock_page(pages[i]);
1716 put_page(pages[i]);
1717 err = -EAGAIN;
1718 goto out;
1719 }
1720 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001721 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001722 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001723 }
1724 nr_pages -= ret;
1725 index += ret;
1726 cond_resched();
1727 }
Liu Boda2c7002017-02-10 16:41:05 +01001728out:
1729 if (err && index_ret)
1730 *index_ret = start_index + pages_locked - 1;
1731 return err;
Chris Masonc8b97812008-10-29 14:49:59 -04001732}
Chris Masonc8b97812008-10-29 14:49:59 -04001733
Liu Bo873695b2017-02-02 17:49:22 -08001734void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1735 u64 delalloc_end, struct page *locked_page,
1736 unsigned clear_bits,
1737 unsigned long page_ops)
1738{
1739 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001740 NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001741
1742 __process_pages_contig(inode->i_mapping, locked_page,
1743 start >> PAGE_SHIFT, end >> PAGE_SHIFT,
Liu Boda2c7002017-02-10 16:41:05 +01001744 page_ops, NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001745}
1746
Chris Masond352ac62008-09-29 15:18:18 -04001747/*
1748 * count the number of bytes in the tree that have a given bit(s)
1749 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1750 * cached. The total number found is returned.
1751 */
Chris Masond1310b22008-01-24 16:13:08 -05001752u64 count_range_bits(struct extent_io_tree *tree,
1753 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001754 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001755{
1756 struct rb_node *node;
1757 struct extent_state *state;
1758 u64 cur_start = *start;
1759 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001760 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001761 int found = 0;
1762
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301763 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001764 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001765
Chris Masoncad321a2008-12-17 14:51:42 -05001766 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001767 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1768 total_bytes = tree->dirty_bytes;
1769 goto out;
1770 }
1771 /*
1772 * this search will find all the extents that end after
1773 * our range starts.
1774 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001775 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001776 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001777 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001778
Chris Masond3977122009-01-05 21:25:51 -05001779 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001780 state = rb_entry(node, struct extent_state, rb_node);
1781 if (state->start > search_end)
1782 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001783 if (contig && found && state->start > last + 1)
1784 break;
1785 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001786 total_bytes += min(search_end, state->end) + 1 -
1787 max(cur_start, state->start);
1788 if (total_bytes >= max_bytes)
1789 break;
1790 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001791 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001792 found = 1;
1793 }
Chris Masonec29ed52011-02-23 16:23:20 -05001794 last = state->end;
1795 } else if (contig && found) {
1796 break;
Chris Masond1310b22008-01-24 16:13:08 -05001797 }
1798 node = rb_next(node);
1799 if (!node)
1800 break;
1801 }
1802out:
Chris Masoncad321a2008-12-17 14:51:42 -05001803 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001804 return total_bytes;
1805}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001806
Chris Masond352ac62008-09-29 15:18:18 -04001807/*
1808 * set the private field for a given byte offset in the tree. If there isn't
1809 * an extent_state there already, this does nothing.
1810 */
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001811static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001812 struct io_failure_record *failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001813{
1814 struct rb_node *node;
1815 struct extent_state *state;
1816 int ret = 0;
1817
Chris Masoncad321a2008-12-17 14:51:42 -05001818 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001819 /*
1820 * this search will find all the extents that end after
1821 * our range starts.
1822 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001823 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001824 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001825 ret = -ENOENT;
1826 goto out;
1827 }
1828 state = rb_entry(node, struct extent_state, rb_node);
1829 if (state->start != start) {
1830 ret = -ENOENT;
1831 goto out;
1832 }
David Sterba47dc1962016-02-11 13:24:13 +01001833 state->failrec = failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001834out:
Chris Masoncad321a2008-12-17 14:51:42 -05001835 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001836 return ret;
1837}
1838
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001839static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001840 struct io_failure_record **failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001841{
1842 struct rb_node *node;
1843 struct extent_state *state;
1844 int ret = 0;
1845
Chris Masoncad321a2008-12-17 14:51:42 -05001846 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001847 /*
1848 * this search will find all the extents that end after
1849 * our range starts.
1850 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001851 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001852 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001853 ret = -ENOENT;
1854 goto out;
1855 }
1856 state = rb_entry(node, struct extent_state, rb_node);
1857 if (state->start != start) {
1858 ret = -ENOENT;
1859 goto out;
1860 }
David Sterba47dc1962016-02-11 13:24:13 +01001861 *failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001862out:
Chris Masoncad321a2008-12-17 14:51:42 -05001863 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001864 return ret;
1865}
1866
1867/*
1868 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001869 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001870 * has the bits set. Otherwise, 1 is returned if any bit in the
1871 * range is found set.
1872 */
1873int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001874 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001875{
1876 struct extent_state *state = NULL;
1877 struct rb_node *node;
1878 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001879
Chris Masoncad321a2008-12-17 14:51:42 -05001880 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01001881 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001882 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001883 node = &cached->rb_node;
1884 else
1885 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001886 while (node && start <= end) {
1887 state = rb_entry(node, struct extent_state, rb_node);
1888
1889 if (filled && state->start > start) {
1890 bitset = 0;
1891 break;
1892 }
1893
1894 if (state->start > end)
1895 break;
1896
1897 if (state->state & bits) {
1898 bitset = 1;
1899 if (!filled)
1900 break;
1901 } else if (filled) {
1902 bitset = 0;
1903 break;
1904 }
Chris Mason46562ce2009-09-23 20:23:16 -04001905
1906 if (state->end == (u64)-1)
1907 break;
1908
Chris Masond1310b22008-01-24 16:13:08 -05001909 start = state->end + 1;
1910 if (start > end)
1911 break;
1912 node = rb_next(node);
1913 if (!node) {
1914 if (filled)
1915 bitset = 0;
1916 break;
1917 }
1918 }
Chris Masoncad321a2008-12-17 14:51:42 -05001919 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001920 return bitset;
1921}
Chris Masond1310b22008-01-24 16:13:08 -05001922
1923/*
1924 * helper function to set a given page up to date if all the
1925 * extents in the tree for that page are up to date
1926 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001927static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001928{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001929 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001930 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001931 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001932 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001933}
1934
Josef Bacik7870d082017-05-05 11:57:15 -04001935int free_io_failure(struct extent_io_tree *failure_tree,
1936 struct extent_io_tree *io_tree,
1937 struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001938{
1939 int ret;
1940 int err = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001941
David Sterba47dc1962016-02-11 13:24:13 +01001942 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001943 ret = clear_extent_bits(failure_tree, rec->start,
1944 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001945 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001946 if (ret)
1947 err = ret;
1948
Josef Bacik7870d082017-05-05 11:57:15 -04001949 ret = clear_extent_bits(io_tree, rec->start,
David Woodhouse53b381b2013-01-29 18:40:14 -05001950 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001951 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05001952 if (ret && !err)
1953 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001954
1955 kfree(rec);
1956 return err;
1957}
1958
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001959/*
1960 * this bypasses the standard btrfs submit functions deliberately, as
1961 * the standard behavior is to write all copies in a raid setup. here we only
1962 * want to write the one bad copy. so we do the mapping for ourselves and issue
1963 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001964 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001965 * actually prevents the read that triggered the error from finishing.
1966 * currently, there can be no more than two copies of every data bit. thus,
1967 * exactly one rewrite is required.
1968 */
Josef Bacik6ec656b2017-05-05 11:57:14 -04001969int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
1970 u64 length, u64 logical, struct page *page,
1971 unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001972{
1973 struct bio *bio;
1974 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001975 u64 map_length = 0;
1976 u64 sector;
1977 struct btrfs_bio *bbio = NULL;
1978 int ret;
1979
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001980 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001981 BUG_ON(!mirror_num);
1982
David Sterbac5e4c3d2017-06-12 17:29:41 +02001983 bio = btrfs_io_bio_alloc(1);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001984 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001985 map_length = length;
1986
Filipe Mananab5de8d02016-05-27 22:21:27 +01001987 /*
1988 * Avoid races with device replace and make sure our bbio has devices
1989 * associated to its stripes that don't go away while we are doing the
1990 * read repair operation.
1991 */
1992 btrfs_bio_counter_inc_blocked(fs_info);
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03001993 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
Liu Boc7253282017-03-29 10:53:58 -07001994 /*
1995 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
1996 * to update all raid stripes, but here we just want to correct
1997 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
1998 * stripe's dev and sector.
1999 */
2000 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2001 &map_length, &bbio, 0);
2002 if (ret) {
2003 btrfs_bio_counter_dec(fs_info);
2004 bio_put(bio);
2005 return -EIO;
2006 }
2007 ASSERT(bbio->mirror_num == 1);
2008 } else {
2009 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2010 &map_length, &bbio, mirror_num);
2011 if (ret) {
2012 btrfs_bio_counter_dec(fs_info);
2013 bio_put(bio);
2014 return -EIO;
2015 }
2016 BUG_ON(mirror_num != bbio->mirror_num);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002017 }
Liu Boc7253282017-03-29 10:53:58 -07002018
2019 sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002020 bio->bi_iter.bi_sector = sector;
Liu Boc7253282017-03-29 10:53:58 -07002021 dev = bbio->stripes[bbio->mirror_num - 1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002022 btrfs_put_bbio(bbio);
Anand Jainebbede42017-12-04 12:54:52 +08002023 if (!dev || !dev->bdev ||
2024 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002025 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002026 bio_put(bio);
2027 return -EIO;
2028 }
Christoph Hellwig74d46992017-08-23 19:10:32 +02002029 bio_set_dev(bio, dev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002030 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Miao Xieffdd2012014-09-12 18:44:00 +08002031 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002032
Mike Christie4e49ea42016-06-05 14:31:41 -05002033 if (btrfsic_submit_bio_wait(bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002034 /* try to remap that extent elsewhere? */
Filipe Mananab5de8d02016-05-27 22:21:27 +01002035 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002036 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002037 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002038 return -EIO;
2039 }
2040
David Sterbab14af3b2015-10-08 10:43:10 +02002041 btrfs_info_rl_in_rcu(fs_info,
2042 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Josef Bacik6ec656b2017-05-05 11:57:14 -04002043 ino, start,
Miao Xie1203b682014-09-12 18:44:01 +08002044 rcu_str_deref(dev->name), sector);
Filipe Mananab5de8d02016-05-27 22:21:27 +01002045 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002046 bio_put(bio);
2047 return 0;
2048}
2049
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002050int repair_eb_io_failure(struct btrfs_fs_info *fs_info,
2051 struct extent_buffer *eb, int mirror_num)
Josef Bacikea466792012-03-26 21:57:36 -04002052{
Josef Bacikea466792012-03-26 21:57:36 -04002053 u64 start = eb->start;
David Sterbacc5e31a2018-03-01 18:20:27 +01002054 int i, num_pages = num_extent_pages(eb);
Chris Masond95603b2012-04-12 15:55:15 -04002055 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002056
David Howellsbc98a422017-07-17 08:45:34 +01002057 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002058 return -EROFS;
2059
Josef Bacikea466792012-03-26 21:57:36 -04002060 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002061 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002062
Josef Bacik6ec656b2017-05-05 11:57:14 -04002063 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002064 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002065 if (ret)
2066 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002067 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002068 }
2069
2070 return ret;
2071}
2072
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002073/*
2074 * each time an IO finishes, we do a fast check in the IO failure tree
2075 * to see if we need to process or clean up an io_failure_record
2076 */
Josef Bacik7870d082017-05-05 11:57:15 -04002077int clean_io_failure(struct btrfs_fs_info *fs_info,
2078 struct extent_io_tree *failure_tree,
2079 struct extent_io_tree *io_tree, u64 start,
2080 struct page *page, u64 ino, unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002081{
2082 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002083 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002084 struct extent_state *state;
2085 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002086 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002087
2088 private = 0;
Josef Bacik7870d082017-05-05 11:57:15 -04002089 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2090 EXTENT_DIRTY, 0);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002091 if (!ret)
2092 return 0;
2093
Josef Bacik7870d082017-05-05 11:57:15 -04002094 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002095 if (ret)
2096 return 0;
2097
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002098 BUG_ON(!failrec->this_mirror);
2099
2100 if (failrec->in_validation) {
2101 /* there was no real error, just free the record */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002102 btrfs_debug(fs_info,
2103 "clean_io_failure: freeing dummy error at %llu",
2104 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002105 goto out;
2106 }
David Howellsbc98a422017-07-17 08:45:34 +01002107 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002108 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002109
Josef Bacik7870d082017-05-05 11:57:15 -04002110 spin_lock(&io_tree->lock);
2111 state = find_first_extent_bit_state(io_tree,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002112 failrec->start,
2113 EXTENT_LOCKED);
Josef Bacik7870d082017-05-05 11:57:15 -04002114 spin_unlock(&io_tree->lock);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002115
Miao Xie883d0de2013-07-25 19:22:35 +08002116 if (state && state->start <= failrec->start &&
2117 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002118 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2119 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002120 if (num_copies > 1) {
Josef Bacik7870d082017-05-05 11:57:15 -04002121 repair_io_failure(fs_info, ino, start, failrec->len,
2122 failrec->logical, page, pg_offset,
2123 failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002124 }
2125 }
2126
2127out:
Josef Bacik7870d082017-05-05 11:57:15 -04002128 free_io_failure(failure_tree, io_tree, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002129
Miao Xie454ff3d2014-09-12 18:43:58 +08002130 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002131}
2132
Miao Xief6124962014-09-12 18:44:04 +08002133/*
2134 * Can be called when
2135 * - hold extent lock
2136 * - under ordered extent
2137 * - the inode is freeing
2138 */
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002139void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
Miao Xief6124962014-09-12 18:44:04 +08002140{
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002141 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
Miao Xief6124962014-09-12 18:44:04 +08002142 struct io_failure_record *failrec;
2143 struct extent_state *state, *next;
2144
2145 if (RB_EMPTY_ROOT(&failure_tree->state))
2146 return;
2147
2148 spin_lock(&failure_tree->lock);
2149 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2150 while (state) {
2151 if (state->start > end)
2152 break;
2153
2154 ASSERT(state->end <= end);
2155
2156 next = next_state(state);
2157
David Sterba47dc1962016-02-11 13:24:13 +01002158 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002159 free_extent_state(state);
2160 kfree(failrec);
2161
2162 state = next;
2163 }
2164 spin_unlock(&failure_tree->lock);
2165}
2166
Miao Xie2fe63032014-09-12 18:43:59 +08002167int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
David Sterba47dc1962016-02-11 13:24:13 +01002168 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002169{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002170 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002171 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002172 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002173 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2174 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2175 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002176 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002177 u64 logical;
2178
David Sterba47dc1962016-02-11 13:24:13 +01002179 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002180 if (ret) {
2181 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2182 if (!failrec)
2183 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002184
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002185 failrec->start = start;
2186 failrec->len = end - start + 1;
2187 failrec->this_mirror = 0;
2188 failrec->bio_flags = 0;
2189 failrec->in_validation = 0;
2190
2191 read_lock(&em_tree->lock);
2192 em = lookup_extent_mapping(em_tree, start, failrec->len);
2193 if (!em) {
2194 read_unlock(&em_tree->lock);
2195 kfree(failrec);
2196 return -EIO;
2197 }
2198
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002199 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002200 free_extent_map(em);
2201 em = NULL;
2202 }
2203 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002204 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002205 kfree(failrec);
2206 return -EIO;
2207 }
Miao Xie2fe63032014-09-12 18:43:59 +08002208
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002209 logical = start - em->start;
2210 logical = em->block_start + logical;
2211 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2212 logical = em->block_start;
2213 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2214 extent_set_compress_type(&failrec->bio_flags,
2215 em->compress_type);
2216 }
Miao Xie2fe63032014-09-12 18:43:59 +08002217
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002218 btrfs_debug(fs_info,
2219 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2220 logical, start, failrec->len);
Miao Xie2fe63032014-09-12 18:43:59 +08002221
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002222 failrec->logical = logical;
2223 free_extent_map(em);
2224
2225 /* set the bits in the private failure tree */
2226 ret = set_extent_bits(failure_tree, start, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002227 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002228 if (ret >= 0)
David Sterba47dc1962016-02-11 13:24:13 +01002229 ret = set_state_failrec(failure_tree, start, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002230 /* set the bits in the inode's tree */
2231 if (ret >= 0)
David Sterbaceeb0ae2016-04-26 23:54:39 +02002232 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002233 if (ret < 0) {
2234 kfree(failrec);
2235 return ret;
2236 }
2237 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002238 btrfs_debug(fs_info,
2239 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2240 failrec->logical, failrec->start, failrec->len,
2241 failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002242 /*
2243 * when data can be on disk more than twice, add to failrec here
2244 * (e.g. with a list for failed_mirror) to make
2245 * clean_io_failure() clean all those errors at once.
2246 */
2247 }
Miao Xie2fe63032014-09-12 18:43:59 +08002248
2249 *failrec_ret = failrec;
2250
2251 return 0;
2252}
2253
Ming Leia0b60d72017-12-18 20:22:11 +08002254bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages,
Miao Xie2fe63032014-09-12 18:43:59 +08002255 struct io_failure_record *failrec, int failed_mirror)
2256{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002257 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002258 int num_copies;
2259
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002260 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002261 if (num_copies == 1) {
2262 /*
2263 * we only have a single copy of the data, so don't bother with
2264 * all the retry and error correction code that follows. no
2265 * matter what the error is, it is very likely to persist.
2266 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002267 btrfs_debug(fs_info,
2268 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2269 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002270 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002271 }
2272
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002273 /*
2274 * there are two premises:
2275 * a) deliver good data to the caller
2276 * b) correct the bad sectors on disk
2277 */
Ming Leia0b60d72017-12-18 20:22:11 +08002278 if (failed_bio_pages > 1) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002279 /*
2280 * to fulfill b), we need to know the exact failing sectors, as
2281 * we don't want to rewrite any more than the failed ones. thus,
2282 * we need separate read requests for the failed bio
2283 *
2284 * if the following BUG_ON triggers, our validation request got
2285 * merged. we need separate requests for our algorithm to work.
2286 */
2287 BUG_ON(failrec->in_validation);
2288 failrec->in_validation = 1;
2289 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002290 } else {
2291 /*
2292 * we're ready to fulfill a) and b) alongside. get a good copy
2293 * of the failed sector and if we succeed, we have setup
2294 * everything for repair_io_failure to do the rest for us.
2295 */
2296 if (failrec->in_validation) {
2297 BUG_ON(failrec->this_mirror != failed_mirror);
2298 failrec->in_validation = 0;
2299 failrec->this_mirror = 0;
2300 }
2301 failrec->failed_mirror = failed_mirror;
2302 failrec->this_mirror++;
2303 if (failrec->this_mirror == failed_mirror)
2304 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002305 }
2306
Miao Xiefacc8a222013-07-25 19:22:34 +08002307 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002308 btrfs_debug(fs_info,
2309 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2310 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002311 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002312 }
2313
Liu Boc3cfb652017-07-13 15:00:50 -07002314 return true;
Miao Xie2fe63032014-09-12 18:43:59 +08002315}
2316
2317
2318struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2319 struct io_failure_record *failrec,
2320 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002321 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002322{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002323 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002324 struct bio *bio;
2325 struct btrfs_io_bio *btrfs_failed_bio;
2326 struct btrfs_io_bio *btrfs_bio;
2327
David Sterbac5e4c3d2017-06-12 17:29:41 +02002328 bio = btrfs_io_bio_alloc(1);
Miao Xie2fe63032014-09-12 18:43:59 +08002329 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002330 bio->bi_iter.bi_sector = failrec->logical >> 9;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002331 bio_set_dev(bio, fs_info->fs_devices->latest_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002332 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002333 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002334
Miao Xiefacc8a222013-07-25 19:22:34 +08002335 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2336 if (btrfs_failed_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002337 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2338
2339 btrfs_bio = btrfs_io_bio(bio);
2340 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002341 icsum *= csum_size;
2342 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002343 csum_size);
2344 }
2345
Miao Xie2fe63032014-09-12 18:43:59 +08002346 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002347
Miao Xie2fe63032014-09-12 18:43:59 +08002348 return bio;
2349}
2350
2351/*
2352 * this is a generic handler for readpage errors (default
2353 * readpage_io_failed_hook). if other copies exist, read those and write back
2354 * good data to the failed position. does not investigate in remapping the
2355 * failed extent elsewhere, hoping the device will be smart enough to do this as
2356 * needed
2357 */
2358
2359static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2360 struct page *page, u64 start, u64 end,
2361 int failed_mirror)
2362{
2363 struct io_failure_record *failrec;
2364 struct inode *inode = page->mapping->host;
2365 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002366 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie2fe63032014-09-12 18:43:59 +08002367 struct bio *bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002368 int read_mode = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002369 blk_status_t status;
Miao Xie2fe63032014-09-12 18:43:59 +08002370 int ret;
Ming Leia0b60d72017-12-18 20:22:11 +08002371 unsigned failed_bio_pages = bio_pages_all(failed_bio);
Miao Xie2fe63032014-09-12 18:43:59 +08002372
Mike Christie1f7ad752016-06-05 14:31:51 -05002373 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002374
2375 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2376 if (ret)
2377 return ret;
2378
Ming Leia0b60d72017-12-18 20:22:11 +08002379 if (!btrfs_check_repairable(inode, failed_bio_pages, failrec,
Liu Boc3cfb652017-07-13 15:00:50 -07002380 failed_mirror)) {
Josef Bacik7870d082017-05-05 11:57:15 -04002381 free_io_failure(failure_tree, tree, failrec);
Miao Xie2fe63032014-09-12 18:43:59 +08002382 return -EIO;
2383 }
2384
Ming Leia0b60d72017-12-18 20:22:11 +08002385 if (failed_bio_pages > 1)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002386 read_mode |= REQ_FAILFAST_DEV;
Miao Xie2fe63032014-09-12 18:43:59 +08002387
2388 phy_offset >>= inode->i_sb->s_blocksize_bits;
2389 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2390 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002391 (int)phy_offset, failed_bio->bi_end_io,
2392 NULL);
David Sterbaebcc3262018-06-29 10:56:53 +02002393 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie2fe63032014-09-12 18:43:59 +08002394
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002395 btrfs_debug(btrfs_sb(inode->i_sb),
2396 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2397 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002398
Linus Torvalds8c27cb32017-07-05 16:41:23 -07002399 status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002400 failrec->bio_flags, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002401 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04002402 free_io_failure(failure_tree, tree, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002403 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002404 ret = blk_status_to_errno(status);
Miao Xie6c387ab2014-09-12 18:43:57 +08002405 }
2406
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002407 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002408}
2409
Chris Masond1310b22008-01-24 16:13:08 -05002410/* lots and lots of room for performance fixes in the end_bio funcs */
2411
David Sterbab5227c02015-12-03 13:08:59 +01002412void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002413{
2414 int uptodate = (err == 0);
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002415 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002416
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02002417 btrfs_writepage_endio_finish_ordered(page, start, end, NULL, uptodate);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002418
Jeff Mahoney87826df2012-02-15 16:23:57 +01002419 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002420 ClearPageUptodate(page);
2421 SetPageError(page);
Colin Ian Kingbff5baf2017-05-09 18:14:01 +01002422 ret = err < 0 ? err : -EIO;
Liu Bo5dca6ee2014-05-12 12:47:36 +08002423 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002424 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002425}
2426
Chris Masond1310b22008-01-24 16:13:08 -05002427/*
2428 * after a writepage IO is done, we need to:
2429 * clear the uptodate bits on error
2430 * clear the writeback bits in the extent tree for this IO
2431 * end_page_writeback if the page has no more pending IO
2432 *
2433 * Scheduling is not allowed, so the extent state tree is expected
2434 * to have one and only one object corresponding to this IO.
2435 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002436static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002437{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002438 int error = blk_status_to_errno(bio->bi_status);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002439 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002440 u64 start;
2441 u64 end;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002442 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002443
David Sterbac09abff2017-07-13 18:10:07 +02002444 ASSERT(!bio_flagged(bio, BIO_CLONED));
Kent Overstreet2c30c712013-11-07 12:20:26 -08002445 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002446 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002447 struct inode *inode = page->mapping->host;
2448 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002449
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002450 /* We always issue full-page reads, but if some block
2451 * in a page fails to read, blk_update_request() will
2452 * advance bv_offset and adjust bv_len to compensate.
2453 * Print a warning for nonzero offsets, and an error
2454 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002455 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2456 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002457 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002458 "partial page write in btrfs with offset %u and length %u",
2459 bvec->bv_offset, bvec->bv_len);
2460 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002461 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002462 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002463 bvec->bv_offset, bvec->bv_len);
2464 }
Chris Masond1310b22008-01-24 16:13:08 -05002465
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002466 start = page_offset(page);
2467 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002468
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002469 end_extent_writepage(page, error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002470 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002471 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002472
Chris Masond1310b22008-01-24 16:13:08 -05002473 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002474}
2475
Miao Xie883d0de2013-07-25 19:22:35 +08002476static void
2477endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2478 int uptodate)
2479{
2480 struct extent_state *cached = NULL;
2481 u64 end = start + len - 1;
2482
2483 if (uptodate && tree->track_uptodate)
2484 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
David Sterbad810a4b2017-12-07 18:52:54 +01002485 unlock_extent_cached_atomic(tree, start, end, &cached);
Miao Xie883d0de2013-07-25 19:22:35 +08002486}
2487
Chris Masond1310b22008-01-24 16:13:08 -05002488/*
2489 * after a readpage IO is done, we need to:
2490 * clear the uptodate bits on error
2491 * set the uptodate bits if things worked
2492 * set the page up to date if all extents in the tree are uptodate
2493 * clear the lock bit in the extent tree
2494 * unlock the page if there are no other extents locked for it
2495 *
2496 * Scheduling is not allowed, so the extent state tree is expected
2497 * to have one and only one object corresponding to this IO.
2498 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002499static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002500{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002501 struct bio_vec *bvec;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002502 int uptodate = !bio->bi_status;
Miao Xiefacc8a222013-07-25 19:22:34 +08002503 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04002504 struct extent_io_tree *tree, *failure_tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002505 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002506 u64 start;
2507 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002508 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002509 u64 extent_start = 0;
2510 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002511 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002512 int ret;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002513 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002514
David Sterbac09abff2017-07-13 18:10:07 +02002515 ASSERT(!bio_flagged(bio, BIO_CLONED));
Kent Overstreet2c30c712013-11-07 12:20:26 -08002516 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002517 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002518 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002519 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen507903b2011-04-06 10:02:20 +00002520
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002521 btrfs_debug(fs_info,
2522 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002523 (u64)bio->bi_iter.bi_sector, bio->bi_status,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002524 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002525 tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002526 failure_tree = &BTRFS_I(inode)->io_failure_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002527
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002528 /* We always issue full-page reads, but if some block
2529 * in a page fails to read, blk_update_request() will
2530 * advance bv_offset and adjust bv_len to compensate.
2531 * Print a warning for nonzero offsets, and an error
2532 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002533 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2534 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002535 btrfs_err(fs_info,
2536 "partial page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002537 bvec->bv_offset, bvec->bv_len);
2538 else
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002539 btrfs_info(fs_info,
2540 "incomplete page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002541 bvec->bv_offset, bvec->bv_len);
2542 }
Chris Masond1310b22008-01-24 16:13:08 -05002543
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002544 start = page_offset(page);
2545 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002546 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002547
Chris Mason9be33952013-05-17 18:30:14 -04002548 mirror = io_bio->mirror_num;
David Sterba20c98012017-02-17 15:59:35 +01002549 if (likely(uptodate && tree->ops)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002550 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2551 page, start, end,
2552 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002553 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002554 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002555 else
Josef Bacik7870d082017-05-05 11:57:15 -04002556 clean_io_failure(BTRFS_I(inode)->root->fs_info,
2557 failure_tree, tree, start,
2558 page,
2559 btrfs_ino(BTRFS_I(inode)), 0);
Chris Masond1310b22008-01-24 16:13:08 -05002560 }
Josef Bacikea466792012-03-26 21:57:36 -04002561
Miao Xief2a09da2013-07-25 19:22:33 +08002562 if (likely(uptodate))
2563 goto readpage_ok;
2564
David Sterba20a7db82017-02-17 16:24:29 +01002565 if (tree->ops) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002566 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Liu Bo9d0d1c82017-03-24 15:04:50 -07002567 if (ret == -EAGAIN) {
2568 /*
2569 * Data inode's readpage_io_failed_hook() always
2570 * returns -EAGAIN.
2571 *
2572 * The generic bio_readpage_error handles errors
2573 * the following way: If possible, new read
2574 * requests are created and submitted and will
2575 * end up in end_bio_extent_readpage as well (if
2576 * we're lucky, not in the !uptodate case). In
2577 * that case it returns 0 and we just go on with
2578 * the next page in our bio. If it can't handle
2579 * the error it will return -EIO and we remain
2580 * responsible for that page.
2581 */
2582 ret = bio_readpage_error(bio, offset, page,
2583 start, end, mirror);
2584 if (ret == 0) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002585 uptodate = !bio->bi_status;
Liu Bo9d0d1c82017-03-24 15:04:50 -07002586 offset += len;
2587 continue;
2588 }
Chris Mason7e383262008-04-09 16:28:12 -04002589 }
Liu Bo9d0d1c82017-03-24 15:04:50 -07002590
2591 /*
2592 * metadata's readpage_io_failed_hook() always returns
2593 * -EIO and fixes nothing. -EIO is also returned if
2594 * data inode error could not be fixed.
2595 */
2596 ASSERT(ret == -EIO);
Chris Mason7e383262008-04-09 16:28:12 -04002597 }
Miao Xief2a09da2013-07-25 19:22:33 +08002598readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002599 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002600 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002601 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002602 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002603
2604 /* Zero out the end if this page straddles i_size */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002605 off = i_size & (PAGE_SIZE-1);
Liu Boa583c022014-08-19 23:32:22 +08002606 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002607 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002608 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002609 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002610 ClearPageUptodate(page);
2611 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002612 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002613 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002614 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002615
2616 if (unlikely(!uptodate)) {
2617 if (extent_len) {
2618 endio_readpage_release_extent(tree,
2619 extent_start,
2620 extent_len, 1);
2621 extent_start = 0;
2622 extent_len = 0;
2623 }
2624 endio_readpage_release_extent(tree, start,
2625 end - start + 1, 0);
2626 } else if (!extent_len) {
2627 extent_start = start;
2628 extent_len = end + 1 - start;
2629 } else if (extent_start + extent_len == start) {
2630 extent_len += end + 1 - start;
2631 } else {
2632 endio_readpage_release_extent(tree, extent_start,
2633 extent_len, uptodate);
2634 extent_start = start;
2635 extent_len = end + 1 - start;
2636 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002637 }
Chris Masond1310b22008-01-24 16:13:08 -05002638
Miao Xie883d0de2013-07-25 19:22:35 +08002639 if (extent_len)
2640 endio_readpage_release_extent(tree, extent_start, extent_len,
2641 uptodate);
Miao Xiefacc8a222013-07-25 19:22:34 +08002642 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002643 io_bio->end_io(io_bio, blk_status_to_errno(bio->bi_status));
Chris Masond1310b22008-01-24 16:13:08 -05002644 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002645}
2646
Chris Mason9be33952013-05-17 18:30:14 -04002647/*
David Sterba184f9992017-06-12 17:29:39 +02002648 * Initialize the members up to but not including 'bio'. Use after allocating a
2649 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
2650 * 'bio' because use of __GFP_ZERO is not supported.
Chris Mason9be33952013-05-17 18:30:14 -04002651 */
David Sterba184f9992017-06-12 17:29:39 +02002652static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
Chris Masond1310b22008-01-24 16:13:08 -05002653{
David Sterba184f9992017-06-12 17:29:39 +02002654 memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
2655}
2656
2657/*
David Sterba6e707bc2017-06-02 17:26:26 +02002658 * The following helpers allocate a bio. As it's backed by a bioset, it'll
2659 * never fail. We're returning a bio right now but you can call btrfs_io_bio
2660 * for the appropriate container_of magic
Chris Masond1310b22008-01-24 16:13:08 -05002661 */
David Sterbac821e7f32017-06-02 18:35:36 +02002662struct bio *btrfs_bio_alloc(struct block_device *bdev, u64 first_byte)
Chris Masond1310b22008-01-24 16:13:08 -05002663{
2664 struct bio *bio;
2665
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002666 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset);
Christoph Hellwig74d46992017-08-23 19:10:32 +02002667 bio_set_dev(bio, bdev);
David Sterbac821e7f32017-06-02 18:35:36 +02002668 bio->bi_iter.bi_sector = first_byte >> 9;
David Sterba184f9992017-06-12 17:29:39 +02002669 btrfs_io_bio_init(btrfs_io_bio(bio));
Chris Masond1310b22008-01-24 16:13:08 -05002670 return bio;
2671}
2672
David Sterba8b6c1d52017-06-02 17:48:13 +02002673struct bio *btrfs_bio_clone(struct bio *bio)
Chris Mason9be33952013-05-17 18:30:14 -04002674{
Miao Xie23ea8e52014-09-12 18:43:54 +08002675 struct btrfs_io_bio *btrfs_bio;
2676 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002677
David Sterba6e707bc2017-06-02 17:26:26 +02002678 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002679 new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset);
David Sterba6e707bc2017-06-02 17:26:26 +02002680 btrfs_bio = btrfs_io_bio(new);
David Sterba184f9992017-06-12 17:29:39 +02002681 btrfs_io_bio_init(btrfs_bio);
David Sterba6e707bc2017-06-02 17:26:26 +02002682 btrfs_bio->iter = bio->bi_iter;
Miao Xie23ea8e52014-09-12 18:43:54 +08002683 return new;
2684}
Chris Mason9be33952013-05-17 18:30:14 -04002685
David Sterbac5e4c3d2017-06-12 17:29:41 +02002686struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
Chris Mason9be33952013-05-17 18:30:14 -04002687{
Miao Xiefacc8a222013-07-25 19:22:34 +08002688 struct bio *bio;
2689
David Sterba6e707bc2017-06-02 17:26:26 +02002690 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002691 bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
David Sterba184f9992017-06-12 17:29:39 +02002692 btrfs_io_bio_init(btrfs_io_bio(bio));
Miao Xiefacc8a222013-07-25 19:22:34 +08002693 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002694}
2695
Liu Boe4770942017-05-16 10:57:14 -07002696struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
Liu Bo2f8e9142017-05-15 17:43:31 -07002697{
2698 struct bio *bio;
2699 struct btrfs_io_bio *btrfs_bio;
2700
2701 /* this will never fail when it's backed by a bioset */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002702 bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset);
Liu Bo2f8e9142017-05-15 17:43:31 -07002703 ASSERT(bio);
2704
2705 btrfs_bio = btrfs_io_bio(bio);
David Sterba184f9992017-06-12 17:29:39 +02002706 btrfs_io_bio_init(btrfs_bio);
Liu Bo2f8e9142017-05-15 17:43:31 -07002707
2708 bio_trim(bio, offset >> 9, size >> 9);
Liu Bo17347ce2017-05-15 15:33:27 -07002709 btrfs_bio->iter = bio->bi_iter;
Liu Bo2f8e9142017-05-15 17:43:31 -07002710 return bio;
2711}
Chris Mason9be33952013-05-17 18:30:14 -04002712
Mike Christie1f7ad752016-06-05 14:31:51 -05002713static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
2714 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002715{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002716 blk_status_t ret = 0;
Ming Leic45a8f22017-12-18 20:22:05 +08002717 struct bio_vec *bvec = bio_last_bvec_all(bio);
Chris Mason70dec802008-01-29 09:59:12 -05002718 struct page *page = bvec->bv_page;
2719 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002720 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002721
Miao Xie4eee4fa2012-12-21 09:17:45 +00002722 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002723
David Woodhouse902b22f2008-08-20 08:51:49 -04002724 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002725
David Sterba20c98012017-02-17 15:59:35 +01002726 if (tree->ops)
Josef Bacikc6100a42017-05-05 11:57:13 -04002727 ret = tree->ops->submit_bio_hook(tree->private_data, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002728 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002729 else
Mike Christie4e49ea42016-06-05 14:31:41 -05002730 btrfsic_submit_bio(bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002731
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002732 return blk_status_to_errno(ret);
Chris Masond1310b22008-01-24 16:13:08 -05002733}
2734
David Sterba4b81ba42017-06-06 19:14:26 +02002735/*
2736 * @opf: bio REQ_OP_* and REQ_* flags as one value
David Sterbab8b3d622017-06-12 19:50:41 +02002737 * @tree: tree so we can call our merge_bio hook
2738 * @wbc: optional writeback control for io accounting
2739 * @page: page to add to the bio
2740 * @pg_offset: offset of the new bio or to check whether we are adding
2741 * a contiguous page to the previous one
2742 * @size: portion of page that we want to write
2743 * @offset: starting offset in the page
2744 * @bdev: attach newly created bios to this bdev
David Sterba5c2b1fd2017-06-06 19:22:55 +02002745 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
David Sterbab8b3d622017-06-12 19:50:41 +02002746 * @end_io_func: end_io callback for new bio
2747 * @mirror_num: desired mirror to read/write
2748 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
2749 * @bio_flags: flags of the current bio to see if we can merge them
David Sterba4b81ba42017-06-06 19:14:26 +02002750 */
2751static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002752 struct writeback_control *wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02002753 struct page *page, u64 offset,
David Sterba6c5a4e22017-10-04 17:10:34 +02002754 size_t size, unsigned long pg_offset,
Chris Masond1310b22008-01-24 16:13:08 -05002755 struct block_device *bdev,
2756 struct bio **bio_ret,
Chris Masonf1885912008-04-09 16:28:12 -04002757 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002758 int mirror_num,
2759 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002760 unsigned long bio_flags,
2761 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002762{
2763 int ret = 0;
2764 struct bio *bio;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002765 size_t page_size = min_t(size_t, size, PAGE_SIZE);
David Sterba6273b7f2017-10-04 17:30:11 +02002766 sector_t sector = offset >> 9;
Chris Masond1310b22008-01-24 16:13:08 -05002767
David Sterba5c2b1fd2017-06-06 19:22:55 +02002768 ASSERT(bio_ret);
2769
2770 if (*bio_ret) {
David Sterba0c8508a2017-06-12 20:00:43 +02002771 bool contig;
2772 bool can_merge = true;
2773
Chris Masond1310b22008-01-24 16:13:08 -05002774 bio = *bio_ret;
David Sterba0c8508a2017-06-12 20:00:43 +02002775 if (prev_bio_flags & EXTENT_BIO_COMPRESSED)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002776 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002777 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002778 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002779
David Sterba00032d32018-07-18 19:28:09 +02002780 if (tree->ops && btrfs_merge_bio_hook(page, offset, page_size,
2781 bio, bio_flags))
David Sterba0c8508a2017-06-12 20:00:43 +02002782 can_merge = false;
2783
2784 if (prev_bio_flags != bio_flags || !contig || !can_merge ||
Filipe Manana005efed2015-09-14 09:09:31 +01002785 force_bio_submit ||
David Sterba6c5a4e22017-10-04 17:10:34 +02002786 bio_add_page(bio, page, page_size, pg_offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002787 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002788 if (ret < 0) {
2789 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002790 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002791 }
Chris Masond1310b22008-01-24 16:13:08 -05002792 bio = NULL;
2793 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002794 if (wbc)
2795 wbc_account_io(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002796 return 0;
2797 }
2798 }
Chris Masonc8b97812008-10-29 14:49:59 -04002799
David Sterba6273b7f2017-10-04 17:30:11 +02002800 bio = btrfs_bio_alloc(bdev, offset);
David Sterba6c5a4e22017-10-04 17:10:34 +02002801 bio_add_page(bio, page, page_size, pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002802 bio->bi_end_io = end_io_func;
2803 bio->bi_private = tree;
Jens Axboee6959b92017-06-27 11:51:28 -06002804 bio->bi_write_hint = page->mapping->host->i_write_hint;
David Sterba4b81ba42017-06-06 19:14:26 +02002805 bio->bi_opf = opf;
Chris Masonda2f0f72015-07-02 13:57:22 -07002806 if (wbc) {
2807 wbc_init_bio(wbc, bio);
2808 wbc_account_io(wbc, page, page_size);
2809 }
Chris Mason70dec802008-01-29 09:59:12 -05002810
David Sterba5c2b1fd2017-06-06 19:22:55 +02002811 *bio_ret = bio;
Chris Masond1310b22008-01-24 16:13:08 -05002812
2813 return ret;
2814}
2815
Eric Sandeen48a3b632013-04-25 20:41:01 +00002816static void attach_extent_buffer_page(struct extent_buffer *eb,
2817 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002818{
2819 if (!PagePrivate(page)) {
2820 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002821 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002822 set_page_private(page, (unsigned long)eb);
2823 } else {
2824 WARN_ON(page->private != (unsigned long)eb);
2825 }
2826}
2827
Chris Masond1310b22008-01-24 16:13:08 -05002828void set_page_extent_mapped(struct page *page)
2829{
2830 if (!PagePrivate(page)) {
2831 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002832 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002833 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002834 }
2835}
2836
Miao Xie125bac012013-07-25 19:22:37 +08002837static struct extent_map *
2838__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2839 u64 start, u64 len, get_extent_t *get_extent,
2840 struct extent_map **em_cached)
2841{
2842 struct extent_map *em;
2843
2844 if (em_cached && *em_cached) {
2845 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002846 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002847 start < extent_map_end(em)) {
Elena Reshetova490b54d2017-03-03 10:55:12 +02002848 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002849 return em;
2850 }
2851
2852 free_extent_map(em);
2853 *em_cached = NULL;
2854 }
2855
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02002856 em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
Miao Xie125bac012013-07-25 19:22:37 +08002857 if (em_cached && !IS_ERR_OR_NULL(em)) {
2858 BUG_ON(*em_cached);
Elena Reshetova490b54d2017-03-03 10:55:12 +02002859 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002860 *em_cached = em;
2861 }
2862 return em;
2863}
Chris Masond1310b22008-01-24 16:13:08 -05002864/*
2865 * basic readpage implementation. Locked extent state structs are inserted
2866 * into the tree that are removed when the IO is done (by the end_io
2867 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002868 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07002869 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05002870 */
Miao Xie99740902013-07-25 19:22:36 +08002871static int __do_readpage(struct extent_io_tree *tree,
2872 struct page *page,
2873 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002874 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002875 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02002876 unsigned long *bio_flags, unsigned int read_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002877 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05002878{
2879 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002880 u64 start = page_offset(page);
David Sterba8eec8292017-06-06 19:50:13 +02002881 const u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002882 u64 cur = start;
2883 u64 extent_offset;
2884 u64 last_byte = i_size_read(inode);
2885 u64 block_start;
2886 u64 cur_end;
Chris Masond1310b22008-01-24 16:13:08 -05002887 struct extent_map *em;
2888 struct block_device *bdev;
Liu Bobaf863b2016-07-11 10:39:07 -07002889 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002890 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002891 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002892 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002893 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002894 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00002895 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002896
2897 set_page_extent_mapped(page);
2898
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002899 if (!PageUptodate(page)) {
2900 if (cleancache_get_page(page) == 0) {
2901 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08002902 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002903 goto out;
2904 }
2905 }
2906
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002907 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04002908 char *userpage;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002909 size_t zero_offset = last_byte & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002910
2911 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002912 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002913 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002914 memset(userpage + zero_offset, 0, iosize);
2915 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002916 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002917 }
2918 }
Chris Masond1310b22008-01-24 16:13:08 -05002919 while (cur <= end) {
Filipe Manana005efed2015-09-14 09:09:31 +01002920 bool force_bio_submit = false;
David Sterba6273b7f2017-10-04 17:30:11 +02002921 u64 offset;
Josef Bacikc8f2f242013-02-11 11:33:00 -05002922
Chris Masond1310b22008-01-24 16:13:08 -05002923 if (cur >= last_byte) {
2924 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002925 struct extent_state *cached = NULL;
2926
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002927 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002928 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002929 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002930 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002931 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002932 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002933 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00002934 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01002935 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05002936 break;
2937 }
Miao Xie125bac012013-07-25 19:22:37 +08002938 em = __get_extent_map(inode, page, pg_offset, cur,
2939 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02002940 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002941 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00002942 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002943 break;
2944 }
Chris Masond1310b22008-01-24 16:13:08 -05002945 extent_offset = cur - em->start;
2946 BUG_ON(extent_map_end(em) <= cur);
2947 BUG_ON(end < cur);
2948
Li Zefan261507a02010-12-17 14:21:50 +08002949 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07002950 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002951 extent_set_compress_type(&this_bio_flag,
2952 em->compress_type);
2953 }
Chris Masonc8b97812008-10-29 14:49:59 -04002954
Chris Masond1310b22008-01-24 16:13:08 -05002955 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2956 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002957 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002958 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2959 disk_io_size = em->block_len;
David Sterba6273b7f2017-10-04 17:30:11 +02002960 offset = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002961 } else {
David Sterba6273b7f2017-10-04 17:30:11 +02002962 offset = em->block_start + extent_offset;
Chris Masonc8b97812008-10-29 14:49:59 -04002963 disk_io_size = iosize;
2964 }
Chris Masond1310b22008-01-24 16:13:08 -05002965 bdev = em->bdev;
2966 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002967 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2968 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01002969
2970 /*
2971 * If we have a file range that points to a compressed extent
2972 * and it's followed by a consecutive file range that points to
2973 * to the same compressed extent (possibly with a different
2974 * offset and/or length, so it either points to the whole extent
2975 * or only part of it), we must make sure we do not submit a
2976 * single bio to populate the pages for the 2 ranges because
2977 * this makes the compressed extent read zero out the pages
2978 * belonging to the 2nd range. Imagine the following scenario:
2979 *
2980 * File layout
2981 * [0 - 8K] [8K - 24K]
2982 * | |
2983 * | |
2984 * points to extent X, points to extent X,
2985 * offset 4K, length of 8K offset 0, length 16K
2986 *
2987 * [extent X, compressed length = 4K uncompressed length = 16K]
2988 *
2989 * If the bio to read the compressed extent covers both ranges,
2990 * it will decompress extent X into the pages belonging to the
2991 * first range and then it will stop, zeroing out the remaining
2992 * pages that belong to the other range that points to extent X.
2993 * So here we make sure we submit 2 bios, one for the first
2994 * range and another one for the third range. Both will target
2995 * the same physical extent from disk, but we can't currently
2996 * make the compressed bio endio callback populate the pages
2997 * for both ranges because each compressed bio is tightly
2998 * coupled with a single extent map, and each range can have
2999 * an extent map with a different offset value relative to the
3000 * uncompressed data of our extent and different lengths. This
3001 * is a corner case so we prioritize correctness over
3002 * non-optimal behavior (submitting 2 bios for the same extent).
3003 */
3004 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3005 prev_em_start && *prev_em_start != (u64)-1 &&
3006 *prev_em_start != em->orig_start)
3007 force_bio_submit = true;
3008
3009 if (prev_em_start)
3010 *prev_em_start = em->orig_start;
3011
Chris Masond1310b22008-01-24 16:13:08 -05003012 free_extent_map(em);
3013 em = NULL;
3014
3015 /* we've found a hole, just zero and go on */
3016 if (block_start == EXTENT_MAP_HOLE) {
3017 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003018 struct extent_state *cached = NULL;
3019
Cong Wang7ac687d2011-11-25 23:14:28 +08003020 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003021 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003022 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003023 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003024
3025 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003026 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003027 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01003028 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003029 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003030 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003031 continue;
3032 }
3033 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003034 if (test_range_bit(tree, cur, cur_end,
3035 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003036 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003037 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003038 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003039 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003040 continue;
3041 }
Chris Mason70dec802008-01-29 09:59:12 -05003042 /* we have an inline extent but it didn't get marked up
3043 * to date. Error out
3044 */
3045 if (block_start == EXTENT_MAP_INLINE) {
3046 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003047 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003048 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003049 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003050 continue;
3051 }
Chris Masond1310b22008-01-24 16:13:08 -05003052
David Sterba4b81ba42017-06-06 19:14:26 +02003053 ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL,
David Sterba6273b7f2017-10-04 17:30:11 +02003054 page, offset, disk_io_size,
3055 pg_offset, bdev, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003056 end_bio_extent_readpage, mirror_num,
3057 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003058 this_bio_flag,
3059 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003060 if (!ret) {
3061 nr++;
3062 *bio_flags = this_bio_flag;
3063 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003064 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003065 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003066 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003067 }
Chris Masond1310b22008-01-24 16:13:08 -05003068 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003069 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003070 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003071out:
Chris Masond1310b22008-01-24 16:13:08 -05003072 if (!nr) {
3073 if (!PageError(page))
3074 SetPageUptodate(page);
3075 unlock_page(page);
3076 }
Liu Bobaf863b2016-07-11 10:39:07 -07003077 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003078}
3079
Miao Xie99740902013-07-25 19:22:36 +08003080static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
3081 struct page *pages[], int nr_pages,
3082 u64 start, u64 end,
Miao Xie125bac012013-07-25 19:22:37 +08003083 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003084 struct bio **bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003085 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003086 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003087{
3088 struct inode *inode;
3089 struct btrfs_ordered_extent *ordered;
3090 int index;
3091
3092 inode = pages[0]->mapping->host;
3093 while (1) {
3094 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003095 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Miao Xie99740902013-07-25 19:22:36 +08003096 end - start + 1);
3097 if (!ordered)
3098 break;
3099 unlock_extent(tree, start, end);
3100 btrfs_start_ordered_extent(inode, ordered, 1);
3101 btrfs_put_ordered_extent(ordered);
3102 }
3103
3104 for (index = 0; index < nr_pages; index++) {
David Sterba4ef77692017-06-23 04:09:57 +02003105 __do_readpage(tree, pages[index], btrfs_get_extent, em_cached,
Jens Axboe5e9d3982018-08-17 15:45:39 -07003106 bio, 0, bio_flags, REQ_RAHEAD, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003107 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003108 }
3109}
3110
3111static void __extent_readpages(struct extent_io_tree *tree,
3112 struct page *pages[],
David Sterbae4d17ef2017-06-23 04:09:57 +02003113 int nr_pages,
Miao Xie125bac012013-07-25 19:22:37 +08003114 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003115 struct bio **bio, unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003116 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003117{
Stefan Behrens35a36212013-08-14 18:12:25 +02003118 u64 start = 0;
Miao Xie99740902013-07-25 19:22:36 +08003119 u64 end = 0;
3120 u64 page_start;
3121 int index;
Stefan Behrens35a36212013-08-14 18:12:25 +02003122 int first_index = 0;
Miao Xie99740902013-07-25 19:22:36 +08003123
3124 for (index = 0; index < nr_pages; index++) {
3125 page_start = page_offset(pages[index]);
3126 if (!end) {
3127 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003128 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003129 first_index = index;
3130 } else if (end + 1 == page_start) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003131 end += PAGE_SIZE;
Miao Xie99740902013-07-25 19:22:36 +08003132 } else {
3133 __do_contiguous_readpages(tree, &pages[first_index],
3134 index - first_index, start,
David Sterba4ef77692017-06-23 04:09:57 +02003135 end, em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003136 bio, bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05003137 prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003138 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003139 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003140 first_index = index;
3141 }
3142 }
3143
3144 if (end)
3145 __do_contiguous_readpages(tree, &pages[first_index],
3146 index - first_index, start,
David Sterba4ef77692017-06-23 04:09:57 +02003147 end, em_cached, bio,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003148 bio_flags, prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003149}
3150
3151static int __extent_read_full_page(struct extent_io_tree *tree,
3152 struct page *page,
3153 get_extent_t *get_extent,
3154 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003155 unsigned long *bio_flags,
3156 unsigned int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003157{
3158 struct inode *inode = page->mapping->host;
3159 struct btrfs_ordered_extent *ordered;
3160 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003161 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003162 int ret;
3163
3164 while (1) {
3165 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003166 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003167 PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003168 if (!ordered)
3169 break;
3170 unlock_extent(tree, start, end);
3171 btrfs_start_ordered_extent(inode, ordered, 1);
3172 btrfs_put_ordered_extent(ordered);
3173 }
3174
Miao Xie125bac012013-07-25 19:22:37 +08003175 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003176 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003177 return ret;
3178}
3179
Chris Masond1310b22008-01-24 16:13:08 -05003180int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003181 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003182{
3183 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003184 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003185 int ret;
3186
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003187 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003188 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003189 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003190 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003191 return ret;
3192}
Chris Masond1310b22008-01-24 16:13:08 -05003193
David Sterba3d4b9492017-02-10 19:33:41 +01003194static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003195 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003196{
3197 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003198}
3199
Chris Masond1310b22008-01-24 16:13:08 -05003200/*
Chris Mason40f76582014-05-21 13:35:51 -07003201 * helper for __extent_writepage, doing all of the delayed allocation setup.
3202 *
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003203 * This returns 1 if btrfs_run_delalloc_range function did all the work required
Chris Mason40f76582014-05-21 13:35:51 -07003204 * to write the page (copy into inline extent). In this case the IO has
3205 * been started and the page is already unlocked.
3206 *
3207 * This returns 0 if all went well (page still locked)
3208 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003209 */
Chris Mason40f76582014-05-21 13:35:51 -07003210static noinline_for_stack int writepage_delalloc(struct inode *inode,
3211 struct page *page, struct writeback_control *wbc,
3212 struct extent_page_data *epd,
3213 u64 delalloc_start,
3214 unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003215{
Chris Mason40f76582014-05-21 13:35:51 -07003216 struct extent_io_tree *tree = epd->tree;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003217 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003218 u64 nr_delalloc;
3219 u64 delalloc_to_write = 0;
3220 u64 delalloc_end = 0;
3221 int ret;
3222 int page_started = 0;
3223
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003224 if (epd->extent_locked)
Chris Mason40f76582014-05-21 13:35:51 -07003225 return 0;
3226
3227 while (delalloc_end < page_end) {
3228 nr_delalloc = find_lock_delalloc_range(inode, tree,
3229 page,
3230 &delalloc_start,
3231 &delalloc_end,
Josef Bacikdcab6a32015-02-11 15:08:59 -05003232 BTRFS_MAX_EXTENT_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003233 if (nr_delalloc == 0) {
3234 delalloc_start = delalloc_end + 1;
3235 continue;
3236 }
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003237 ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
3238 delalloc_end, &page_started, nr_written, wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003239 /* File system has been set read-only */
3240 if (ret) {
3241 SetPageError(page);
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003242 /*
3243 * btrfs_run_delalloc_range should return < 0 for error
3244 * but just in case, we use > 0 here meaning the IO is
3245 * started, so we don't want to return > 0 unless
3246 * things are going well.
Chris Mason40f76582014-05-21 13:35:51 -07003247 */
3248 ret = ret < 0 ? ret : -EIO;
3249 goto done;
3250 }
3251 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003252 * delalloc_end is already one less than the total length, so
3253 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003254 */
3255 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003256 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003257 delalloc_start = delalloc_end + 1;
3258 }
3259 if (wbc->nr_to_write < delalloc_to_write) {
3260 int thresh = 8192;
3261
3262 if (delalloc_to_write < thresh * 2)
3263 thresh = delalloc_to_write;
3264 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3265 thresh);
3266 }
3267
3268 /* did the fill delalloc function already unlock and start
3269 * the IO?
3270 */
3271 if (page_started) {
3272 /*
3273 * we've unlocked the page, so we can't update
3274 * the mapping's writeback index, just update
3275 * nr_to_write.
3276 */
3277 wbc->nr_to_write -= *nr_written;
3278 return 1;
3279 }
3280
3281 ret = 0;
3282
3283done:
3284 return ret;
3285}
3286
3287/*
3288 * helper for __extent_writepage. This calls the writepage start hooks,
3289 * and does the loop to map the page into extents and bios.
3290 *
3291 * We return 1 if the IO is started and the page is unlocked,
3292 * 0 if all went well (page still locked)
3293 * < 0 if there were errors (page still locked)
3294 */
3295static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3296 struct page *page,
3297 struct writeback_control *wbc,
3298 struct extent_page_data *epd,
3299 loff_t i_size,
3300 unsigned long nr_written,
David Sterbaf1c77c52017-06-06 19:03:49 +02003301 unsigned int write_flags, int *nr_ret)
Chris Mason40f76582014-05-21 13:35:51 -07003302{
Chris Masond1310b22008-01-24 16:13:08 -05003303 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003304 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003305 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003306 u64 end;
3307 u64 cur = start;
3308 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003309 u64 block_start;
3310 u64 iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003311 struct extent_map *em;
3312 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003313 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003314 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003315 int ret = 0;
3316 int nr = 0;
3317 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003318
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003319 ret = btrfs_writepage_cow_fixup(page, start, page_end);
3320 if (ret) {
3321 /* Fixup worker will requeue */
3322 if (ret == -EBUSY)
3323 wbc->pages_skipped++;
3324 else
3325 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003326
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003327 update_nr_written(wbc, nr_written);
3328 unlock_page(page);
3329 return 1;
Chris Mason247e7432008-07-17 12:53:51 -04003330 }
3331
Chris Mason11c83492009-04-20 15:50:09 -04003332 /*
3333 * we don't want to touch the inode after unlocking the page,
3334 * so we update the mapping writeback index now
3335 */
David Sterba3d4b9492017-02-10 19:33:41 +01003336 update_nr_written(wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003337
Chris Masond1310b22008-01-24 16:13:08 -05003338 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003339 if (i_size <= start) {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003340 btrfs_writepage_endio_finish_ordered(page, start, page_end,
3341 NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003342 goto done;
3343 }
3344
Chris Masond1310b22008-01-24 16:13:08 -05003345 blocksize = inode->i_sb->s_blocksize;
3346
3347 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003348 u64 em_end;
David Sterba6273b7f2017-10-04 17:30:11 +02003349 u64 offset;
David Sterba58409ed2016-05-04 11:46:10 +02003350
Chris Mason40f76582014-05-21 13:35:51 -07003351 if (cur >= i_size) {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003352 btrfs_writepage_endio_finish_ordered(page, cur,
3353 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003354 break;
3355 }
David Sterba3c98c622017-06-23 04:01:08 +02003356 em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003357 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003358 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003359 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003360 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003361 break;
3362 }
3363
3364 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003365 em_end = extent_map_end(em);
3366 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003367 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003368 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003369 iosize = ALIGN(iosize, blocksize);
David Sterba6273b7f2017-10-04 17:30:11 +02003370 offset = em->block_start + extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003371 bdev = em->bdev;
3372 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003373 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003374 free_extent_map(em);
3375 em = NULL;
3376
Chris Masonc8b97812008-10-29 14:49:59 -04003377 /*
3378 * compressed and inline extents are written through other
3379 * paths in the FS
3380 */
3381 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003382 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003383 /*
3384 * end_io notification does not happen here for
3385 * compressed extents
3386 */
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003387 if (!compressed)
3388 btrfs_writepage_endio_finish_ordered(page, cur,
3389 cur + iosize - 1,
3390 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003391 else if (compressed) {
3392 /* we don't want to end_page_writeback on
3393 * a compressed extent. this happens
3394 * elsewhere
3395 */
3396 nr++;
3397 }
3398
3399 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003400 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003401 continue;
3402 }
Chris Masonc8b97812008-10-29 14:49:59 -04003403
David Sterba5cdc84b2018-07-18 20:32:52 +02003404 btrfs_set_range_writeback(tree, cur, cur + iosize - 1);
David Sterba58409ed2016-05-04 11:46:10 +02003405 if (!PageWriteback(page)) {
3406 btrfs_err(BTRFS_I(inode)->root->fs_info,
3407 "page %lu not writeback, cur %llu end %llu",
3408 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003409 }
David Sterba58409ed2016-05-04 11:46:10 +02003410
David Sterba4b81ba42017-06-06 19:14:26 +02003411 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003412 page, offset, iosize, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003413 bdev, &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003414 end_bio_extent_writepage,
3415 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003416 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003417 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003418 if (PageWriteback(page))
3419 end_page_writeback(page);
3420 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003421
Chris Masond1310b22008-01-24 16:13:08 -05003422 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003423 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003424 nr++;
3425 }
3426done:
Chris Mason40f76582014-05-21 13:35:51 -07003427 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003428 return ret;
3429}
3430
3431/*
3432 * the writepage semantics are similar to regular writepage. extent
3433 * records are inserted to lock ranges in the tree, and as dirty areas
3434 * are found, they are marked writeback. Then the lock bits are removed
3435 * and the end_io handler clears the writeback ranges
3436 */
3437static int __extent_writepage(struct page *page, struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003438 struct extent_page_data *epd)
Chris Mason40f76582014-05-21 13:35:51 -07003439{
3440 struct inode *inode = page->mapping->host;
Chris Mason40f76582014-05-21 13:35:51 -07003441 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003442 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003443 int ret;
3444 int nr = 0;
3445 size_t pg_offset = 0;
3446 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003447 unsigned long end_index = i_size >> PAGE_SHIFT;
David Sterbaf1c77c52017-06-06 19:03:49 +02003448 unsigned int write_flags = 0;
Chris Mason40f76582014-05-21 13:35:51 -07003449 unsigned long nr_written = 0;
3450
Liu Boff40adf2017-08-24 18:19:48 -06003451 write_flags = wbc_to_write_flags(wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003452
3453 trace___extent_writepage(page, inode, wbc);
3454
3455 WARN_ON(!PageLocked(page));
3456
3457 ClearPageError(page);
3458
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003459 pg_offset = i_size & (PAGE_SIZE - 1);
Chris Mason40f76582014-05-21 13:35:51 -07003460 if (page->index > end_index ||
3461 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003462 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003463 unlock_page(page);
3464 return 0;
3465 }
3466
3467 if (page->index == end_index) {
3468 char *userpage;
3469
3470 userpage = kmap_atomic(page);
3471 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003472 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003473 kunmap_atomic(userpage);
3474 flush_dcache_page(page);
3475 }
3476
3477 pg_offset = 0;
3478
3479 set_page_extent_mapped(page);
3480
3481 ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
3482 if (ret == 1)
3483 goto done_unlocked;
3484 if (ret)
3485 goto done;
3486
3487 ret = __extent_writepage_io(inode, page, wbc, epd,
3488 i_size, nr_written, write_flags, &nr);
3489 if (ret == 1)
3490 goto done_unlocked;
3491
3492done:
Chris Masond1310b22008-01-24 16:13:08 -05003493 if (nr == 0) {
3494 /* make sure the mapping tag for page dirty gets cleared */
3495 set_page_writeback(page);
3496 end_page_writeback(page);
3497 }
Filipe Manana61391d52014-05-09 17:17:40 +01003498 if (PageError(page)) {
3499 ret = ret < 0 ? ret : -EIO;
3500 end_extent_writepage(page, ret, start, page_end);
3501 }
Chris Masond1310b22008-01-24 16:13:08 -05003502 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003503 return ret;
Chris Mason771ed682008-11-06 22:02:51 -05003504
Chris Mason11c83492009-04-20 15:50:09 -04003505done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003506 return 0;
3507}
3508
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003509void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003510{
NeilBrown74316202014-07-07 15:16:04 +10003511 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3512 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003513}
3514
Chris Mason0e378df2014-05-19 20:55:27 -07003515static noinline_for_stack int
3516lock_extent_buffer_for_io(struct extent_buffer *eb,
3517 struct btrfs_fs_info *fs_info,
3518 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003519{
David Sterbacc5e31a2018-03-01 18:20:27 +01003520 int i, num_pages;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003521 int flush = 0;
3522 int ret = 0;
3523
3524 if (!btrfs_try_tree_write_lock(eb)) {
3525 flush = 1;
3526 flush_write_bio(epd);
3527 btrfs_tree_lock(eb);
3528 }
3529
3530 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3531 btrfs_tree_unlock(eb);
3532 if (!epd->sync_io)
3533 return 0;
3534 if (!flush) {
3535 flush_write_bio(epd);
3536 flush = 1;
3537 }
Chris Masona098d8e82012-03-21 12:09:56 -04003538 while (1) {
3539 wait_on_extent_buffer_writeback(eb);
3540 btrfs_tree_lock(eb);
3541 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3542 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003543 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003544 }
3545 }
3546
Josef Bacik51561ff2012-07-20 16:25:24 -04003547 /*
3548 * We need to do this to prevent races in people who check if the eb is
3549 * under IO since we can end up having no IO bits set for a short period
3550 * of time.
3551 */
3552 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003553 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3554 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003555 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003556 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Nikolay Borisov104b4e52017-06-20 21:01:20 +03003557 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3558 -eb->len,
3559 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003560 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003561 } else {
3562 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003563 }
3564
3565 btrfs_tree_unlock(eb);
3566
3567 if (!ret)
3568 return ret;
3569
David Sterba65ad0102018-06-29 10:56:49 +02003570 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003571 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003572 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003573
3574 if (!trylock_page(p)) {
3575 if (!flush) {
3576 flush_write_bio(epd);
3577 flush = 1;
3578 }
3579 lock_page(p);
3580 }
3581 }
3582
3583 return ret;
3584}
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;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003659
David Sterbac09abff2017-07-13 18:10:07 +02003660 ASSERT(!bio_flagged(bio, BIO_CLONED));
Kent Overstreet2c30c712013-11-07 12:20:26 -08003661 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003662 struct page *page = bvec->bv_page;
3663
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003664 eb = (struct extent_buffer *)page->private;
3665 BUG_ON(!eb);
3666 done = atomic_dec_and_test(&eb->io_pages);
3667
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003668 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003669 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003670 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003671 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003672 }
3673
3674 end_page_writeback(page);
3675
3676 if (!done)
3677 continue;
3678
3679 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003680 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003681
3682 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003683}
3684
Chris Mason0e378df2014-05-19 20:55:27 -07003685static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003686 struct btrfs_fs_info *fs_info,
3687 struct writeback_control *wbc,
3688 struct extent_page_data *epd)
3689{
3690 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003691 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003692 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003693 u32 nritems;
David Sterbacc5e31a2018-03-01 18:20:27 +01003694 int i, num_pages;
Liu Bo851cd172016-09-23 13:44:44 -07003695 unsigned long start, end;
Liu Boff40adf2017-08-24 18:19:48 -06003696 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003697 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003698
Filipe Manana656f30d2014-09-26 12:25:56 +01003699 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02003700 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003701 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003702
Liu Bo851cd172016-09-23 13:44:44 -07003703 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3704 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003705 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003706 end = btrfs_node_key_ptr_offset(nritems);
3707
David Sterbab159fa22016-11-08 18:09:03 +01003708 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003709 } else {
3710 /*
3711 * leaf:
3712 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3713 */
3714 start = btrfs_item_nr_offset(nritems);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003715 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, eb);
David Sterbab159fa22016-11-08 18:09:03 +01003716 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003717 }
3718
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003719 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003720 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003721
3722 clear_page_dirty_for_io(p);
3723 set_page_writeback(p);
David Sterba4b81ba42017-06-06 19:14:26 +02003724 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003725 p, offset, PAGE_SIZE, 0, bdev,
David Sterbac2df8bb2017-02-10 19:29:38 +01003726 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003727 end_bio_extent_buffer_writepage,
Liu Bo18fdc672017-09-13 12:18:22 -06003728 0, 0, 0, false);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003729 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003730 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003731 if (PageWriteback(p))
3732 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003733 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3734 end_extent_buffer_writeback(eb);
3735 ret = -EIO;
3736 break;
3737 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003738 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003739 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003740 unlock_page(p);
3741 }
3742
3743 if (unlikely(ret)) {
3744 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003745 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003746 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003747 unlock_page(p);
3748 }
3749 }
3750
3751 return ret;
3752}
3753
3754int btree_write_cache_pages(struct address_space *mapping,
3755 struct writeback_control *wbc)
3756{
3757 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3758 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3759 struct extent_buffer *eb, *prev_eb = NULL;
3760 struct extent_page_data epd = {
3761 .bio = NULL,
3762 .tree = tree,
3763 .extent_locked = 0,
3764 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3765 };
3766 int ret = 0;
3767 int done = 0;
3768 int nr_to_write_done = 0;
3769 struct pagevec pvec;
3770 int nr_pages;
3771 pgoff_t index;
3772 pgoff_t end; /* Inclusive */
3773 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003774 xa_mark_t tag;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003775
Mel Gorman86679822017-11-15 17:37:52 -08003776 pagevec_init(&pvec);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003777 if (wbc->range_cyclic) {
3778 index = mapping->writeback_index; /* Start from prev offset */
3779 end = -1;
3780 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003781 index = wbc->range_start >> PAGE_SHIFT;
3782 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003783 scanned = 1;
3784 }
3785 if (wbc->sync_mode == WB_SYNC_ALL)
3786 tag = PAGECACHE_TAG_TOWRITE;
3787 else
3788 tag = PAGECACHE_TAG_DIRTY;
3789retry:
3790 if (wbc->sync_mode == WB_SYNC_ALL)
3791 tag_pages_for_writeback(mapping, index, end);
3792 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara4006f432017-11-15 17:34:37 -08003793 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08003794 tag))) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003795 unsigned i;
3796
3797 scanned = 1;
3798 for (i = 0; i < nr_pages; i++) {
3799 struct page *page = pvec.pages[i];
3800
3801 if (!PagePrivate(page))
3802 continue;
3803
Josef Bacikb5bae262012-09-14 13:43:01 -04003804 spin_lock(&mapping->private_lock);
3805 if (!PagePrivate(page)) {
3806 spin_unlock(&mapping->private_lock);
3807 continue;
3808 }
3809
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003810 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003811
3812 /*
3813 * Shouldn't happen and normally this would be a BUG_ON
3814 * but no sense in crashing the users box for something
3815 * we can survive anyway.
3816 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303817 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003818 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003819 continue;
3820 }
3821
Josef Bacikb5bae262012-09-14 13:43:01 -04003822 if (eb == prev_eb) {
3823 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003824 continue;
3825 }
3826
Josef Bacikb5bae262012-09-14 13:43:01 -04003827 ret = atomic_inc_not_zero(&eb->refs);
3828 spin_unlock(&mapping->private_lock);
3829 if (!ret)
3830 continue;
3831
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003832 prev_eb = eb;
3833 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3834 if (!ret) {
3835 free_extent_buffer(eb);
3836 continue;
3837 }
3838
3839 ret = write_one_eb(eb, fs_info, wbc, &epd);
3840 if (ret) {
3841 done = 1;
3842 free_extent_buffer(eb);
3843 break;
3844 }
3845 free_extent_buffer(eb);
3846
3847 /*
3848 * the filesystem may choose to bump up nr_to_write.
3849 * We have to make sure to honor the new nr_to_write
3850 * at any time
3851 */
3852 nr_to_write_done = wbc->nr_to_write <= 0;
3853 }
3854 pagevec_release(&pvec);
3855 cond_resched();
3856 }
3857 if (!scanned && !done) {
3858 /*
3859 * We hit the last page and there is more work to be done: wrap
3860 * back to the start of the file
3861 */
3862 scanned = 1;
3863 index = 0;
3864 goto retry;
3865 }
3866 flush_write_bio(&epd);
3867 return ret;
3868}
3869
Chris Masond1310b22008-01-24 16:13:08 -05003870/**
Chris Mason4bef0842008-09-08 11:18:08 -04003871 * 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 -05003872 * @mapping: address space structure to write
3873 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
David Sterba935db852017-06-23 04:30:28 +02003874 * @data: data passed to __extent_writepage function
Chris Masond1310b22008-01-24 16:13:08 -05003875 *
3876 * If a page is already under I/O, write_cache_pages() skips it, even
3877 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3878 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3879 * and msync() need to guarantee that all the data which was dirty at the time
3880 * the call was made get new I/O started against them. If wbc->sync_mode is
3881 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3882 * existing IO to complete.
3883 */
David Sterba4242b642017-02-10 19:38:24 +01003884static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04003885 struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003886 struct extent_page_data *epd)
Chris Masond1310b22008-01-24 16:13:08 -05003887{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003888 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003889 int ret = 0;
3890 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003891 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003892 struct pagevec pvec;
3893 int nr_pages;
3894 pgoff_t index;
3895 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08003896 pgoff_t done_index;
3897 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003898 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003899 xa_mark_t tag;
Chris Masond1310b22008-01-24 16:13:08 -05003900
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003901 /*
3902 * We have to hold onto the inode so that ordered extents can do their
3903 * work when the IO finishes. The alternative to this is failing to add
3904 * an ordered extent if the igrab() fails there and that is a huge pain
3905 * to deal with, so instead just hold onto the inode throughout the
3906 * writepages operation. If it fails here we are freeing up the inode
3907 * anyway and we'd rather not waste our time writing out stuff that is
3908 * going to be truncated anyway.
3909 */
3910 if (!igrab(inode))
3911 return 0;
3912
Mel Gorman86679822017-11-15 17:37:52 -08003913 pagevec_init(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05003914 if (wbc->range_cyclic) {
3915 index = mapping->writeback_index; /* Start from prev offset */
3916 end = -1;
3917 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003918 index = wbc->range_start >> PAGE_SHIFT;
3919 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08003920 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3921 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05003922 scanned = 1;
3923 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003924 if (wbc->sync_mode == WB_SYNC_ALL)
3925 tag = PAGECACHE_TAG_TOWRITE;
3926 else
3927 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003928retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003929 if (wbc->sync_mode == WB_SYNC_ALL)
3930 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08003931 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003932 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara67fd7072017-11-15 17:35:19 -08003933 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
3934 &index, end, tag))) {
Chris Masond1310b22008-01-24 16:13:08 -05003935 unsigned i;
3936
3937 scanned = 1;
3938 for (i = 0; i < nr_pages; i++) {
3939 struct page *page = pvec.pages[i];
3940
Liu Boa91326672016-03-07 16:56:21 -08003941 done_index = page->index;
Chris Masond1310b22008-01-24 16:13:08 -05003942 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07003943 * At this point we hold neither the i_pages lock nor
3944 * the page lock: the page may be truncated or
3945 * invalidated (changing page->mapping to NULL),
3946 * or even swizzled back from swapper_space to
3947 * tmpfs file mapping
Chris Masond1310b22008-01-24 16:13:08 -05003948 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003949 if (!trylock_page(page)) {
David Sterbaaab6e9e2017-11-30 18:00:02 +01003950 flush_write_bio(epd);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003951 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003952 }
Chris Masond1310b22008-01-24 16:13:08 -05003953
3954 if (unlikely(page->mapping != mapping)) {
3955 unlock_page(page);
3956 continue;
3957 }
3958
Chris Masond2c3f4f2008-11-19 12:44:22 -05003959 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003960 if (PageWriteback(page))
David Sterbaaab6e9e2017-11-30 18:00:02 +01003961 flush_write_bio(epd);
Chris Masond1310b22008-01-24 16:13:08 -05003962 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003963 }
Chris Masond1310b22008-01-24 16:13:08 -05003964
3965 if (PageWriteback(page) ||
3966 !clear_page_dirty_for_io(page)) {
3967 unlock_page(page);
3968 continue;
3969 }
3970
David Sterbaaab6e9e2017-11-30 18:00:02 +01003971 ret = __extent_writepage(page, wbc, epd);
Chris Masond1310b22008-01-24 16:13:08 -05003972
3973 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3974 unlock_page(page);
3975 ret = 0;
3976 }
Liu Boa91326672016-03-07 16:56:21 -08003977 if (ret < 0) {
3978 /*
3979 * done_index is set past this page,
3980 * so media errors will not choke
3981 * background writeout for the entire
3982 * file. This has consequences for
3983 * range_cyclic semantics (ie. it may
3984 * not be suitable for data integrity
3985 * writeout).
3986 */
3987 done_index = page->index + 1;
3988 done = 1;
3989 break;
3990 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003991
3992 /*
3993 * the filesystem may choose to bump up nr_to_write.
3994 * We have to make sure to honor the new nr_to_write
3995 * at any time
3996 */
3997 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003998 }
3999 pagevec_release(&pvec);
4000 cond_resched();
4001 }
Liu Bo894b36e2016-03-07 16:56:22 -08004002 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004003 /*
4004 * We hit the last page and there is more work to be done: wrap
4005 * back to the start of the file
4006 */
4007 scanned = 1;
4008 index = 0;
4009 goto retry;
4010 }
Liu Boa91326672016-03-07 16:56:21 -08004011
4012 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4013 mapping->writeback_index = done_index;
4014
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004015 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004016 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004017}
Chris Masond1310b22008-01-24 16:13:08 -05004018
David Sterbaaab6e9e2017-11-30 18:00:02 +01004019static void flush_write_bio(struct extent_page_data *epd)
Chris Masonffbd5172009-04-20 15:50:09 -04004020{
4021 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04004022 int ret;
4023
Liu Bo18fdc672017-09-13 12:18:22 -06004024 ret = submit_one_bio(epd->bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004025 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04004026 epd->bio = NULL;
4027 }
4028}
4029
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004030int extent_write_full_page(struct page *page, struct writeback_control *wbc)
Chris Masond1310b22008-01-24 16:13:08 -05004031{
4032 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004033 struct extent_page_data epd = {
4034 .bio = NULL,
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004035 .tree = &BTRFS_I(page->mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004036 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004037 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004038 };
Chris Masond1310b22008-01-24 16:13:08 -05004039
Chris Masond1310b22008-01-24 16:13:08 -05004040 ret = __extent_writepage(page, wbc, &epd);
4041
David Sterbae2932ee2017-06-23 04:16:17 +02004042 flush_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004043 return ret;
4044}
Chris Masond1310b22008-01-24 16:13:08 -05004045
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004046int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
Chris Mason771ed682008-11-06 22:02:51 -05004047 int mode)
4048{
4049 int ret = 0;
4050 struct address_space *mapping = inode->i_mapping;
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004051 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Mason771ed682008-11-06 22:02:51 -05004052 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004053 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4054 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004055
4056 struct extent_page_data epd = {
4057 .bio = NULL,
4058 .tree = tree,
Chris Mason771ed682008-11-06 22:02:51 -05004059 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004060 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05004061 };
4062 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004063 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004064 .nr_to_write = nr_pages * 2,
4065 .range_start = start,
4066 .range_end = end + 1,
4067 };
4068
Chris Masond3977122009-01-05 21:25:51 -05004069 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004070 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004071 if (clear_page_dirty_for_io(page))
4072 ret = __extent_writepage(page, &wbc_writepages, &epd);
4073 else {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02004074 btrfs_writepage_endio_finish_ordered(page, start,
4075 start + PAGE_SIZE - 1,
4076 NULL, 1);
Chris Mason771ed682008-11-06 22:02:51 -05004077 unlock_page(page);
4078 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004079 put_page(page);
4080 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004081 }
4082
David Sterbae2932ee2017-06-23 04:16:17 +02004083 flush_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004084 return ret;
4085}
Chris Masond1310b22008-01-24 16:13:08 -05004086
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004087int extent_writepages(struct address_space *mapping,
Chris Masond1310b22008-01-24 16:13:08 -05004088 struct writeback_control *wbc)
4089{
4090 int ret = 0;
4091 struct extent_page_data epd = {
4092 .bio = NULL,
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004093 .tree = &BTRFS_I(mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004094 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004095 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004096 };
4097
David Sterba935db852017-06-23 04:30:28 +02004098 ret = extent_write_cache_pages(mapping, wbc, &epd);
David Sterbae2932ee2017-06-23 04:16:17 +02004099 flush_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004100 return ret;
4101}
Chris Masond1310b22008-01-24 16:13:08 -05004102
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004103int extent_readpages(struct address_space *mapping, struct list_head *pages,
4104 unsigned nr_pages)
Chris Masond1310b22008-01-24 16:13:08 -05004105{
4106 struct bio *bio = NULL;
4107 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04004108 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004109 struct page *pagepool[16];
4110 struct page *page;
Miao Xie125bac012013-07-25 19:22:37 +08004111 struct extent_map *em_cached = NULL;
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004112 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Liu Bo67c96842012-07-20 21:43:09 -06004113 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004114 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004115
Chris Masond1310b22008-01-24 16:13:08 -05004116 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06004117 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05004118
4119 prefetchw(&page->flags);
4120 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06004121 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07004122 page->index,
4123 readahead_gfp_mask(mapping))) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004124 put_page(page);
Liu Bo67c96842012-07-20 21:43:09 -06004125 continue;
Chris Masond1310b22008-01-24 16:13:08 -05004126 }
Liu Bo67c96842012-07-20 21:43:09 -06004127
4128 pagepool[nr++] = page;
4129 if (nr < ARRAY_SIZE(pagepool))
4130 continue;
David Sterbae4d17ef2017-06-23 04:09:57 +02004131 __extent_readpages(tree, pagepool, nr, &em_cached, &bio,
4132 &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004133 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004134 }
Miao Xie99740902013-07-25 19:22:36 +08004135 if (nr)
David Sterbae4d17ef2017-06-23 04:09:57 +02004136 __extent_readpages(tree, pagepool, nr, &em_cached, &bio,
4137 &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004138
Miao Xie125bac012013-07-25 19:22:37 +08004139 if (em_cached)
4140 free_extent_map(em_cached);
4141
Chris Masond1310b22008-01-24 16:13:08 -05004142 BUG_ON(!list_empty(pages));
4143 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004144 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004145 return 0;
4146}
Chris Masond1310b22008-01-24 16:13:08 -05004147
4148/*
4149 * basic invalidatepage code, this waits on any locked or writeback
4150 * ranges corresponding to the page, and then deletes any extent state
4151 * records from the tree
4152 */
4153int extent_invalidatepage(struct extent_io_tree *tree,
4154 struct page *page, unsigned long offset)
4155{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004156 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004157 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004158 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004159 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4160
Qu Wenruofda28322013-02-26 08:10:22 +00004161 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004162 if (start > end)
4163 return 0;
4164
David Sterbaff13db42015-12-03 14:30:40 +01004165 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004166 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004167 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004168 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4169 EXTENT_DO_ACCOUNTING,
David Sterbaae0f1622017-10-31 16:37:52 +01004170 1, 1, &cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05004171 return 0;
4172}
Chris Masond1310b22008-01-24 16:13:08 -05004173
4174/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004175 * a helper for releasepage, this tests for areas of the page that
4176 * are locked or under IO and drops the related state bits if it is safe
4177 * to drop the page.
4178 */
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004179static int try_release_extent_state(struct extent_io_tree *tree,
Eric Sandeen48a3b632013-04-25 20:41:01 +00004180 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004181{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004182 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004183 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004184 int ret = 1;
4185
Chris Mason211f90e2008-07-18 11:56:15 -04004186 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04004187 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04004188 ret = 0;
4189 else {
Chris Mason11ef1602009-09-23 20:28:46 -04004190 /*
4191 * at this point we can safely clear everything except the
4192 * locked bit and the nodatasum bit
4193 */
David Sterba66b0c882017-10-31 16:30:47 +01004194 ret = __clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004195 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
David Sterba66b0c882017-10-31 16:30:47 +01004196 0, 0, NULL, mask, NULL);
Chris Masone3f24cc2011-02-14 12:52:08 -05004197
4198 /* if clear_extent_bit failed for enomem reasons,
4199 * we can't allow the release to continue.
4200 */
4201 if (ret < 0)
4202 ret = 0;
4203 else
4204 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004205 }
4206 return ret;
4207}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004208
4209/*
Chris Masond1310b22008-01-24 16:13:08 -05004210 * a helper for releasepage. As long as there are no locked extents
4211 * in the range corresponding to the page, both state records and extent
4212 * map records are removed
4213 */
Nikolay Borisov477a30b2018-04-19 10:46:34 +03004214int try_release_extent_mapping(struct page *page, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004215{
4216 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004217 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004218 u64 end = start + PAGE_SIZE - 1;
Filipe Mananabd3599a2018-07-12 01:36:43 +01004219 struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
4220 struct extent_io_tree *tree = &btrfs_inode->io_tree;
4221 struct extent_map_tree *map = &btrfs_inode->extent_tree;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004222
Mel Gormand0164ad2015-11-06 16:28:21 -08004223 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004224 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004225 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004226 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004227 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004228 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004229 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004230 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004231 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004232 break;
4233 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004234 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4235 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004236 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004237 free_extent_map(em);
4238 break;
4239 }
4240 if (!test_range_bit(tree, em->start,
4241 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04004242 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04004243 0, NULL)) {
Filipe Mananabd3599a2018-07-12 01:36:43 +01004244 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4245 &btrfs_inode->runtime_flags);
Chris Mason70dec802008-01-29 09:59:12 -05004246 remove_extent_mapping(map, em);
4247 /* once for the rb tree */
4248 free_extent_map(em);
4249 }
4250 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004251 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004252
4253 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004254 free_extent_map(em);
4255 }
Chris Masond1310b22008-01-24 16:13:08 -05004256 }
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004257 return try_release_extent_state(tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004258}
Chris Masond1310b22008-01-24 16:13:08 -05004259
Chris Masonec29ed52011-02-23 16:23:20 -05004260/*
4261 * helper function for fiemap, which doesn't want to see any holes.
4262 * This maps until we find something past 'last'
4263 */
4264static struct extent_map *get_extent_skip_holes(struct inode *inode,
David Sterbae3350e12017-06-23 04:09:57 +02004265 u64 offset, u64 last)
Chris Masonec29ed52011-02-23 16:23:20 -05004266{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004267 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004268 struct extent_map *em;
4269 u64 len;
4270
4271 if (offset >= last)
4272 return NULL;
4273
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304274 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004275 len = last - offset;
4276 if (len == 0)
4277 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004278 len = ALIGN(len, sectorsize);
David Sterbae3350e12017-06-23 04:09:57 +02004279 em = btrfs_get_extent_fiemap(BTRFS_I(inode), NULL, 0, offset,
4280 len, 0);
David Sterbac7040052011-04-19 18:00:01 +02004281 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004282 return em;
4283
4284 /* if this isn't a hole return it */
Nikolay Borisov4a2d25c2017-11-23 10:51:43 +02004285 if (em->block_start != EXTENT_MAP_HOLE)
Chris Masonec29ed52011-02-23 16:23:20 -05004286 return em;
Chris Masonec29ed52011-02-23 16:23:20 -05004287
4288 /* this is a hole, advance to the next extent */
4289 offset = extent_map_end(em);
4290 free_extent_map(em);
4291 if (offset >= last)
4292 break;
4293 }
4294 return NULL;
4295}
4296
Qu Wenruo47518322017-04-07 10:43:15 +08004297/*
4298 * To cache previous fiemap extent
4299 *
4300 * Will be used for merging fiemap extent
4301 */
4302struct fiemap_cache {
4303 u64 offset;
4304 u64 phys;
4305 u64 len;
4306 u32 flags;
4307 bool cached;
4308};
4309
4310/*
4311 * Helper to submit fiemap extent.
4312 *
4313 * Will try to merge current fiemap extent specified by @offset, @phys,
4314 * @len and @flags with cached one.
4315 * And only when we fails to merge, cached one will be submitted as
4316 * fiemap extent.
4317 *
4318 * Return value is the same as fiemap_fill_next_extent().
4319 */
4320static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4321 struct fiemap_cache *cache,
4322 u64 offset, u64 phys, u64 len, u32 flags)
4323{
4324 int ret = 0;
4325
4326 if (!cache->cached)
4327 goto assign;
4328
4329 /*
4330 * Sanity check, extent_fiemap() should have ensured that new
4331 * fiemap extent won't overlap with cahced one.
4332 * Not recoverable.
4333 *
4334 * NOTE: Physical address can overlap, due to compression
4335 */
4336 if (cache->offset + cache->len > offset) {
4337 WARN_ON(1);
4338 return -EINVAL;
4339 }
4340
4341 /*
4342 * Only merges fiemap extents if
4343 * 1) Their logical addresses are continuous
4344 *
4345 * 2) Their physical addresses are continuous
4346 * So truly compressed (physical size smaller than logical size)
4347 * extents won't get merged with each other
4348 *
4349 * 3) Share same flags except FIEMAP_EXTENT_LAST
4350 * So regular extent won't get merged with prealloc extent
4351 */
4352 if (cache->offset + cache->len == offset &&
4353 cache->phys + cache->len == phys &&
4354 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4355 (flags & ~FIEMAP_EXTENT_LAST)) {
4356 cache->len += len;
4357 cache->flags |= flags;
4358 goto try_submit_last;
4359 }
4360
4361 /* Not mergeable, need to submit cached one */
4362 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4363 cache->len, cache->flags);
4364 cache->cached = false;
4365 if (ret)
4366 return ret;
4367assign:
4368 cache->cached = true;
4369 cache->offset = offset;
4370 cache->phys = phys;
4371 cache->len = len;
4372 cache->flags = flags;
4373try_submit_last:
4374 if (cache->flags & FIEMAP_EXTENT_LAST) {
4375 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4376 cache->phys, cache->len, cache->flags);
4377 cache->cached = false;
4378 }
4379 return ret;
4380}
4381
4382/*
Qu Wenruo848c23b2017-06-22 10:01:21 +08004383 * Emit last fiemap cache
Qu Wenruo47518322017-04-07 10:43:15 +08004384 *
Qu Wenruo848c23b2017-06-22 10:01:21 +08004385 * The last fiemap cache may still be cached in the following case:
4386 * 0 4k 8k
4387 * |<- Fiemap range ->|
4388 * |<------------ First extent ----------->|
4389 *
4390 * In this case, the first extent range will be cached but not emitted.
4391 * So we must emit it before ending extent_fiemap().
Qu Wenruo47518322017-04-07 10:43:15 +08004392 */
Qu Wenruo848c23b2017-06-22 10:01:21 +08004393static int emit_last_fiemap_cache(struct btrfs_fs_info *fs_info,
4394 struct fiemap_extent_info *fieinfo,
4395 struct fiemap_cache *cache)
Qu Wenruo47518322017-04-07 10:43:15 +08004396{
4397 int ret;
4398
4399 if (!cache->cached)
4400 return 0;
4401
Qu Wenruo47518322017-04-07 10:43:15 +08004402 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4403 cache->len, cache->flags);
4404 cache->cached = false;
4405 if (ret > 0)
4406 ret = 0;
4407 return ret;
4408}
4409
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004410int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterba2135fb92017-06-23 04:09:57 +02004411 __u64 start, __u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004412{
Josef Bacik975f84f2010-11-23 19:36:57 +00004413 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004414 u64 off = start;
4415 u64 max = start + len;
4416 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004417 u32 found_type;
4418 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004419 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004420 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004421 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004422 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004423 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004424 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004425 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004426 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004427 struct fiemap_cache cache = { 0 };
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004428 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004429 u64 em_start = 0;
4430 u64 em_len = 0;
4431 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004432
4433 if (len == 0)
4434 return -EINVAL;
4435
Josef Bacik975f84f2010-11-23 19:36:57 +00004436 path = btrfs_alloc_path();
4437 if (!path)
4438 return -ENOMEM;
4439 path->leave_spinning = 1;
4440
Jeff Mahoneyda170662016-06-15 09:22:56 -04004441 start = round_down(start, btrfs_inode_sectorsize(inode));
4442 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004443
Chris Masonec29ed52011-02-23 16:23:20 -05004444 /*
4445 * lookup the last file extent. We're not using i_size here
4446 * because there might be preallocation past i_size
4447 */
David Sterbaf85b7372017-01-20 14:54:07 +01004448 ret = btrfs_lookup_file_extent(NULL, root, path,
4449 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004450 if (ret < 0) {
4451 btrfs_free_path(path);
4452 return ret;
Liu Bo2d324f52016-05-17 17:21:48 -07004453 } else {
4454 WARN_ON(!ret);
4455 if (ret == 1)
4456 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004457 }
Liu Bo2d324f52016-05-17 17:21:48 -07004458
Josef Bacik975f84f2010-11-23 19:36:57 +00004459 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004460 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004461 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004462
Chris Masonec29ed52011-02-23 16:23:20 -05004463 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004464 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004465 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004466 /* have to trust i_size as the end */
4467 last = (u64)-1;
4468 last_for_get_extent = isize;
4469 } else {
4470 /*
4471 * remember the start of the last extent. There are a
4472 * bunch of different factors that go into the length of the
4473 * extent, so its much less complex to remember where it started
4474 */
4475 last = found_key.offset;
4476 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004477 }
Liu Bofe09e162013-09-22 12:54:23 +08004478 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004479
Chris Masonec29ed52011-02-23 16:23:20 -05004480 /*
4481 * we might have some extents allocated but more delalloc past those
4482 * extents. so, we trust isize unless the start of the last extent is
4483 * beyond isize
4484 */
4485 if (last < isize) {
4486 last = (u64)-1;
4487 last_for_get_extent = isize;
4488 }
4489
David Sterbaff13db42015-12-03 14:30:40 +01004490 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004491 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004492
David Sterbae3350e12017-06-23 04:09:57 +02004493 em = get_extent_skip_holes(inode, start, last_for_get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004494 if (!em)
4495 goto out;
4496 if (IS_ERR(em)) {
4497 ret = PTR_ERR(em);
4498 goto out;
4499 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004500
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004501 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004502 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004503
Chris Masonea8efc72011-03-08 11:54:40 -05004504 /* break if the extent we found is outside the range */
4505 if (em->start >= max || extent_map_end(em) < off)
4506 break;
4507
4508 /*
4509 * get_extent may return an extent that starts before our
4510 * requested range. We have to make sure the ranges
4511 * we return to fiemap always move forward and don't
4512 * overlap, so adjust the offsets here
4513 */
4514 em_start = max(em->start, off);
4515
4516 /*
4517 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004518 * for adjusting the disk offset below. Only do this if the
4519 * extent isn't compressed since our in ram offset may be past
4520 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004521 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004522 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4523 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004524 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004525 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004526 flags = 0;
Filipe Mananaf0986312018-06-20 10:02:30 +01004527 if (em->block_start < EXTENT_MAP_LAST_BYTE)
4528 disko = em->block_start + offset_in_extent;
4529 else
4530 disko = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004531
Chris Masonea8efc72011-03-08 11:54:40 -05004532 /*
4533 * bump off for our next call to get_extent
4534 */
4535 off = extent_map_end(em);
4536 if (off >= max)
4537 end = 1;
4538
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004539 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004540 end = 1;
4541 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004542 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004543 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4544 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004545 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004546 flags |= (FIEMAP_EXTENT_DELALLOC |
4547 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004548 } else if (fieinfo->fi_extents_max) {
4549 u64 bytenr = em->block_start -
4550 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004551
Liu Bofe09e162013-09-22 12:54:23 +08004552 /*
4553 * As btrfs supports shared space, this information
4554 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004555 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4556 * then we're just getting a count and we can skip the
4557 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004558 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06004559 ret = btrfs_check_shared(root,
4560 btrfs_ino(BTRFS_I(inode)),
4561 bytenr);
Josef Bacikdc046b12014-09-10 16:20:45 -04004562 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004563 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004564 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004565 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004566 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004567 }
4568 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4569 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004570 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4571 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004572
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004573 free_extent_map(em);
4574 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004575 if ((em_start >= last) || em_len == (u64)-1 ||
4576 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004577 flags |= FIEMAP_EXTENT_LAST;
4578 end = 1;
4579 }
4580
Chris Masonec29ed52011-02-23 16:23:20 -05004581 /* now scan forward to see if this is really the last extent. */
David Sterbae3350e12017-06-23 04:09:57 +02004582 em = get_extent_skip_holes(inode, off, last_for_get_extent);
Chris Masonec29ed52011-02-23 16:23:20 -05004583 if (IS_ERR(em)) {
4584 ret = PTR_ERR(em);
4585 goto out;
4586 }
4587 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004588 flags |= FIEMAP_EXTENT_LAST;
4589 end = 1;
4590 }
Qu Wenruo47518322017-04-07 10:43:15 +08004591 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4592 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004593 if (ret) {
4594 if (ret == 1)
4595 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004596 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004597 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004598 }
4599out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004600 if (!ret)
Qu Wenruo848c23b2017-06-22 10:01:21 +08004601 ret = emit_last_fiemap_cache(root->fs_info, fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004602 free_extent_map(em);
4603out:
Liu Bofe09e162013-09-22 12:54:23 +08004604 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004605 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004606 &cached_state);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004607 return ret;
4608}
4609
Chris Mason727011e2010-08-06 13:21:20 -04004610static void __free_extent_buffer(struct extent_buffer *eb)
4611{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004612 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004613 kmem_cache_free(extent_buffer_cache, eb);
4614}
4615
Josef Bacika26e8c92014-03-28 17:07:27 -04004616int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004617{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004618 return (atomic_read(&eb->io_pages) ||
4619 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4620 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004621}
4622
Miao Xie897ca6e92010-10-26 20:57:29 -04004623/*
David Sterba55ac0132018-07-19 17:24:32 +02004624 * Release all pages attached to the extent buffer.
Miao Xie897ca6e92010-10-26 20:57:29 -04004625 */
David Sterba55ac0132018-07-19 17:24:32 +02004626static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004627{
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004628 int i;
4629 int num_pages;
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004630 int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004631
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004632 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004633
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004634 num_pages = num_extent_pages(eb);
4635 for (i = 0; i < num_pages; i++) {
4636 struct page *page = eb->pages[i];
Miao Xie897ca6e92010-10-26 20:57:29 -04004637
Forrest Liu5d2361d2015-02-09 17:31:45 +08004638 if (!page)
4639 continue;
4640 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004641 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004642 /*
4643 * We do this since we'll remove the pages after we've
4644 * removed the eb from the radix tree, so we could race
4645 * and have this page now attached to the new eb. So
4646 * only clear page_private if it's still connected to
4647 * this eb.
4648 */
4649 if (PagePrivate(page) &&
4650 page->private == (unsigned long)eb) {
4651 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4652 BUG_ON(PageDirty(page));
4653 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004654 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004655 * We need to make sure we haven't be attached
4656 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004657 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004658 ClearPagePrivate(page);
4659 set_page_private(page, 0);
4660 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004661 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004662 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004663
4664 if (mapped)
4665 spin_unlock(&page->mapping->private_lock);
4666
Nicholas D Steeves01327612016-05-19 21:18:45 -04004667 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004668 put_page(page);
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004669 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004670}
4671
4672/*
4673 * Helper for releasing the extent buffer.
4674 */
4675static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4676{
David Sterba55ac0132018-07-19 17:24:32 +02004677 btrfs_release_extent_buffer_pages(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004678 __free_extent_buffer(eb);
4679}
4680
Josef Bacikf28491e2013-12-16 13:24:27 -05004681static struct extent_buffer *
4682__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004683 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004684{
4685 struct extent_buffer *eb = NULL;
4686
Michal Hockod1b5c562015-08-19 14:17:40 +02004687 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004688 eb->start = start;
4689 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004690 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004691 eb->bflags = 0;
4692 rwlock_init(&eb->lock);
4693 atomic_set(&eb->write_locks, 0);
4694 atomic_set(&eb->read_locks, 0);
4695 atomic_set(&eb->blocking_readers, 0);
4696 atomic_set(&eb->blocking_writers, 0);
4697 atomic_set(&eb->spinning_readers, 0);
4698 atomic_set(&eb->spinning_writers, 0);
4699 eb->lock_nested = 0;
4700 init_waitqueue_head(&eb->write_lock_wq);
4701 init_waitqueue_head(&eb->read_lock_wq);
4702
4703 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4704
4705 spin_lock_init(&eb->refs_lock);
4706 atomic_set(&eb->refs, 1);
4707 atomic_set(&eb->io_pages, 0);
4708
4709 /*
4710 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4711 */
4712 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4713 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4714 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4715
4716 return eb;
4717}
4718
4719struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4720{
David Sterbacc5e31a2018-03-01 18:20:27 +01004721 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004722 struct page *p;
4723 struct extent_buffer *new;
David Sterbacc5e31a2018-03-01 18:20:27 +01004724 int num_pages = num_extent_pages(src);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004725
David Sterba3f556f72014-06-15 03:20:26 +02004726 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004727 if (new == NULL)
4728 return NULL;
4729
4730 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004731 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004732 if (!p) {
4733 btrfs_release_extent_buffer(new);
4734 return NULL;
4735 }
4736 attach_extent_buffer_page(new, p);
4737 WARN_ON(PageDirty(p));
4738 SetPageUptodate(p);
4739 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004740 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004741 }
4742
Josef Bacikdb7f3432013-08-07 14:54:37 -04004743 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004744 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004745
4746 return new;
4747}
4748
Omar Sandoval0f331222015-09-29 20:50:31 -07004749struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4750 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004751{
4752 struct extent_buffer *eb;
David Sterbacc5e31a2018-03-01 18:20:27 +01004753 int num_pages;
4754 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004755
David Sterba3f556f72014-06-15 03:20:26 +02004756 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004757 if (!eb)
4758 return NULL;
4759
David Sterba65ad0102018-06-29 10:56:49 +02004760 num_pages = num_extent_pages(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004761 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004762 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004763 if (!eb->pages[i])
4764 goto err;
4765 }
4766 set_extent_buffer_uptodate(eb);
4767 btrfs_set_header_nritems(eb, 0);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004768 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004769
4770 return eb;
4771err:
4772 for (; i > 0; i--)
4773 __free_page(eb->pages[i - 1]);
4774 __free_extent_buffer(eb);
4775 return NULL;
4776}
4777
Omar Sandoval0f331222015-09-29 20:50:31 -07004778struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004779 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004780{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004781 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004782}
4783
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004784static void check_buffer_tree_ref(struct extent_buffer *eb)
4785{
Chris Mason242e18c2013-01-29 17:49:37 -05004786 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004787 /* the ref bit is tricky. We have to make sure it is set
4788 * if we have the buffer dirty. Otherwise the
4789 * code to free a buffer can end up dropping a dirty
4790 * page
4791 *
4792 * Once the ref bit is set, it won't go away while the
4793 * buffer is dirty or in writeback, and it also won't
4794 * go away while we have the reference count on the
4795 * eb bumped.
4796 *
4797 * We can't just set the ref bit without bumping the
4798 * ref on the eb because free_extent_buffer might
4799 * see the ref bit and try to clear it. If this happens
4800 * free_extent_buffer might end up dropping our original
4801 * ref by mistake and freeing the page before we are able
4802 * to add one more ref.
4803 *
4804 * So bump the ref count first, then set the bit. If someone
4805 * beat us to it, drop the ref we added.
4806 */
Chris Mason242e18c2013-01-29 17:49:37 -05004807 refs = atomic_read(&eb->refs);
4808 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4809 return;
4810
Josef Bacik594831c2012-07-20 16:11:08 -04004811 spin_lock(&eb->refs_lock);
4812 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004813 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004814 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004815}
4816
Mel Gorman2457aec2014-06-04 16:10:31 -07004817static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4818 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004819{
David Sterbacc5e31a2018-03-01 18:20:27 +01004820 int num_pages, i;
Josef Bacik5df42352012-03-15 18:24:42 -04004821
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004822 check_buffer_tree_ref(eb);
4823
David Sterba65ad0102018-06-29 10:56:49 +02004824 num_pages = num_extent_pages(eb);
Josef Bacik5df42352012-03-15 18:24:42 -04004825 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004826 struct page *p = eb->pages[i];
4827
Mel Gorman2457aec2014-06-04 16:10:31 -07004828 if (p != accessed)
4829 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004830 }
4831}
4832
Josef Bacikf28491e2013-12-16 13:24:27 -05004833struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4834 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004835{
4836 struct extent_buffer *eb;
4837
4838 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004839 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004840 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004841 if (eb && atomic_inc_not_zero(&eb->refs)) {
4842 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004843 /*
4844 * Lock our eb's refs_lock to avoid races with
4845 * free_extent_buffer. When we get our eb it might be flagged
4846 * with EXTENT_BUFFER_STALE and another task running
4847 * free_extent_buffer might have seen that flag set,
4848 * eb->refs == 2, that the buffer isn't under IO (dirty and
4849 * writeback flags not set) and it's still in the tree (flag
4850 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4851 * of decrementing the extent buffer's reference count twice.
4852 * So here we could race and increment the eb's reference count,
4853 * clear its stale flag, mark it as dirty and drop our reference
4854 * before the other task finishes executing free_extent_buffer,
4855 * which would later result in an attempt to free an extent
4856 * buffer that is dirty.
4857 */
4858 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4859 spin_lock(&eb->refs_lock);
4860 spin_unlock(&eb->refs_lock);
4861 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004862 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004863 return eb;
4864 }
4865 rcu_read_unlock();
4866
4867 return NULL;
4868}
4869
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004870#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4871struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004872 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004873{
4874 struct extent_buffer *eb, *exists = NULL;
4875 int ret;
4876
4877 eb = find_extent_buffer(fs_info, start);
4878 if (eb)
4879 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04004880 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004881 if (!eb)
4882 return NULL;
4883 eb->fs_info = fs_info;
4884again:
David Sterbae1860a72016-05-09 14:11:38 +02004885 ret = radix_tree_preload(GFP_NOFS);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004886 if (ret)
4887 goto free_eb;
4888 spin_lock(&fs_info->buffer_lock);
4889 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004890 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004891 spin_unlock(&fs_info->buffer_lock);
4892 radix_tree_preload_end();
4893 if (ret == -EEXIST) {
4894 exists = find_extent_buffer(fs_info, start);
4895 if (exists)
4896 goto free_eb;
4897 else
4898 goto again;
4899 }
4900 check_buffer_tree_ref(eb);
4901 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
4902
4903 /*
4904 * We will free dummy extent buffer's if they come into
4905 * free_extent_buffer with a ref count of 2, but if we are using this we
4906 * want the buffers to stay in memory until we're done with them, so
4907 * bump the ref count again.
4908 */
4909 atomic_inc(&eb->refs);
4910 return eb;
4911free_eb:
4912 btrfs_release_extent_buffer(eb);
4913 return exists;
4914}
4915#endif
4916
Josef Bacikf28491e2013-12-16 13:24:27 -05004917struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004918 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05004919{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004920 unsigned long len = fs_info->nodesize;
David Sterbacc5e31a2018-03-01 18:20:27 +01004921 int num_pages;
4922 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004923 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004924 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004925 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004926 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05004927 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05004928 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004929 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004930
Jeff Mahoneyda170662016-06-15 09:22:56 -04004931 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07004932 btrfs_err(fs_info, "bad tree block start %llu", start);
4933 return ERR_PTR(-EINVAL);
4934 }
4935
Josef Bacikf28491e2013-12-16 13:24:27 -05004936 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004937 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004938 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004939
David Sterba23d79d82014-06-15 02:55:29 +02004940 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04004941 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07004942 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05004943
David Sterba65ad0102018-06-29 10:56:49 +02004944 num_pages = num_extent_pages(eb);
Chris Mason727011e2010-08-06 13:21:20 -04004945 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02004946 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07004947 if (!p) {
4948 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04004949 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004950 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004951
4952 spin_lock(&mapping->private_lock);
4953 if (PagePrivate(p)) {
4954 /*
4955 * We could have already allocated an eb for this page
4956 * and attached one so lets see if we can get a ref on
4957 * the existing eb, and if we can we know it's good and
4958 * we can just return that one, else we know we can just
4959 * overwrite page->private.
4960 */
4961 exists = (struct extent_buffer *)p->private;
4962 if (atomic_inc_not_zero(&exists->refs)) {
4963 spin_unlock(&mapping->private_lock);
4964 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004965 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07004966 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004967 goto free_eb;
4968 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08004969 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004970
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004971 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004972 * Do this so attach doesn't complain and we need to
4973 * drop the ref the old guy had.
4974 */
4975 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004976 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004977 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05004978 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004979 attach_extent_buffer_page(eb, p);
4980 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004981 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04004982 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004983 if (!PageUptodate(p))
4984 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004985
4986 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03004987 * We can't unlock the pages just yet since the extent buffer
4988 * hasn't been properly inserted in the radix tree, this
4989 * opens a race with btree_releasepage which can free a page
4990 * while we are still filling in all pages for the buffer and
4991 * we could crash.
Chris Masoneb14ab82011-02-10 12:35:00 -05004992 */
Chris Masond1310b22008-01-24 16:13:08 -05004993 }
4994 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004995 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004996again:
David Sterbae1860a72016-05-09 14:11:38 +02004997 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07004998 if (ret) {
4999 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04005000 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005001 }
Miao Xie19fe0a82010-10-26 20:57:29 -04005002
Josef Bacikf28491e2013-12-16 13:24:27 -05005003 spin_lock(&fs_info->buffer_lock);
5004 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005005 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05005006 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005007 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04005008 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005009 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005010 if (exists)
5011 goto free_eb;
5012 else
Josef Bacik115391d2012-03-09 09:51:43 -05005013 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04005014 }
Chris Mason6af118ce2008-07-22 11:18:07 -04005015 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005016 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005017 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005018
5019 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005020 * Now it's safe to unlock the pages because any calls to
5021 * btree_releasepage will correctly detect that a page belongs to a
5022 * live buffer and won't free them prematurely.
Chris Masoneb14ab82011-02-10 12:35:00 -05005023 */
Nikolay Borisov28187ae2018-07-04 10:24:51 +03005024 for (i = 0; i < num_pages; i++)
5025 unlock_page(eb->pages[i]);
Chris Masond1310b22008-01-24 16:13:08 -05005026 return eb;
5027
Chris Mason6af118ce2008-07-22 11:18:07 -04005028free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005029 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005030 for (i = 0; i < num_pages; i++) {
5031 if (eb->pages[i])
5032 unlock_page(eb->pages[i]);
5033 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005034
Miao Xie897ca6e92010-10-26 20:57:29 -04005035 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005036 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005037}
Chris Masond1310b22008-01-24 16:13:08 -05005038
Josef Bacik3083ee22012-03-09 16:01:49 -05005039static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5040{
5041 struct extent_buffer *eb =
5042 container_of(head, struct extent_buffer, rcu_head);
5043
5044 __free_extent_buffer(eb);
5045}
5046
David Sterbaf7a52a42013-04-26 14:56:29 +00005047static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005048{
Nikolay Borisov07e21c42018-06-27 16:38:23 +03005049 lockdep_assert_held(&eb->refs_lock);
5050
Josef Bacik3083ee22012-03-09 16:01:49 -05005051 WARN_ON(atomic_read(&eb->refs) == 0);
5052 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005053 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005054 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005055
Jan Schmidt815a51c2012-05-16 17:00:02 +02005056 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005057
Josef Bacikf28491e2013-12-16 13:24:27 -05005058 spin_lock(&fs_info->buffer_lock);
5059 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005060 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005061 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005062 } else {
5063 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005064 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005065
5066 /* Should be safe to release our pages at this point */
David Sterba55ac0132018-07-19 17:24:32 +02005067 btrfs_release_extent_buffer_pages(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005068#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005069 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
Josef Bacikbcb7e442015-03-16 17:38:02 -04005070 __free_extent_buffer(eb);
5071 return 1;
5072 }
5073#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005074 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005075 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005076 }
5077 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005078
5079 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005080}
5081
Chris Masond1310b22008-01-24 16:13:08 -05005082void free_extent_buffer(struct extent_buffer *eb)
5083{
Chris Mason242e18c2013-01-29 17:49:37 -05005084 int refs;
5085 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005086 if (!eb)
5087 return;
5088
Chris Mason242e18c2013-01-29 17:49:37 -05005089 while (1) {
5090 refs = atomic_read(&eb->refs);
5091 if (refs <= 3)
5092 break;
5093 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5094 if (old == refs)
5095 return;
5096 }
5097
Josef Bacik3083ee22012-03-09 16:01:49 -05005098 spin_lock(&eb->refs_lock);
5099 if (atomic_read(&eb->refs) == 2 &&
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005100 test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))
Jan Schmidt815a51c2012-05-16 17:00:02 +02005101 atomic_dec(&eb->refs);
5102
5103 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005104 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005105 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005106 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5107 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005108
Josef Bacik3083ee22012-03-09 16:01:49 -05005109 /*
5110 * I know this is terrible, but it's temporary until we stop tracking
5111 * the uptodate bits and such for the extent buffers.
5112 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005113 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005114}
Chris Masond1310b22008-01-24 16:13:08 -05005115
Josef Bacik3083ee22012-03-09 16:01:49 -05005116void free_extent_buffer_stale(struct extent_buffer *eb)
5117{
5118 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005119 return;
5120
Josef Bacik3083ee22012-03-09 16:01:49 -05005121 spin_lock(&eb->refs_lock);
5122 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5123
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005124 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005125 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5126 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005127 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005128}
5129
Chris Mason1d4284b2012-03-28 20:31:37 -04005130void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005131{
David Sterbacc5e31a2018-03-01 18:20:27 +01005132 int i;
5133 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005134 struct page *page;
5135
David Sterba65ad0102018-06-29 10:56:49 +02005136 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005137
5138 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005139 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005140 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005141 continue;
5142
Chris Masona61e6f22008-07-22 11:18:08 -04005143 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005144 WARN_ON(!PagePrivate(page));
5145
Chris Masond1310b22008-01-24 16:13:08 -05005146 clear_page_dirty_for_io(page);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005147 xa_lock_irq(&page->mapping->i_pages);
Matthew Wilcox0a943c62017-12-04 10:37:22 -05005148 if (!PageDirty(page))
5149 __xa_clear_mark(&page->mapping->i_pages,
5150 page_index(page), PAGECACHE_TAG_DIRTY);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005151 xa_unlock_irq(&page->mapping->i_pages);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005152 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005153 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005154 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005155 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005156}
Chris Masond1310b22008-01-24 16:13:08 -05005157
Liu Boabb57ef2018-09-14 01:44:42 +08005158bool set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005159{
David Sterbacc5e31a2018-03-01 18:20:27 +01005160 int i;
5161 int num_pages;
Liu Boabb57ef2018-09-14 01:44:42 +08005162 bool was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005163
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005164 check_buffer_tree_ref(eb);
5165
Chris Masonb9473432009-03-13 11:00:37 -04005166 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005167
David Sterba65ad0102018-06-29 10:56:49 +02005168 num_pages = num_extent_pages(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05005169 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005170 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5171
Liu Boabb57ef2018-09-14 01:44:42 +08005172 if (!was_dirty)
5173 for (i = 0; i < num_pages; i++)
5174 set_page_dirty(eb->pages[i]);
Liu Bo51995c32018-09-14 01:46:08 +08005175
5176#ifdef CONFIG_BTRFS_DEBUG
5177 for (i = 0; i < num_pages; i++)
5178 ASSERT(PageDirty(eb->pages[i]));
5179#endif
5180
Chris Masonb9473432009-03-13 11:00:37 -04005181 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005182}
Chris Masond1310b22008-01-24 16:13:08 -05005183
David Sterba69ba3922015-12-03 13:08:59 +01005184void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005185{
David Sterbacc5e31a2018-03-01 18:20:27 +01005186 int i;
Chris Mason1259ab72008-05-12 13:39:03 -04005187 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005188 int num_pages;
Chris Mason1259ab72008-05-12 13:39:03 -04005189
Chris Masonb4ce94d2009-02-04 09:25:08 -05005190 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005191 num_pages = num_extent_pages(eb);
Chris Mason1259ab72008-05-12 13:39:03 -04005192 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005193 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005194 if (page)
5195 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005196 }
Chris Mason1259ab72008-05-12 13:39:03 -04005197}
5198
David Sterba09c25a82015-12-03 13:08:59 +01005199void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005200{
David Sterbacc5e31a2018-03-01 18:20:27 +01005201 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005202 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005203 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005204
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005205 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005206 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005207 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005208 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005209 SetPageUptodate(page);
5210 }
Chris Masond1310b22008-01-24 16:13:08 -05005211}
Chris Masond1310b22008-01-24 16:13:08 -05005212
Chris Masond1310b22008-01-24 16:13:08 -05005213int read_extent_buffer_pages(struct extent_io_tree *tree,
David Sterba6af49db2017-06-23 04:09:57 +02005214 struct extent_buffer *eb, int wait, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005215{
David Sterbacc5e31a2018-03-01 18:20:27 +01005216 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005217 struct page *page;
5218 int err;
5219 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005220 int locked_pages = 0;
5221 int all_uptodate = 1;
David Sterbacc5e31a2018-03-01 18:20:27 +01005222 int num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005223 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005224 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005225 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005226
Chris Masonb4ce94d2009-02-04 09:25:08 -05005227 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005228 return 0;
5229
David Sterba65ad0102018-06-29 10:56:49 +02005230 num_pages = num_extent_pages(eb);
Josef Bacik8436ea912016-09-02 15:40:03 -04005231 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005232 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005233 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005234 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005235 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005236 } else {
5237 lock_page(page);
5238 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005239 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005240 }
5241 /*
5242 * We need to firstly lock all pages to make sure that
5243 * the uptodate bit of our pages won't be affected by
5244 * clear_extent_buffer_uptodate().
5245 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005246 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005247 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005248 if (!PageUptodate(page)) {
5249 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005250 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005251 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005252 }
Liu Bo2571e732016-08-03 12:33:01 -07005253
Chris Masonce9adaa2008-04-09 16:28:12 -04005254 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005255 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005256 goto unlock_exit;
5257 }
5258
Filipe Manana656f30d2014-09-26 12:25:56 +01005259 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005260 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005261 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005262 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005263 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005264
Chris Masonce9adaa2008-04-09 16:28:12 -04005265 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005266 if (ret) {
5267 atomic_dec(&eb->io_pages);
5268 unlock_page(page);
5269 continue;
5270 }
5271
Chris Masonf1885912008-04-09 16:28:12 -04005272 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005273 err = __extent_read_full_page(tree, page,
David Sterba6af49db2017-06-23 04:09:57 +02005274 btree_get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005275 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005276 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005277 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005278 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005279 /*
5280 * We use &bio in above __extent_read_full_page,
5281 * so we ensure that if it returns error, the
5282 * current page fails to add itself to bio and
5283 * it's been unlocked.
5284 *
5285 * We must dec io_pages by ourselves.
5286 */
5287 atomic_dec(&eb->io_pages);
5288 }
Chris Masond1310b22008-01-24 16:13:08 -05005289 } else {
5290 unlock_page(page);
5291 }
5292 }
5293
Jeff Mahoney355808c2011-10-03 23:23:14 -04005294 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005295 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005296 if (err)
5297 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005298 }
Chris Masona86c12c2008-02-07 10:50:54 -05005299
Arne Jansenbb82ab82011-06-10 14:06:53 +02005300 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005301 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005302
Josef Bacik8436ea912016-09-02 15:40:03 -04005303 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005304 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005305 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005306 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005307 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005308 }
Chris Masond3977122009-01-05 21:25:51 -05005309
Chris Masond1310b22008-01-24 16:13:08 -05005310 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005311
5312unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005313 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005314 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005315 page = eb->pages[locked_pages];
5316 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005317 }
5318 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005319}
Chris Masond1310b22008-01-24 16:13:08 -05005320
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005321void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5322 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005323{
5324 size_t cur;
5325 size_t offset;
5326 struct page *page;
5327 char *kaddr;
5328 char *dst = (char *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005329 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5330 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005331
Liu Bof716abd2017-08-09 11:10:16 -06005332 if (start + len > eb->len) {
5333 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5334 eb->start, eb->len, start, len);
5335 memset(dst, 0, len);
5336 return;
5337 }
Chris Masond1310b22008-01-24 16:13:08 -05005338
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005339 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005340
Chris Masond3977122009-01-05 21:25:51 -05005341 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005342 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005343
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005344 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005345 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005346 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005347
5348 dst += cur;
5349 len -= cur;
5350 offset = 0;
5351 i++;
5352 }
5353}
Chris Masond1310b22008-01-24 16:13:08 -05005354
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005355int read_extent_buffer_to_user(const struct extent_buffer *eb,
5356 void __user *dstv,
5357 unsigned long start, unsigned long len)
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005358{
5359 size_t cur;
5360 size_t offset;
5361 struct page *page;
5362 char *kaddr;
5363 char __user *dst = (char __user *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005364 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5365 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005366 int ret = 0;
5367
5368 WARN_ON(start > eb->len);
5369 WARN_ON(start + len > eb->start + eb->len);
5370
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005371 offset = (start_offset + start) & (PAGE_SIZE - 1);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005372
5373 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005374 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005375
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005376 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005377 kaddr = page_address(page);
5378 if (copy_to_user(dst, kaddr + offset, cur)) {
5379 ret = -EFAULT;
5380 break;
5381 }
5382
5383 dst += cur;
5384 len -= cur;
5385 offset = 0;
5386 i++;
5387 }
5388
5389 return ret;
5390}
5391
Liu Bo415b35a2016-06-17 19:16:21 -07005392/*
5393 * return 0 if the item is found within a page.
5394 * return 1 if the item spans two pages.
5395 * return -EINVAL otherwise.
5396 */
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005397int map_private_extent_buffer(const struct extent_buffer *eb,
5398 unsigned long start, unsigned long min_len,
5399 char **map, unsigned long *map_start,
5400 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005401{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005402 size_t offset = start & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005403 char *kaddr;
5404 struct page *p;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005405 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5406 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005407 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005408 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005409
Liu Bof716abd2017-08-09 11:10:16 -06005410 if (start + min_len > eb->len) {
5411 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5412 eb->start, eb->len, start, min_len);
5413 return -EINVAL;
5414 }
5415
Chris Masond1310b22008-01-24 16:13:08 -05005416 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005417 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005418
5419 if (i == 0) {
5420 offset = start_offset;
5421 *map_start = 0;
5422 } else {
5423 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005424 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005425 }
Chris Masond3977122009-01-05 21:25:51 -05005426
David Sterbafb85fc92014-07-31 01:03:53 +02005427 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005428 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005429 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005430 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005431 return 0;
5432}
Chris Masond1310b22008-01-24 16:13:08 -05005433
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005434int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5435 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005436{
5437 size_t cur;
5438 size_t offset;
5439 struct page *page;
5440 char *kaddr;
5441 char *ptr = (char *)ptrv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005442 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5443 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005444 int ret = 0;
5445
5446 WARN_ON(start > eb->len);
5447 WARN_ON(start + len > eb->start + eb->len);
5448
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005449 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005450
Chris Masond3977122009-01-05 21:25:51 -05005451 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005452 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005453
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005454 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005455
Chris Masona6591712011-07-19 12:04:14 -04005456 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005457 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005458 if (ret)
5459 break;
5460
5461 ptr += cur;
5462 len -= cur;
5463 offset = 0;
5464 i++;
5465 }
5466 return ret;
5467}
Chris Masond1310b22008-01-24 16:13:08 -05005468
David Sterbaf157bf72016-11-09 17:43:38 +01005469void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5470 const void *srcv)
5471{
5472 char *kaddr;
5473
5474 WARN_ON(!PageUptodate(eb->pages[0]));
5475 kaddr = page_address(eb->pages[0]);
5476 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5477 BTRFS_FSID_SIZE);
5478}
5479
5480void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5481{
5482 char *kaddr;
5483
5484 WARN_ON(!PageUptodate(eb->pages[0]));
5485 kaddr = page_address(eb->pages[0]);
5486 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5487 BTRFS_FSID_SIZE);
5488}
5489
Chris Masond1310b22008-01-24 16:13:08 -05005490void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5491 unsigned long start, unsigned long len)
5492{
5493 size_t cur;
5494 size_t offset;
5495 struct page *page;
5496 char *kaddr;
5497 char *src = (char *)srcv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005498 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5499 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005500
5501 WARN_ON(start > eb->len);
5502 WARN_ON(start + len > eb->start + eb->len);
5503
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005504 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005505
Chris Masond3977122009-01-05 21:25:51 -05005506 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005507 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005508 WARN_ON(!PageUptodate(page));
5509
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005510 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005511 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005512 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005513
5514 src += cur;
5515 len -= cur;
5516 offset = 0;
5517 i++;
5518 }
5519}
Chris Masond1310b22008-01-24 16:13:08 -05005520
David Sterbab159fa22016-11-08 18:09:03 +01005521void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5522 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005523{
5524 size_t cur;
5525 size_t offset;
5526 struct page *page;
5527 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005528 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5529 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005530
5531 WARN_ON(start > eb->len);
5532 WARN_ON(start + len > eb->start + eb->len);
5533
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005534 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005535
Chris Masond3977122009-01-05 21:25:51 -05005536 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005537 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005538 WARN_ON(!PageUptodate(page));
5539
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005540 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005541 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005542 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005543
5544 len -= cur;
5545 offset = 0;
5546 i++;
5547 }
5548}
Chris Masond1310b22008-01-24 16:13:08 -05005549
David Sterba58e80122016-11-08 18:30:31 +01005550void copy_extent_buffer_full(struct extent_buffer *dst,
5551 struct extent_buffer *src)
5552{
5553 int i;
David Sterbacc5e31a2018-03-01 18:20:27 +01005554 int num_pages;
David Sterba58e80122016-11-08 18:30:31 +01005555
5556 ASSERT(dst->len == src->len);
5557
David Sterba65ad0102018-06-29 10:56:49 +02005558 num_pages = num_extent_pages(dst);
David Sterba58e80122016-11-08 18:30:31 +01005559 for (i = 0; i < num_pages; i++)
5560 copy_page(page_address(dst->pages[i]),
5561 page_address(src->pages[i]));
5562}
5563
Chris Masond1310b22008-01-24 16:13:08 -05005564void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5565 unsigned long dst_offset, unsigned long src_offset,
5566 unsigned long len)
5567{
5568 u64 dst_len = dst->len;
5569 size_t cur;
5570 size_t offset;
5571 struct page *page;
5572 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005573 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
5574 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005575
5576 WARN_ON(src->len != dst_len);
5577
5578 offset = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005579 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005580
Chris Masond3977122009-01-05 21:25:51 -05005581 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005582 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005583 WARN_ON(!PageUptodate(page));
5584
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005585 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005586
Chris Masona6591712011-07-19 12:04:14 -04005587 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005588 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005589
5590 src_offset += cur;
5591 len -= cur;
5592 offset = 0;
5593 i++;
5594 }
5595}
Chris Masond1310b22008-01-24 16:13:08 -05005596
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005597/*
5598 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5599 * given bit number
5600 * @eb: the extent buffer
5601 * @start: offset of the bitmap item in the extent buffer
5602 * @nr: bit number
5603 * @page_index: return index of the page in the extent buffer that contains the
5604 * given bit number
5605 * @page_offset: return offset into the page given by page_index
5606 *
5607 * This helper hides the ugliness of finding the byte in an extent buffer which
5608 * contains a given bit.
5609 */
5610static inline void eb_bitmap_offset(struct extent_buffer *eb,
5611 unsigned long start, unsigned long nr,
5612 unsigned long *page_index,
5613 size_t *page_offset)
5614{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005615 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005616 size_t byte_offset = BIT_BYTE(nr);
5617 size_t offset;
5618
5619 /*
5620 * The byte we want is the offset of the extent buffer + the offset of
5621 * the bitmap item in the extent buffer + the offset of the byte in the
5622 * bitmap item.
5623 */
5624 offset = start_offset + start + byte_offset;
5625
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005626 *page_index = offset >> PAGE_SHIFT;
5627 *page_offset = offset & (PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005628}
5629
5630/**
5631 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5632 * @eb: the extent buffer
5633 * @start: offset of the bitmap item in the extent buffer
5634 * @nr: bit number to test
5635 */
5636int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5637 unsigned long nr)
5638{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005639 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005640 struct page *page;
5641 unsigned long i;
5642 size_t offset;
5643
5644 eb_bitmap_offset(eb, start, nr, &i, &offset);
5645 page = eb->pages[i];
5646 WARN_ON(!PageUptodate(page));
5647 kaddr = page_address(page);
5648 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5649}
5650
5651/**
5652 * extent_buffer_bitmap_set - set an area of a bitmap
5653 * @eb: the extent buffer
5654 * @start: offset of the bitmap item in the extent buffer
5655 * @pos: bit number of the first bit
5656 * @len: number of bits to set
5657 */
5658void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5659 unsigned long pos, unsigned long len)
5660{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005661 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005662 struct page *page;
5663 unsigned long i;
5664 size_t offset;
5665 const unsigned int size = pos + len;
5666 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005667 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005668
5669 eb_bitmap_offset(eb, start, pos, &i, &offset);
5670 page = eb->pages[i];
5671 WARN_ON(!PageUptodate(page));
5672 kaddr = page_address(page);
5673
5674 while (len >= bits_to_set) {
5675 kaddr[offset] |= mask_to_set;
5676 len -= bits_to_set;
5677 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005678 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005679 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005680 offset = 0;
5681 page = eb->pages[++i];
5682 WARN_ON(!PageUptodate(page));
5683 kaddr = page_address(page);
5684 }
5685 }
5686 if (len) {
5687 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5688 kaddr[offset] |= mask_to_set;
5689 }
5690}
5691
5692
5693/**
5694 * extent_buffer_bitmap_clear - clear an area of a bitmap
5695 * @eb: the extent buffer
5696 * @start: offset of the bitmap item in the extent buffer
5697 * @pos: bit number of the first bit
5698 * @len: number of bits to clear
5699 */
5700void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5701 unsigned long pos, unsigned long len)
5702{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005703 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005704 struct page *page;
5705 unsigned long i;
5706 size_t offset;
5707 const unsigned int size = pos + len;
5708 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005709 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005710
5711 eb_bitmap_offset(eb, start, pos, &i, &offset);
5712 page = eb->pages[i];
5713 WARN_ON(!PageUptodate(page));
5714 kaddr = page_address(page);
5715
5716 while (len >= bits_to_clear) {
5717 kaddr[offset] &= ~mask_to_clear;
5718 len -= bits_to_clear;
5719 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005720 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005721 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005722 offset = 0;
5723 page = eb->pages[++i];
5724 WARN_ON(!PageUptodate(page));
5725 kaddr = page_address(page);
5726 }
5727 }
5728 if (len) {
5729 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5730 kaddr[offset] &= ~mask_to_clear;
5731 }
5732}
5733
Sergei Trofimovich33872062011-04-11 21:52:52 +00005734static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5735{
5736 unsigned long distance = (src > dst) ? src - dst : dst - src;
5737 return distance < len;
5738}
5739
Chris Masond1310b22008-01-24 16:13:08 -05005740static void copy_pages(struct page *dst_page, struct page *src_page,
5741 unsigned long dst_off, unsigned long src_off,
5742 unsigned long len)
5743{
Chris Masona6591712011-07-19 12:04:14 -04005744 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005745 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005746 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005747
Sergei Trofimovich33872062011-04-11 21:52:52 +00005748 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005749 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005750 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005751 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005752 if (areas_overlap(src_off, dst_off, len))
5753 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005754 }
Chris Masond1310b22008-01-24 16:13:08 -05005755
Chris Mason727011e2010-08-06 13:21:20 -04005756 if (must_memmove)
5757 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5758 else
5759 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005760}
5761
5762void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5763 unsigned long src_offset, unsigned long len)
5764{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005765 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005766 size_t cur;
5767 size_t dst_off_in_page;
5768 size_t src_off_in_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005769 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005770 unsigned long dst_i;
5771 unsigned long src_i;
5772
5773 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005774 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005775 "memmove bogus src_offset %lu move len %lu dst len %lu",
5776 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005777 BUG_ON(1);
5778 }
5779 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005780 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005781 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5782 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005783 BUG_ON(1);
5784 }
5785
Chris Masond3977122009-01-05 21:25:51 -05005786 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005787 dst_off_in_page = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005788 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005789 src_off_in_page = (start_offset + src_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005790 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005791
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005792 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5793 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005794
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005795 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005796 src_off_in_page));
5797 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005798 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005799
David Sterbafb85fc92014-07-31 01:03:53 +02005800 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005801 dst_off_in_page, src_off_in_page, cur);
5802
5803 src_offset += cur;
5804 dst_offset += cur;
5805 len -= cur;
5806 }
5807}
Chris Masond1310b22008-01-24 16:13:08 -05005808
5809void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5810 unsigned long src_offset, unsigned long len)
5811{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005812 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005813 size_t cur;
5814 size_t dst_off_in_page;
5815 size_t src_off_in_page;
5816 unsigned long dst_end = dst_offset + len - 1;
5817 unsigned long src_end = src_offset + len - 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005818 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005819 unsigned long dst_i;
5820 unsigned long src_i;
5821
5822 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005823 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005824 "memmove bogus src_offset %lu move len %lu len %lu",
5825 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005826 BUG_ON(1);
5827 }
5828 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005829 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005830 "memmove bogus dst_offset %lu move len %lu len %lu",
5831 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005832 BUG_ON(1);
5833 }
Chris Mason727011e2010-08-06 13:21:20 -04005834 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005835 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5836 return;
5837 }
Chris Masond3977122009-01-05 21:25:51 -05005838 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005839 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
5840 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005841
5842 dst_off_in_page = (start_offset + dst_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005843 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005844 src_off_in_page = (start_offset + src_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005845 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005846
5847 cur = min_t(unsigned long, len, src_off_in_page + 1);
5848 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005849 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005850 dst_off_in_page - cur + 1,
5851 src_off_in_page - cur + 1, cur);
5852
5853 dst_end -= cur;
5854 src_end -= cur;
5855 len -= cur;
5856 }
5857}
Chris Mason6af118ce2008-07-22 11:18:07 -04005858
David Sterbaf7a52a42013-04-26 14:56:29 +00005859int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005860{
Chris Mason6af118ce2008-07-22 11:18:07 -04005861 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005862
Miao Xie19fe0a82010-10-26 20:57:29 -04005863 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005864 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05005865 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005866 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005867 spin_lock(&page->mapping->private_lock);
5868 if (!PagePrivate(page)) {
5869 spin_unlock(&page->mapping->private_lock);
5870 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005871 }
5872
Josef Bacik3083ee22012-03-09 16:01:49 -05005873 eb = (struct extent_buffer *)page->private;
5874 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005875
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005876 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005877 * This is a little awful but should be ok, we need to make sure that
5878 * the eb doesn't disappear out from under us while we're looking at
5879 * this page.
5880 */
5881 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005882 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005883 spin_unlock(&eb->refs_lock);
5884 spin_unlock(&page->mapping->private_lock);
5885 return 0;
5886 }
5887 spin_unlock(&page->mapping->private_lock);
5888
Josef Bacik3083ee22012-03-09 16:01:49 -05005889 /*
5890 * If tree ref isn't set then we know the ref on this eb is a real ref,
5891 * so just return, this page will likely be freed soon anyway.
5892 */
5893 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5894 spin_unlock(&eb->refs_lock);
5895 return 0;
5896 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005897
David Sterbaf7a52a42013-04-26 14:56:29 +00005898 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005899}