blob: 20af0efd7c17dba981cd5572eef3e6f9b5ba82b2 [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 Bacik251792012008-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 Bacik251792012008-10-29 14:49:05 -04001347 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001348 }
Josef Bacik251792012008-10-29 14:49:05 -04001349 return 1;
1350}
Josef Bacik251792012008-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 Masond1310b22008-01-24 16:13:08 -05001383/*
Chris Masond1310b22008-01-24 16:13:08 -05001384 * helper function to set both pages and extents in the tree writeback
1385 */
David Sterba35de6db2015-12-03 13:08:59 +01001386static void set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001387{
Josef Bacikc6100a42017-05-05 11:57:13 -04001388 tree->ops->set_range_writeback(tree->private_data, start, end);
Chris Masond1310b22008-01-24 16:13:08 -05001389}
Chris Masond1310b22008-01-24 16:13:08 -05001390
Chris Masond352ac62008-09-29 15:18:18 -04001391/* find the first state struct with 'bits' set after 'start', and
1392 * return it. tree->lock must be held. NULL will returned if
1393 * nothing was found after 'start'
1394 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001395static struct extent_state *
1396find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +01001397 u64 start, unsigned bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001398{
1399 struct rb_node *node;
1400 struct extent_state *state;
1401
1402 /*
1403 * this search will find all the extents that end after
1404 * our range starts.
1405 */
1406 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001407 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001408 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001409
Chris Masond3977122009-01-05 21:25:51 -05001410 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001411 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001412 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001413 return state;
Chris Masond3977122009-01-05 21:25:51 -05001414
Chris Masond7fc6402008-02-18 12:12:38 -05001415 node = rb_next(node);
1416 if (!node)
1417 break;
1418 }
1419out:
1420 return NULL;
1421}
Chris Masond7fc6402008-02-18 12:12:38 -05001422
Chris Masond352ac62008-09-29 15:18:18 -04001423/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001424 * find the first offset in the io tree with 'bits' set. zero is
1425 * returned if we find something, and *start_ret and *end_ret are
1426 * set to reflect the state struct that was found.
1427 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001428 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001429 */
1430int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba9ee49a042015-01-14 19:52:13 +01001431 u64 *start_ret, u64 *end_ret, unsigned bits,
Josef Bacike6138872012-09-27 17:07:30 -04001432 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001433{
1434 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001435 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001436 int ret = 1;
1437
1438 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001439 if (cached_state && *cached_state) {
1440 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001441 if (state->end == start - 1 && extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001442 n = rb_next(&state->rb_node);
1443 while (n) {
1444 state = rb_entry(n, struct extent_state,
1445 rb_node);
1446 if (state->state & bits)
1447 goto got_it;
1448 n = rb_next(n);
1449 }
1450 free_extent_state(*cached_state);
1451 *cached_state = NULL;
1452 goto out;
1453 }
1454 free_extent_state(*cached_state);
1455 *cached_state = NULL;
1456 }
1457
Xiao Guangrong69261c42011-07-14 03:19:45 +00001458 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001459got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001460 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001461 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001462 *start_ret = state->start;
1463 *end_ret = state->end;
1464 ret = 0;
1465 }
Josef Bacike6138872012-09-27 17:07:30 -04001466out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001467 spin_unlock(&tree->lock);
1468 return ret;
1469}
1470
1471/*
Chris Masond352ac62008-09-29 15:18:18 -04001472 * find a contiguous range of bytes in the file marked as delalloc, not
1473 * more than 'max_bytes'. start and end are used to return the range,
1474 *
1475 * 1 is returned if we find something, 0 if nothing was in the tree
1476 */
Chris Masonc8b97812008-10-29 14:49:59 -04001477static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001478 u64 *start, u64 *end, u64 max_bytes,
1479 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001480{
1481 struct rb_node *node;
1482 struct extent_state *state;
1483 u64 cur_start = *start;
1484 u64 found = 0;
1485 u64 total_bytes = 0;
1486
Chris Masoncad321a2008-12-17 14:51:42 -05001487 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001488
Chris Masond1310b22008-01-24 16:13:08 -05001489 /*
1490 * this search will find all the extents that end after
1491 * our range starts.
1492 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001493 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001494 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001495 if (!found)
1496 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001497 goto out;
1498 }
1499
Chris Masond3977122009-01-05 21:25:51 -05001500 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001501 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001502 if (found && (state->start != cur_start ||
1503 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001504 goto out;
1505 }
1506 if (!(state->state & EXTENT_DELALLOC)) {
1507 if (!found)
1508 *end = state->end;
1509 goto out;
1510 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001511 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001512 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001513 *cached_state = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +02001514 refcount_inc(&state->refs);
Josef Bacikc2a128d2010-02-02 21:19:11 +00001515 }
Chris Masond1310b22008-01-24 16:13:08 -05001516 found++;
1517 *end = state->end;
1518 cur_start = state->end + 1;
1519 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001520 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001521 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001522 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001523 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001524 break;
1525 }
1526out:
Chris Masoncad321a2008-12-17 14:51:42 -05001527 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001528 return found;
1529}
1530
Liu Boda2c7002017-02-10 16:41:05 +01001531static int __process_pages_contig(struct address_space *mapping,
1532 struct page *locked_page,
1533 pgoff_t start_index, pgoff_t end_index,
1534 unsigned long page_ops, pgoff_t *index_ret);
1535
Jeff Mahoney143bede2012-03-01 14:56:26 +01001536static noinline void __unlock_for_delalloc(struct inode *inode,
1537 struct page *locked_page,
1538 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001539{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001540 unsigned long index = start >> PAGE_SHIFT;
1541 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001542
Liu Bo76c00212017-02-10 16:42:14 +01001543 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001544 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001545 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001546
Liu Bo76c00212017-02-10 16:42:14 +01001547 __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1548 PAGE_UNLOCK, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -04001549}
1550
1551static noinline int lock_delalloc_pages(struct inode *inode,
1552 struct page *locked_page,
1553 u64 delalloc_start,
1554 u64 delalloc_end)
1555{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001556 unsigned long index = delalloc_start >> PAGE_SHIFT;
Liu Bo76c00212017-02-10 16:42:14 +01001557 unsigned long index_ret = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001558 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001559 int ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001560
Liu Bo76c00212017-02-10 16:42:14 +01001561 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001562 if (index == locked_page->index && index == end_index)
1563 return 0;
1564
Liu Bo76c00212017-02-10 16:42:14 +01001565 ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1566 end_index, PAGE_LOCK, &index_ret);
1567 if (ret == -EAGAIN)
1568 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1569 (u64)index_ret << PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001570 return ret;
1571}
1572
1573/*
1574 * find a contiguous range of bytes in the file marked as delalloc, not
1575 * more than 'max_bytes'. start and end are used to return the range,
1576 *
1577 * 1 is returned if we find something, 0 if nothing was in the tree
1578 */
Josef Bacik294e30f2013-10-09 12:00:56 -04001579STATIC u64 find_lock_delalloc_range(struct inode *inode,
1580 struct extent_io_tree *tree,
1581 struct page *locked_page, u64 *start,
1582 u64 *end, u64 max_bytes)
Chris Masonc8b97812008-10-29 14:49:59 -04001583{
1584 u64 delalloc_start;
1585 u64 delalloc_end;
1586 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001587 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001588 int ret;
1589 int loops = 0;
1590
1591again:
1592 /* step one, find a bunch of delalloc bytes starting at start */
1593 delalloc_start = *start;
1594 delalloc_end = 0;
1595 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001596 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001597 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001598 *start = delalloc_start;
1599 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001600 free_extent_state(cached_state);
Liu Bo385fe0b2013-10-01 23:49:49 +08001601 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001602 }
1603
1604 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001605 * start comes from the offset of locked_page. We have to lock
1606 * pages in order, so we can't process delalloc bytes before
1607 * locked_page
1608 */
Chris Masond3977122009-01-05 21:25:51 -05001609 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001610 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001611
1612 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001613 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001614 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001615 if (delalloc_end + 1 - delalloc_start > max_bytes)
1616 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001617
Chris Masonc8b97812008-10-29 14:49:59 -04001618 /* step two, lock all the pages after the page that has start */
1619 ret = lock_delalloc_pages(inode, locked_page,
1620 delalloc_start, delalloc_end);
1621 if (ret == -EAGAIN) {
1622 /* some of the pages are gone, lets avoid looping by
1623 * shortening the size of the delalloc range we're searching
1624 */
Chris Mason9655d292009-09-02 15:22:30 -04001625 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001626 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001627 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001628 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001629 loops = 1;
1630 goto again;
1631 } else {
1632 found = 0;
1633 goto out_failed;
1634 }
1635 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001636 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001637
1638 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001639 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001640
1641 /* then test to make sure it is all still delalloc */
1642 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001643 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001644 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001645 unlock_extent_cached(tree, delalloc_start, delalloc_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001646 &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001647 __unlock_for_delalloc(inode, locked_page,
1648 delalloc_start, delalloc_end);
1649 cond_resched();
1650 goto again;
1651 }
Chris Mason9655d292009-09-02 15:22:30 -04001652 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001653 *start = delalloc_start;
1654 *end = delalloc_end;
1655out_failed:
1656 return found;
1657}
1658
Liu Boda2c7002017-02-10 16:41:05 +01001659static int __process_pages_contig(struct address_space *mapping,
1660 struct page *locked_page,
1661 pgoff_t start_index, pgoff_t end_index,
1662 unsigned long page_ops, pgoff_t *index_ret)
Chris Masonc8b97812008-10-29 14:49:59 -04001663{
Liu Bo873695b2017-02-02 17:49:22 -08001664 unsigned long nr_pages = end_index - start_index + 1;
Liu Boda2c7002017-02-10 16:41:05 +01001665 unsigned long pages_locked = 0;
Liu Bo873695b2017-02-02 17:49:22 -08001666 pgoff_t index = start_index;
Chris Masonc8b97812008-10-29 14:49:59 -04001667 struct page *pages[16];
Liu Bo873695b2017-02-02 17:49:22 -08001668 unsigned ret;
Liu Boda2c7002017-02-10 16:41:05 +01001669 int err = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001670 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001671
Liu Boda2c7002017-02-10 16:41:05 +01001672 if (page_ops & PAGE_LOCK) {
1673 ASSERT(page_ops == PAGE_LOCK);
1674 ASSERT(index_ret && *index_ret == start_index);
1675 }
1676
Filipe Manana704de492014-10-06 22:14:22 +01001677 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
Liu Bo873695b2017-02-02 17:49:22 -08001678 mapping_set_error(mapping, -EIO);
Filipe Manana704de492014-10-06 22:14:22 +01001679
Chris Masond3977122009-01-05 21:25:51 -05001680 while (nr_pages > 0) {
Liu Bo873695b2017-02-02 17:49:22 -08001681 ret = find_get_pages_contig(mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001682 min_t(unsigned long,
1683 nr_pages, ARRAY_SIZE(pages)), pages);
Liu Boda2c7002017-02-10 16:41:05 +01001684 if (ret == 0) {
1685 /*
1686 * Only if we're going to lock these pages,
1687 * can we find nothing at @index.
1688 */
1689 ASSERT(page_ops & PAGE_LOCK);
Liu Bo49d4a332017-03-06 18:20:56 -08001690 err = -EAGAIN;
1691 goto out;
Liu Boda2c7002017-02-10 16:41:05 +01001692 }
Chris Mason8b62b722009-09-02 16:53:46 -04001693
Liu Boda2c7002017-02-10 16:41:05 +01001694 for (i = 0; i < ret; i++) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001695 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001696 SetPagePrivate2(pages[i]);
1697
Chris Masonc8b97812008-10-29 14:49:59 -04001698 if (pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001699 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001700 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001701 continue;
1702 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001703 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001704 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001705 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001706 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001707 if (page_ops & PAGE_SET_ERROR)
1708 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001709 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001710 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001711 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001712 unlock_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001713 if (page_ops & PAGE_LOCK) {
1714 lock_page(pages[i]);
1715 if (!PageDirty(pages[i]) ||
1716 pages[i]->mapping != mapping) {
1717 unlock_page(pages[i]);
1718 put_page(pages[i]);
1719 err = -EAGAIN;
1720 goto out;
1721 }
1722 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001723 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001724 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001725 }
1726 nr_pages -= ret;
1727 index += ret;
1728 cond_resched();
1729 }
Liu Boda2c7002017-02-10 16:41:05 +01001730out:
1731 if (err && index_ret)
1732 *index_ret = start_index + pages_locked - 1;
1733 return err;
Chris Masonc8b97812008-10-29 14:49:59 -04001734}
Chris Masonc8b97812008-10-29 14:49:59 -04001735
Liu Bo873695b2017-02-02 17:49:22 -08001736void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1737 u64 delalloc_end, struct page *locked_page,
1738 unsigned clear_bits,
1739 unsigned long page_ops)
1740{
1741 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001742 NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001743
1744 __process_pages_contig(inode->i_mapping, locked_page,
1745 start >> PAGE_SHIFT, end >> PAGE_SHIFT,
Liu Boda2c7002017-02-10 16:41:05 +01001746 page_ops, NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001747}
1748
Chris Masond352ac62008-09-29 15:18:18 -04001749/*
1750 * count the number of bytes in the tree that have a given bit(s)
1751 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1752 * cached. The total number found is returned.
1753 */
Chris Masond1310b22008-01-24 16:13:08 -05001754u64 count_range_bits(struct extent_io_tree *tree,
1755 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001756 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001757{
1758 struct rb_node *node;
1759 struct extent_state *state;
1760 u64 cur_start = *start;
1761 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001762 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001763 int found = 0;
1764
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301765 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001766 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001767
Chris Masoncad321a2008-12-17 14:51:42 -05001768 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001769 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1770 total_bytes = tree->dirty_bytes;
1771 goto out;
1772 }
1773 /*
1774 * this search will find all the extents that end after
1775 * our range starts.
1776 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001777 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001778 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001779 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001780
Chris Masond3977122009-01-05 21:25:51 -05001781 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001782 state = rb_entry(node, struct extent_state, rb_node);
1783 if (state->start > search_end)
1784 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001785 if (contig && found && state->start > last + 1)
1786 break;
1787 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001788 total_bytes += min(search_end, state->end) + 1 -
1789 max(cur_start, state->start);
1790 if (total_bytes >= max_bytes)
1791 break;
1792 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001793 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001794 found = 1;
1795 }
Chris Masonec29ed52011-02-23 16:23:20 -05001796 last = state->end;
1797 } else if (contig && found) {
1798 break;
Chris Masond1310b22008-01-24 16:13:08 -05001799 }
1800 node = rb_next(node);
1801 if (!node)
1802 break;
1803 }
1804out:
Chris Masoncad321a2008-12-17 14:51:42 -05001805 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001806 return total_bytes;
1807}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001808
Chris Masond352ac62008-09-29 15:18:18 -04001809/*
1810 * set the private field for a given byte offset in the tree. If there isn't
1811 * an extent_state there already, this does nothing.
1812 */
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001813static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001814 struct io_failure_record *failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001815{
1816 struct rb_node *node;
1817 struct extent_state *state;
1818 int ret = 0;
1819
Chris Masoncad321a2008-12-17 14:51:42 -05001820 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001821 /*
1822 * this search will find all the extents that end after
1823 * our range starts.
1824 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001825 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001826 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001827 ret = -ENOENT;
1828 goto out;
1829 }
1830 state = rb_entry(node, struct extent_state, rb_node);
1831 if (state->start != start) {
1832 ret = -ENOENT;
1833 goto out;
1834 }
David Sterba47dc1962016-02-11 13:24:13 +01001835 state->failrec = failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001836out:
Chris Masoncad321a2008-12-17 14:51:42 -05001837 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001838 return ret;
1839}
1840
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001841static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001842 struct io_failure_record **failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001843{
1844 struct rb_node *node;
1845 struct extent_state *state;
1846 int ret = 0;
1847
Chris Masoncad321a2008-12-17 14:51:42 -05001848 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001849 /*
1850 * this search will find all the extents that end after
1851 * our range starts.
1852 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001853 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001854 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001855 ret = -ENOENT;
1856 goto out;
1857 }
1858 state = rb_entry(node, struct extent_state, rb_node);
1859 if (state->start != start) {
1860 ret = -ENOENT;
1861 goto out;
1862 }
David Sterba47dc1962016-02-11 13:24:13 +01001863 *failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001864out:
Chris Masoncad321a2008-12-17 14:51:42 -05001865 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001866 return ret;
1867}
1868
1869/*
1870 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001871 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001872 * has the bits set. Otherwise, 1 is returned if any bit in the
1873 * range is found set.
1874 */
1875int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001876 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001877{
1878 struct extent_state *state = NULL;
1879 struct rb_node *node;
1880 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001881
Chris Masoncad321a2008-12-17 14:51:42 -05001882 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01001883 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001884 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001885 node = &cached->rb_node;
1886 else
1887 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001888 while (node && start <= end) {
1889 state = rb_entry(node, struct extent_state, rb_node);
1890
1891 if (filled && state->start > start) {
1892 bitset = 0;
1893 break;
1894 }
1895
1896 if (state->start > end)
1897 break;
1898
1899 if (state->state & bits) {
1900 bitset = 1;
1901 if (!filled)
1902 break;
1903 } else if (filled) {
1904 bitset = 0;
1905 break;
1906 }
Chris Mason46562cec2009-09-23 20:23:16 -04001907
1908 if (state->end == (u64)-1)
1909 break;
1910
Chris Masond1310b22008-01-24 16:13:08 -05001911 start = state->end + 1;
1912 if (start > end)
1913 break;
1914 node = rb_next(node);
1915 if (!node) {
1916 if (filled)
1917 bitset = 0;
1918 break;
1919 }
1920 }
Chris Masoncad321a2008-12-17 14:51:42 -05001921 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001922 return bitset;
1923}
Chris Masond1310b22008-01-24 16:13:08 -05001924
1925/*
1926 * helper function to set a given page up to date if all the
1927 * extents in the tree for that page are up to date
1928 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001929static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001930{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001931 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001932 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001933 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001934 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001935}
1936
Josef Bacik7870d082017-05-05 11:57:15 -04001937int free_io_failure(struct extent_io_tree *failure_tree,
1938 struct extent_io_tree *io_tree,
1939 struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001940{
1941 int ret;
1942 int err = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001943
David Sterba47dc1962016-02-11 13:24:13 +01001944 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001945 ret = clear_extent_bits(failure_tree, rec->start,
1946 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001947 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001948 if (ret)
1949 err = ret;
1950
Josef Bacik7870d082017-05-05 11:57:15 -04001951 ret = clear_extent_bits(io_tree, rec->start,
David Woodhouse53b381b2013-01-29 18:40:14 -05001952 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001953 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05001954 if (ret && !err)
1955 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001956
1957 kfree(rec);
1958 return err;
1959}
1960
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001961/*
1962 * this bypasses the standard btrfs submit functions deliberately, as
1963 * the standard behavior is to write all copies in a raid setup. here we only
1964 * want to write the one bad copy. so we do the mapping for ourselves and issue
1965 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001966 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001967 * actually prevents the read that triggered the error from finishing.
1968 * currently, there can be no more than two copies of every data bit. thus,
1969 * exactly one rewrite is required.
1970 */
Josef Bacik6ec656b2017-05-05 11:57:14 -04001971int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
1972 u64 length, u64 logical, struct page *page,
1973 unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001974{
1975 struct bio *bio;
1976 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001977 u64 map_length = 0;
1978 u64 sector;
1979 struct btrfs_bio *bbio = NULL;
1980 int ret;
1981
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001982 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001983 BUG_ON(!mirror_num);
1984
David Sterbac5e4c3d2017-06-12 17:29:41 +02001985 bio = btrfs_io_bio_alloc(1);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001986 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001987 map_length = length;
1988
Filipe Mananab5de8d02016-05-27 22:21:27 +01001989 /*
1990 * Avoid races with device replace and make sure our bbio has devices
1991 * associated to its stripes that don't go away while we are doing the
1992 * read repair operation.
1993 */
1994 btrfs_bio_counter_inc_blocked(fs_info);
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03001995 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
Liu Boc7253282017-03-29 10:53:58 -07001996 /*
1997 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
1998 * to update all raid stripes, but here we just want to correct
1999 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2000 * stripe's dev and sector.
2001 */
2002 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2003 &map_length, &bbio, 0);
2004 if (ret) {
2005 btrfs_bio_counter_dec(fs_info);
2006 bio_put(bio);
2007 return -EIO;
2008 }
2009 ASSERT(bbio->mirror_num == 1);
2010 } else {
2011 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2012 &map_length, &bbio, mirror_num);
2013 if (ret) {
2014 btrfs_bio_counter_dec(fs_info);
2015 bio_put(bio);
2016 return -EIO;
2017 }
2018 BUG_ON(mirror_num != bbio->mirror_num);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002019 }
Liu Boc7253282017-03-29 10:53:58 -07002020
2021 sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002022 bio->bi_iter.bi_sector = sector;
Liu Boc7253282017-03-29 10:53:58 -07002023 dev = bbio->stripes[bbio->mirror_num - 1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002024 btrfs_put_bbio(bbio);
Anand Jainebbede42017-12-04 12:54:52 +08002025 if (!dev || !dev->bdev ||
2026 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002027 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002028 bio_put(bio);
2029 return -EIO;
2030 }
Christoph Hellwig74d46992017-08-23 19:10:32 +02002031 bio_set_dev(bio, dev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002032 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Miao Xieffdd2012014-09-12 18:44:00 +08002033 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002034
Mike Christie4e49ea42016-06-05 14:31:41 -05002035 if (btrfsic_submit_bio_wait(bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002036 /* try to remap that extent elsewhere? */
Filipe Mananab5de8d02016-05-27 22:21:27 +01002037 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002038 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002039 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002040 return -EIO;
2041 }
2042
David Sterbab14af3b2015-10-08 10:43:10 +02002043 btrfs_info_rl_in_rcu(fs_info,
2044 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Josef Bacik6ec656b2017-05-05 11:57:14 -04002045 ino, start,
Miao Xie1203b682014-09-12 18:44:01 +08002046 rcu_str_deref(dev->name), sector);
Filipe Mananab5de8d02016-05-27 22:21:27 +01002047 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002048 bio_put(bio);
2049 return 0;
2050}
2051
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002052int repair_eb_io_failure(struct btrfs_fs_info *fs_info,
2053 struct extent_buffer *eb, int mirror_num)
Josef Bacikea466792012-03-26 21:57:36 -04002054{
Josef Bacikea466792012-03-26 21:57:36 -04002055 u64 start = eb->start;
David Sterbacc5e31a2018-03-01 18:20:27 +01002056 int i, num_pages = num_extent_pages(eb);
Chris Masond95603b2012-04-12 15:55:15 -04002057 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002058
David Howellsbc98a422017-07-17 08:45:34 +01002059 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002060 return -EROFS;
2061
Josef Bacikea466792012-03-26 21:57:36 -04002062 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002063 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002064
Josef Bacik6ec656b2017-05-05 11:57:14 -04002065 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002066 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002067 if (ret)
2068 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002069 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002070 }
2071
2072 return ret;
2073}
2074
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002075/*
2076 * each time an IO finishes, we do a fast check in the IO failure tree
2077 * to see if we need to process or clean up an io_failure_record
2078 */
Josef Bacik7870d082017-05-05 11:57:15 -04002079int clean_io_failure(struct btrfs_fs_info *fs_info,
2080 struct extent_io_tree *failure_tree,
2081 struct extent_io_tree *io_tree, u64 start,
2082 struct page *page, u64 ino, unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002083{
2084 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002085 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002086 struct extent_state *state;
2087 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002088 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002089
2090 private = 0;
Josef Bacik7870d082017-05-05 11:57:15 -04002091 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2092 EXTENT_DIRTY, 0);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002093 if (!ret)
2094 return 0;
2095
Josef Bacik7870d082017-05-05 11:57:15 -04002096 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002097 if (ret)
2098 return 0;
2099
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002100 BUG_ON(!failrec->this_mirror);
2101
2102 if (failrec->in_validation) {
2103 /* there was no real error, just free the record */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002104 btrfs_debug(fs_info,
2105 "clean_io_failure: freeing dummy error at %llu",
2106 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002107 goto out;
2108 }
David Howellsbc98a422017-07-17 08:45:34 +01002109 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002110 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002111
Josef Bacik7870d082017-05-05 11:57:15 -04002112 spin_lock(&io_tree->lock);
2113 state = find_first_extent_bit_state(io_tree,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002114 failrec->start,
2115 EXTENT_LOCKED);
Josef Bacik7870d082017-05-05 11:57:15 -04002116 spin_unlock(&io_tree->lock);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002117
Miao Xie883d0de2013-07-25 19:22:35 +08002118 if (state && state->start <= failrec->start &&
2119 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002120 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2121 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002122 if (num_copies > 1) {
Josef Bacik7870d082017-05-05 11:57:15 -04002123 repair_io_failure(fs_info, ino, start, failrec->len,
2124 failrec->logical, page, pg_offset,
2125 failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002126 }
2127 }
2128
2129out:
Josef Bacik7870d082017-05-05 11:57:15 -04002130 free_io_failure(failure_tree, io_tree, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002131
Miao Xie454ff3d2014-09-12 18:43:58 +08002132 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002133}
2134
Miao Xief6124962014-09-12 18:44:04 +08002135/*
2136 * Can be called when
2137 * - hold extent lock
2138 * - under ordered extent
2139 * - the inode is freeing
2140 */
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002141void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
Miao Xief6124962014-09-12 18:44:04 +08002142{
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002143 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
Miao Xief6124962014-09-12 18:44:04 +08002144 struct io_failure_record *failrec;
2145 struct extent_state *state, *next;
2146
2147 if (RB_EMPTY_ROOT(&failure_tree->state))
2148 return;
2149
2150 spin_lock(&failure_tree->lock);
2151 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2152 while (state) {
2153 if (state->start > end)
2154 break;
2155
2156 ASSERT(state->end <= end);
2157
2158 next = next_state(state);
2159
David Sterba47dc1962016-02-11 13:24:13 +01002160 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002161 free_extent_state(state);
2162 kfree(failrec);
2163
2164 state = next;
2165 }
2166 spin_unlock(&failure_tree->lock);
2167}
2168
Miao Xie2fe63032014-09-12 18:43:59 +08002169int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
David Sterba47dc1962016-02-11 13:24:13 +01002170 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002171{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002172 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002173 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002174 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002175 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2176 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2177 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002178 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002179 u64 logical;
2180
David Sterba47dc1962016-02-11 13:24:13 +01002181 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002182 if (ret) {
2183 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2184 if (!failrec)
2185 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002186
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002187 failrec->start = start;
2188 failrec->len = end - start + 1;
2189 failrec->this_mirror = 0;
2190 failrec->bio_flags = 0;
2191 failrec->in_validation = 0;
2192
2193 read_lock(&em_tree->lock);
2194 em = lookup_extent_mapping(em_tree, start, failrec->len);
2195 if (!em) {
2196 read_unlock(&em_tree->lock);
2197 kfree(failrec);
2198 return -EIO;
2199 }
2200
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002201 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002202 free_extent_map(em);
2203 em = NULL;
2204 }
2205 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002206 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002207 kfree(failrec);
2208 return -EIO;
2209 }
Miao Xie2fe63032014-09-12 18:43:59 +08002210
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002211 logical = start - em->start;
2212 logical = em->block_start + logical;
2213 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2214 logical = em->block_start;
2215 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2216 extent_set_compress_type(&failrec->bio_flags,
2217 em->compress_type);
2218 }
Miao Xie2fe63032014-09-12 18:43:59 +08002219
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002220 btrfs_debug(fs_info,
2221 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2222 logical, start, failrec->len);
Miao Xie2fe63032014-09-12 18:43:59 +08002223
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002224 failrec->logical = logical;
2225 free_extent_map(em);
2226
2227 /* set the bits in the private failure tree */
2228 ret = set_extent_bits(failure_tree, start, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002229 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002230 if (ret >= 0)
David Sterba47dc1962016-02-11 13:24:13 +01002231 ret = set_state_failrec(failure_tree, start, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002232 /* set the bits in the inode's tree */
2233 if (ret >= 0)
David Sterbaceeb0ae2016-04-26 23:54:39 +02002234 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002235 if (ret < 0) {
2236 kfree(failrec);
2237 return ret;
2238 }
2239 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002240 btrfs_debug(fs_info,
2241 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2242 failrec->logical, failrec->start, failrec->len,
2243 failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002244 /*
2245 * when data can be on disk more than twice, add to failrec here
2246 * (e.g. with a list for failed_mirror) to make
2247 * clean_io_failure() clean all those errors at once.
2248 */
2249 }
Miao Xie2fe63032014-09-12 18:43:59 +08002250
2251 *failrec_ret = failrec;
2252
2253 return 0;
2254}
2255
Ming Leia0b60d72017-12-18 20:22:11 +08002256bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages,
Miao Xie2fe63032014-09-12 18:43:59 +08002257 struct io_failure_record *failrec, int failed_mirror)
2258{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002259 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002260 int num_copies;
2261
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002262 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002263 if (num_copies == 1) {
2264 /*
2265 * we only have a single copy of the data, so don't bother with
2266 * all the retry and error correction code that follows. no
2267 * matter what the error is, it is very likely to persist.
2268 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002269 btrfs_debug(fs_info,
2270 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2271 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002272 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002273 }
2274
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002275 /*
2276 * there are two premises:
2277 * a) deliver good data to the caller
2278 * b) correct the bad sectors on disk
2279 */
Ming Leia0b60d72017-12-18 20:22:11 +08002280 if (failed_bio_pages > 1) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002281 /*
2282 * to fulfill b), we need to know the exact failing sectors, as
2283 * we don't want to rewrite any more than the failed ones. thus,
2284 * we need separate read requests for the failed bio
2285 *
2286 * if the following BUG_ON triggers, our validation request got
2287 * merged. we need separate requests for our algorithm to work.
2288 */
2289 BUG_ON(failrec->in_validation);
2290 failrec->in_validation = 1;
2291 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002292 } else {
2293 /*
2294 * we're ready to fulfill a) and b) alongside. get a good copy
2295 * of the failed sector and if we succeed, we have setup
2296 * everything for repair_io_failure to do the rest for us.
2297 */
2298 if (failrec->in_validation) {
2299 BUG_ON(failrec->this_mirror != failed_mirror);
2300 failrec->in_validation = 0;
2301 failrec->this_mirror = 0;
2302 }
2303 failrec->failed_mirror = failed_mirror;
2304 failrec->this_mirror++;
2305 if (failrec->this_mirror == failed_mirror)
2306 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002307 }
2308
Miao Xiefacc8a222013-07-25 19:22:34 +08002309 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002310 btrfs_debug(fs_info,
2311 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2312 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002313 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002314 }
2315
Liu Boc3cfb652017-07-13 15:00:50 -07002316 return true;
Miao Xie2fe63032014-09-12 18:43:59 +08002317}
2318
2319
2320struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2321 struct io_failure_record *failrec,
2322 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002323 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002324{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002325 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002326 struct bio *bio;
2327 struct btrfs_io_bio *btrfs_failed_bio;
2328 struct btrfs_io_bio *btrfs_bio;
2329
David Sterbac5e4c3d2017-06-12 17:29:41 +02002330 bio = btrfs_io_bio_alloc(1);
Miao Xie2fe63032014-09-12 18:43:59 +08002331 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002332 bio->bi_iter.bi_sector = failrec->logical >> 9;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002333 bio_set_dev(bio, fs_info->fs_devices->latest_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002334 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002335 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002336
Miao Xiefacc8a222013-07-25 19:22:34 +08002337 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2338 if (btrfs_failed_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002339 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2340
2341 btrfs_bio = btrfs_io_bio(bio);
2342 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002343 icsum *= csum_size;
2344 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002345 csum_size);
2346 }
2347
Miao Xie2fe63032014-09-12 18:43:59 +08002348 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002349
Miao Xie2fe63032014-09-12 18:43:59 +08002350 return bio;
2351}
2352
2353/*
2354 * this is a generic handler for readpage errors (default
2355 * readpage_io_failed_hook). if other copies exist, read those and write back
2356 * good data to the failed position. does not investigate in remapping the
2357 * failed extent elsewhere, hoping the device will be smart enough to do this as
2358 * needed
2359 */
2360
2361static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2362 struct page *page, u64 start, u64 end,
2363 int failed_mirror)
2364{
2365 struct io_failure_record *failrec;
2366 struct inode *inode = page->mapping->host;
2367 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002368 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie2fe63032014-09-12 18:43:59 +08002369 struct bio *bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002370 int read_mode = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002371 blk_status_t status;
Miao Xie2fe63032014-09-12 18:43:59 +08002372 int ret;
Ming Leia0b60d72017-12-18 20:22:11 +08002373 unsigned failed_bio_pages = bio_pages_all(failed_bio);
Miao Xie2fe63032014-09-12 18:43:59 +08002374
Mike Christie1f7ad752016-06-05 14:31:51 -05002375 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002376
2377 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2378 if (ret)
2379 return ret;
2380
Ming Leia0b60d72017-12-18 20:22:11 +08002381 if (!btrfs_check_repairable(inode, failed_bio_pages, failrec,
Liu Boc3cfb652017-07-13 15:00:50 -07002382 failed_mirror)) {
Josef Bacik7870d082017-05-05 11:57:15 -04002383 free_io_failure(failure_tree, tree, failrec);
Miao Xie2fe63032014-09-12 18:43:59 +08002384 return -EIO;
2385 }
2386
Ming Leia0b60d72017-12-18 20:22:11 +08002387 if (failed_bio_pages > 1)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002388 read_mode |= REQ_FAILFAST_DEV;
Miao Xie2fe63032014-09-12 18:43:59 +08002389
2390 phy_offset >>= inode->i_sb->s_blocksize_bits;
2391 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2392 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002393 (int)phy_offset, failed_bio->bi_end_io,
2394 NULL);
David Sterbaebcc3262018-06-29 10:56:53 +02002395 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie2fe63032014-09-12 18:43:59 +08002396
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002397 btrfs_debug(btrfs_sb(inode->i_sb),
2398 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2399 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002400
Linus Torvalds8c27cb32017-07-05 16:41:23 -07002401 status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002402 failrec->bio_flags, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002403 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04002404 free_io_failure(failure_tree, tree, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002405 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002406 ret = blk_status_to_errno(status);
Miao Xie6c387ab2014-09-12 18:43:57 +08002407 }
2408
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002409 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002410}
2411
Chris Masond1310b22008-01-24 16:13:08 -05002412/* lots and lots of room for performance fixes in the end_bio funcs */
2413
David Sterbab5227c02015-12-03 13:08:59 +01002414void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002415{
2416 int uptodate = (err == 0);
2417 struct extent_io_tree *tree;
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002418 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002419
2420 tree = &BTRFS_I(page->mapping->host)->io_tree;
2421
David Sterbac3988d62017-02-17 15:18:32 +01002422 if (tree->ops && tree->ops->writepage_end_io_hook)
2423 tree->ops->writepage_end_io_hook(page, start, end, NULL,
2424 uptodate);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002425
Jeff Mahoney87826df2012-02-15 16:23:57 +01002426 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002427 ClearPageUptodate(page);
2428 SetPageError(page);
Colin Ian Kingbff5baf2017-05-09 18:14:01 +01002429 ret = err < 0 ? err : -EIO;
Liu Bo5dca6ee2014-05-12 12:47:36 +08002430 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002431 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002432}
2433
Chris Masond1310b22008-01-24 16:13:08 -05002434/*
2435 * after a writepage IO is done, we need to:
2436 * clear the uptodate bits on error
2437 * clear the writeback bits in the extent tree for this IO
2438 * end_page_writeback if the page has no more pending IO
2439 *
2440 * Scheduling is not allowed, so the extent state tree is expected
2441 * to have one and only one object corresponding to this IO.
2442 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002443static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002444{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002445 int error = blk_status_to_errno(bio->bi_status);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002446 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002447 u64 start;
2448 u64 end;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002449 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002450
David Sterbac09abff2017-07-13 18:10:07 +02002451 ASSERT(!bio_flagged(bio, BIO_CLONED));
Kent Overstreet2c30c712013-11-07 12:20:26 -08002452 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002453 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002454 struct inode *inode = page->mapping->host;
2455 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002456
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002457 /* We always issue full-page reads, but if some block
2458 * in a page fails to read, blk_update_request() will
2459 * advance bv_offset and adjust bv_len to compensate.
2460 * Print a warning for nonzero offsets, and an error
2461 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002462 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2463 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002464 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002465 "partial page write in btrfs with offset %u and length %u",
2466 bvec->bv_offset, bvec->bv_len);
2467 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002468 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002469 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002470 bvec->bv_offset, bvec->bv_len);
2471 }
Chris Masond1310b22008-01-24 16:13:08 -05002472
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002473 start = page_offset(page);
2474 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002475
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002476 end_extent_writepage(page, error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002477 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002478 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002479
Chris Masond1310b22008-01-24 16:13:08 -05002480 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002481}
2482
Miao Xie883d0de2013-07-25 19:22:35 +08002483static void
2484endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2485 int uptodate)
2486{
2487 struct extent_state *cached = NULL;
2488 u64 end = start + len - 1;
2489
2490 if (uptodate && tree->track_uptodate)
2491 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
David Sterbad810a4b2017-12-07 18:52:54 +01002492 unlock_extent_cached_atomic(tree, start, end, &cached);
Miao Xie883d0de2013-07-25 19:22:35 +08002493}
2494
Chris Masond1310b22008-01-24 16:13:08 -05002495/*
2496 * after a readpage IO is done, we need to:
2497 * clear the uptodate bits on error
2498 * set the uptodate bits if things worked
2499 * set the page up to date if all extents in the tree are uptodate
2500 * clear the lock bit in the extent tree
2501 * unlock the page if there are no other extents locked for it
2502 *
2503 * Scheduling is not allowed, so the extent state tree is expected
2504 * to have one and only one object corresponding to this IO.
2505 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002506static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002507{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002508 struct bio_vec *bvec;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002509 int uptodate = !bio->bi_status;
Miao Xiefacc8a222013-07-25 19:22:34 +08002510 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04002511 struct extent_io_tree *tree, *failure_tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002512 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002513 u64 start;
2514 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002515 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002516 u64 extent_start = 0;
2517 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002518 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002519 int ret;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002520 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002521
David Sterbac09abff2017-07-13 18:10:07 +02002522 ASSERT(!bio_flagged(bio, BIO_CLONED));
Kent Overstreet2c30c712013-11-07 12:20:26 -08002523 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002524 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002525 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002526 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen507903b2011-04-06 10:02:20 +00002527
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002528 btrfs_debug(fs_info,
2529 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002530 (u64)bio->bi_iter.bi_sector, bio->bi_status,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002531 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002532 tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002533 failure_tree = &BTRFS_I(inode)->io_failure_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002534
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002535 /* We always issue full-page reads, but if some block
2536 * in a page fails to read, blk_update_request() will
2537 * advance bv_offset and adjust bv_len to compensate.
2538 * Print a warning for nonzero offsets, and an error
2539 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002540 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2541 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002542 btrfs_err(fs_info,
2543 "partial page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002544 bvec->bv_offset, bvec->bv_len);
2545 else
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002546 btrfs_info(fs_info,
2547 "incomplete page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002548 bvec->bv_offset, bvec->bv_len);
2549 }
Chris Masond1310b22008-01-24 16:13:08 -05002550
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002551 start = page_offset(page);
2552 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002553 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002554
Chris Mason9be33952013-05-17 18:30:14 -04002555 mirror = io_bio->mirror_num;
David Sterba20c98012017-02-17 15:59:35 +01002556 if (likely(uptodate && tree->ops)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002557 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2558 page, start, end,
2559 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002560 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002561 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002562 else
Josef Bacik7870d082017-05-05 11:57:15 -04002563 clean_io_failure(BTRFS_I(inode)->root->fs_info,
2564 failure_tree, tree, start,
2565 page,
2566 btrfs_ino(BTRFS_I(inode)), 0);
Chris Masond1310b22008-01-24 16:13:08 -05002567 }
Josef Bacikea466792012-03-26 21:57:36 -04002568
Miao Xief2a09da2013-07-25 19:22:33 +08002569 if (likely(uptodate))
2570 goto readpage_ok;
2571
David Sterba20a7db82017-02-17 16:24:29 +01002572 if (tree->ops) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002573 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Liu Bo9d0d1c82017-03-24 15:04:50 -07002574 if (ret == -EAGAIN) {
2575 /*
2576 * Data inode's readpage_io_failed_hook() always
2577 * returns -EAGAIN.
2578 *
2579 * The generic bio_readpage_error handles errors
2580 * the following way: If possible, new read
2581 * requests are created and submitted and will
2582 * end up in end_bio_extent_readpage as well (if
2583 * we're lucky, not in the !uptodate case). In
2584 * that case it returns 0 and we just go on with
2585 * the next page in our bio. If it can't handle
2586 * the error it will return -EIO and we remain
2587 * responsible for that page.
2588 */
2589 ret = bio_readpage_error(bio, offset, page,
2590 start, end, mirror);
2591 if (ret == 0) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002592 uptodate = !bio->bi_status;
Liu Bo9d0d1c82017-03-24 15:04:50 -07002593 offset += len;
2594 continue;
2595 }
Chris Mason7e383262008-04-09 16:28:12 -04002596 }
Liu Bo9d0d1c82017-03-24 15:04:50 -07002597
2598 /*
2599 * metadata's readpage_io_failed_hook() always returns
2600 * -EIO and fixes nothing. -EIO is also returned if
2601 * data inode error could not be fixed.
2602 */
2603 ASSERT(ret == -EIO);
Chris Mason7e383262008-04-09 16:28:12 -04002604 }
Miao Xief2a09da2013-07-25 19:22:33 +08002605readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002606 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002607 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002608 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002609 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002610
2611 /* Zero out the end if this page straddles i_size */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002612 off = i_size & (PAGE_SIZE-1);
Liu Boa583c022014-08-19 23:32:22 +08002613 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002614 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002615 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002616 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002617 ClearPageUptodate(page);
2618 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002619 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002620 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002621 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002622
2623 if (unlikely(!uptodate)) {
2624 if (extent_len) {
2625 endio_readpage_release_extent(tree,
2626 extent_start,
2627 extent_len, 1);
2628 extent_start = 0;
2629 extent_len = 0;
2630 }
2631 endio_readpage_release_extent(tree, start,
2632 end - start + 1, 0);
2633 } else if (!extent_len) {
2634 extent_start = start;
2635 extent_len = end + 1 - start;
2636 } else if (extent_start + extent_len == start) {
2637 extent_len += end + 1 - start;
2638 } else {
2639 endio_readpage_release_extent(tree, extent_start,
2640 extent_len, uptodate);
2641 extent_start = start;
2642 extent_len = end + 1 - start;
2643 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002644 }
Chris Masond1310b22008-01-24 16:13:08 -05002645
Miao Xie883d0de2013-07-25 19:22:35 +08002646 if (extent_len)
2647 endio_readpage_release_extent(tree, extent_start, extent_len,
2648 uptodate);
Miao Xiefacc8a222013-07-25 19:22:34 +08002649 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002650 io_bio->end_io(io_bio, blk_status_to_errno(bio->bi_status));
Chris Masond1310b22008-01-24 16:13:08 -05002651 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002652}
2653
Chris Mason9be33952013-05-17 18:30:14 -04002654/*
David Sterba184f9992017-06-12 17:29:39 +02002655 * Initialize the members up to but not including 'bio'. Use after allocating a
2656 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
2657 * 'bio' because use of __GFP_ZERO is not supported.
Chris Mason9be33952013-05-17 18:30:14 -04002658 */
David Sterba184f9992017-06-12 17:29:39 +02002659static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
Chris Masond1310b22008-01-24 16:13:08 -05002660{
David Sterba184f9992017-06-12 17:29:39 +02002661 memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
2662}
2663
2664/*
David Sterba6e707bc2017-06-02 17:26:26 +02002665 * The following helpers allocate a bio. As it's backed by a bioset, it'll
2666 * never fail. We're returning a bio right now but you can call btrfs_io_bio
2667 * for the appropriate container_of magic
Chris Masond1310b22008-01-24 16:13:08 -05002668 */
David Sterbac821e7f32017-06-02 18:35:36 +02002669struct bio *btrfs_bio_alloc(struct block_device *bdev, u64 first_byte)
Chris Masond1310b22008-01-24 16:13:08 -05002670{
2671 struct bio *bio;
2672
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002673 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset);
Christoph Hellwig74d46992017-08-23 19:10:32 +02002674 bio_set_dev(bio, bdev);
David Sterbac821e7f32017-06-02 18:35:36 +02002675 bio->bi_iter.bi_sector = first_byte >> 9;
David Sterba184f9992017-06-12 17:29:39 +02002676 btrfs_io_bio_init(btrfs_io_bio(bio));
Chris Masond1310b22008-01-24 16:13:08 -05002677 return bio;
2678}
2679
David Sterba8b6c1d52017-06-02 17:48:13 +02002680struct bio *btrfs_bio_clone(struct bio *bio)
Chris Mason9be33952013-05-17 18:30:14 -04002681{
Miao Xie23ea8e52014-09-12 18:43:54 +08002682 struct btrfs_io_bio *btrfs_bio;
2683 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002684
David Sterba6e707bc2017-06-02 17:26:26 +02002685 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002686 new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset);
David Sterba6e707bc2017-06-02 17:26:26 +02002687 btrfs_bio = btrfs_io_bio(new);
David Sterba184f9992017-06-12 17:29:39 +02002688 btrfs_io_bio_init(btrfs_bio);
David Sterba6e707bc2017-06-02 17:26:26 +02002689 btrfs_bio->iter = bio->bi_iter;
Miao Xie23ea8e52014-09-12 18:43:54 +08002690 return new;
2691}
Chris Mason9be33952013-05-17 18:30:14 -04002692
David Sterbac5e4c3d2017-06-12 17:29:41 +02002693struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
Chris Mason9be33952013-05-17 18:30:14 -04002694{
Miao Xiefacc8a222013-07-25 19:22:34 +08002695 struct bio *bio;
2696
David Sterba6e707bc2017-06-02 17:26:26 +02002697 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002698 bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
David Sterba184f9992017-06-12 17:29:39 +02002699 btrfs_io_bio_init(btrfs_io_bio(bio));
Miao Xiefacc8a222013-07-25 19:22:34 +08002700 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002701}
2702
Liu Boe4770942017-05-16 10:57:14 -07002703struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
Liu Bo2f8e9142017-05-15 17:43:31 -07002704{
2705 struct bio *bio;
2706 struct btrfs_io_bio *btrfs_bio;
2707
2708 /* this will never fail when it's backed by a bioset */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002709 bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset);
Liu Bo2f8e9142017-05-15 17:43:31 -07002710 ASSERT(bio);
2711
2712 btrfs_bio = btrfs_io_bio(bio);
David Sterba184f9992017-06-12 17:29:39 +02002713 btrfs_io_bio_init(btrfs_bio);
Liu Bo2f8e9142017-05-15 17:43:31 -07002714
2715 bio_trim(bio, offset >> 9, size >> 9);
Liu Bo17347ce2017-05-15 15:33:27 -07002716 btrfs_bio->iter = bio->bi_iter;
Liu Bo2f8e9142017-05-15 17:43:31 -07002717 return bio;
2718}
Chris Mason9be33952013-05-17 18:30:14 -04002719
Mike Christie1f7ad752016-06-05 14:31:51 -05002720static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
2721 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002722{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002723 blk_status_t ret = 0;
Ming Leic45a8f22017-12-18 20:22:05 +08002724 struct bio_vec *bvec = bio_last_bvec_all(bio);
Chris Mason70dec802008-01-29 09:59:12 -05002725 struct page *page = bvec->bv_page;
2726 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002727 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002728
Miao Xie4eee4fa2012-12-21 09:17:45 +00002729 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002730
David Woodhouse902b22f2008-08-20 08:51:49 -04002731 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002732
David Sterba20c98012017-02-17 15:59:35 +01002733 if (tree->ops)
Josef Bacikc6100a42017-05-05 11:57:13 -04002734 ret = tree->ops->submit_bio_hook(tree->private_data, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002735 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002736 else
Mike Christie4e49ea42016-06-05 14:31:41 -05002737 btrfsic_submit_bio(bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002738
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002739 return blk_status_to_errno(ret);
Chris Masond1310b22008-01-24 16:13:08 -05002740}
2741
David Sterba4b81ba42017-06-06 19:14:26 +02002742/*
2743 * @opf: bio REQ_OP_* and REQ_* flags as one value
David Sterbab8b3d622017-06-12 19:50:41 +02002744 * @tree: tree so we can call our merge_bio hook
2745 * @wbc: optional writeback control for io accounting
2746 * @page: page to add to the bio
2747 * @pg_offset: offset of the new bio or to check whether we are adding
2748 * a contiguous page to the previous one
2749 * @size: portion of page that we want to write
2750 * @offset: starting offset in the page
2751 * @bdev: attach newly created bios to this bdev
David Sterba5c2b1fd2017-06-06 19:22:55 +02002752 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
David Sterbab8b3d622017-06-12 19:50:41 +02002753 * @end_io_func: end_io callback for new bio
2754 * @mirror_num: desired mirror to read/write
2755 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
2756 * @bio_flags: flags of the current bio to see if we can merge them
David Sterba4b81ba42017-06-06 19:14:26 +02002757 */
2758static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002759 struct writeback_control *wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02002760 struct page *page, u64 offset,
David Sterba6c5a4e22017-10-04 17:10:34 +02002761 size_t size, unsigned long pg_offset,
Chris Masond1310b22008-01-24 16:13:08 -05002762 struct block_device *bdev,
2763 struct bio **bio_ret,
Chris Masonf1885912008-04-09 16:28:12 -04002764 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002765 int mirror_num,
2766 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002767 unsigned long bio_flags,
2768 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002769{
2770 int ret = 0;
2771 struct bio *bio;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002772 size_t page_size = min_t(size_t, size, PAGE_SIZE);
David Sterba6273b7f2017-10-04 17:30:11 +02002773 sector_t sector = offset >> 9;
Chris Masond1310b22008-01-24 16:13:08 -05002774
David Sterba5c2b1fd2017-06-06 19:22:55 +02002775 ASSERT(bio_ret);
2776
2777 if (*bio_ret) {
David Sterba0c8508a2017-06-12 20:00:43 +02002778 bool contig;
2779 bool can_merge = true;
2780
Chris Masond1310b22008-01-24 16:13:08 -05002781 bio = *bio_ret;
David Sterba0c8508a2017-06-12 20:00:43 +02002782 if (prev_bio_flags & EXTENT_BIO_COMPRESSED)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002783 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002784 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002785 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002786
David Sterba00032d32018-07-18 19:28:09 +02002787 if (tree->ops && btrfs_merge_bio_hook(page, offset, page_size,
2788 bio, bio_flags))
David Sterba0c8508a2017-06-12 20:00:43 +02002789 can_merge = false;
2790
2791 if (prev_bio_flags != bio_flags || !contig || !can_merge ||
Filipe Manana005efed2015-09-14 09:09:31 +01002792 force_bio_submit ||
David Sterba6c5a4e22017-10-04 17:10:34 +02002793 bio_add_page(bio, page, page_size, pg_offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002794 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002795 if (ret < 0) {
2796 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002797 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002798 }
Chris Masond1310b22008-01-24 16:13:08 -05002799 bio = NULL;
2800 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002801 if (wbc)
2802 wbc_account_io(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002803 return 0;
2804 }
2805 }
Chris Masonc8b97812008-10-29 14:49:59 -04002806
David Sterba6273b7f2017-10-04 17:30:11 +02002807 bio = btrfs_bio_alloc(bdev, offset);
David Sterba6c5a4e22017-10-04 17:10:34 +02002808 bio_add_page(bio, page, page_size, pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002809 bio->bi_end_io = end_io_func;
2810 bio->bi_private = tree;
Jens Axboee6959b92017-06-27 11:51:28 -06002811 bio->bi_write_hint = page->mapping->host->i_write_hint;
David Sterba4b81ba42017-06-06 19:14:26 +02002812 bio->bi_opf = opf;
Chris Masonda2f0f72015-07-02 13:57:22 -07002813 if (wbc) {
2814 wbc_init_bio(wbc, bio);
2815 wbc_account_io(wbc, page, page_size);
2816 }
Chris Mason70dec802008-01-29 09:59:12 -05002817
David Sterba5c2b1fd2017-06-06 19:22:55 +02002818 *bio_ret = bio;
Chris Masond1310b22008-01-24 16:13:08 -05002819
2820 return ret;
2821}
2822
Eric Sandeen48a3b632013-04-25 20:41:01 +00002823static void attach_extent_buffer_page(struct extent_buffer *eb,
2824 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002825{
2826 if (!PagePrivate(page)) {
2827 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002828 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002829 set_page_private(page, (unsigned long)eb);
2830 } else {
2831 WARN_ON(page->private != (unsigned long)eb);
2832 }
2833}
2834
Chris Masond1310b22008-01-24 16:13:08 -05002835void set_page_extent_mapped(struct page *page)
2836{
2837 if (!PagePrivate(page)) {
2838 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002839 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002840 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002841 }
2842}
2843
Miao Xie125bac012013-07-25 19:22:37 +08002844static struct extent_map *
2845__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2846 u64 start, u64 len, get_extent_t *get_extent,
2847 struct extent_map **em_cached)
2848{
2849 struct extent_map *em;
2850
2851 if (em_cached && *em_cached) {
2852 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002853 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002854 start < extent_map_end(em)) {
Elena Reshetova490b54d2017-03-03 10:55:12 +02002855 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002856 return em;
2857 }
2858
2859 free_extent_map(em);
2860 *em_cached = NULL;
2861 }
2862
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02002863 em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
Miao Xie125bac012013-07-25 19:22:37 +08002864 if (em_cached && !IS_ERR_OR_NULL(em)) {
2865 BUG_ON(*em_cached);
Elena Reshetova490b54d2017-03-03 10:55:12 +02002866 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002867 *em_cached = em;
2868 }
2869 return em;
2870}
Chris Masond1310b22008-01-24 16:13:08 -05002871/*
2872 * basic readpage implementation. Locked extent state structs are inserted
2873 * into the tree that are removed when the IO is done (by the end_io
2874 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002875 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07002876 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05002877 */
Miao Xie99740902013-07-25 19:22:36 +08002878static int __do_readpage(struct extent_io_tree *tree,
2879 struct page *page,
2880 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002881 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002882 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02002883 unsigned long *bio_flags, unsigned int read_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002884 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05002885{
2886 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002887 u64 start = page_offset(page);
David Sterba8eec8292017-06-06 19:50:13 +02002888 const u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002889 u64 cur = start;
2890 u64 extent_offset;
2891 u64 last_byte = i_size_read(inode);
2892 u64 block_start;
2893 u64 cur_end;
Chris Masond1310b22008-01-24 16:13:08 -05002894 struct extent_map *em;
2895 struct block_device *bdev;
Liu Bobaf863b2016-07-11 10:39:07 -07002896 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002897 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002898 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002899 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002900 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002901 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00002902 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002903
2904 set_page_extent_mapped(page);
2905
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002906 if (!PageUptodate(page)) {
2907 if (cleancache_get_page(page) == 0) {
2908 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08002909 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002910 goto out;
2911 }
2912 }
2913
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002914 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04002915 char *userpage;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002916 size_t zero_offset = last_byte & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002917
2918 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002919 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002920 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002921 memset(userpage + zero_offset, 0, iosize);
2922 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002923 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002924 }
2925 }
Chris Masond1310b22008-01-24 16:13:08 -05002926 while (cur <= end) {
Filipe Manana005efed2015-09-14 09:09:31 +01002927 bool force_bio_submit = false;
David Sterba6273b7f2017-10-04 17:30:11 +02002928 u64 offset;
Josef Bacikc8f2f242013-02-11 11:33:00 -05002929
Chris Masond1310b22008-01-24 16:13:08 -05002930 if (cur >= last_byte) {
2931 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002932 struct extent_state *cached = NULL;
2933
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002934 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002935 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002936 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002937 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002938 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002939 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002940 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00002941 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01002942 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05002943 break;
2944 }
Miao Xie125bac012013-07-25 19:22:37 +08002945 em = __get_extent_map(inode, page, pg_offset, cur,
2946 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02002947 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002948 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00002949 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002950 break;
2951 }
Chris Masond1310b22008-01-24 16:13:08 -05002952 extent_offset = cur - em->start;
2953 BUG_ON(extent_map_end(em) <= cur);
2954 BUG_ON(end < cur);
2955
Li Zefan261507a02010-12-17 14:21:50 +08002956 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07002957 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002958 extent_set_compress_type(&this_bio_flag,
2959 em->compress_type);
2960 }
Chris Masonc8b97812008-10-29 14:49:59 -04002961
Chris Masond1310b22008-01-24 16:13:08 -05002962 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2963 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002964 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002965 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2966 disk_io_size = em->block_len;
David Sterba6273b7f2017-10-04 17:30:11 +02002967 offset = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002968 } else {
David Sterba6273b7f2017-10-04 17:30:11 +02002969 offset = em->block_start + extent_offset;
Chris Masonc8b97812008-10-29 14:49:59 -04002970 disk_io_size = iosize;
2971 }
Chris Masond1310b22008-01-24 16:13:08 -05002972 bdev = em->bdev;
2973 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002974 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2975 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01002976
2977 /*
2978 * If we have a file range that points to a compressed extent
2979 * and it's followed by a consecutive file range that points to
2980 * to the same compressed extent (possibly with a different
2981 * offset and/or length, so it either points to the whole extent
2982 * or only part of it), we must make sure we do not submit a
2983 * single bio to populate the pages for the 2 ranges because
2984 * this makes the compressed extent read zero out the pages
2985 * belonging to the 2nd range. Imagine the following scenario:
2986 *
2987 * File layout
2988 * [0 - 8K] [8K - 24K]
2989 * | |
2990 * | |
2991 * points to extent X, points to extent X,
2992 * offset 4K, length of 8K offset 0, length 16K
2993 *
2994 * [extent X, compressed length = 4K uncompressed length = 16K]
2995 *
2996 * If the bio to read the compressed extent covers both ranges,
2997 * it will decompress extent X into the pages belonging to the
2998 * first range and then it will stop, zeroing out the remaining
2999 * pages that belong to the other range that points to extent X.
3000 * So here we make sure we submit 2 bios, one for the first
3001 * range and another one for the third range. Both will target
3002 * the same physical extent from disk, but we can't currently
3003 * make the compressed bio endio callback populate the pages
3004 * for both ranges because each compressed bio is tightly
3005 * coupled with a single extent map, and each range can have
3006 * an extent map with a different offset value relative to the
3007 * uncompressed data of our extent and different lengths. This
3008 * is a corner case so we prioritize correctness over
3009 * non-optimal behavior (submitting 2 bios for the same extent).
3010 */
3011 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3012 prev_em_start && *prev_em_start != (u64)-1 &&
3013 *prev_em_start != em->orig_start)
3014 force_bio_submit = true;
3015
3016 if (prev_em_start)
3017 *prev_em_start = em->orig_start;
3018
Chris Masond1310b22008-01-24 16:13:08 -05003019 free_extent_map(em);
3020 em = NULL;
3021
3022 /* we've found a hole, just zero and go on */
3023 if (block_start == EXTENT_MAP_HOLE) {
3024 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003025 struct extent_state *cached = NULL;
3026
Cong Wang7ac687d2011-11-25 23:14:28 +08003027 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003028 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003029 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003030 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003031
3032 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003033 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003034 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01003035 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003036 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003037 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003038 continue;
3039 }
3040 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003041 if (test_range_bit(tree, cur, cur_end,
3042 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003043 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003044 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003045 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003046 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003047 continue;
3048 }
Chris Mason70dec802008-01-29 09:59:12 -05003049 /* we have an inline extent but it didn't get marked up
3050 * to date. Error out
3051 */
3052 if (block_start == EXTENT_MAP_INLINE) {
3053 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003054 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003055 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003056 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003057 continue;
3058 }
Chris Masond1310b22008-01-24 16:13:08 -05003059
David Sterba4b81ba42017-06-06 19:14:26 +02003060 ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL,
David Sterba6273b7f2017-10-04 17:30:11 +02003061 page, offset, disk_io_size,
3062 pg_offset, bdev, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003063 end_bio_extent_readpage, mirror_num,
3064 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003065 this_bio_flag,
3066 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003067 if (!ret) {
3068 nr++;
3069 *bio_flags = this_bio_flag;
3070 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003071 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003072 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003073 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003074 }
Chris Masond1310b22008-01-24 16:13:08 -05003075 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003076 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003077 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003078out:
Chris Masond1310b22008-01-24 16:13:08 -05003079 if (!nr) {
3080 if (!PageError(page))
3081 SetPageUptodate(page);
3082 unlock_page(page);
3083 }
Liu Bobaf863b2016-07-11 10:39:07 -07003084 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003085}
3086
Miao Xie99740902013-07-25 19:22:36 +08003087static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
3088 struct page *pages[], int nr_pages,
3089 u64 start, u64 end,
Miao Xie125bac012013-07-25 19:22:37 +08003090 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003091 struct bio **bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003092 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003093 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003094{
3095 struct inode *inode;
3096 struct btrfs_ordered_extent *ordered;
3097 int index;
3098
3099 inode = pages[0]->mapping->host;
3100 while (1) {
3101 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003102 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Miao Xie99740902013-07-25 19:22:36 +08003103 end - start + 1);
3104 if (!ordered)
3105 break;
3106 unlock_extent(tree, start, end);
3107 btrfs_start_ordered_extent(inode, ordered, 1);
3108 btrfs_put_ordered_extent(ordered);
3109 }
3110
3111 for (index = 0; index < nr_pages; index++) {
David Sterba4ef77692017-06-23 04:09:57 +02003112 __do_readpage(tree, pages[index], btrfs_get_extent, em_cached,
3113 bio, 0, bio_flags, 0, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003114 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003115 }
3116}
3117
3118static void __extent_readpages(struct extent_io_tree *tree,
3119 struct page *pages[],
David Sterbae4d17ef2017-06-23 04:09:57 +02003120 int nr_pages,
Miao Xie125bac012013-07-25 19:22:37 +08003121 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003122 struct bio **bio, unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003123 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003124{
Stefan Behrens35a36212013-08-14 18:12:25 +02003125 u64 start = 0;
Miao Xie99740902013-07-25 19:22:36 +08003126 u64 end = 0;
3127 u64 page_start;
3128 int index;
Stefan Behrens35a36212013-08-14 18:12:25 +02003129 int first_index = 0;
Miao Xie99740902013-07-25 19:22:36 +08003130
3131 for (index = 0; index < nr_pages; index++) {
3132 page_start = page_offset(pages[index]);
3133 if (!end) {
3134 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003135 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003136 first_index = index;
3137 } else if (end + 1 == page_start) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003138 end += PAGE_SIZE;
Miao Xie99740902013-07-25 19:22:36 +08003139 } else {
3140 __do_contiguous_readpages(tree, &pages[first_index],
3141 index - first_index, start,
David Sterba4ef77692017-06-23 04:09:57 +02003142 end, em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003143 bio, bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05003144 prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003145 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003146 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003147 first_index = index;
3148 }
3149 }
3150
3151 if (end)
3152 __do_contiguous_readpages(tree, &pages[first_index],
3153 index - first_index, start,
David Sterba4ef77692017-06-23 04:09:57 +02003154 end, em_cached, bio,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003155 bio_flags, prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003156}
3157
3158static int __extent_read_full_page(struct extent_io_tree *tree,
3159 struct page *page,
3160 get_extent_t *get_extent,
3161 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003162 unsigned long *bio_flags,
3163 unsigned int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003164{
3165 struct inode *inode = page->mapping->host;
3166 struct btrfs_ordered_extent *ordered;
3167 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003168 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003169 int ret;
3170
3171 while (1) {
3172 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003173 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003174 PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003175 if (!ordered)
3176 break;
3177 unlock_extent(tree, start, end);
3178 btrfs_start_ordered_extent(inode, ordered, 1);
3179 btrfs_put_ordered_extent(ordered);
3180 }
3181
Miao Xie125bac012013-07-25 19:22:37 +08003182 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003183 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003184 return ret;
3185}
3186
Chris Masond1310b22008-01-24 16:13:08 -05003187int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003188 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003189{
3190 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003191 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003192 int ret;
3193
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003194 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003195 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003196 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003197 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003198 return ret;
3199}
Chris Masond1310b22008-01-24 16:13:08 -05003200
David Sterba3d4b9492017-02-10 19:33:41 +01003201static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003202 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003203{
3204 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003205}
3206
Chris Masond1310b22008-01-24 16:13:08 -05003207/*
Chris Mason40f76582014-05-21 13:35:51 -07003208 * helper for __extent_writepage, doing all of the delayed allocation setup.
3209 *
3210 * This returns 1 if our fill_delalloc function did all the work required
3211 * to write the page (copy into inline extent). In this case the IO has
3212 * been started and the page is already unlocked.
3213 *
3214 * This returns 0 if all went well (page still locked)
3215 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003216 */
Chris Mason40f76582014-05-21 13:35:51 -07003217static noinline_for_stack int writepage_delalloc(struct inode *inode,
3218 struct page *page, struct writeback_control *wbc,
3219 struct extent_page_data *epd,
3220 u64 delalloc_start,
3221 unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003222{
Chris Mason40f76582014-05-21 13:35:51 -07003223 struct extent_io_tree *tree = epd->tree;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003224 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003225 u64 nr_delalloc;
3226 u64 delalloc_to_write = 0;
3227 u64 delalloc_end = 0;
3228 int ret;
3229 int page_started = 0;
3230
3231 if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc)
3232 return 0;
3233
3234 while (delalloc_end < page_end) {
3235 nr_delalloc = find_lock_delalloc_range(inode, tree,
3236 page,
3237 &delalloc_start,
3238 &delalloc_end,
Josef Bacikdcab6a32015-02-11 15:08:59 -05003239 BTRFS_MAX_EXTENT_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003240 if (nr_delalloc == 0) {
3241 delalloc_start = delalloc_end + 1;
3242 continue;
3243 }
3244 ret = tree->ops->fill_delalloc(inode, page,
3245 delalloc_start,
3246 delalloc_end,
3247 &page_started,
Liu Bof82b7352017-10-23 23:18:16 -06003248 nr_written, wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003249 /* File system has been set read-only */
3250 if (ret) {
3251 SetPageError(page);
3252 /* fill_delalloc should be return < 0 for error
3253 * but just in case, we use > 0 here meaning the
3254 * IO is started, so we don't want to return > 0
3255 * unless things are going well.
3256 */
3257 ret = ret < 0 ? ret : -EIO;
3258 goto done;
3259 }
3260 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003261 * delalloc_end is already one less than the total length, so
3262 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003263 */
3264 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003265 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003266 delalloc_start = delalloc_end + 1;
3267 }
3268 if (wbc->nr_to_write < delalloc_to_write) {
3269 int thresh = 8192;
3270
3271 if (delalloc_to_write < thresh * 2)
3272 thresh = delalloc_to_write;
3273 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3274 thresh);
3275 }
3276
3277 /* did the fill delalloc function already unlock and start
3278 * the IO?
3279 */
3280 if (page_started) {
3281 /*
3282 * we've unlocked the page, so we can't update
3283 * the mapping's writeback index, just update
3284 * nr_to_write.
3285 */
3286 wbc->nr_to_write -= *nr_written;
3287 return 1;
3288 }
3289
3290 ret = 0;
3291
3292done:
3293 return ret;
3294}
3295
3296/*
3297 * helper for __extent_writepage. This calls the writepage start hooks,
3298 * and does the loop to map the page into extents and bios.
3299 *
3300 * We return 1 if the IO is started and the page is unlocked,
3301 * 0 if all went well (page still locked)
3302 * < 0 if there were errors (page still locked)
3303 */
3304static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3305 struct page *page,
3306 struct writeback_control *wbc,
3307 struct extent_page_data *epd,
3308 loff_t i_size,
3309 unsigned long nr_written,
David Sterbaf1c77c52017-06-06 19:03:49 +02003310 unsigned int write_flags, int *nr_ret)
Chris Mason40f76582014-05-21 13:35:51 -07003311{
Chris Masond1310b22008-01-24 16:13:08 -05003312 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003313 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003314 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003315 u64 end;
3316 u64 cur = start;
3317 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003318 u64 block_start;
3319 u64 iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003320 struct extent_map *em;
3321 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003322 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003323 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003324 int ret = 0;
3325 int nr = 0;
3326 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003327
Chris Mason247e7432008-07-17 12:53:51 -04003328 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04003329 ret = tree->ops->writepage_start_hook(page, start,
3330 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01003331 if (ret) {
3332 /* Fixup worker will requeue */
3333 if (ret == -EBUSY)
3334 wbc->pages_skipped++;
3335 else
3336 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003337
David Sterba3d4b9492017-02-10 19:33:41 +01003338 update_nr_written(wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04003339 unlock_page(page);
Liu Bobcf93482017-01-25 17:15:54 -08003340 return 1;
Chris Mason247e7432008-07-17 12:53:51 -04003341 }
3342 }
3343
Chris Mason11c83492009-04-20 15:50:09 -04003344 /*
3345 * we don't want to touch the inode after unlocking the page,
3346 * so we update the mapping writeback index now
3347 */
David Sterba3d4b9492017-02-10 19:33:41 +01003348 update_nr_written(wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003349
Chris Masond1310b22008-01-24 16:13:08 -05003350 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003351 if (i_size <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003352 if (tree->ops && tree->ops->writepage_end_io_hook)
3353 tree->ops->writepage_end_io_hook(page, start,
3354 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003355 goto done;
3356 }
3357
Chris Masond1310b22008-01-24 16:13:08 -05003358 blocksize = inode->i_sb->s_blocksize;
3359
3360 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003361 u64 em_end;
David Sterba6273b7f2017-10-04 17:30:11 +02003362 u64 offset;
David Sterba58409ed2016-05-04 11:46:10 +02003363
Chris Mason40f76582014-05-21 13:35:51 -07003364 if (cur >= i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003365 if (tree->ops && tree->ops->writepage_end_io_hook)
3366 tree->ops->writepage_end_io_hook(page, cur,
3367 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003368 break;
3369 }
David Sterba3c98c622017-06-23 04:01:08 +02003370 em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003371 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003372 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003373 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003374 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003375 break;
3376 }
3377
3378 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003379 em_end = extent_map_end(em);
3380 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003381 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003382 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003383 iosize = ALIGN(iosize, blocksize);
David Sterba6273b7f2017-10-04 17:30:11 +02003384 offset = em->block_start + extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003385 bdev = em->bdev;
3386 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003387 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003388 free_extent_map(em);
3389 em = NULL;
3390
Chris Masonc8b97812008-10-29 14:49:59 -04003391 /*
3392 * compressed and inline extents are written through other
3393 * paths in the FS
3394 */
3395 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003396 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003397 /*
3398 * end_io notification does not happen here for
3399 * compressed extents
3400 */
3401 if (!compressed && tree->ops &&
3402 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003403 tree->ops->writepage_end_io_hook(page, cur,
3404 cur + iosize - 1,
3405 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003406 else if (compressed) {
3407 /* we don't want to end_page_writeback on
3408 * a compressed extent. this happens
3409 * elsewhere
3410 */
3411 nr++;
3412 }
3413
3414 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003415 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003416 continue;
3417 }
Chris Masonc8b97812008-10-29 14:49:59 -04003418
David Sterba58409ed2016-05-04 11:46:10 +02003419 set_range_writeback(tree, cur, cur + iosize - 1);
3420 if (!PageWriteback(page)) {
3421 btrfs_err(BTRFS_I(inode)->root->fs_info,
3422 "page %lu not writeback, cur %llu end %llu",
3423 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003424 }
David Sterba58409ed2016-05-04 11:46:10 +02003425
David Sterba4b81ba42017-06-06 19:14:26 +02003426 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003427 page, offset, iosize, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003428 bdev, &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003429 end_bio_extent_writepage,
3430 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003431 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003432 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003433 if (PageWriteback(page))
3434 end_page_writeback(page);
3435 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003436
Chris Masond1310b22008-01-24 16:13:08 -05003437 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003438 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003439 nr++;
3440 }
3441done:
Chris Mason40f76582014-05-21 13:35:51 -07003442 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003443 return ret;
3444}
3445
3446/*
3447 * the writepage semantics are similar to regular writepage. extent
3448 * records are inserted to lock ranges in the tree, and as dirty areas
3449 * are found, they are marked writeback. Then the lock bits are removed
3450 * and the end_io handler clears the writeback ranges
3451 */
3452static int __extent_writepage(struct page *page, struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003453 struct extent_page_data *epd)
Chris Mason40f76582014-05-21 13:35:51 -07003454{
3455 struct inode *inode = page->mapping->host;
Chris Mason40f76582014-05-21 13:35:51 -07003456 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003457 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003458 int ret;
3459 int nr = 0;
3460 size_t pg_offset = 0;
3461 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003462 unsigned long end_index = i_size >> PAGE_SHIFT;
David Sterbaf1c77c52017-06-06 19:03:49 +02003463 unsigned int write_flags = 0;
Chris Mason40f76582014-05-21 13:35:51 -07003464 unsigned long nr_written = 0;
3465
Liu Boff40adf2017-08-24 18:19:48 -06003466 write_flags = wbc_to_write_flags(wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003467
3468 trace___extent_writepage(page, inode, wbc);
3469
3470 WARN_ON(!PageLocked(page));
3471
3472 ClearPageError(page);
3473
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003474 pg_offset = i_size & (PAGE_SIZE - 1);
Chris Mason40f76582014-05-21 13:35:51 -07003475 if (page->index > end_index ||
3476 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003477 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003478 unlock_page(page);
3479 return 0;
3480 }
3481
3482 if (page->index == end_index) {
3483 char *userpage;
3484
3485 userpage = kmap_atomic(page);
3486 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003487 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003488 kunmap_atomic(userpage);
3489 flush_dcache_page(page);
3490 }
3491
3492 pg_offset = 0;
3493
3494 set_page_extent_mapped(page);
3495
3496 ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
3497 if (ret == 1)
3498 goto done_unlocked;
3499 if (ret)
3500 goto done;
3501
3502 ret = __extent_writepage_io(inode, page, wbc, epd,
3503 i_size, nr_written, write_flags, &nr);
3504 if (ret == 1)
3505 goto done_unlocked;
3506
3507done:
Chris Masond1310b22008-01-24 16:13:08 -05003508 if (nr == 0) {
3509 /* make sure the mapping tag for page dirty gets cleared */
3510 set_page_writeback(page);
3511 end_page_writeback(page);
3512 }
Filipe Manana61391d52014-05-09 17:17:40 +01003513 if (PageError(page)) {
3514 ret = ret < 0 ? ret : -EIO;
3515 end_extent_writepage(page, ret, start, page_end);
3516 }
Chris Masond1310b22008-01-24 16:13:08 -05003517 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003518 return ret;
Chris Mason771ed682008-11-06 22:02:51 -05003519
Chris Mason11c83492009-04-20 15:50:09 -04003520done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003521 return 0;
3522}
3523
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003524void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003525{
NeilBrown74316202014-07-07 15:16:04 +10003526 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3527 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003528}
3529
Chris Mason0e378df2014-05-19 20:55:27 -07003530static noinline_for_stack int
3531lock_extent_buffer_for_io(struct extent_buffer *eb,
3532 struct btrfs_fs_info *fs_info,
3533 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003534{
David Sterbacc5e31a2018-03-01 18:20:27 +01003535 int i, num_pages;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003536 int flush = 0;
3537 int ret = 0;
3538
3539 if (!btrfs_try_tree_write_lock(eb)) {
3540 flush = 1;
3541 flush_write_bio(epd);
3542 btrfs_tree_lock(eb);
3543 }
3544
3545 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3546 btrfs_tree_unlock(eb);
3547 if (!epd->sync_io)
3548 return 0;
3549 if (!flush) {
3550 flush_write_bio(epd);
3551 flush = 1;
3552 }
Chris Masona098d8e82012-03-21 12:09:56 -04003553 while (1) {
3554 wait_on_extent_buffer_writeback(eb);
3555 btrfs_tree_lock(eb);
3556 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3557 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003558 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003559 }
3560 }
3561
Josef Bacik51561ff2012-07-20 16:25:24 -04003562 /*
3563 * We need to do this to prevent races in people who check if the eb is
3564 * under IO since we can end up having no IO bits set for a short period
3565 * of time.
3566 */
3567 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003568 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3569 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003570 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003571 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Nikolay Borisov104b4e52017-06-20 21:01:20 +03003572 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3573 -eb->len,
3574 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003575 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003576 } else {
3577 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003578 }
3579
3580 btrfs_tree_unlock(eb);
3581
3582 if (!ret)
3583 return ret;
3584
David Sterba65ad0102018-06-29 10:56:49 +02003585 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003586 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003587 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003588
3589 if (!trylock_page(p)) {
3590 if (!flush) {
3591 flush_write_bio(epd);
3592 flush = 1;
3593 }
3594 lock_page(p);
3595 }
3596 }
3597
3598 return ret;
3599}
3600
3601static void end_extent_buffer_writeback(struct extent_buffer *eb)
3602{
3603 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003604 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003605 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3606}
3607
Filipe Manana656f30d2014-09-26 12:25:56 +01003608static void set_btree_ioerr(struct page *page)
3609{
3610 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Filipe Manana656f30d2014-09-26 12:25:56 +01003611
3612 SetPageError(page);
3613 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3614 return;
3615
3616 /*
3617 * If writeback for a btree extent that doesn't belong to a log tree
3618 * failed, increment the counter transaction->eb_write_errors.
3619 * We do this because while the transaction is running and before it's
3620 * committing (when we call filemap_fdata[write|wait]_range against
3621 * the btree inode), we might have
3622 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3623 * returns an error or an error happens during writeback, when we're
3624 * committing the transaction we wouldn't know about it, since the pages
3625 * can be no longer dirty nor marked anymore for writeback (if a
3626 * subsequent modification to the extent buffer didn't happen before the
3627 * transaction commit), which makes filemap_fdata[write|wait]_range not
3628 * able to find the pages tagged with SetPageError at transaction
3629 * commit time. So if this happens we must abort the transaction,
3630 * otherwise we commit a super block with btree roots that point to
3631 * btree nodes/leafs whose content on disk is invalid - either garbage
3632 * or the content of some node/leaf from a past generation that got
3633 * cowed or deleted and is no longer valid.
3634 *
3635 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3636 * not be enough - we need to distinguish between log tree extents vs
3637 * non-log tree extents, and the next filemap_fdatawait_range() call
3638 * will catch and clear such errors in the mapping - and that call might
3639 * be from a log sync and not from a transaction commit. Also, checking
3640 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3641 * not done and would not be reliable - the eb might have been released
3642 * from memory and reading it back again means that flag would not be
3643 * set (since it's a runtime flag, not persisted on disk).
3644 *
3645 * Using the flags below in the btree inode also makes us achieve the
3646 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3647 * writeback for all dirty pages and before filemap_fdatawait_range()
3648 * is called, the writeback for all dirty pages had already finished
3649 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3650 * filemap_fdatawait_range() would return success, as it could not know
3651 * that writeback errors happened (the pages were no longer tagged for
3652 * writeback).
3653 */
3654 switch (eb->log_index) {
3655 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003656 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003657 break;
3658 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003659 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003660 break;
3661 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003662 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003663 break;
3664 default:
3665 BUG(); /* unexpected, logic error */
3666 }
3667}
3668
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003669static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003670{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003671 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003672 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003673 int i, done;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003674
David Sterbac09abff2017-07-13 18:10:07 +02003675 ASSERT(!bio_flagged(bio, BIO_CLONED));
Kent Overstreet2c30c712013-11-07 12:20:26 -08003676 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003677 struct page *page = bvec->bv_page;
3678
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003679 eb = (struct extent_buffer *)page->private;
3680 BUG_ON(!eb);
3681 done = atomic_dec_and_test(&eb->io_pages);
3682
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003683 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003684 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003685 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003686 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003687 }
3688
3689 end_page_writeback(page);
3690
3691 if (!done)
3692 continue;
3693
3694 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003695 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003696
3697 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003698}
3699
Chris Mason0e378df2014-05-19 20:55:27 -07003700static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003701 struct btrfs_fs_info *fs_info,
3702 struct writeback_control *wbc,
3703 struct extent_page_data *epd)
3704{
3705 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003706 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003707 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003708 u32 nritems;
David Sterbacc5e31a2018-03-01 18:20:27 +01003709 int i, num_pages;
Liu Bo851cd172016-09-23 13:44:44 -07003710 unsigned long start, end;
Liu Boff40adf2017-08-24 18:19:48 -06003711 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003712 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003713
Filipe Manana656f30d2014-09-26 12:25:56 +01003714 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02003715 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003716 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003717
Liu Bo851cd172016-09-23 13:44:44 -07003718 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3719 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003720 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003721 end = btrfs_node_key_ptr_offset(nritems);
3722
David Sterbab159fa22016-11-08 18:09:03 +01003723 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003724 } else {
3725 /*
3726 * leaf:
3727 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3728 */
3729 start = btrfs_item_nr_offset(nritems);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003730 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, eb);
David Sterbab159fa22016-11-08 18:09:03 +01003731 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003732 }
3733
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003734 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003735 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003736
3737 clear_page_dirty_for_io(p);
3738 set_page_writeback(p);
David Sterba4b81ba42017-06-06 19:14:26 +02003739 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003740 p, offset, PAGE_SIZE, 0, bdev,
David Sterbac2df8bb2017-02-10 19:29:38 +01003741 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003742 end_bio_extent_buffer_writepage,
Liu Bo18fdc672017-09-13 12:18:22 -06003743 0, 0, 0, false);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003744 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003745 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003746 if (PageWriteback(p))
3747 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003748 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3749 end_extent_buffer_writeback(eb);
3750 ret = -EIO;
3751 break;
3752 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003753 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003754 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003755 unlock_page(p);
3756 }
3757
3758 if (unlikely(ret)) {
3759 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003760 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003761 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003762 unlock_page(p);
3763 }
3764 }
3765
3766 return ret;
3767}
3768
3769int btree_write_cache_pages(struct address_space *mapping,
3770 struct writeback_control *wbc)
3771{
3772 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3773 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3774 struct extent_buffer *eb, *prev_eb = NULL;
3775 struct extent_page_data epd = {
3776 .bio = NULL,
3777 .tree = tree,
3778 .extent_locked = 0,
3779 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3780 };
3781 int ret = 0;
3782 int done = 0;
3783 int nr_to_write_done = 0;
3784 struct pagevec pvec;
3785 int nr_pages;
3786 pgoff_t index;
3787 pgoff_t end; /* Inclusive */
3788 int scanned = 0;
3789 int tag;
3790
Mel Gorman86679822017-11-15 17:37:52 -08003791 pagevec_init(&pvec);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003792 if (wbc->range_cyclic) {
3793 index = mapping->writeback_index; /* Start from prev offset */
3794 end = -1;
3795 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003796 index = wbc->range_start >> PAGE_SHIFT;
3797 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003798 scanned = 1;
3799 }
3800 if (wbc->sync_mode == WB_SYNC_ALL)
3801 tag = PAGECACHE_TAG_TOWRITE;
3802 else
3803 tag = PAGECACHE_TAG_DIRTY;
3804retry:
3805 if (wbc->sync_mode == WB_SYNC_ALL)
3806 tag_pages_for_writeback(mapping, index, end);
3807 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara4006f432017-11-15 17:34:37 -08003808 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08003809 tag))) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003810 unsigned i;
3811
3812 scanned = 1;
3813 for (i = 0; i < nr_pages; i++) {
3814 struct page *page = pvec.pages[i];
3815
3816 if (!PagePrivate(page))
3817 continue;
3818
Josef Bacikb5bae262012-09-14 13:43:01 -04003819 spin_lock(&mapping->private_lock);
3820 if (!PagePrivate(page)) {
3821 spin_unlock(&mapping->private_lock);
3822 continue;
3823 }
3824
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003825 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003826
3827 /*
3828 * Shouldn't happen and normally this would be a BUG_ON
3829 * but no sense in crashing the users box for something
3830 * we can survive anyway.
3831 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303832 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003833 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003834 continue;
3835 }
3836
Josef Bacikb5bae262012-09-14 13:43:01 -04003837 if (eb == prev_eb) {
3838 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003839 continue;
3840 }
3841
Josef Bacikb5bae262012-09-14 13:43:01 -04003842 ret = atomic_inc_not_zero(&eb->refs);
3843 spin_unlock(&mapping->private_lock);
3844 if (!ret)
3845 continue;
3846
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003847 prev_eb = eb;
3848 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3849 if (!ret) {
3850 free_extent_buffer(eb);
3851 continue;
3852 }
3853
3854 ret = write_one_eb(eb, fs_info, wbc, &epd);
3855 if (ret) {
3856 done = 1;
3857 free_extent_buffer(eb);
3858 break;
3859 }
3860 free_extent_buffer(eb);
3861
3862 /*
3863 * the filesystem may choose to bump up nr_to_write.
3864 * We have to make sure to honor the new nr_to_write
3865 * at any time
3866 */
3867 nr_to_write_done = wbc->nr_to_write <= 0;
3868 }
3869 pagevec_release(&pvec);
3870 cond_resched();
3871 }
3872 if (!scanned && !done) {
3873 /*
3874 * We hit the last page and there is more work to be done: wrap
3875 * back to the start of the file
3876 */
3877 scanned = 1;
3878 index = 0;
3879 goto retry;
3880 }
3881 flush_write_bio(&epd);
3882 return ret;
3883}
3884
Chris Masond1310b22008-01-24 16:13:08 -05003885/**
Chris Mason4bef0842008-09-08 11:18:08 -04003886 * 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 -05003887 * @mapping: address space structure to write
3888 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
David Sterba935db852017-06-23 04:30:28 +02003889 * @data: data passed to __extent_writepage function
Chris Masond1310b22008-01-24 16:13:08 -05003890 *
3891 * If a page is already under I/O, write_cache_pages() skips it, even
3892 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3893 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3894 * and msync() need to guarantee that all the data which was dirty at the time
3895 * the call was made get new I/O started against them. If wbc->sync_mode is
3896 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3897 * existing IO to complete.
3898 */
David Sterba4242b642017-02-10 19:38:24 +01003899static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04003900 struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003901 struct extent_page_data *epd)
Chris Masond1310b22008-01-24 16:13:08 -05003902{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003903 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003904 int ret = 0;
3905 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003906 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003907 struct pagevec pvec;
3908 int nr_pages;
3909 pgoff_t index;
3910 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08003911 pgoff_t done_index;
3912 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003913 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003914 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003915
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003916 /*
3917 * We have to hold onto the inode so that ordered extents can do their
3918 * work when the IO finishes. The alternative to this is failing to add
3919 * an ordered extent if the igrab() fails there and that is a huge pain
3920 * to deal with, so instead just hold onto the inode throughout the
3921 * writepages operation. If it fails here we are freeing up the inode
3922 * anyway and we'd rather not waste our time writing out stuff that is
3923 * going to be truncated anyway.
3924 */
3925 if (!igrab(inode))
3926 return 0;
3927
Mel Gorman86679822017-11-15 17:37:52 -08003928 pagevec_init(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05003929 if (wbc->range_cyclic) {
3930 index = mapping->writeback_index; /* Start from prev offset */
3931 end = -1;
3932 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003933 index = wbc->range_start >> PAGE_SHIFT;
3934 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08003935 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3936 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05003937 scanned = 1;
3938 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003939 if (wbc->sync_mode == WB_SYNC_ALL)
3940 tag = PAGECACHE_TAG_TOWRITE;
3941 else
3942 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003943retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003944 if (wbc->sync_mode == WB_SYNC_ALL)
3945 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08003946 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003947 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara67fd7072017-11-15 17:35:19 -08003948 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
3949 &index, end, tag))) {
Chris Masond1310b22008-01-24 16:13:08 -05003950 unsigned i;
3951
3952 scanned = 1;
3953 for (i = 0; i < nr_pages; i++) {
3954 struct page *page = pvec.pages[i];
3955
Liu Boa91326672016-03-07 16:56:21 -08003956 done_index = page->index;
Chris Masond1310b22008-01-24 16:13:08 -05003957 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07003958 * At this point we hold neither the i_pages lock nor
3959 * the page lock: the page may be truncated or
3960 * invalidated (changing page->mapping to NULL),
3961 * or even swizzled back from swapper_space to
3962 * tmpfs file mapping
Chris Masond1310b22008-01-24 16:13:08 -05003963 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003964 if (!trylock_page(page)) {
David Sterbaaab6e9e2017-11-30 18:00:02 +01003965 flush_write_bio(epd);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003966 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003967 }
Chris Masond1310b22008-01-24 16:13:08 -05003968
3969 if (unlikely(page->mapping != mapping)) {
3970 unlock_page(page);
3971 continue;
3972 }
3973
Chris Masond2c3f4f2008-11-19 12:44:22 -05003974 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003975 if (PageWriteback(page))
David Sterbaaab6e9e2017-11-30 18:00:02 +01003976 flush_write_bio(epd);
Chris Masond1310b22008-01-24 16:13:08 -05003977 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003978 }
Chris Masond1310b22008-01-24 16:13:08 -05003979
3980 if (PageWriteback(page) ||
3981 !clear_page_dirty_for_io(page)) {
3982 unlock_page(page);
3983 continue;
3984 }
3985
David Sterbaaab6e9e2017-11-30 18:00:02 +01003986 ret = __extent_writepage(page, wbc, epd);
Chris Masond1310b22008-01-24 16:13:08 -05003987
3988 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3989 unlock_page(page);
3990 ret = 0;
3991 }
Liu Boa91326672016-03-07 16:56:21 -08003992 if (ret < 0) {
3993 /*
3994 * done_index is set past this page,
3995 * so media errors will not choke
3996 * background writeout for the entire
3997 * file. This has consequences for
3998 * range_cyclic semantics (ie. it may
3999 * not be suitable for data integrity
4000 * writeout).
4001 */
4002 done_index = page->index + 1;
4003 done = 1;
4004 break;
4005 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004006
4007 /*
4008 * the filesystem may choose to bump up nr_to_write.
4009 * We have to make sure to honor the new nr_to_write
4010 * at any time
4011 */
4012 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004013 }
4014 pagevec_release(&pvec);
4015 cond_resched();
4016 }
Liu Bo894b36e2016-03-07 16:56:22 -08004017 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004018 /*
4019 * We hit the last page and there is more work to be done: wrap
4020 * back to the start of the file
4021 */
4022 scanned = 1;
4023 index = 0;
4024 goto retry;
4025 }
Liu Boa91326672016-03-07 16:56:21 -08004026
4027 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4028 mapping->writeback_index = done_index;
4029
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004030 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004031 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004032}
Chris Masond1310b22008-01-24 16:13:08 -05004033
David Sterbaaab6e9e2017-11-30 18:00:02 +01004034static void flush_write_bio(struct extent_page_data *epd)
Chris Masonffbd5172009-04-20 15:50:09 -04004035{
4036 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04004037 int ret;
4038
Liu Bo18fdc672017-09-13 12:18:22 -06004039 ret = submit_one_bio(epd->bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004040 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04004041 epd->bio = NULL;
4042 }
4043}
4044
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004045int extent_write_full_page(struct page *page, struct writeback_control *wbc)
Chris Masond1310b22008-01-24 16:13:08 -05004046{
4047 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004048 struct extent_page_data epd = {
4049 .bio = NULL,
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004050 .tree = &BTRFS_I(page->mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004051 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004052 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004053 };
Chris Masond1310b22008-01-24 16:13:08 -05004054
Chris Masond1310b22008-01-24 16:13:08 -05004055 ret = __extent_writepage(page, wbc, &epd);
4056
David Sterbae2932ee2017-06-23 04:16:17 +02004057 flush_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004058 return ret;
4059}
Chris Masond1310b22008-01-24 16:13:08 -05004060
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004061int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
Chris Mason771ed682008-11-06 22:02:51 -05004062 int mode)
4063{
4064 int ret = 0;
4065 struct address_space *mapping = inode->i_mapping;
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004066 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Mason771ed682008-11-06 22:02:51 -05004067 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004068 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4069 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004070
4071 struct extent_page_data epd = {
4072 .bio = NULL,
4073 .tree = tree,
Chris Mason771ed682008-11-06 22:02:51 -05004074 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004075 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05004076 };
4077 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004078 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004079 .nr_to_write = nr_pages * 2,
4080 .range_start = start,
4081 .range_end = end + 1,
4082 };
4083
Chris Masond3977122009-01-05 21:25:51 -05004084 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004085 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004086 if (clear_page_dirty_for_io(page))
4087 ret = __extent_writepage(page, &wbc_writepages, &epd);
4088 else {
4089 if (tree->ops && tree->ops->writepage_end_io_hook)
4090 tree->ops->writepage_end_io_hook(page, start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004091 start + PAGE_SIZE - 1,
Chris Mason771ed682008-11-06 22:02:51 -05004092 NULL, 1);
4093 unlock_page(page);
4094 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004095 put_page(page);
4096 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004097 }
4098
David Sterbae2932ee2017-06-23 04:16:17 +02004099 flush_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004100 return ret;
4101}
Chris Masond1310b22008-01-24 16:13:08 -05004102
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004103int extent_writepages(struct address_space *mapping,
Chris Masond1310b22008-01-24 16:13:08 -05004104 struct writeback_control *wbc)
4105{
4106 int ret = 0;
4107 struct extent_page_data epd = {
4108 .bio = NULL,
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004109 .tree = &BTRFS_I(mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004110 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004111 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004112 };
4113
David Sterba935db852017-06-23 04:30:28 +02004114 ret = extent_write_cache_pages(mapping, wbc, &epd);
David Sterbae2932ee2017-06-23 04:16:17 +02004115 flush_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004116 return ret;
4117}
Chris Masond1310b22008-01-24 16:13:08 -05004118
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004119int extent_readpages(struct address_space *mapping, struct list_head *pages,
4120 unsigned nr_pages)
Chris Masond1310b22008-01-24 16:13:08 -05004121{
4122 struct bio *bio = NULL;
4123 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04004124 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004125 struct page *pagepool[16];
4126 struct page *page;
Miao Xie125bac012013-07-25 19:22:37 +08004127 struct extent_map *em_cached = NULL;
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004128 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Liu Bo67c96842012-07-20 21:43:09 -06004129 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004130 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004131
Chris Masond1310b22008-01-24 16:13:08 -05004132 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06004133 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05004134
4135 prefetchw(&page->flags);
4136 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06004137 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07004138 page->index,
4139 readahead_gfp_mask(mapping))) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004140 put_page(page);
Liu Bo67c96842012-07-20 21:43:09 -06004141 continue;
Chris Masond1310b22008-01-24 16:13:08 -05004142 }
Liu Bo67c96842012-07-20 21:43:09 -06004143
4144 pagepool[nr++] = page;
4145 if (nr < ARRAY_SIZE(pagepool))
4146 continue;
David Sterbae4d17ef2017-06-23 04:09:57 +02004147 __extent_readpages(tree, pagepool, nr, &em_cached, &bio,
4148 &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004149 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004150 }
Miao Xie99740902013-07-25 19:22:36 +08004151 if (nr)
David Sterbae4d17ef2017-06-23 04:09:57 +02004152 __extent_readpages(tree, pagepool, nr, &em_cached, &bio,
4153 &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004154
Miao Xie125bac012013-07-25 19:22:37 +08004155 if (em_cached)
4156 free_extent_map(em_cached);
4157
Chris Masond1310b22008-01-24 16:13:08 -05004158 BUG_ON(!list_empty(pages));
4159 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004160 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004161 return 0;
4162}
Chris Masond1310b22008-01-24 16:13:08 -05004163
4164/*
4165 * basic invalidatepage code, this waits on any locked or writeback
4166 * ranges corresponding to the page, and then deletes any extent state
4167 * records from the tree
4168 */
4169int extent_invalidatepage(struct extent_io_tree *tree,
4170 struct page *page, unsigned long offset)
4171{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004172 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004173 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004174 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004175 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4176
Qu Wenruofda28322013-02-26 08:10:22 +00004177 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004178 if (start > end)
4179 return 0;
4180
David Sterbaff13db42015-12-03 14:30:40 +01004181 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004182 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004183 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004184 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4185 EXTENT_DO_ACCOUNTING,
David Sterbaae0f1622017-10-31 16:37:52 +01004186 1, 1, &cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05004187 return 0;
4188}
Chris Masond1310b22008-01-24 16:13:08 -05004189
4190/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004191 * a helper for releasepage, this tests for areas of the page that
4192 * are locked or under IO and drops the related state bits if it is safe
4193 * to drop the page.
4194 */
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004195static int try_release_extent_state(struct extent_io_tree *tree,
Eric Sandeen48a3b632013-04-25 20:41:01 +00004196 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004197{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004198 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004199 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004200 int ret = 1;
4201
Chris Mason211f90e2008-07-18 11:56:15 -04004202 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04004203 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04004204 ret = 0;
4205 else {
Chris Mason11ef1602009-09-23 20:28:46 -04004206 /*
4207 * at this point we can safely clear everything except the
4208 * locked bit and the nodatasum bit
4209 */
David Sterba66b0c882017-10-31 16:30:47 +01004210 ret = __clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004211 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
David Sterba66b0c882017-10-31 16:30:47 +01004212 0, 0, NULL, mask, NULL);
Chris Masone3f24cc2011-02-14 12:52:08 -05004213
4214 /* if clear_extent_bit failed for enomem reasons,
4215 * we can't allow the release to continue.
4216 */
4217 if (ret < 0)
4218 ret = 0;
4219 else
4220 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004221 }
4222 return ret;
4223}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004224
4225/*
Chris Masond1310b22008-01-24 16:13:08 -05004226 * a helper for releasepage. As long as there are no locked extents
4227 * in the range corresponding to the page, both state records and extent
4228 * map records are removed
4229 */
Nikolay Borisov477a30b2018-04-19 10:46:34 +03004230int try_release_extent_mapping(struct page *page, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004231{
4232 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004233 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004234 u64 end = start + PAGE_SIZE - 1;
Filipe Mananabd3599a2018-07-12 01:36:43 +01004235 struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
4236 struct extent_io_tree *tree = &btrfs_inode->io_tree;
4237 struct extent_map_tree *map = &btrfs_inode->extent_tree;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004238
Mel Gormand0164ad2015-11-06 16:28:21 -08004239 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004240 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004241 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004242 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004243 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004244 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004245 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004246 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004247 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004248 break;
4249 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004250 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4251 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004252 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004253 free_extent_map(em);
4254 break;
4255 }
4256 if (!test_range_bit(tree, em->start,
4257 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04004258 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04004259 0, NULL)) {
Filipe Mananabd3599a2018-07-12 01:36:43 +01004260 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4261 &btrfs_inode->runtime_flags);
Chris Mason70dec802008-01-29 09:59:12 -05004262 remove_extent_mapping(map, em);
4263 /* once for the rb tree */
4264 free_extent_map(em);
4265 }
4266 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004267 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004268
4269 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004270 free_extent_map(em);
4271 }
Chris Masond1310b22008-01-24 16:13:08 -05004272 }
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004273 return try_release_extent_state(tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004274}
Chris Masond1310b22008-01-24 16:13:08 -05004275
Chris Masonec29ed52011-02-23 16:23:20 -05004276/*
4277 * helper function for fiemap, which doesn't want to see any holes.
4278 * This maps until we find something past 'last'
4279 */
4280static struct extent_map *get_extent_skip_holes(struct inode *inode,
David Sterbae3350e12017-06-23 04:09:57 +02004281 u64 offset, u64 last)
Chris Masonec29ed52011-02-23 16:23:20 -05004282{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004283 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004284 struct extent_map *em;
4285 u64 len;
4286
4287 if (offset >= last)
4288 return NULL;
4289
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304290 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004291 len = last - offset;
4292 if (len == 0)
4293 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004294 len = ALIGN(len, sectorsize);
David Sterbae3350e12017-06-23 04:09:57 +02004295 em = btrfs_get_extent_fiemap(BTRFS_I(inode), NULL, 0, offset,
4296 len, 0);
David Sterbac7040052011-04-19 18:00:01 +02004297 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004298 return em;
4299
4300 /* if this isn't a hole return it */
Nikolay Borisov4a2d25c2017-11-23 10:51:43 +02004301 if (em->block_start != EXTENT_MAP_HOLE)
Chris Masonec29ed52011-02-23 16:23:20 -05004302 return em;
Chris Masonec29ed52011-02-23 16:23:20 -05004303
4304 /* this is a hole, advance to the next extent */
4305 offset = extent_map_end(em);
4306 free_extent_map(em);
4307 if (offset >= last)
4308 break;
4309 }
4310 return NULL;
4311}
4312
Qu Wenruo47518322017-04-07 10:43:15 +08004313/*
4314 * To cache previous fiemap extent
4315 *
4316 * Will be used for merging fiemap extent
4317 */
4318struct fiemap_cache {
4319 u64 offset;
4320 u64 phys;
4321 u64 len;
4322 u32 flags;
4323 bool cached;
4324};
4325
4326/*
4327 * Helper to submit fiemap extent.
4328 *
4329 * Will try to merge current fiemap extent specified by @offset, @phys,
4330 * @len and @flags with cached one.
4331 * And only when we fails to merge, cached one will be submitted as
4332 * fiemap extent.
4333 *
4334 * Return value is the same as fiemap_fill_next_extent().
4335 */
4336static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4337 struct fiemap_cache *cache,
4338 u64 offset, u64 phys, u64 len, u32 flags)
4339{
4340 int ret = 0;
4341
4342 if (!cache->cached)
4343 goto assign;
4344
4345 /*
4346 * Sanity check, extent_fiemap() should have ensured that new
4347 * fiemap extent won't overlap with cahced one.
4348 * Not recoverable.
4349 *
4350 * NOTE: Physical address can overlap, due to compression
4351 */
4352 if (cache->offset + cache->len > offset) {
4353 WARN_ON(1);
4354 return -EINVAL;
4355 }
4356
4357 /*
4358 * Only merges fiemap extents if
4359 * 1) Their logical addresses are continuous
4360 *
4361 * 2) Their physical addresses are continuous
4362 * So truly compressed (physical size smaller than logical size)
4363 * extents won't get merged with each other
4364 *
4365 * 3) Share same flags except FIEMAP_EXTENT_LAST
4366 * So regular extent won't get merged with prealloc extent
4367 */
4368 if (cache->offset + cache->len == offset &&
4369 cache->phys + cache->len == phys &&
4370 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4371 (flags & ~FIEMAP_EXTENT_LAST)) {
4372 cache->len += len;
4373 cache->flags |= flags;
4374 goto try_submit_last;
4375 }
4376
4377 /* Not mergeable, need to submit cached one */
4378 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4379 cache->len, cache->flags);
4380 cache->cached = false;
4381 if (ret)
4382 return ret;
4383assign:
4384 cache->cached = true;
4385 cache->offset = offset;
4386 cache->phys = phys;
4387 cache->len = len;
4388 cache->flags = flags;
4389try_submit_last:
4390 if (cache->flags & FIEMAP_EXTENT_LAST) {
4391 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4392 cache->phys, cache->len, cache->flags);
4393 cache->cached = false;
4394 }
4395 return ret;
4396}
4397
4398/*
Qu Wenruo848c23b2017-06-22 10:01:21 +08004399 * Emit last fiemap cache
Qu Wenruo47518322017-04-07 10:43:15 +08004400 *
Qu Wenruo848c23b2017-06-22 10:01:21 +08004401 * The last fiemap cache may still be cached in the following case:
4402 * 0 4k 8k
4403 * |<- Fiemap range ->|
4404 * |<------------ First extent ----------->|
4405 *
4406 * In this case, the first extent range will be cached but not emitted.
4407 * So we must emit it before ending extent_fiemap().
Qu Wenruo47518322017-04-07 10:43:15 +08004408 */
Qu Wenruo848c23b2017-06-22 10:01:21 +08004409static int emit_last_fiemap_cache(struct btrfs_fs_info *fs_info,
4410 struct fiemap_extent_info *fieinfo,
4411 struct fiemap_cache *cache)
Qu Wenruo47518322017-04-07 10:43:15 +08004412{
4413 int ret;
4414
4415 if (!cache->cached)
4416 return 0;
4417
Qu Wenruo47518322017-04-07 10:43:15 +08004418 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4419 cache->len, cache->flags);
4420 cache->cached = false;
4421 if (ret > 0)
4422 ret = 0;
4423 return ret;
4424}
4425
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004426int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterba2135fb92017-06-23 04:09:57 +02004427 __u64 start, __u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004428{
Josef Bacik975f84f2010-11-23 19:36:57 +00004429 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004430 u64 off = start;
4431 u64 max = start + len;
4432 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004433 u32 found_type;
4434 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004435 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004436 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004437 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004438 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004439 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004440 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004441 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004442 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004443 struct fiemap_cache cache = { 0 };
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004444 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004445 u64 em_start = 0;
4446 u64 em_len = 0;
4447 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004448
4449 if (len == 0)
4450 return -EINVAL;
4451
Josef Bacik975f84f2010-11-23 19:36:57 +00004452 path = btrfs_alloc_path();
4453 if (!path)
4454 return -ENOMEM;
4455 path->leave_spinning = 1;
4456
Jeff Mahoneyda170662016-06-15 09:22:56 -04004457 start = round_down(start, btrfs_inode_sectorsize(inode));
4458 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004459
Chris Masonec29ed52011-02-23 16:23:20 -05004460 /*
4461 * lookup the last file extent. We're not using i_size here
4462 * because there might be preallocation past i_size
4463 */
David Sterbaf85b7372017-01-20 14:54:07 +01004464 ret = btrfs_lookup_file_extent(NULL, root, path,
4465 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004466 if (ret < 0) {
4467 btrfs_free_path(path);
4468 return ret;
Liu Bo2d324f52016-05-17 17:21:48 -07004469 } else {
4470 WARN_ON(!ret);
4471 if (ret == 1)
4472 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004473 }
Liu Bo2d324f52016-05-17 17:21:48 -07004474
Josef Bacik975f84f2010-11-23 19:36:57 +00004475 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004476 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004477 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004478
Chris Masonec29ed52011-02-23 16:23:20 -05004479 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004480 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004481 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004482 /* have to trust i_size as the end */
4483 last = (u64)-1;
4484 last_for_get_extent = isize;
4485 } else {
4486 /*
4487 * remember the start of the last extent. There are a
4488 * bunch of different factors that go into the length of the
4489 * extent, so its much less complex to remember where it started
4490 */
4491 last = found_key.offset;
4492 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004493 }
Liu Bofe09e162013-09-22 12:54:23 +08004494 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004495
Chris Masonec29ed52011-02-23 16:23:20 -05004496 /*
4497 * we might have some extents allocated but more delalloc past those
4498 * extents. so, we trust isize unless the start of the last extent is
4499 * beyond isize
4500 */
4501 if (last < isize) {
4502 last = (u64)-1;
4503 last_for_get_extent = isize;
4504 }
4505
David Sterbaff13db42015-12-03 14:30:40 +01004506 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004507 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004508
David Sterbae3350e12017-06-23 04:09:57 +02004509 em = get_extent_skip_holes(inode, start, last_for_get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004510 if (!em)
4511 goto out;
4512 if (IS_ERR(em)) {
4513 ret = PTR_ERR(em);
4514 goto out;
4515 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004516
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004517 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004518 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004519
Chris Masonea8efc72011-03-08 11:54:40 -05004520 /* break if the extent we found is outside the range */
4521 if (em->start >= max || extent_map_end(em) < off)
4522 break;
4523
4524 /*
4525 * get_extent may return an extent that starts before our
4526 * requested range. We have to make sure the ranges
4527 * we return to fiemap always move forward and don't
4528 * overlap, so adjust the offsets here
4529 */
4530 em_start = max(em->start, off);
4531
4532 /*
4533 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004534 * for adjusting the disk offset below. Only do this if the
4535 * extent isn't compressed since our in ram offset may be past
4536 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004537 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004538 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4539 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004540 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004541 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004542 flags = 0;
Filipe Mananaf0986312018-06-20 10:02:30 +01004543 if (em->block_start < EXTENT_MAP_LAST_BYTE)
4544 disko = em->block_start + offset_in_extent;
4545 else
4546 disko = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004547
Chris Masonea8efc72011-03-08 11:54:40 -05004548 /*
4549 * bump off for our next call to get_extent
4550 */
4551 off = extent_map_end(em);
4552 if (off >= max)
4553 end = 1;
4554
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004555 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004556 end = 1;
4557 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004558 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004559 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4560 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004561 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004562 flags |= (FIEMAP_EXTENT_DELALLOC |
4563 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004564 } else if (fieinfo->fi_extents_max) {
4565 u64 bytenr = em->block_start -
4566 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004567
Liu Bofe09e162013-09-22 12:54:23 +08004568 /*
4569 * As btrfs supports shared space, this information
4570 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004571 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4572 * then we're just getting a count and we can skip the
4573 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004574 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06004575 ret = btrfs_check_shared(root,
4576 btrfs_ino(BTRFS_I(inode)),
4577 bytenr);
Josef Bacikdc046b12014-09-10 16:20:45 -04004578 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004579 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004580 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004581 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004582 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004583 }
4584 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4585 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004586 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4587 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004588
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004589 free_extent_map(em);
4590 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004591 if ((em_start >= last) || em_len == (u64)-1 ||
4592 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004593 flags |= FIEMAP_EXTENT_LAST;
4594 end = 1;
4595 }
4596
Chris Masonec29ed52011-02-23 16:23:20 -05004597 /* now scan forward to see if this is really the last extent. */
David Sterbae3350e12017-06-23 04:09:57 +02004598 em = get_extent_skip_holes(inode, off, last_for_get_extent);
Chris Masonec29ed52011-02-23 16:23:20 -05004599 if (IS_ERR(em)) {
4600 ret = PTR_ERR(em);
4601 goto out;
4602 }
4603 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004604 flags |= FIEMAP_EXTENT_LAST;
4605 end = 1;
4606 }
Qu Wenruo47518322017-04-07 10:43:15 +08004607 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4608 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004609 if (ret) {
4610 if (ret == 1)
4611 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004612 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004613 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004614 }
4615out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004616 if (!ret)
Qu Wenruo848c23b2017-06-22 10:01:21 +08004617 ret = emit_last_fiemap_cache(root->fs_info, fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004618 free_extent_map(em);
4619out:
Liu Bofe09e162013-09-22 12:54:23 +08004620 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004621 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004622 &cached_state);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004623 return ret;
4624}
4625
Chris Mason727011e2010-08-06 13:21:20 -04004626static void __free_extent_buffer(struct extent_buffer *eb)
4627{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004628 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004629 kmem_cache_free(extent_buffer_cache, eb);
4630}
4631
Josef Bacika26e8c92014-03-28 17:07:27 -04004632int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004633{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004634 return (atomic_read(&eb->io_pages) ||
4635 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4636 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004637}
4638
Miao Xie897ca6e92010-10-26 20:57:29 -04004639/*
David Sterba55ac0132018-07-19 17:24:32 +02004640 * Release all pages attached to the extent buffer.
Miao Xie897ca6e92010-10-26 20:57:29 -04004641 */
David Sterba55ac0132018-07-19 17:24:32 +02004642static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004643{
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004644 int i;
4645 int num_pages;
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004646 int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004647
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004648 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004649
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004650 num_pages = num_extent_pages(eb);
4651 for (i = 0; i < num_pages; i++) {
4652 struct page *page = eb->pages[i];
Miao Xie897ca6e92010-10-26 20:57:29 -04004653
Forrest Liu5d2361d2015-02-09 17:31:45 +08004654 if (!page)
4655 continue;
4656 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004657 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004658 /*
4659 * We do this since we'll remove the pages after we've
4660 * removed the eb from the radix tree, so we could race
4661 * and have this page now attached to the new eb. So
4662 * only clear page_private if it's still connected to
4663 * this eb.
4664 */
4665 if (PagePrivate(page) &&
4666 page->private == (unsigned long)eb) {
4667 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4668 BUG_ON(PageDirty(page));
4669 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004670 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004671 * We need to make sure we haven't be attached
4672 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004673 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004674 ClearPagePrivate(page);
4675 set_page_private(page, 0);
4676 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004677 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004678 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004679
4680 if (mapped)
4681 spin_unlock(&page->mapping->private_lock);
4682
Nicholas D Steeves01327612016-05-19 21:18:45 -04004683 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004684 put_page(page);
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004685 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004686}
4687
4688/*
4689 * Helper for releasing the extent buffer.
4690 */
4691static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4692{
David Sterba55ac0132018-07-19 17:24:32 +02004693 btrfs_release_extent_buffer_pages(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004694 __free_extent_buffer(eb);
4695}
4696
Josef Bacikf28491e2013-12-16 13:24:27 -05004697static struct extent_buffer *
4698__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004699 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004700{
4701 struct extent_buffer *eb = NULL;
4702
Michal Hockod1b5c562015-08-19 14:17:40 +02004703 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004704 eb->start = start;
4705 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004706 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004707 eb->bflags = 0;
4708 rwlock_init(&eb->lock);
4709 atomic_set(&eb->write_locks, 0);
4710 atomic_set(&eb->read_locks, 0);
4711 atomic_set(&eb->blocking_readers, 0);
4712 atomic_set(&eb->blocking_writers, 0);
4713 atomic_set(&eb->spinning_readers, 0);
4714 atomic_set(&eb->spinning_writers, 0);
4715 eb->lock_nested = 0;
4716 init_waitqueue_head(&eb->write_lock_wq);
4717 init_waitqueue_head(&eb->read_lock_wq);
4718
4719 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4720
4721 spin_lock_init(&eb->refs_lock);
4722 atomic_set(&eb->refs, 1);
4723 atomic_set(&eb->io_pages, 0);
4724
4725 /*
4726 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4727 */
4728 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4729 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4730 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4731
4732 return eb;
4733}
4734
4735struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4736{
David Sterbacc5e31a2018-03-01 18:20:27 +01004737 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004738 struct page *p;
4739 struct extent_buffer *new;
David Sterbacc5e31a2018-03-01 18:20:27 +01004740 int num_pages = num_extent_pages(src);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004741
David Sterba3f556f72014-06-15 03:20:26 +02004742 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004743 if (new == NULL)
4744 return NULL;
4745
4746 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004747 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004748 if (!p) {
4749 btrfs_release_extent_buffer(new);
4750 return NULL;
4751 }
4752 attach_extent_buffer_page(new, p);
4753 WARN_ON(PageDirty(p));
4754 SetPageUptodate(p);
4755 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004756 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004757 }
4758
Josef Bacikdb7f3432013-08-07 14:54:37 -04004759 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004760 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004761
4762 return new;
4763}
4764
Omar Sandoval0f331222015-09-29 20:50:31 -07004765struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4766 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004767{
4768 struct extent_buffer *eb;
David Sterbacc5e31a2018-03-01 18:20:27 +01004769 int num_pages;
4770 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004771
David Sterba3f556f72014-06-15 03:20:26 +02004772 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004773 if (!eb)
4774 return NULL;
4775
David Sterba65ad0102018-06-29 10:56:49 +02004776 num_pages = num_extent_pages(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004777 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004778 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004779 if (!eb->pages[i])
4780 goto err;
4781 }
4782 set_extent_buffer_uptodate(eb);
4783 btrfs_set_header_nritems(eb, 0);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004784 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004785
4786 return eb;
4787err:
4788 for (; i > 0; i--)
4789 __free_page(eb->pages[i - 1]);
4790 __free_extent_buffer(eb);
4791 return NULL;
4792}
4793
Omar Sandoval0f331222015-09-29 20:50:31 -07004794struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004795 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004796{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004797 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004798}
4799
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004800static void check_buffer_tree_ref(struct extent_buffer *eb)
4801{
Chris Mason242e18c2013-01-29 17:49:37 -05004802 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004803 /* the ref bit is tricky. We have to make sure it is set
4804 * if we have the buffer dirty. Otherwise the
4805 * code to free a buffer can end up dropping a dirty
4806 * page
4807 *
4808 * Once the ref bit is set, it won't go away while the
4809 * buffer is dirty or in writeback, and it also won't
4810 * go away while we have the reference count on the
4811 * eb bumped.
4812 *
4813 * We can't just set the ref bit without bumping the
4814 * ref on the eb because free_extent_buffer might
4815 * see the ref bit and try to clear it. If this happens
4816 * free_extent_buffer might end up dropping our original
4817 * ref by mistake and freeing the page before we are able
4818 * to add one more ref.
4819 *
4820 * So bump the ref count first, then set the bit. If someone
4821 * beat us to it, drop the ref we added.
4822 */
Chris Mason242e18c2013-01-29 17:49:37 -05004823 refs = atomic_read(&eb->refs);
4824 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4825 return;
4826
Josef Bacik594831c2012-07-20 16:11:08 -04004827 spin_lock(&eb->refs_lock);
4828 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004829 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004830 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004831}
4832
Mel Gorman2457aec2014-06-04 16:10:31 -07004833static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4834 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004835{
David Sterbacc5e31a2018-03-01 18:20:27 +01004836 int num_pages, i;
Josef Bacik5df42352012-03-15 18:24:42 -04004837
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004838 check_buffer_tree_ref(eb);
4839
David Sterba65ad0102018-06-29 10:56:49 +02004840 num_pages = num_extent_pages(eb);
Josef Bacik5df42352012-03-15 18:24:42 -04004841 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004842 struct page *p = eb->pages[i];
4843
Mel Gorman2457aec2014-06-04 16:10:31 -07004844 if (p != accessed)
4845 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004846 }
4847}
4848
Josef Bacikf28491e2013-12-16 13:24:27 -05004849struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4850 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004851{
4852 struct extent_buffer *eb;
4853
4854 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004855 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004856 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004857 if (eb && atomic_inc_not_zero(&eb->refs)) {
4858 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004859 /*
4860 * Lock our eb's refs_lock to avoid races with
4861 * free_extent_buffer. When we get our eb it might be flagged
4862 * with EXTENT_BUFFER_STALE and another task running
4863 * free_extent_buffer might have seen that flag set,
4864 * eb->refs == 2, that the buffer isn't under IO (dirty and
4865 * writeback flags not set) and it's still in the tree (flag
4866 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4867 * of decrementing the extent buffer's reference count twice.
4868 * So here we could race and increment the eb's reference count,
4869 * clear its stale flag, mark it as dirty and drop our reference
4870 * before the other task finishes executing free_extent_buffer,
4871 * which would later result in an attempt to free an extent
4872 * buffer that is dirty.
4873 */
4874 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4875 spin_lock(&eb->refs_lock);
4876 spin_unlock(&eb->refs_lock);
4877 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004878 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004879 return eb;
4880 }
4881 rcu_read_unlock();
4882
4883 return NULL;
4884}
4885
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004886#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4887struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004888 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004889{
4890 struct extent_buffer *eb, *exists = NULL;
4891 int ret;
4892
4893 eb = find_extent_buffer(fs_info, start);
4894 if (eb)
4895 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04004896 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004897 if (!eb)
4898 return NULL;
4899 eb->fs_info = fs_info;
4900again:
David Sterbae1860a72016-05-09 14:11:38 +02004901 ret = radix_tree_preload(GFP_NOFS);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004902 if (ret)
4903 goto free_eb;
4904 spin_lock(&fs_info->buffer_lock);
4905 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004906 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004907 spin_unlock(&fs_info->buffer_lock);
4908 radix_tree_preload_end();
4909 if (ret == -EEXIST) {
4910 exists = find_extent_buffer(fs_info, start);
4911 if (exists)
4912 goto free_eb;
4913 else
4914 goto again;
4915 }
4916 check_buffer_tree_ref(eb);
4917 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
4918
4919 /*
4920 * We will free dummy extent buffer's if they come into
4921 * free_extent_buffer with a ref count of 2, but if we are using this we
4922 * want the buffers to stay in memory until we're done with them, so
4923 * bump the ref count again.
4924 */
4925 atomic_inc(&eb->refs);
4926 return eb;
4927free_eb:
4928 btrfs_release_extent_buffer(eb);
4929 return exists;
4930}
4931#endif
4932
Josef Bacikf28491e2013-12-16 13:24:27 -05004933struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004934 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05004935{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004936 unsigned long len = fs_info->nodesize;
David Sterbacc5e31a2018-03-01 18:20:27 +01004937 int num_pages;
4938 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004939 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004940 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004941 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004942 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05004943 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05004944 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004945 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004946
Jeff Mahoneyda170662016-06-15 09:22:56 -04004947 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07004948 btrfs_err(fs_info, "bad tree block start %llu", start);
4949 return ERR_PTR(-EINVAL);
4950 }
4951
Josef Bacikf28491e2013-12-16 13:24:27 -05004952 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004953 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004954 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004955
David Sterba23d79d82014-06-15 02:55:29 +02004956 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04004957 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07004958 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05004959
David Sterba65ad0102018-06-29 10:56:49 +02004960 num_pages = num_extent_pages(eb);
Chris Mason727011e2010-08-06 13:21:20 -04004961 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02004962 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07004963 if (!p) {
4964 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04004965 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004966 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004967
4968 spin_lock(&mapping->private_lock);
4969 if (PagePrivate(p)) {
4970 /*
4971 * We could have already allocated an eb for this page
4972 * and attached one so lets see if we can get a ref on
4973 * the existing eb, and if we can we know it's good and
4974 * we can just return that one, else we know we can just
4975 * overwrite page->private.
4976 */
4977 exists = (struct extent_buffer *)p->private;
4978 if (atomic_inc_not_zero(&exists->refs)) {
4979 spin_unlock(&mapping->private_lock);
4980 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004981 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07004982 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004983 goto free_eb;
4984 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08004985 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004986
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004987 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004988 * Do this so attach doesn't complain and we need to
4989 * drop the ref the old guy had.
4990 */
4991 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004992 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004993 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05004994 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004995 attach_extent_buffer_page(eb, p);
4996 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004997 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04004998 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004999 if (!PageUptodate(p))
5000 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05005001
5002 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005003 * We can't unlock the pages just yet since the extent buffer
5004 * hasn't been properly inserted in the radix tree, this
5005 * opens a race with btree_releasepage which can free a page
5006 * while we are still filling in all pages for the buffer and
5007 * we could crash.
Chris Masoneb14ab82011-02-10 12:35:00 -05005008 */
Chris Masond1310b22008-01-24 16:13:08 -05005009 }
5010 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05005011 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05005012again:
David Sterbae1860a72016-05-09 14:11:38 +02005013 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07005014 if (ret) {
5015 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04005016 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005017 }
Miao Xie19fe0a82010-10-26 20:57:29 -04005018
Josef Bacikf28491e2013-12-16 13:24:27 -05005019 spin_lock(&fs_info->buffer_lock);
5020 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005021 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05005022 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005023 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04005024 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005025 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005026 if (exists)
5027 goto free_eb;
5028 else
Josef Bacik115391d2012-03-09 09:51:43 -05005029 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04005030 }
Chris Mason6af118ce2008-07-22 11:18:07 -04005031 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005032 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005033 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005034
5035 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005036 * Now it's safe to unlock the pages because any calls to
5037 * btree_releasepage will correctly detect that a page belongs to a
5038 * live buffer and won't free them prematurely.
Chris Masoneb14ab82011-02-10 12:35:00 -05005039 */
Nikolay Borisov28187ae2018-07-04 10:24:51 +03005040 for (i = 0; i < num_pages; i++)
5041 unlock_page(eb->pages[i]);
Chris Masond1310b22008-01-24 16:13:08 -05005042 return eb;
5043
Chris Mason6af118ce2008-07-22 11:18:07 -04005044free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005045 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005046 for (i = 0; i < num_pages; i++) {
5047 if (eb->pages[i])
5048 unlock_page(eb->pages[i]);
5049 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005050
Miao Xie897ca6e92010-10-26 20:57:29 -04005051 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005052 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005053}
Chris Masond1310b22008-01-24 16:13:08 -05005054
Josef Bacik3083ee22012-03-09 16:01:49 -05005055static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5056{
5057 struct extent_buffer *eb =
5058 container_of(head, struct extent_buffer, rcu_head);
5059
5060 __free_extent_buffer(eb);
5061}
5062
David Sterbaf7a52a42013-04-26 14:56:29 +00005063static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005064{
Nikolay Borisov07e21c42018-06-27 16:38:23 +03005065 lockdep_assert_held(&eb->refs_lock);
5066
Josef Bacik3083ee22012-03-09 16:01:49 -05005067 WARN_ON(atomic_read(&eb->refs) == 0);
5068 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005069 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005070 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005071
Jan Schmidt815a51c2012-05-16 17:00:02 +02005072 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005073
Josef Bacikf28491e2013-12-16 13:24:27 -05005074 spin_lock(&fs_info->buffer_lock);
5075 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005076 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005077 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005078 } else {
5079 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005080 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005081
5082 /* Should be safe to release our pages at this point */
David Sterba55ac0132018-07-19 17:24:32 +02005083 btrfs_release_extent_buffer_pages(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005084#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005085 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
Josef Bacikbcb7e442015-03-16 17:38:02 -04005086 __free_extent_buffer(eb);
5087 return 1;
5088 }
5089#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005090 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005091 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005092 }
5093 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005094
5095 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005096}
5097
Chris Masond1310b22008-01-24 16:13:08 -05005098void free_extent_buffer(struct extent_buffer *eb)
5099{
Chris Mason242e18c2013-01-29 17:49:37 -05005100 int refs;
5101 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005102 if (!eb)
5103 return;
5104
Chris Mason242e18c2013-01-29 17:49:37 -05005105 while (1) {
5106 refs = atomic_read(&eb->refs);
5107 if (refs <= 3)
5108 break;
5109 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5110 if (old == refs)
5111 return;
5112 }
5113
Josef Bacik3083ee22012-03-09 16:01:49 -05005114 spin_lock(&eb->refs_lock);
5115 if (atomic_read(&eb->refs) == 2 &&
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005116 test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))
Jan Schmidt815a51c2012-05-16 17:00:02 +02005117 atomic_dec(&eb->refs);
5118
5119 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005120 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005121 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005122 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5123 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005124
Josef Bacik3083ee22012-03-09 16:01:49 -05005125 /*
5126 * I know this is terrible, but it's temporary until we stop tracking
5127 * the uptodate bits and such for the extent buffers.
5128 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005129 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005130}
Chris Masond1310b22008-01-24 16:13:08 -05005131
Josef Bacik3083ee22012-03-09 16:01:49 -05005132void free_extent_buffer_stale(struct extent_buffer *eb)
5133{
5134 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005135 return;
5136
Josef Bacik3083ee22012-03-09 16:01:49 -05005137 spin_lock(&eb->refs_lock);
5138 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5139
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005140 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005141 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5142 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005143 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005144}
5145
Chris Mason1d4284b2012-03-28 20:31:37 -04005146void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005147{
David Sterbacc5e31a2018-03-01 18:20:27 +01005148 int i;
5149 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005150 struct page *page;
5151
David Sterba65ad0102018-06-29 10:56:49 +02005152 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005153
5154 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005155 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005156 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005157 continue;
5158
Chris Masona61e6f22008-07-22 11:18:08 -04005159 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005160 WARN_ON(!PagePrivate(page));
5161
Chris Masond1310b22008-01-24 16:13:08 -05005162 clear_page_dirty_for_io(page);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005163 xa_lock_irq(&page->mapping->i_pages);
Chris Masond1310b22008-01-24 16:13:08 -05005164 if (!PageDirty(page)) {
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005165 radix_tree_tag_clear(&page->mapping->i_pages,
Chris Masond1310b22008-01-24 16:13:08 -05005166 page_index(page),
5167 PAGECACHE_TAG_DIRTY);
5168 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005169 xa_unlock_irq(&page->mapping->i_pages);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005170 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005171 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005172 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005173 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005174}
Chris Masond1310b22008-01-24 16:13:08 -05005175
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005176int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005177{
David Sterbacc5e31a2018-03-01 18:20:27 +01005178 int i;
5179 int num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04005180 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005181
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005182 check_buffer_tree_ref(eb);
5183
Chris Masonb9473432009-03-13 11:00:37 -04005184 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005185
David Sterba65ad0102018-06-29 10:56:49 +02005186 num_pages = num_extent_pages(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05005187 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005188 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5189
Chris Masonb9473432009-03-13 11:00:37 -04005190 for (i = 0; i < num_pages; i++)
David Sterbafb85fc92014-07-31 01:03:53 +02005191 set_page_dirty(eb->pages[i]);
Chris Masonb9473432009-03-13 11:00:37 -04005192 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005193}
Chris Masond1310b22008-01-24 16:13:08 -05005194
David Sterba69ba3922015-12-03 13:08:59 +01005195void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005196{
David Sterbacc5e31a2018-03-01 18:20:27 +01005197 int i;
Chris Mason1259ab72008-05-12 13:39:03 -04005198 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005199 int num_pages;
Chris Mason1259ab72008-05-12 13:39:03 -04005200
Chris Masonb4ce94d2009-02-04 09:25:08 -05005201 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005202 num_pages = num_extent_pages(eb);
Chris Mason1259ab72008-05-12 13:39:03 -04005203 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005204 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005205 if (page)
5206 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005207 }
Chris Mason1259ab72008-05-12 13:39:03 -04005208}
5209
David Sterba09c25a82015-12-03 13:08:59 +01005210void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005211{
David Sterbacc5e31a2018-03-01 18:20:27 +01005212 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005213 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005214 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005215
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005216 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005217 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005218 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005219 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005220 SetPageUptodate(page);
5221 }
Chris Masond1310b22008-01-24 16:13:08 -05005222}
Chris Masond1310b22008-01-24 16:13:08 -05005223
Chris Masond1310b22008-01-24 16:13:08 -05005224int read_extent_buffer_pages(struct extent_io_tree *tree,
David Sterba6af49db2017-06-23 04:09:57 +02005225 struct extent_buffer *eb, int wait, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005226{
David Sterbacc5e31a2018-03-01 18:20:27 +01005227 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005228 struct page *page;
5229 int err;
5230 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005231 int locked_pages = 0;
5232 int all_uptodate = 1;
David Sterbacc5e31a2018-03-01 18:20:27 +01005233 int num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005234 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005235 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005236 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005237
Chris Masonb4ce94d2009-02-04 09:25:08 -05005238 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005239 return 0;
5240
David Sterba65ad0102018-06-29 10:56:49 +02005241 num_pages = num_extent_pages(eb);
Josef Bacik8436ea912016-09-02 15:40:03 -04005242 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005243 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005244 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005245 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005246 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005247 } else {
5248 lock_page(page);
5249 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005250 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005251 }
5252 /*
5253 * We need to firstly lock all pages to make sure that
5254 * the uptodate bit of our pages won't be affected by
5255 * clear_extent_buffer_uptodate().
5256 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005257 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005258 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005259 if (!PageUptodate(page)) {
5260 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005261 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005262 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005263 }
Liu Bo2571e732016-08-03 12:33:01 -07005264
Chris Masonce9adaa2008-04-09 16:28:12 -04005265 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005266 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005267 goto unlock_exit;
5268 }
5269
Filipe Manana656f30d2014-09-26 12:25:56 +01005270 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005271 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005272 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005273 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005274 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005275
Chris Masonce9adaa2008-04-09 16:28:12 -04005276 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005277 if (ret) {
5278 atomic_dec(&eb->io_pages);
5279 unlock_page(page);
5280 continue;
5281 }
5282
Chris Masonf1885912008-04-09 16:28:12 -04005283 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005284 err = __extent_read_full_page(tree, page,
David Sterba6af49db2017-06-23 04:09:57 +02005285 btree_get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005286 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005287 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005288 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005289 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005290 /*
5291 * We use &bio in above __extent_read_full_page,
5292 * so we ensure that if it returns error, the
5293 * current page fails to add itself to bio and
5294 * it's been unlocked.
5295 *
5296 * We must dec io_pages by ourselves.
5297 */
5298 atomic_dec(&eb->io_pages);
5299 }
Chris Masond1310b22008-01-24 16:13:08 -05005300 } else {
5301 unlock_page(page);
5302 }
5303 }
5304
Jeff Mahoney355808c2011-10-03 23:23:14 -04005305 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005306 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005307 if (err)
5308 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005309 }
Chris Masona86c12c2008-02-07 10:50:54 -05005310
Arne Jansenbb82ab82011-06-10 14:06:53 +02005311 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005312 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005313
Josef Bacik8436ea912016-09-02 15:40:03 -04005314 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005315 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005316 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005317 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005318 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005319 }
Chris Masond3977122009-01-05 21:25:51 -05005320
Chris Masond1310b22008-01-24 16:13:08 -05005321 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005322
5323unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005324 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005325 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005326 page = eb->pages[locked_pages];
5327 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005328 }
5329 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005330}
Chris Masond1310b22008-01-24 16:13:08 -05005331
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005332void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5333 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005334{
5335 size_t cur;
5336 size_t offset;
5337 struct page *page;
5338 char *kaddr;
5339 char *dst = (char *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005340 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5341 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005342
Liu Bof716abd2017-08-09 11:10:16 -06005343 if (start + len > eb->len) {
5344 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5345 eb->start, eb->len, start, len);
5346 memset(dst, 0, len);
5347 return;
5348 }
Chris Masond1310b22008-01-24 16:13:08 -05005349
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005350 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005351
Chris Masond3977122009-01-05 21:25:51 -05005352 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005353 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005354
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005355 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005356 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005357 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005358
5359 dst += cur;
5360 len -= cur;
5361 offset = 0;
5362 i++;
5363 }
5364}
Chris Masond1310b22008-01-24 16:13:08 -05005365
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005366int read_extent_buffer_to_user(const struct extent_buffer *eb,
5367 void __user *dstv,
5368 unsigned long start, unsigned long len)
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005369{
5370 size_t cur;
5371 size_t offset;
5372 struct page *page;
5373 char *kaddr;
5374 char __user *dst = (char __user *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005375 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5376 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005377 int ret = 0;
5378
5379 WARN_ON(start > eb->len);
5380 WARN_ON(start + len > eb->start + eb->len);
5381
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005382 offset = (start_offset + start) & (PAGE_SIZE - 1);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005383
5384 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005385 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005386
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005387 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005388 kaddr = page_address(page);
5389 if (copy_to_user(dst, kaddr + offset, cur)) {
5390 ret = -EFAULT;
5391 break;
5392 }
5393
5394 dst += cur;
5395 len -= cur;
5396 offset = 0;
5397 i++;
5398 }
5399
5400 return ret;
5401}
5402
Liu Bo415b35a2016-06-17 19:16:21 -07005403/*
5404 * return 0 if the item is found within a page.
5405 * return 1 if the item spans two pages.
5406 * return -EINVAL otherwise.
5407 */
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005408int map_private_extent_buffer(const struct extent_buffer *eb,
5409 unsigned long start, unsigned long min_len,
5410 char **map, unsigned long *map_start,
5411 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005412{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005413 size_t offset = start & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005414 char *kaddr;
5415 struct page *p;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005416 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5417 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005418 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005419 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005420
Liu Bof716abd2017-08-09 11:10:16 -06005421 if (start + min_len > eb->len) {
5422 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5423 eb->start, eb->len, start, min_len);
5424 return -EINVAL;
5425 }
5426
Chris Masond1310b22008-01-24 16:13:08 -05005427 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005428 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005429
5430 if (i == 0) {
5431 offset = start_offset;
5432 *map_start = 0;
5433 } else {
5434 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005435 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005436 }
Chris Masond3977122009-01-05 21:25:51 -05005437
David Sterbafb85fc92014-07-31 01:03:53 +02005438 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005439 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005440 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005441 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005442 return 0;
5443}
Chris Masond1310b22008-01-24 16:13:08 -05005444
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005445int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5446 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005447{
5448 size_t cur;
5449 size_t offset;
5450 struct page *page;
5451 char *kaddr;
5452 char *ptr = (char *)ptrv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005453 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5454 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005455 int ret = 0;
5456
5457 WARN_ON(start > eb->len);
5458 WARN_ON(start + len > eb->start + eb->len);
5459
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005460 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005461
Chris Masond3977122009-01-05 21:25:51 -05005462 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005463 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005464
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005465 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005466
Chris Masona6591712011-07-19 12:04:14 -04005467 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005468 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005469 if (ret)
5470 break;
5471
5472 ptr += cur;
5473 len -= cur;
5474 offset = 0;
5475 i++;
5476 }
5477 return ret;
5478}
Chris Masond1310b22008-01-24 16:13:08 -05005479
David Sterbaf157bf72016-11-09 17:43:38 +01005480void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5481 const void *srcv)
5482{
5483 char *kaddr;
5484
5485 WARN_ON(!PageUptodate(eb->pages[0]));
5486 kaddr = page_address(eb->pages[0]);
5487 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5488 BTRFS_FSID_SIZE);
5489}
5490
5491void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5492{
5493 char *kaddr;
5494
5495 WARN_ON(!PageUptodate(eb->pages[0]));
5496 kaddr = page_address(eb->pages[0]);
5497 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5498 BTRFS_FSID_SIZE);
5499}
5500
Chris Masond1310b22008-01-24 16:13:08 -05005501void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5502 unsigned long start, unsigned long len)
5503{
5504 size_t cur;
5505 size_t offset;
5506 struct page *page;
5507 char *kaddr;
5508 char *src = (char *)srcv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005509 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5510 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005511
5512 WARN_ON(start > eb->len);
5513 WARN_ON(start + len > eb->start + eb->len);
5514
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005515 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005516
Chris Masond3977122009-01-05 21:25:51 -05005517 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005518 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005519 WARN_ON(!PageUptodate(page));
5520
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005521 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005522 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005523 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005524
5525 src += cur;
5526 len -= cur;
5527 offset = 0;
5528 i++;
5529 }
5530}
Chris Masond1310b22008-01-24 16:13:08 -05005531
David Sterbab159fa22016-11-08 18:09:03 +01005532void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5533 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005534{
5535 size_t cur;
5536 size_t offset;
5537 struct page *page;
5538 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005539 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5540 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005541
5542 WARN_ON(start > eb->len);
5543 WARN_ON(start + len > eb->start + eb->len);
5544
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005545 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005546
Chris Masond3977122009-01-05 21:25:51 -05005547 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005548 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005549 WARN_ON(!PageUptodate(page));
5550
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005551 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005552 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005553 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005554
5555 len -= cur;
5556 offset = 0;
5557 i++;
5558 }
5559}
Chris Masond1310b22008-01-24 16:13:08 -05005560
David Sterba58e80122016-11-08 18:30:31 +01005561void copy_extent_buffer_full(struct extent_buffer *dst,
5562 struct extent_buffer *src)
5563{
5564 int i;
David Sterbacc5e31a2018-03-01 18:20:27 +01005565 int num_pages;
David Sterba58e80122016-11-08 18:30:31 +01005566
5567 ASSERT(dst->len == src->len);
5568
David Sterba65ad0102018-06-29 10:56:49 +02005569 num_pages = num_extent_pages(dst);
David Sterba58e80122016-11-08 18:30:31 +01005570 for (i = 0; i < num_pages; i++)
5571 copy_page(page_address(dst->pages[i]),
5572 page_address(src->pages[i]));
5573}
5574
Chris Masond1310b22008-01-24 16:13:08 -05005575void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5576 unsigned long dst_offset, unsigned long src_offset,
5577 unsigned long len)
5578{
5579 u64 dst_len = dst->len;
5580 size_t cur;
5581 size_t offset;
5582 struct page *page;
5583 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005584 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
5585 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005586
5587 WARN_ON(src->len != dst_len);
5588
5589 offset = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005590 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005591
Chris Masond3977122009-01-05 21:25:51 -05005592 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005593 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005594 WARN_ON(!PageUptodate(page));
5595
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005596 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005597
Chris Masona6591712011-07-19 12:04:14 -04005598 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005599 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005600
5601 src_offset += cur;
5602 len -= cur;
5603 offset = 0;
5604 i++;
5605 }
5606}
Chris Masond1310b22008-01-24 16:13:08 -05005607
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005608/*
5609 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5610 * given bit number
5611 * @eb: the extent buffer
5612 * @start: offset of the bitmap item in the extent buffer
5613 * @nr: bit number
5614 * @page_index: return index of the page in the extent buffer that contains the
5615 * given bit number
5616 * @page_offset: return offset into the page given by page_index
5617 *
5618 * This helper hides the ugliness of finding the byte in an extent buffer which
5619 * contains a given bit.
5620 */
5621static inline void eb_bitmap_offset(struct extent_buffer *eb,
5622 unsigned long start, unsigned long nr,
5623 unsigned long *page_index,
5624 size_t *page_offset)
5625{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005626 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005627 size_t byte_offset = BIT_BYTE(nr);
5628 size_t offset;
5629
5630 /*
5631 * The byte we want is the offset of the extent buffer + the offset of
5632 * the bitmap item in the extent buffer + the offset of the byte in the
5633 * bitmap item.
5634 */
5635 offset = start_offset + start + byte_offset;
5636
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005637 *page_index = offset >> PAGE_SHIFT;
5638 *page_offset = offset & (PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005639}
5640
5641/**
5642 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5643 * @eb: the extent buffer
5644 * @start: offset of the bitmap item in the extent buffer
5645 * @nr: bit number to test
5646 */
5647int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5648 unsigned long nr)
5649{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005650 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005651 struct page *page;
5652 unsigned long i;
5653 size_t offset;
5654
5655 eb_bitmap_offset(eb, start, nr, &i, &offset);
5656 page = eb->pages[i];
5657 WARN_ON(!PageUptodate(page));
5658 kaddr = page_address(page);
5659 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5660}
5661
5662/**
5663 * extent_buffer_bitmap_set - set an area of a bitmap
5664 * @eb: the extent buffer
5665 * @start: offset of the bitmap item in the extent buffer
5666 * @pos: bit number of the first bit
5667 * @len: number of bits to set
5668 */
5669void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5670 unsigned long pos, unsigned long len)
5671{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005672 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005673 struct page *page;
5674 unsigned long i;
5675 size_t offset;
5676 const unsigned int size = pos + len;
5677 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005678 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005679
5680 eb_bitmap_offset(eb, start, pos, &i, &offset);
5681 page = eb->pages[i];
5682 WARN_ON(!PageUptodate(page));
5683 kaddr = page_address(page);
5684
5685 while (len >= bits_to_set) {
5686 kaddr[offset] |= mask_to_set;
5687 len -= bits_to_set;
5688 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005689 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005690 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005691 offset = 0;
5692 page = eb->pages[++i];
5693 WARN_ON(!PageUptodate(page));
5694 kaddr = page_address(page);
5695 }
5696 }
5697 if (len) {
5698 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5699 kaddr[offset] |= mask_to_set;
5700 }
5701}
5702
5703
5704/**
5705 * extent_buffer_bitmap_clear - clear an area of a bitmap
5706 * @eb: the extent buffer
5707 * @start: offset of the bitmap item in the extent buffer
5708 * @pos: bit number of the first bit
5709 * @len: number of bits to clear
5710 */
5711void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5712 unsigned long pos, unsigned long len)
5713{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005714 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005715 struct page *page;
5716 unsigned long i;
5717 size_t offset;
5718 const unsigned int size = pos + len;
5719 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005720 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005721
5722 eb_bitmap_offset(eb, start, pos, &i, &offset);
5723 page = eb->pages[i];
5724 WARN_ON(!PageUptodate(page));
5725 kaddr = page_address(page);
5726
5727 while (len >= bits_to_clear) {
5728 kaddr[offset] &= ~mask_to_clear;
5729 len -= bits_to_clear;
5730 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005731 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005732 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005733 offset = 0;
5734 page = eb->pages[++i];
5735 WARN_ON(!PageUptodate(page));
5736 kaddr = page_address(page);
5737 }
5738 }
5739 if (len) {
5740 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5741 kaddr[offset] &= ~mask_to_clear;
5742 }
5743}
5744
Sergei Trofimovich33872062011-04-11 21:52:52 +00005745static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5746{
5747 unsigned long distance = (src > dst) ? src - dst : dst - src;
5748 return distance < len;
5749}
5750
Chris Masond1310b22008-01-24 16:13:08 -05005751static void copy_pages(struct page *dst_page, struct page *src_page,
5752 unsigned long dst_off, unsigned long src_off,
5753 unsigned long len)
5754{
Chris Masona6591712011-07-19 12:04:14 -04005755 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005756 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005757 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005758
Sergei Trofimovich33872062011-04-11 21:52:52 +00005759 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005760 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005761 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005762 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005763 if (areas_overlap(src_off, dst_off, len))
5764 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005765 }
Chris Masond1310b22008-01-24 16:13:08 -05005766
Chris Mason727011e2010-08-06 13:21:20 -04005767 if (must_memmove)
5768 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5769 else
5770 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005771}
5772
5773void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5774 unsigned long src_offset, unsigned long len)
5775{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005776 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005777 size_t cur;
5778 size_t dst_off_in_page;
5779 size_t src_off_in_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005780 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005781 unsigned long dst_i;
5782 unsigned long src_i;
5783
5784 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005785 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005786 "memmove bogus src_offset %lu move len %lu dst len %lu",
5787 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005788 BUG_ON(1);
5789 }
5790 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005791 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005792 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5793 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005794 BUG_ON(1);
5795 }
5796
Chris Masond3977122009-01-05 21:25:51 -05005797 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005798 dst_off_in_page = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005799 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005800 src_off_in_page = (start_offset + src_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005801 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005802
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005803 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5804 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005805
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005806 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005807 src_off_in_page));
5808 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005809 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005810
David Sterbafb85fc92014-07-31 01:03:53 +02005811 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005812 dst_off_in_page, src_off_in_page, cur);
5813
5814 src_offset += cur;
5815 dst_offset += cur;
5816 len -= cur;
5817 }
5818}
Chris Masond1310b22008-01-24 16:13:08 -05005819
5820void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5821 unsigned long src_offset, unsigned long len)
5822{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005823 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005824 size_t cur;
5825 size_t dst_off_in_page;
5826 size_t src_off_in_page;
5827 unsigned long dst_end = dst_offset + len - 1;
5828 unsigned long src_end = src_offset + len - 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005829 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005830 unsigned long dst_i;
5831 unsigned long src_i;
5832
5833 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005834 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005835 "memmove bogus src_offset %lu move len %lu len %lu",
5836 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005837 BUG_ON(1);
5838 }
5839 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005840 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005841 "memmove bogus dst_offset %lu move len %lu len %lu",
5842 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005843 BUG_ON(1);
5844 }
Chris Mason727011e2010-08-06 13:21:20 -04005845 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005846 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5847 return;
5848 }
Chris Masond3977122009-01-05 21:25:51 -05005849 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005850 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
5851 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005852
5853 dst_off_in_page = (start_offset + dst_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005854 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005855 src_off_in_page = (start_offset + src_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005856 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005857
5858 cur = min_t(unsigned long, len, src_off_in_page + 1);
5859 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005860 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005861 dst_off_in_page - cur + 1,
5862 src_off_in_page - cur + 1, cur);
5863
5864 dst_end -= cur;
5865 src_end -= cur;
5866 len -= cur;
5867 }
5868}
Chris Mason6af118ce2008-07-22 11:18:07 -04005869
David Sterbaf7a52a42013-04-26 14:56:29 +00005870int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005871{
Chris Mason6af118ce2008-07-22 11:18:07 -04005872 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005873
Miao Xie19fe0a82010-10-26 20:57:29 -04005874 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005875 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05005876 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005877 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005878 spin_lock(&page->mapping->private_lock);
5879 if (!PagePrivate(page)) {
5880 spin_unlock(&page->mapping->private_lock);
5881 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005882 }
5883
Josef Bacik3083ee22012-03-09 16:01:49 -05005884 eb = (struct extent_buffer *)page->private;
5885 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005886
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005887 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005888 * This is a little awful but should be ok, we need to make sure that
5889 * the eb doesn't disappear out from under us while we're looking at
5890 * this page.
5891 */
5892 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005893 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005894 spin_unlock(&eb->refs_lock);
5895 spin_unlock(&page->mapping->private_lock);
5896 return 0;
5897 }
5898 spin_unlock(&page->mapping->private_lock);
5899
Josef Bacik3083ee22012-03-09 16:01:49 -05005900 /*
5901 * If tree ref isn't set then we know the ref on this eb is a real ref,
5902 * so just return, this page will likely be freed soon anyway.
5903 */
5904 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5905 spin_unlock(&eb->refs_lock);
5906 return 0;
5907 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005908
David Sterbaf7a52a42013-04-26 14:56:29 +00005909 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005910}