Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 2 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3 | #include <linux/bitops.h> |
| 4 | #include <linux/slab.h> |
| 5 | #include <linux/bio.h> |
| 6 | #include <linux/mm.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7 | #include <linux/pagemap.h> |
| 8 | #include <linux/page-flags.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 9 | #include <linux/spinlock.h> |
| 10 | #include <linux/blkdev.h> |
| 11 | #include <linux/swap.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 12 | #include <linux/writeback.h> |
| 13 | #include <linux/pagevec.h> |
Linus Torvalds | 268bb0c | 2011-05-20 12:50:29 -0700 | [diff] [blame] | 14 | #include <linux/prefetch.h> |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 15 | #include <linux/cleancache.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 16 | #include "extent_io.h" |
| 17 | #include "extent_map.h" |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 18 | #include "ctree.h" |
| 19 | #include "btrfs_inode.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 20 | #include "volumes.h" |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 21 | #include "check-integrity.h" |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 22 | #include "locking.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 23 | #include "rcu-string.h" |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 24 | #include "backref.h" |
David Sterba | 6af49db | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 25 | #include "disk-io.h" |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 26 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 27 | static struct kmem_cache *extent_state_cache; |
| 28 | static struct kmem_cache *extent_buffer_cache; |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 29 | static struct bio_set btrfs_bioset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 30 | |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 31 | static inline bool extent_state_in_tree(const struct extent_state *state) |
| 32 | { |
| 33 | return !RB_EMPTY_NODE(&state->rb_node); |
| 34 | } |
| 35 | |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 36 | #ifdef CONFIG_BTRFS_DEBUG |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 37 | static LIST_HEAD(buffers); |
| 38 | static LIST_HEAD(states); |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 39 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 40 | static DEFINE_SPINLOCK(leak_lock); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 41 | |
| 42 | static inline |
| 43 | void 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 | |
| 52 | static inline |
| 53 | void 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 | |
| 62 | static inline |
| 63 | void 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 Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 70 | pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n", |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 71 | state->start, state->end, state->state, |
| 72 | extent_state_in_tree(state), |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 73 | refcount_read(&state->refs)); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 74 | 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 Bo | af2679e | 2018-01-25 11:02:48 -0700 | [diff] [blame] | 80 | 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 Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 82 | list_del(&eb->leak_list); |
| 83 | kmem_cache_free(extent_buffer_cache, eb); |
| 84 | } |
| 85 | } |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 86 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 87 | #define btrfs_debug_check_extent_io_range(tree, start, end) \ |
| 88 | __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end)) |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 89 | static inline void __btrfs_debug_check_extent_io_range(const char *caller, |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 90 | struct extent_io_tree *tree, u64 start, u64 end) |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 91 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 92 | if (tree->ops && tree->ops->check_extent_io_range) |
| 93 | tree->ops->check_extent_io_range(tree->private_data, caller, |
| 94 | start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 95 | } |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 96 | #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 Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 100 | #define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0) |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 101 | #endif |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 102 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 103 | #define BUFFER_LRU_MAX 64 |
| 104 | |
| 105 | struct tree_entry { |
| 106 | u64 start; |
| 107 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 108 | struct rb_node rb_node; |
| 109 | }; |
| 110 | |
| 111 | struct extent_page_data { |
| 112 | struct bio *bio; |
| 113 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 114 | /* tells writepage not to lock the state bits for this range |
| 115 | * it still does the unlocking |
| 116 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 117 | unsigned int extent_locked:1; |
| 118 | |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 119 | /* tells the submit_bio code to use REQ_SYNC */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 120 | unsigned int sync_io:1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 121 | }; |
| 122 | |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 123 | static int add_extent_changeset(struct extent_state *state, unsigned bits, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 124 | struct extent_changeset *changeset, |
| 125 | int set) |
| 126 | { |
| 127 | int ret; |
| 128 | |
| 129 | if (!changeset) |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 130 | return 0; |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 131 | if (set && (state->state & bits) == bits) |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 132 | return 0; |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 133 | if (!set && (state->state & bits) == 0) |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 134 | return 0; |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 135 | changeset->bytes_changed += state->end - state->start + 1; |
David Sterba | 53d3235 | 2017-02-13 13:42:29 +0100 | [diff] [blame] | 136 | ret = ulist_add(&changeset->range_changed, state->start, state->end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 137 | GFP_ATOMIC); |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 138 | return ret; |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 139 | } |
| 140 | |
David Sterba | aab6e9e | 2017-11-30 18:00:02 +0100 | [diff] [blame] | 141 | static void flush_write_bio(struct extent_page_data *epd); |
David Sterba | e2932ee | 2017-06-23 04:16:17 +0200 | [diff] [blame] | 142 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 143 | int __init extent_io_init(void) |
| 144 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 145 | extent_state_cache = kmem_cache_create("btrfs_extent_state", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 146 | sizeof(struct extent_state), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 147 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 148 | if (!extent_state_cache) |
| 149 | return -ENOMEM; |
| 150 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 151 | extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 152 | sizeof(struct extent_buffer), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 153 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 154 | if (!extent_buffer_cache) |
| 155 | goto free_state_cache; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 156 | |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 157 | if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE, |
| 158 | offsetof(struct btrfs_io_bio, bio), |
| 159 | BIOSET_NEED_BVECS)) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 160 | goto free_buffer_cache; |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 161 | |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 162 | if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE)) |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 163 | goto free_bioset; |
| 164 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 165 | return 0; |
| 166 | |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 167 | free_bioset: |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 168 | bioset_exit(&btrfs_bioset); |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 169 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 170 | free_buffer_cache: |
| 171 | kmem_cache_destroy(extent_buffer_cache); |
| 172 | extent_buffer_cache = NULL; |
| 173 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 174 | free_state_cache: |
| 175 | kmem_cache_destroy(extent_state_cache); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 176 | extent_state_cache = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 177 | return -ENOMEM; |
| 178 | } |
| 179 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 180 | void __cold extent_io_exit(void) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 181 | { |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 182 | btrfs_leak_debug_check(); |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | * Make sure all delayed rcu free are flushed before we |
| 186 | * destroy caches. |
| 187 | */ |
| 188 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 189 | kmem_cache_destroy(extent_state_cache); |
| 190 | kmem_cache_destroy(extent_buffer_cache); |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 191 | bioset_exit(&btrfs_bioset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void extent_io_tree_init(struct extent_io_tree *tree, |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 195 | void *private_data) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 196 | { |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 197 | tree->state = RB_ROOT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 198 | tree->ops = NULL; |
| 199 | tree->dirty_bytes = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 200 | spin_lock_init(&tree->lock); |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 201 | tree->private_data = private_data; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 202 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 203 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 204 | static struct extent_state *alloc_extent_state(gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 205 | { |
| 206 | struct extent_state *state; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 207 | |
Michal Hocko | 3ba7ab2 | 2017-01-09 15:39:02 +0100 | [diff] [blame] | 208 | /* |
| 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 Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 213 | state = kmem_cache_alloc(extent_state_cache, mask); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 214 | if (!state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 215 | return state; |
| 216 | state->state = 0; |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 217 | state->failrec = NULL; |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 218 | RB_CLEAR_NODE(&state->rb_node); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 219 | btrfs_leak_debug_add(&state->leak_list, &states); |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 220 | refcount_set(&state->refs, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 221 | init_waitqueue_head(&state->wq); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 222 | trace_alloc_extent_state(state, mask, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 223 | return state; |
| 224 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 225 | |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 226 | void free_extent_state(struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 227 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 228 | if (!state) |
| 229 | return; |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 230 | if (refcount_dec_and_test(&state->refs)) { |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 231 | WARN_ON(extent_state_in_tree(state)); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 232 | btrfs_leak_debug_del(&state->leak_list); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 233 | trace_free_extent_state(state, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 234 | kmem_cache_free(extent_state_cache, state); |
| 235 | } |
| 236 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 237 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 238 | static struct rb_node *tree_insert(struct rb_root *root, |
| 239 | struct rb_node *search_start, |
| 240 | u64 offset, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 241 | struct rb_node *node, |
| 242 | struct rb_node ***p_in, |
| 243 | struct rb_node **parent_in) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 244 | { |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 245 | struct rb_node **p; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 246 | struct rb_node *parent = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 247 | struct tree_entry *entry; |
| 248 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 249 | if (p_in && parent_in) { |
| 250 | p = *p_in; |
| 251 | parent = *parent_in; |
| 252 | goto do_insert; |
| 253 | } |
| 254 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 255 | p = search_start ? &search_start : &root->rb_node; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 256 | while (*p) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 257 | 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 Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 268 | do_insert: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 269 | rb_link_node(node, parent, p); |
| 270 | rb_insert_color(node, root); |
| 271 | return NULL; |
| 272 | } |
| 273 | |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 274 | static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 275 | struct rb_node **prev_ret, |
| 276 | struct rb_node **next_ret, |
| 277 | struct rb_node ***p_ret, |
| 278 | struct rb_node **parent_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 279 | { |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 280 | struct rb_root *root = &tree->state; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 281 | struct rb_node **n = &root->rb_node; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 282 | 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 Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 287 | while (*n) { |
| 288 | prev = *n; |
| 289 | entry = rb_entry(prev, struct tree_entry, rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 290 | prev_entry = entry; |
| 291 | |
| 292 | if (offset < entry->start) |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 293 | n = &(*n)->rb_left; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 294 | else if (offset > entry->end) |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 295 | n = &(*n)->rb_right; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 296 | else |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 297 | return *n; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 298 | } |
| 299 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 300 | if (p_ret) |
| 301 | *p_ret = n; |
| 302 | if (parent_ret) |
| 303 | *parent_ret = prev; |
| 304 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 305 | if (prev_ret) { |
| 306 | orig_prev = prev; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 307 | while (prev && offset > prev_entry->end) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 308 | 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 Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 317 | while (prev && offset < prev_entry->start) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 318 | 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 Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 326 | static inline struct rb_node * |
| 327 | tree_search_for_insert(struct extent_io_tree *tree, |
| 328 | u64 offset, |
| 329 | struct rb_node ***p_ret, |
| 330 | struct rb_node **parent_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 331 | { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 332 | struct rb_node *prev = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 333 | struct rb_node *ret; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 334 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 335 | ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 336 | if (!ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 337 | return prev; |
| 338 | return ret; |
| 339 | } |
| 340 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 341 | static 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 Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 347 | static 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 Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 351 | tree->ops->merge_extent_hook(tree->private_data, new, other); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 352 | } |
| 353 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 354 | /* |
| 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 Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 363 | static void merge_state(struct extent_io_tree *tree, |
| 364 | struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 365 | { |
| 366 | struct extent_state *other; |
| 367 | struct rb_node *other_node; |
| 368 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 369 | if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 370 | return; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 371 | |
| 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 Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 377 | merge_cb(tree, state, other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 378 | state->start = other->start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 379 | rb_erase(&other->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 380 | RB_CLEAR_NODE(&other->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 381 | 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 Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 389 | merge_cb(tree, state, other); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 390 | state->end = other->end; |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 391 | rb_erase(&other->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 392 | RB_CLEAR_NODE(&other->rb_node); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 393 | free_extent_state(other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 394 | } |
| 395 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 396 | } |
| 397 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 398 | static void set_state_cb(struct extent_io_tree *tree, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 399 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 400 | { |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 401 | if (tree->ops && tree->ops->set_bit_hook) |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 402 | tree->ops->set_bit_hook(tree->private_data, state, bits); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | static void clear_state_cb(struct extent_io_tree *tree, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 406 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 407 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 408 | if (tree->ops && tree->ops->clear_bit_hook) |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 409 | tree->ops->clear_bit_hook(tree->private_data, state, bits); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 410 | } |
| 411 | |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 412 | static void set_state_bits(struct extent_io_tree *tree, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 413 | struct extent_state *state, unsigned *bits, |
| 414 | struct extent_changeset *changeset); |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 415 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 416 | /* |
| 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 | */ |
| 426 | static int insert_state(struct extent_io_tree *tree, |
| 427 | struct extent_state *state, u64 start, u64 end, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 428 | struct rb_node ***p, |
| 429 | struct rb_node **parent, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 430 | unsigned *bits, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 431 | { |
| 432 | struct rb_node *node; |
| 433 | |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 434 | if (end < start) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 435 | WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 436 | end, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 437 | state->start = start; |
| 438 | state->end = end; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 439 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 440 | set_state_bits(tree, state, bits, changeset); |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 441 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 442 | node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 443 | if (node) { |
| 444 | struct extent_state *found; |
| 445 | found = rb_entry(node, struct extent_state, rb_node); |
Jeff Mahoney | 62e8557 | 2016-09-20 10:05:01 -0400 | [diff] [blame] | 446 | pr_err("BTRFS: found node %llu %llu on insert of %llu %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 447 | found->start, found->end, start, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 448 | return -EEXIST; |
| 449 | } |
| 450 | merge_state(tree, state); |
| 451 | return 0; |
| 452 | } |
| 453 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 454 | static void split_cb(struct extent_io_tree *tree, struct extent_state *orig, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 455 | u64 split) |
| 456 | { |
| 457 | if (tree->ops && tree->ops->split_extent_hook) |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 458 | tree->ops->split_extent_hook(tree->private_data, orig, split); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 459 | } |
| 460 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 461 | /* |
| 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 | */ |
| 475 | static 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 Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 479 | |
| 480 | split_cb(tree, orig, split); |
| 481 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 482 | prealloc->start = orig->start; |
| 483 | prealloc->end = split - 1; |
| 484 | prealloc->state = orig->state; |
| 485 | orig->start = split; |
| 486 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 487 | node = tree_insert(&tree->state, &orig->rb_node, prealloc->end, |
| 488 | &prealloc->rb_node, NULL, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 489 | if (node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 490 | free_extent_state(prealloc); |
| 491 | return -EEXIST; |
| 492 | } |
| 493 | return 0; |
| 494 | } |
| 495 | |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 496 | static 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 Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 505 | /* |
| 506 | * utility function to clear some bits in an extent state struct. |
Wang Sheng-Hui | 1b303fc | 2012-04-06 14:35:18 +0800 | [diff] [blame] | 507 | * it will optionally wake up any one waiting on this state (wake == 1). |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 508 | * |
| 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 Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 512 | static struct extent_state *clear_state_bit(struct extent_io_tree *tree, |
| 513 | struct extent_state *state, |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 514 | unsigned *bits, int wake, |
| 515 | struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 516 | { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 517 | struct extent_state *next; |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 518 | unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS; |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 519 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 520 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 521 | if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 522 | u64 range = state->end - state->start + 1; |
| 523 | WARN_ON(range > tree->dirty_bytes); |
| 524 | tree->dirty_bytes -= range; |
| 525 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 526 | clear_state_cb(tree, state, bits); |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 527 | ret = add_extent_changeset(state, bits_to_clear, changeset, 0); |
| 528 | BUG_ON(ret < 0); |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 529 | state->state &= ~bits_to_clear; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 530 | if (wake) |
| 531 | wake_up(&state->wq); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 532 | if (state->state == 0) { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 533 | next = next_state(state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 534 | if (extent_state_in_tree(state)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 535 | rb_erase(&state->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 536 | RB_CLEAR_NODE(&state->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 537 | free_extent_state(state); |
| 538 | } else { |
| 539 | WARN_ON(1); |
| 540 | } |
| 541 | } else { |
| 542 | merge_state(tree, state); |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 543 | next = next_state(state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 544 | } |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 545 | return next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 546 | } |
| 547 | |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 548 | static struct extent_state * |
| 549 | alloc_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 Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 557 | static void extent_io_tree_panic(struct extent_io_tree *tree, int err) |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 558 | { |
David Sterba | 05912a3 | 2018-07-18 19:23:45 +0200 | [diff] [blame] | 559 | 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 Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 563 | } |
| 564 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 565 | /* |
| 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 Mahoney | 6763af8 | 2012-03-01 14:56:29 +0100 | [diff] [blame] | 575 | * This takes the tree lock, and returns 0 on success and < 0 on error. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 576 | */ |
David Sterba | 66b0c88 | 2017-10-31 16:30:47 +0100 | [diff] [blame] | 577 | int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 578 | unsigned bits, int wake, int delete, |
| 579 | struct extent_state **cached_state, |
| 580 | gfp_t mask, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 581 | { |
| 582 | struct extent_state *state; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 583 | struct extent_state *cached; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 584 | struct extent_state *prealloc = NULL; |
| 585 | struct rb_node *node; |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 586 | u64 last_end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 587 | int err; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 588 | int clear = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 589 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 590 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 591 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 592 | if (bits & EXTENT_DELALLOC) |
| 593 | bits |= EXTENT_NORESERVE; |
| 594 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 595 | if (delete) |
| 596 | bits |= ~EXTENT_CTLBITS; |
| 597 | bits |= EXTENT_FIRST_DELALLOC; |
| 598 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 599 | if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY)) |
| 600 | clear = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 601 | again: |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 602 | if (!prealloc && gfpflags_allow_blocking(mask)) { |
Filipe Manana | c7bc631 | 2014-11-03 14:12:57 +0000 | [diff] [blame] | 603 | /* |
| 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 Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 610 | prealloc = alloc_extent_state(mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 611 | } |
| 612 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 613 | spin_lock(&tree->lock); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 614 | if (cached_state) { |
| 615 | cached = *cached_state; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 616 | |
| 617 | if (clear) { |
| 618 | *cached_state = NULL; |
| 619 | cached_state = NULL; |
| 620 | } |
| 621 | |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 622 | if (cached && extent_state_in_tree(cached) && |
| 623 | cached->start <= start && cached->end > start) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 624 | if (clear) |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 625 | refcount_dec(&cached->refs); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 626 | state = cached; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 627 | goto hit_next; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 628 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 629 | if (clear) |
| 630 | free_extent_state(cached); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 631 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 632 | /* |
| 633 | * this search will find the extents that end after |
| 634 | * our range starts |
| 635 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 636 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 637 | if (!node) |
| 638 | goto out; |
| 639 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 640 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 641 | if (state->start > end) |
| 642 | goto out; |
| 643 | WARN_ON(state->end < start); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 644 | last_end = state->end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 645 | |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 646 | /* the state doesn't have the wanted bits, go ahead */ |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 647 | if (!(state->state & bits)) { |
| 648 | state = next_state(state); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 649 | goto next; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 650 | } |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 651 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 652 | /* |
| 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 Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 669 | prealloc = alloc_extent_state_atomic(prealloc); |
| 670 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 671 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 672 | if (err) |
| 673 | extent_io_tree_panic(tree, err); |
| 674 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 675 | prealloc = NULL; |
| 676 | if (err) |
| 677 | goto out; |
| 678 | if (state->end <= end) { |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 679 | state = clear_state_bit(tree, state, &bits, wake, |
| 680 | changeset); |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 681 | goto next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 682 | } |
| 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 Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 692 | prealloc = alloc_extent_state_atomic(prealloc); |
| 693 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 694 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 695 | if (err) |
| 696 | extent_io_tree_panic(tree, err); |
| 697 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 698 | if (wake) |
| 699 | wake_up(&state->wq); |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 700 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 701 | clear_state_bit(tree, prealloc, &bits, wake, changeset); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 702 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 703 | prealloc = NULL; |
| 704 | goto out; |
| 705 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 706 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 707 | state = clear_state_bit(tree, state, &bits, wake, changeset); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 708 | next: |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 709 | if (last_end == (u64)-1) |
| 710 | goto out; |
| 711 | start = last_end + 1; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 712 | if (start <= end && state && !need_resched()) |
Liu Bo | 692e575 | 2012-02-16 18:34:36 +0800 | [diff] [blame] | 713 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 714 | |
| 715 | search_again: |
| 716 | if (start > end) |
| 717 | goto out; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 718 | spin_unlock(&tree->lock); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 719 | if (gfpflags_allow_blocking(mask)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 720 | cond_resched(); |
| 721 | goto again; |
David Sterba | 7ab5cb2 | 2016-04-27 01:02:15 +0200 | [diff] [blame] | 722 | |
| 723 | out: |
| 724 | spin_unlock(&tree->lock); |
| 725 | if (prealloc) |
| 726 | free_extent_state(prealloc); |
| 727 | |
| 728 | return 0; |
| 729 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 730 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 731 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 732 | static void wait_on_state(struct extent_io_tree *tree, |
| 733 | struct extent_state *state) |
Christoph Hellwig | 641f521 | 2008-12-02 06:36:10 -0500 | [diff] [blame] | 734 | __releases(tree->lock) |
| 735 | __acquires(tree->lock) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 736 | { |
| 737 | DEFINE_WAIT(wait); |
| 738 | prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 739 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 740 | schedule(); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 741 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 742 | finish_wait(&state->wq, &wait); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 743 | } |
| 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 Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 750 | static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 751 | unsigned long bits) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 752 | { |
| 753 | struct extent_state *state; |
| 754 | struct rb_node *node; |
| 755 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 756 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 757 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 758 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 759 | again: |
| 760 | while (1) { |
| 761 | /* |
| 762 | * this search will find all the extents that end after |
| 763 | * our range starts |
| 764 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 765 | node = tree_search(tree, start); |
Filipe Manana | c50d3e7 | 2014-03-31 14:53:25 +0100 | [diff] [blame] | 766 | process_node: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 767 | 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 Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 777 | refcount_inc(&state->refs); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 778 | 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 Manana | c50d3e7 | 2014-03-31 14:53:25 +0100 | [diff] [blame] | 787 | if (!cond_resched_lock(&tree->lock)) { |
| 788 | node = rb_next(node); |
| 789 | goto process_node; |
| 790 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 791 | } |
| 792 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 793 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 794 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 795 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 796 | static void set_state_bits(struct extent_io_tree *tree, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 797 | struct extent_state *state, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 798 | unsigned *bits, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 799 | { |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 800 | unsigned bits_to_set = *bits & ~EXTENT_CTLBITS; |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 801 | int ret; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 802 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 803 | set_state_cb(tree, state, bits); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 804 | if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 805 | u64 range = state->end - state->start + 1; |
| 806 | tree->dirty_bytes += range; |
| 807 | } |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 808 | ret = add_extent_changeset(state, bits_to_set, changeset, 1); |
| 809 | BUG_ON(ret < 0); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 810 | state->state |= bits_to_set; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 811 | } |
| 812 | |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 813 | static void cache_state_if_flags(struct extent_state *state, |
| 814 | struct extent_state **cached_ptr, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 815 | unsigned flags) |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 816 | { |
| 817 | if (cached_ptr && !(*cached_ptr)) { |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 818 | if (!flags || (state->state & flags)) { |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 819 | *cached_ptr = state; |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 820 | refcount_inc(&state->refs); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 821 | } |
| 822 | } |
| 823 | } |
| 824 | |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 825 | static 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 Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 832 | /* |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 833 | * 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 Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 835 | * |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 836 | * 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 Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 839 | * |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 840 | * [start, end] is inclusive This takes the tree lock. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 841 | */ |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 842 | |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 843 | static int __must_check |
| 844 | __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 845 | unsigned bits, unsigned exclusive_bits, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 846 | u64 *failed_start, struct extent_state **cached_state, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 847 | gfp_t mask, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 848 | { |
| 849 | struct extent_state *state; |
| 850 | struct extent_state *prealloc = NULL; |
| 851 | struct rb_node *node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 852 | struct rb_node **p; |
| 853 | struct rb_node *parent; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 854 | int err = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 855 | u64 last_start; |
| 856 | u64 last_end; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 857 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 858 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 859 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 860 | bits |= EXTENT_FIRST_DELALLOC; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 861 | again: |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 862 | if (!prealloc && gfpflags_allow_blocking(mask)) { |
David Sterba | 059f791 | 2016-04-27 01:03:45 +0200 | [diff] [blame] | 863 | /* |
| 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 Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 870 | prealloc = alloc_extent_state(mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 871 | } |
| 872 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 873 | spin_lock(&tree->lock); |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 874 | if (cached_state && *cached_state) { |
| 875 | state = *cached_state; |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 876 | if (state->start <= start && state->end > start && |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 877 | extent_state_in_tree(state)) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 878 | node = &state->rb_node; |
| 879 | goto hit_next; |
| 880 | } |
| 881 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 882 | /* |
| 883 | * this search will find all the extents that end after |
| 884 | * our range starts. |
| 885 | */ |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 886 | node = tree_search_for_insert(tree, start, &p, &parent); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 887 | if (!node) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 888 | prealloc = alloc_extent_state_atomic(prealloc); |
| 889 | BUG_ON(!prealloc); |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 890 | err = insert_state(tree, prealloc, start, end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 891 | &p, &parent, &bits, changeset); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 892 | if (err) |
| 893 | extent_io_tree_panic(tree, err); |
| 894 | |
Filipe David Borba Manana | c42ac0b | 2013-11-26 15:01:34 +0000 | [diff] [blame] | 895 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 896 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 897 | goto out; |
| 898 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 899 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 40431d6 | 2009-08-05 12:57:59 -0400 | [diff] [blame] | 900 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 901 | 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 Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 911 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 912 | *failed_start = state->start; |
| 913 | err = -EEXIST; |
| 914 | goto out; |
| 915 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 916 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 917 | set_state_bits(tree, state, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 918 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 919 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 920 | if (last_end == (u64)-1) |
| 921 | goto out; |
| 922 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 923 | state = next_state(state); |
| 924 | if (start < end && state && state->start == start && |
| 925 | !need_resched()) |
| 926 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 927 | 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 Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 947 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 948 | *failed_start = start; |
| 949 | err = -EEXIST; |
| 950 | goto out; |
| 951 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 952 | |
| 953 | prealloc = alloc_extent_state_atomic(prealloc); |
| 954 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 955 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 956 | if (err) |
| 957 | extent_io_tree_panic(tree, err); |
| 958 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 959 | prealloc = NULL; |
| 960 | if (err) |
| 961 | goto out; |
| 962 | if (state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 963 | set_state_bits(tree, state, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 964 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 965 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 966 | if (last_end == (u64)-1) |
| 967 | goto out; |
| 968 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 969 | state = next_state(state); |
| 970 | if (start < end && state && state->start == start && |
| 971 | !need_resched()) |
| 972 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 973 | } |
| 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 Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 988 | this_end = last_start - 1; |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 989 | |
| 990 | prealloc = alloc_extent_state_atomic(prealloc); |
| 991 | BUG_ON(!prealloc); |
Xiao Guangrong | c7f895a | 2011-04-20 06:45:49 +0000 | [diff] [blame] | 992 | |
| 993 | /* |
| 994 | * Avoid to free 'prealloc' if it can be merged with |
| 995 | * the later extent. |
| 996 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 997 | err = insert_state(tree, prealloc, start, this_end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 998 | NULL, NULL, &bits, changeset); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 999 | if (err) |
| 1000 | extent_io_tree_panic(tree, err); |
| 1001 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1002 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1003 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1004 | 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 Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1014 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1015 | *failed_start = start; |
| 1016 | err = -EEXIST; |
| 1017 | goto out; |
| 1018 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 1019 | |
| 1020 | prealloc = alloc_extent_state_atomic(prealloc); |
| 1021 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1022 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1023 | if (err) |
| 1024 | extent_io_tree_panic(tree, err); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1025 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1026 | set_state_bits(tree, prealloc, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1027 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1028 | merge_state(tree, prealloc); |
| 1029 | prealloc = NULL; |
| 1030 | goto out; |
| 1031 | } |
| 1032 | |
David Sterba | b5a4ba14 | 2016-04-27 01:02:15 +0200 | [diff] [blame] | 1033 | search_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 Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1040 | |
| 1041 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1042 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1043 | if (prealloc) |
| 1044 | free_extent_state(prealloc); |
| 1045 | |
| 1046 | return err; |
| 1047 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1048 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1049 | |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1050 | int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1051 | unsigned bits, u64 * failed_start, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1052 | struct extent_state **cached_state, gfp_t mask) |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1053 | { |
| 1054 | return __set_extent_bit(tree, start, end, bits, 0, failed_start, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1055 | cached_state, mask, NULL); |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1059 | /** |
Liu Bo | 10983f2 | 2012-07-11 15:26:19 +0800 | [diff] [blame] | 1060 | * convert_extent_bit - convert all bits in a given range from one bit to |
| 1061 | * another |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1062 | * @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 Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1067 | * @cached_state: state that we're going to cache |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1068 | * |
| 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 Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1074 | * |
| 1075 | * All allocations are done with GFP_NOFS. |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1076 | */ |
| 1077 | int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1078 | unsigned bits, unsigned clear_bits, |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1079 | struct extent_state **cached_state) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1080 | { |
| 1081 | struct extent_state *state; |
| 1082 | struct extent_state *prealloc = NULL; |
| 1083 | struct rb_node *node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1084 | struct rb_node **p; |
| 1085 | struct rb_node *parent; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1086 | int err = 0; |
| 1087 | u64 last_start; |
| 1088 | u64 last_end; |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1089 | bool first_iteration = true; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1090 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 1091 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 1092 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1093 | again: |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1094 | if (!prealloc) { |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1095 | /* |
| 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 Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1102 | prealloc = alloc_extent_state(GFP_NOFS); |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1103 | if (!prealloc && !first_iteration) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1104 | return -ENOMEM; |
| 1105 | } |
| 1106 | |
| 1107 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1108 | if (cached_state && *cached_state) { |
| 1109 | state = *cached_state; |
| 1110 | if (state->start <= start && state->end > start && |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1111 | extent_state_in_tree(state)) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1112 | node = &state->rb_node; |
| 1113 | goto hit_next; |
| 1114 | } |
| 1115 | } |
| 1116 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1117 | /* |
| 1118 | * this search will find all the extents that end after |
| 1119 | * our range starts. |
| 1120 | */ |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1121 | node = tree_search_for_insert(tree, start, &p, &parent); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1122 | if (!node) { |
| 1123 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1124 | if (!prealloc) { |
| 1125 | err = -ENOMEM; |
| 1126 | goto out; |
| 1127 | } |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1128 | err = insert_state(tree, prealloc, start, end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1129 | &p, &parent, &bits, NULL); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1130 | if (err) |
| 1131 | extent_io_tree_panic(tree, err); |
Filipe David Borba Manana | c42ac0b | 2013-11-26 15:01:34 +0000 | [diff] [blame] | 1132 | cache_state(prealloc, cached_state); |
| 1133 | prealloc = NULL; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1134 | goto out; |
| 1135 | } |
| 1136 | state = rb_entry(node, struct extent_state, rb_node); |
| 1137 | hit_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 Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1148 | set_state_bits(tree, state, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1149 | cache_state(state, cached_state); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1150 | state = clear_state_bit(tree, state, &clear_bits, 0, NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1151 | if (last_end == (u64)-1) |
| 1152 | goto out; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1153 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1154 | if (start < end && state && state->start == start && |
| 1155 | !need_resched()) |
| 1156 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1157 | 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 Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1178 | if (!prealloc) { |
| 1179 | err = -ENOMEM; |
| 1180 | goto out; |
| 1181 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1182 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1183 | if (err) |
| 1184 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1185 | prealloc = NULL; |
| 1186 | if (err) |
| 1187 | goto out; |
| 1188 | if (state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1189 | set_state_bits(tree, state, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1190 | cache_state(state, cached_state); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1191 | state = clear_state_bit(tree, state, &clear_bits, 0, |
| 1192 | NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1193 | if (last_end == (u64)-1) |
| 1194 | goto out; |
| 1195 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1196 | if (start < end && state && state->start == start && |
| 1197 | !need_resched()) |
| 1198 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1199 | } |
| 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 Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1217 | if (!prealloc) { |
| 1218 | err = -ENOMEM; |
| 1219 | goto out; |
| 1220 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1221 | |
| 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 Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1227 | NULL, NULL, &bits, NULL); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1228 | if (err) |
| 1229 | extent_io_tree_panic(tree, err); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1230 | cache_state(prealloc, cached_state); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1231 | 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 Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1243 | if (!prealloc) { |
| 1244 | err = -ENOMEM; |
| 1245 | goto out; |
| 1246 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1247 | |
| 1248 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1249 | if (err) |
| 1250 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1251 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1252 | set_state_bits(tree, prealloc, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1253 | cache_state(prealloc, cached_state); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1254 | clear_state_bit(tree, prealloc, &clear_bits, 0, NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1255 | prealloc = NULL; |
| 1256 | goto out; |
| 1257 | } |
| 1258 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1259 | search_again: |
| 1260 | if (start > end) |
| 1261 | goto out; |
| 1262 | spin_unlock(&tree->lock); |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1263 | cond_resched(); |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1264 | first_iteration = false; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1265 | goto again; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1266 | |
| 1267 | out: |
| 1268 | spin_unlock(&tree->lock); |
| 1269 | if (prealloc) |
| 1270 | free_extent_state(prealloc); |
| 1271 | |
| 1272 | return err; |
| 1273 | } |
| 1274 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1275 | /* wrappers around set/clear extent bit */ |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1276 | int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 2c53b91 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1277 | unsigned bits, struct extent_changeset *changeset) |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1278 | { |
| 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 Sterba | 2c53b91 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1287 | return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1288 | changeset); |
| 1289 | } |
| 1290 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1291 | int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 1292 | unsigned bits, int wake, int delete, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 1293 | struct extent_state **cached) |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1294 | { |
| 1295 | return __clear_extent_bit(tree, start, end, bits, wake, delete, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 1296 | cached, GFP_NOFS, NULL); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1297 | } |
| 1298 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1299 | int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | f734c44 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1300 | unsigned bits, struct extent_changeset *changeset) |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1301 | { |
| 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 Sterba | f734c44 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1308 | return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS, |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1309 | changeset); |
| 1310 | } |
| 1311 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1312 | /* |
| 1313 | * either insert or lock state struct between start and end use mask to tell |
| 1314 | * us if waiting is desired. |
| 1315 | */ |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1316 | int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1317 | struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1318 | { |
| 1319 | int err; |
| 1320 | u64 failed_start; |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1321 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1322 | while (1) { |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1323 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1324 | EXTENT_LOCKED, &failed_start, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1325 | cached_state, GFP_NOFS, NULL); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1326 | if (err == -EEXIST) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1327 | wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED); |
| 1328 | start = failed_start; |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1329 | } else |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1330 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1331 | WARN_ON(start > end); |
| 1332 | } |
| 1333 | return err; |
| 1334 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1335 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1336 | int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end) |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1337 | { |
| 1338 | int err; |
| 1339 | u64 failed_start; |
| 1340 | |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1341 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1342 | &failed_start, NULL, GFP_NOFS, NULL); |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1343 | if (err == -EEXIST) { |
| 1344 | if (failed_start > start) |
| 1345 | clear_extent_bit(tree, start, failed_start - 1, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 1346 | EXTENT_LOCKED, 1, 0, NULL); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1347 | return 0; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1348 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1349 | return 1; |
| 1350 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1351 | |
David Sterba | bd1fa4f | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 1352 | void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end) |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1353 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1354 | unsigned long index = start >> PAGE_SHIFT; |
| 1355 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1356 | 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. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1362 | put_page(page); |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1363 | index++; |
| 1364 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1365 | } |
| 1366 | |
David Sterba | f631157 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 1367 | void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end) |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1368 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1369 | unsigned long index = start >> PAGE_SHIFT; |
| 1370 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1371 | 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 Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1376 | __set_page_dirty_nobuffers(page); |
Konstantin Khebnikov | 8d38633 | 2015-02-11 15:26:55 -0800 | [diff] [blame] | 1377 | account_page_redirty(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1378 | put_page(page); |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1379 | index++; |
| 1380 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1381 | } |
| 1382 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1383 | /* find the first state struct with 'bits' set after 'start', and |
| 1384 | * return it. tree->lock must be held. NULL will returned if |
| 1385 | * nothing was found after 'start' |
| 1386 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1387 | static struct extent_state * |
| 1388 | find_first_extent_bit_state(struct extent_io_tree *tree, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1389 | u64 start, unsigned bits) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1390 | { |
| 1391 | struct rb_node *node; |
| 1392 | struct extent_state *state; |
| 1393 | |
| 1394 | /* |
| 1395 | * this search will find all the extents that end after |
| 1396 | * our range starts. |
| 1397 | */ |
| 1398 | node = tree_search(tree, start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1399 | if (!node) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1400 | goto out; |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1401 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1402 | while (1) { |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1403 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1404 | if (state->end >= start && (state->state & bits)) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1405 | return state; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1406 | |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1407 | node = rb_next(node); |
| 1408 | if (!node) |
| 1409 | break; |
| 1410 | } |
| 1411 | out: |
| 1412 | return NULL; |
| 1413 | } |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1414 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1415 | /* |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1416 | * find the first offset in the io tree with 'bits' set. zero is |
| 1417 | * returned if we find something, and *start_ret and *end_ret are |
| 1418 | * set to reflect the state struct that was found. |
| 1419 | * |
Wang Sheng-Hui | 477d7ea | 2012-04-06 14:35:47 +0800 | [diff] [blame] | 1420 | * If nothing was found, 1 is returned. If found something, return 0. |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1421 | */ |
| 1422 | int find_first_extent_bit(struct extent_io_tree *tree, u64 start, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1423 | u64 *start_ret, u64 *end_ret, unsigned bits, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1424 | struct extent_state **cached_state) |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1425 | { |
| 1426 | struct extent_state *state; |
| 1427 | int ret = 1; |
| 1428 | |
| 1429 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1430 | if (cached_state && *cached_state) { |
| 1431 | state = *cached_state; |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1432 | if (state->end == start - 1 && extent_state_in_tree(state)) { |
Liu Bo | 9688e9a | 2018-08-23 03:14:53 +0800 | [diff] [blame] | 1433 | while ((state = next_state(state)) != NULL) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1434 | if (state->state & bits) |
| 1435 | goto got_it; |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1436 | } |
| 1437 | free_extent_state(*cached_state); |
| 1438 | *cached_state = NULL; |
| 1439 | goto out; |
| 1440 | } |
| 1441 | free_extent_state(*cached_state); |
| 1442 | *cached_state = NULL; |
| 1443 | } |
| 1444 | |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1445 | state = find_first_extent_bit_state(tree, start, bits); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1446 | got_it: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1447 | if (state) { |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 1448 | cache_state_if_flags(state, cached_state, 0); |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1449 | *start_ret = state->start; |
| 1450 | *end_ret = state->end; |
| 1451 | ret = 0; |
| 1452 | } |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1453 | out: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1454 | spin_unlock(&tree->lock); |
| 1455 | return ret; |
| 1456 | } |
| 1457 | |
| 1458 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1459 | * find a contiguous range of bytes in the file marked as delalloc, not |
| 1460 | * more than 'max_bytes'. start and end are used to return the range, |
| 1461 | * |
| 1462 | * 1 is returned if we find something, 0 if nothing was in the tree |
| 1463 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1464 | static noinline u64 find_delalloc_range(struct extent_io_tree *tree, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1465 | u64 *start, u64 *end, u64 max_bytes, |
| 1466 | struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1467 | { |
| 1468 | struct rb_node *node; |
| 1469 | struct extent_state *state; |
| 1470 | u64 cur_start = *start; |
| 1471 | u64 found = 0; |
| 1472 | u64 total_bytes = 0; |
| 1473 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1474 | spin_lock(&tree->lock); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1475 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1476 | /* |
| 1477 | * this search will find all the extents that end after |
| 1478 | * our range starts. |
| 1479 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1480 | node = tree_search(tree, cur_start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1481 | if (!node) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1482 | if (!found) |
| 1483 | *end = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1484 | goto out; |
| 1485 | } |
| 1486 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1487 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1488 | state = rb_entry(node, struct extent_state, rb_node); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1489 | if (found && (state->start != cur_start || |
| 1490 | (state->state & EXTENT_BOUNDARY))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1491 | goto out; |
| 1492 | } |
| 1493 | if (!(state->state & EXTENT_DELALLOC)) { |
| 1494 | if (!found) |
| 1495 | *end = state->end; |
| 1496 | goto out; |
| 1497 | } |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1498 | if (!found) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1499 | *start = state->start; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1500 | *cached_state = state; |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 1501 | refcount_inc(&state->refs); |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1502 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1503 | found++; |
| 1504 | *end = state->end; |
| 1505 | cur_start = state->end + 1; |
| 1506 | node = rb_next(node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1507 | total_bytes += state->end - state->start + 1; |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 1508 | if (total_bytes >= max_bytes) |
Josef Bacik | 573aeca | 2013-08-30 14:38:49 -0400 | [diff] [blame] | 1509 | break; |
Josef Bacik | 573aeca | 2013-08-30 14:38:49 -0400 | [diff] [blame] | 1510 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1511 | break; |
| 1512 | } |
| 1513 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1514 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1515 | return found; |
| 1516 | } |
| 1517 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1518 | static int __process_pages_contig(struct address_space *mapping, |
| 1519 | struct page *locked_page, |
| 1520 | pgoff_t start_index, pgoff_t end_index, |
| 1521 | unsigned long page_ops, pgoff_t *index_ret); |
| 1522 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1523 | static noinline void __unlock_for_delalloc(struct inode *inode, |
| 1524 | struct page *locked_page, |
| 1525 | u64 start, u64 end) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1526 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1527 | unsigned long index = start >> PAGE_SHIFT; |
| 1528 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1529 | |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1530 | ASSERT(locked_page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1531 | if (index == locked_page->index && end_index == index) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1532 | return; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1533 | |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1534 | __process_pages_contig(inode->i_mapping, locked_page, index, end_index, |
| 1535 | PAGE_UNLOCK, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | static noinline int lock_delalloc_pages(struct inode *inode, |
| 1539 | struct page *locked_page, |
| 1540 | u64 delalloc_start, |
| 1541 | u64 delalloc_end) |
| 1542 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1543 | unsigned long index = delalloc_start >> PAGE_SHIFT; |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1544 | unsigned long index_ret = index; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1545 | unsigned long end_index = delalloc_end >> PAGE_SHIFT; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1546 | int ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1547 | |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1548 | ASSERT(locked_page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1549 | if (index == locked_page->index && index == end_index) |
| 1550 | return 0; |
| 1551 | |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1552 | ret = __process_pages_contig(inode->i_mapping, locked_page, index, |
| 1553 | end_index, PAGE_LOCK, &index_ret); |
| 1554 | if (ret == -EAGAIN) |
| 1555 | __unlock_for_delalloc(inode, locked_page, delalloc_start, |
| 1556 | (u64)index_ret << PAGE_SHIFT); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1557 | return ret; |
| 1558 | } |
| 1559 | |
| 1560 | /* |
| 1561 | * find a contiguous range of bytes in the file marked as delalloc, not |
| 1562 | * more than 'max_bytes'. start and end are used to return the range, |
| 1563 | * |
| 1564 | * 1 is returned if we find something, 0 if nothing was in the tree |
| 1565 | */ |
Josef Bacik | 294e30f | 2013-10-09 12:00:56 -0400 | [diff] [blame] | 1566 | STATIC u64 find_lock_delalloc_range(struct inode *inode, |
| 1567 | struct extent_io_tree *tree, |
| 1568 | struct page *locked_page, u64 *start, |
| 1569 | u64 *end, u64 max_bytes) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1570 | { |
| 1571 | u64 delalloc_start; |
| 1572 | u64 delalloc_end; |
| 1573 | u64 found; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1574 | struct extent_state *cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1575 | int ret; |
| 1576 | int loops = 0; |
| 1577 | |
| 1578 | again: |
| 1579 | /* step one, find a bunch of delalloc bytes starting at start */ |
| 1580 | delalloc_start = *start; |
| 1581 | delalloc_end = 0; |
| 1582 | found = find_delalloc_range(tree, &delalloc_start, &delalloc_end, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1583 | max_bytes, &cached_state); |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1584 | if (!found || delalloc_end <= *start) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1585 | *start = delalloc_start; |
| 1586 | *end = delalloc_end; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1587 | free_extent_state(cached_state); |
Liu Bo | 385fe0b | 2013-10-01 23:49:49 +0800 | [diff] [blame] | 1588 | return 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | /* |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1592 | * start comes from the offset of locked_page. We have to lock |
| 1593 | * pages in order, so we can't process delalloc bytes before |
| 1594 | * locked_page |
| 1595 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1596 | if (delalloc_start < *start) |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1597 | delalloc_start = *start; |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1598 | |
| 1599 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1600 | * make sure to limit the number of pages we try to lock down |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1601 | */ |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 1602 | if (delalloc_end + 1 - delalloc_start > max_bytes) |
| 1603 | delalloc_end = delalloc_start + max_bytes - 1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1604 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1605 | /* step two, lock all the pages after the page that has start */ |
| 1606 | ret = lock_delalloc_pages(inode, locked_page, |
| 1607 | delalloc_start, delalloc_end); |
| 1608 | if (ret == -EAGAIN) { |
| 1609 | /* some of the pages are gone, lets avoid looping by |
| 1610 | * shortening the size of the delalloc range we're searching |
| 1611 | */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1612 | free_extent_state(cached_state); |
Chris Mason | 7d78874 | 2014-05-21 05:49:54 -0700 | [diff] [blame] | 1613 | cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1614 | if (!loops) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1615 | max_bytes = PAGE_SIZE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1616 | loops = 1; |
| 1617 | goto again; |
| 1618 | } else { |
| 1619 | found = 0; |
| 1620 | goto out_failed; |
| 1621 | } |
| 1622 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1623 | BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1624 | |
| 1625 | /* step three, lock the state bits for the whole range */ |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1626 | lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1627 | |
| 1628 | /* then test to make sure it is all still delalloc */ |
| 1629 | ret = test_range_bit(tree, delalloc_start, delalloc_end, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1630 | EXTENT_DELALLOC, 1, cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1631 | if (!ret) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1632 | unlock_extent_cached(tree, delalloc_start, delalloc_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 1633 | &cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1634 | __unlock_for_delalloc(inode, locked_page, |
| 1635 | delalloc_start, delalloc_end); |
| 1636 | cond_resched(); |
| 1637 | goto again; |
| 1638 | } |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1639 | free_extent_state(cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1640 | *start = delalloc_start; |
| 1641 | *end = delalloc_end; |
| 1642 | out_failed: |
| 1643 | return found; |
| 1644 | } |
| 1645 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1646 | static int __process_pages_contig(struct address_space *mapping, |
| 1647 | struct page *locked_page, |
| 1648 | pgoff_t start_index, pgoff_t end_index, |
| 1649 | unsigned long page_ops, pgoff_t *index_ret) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1650 | { |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1651 | unsigned long nr_pages = end_index - start_index + 1; |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1652 | unsigned long pages_locked = 0; |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1653 | pgoff_t index = start_index; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1654 | struct page *pages[16]; |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1655 | unsigned ret; |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1656 | int err = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1657 | int i; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1658 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1659 | if (page_ops & PAGE_LOCK) { |
| 1660 | ASSERT(page_ops == PAGE_LOCK); |
| 1661 | ASSERT(index_ret && *index_ret == start_index); |
| 1662 | } |
| 1663 | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1664 | if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0) |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1665 | mapping_set_error(mapping, -EIO); |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1666 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1667 | while (nr_pages > 0) { |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1668 | ret = find_get_pages_contig(mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1669 | min_t(unsigned long, |
| 1670 | nr_pages, ARRAY_SIZE(pages)), pages); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1671 | if (ret == 0) { |
| 1672 | /* |
| 1673 | * Only if we're going to lock these pages, |
| 1674 | * can we find nothing at @index. |
| 1675 | */ |
| 1676 | ASSERT(page_ops & PAGE_LOCK); |
Liu Bo | 49d4a33 | 2017-03-06 18:20:56 -0800 | [diff] [blame] | 1677 | err = -EAGAIN; |
| 1678 | goto out; |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1679 | } |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1680 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1681 | for (i = 0; i < ret; i++) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1682 | if (page_ops & PAGE_SET_PRIVATE2) |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1683 | SetPagePrivate2(pages[i]); |
| 1684 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1685 | if (pages[i] == locked_page) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1686 | put_page(pages[i]); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1687 | pages_locked++; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1688 | continue; |
| 1689 | } |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1690 | if (page_ops & PAGE_CLEAR_DIRTY) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1691 | clear_page_dirty_for_io(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1692 | if (page_ops & PAGE_SET_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1693 | set_page_writeback(pages[i]); |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1694 | if (page_ops & PAGE_SET_ERROR) |
| 1695 | SetPageError(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1696 | if (page_ops & PAGE_END_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1697 | end_page_writeback(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1698 | if (page_ops & PAGE_UNLOCK) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1699 | unlock_page(pages[i]); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1700 | if (page_ops & PAGE_LOCK) { |
| 1701 | lock_page(pages[i]); |
| 1702 | if (!PageDirty(pages[i]) || |
| 1703 | pages[i]->mapping != mapping) { |
| 1704 | unlock_page(pages[i]); |
| 1705 | put_page(pages[i]); |
| 1706 | err = -EAGAIN; |
| 1707 | goto out; |
| 1708 | } |
| 1709 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1710 | put_page(pages[i]); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1711 | pages_locked++; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1712 | } |
| 1713 | nr_pages -= ret; |
| 1714 | index += ret; |
| 1715 | cond_resched(); |
| 1716 | } |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1717 | out: |
| 1718 | if (err && index_ret) |
| 1719 | *index_ret = start_index + pages_locked - 1; |
| 1720 | return err; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1721 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1722 | |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1723 | void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end, |
| 1724 | u64 delalloc_end, struct page *locked_page, |
| 1725 | unsigned clear_bits, |
| 1726 | unsigned long page_ops) |
| 1727 | { |
| 1728 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 1729 | NULL); |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1730 | |
| 1731 | __process_pages_contig(inode->i_mapping, locked_page, |
| 1732 | start >> PAGE_SHIFT, end >> PAGE_SHIFT, |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1733 | page_ops, NULL); |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1734 | } |
| 1735 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1736 | /* |
| 1737 | * count the number of bytes in the tree that have a given bit(s) |
| 1738 | * set. This can be fairly slow, except for EXTENT_DIRTY which is |
| 1739 | * cached. The total number found is returned. |
| 1740 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1741 | u64 count_range_bits(struct extent_io_tree *tree, |
| 1742 | u64 *start, u64 search_end, u64 max_bytes, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1743 | unsigned bits, int contig) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1744 | { |
| 1745 | struct rb_node *node; |
| 1746 | struct extent_state *state; |
| 1747 | u64 cur_start = *start; |
| 1748 | u64 total_bytes = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1749 | u64 last = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1750 | int found = 0; |
| 1751 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 1752 | if (WARN_ON(search_end <= cur_start)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1753 | return 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1754 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1755 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1756 | if (cur_start == 0 && bits == EXTENT_DIRTY) { |
| 1757 | total_bytes = tree->dirty_bytes; |
| 1758 | goto out; |
| 1759 | } |
| 1760 | /* |
| 1761 | * this search will find all the extents that end after |
| 1762 | * our range starts. |
| 1763 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1764 | node = tree_search(tree, cur_start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1765 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1766 | goto out; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1767 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1768 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1769 | state = rb_entry(node, struct extent_state, rb_node); |
| 1770 | if (state->start > search_end) |
| 1771 | break; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1772 | if (contig && found && state->start > last + 1) |
| 1773 | break; |
| 1774 | if (state->end >= cur_start && (state->state & bits) == bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1775 | total_bytes += min(search_end, state->end) + 1 - |
| 1776 | max(cur_start, state->start); |
| 1777 | if (total_bytes >= max_bytes) |
| 1778 | break; |
| 1779 | if (!found) { |
Josef Bacik | af60bed | 2011-05-04 11:11:17 -0400 | [diff] [blame] | 1780 | *start = max(cur_start, state->start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1781 | found = 1; |
| 1782 | } |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1783 | last = state->end; |
| 1784 | } else if (contig && found) { |
| 1785 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1786 | } |
| 1787 | node = rb_next(node); |
| 1788 | if (!node) |
| 1789 | break; |
| 1790 | } |
| 1791 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1792 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1793 | return total_bytes; |
| 1794 | } |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1795 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1796 | /* |
| 1797 | * set the private field for a given byte offset in the tree. If there isn't |
| 1798 | * an extent_state there already, this does nothing. |
| 1799 | */ |
Arnd Bergmann | f827ba9 | 2016-02-22 22:53:20 +0100 | [diff] [blame] | 1800 | static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start, |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 1801 | struct io_failure_record *failrec) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1802 | { |
| 1803 | struct rb_node *node; |
| 1804 | struct extent_state *state; |
| 1805 | int ret = 0; |
| 1806 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1807 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1808 | /* |
| 1809 | * this search will find all the extents that end after |
| 1810 | * our range starts. |
| 1811 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1812 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1813 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1814 | ret = -ENOENT; |
| 1815 | goto out; |
| 1816 | } |
| 1817 | state = rb_entry(node, struct extent_state, rb_node); |
| 1818 | if (state->start != start) { |
| 1819 | ret = -ENOENT; |
| 1820 | goto out; |
| 1821 | } |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 1822 | state->failrec = failrec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1823 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1824 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1825 | return ret; |
| 1826 | } |
| 1827 | |
Arnd Bergmann | f827ba9 | 2016-02-22 22:53:20 +0100 | [diff] [blame] | 1828 | static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start, |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 1829 | struct io_failure_record **failrec) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1830 | { |
| 1831 | struct rb_node *node; |
| 1832 | struct extent_state *state; |
| 1833 | int ret = 0; |
| 1834 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1835 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1836 | /* |
| 1837 | * this search will find all the extents that end after |
| 1838 | * our range starts. |
| 1839 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1840 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1841 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1842 | ret = -ENOENT; |
| 1843 | goto out; |
| 1844 | } |
| 1845 | state = rb_entry(node, struct extent_state, rb_node); |
| 1846 | if (state->start != start) { |
| 1847 | ret = -ENOENT; |
| 1848 | goto out; |
| 1849 | } |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 1850 | *failrec = state->failrec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1851 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1852 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1853 | return ret; |
| 1854 | } |
| 1855 | |
| 1856 | /* |
| 1857 | * searches a range in the state tree for a given mask. |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1858 | * If 'filled' == 1, this returns 1 only if every extent in the tree |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1859 | * has the bits set. Otherwise, 1 is returned if any bit in the |
| 1860 | * range is found set. |
| 1861 | */ |
| 1862 | int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1863 | unsigned bits, int filled, struct extent_state *cached) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1864 | { |
| 1865 | struct extent_state *state = NULL; |
| 1866 | struct rb_node *node; |
| 1867 | int bitset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1868 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1869 | spin_lock(&tree->lock); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1870 | if (cached && extent_state_in_tree(cached) && cached->start <= start && |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 1871 | cached->end > start) |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1872 | node = &cached->rb_node; |
| 1873 | else |
| 1874 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1875 | while (node && start <= end) { |
| 1876 | state = rb_entry(node, struct extent_state, rb_node); |
| 1877 | |
| 1878 | if (filled && state->start > start) { |
| 1879 | bitset = 0; |
| 1880 | break; |
| 1881 | } |
| 1882 | |
| 1883 | if (state->start > end) |
| 1884 | break; |
| 1885 | |
| 1886 | if (state->state & bits) { |
| 1887 | bitset = 1; |
| 1888 | if (!filled) |
| 1889 | break; |
| 1890 | } else if (filled) { |
| 1891 | bitset = 0; |
| 1892 | break; |
| 1893 | } |
Chris Mason | 46562ce | 2009-09-23 20:23:16 -0400 | [diff] [blame] | 1894 | |
| 1895 | if (state->end == (u64)-1) |
| 1896 | break; |
| 1897 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1898 | start = state->end + 1; |
| 1899 | if (start > end) |
| 1900 | break; |
| 1901 | node = rb_next(node); |
| 1902 | if (!node) { |
| 1903 | if (filled) |
| 1904 | bitset = 0; |
| 1905 | break; |
| 1906 | } |
| 1907 | } |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1908 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1909 | return bitset; |
| 1910 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1911 | |
| 1912 | /* |
| 1913 | * helper function to set a given page up to date if all the |
| 1914 | * extents in the tree for that page are up to date |
| 1915 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1916 | static void check_page_uptodate(struct extent_io_tree *tree, struct page *page) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1917 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 1918 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1919 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1920 | if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1921 | SetPageUptodate(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1922 | } |
| 1923 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 1924 | int free_io_failure(struct extent_io_tree *failure_tree, |
| 1925 | struct extent_io_tree *io_tree, |
| 1926 | struct io_failure_record *rec) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1927 | { |
| 1928 | int ret; |
| 1929 | int err = 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1930 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 1931 | set_state_failrec(failure_tree, rec->start, NULL); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1932 | ret = clear_extent_bits(failure_tree, rec->start, |
| 1933 | rec->start + rec->len - 1, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1934 | EXTENT_LOCKED | EXTENT_DIRTY); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1935 | if (ret) |
| 1936 | err = ret; |
| 1937 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 1938 | ret = clear_extent_bits(io_tree, rec->start, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1939 | rec->start + rec->len - 1, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1940 | EXTENT_DAMAGED); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1941 | if (ret && !err) |
| 1942 | err = ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1943 | |
| 1944 | kfree(rec); |
| 1945 | return err; |
| 1946 | } |
| 1947 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1948 | /* |
| 1949 | * this bypasses the standard btrfs submit functions deliberately, as |
| 1950 | * the standard behavior is to write all copies in a raid setup. here we only |
| 1951 | * want to write the one bad copy. so we do the mapping for ourselves and issue |
| 1952 | * submit_bio directly. |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1953 | * to avoid any synchronization issues, wait for the data after writing, which |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1954 | * actually prevents the read that triggered the error from finishing. |
| 1955 | * currently, there can be no more than two copies of every data bit. thus, |
| 1956 | * exactly one rewrite is required. |
| 1957 | */ |
Josef Bacik | 6ec656b | 2017-05-05 11:57:14 -0400 | [diff] [blame] | 1958 | int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start, |
| 1959 | u64 length, u64 logical, struct page *page, |
| 1960 | unsigned int pg_offset, int mirror_num) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1961 | { |
| 1962 | struct bio *bio; |
| 1963 | struct btrfs_device *dev; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1964 | u64 map_length = 0; |
| 1965 | u64 sector; |
| 1966 | struct btrfs_bio *bbio = NULL; |
| 1967 | int ret; |
| 1968 | |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 1969 | ASSERT(!(fs_info->sb->s_flags & SB_RDONLY)); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1970 | BUG_ON(!mirror_num); |
| 1971 | |
David Sterba | c5e4c3d | 2017-06-12 17:29:41 +0200 | [diff] [blame] | 1972 | bio = btrfs_io_bio_alloc(1); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1973 | bio->bi_iter.bi_size = 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1974 | map_length = length; |
| 1975 | |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 1976 | /* |
| 1977 | * Avoid races with device replace and make sure our bbio has devices |
| 1978 | * associated to its stripes that don't go away while we are doing the |
| 1979 | * read repair operation. |
| 1980 | */ |
| 1981 | btrfs_bio_counter_inc_blocked(fs_info); |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 1982 | if (btrfs_is_parity_mirror(fs_info, logical, length)) { |
Liu Bo | c725328 | 2017-03-29 10:53:58 -0700 | [diff] [blame] | 1983 | /* |
| 1984 | * Note that we don't use BTRFS_MAP_WRITE because it's supposed |
| 1985 | * to update all raid stripes, but here we just want to correct |
| 1986 | * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad |
| 1987 | * stripe's dev and sector. |
| 1988 | */ |
| 1989 | ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical, |
| 1990 | &map_length, &bbio, 0); |
| 1991 | if (ret) { |
| 1992 | btrfs_bio_counter_dec(fs_info); |
| 1993 | bio_put(bio); |
| 1994 | return -EIO; |
| 1995 | } |
| 1996 | ASSERT(bbio->mirror_num == 1); |
| 1997 | } else { |
| 1998 | ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical, |
| 1999 | &map_length, &bbio, mirror_num); |
| 2000 | if (ret) { |
| 2001 | btrfs_bio_counter_dec(fs_info); |
| 2002 | bio_put(bio); |
| 2003 | return -EIO; |
| 2004 | } |
| 2005 | BUG_ON(mirror_num != bbio->mirror_num); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2006 | } |
Liu Bo | c725328 | 2017-03-29 10:53:58 -0700 | [diff] [blame] | 2007 | |
| 2008 | sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2009 | bio->bi_iter.bi_sector = sector; |
Liu Bo | c725328 | 2017-03-29 10:53:58 -0700 | [diff] [blame] | 2010 | dev = bbio->stripes[bbio->mirror_num - 1].dev; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 2011 | btrfs_put_bbio(bbio); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2012 | if (!dev || !dev->bdev || |
| 2013 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) { |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2014 | btrfs_bio_counter_dec(fs_info); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2015 | bio_put(bio); |
| 2016 | return -EIO; |
| 2017 | } |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 2018 | bio_set_dev(bio, dev->bdev); |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 2019 | bio->bi_opf = REQ_OP_WRITE | REQ_SYNC; |
Miao Xie | ffdd201 | 2014-09-12 18:44:00 +0800 | [diff] [blame] | 2020 | bio_add_page(bio, page, length, pg_offset); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2021 | |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 2022 | if (btrfsic_submit_bio_wait(bio)) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2023 | /* try to remap that extent elsewhere? */ |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2024 | btrfs_bio_counter_dec(fs_info); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2025 | bio_put(bio); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2026 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2027 | return -EIO; |
| 2028 | } |
| 2029 | |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 2030 | btrfs_info_rl_in_rcu(fs_info, |
| 2031 | "read error corrected: ino %llu off %llu (dev %s sector %llu)", |
Josef Bacik | 6ec656b | 2017-05-05 11:57:14 -0400 | [diff] [blame] | 2032 | ino, start, |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2033 | rcu_str_deref(dev->name), sector); |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2034 | btrfs_bio_counter_dec(fs_info); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2035 | bio_put(bio); |
| 2036 | return 0; |
| 2037 | } |
| 2038 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2039 | int repair_eb_io_failure(struct btrfs_fs_info *fs_info, |
| 2040 | struct extent_buffer *eb, int mirror_num) |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2041 | { |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2042 | u64 start = eb->start; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 2043 | int i, num_pages = num_extent_pages(eb); |
Chris Mason | d95603b | 2012-04-12 15:55:15 -0400 | [diff] [blame] | 2044 | int ret = 0; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2045 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 2046 | if (sb_rdonly(fs_info->sb)) |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2047 | return -EROFS; |
| 2048 | |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2049 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 2050 | struct page *p = eb->pages[i]; |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2051 | |
Josef Bacik | 6ec656b | 2017-05-05 11:57:14 -0400 | [diff] [blame] | 2052 | ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p, |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2053 | start - page_offset(p), mirror_num); |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2054 | if (ret) |
| 2055 | break; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2056 | start += PAGE_SIZE; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | return ret; |
| 2060 | } |
| 2061 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2062 | /* |
| 2063 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 2064 | * to see if we need to process or clean up an io_failure_record |
| 2065 | */ |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2066 | int clean_io_failure(struct btrfs_fs_info *fs_info, |
| 2067 | struct extent_io_tree *failure_tree, |
| 2068 | struct extent_io_tree *io_tree, u64 start, |
| 2069 | struct page *page, u64 ino, unsigned int pg_offset) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2070 | { |
| 2071 | u64 private; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2072 | struct io_failure_record *failrec; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2073 | struct extent_state *state; |
| 2074 | int num_copies; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2075 | int ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2076 | |
| 2077 | private = 0; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2078 | ret = count_range_bits(failure_tree, &private, (u64)-1, 1, |
| 2079 | EXTENT_DIRTY, 0); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2080 | if (!ret) |
| 2081 | return 0; |
| 2082 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2083 | ret = get_state_failrec(failure_tree, start, &failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2084 | if (ret) |
| 2085 | return 0; |
| 2086 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2087 | BUG_ON(!failrec->this_mirror); |
| 2088 | |
| 2089 | if (failrec->in_validation) { |
| 2090 | /* there was no real error, just free the record */ |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2091 | btrfs_debug(fs_info, |
| 2092 | "clean_io_failure: freeing dummy error at %llu", |
| 2093 | failrec->start); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2094 | goto out; |
| 2095 | } |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 2096 | if (sb_rdonly(fs_info->sb)) |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2097 | goto out; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2098 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2099 | spin_lock(&io_tree->lock); |
| 2100 | state = find_first_extent_bit_state(io_tree, |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2101 | failrec->start, |
| 2102 | EXTENT_LOCKED); |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2103 | spin_unlock(&io_tree->lock); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2104 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2105 | if (state && state->start <= failrec->start && |
| 2106 | state->end >= failrec->start + failrec->len - 1) { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2107 | num_copies = btrfs_num_copies(fs_info, failrec->logical, |
| 2108 | failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2109 | if (num_copies > 1) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2110 | repair_io_failure(fs_info, ino, start, failrec->len, |
| 2111 | failrec->logical, page, pg_offset, |
| 2112 | failrec->failed_mirror); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2113 | } |
| 2114 | } |
| 2115 | |
| 2116 | out: |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2117 | free_io_failure(failure_tree, io_tree, failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2118 | |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2119 | return 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2120 | } |
| 2121 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2122 | /* |
| 2123 | * Can be called when |
| 2124 | * - hold extent lock |
| 2125 | * - under ordered extent |
| 2126 | * - the inode is freeing |
| 2127 | */ |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 2128 | void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end) |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2129 | { |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 2130 | struct extent_io_tree *failure_tree = &inode->io_failure_tree; |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2131 | struct io_failure_record *failrec; |
| 2132 | struct extent_state *state, *next; |
| 2133 | |
| 2134 | if (RB_EMPTY_ROOT(&failure_tree->state)) |
| 2135 | return; |
| 2136 | |
| 2137 | spin_lock(&failure_tree->lock); |
| 2138 | state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY); |
| 2139 | while (state) { |
| 2140 | if (state->start > end) |
| 2141 | break; |
| 2142 | |
| 2143 | ASSERT(state->end <= end); |
| 2144 | |
| 2145 | next = next_state(state); |
| 2146 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2147 | failrec = state->failrec; |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2148 | free_extent_state(state); |
| 2149 | kfree(failrec); |
| 2150 | |
| 2151 | state = next; |
| 2152 | } |
| 2153 | spin_unlock(&failure_tree->lock); |
| 2154 | } |
| 2155 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2156 | int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end, |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2157 | struct io_failure_record **failrec_ret) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2158 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2159 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2160 | struct io_failure_record *failrec; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2161 | struct extent_map *em; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2162 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 2163 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 2164 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2165 | int ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2166 | u64 logical; |
| 2167 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2168 | ret = get_state_failrec(failure_tree, start, &failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2169 | if (ret) { |
| 2170 | failrec = kzalloc(sizeof(*failrec), GFP_NOFS); |
| 2171 | if (!failrec) |
| 2172 | return -ENOMEM; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2173 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2174 | failrec->start = start; |
| 2175 | failrec->len = end - start + 1; |
| 2176 | failrec->this_mirror = 0; |
| 2177 | failrec->bio_flags = 0; |
| 2178 | failrec->in_validation = 0; |
| 2179 | |
| 2180 | read_lock(&em_tree->lock); |
| 2181 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 2182 | if (!em) { |
| 2183 | read_unlock(&em_tree->lock); |
| 2184 | kfree(failrec); |
| 2185 | return -EIO; |
| 2186 | } |
| 2187 | |
Filipe David Borba Manana | 68ba990 | 2013-11-25 03:22:07 +0000 | [diff] [blame] | 2188 | if (em->start > start || em->start + em->len <= start) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2189 | free_extent_map(em); |
| 2190 | em = NULL; |
| 2191 | } |
| 2192 | read_unlock(&em_tree->lock); |
Tsutomu Itoh | 7a2d6a6 | 2012-10-01 03:07:15 -0600 | [diff] [blame] | 2193 | if (!em) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2194 | kfree(failrec); |
| 2195 | return -EIO; |
| 2196 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2197 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2198 | logical = start - em->start; |
| 2199 | logical = em->block_start + logical; |
| 2200 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 2201 | logical = em->block_start; |
| 2202 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
| 2203 | extent_set_compress_type(&failrec->bio_flags, |
| 2204 | em->compress_type); |
| 2205 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2206 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2207 | btrfs_debug(fs_info, |
| 2208 | "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu", |
| 2209 | logical, start, failrec->len); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2210 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2211 | failrec->logical = logical; |
| 2212 | free_extent_map(em); |
| 2213 | |
| 2214 | /* set the bits in the private failure tree */ |
| 2215 | ret = set_extent_bits(failure_tree, start, end, |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2216 | EXTENT_LOCKED | EXTENT_DIRTY); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2217 | if (ret >= 0) |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2218 | ret = set_state_failrec(failure_tree, start, failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2219 | /* set the bits in the inode's tree */ |
| 2220 | if (ret >= 0) |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2221 | ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2222 | if (ret < 0) { |
| 2223 | kfree(failrec); |
| 2224 | return ret; |
| 2225 | } |
| 2226 | } else { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2227 | btrfs_debug(fs_info, |
| 2228 | "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d", |
| 2229 | failrec->logical, failrec->start, failrec->len, |
| 2230 | failrec->in_validation); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2231 | /* |
| 2232 | * when data can be on disk more than twice, add to failrec here |
| 2233 | * (e.g. with a list for failed_mirror) to make |
| 2234 | * clean_io_failure() clean all those errors at once. |
| 2235 | */ |
| 2236 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2237 | |
| 2238 | *failrec_ret = failrec; |
| 2239 | |
| 2240 | return 0; |
| 2241 | } |
| 2242 | |
Ming Lei | a0b60d7 | 2017-12-18 20:22:11 +0800 | [diff] [blame] | 2243 | bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages, |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2244 | struct io_failure_record *failrec, int failed_mirror) |
| 2245 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2246 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2247 | int num_copies; |
| 2248 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2249 | num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2250 | if (num_copies == 1) { |
| 2251 | /* |
| 2252 | * we only have a single copy of the data, so don't bother with |
| 2253 | * all the retry and error correction code that follows. no |
| 2254 | * matter what the error is, it is very likely to persist. |
| 2255 | */ |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2256 | btrfs_debug(fs_info, |
| 2257 | "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", |
| 2258 | num_copies, failrec->this_mirror, failed_mirror); |
Liu Bo | c3cfb65 | 2017-07-13 15:00:50 -0700 | [diff] [blame] | 2259 | return false; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2260 | } |
| 2261 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2262 | /* |
| 2263 | * there are two premises: |
| 2264 | * a) deliver good data to the caller |
| 2265 | * b) correct the bad sectors on disk |
| 2266 | */ |
Ming Lei | a0b60d7 | 2017-12-18 20:22:11 +0800 | [diff] [blame] | 2267 | if (failed_bio_pages > 1) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2268 | /* |
| 2269 | * to fulfill b), we need to know the exact failing sectors, as |
| 2270 | * we don't want to rewrite any more than the failed ones. thus, |
| 2271 | * we need separate read requests for the failed bio |
| 2272 | * |
| 2273 | * if the following BUG_ON triggers, our validation request got |
| 2274 | * merged. we need separate requests for our algorithm to work. |
| 2275 | */ |
| 2276 | BUG_ON(failrec->in_validation); |
| 2277 | failrec->in_validation = 1; |
| 2278 | failrec->this_mirror = failed_mirror; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2279 | } else { |
| 2280 | /* |
| 2281 | * we're ready to fulfill a) and b) alongside. get a good copy |
| 2282 | * of the failed sector and if we succeed, we have setup |
| 2283 | * everything for repair_io_failure to do the rest for us. |
| 2284 | */ |
| 2285 | if (failrec->in_validation) { |
| 2286 | BUG_ON(failrec->this_mirror != failed_mirror); |
| 2287 | failrec->in_validation = 0; |
| 2288 | failrec->this_mirror = 0; |
| 2289 | } |
| 2290 | failrec->failed_mirror = failed_mirror; |
| 2291 | failrec->this_mirror++; |
| 2292 | if (failrec->this_mirror == failed_mirror) |
| 2293 | failrec->this_mirror++; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2294 | } |
| 2295 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2296 | if (failrec->this_mirror > num_copies) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2297 | btrfs_debug(fs_info, |
| 2298 | "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", |
| 2299 | num_copies, failrec->this_mirror, failed_mirror); |
Liu Bo | c3cfb65 | 2017-07-13 15:00:50 -0700 | [diff] [blame] | 2300 | return false; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2301 | } |
| 2302 | |
Liu Bo | c3cfb65 | 2017-07-13 15:00:50 -0700 | [diff] [blame] | 2303 | return true; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | |
| 2307 | struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio, |
| 2308 | struct io_failure_record *failrec, |
| 2309 | struct page *page, int pg_offset, int icsum, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2310 | bio_end_io_t *endio_func, void *data) |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2311 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2312 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2313 | struct bio *bio; |
| 2314 | struct btrfs_io_bio *btrfs_failed_bio; |
| 2315 | struct btrfs_io_bio *btrfs_bio; |
| 2316 | |
David Sterba | c5e4c3d | 2017-06-12 17:29:41 +0200 | [diff] [blame] | 2317 | bio = btrfs_io_bio_alloc(1); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2318 | bio->bi_end_io = endio_func; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2319 | bio->bi_iter.bi_sector = failrec->logical >> 9; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 2320 | bio_set_dev(bio, fs_info->fs_devices->latest_bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2321 | bio->bi_iter.bi_size = 0; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2322 | bio->bi_private = data; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2323 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2324 | btrfs_failed_bio = btrfs_io_bio(failed_bio); |
| 2325 | if (btrfs_failed_bio->csum) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2326 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
| 2327 | |
| 2328 | btrfs_bio = btrfs_io_bio(bio); |
| 2329 | btrfs_bio->csum = btrfs_bio->csum_inline; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2330 | icsum *= csum_size; |
| 2331 | memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum, |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2332 | csum_size); |
| 2333 | } |
| 2334 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2335 | bio_add_page(bio, page, failrec->len, pg_offset); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2336 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2337 | return bio; |
| 2338 | } |
| 2339 | |
| 2340 | /* |
| 2341 | * this is a generic handler for readpage errors (default |
| 2342 | * readpage_io_failed_hook). if other copies exist, read those and write back |
| 2343 | * good data to the failed position. does not investigate in remapping the |
| 2344 | * failed extent elsewhere, hoping the device will be smart enough to do this as |
| 2345 | * needed |
| 2346 | */ |
| 2347 | |
| 2348 | static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset, |
| 2349 | struct page *page, u64 start, u64 end, |
| 2350 | int failed_mirror) |
| 2351 | { |
| 2352 | struct io_failure_record *failrec; |
| 2353 | struct inode *inode = page->mapping->host; |
| 2354 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2355 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2356 | struct bio *bio; |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 2357 | int read_mode = 0; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2358 | blk_status_t status; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2359 | int ret; |
Ming Lei | a0b60d7 | 2017-12-18 20:22:11 +0800 | [diff] [blame] | 2360 | unsigned failed_bio_pages = bio_pages_all(failed_bio); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2361 | |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2362 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2363 | |
| 2364 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 2365 | if (ret) |
| 2366 | return ret; |
| 2367 | |
Ming Lei | a0b60d7 | 2017-12-18 20:22:11 +0800 | [diff] [blame] | 2368 | if (!btrfs_check_repairable(inode, failed_bio_pages, failrec, |
Liu Bo | c3cfb65 | 2017-07-13 15:00:50 -0700 | [diff] [blame] | 2369 | failed_mirror)) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2370 | free_io_failure(failure_tree, tree, failrec); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2371 | return -EIO; |
| 2372 | } |
| 2373 | |
Ming Lei | a0b60d7 | 2017-12-18 20:22:11 +0800 | [diff] [blame] | 2374 | if (failed_bio_pages > 1) |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 2375 | read_mode |= REQ_FAILFAST_DEV; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2376 | |
| 2377 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
| 2378 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
| 2379 | start - page_offset(page), |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2380 | (int)phy_offset, failed_bio->bi_end_io, |
| 2381 | NULL); |
David Sterba | ebcc326 | 2018-06-29 10:56:53 +0200 | [diff] [blame] | 2382 | bio->bi_opf = REQ_OP_READ | read_mode; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2383 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2384 | btrfs_debug(btrfs_sb(inode->i_sb), |
| 2385 | "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d", |
| 2386 | read_mode, failrec->this_mirror, failrec->in_validation); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2387 | |
Linus Torvalds | 8c27cb3 | 2017-07-05 16:41:23 -0700 | [diff] [blame] | 2388 | status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror, |
Tsutomu Itoh | 013bd4c | 2012-02-16 10:11:40 +0900 | [diff] [blame] | 2389 | failrec->bio_flags, 0); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2390 | if (status) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2391 | free_io_failure(failure_tree, tree, failrec); |
Miao Xie | 6c387ab | 2014-09-12 18:43:57 +0800 | [diff] [blame] | 2392 | bio_put(bio); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2393 | ret = blk_status_to_errno(status); |
Miao Xie | 6c387ab | 2014-09-12 18:43:57 +0800 | [diff] [blame] | 2394 | } |
| 2395 | |
Tsutomu Itoh | 013bd4c | 2012-02-16 10:11:40 +0900 | [diff] [blame] | 2396 | return ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2397 | } |
| 2398 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2399 | /* lots and lots of room for performance fixes in the end_bio funcs */ |
| 2400 | |
David Sterba | b5227c0 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 2401 | void end_extent_writepage(struct page *page, int err, u64 start, u64 end) |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2402 | { |
| 2403 | int uptodate = (err == 0); |
| 2404 | struct extent_io_tree *tree; |
Eric Sandeen | 3e2426b | 2014-06-12 00:39:58 -0500 | [diff] [blame] | 2405 | int ret = 0; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2406 | |
| 2407 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 2408 | |
David Sterba | c3988d6 | 2017-02-17 15:18:32 +0100 | [diff] [blame] | 2409 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 2410 | tree->ops->writepage_end_io_hook(page, start, end, NULL, |
| 2411 | uptodate); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2412 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2413 | if (!uptodate) { |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2414 | ClearPageUptodate(page); |
| 2415 | SetPageError(page); |
Colin Ian King | bff5baf | 2017-05-09 18:14:01 +0100 | [diff] [blame] | 2416 | ret = err < 0 ? err : -EIO; |
Liu Bo | 5dca6ee | 2014-05-12 12:47:36 +0800 | [diff] [blame] | 2417 | mapping_set_error(page->mapping, ret); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2418 | } |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2419 | } |
| 2420 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2421 | /* |
| 2422 | * after a writepage IO is done, we need to: |
| 2423 | * clear the uptodate bits on error |
| 2424 | * clear the writeback bits in the extent tree for this IO |
| 2425 | * end_page_writeback if the page has no more pending IO |
| 2426 | * |
| 2427 | * Scheduling is not allowed, so the extent state tree is expected |
| 2428 | * to have one and only one object corresponding to this IO. |
| 2429 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2430 | static void end_bio_extent_writepage(struct bio *bio) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2431 | { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2432 | int error = blk_status_to_errno(bio->bi_status); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2433 | struct bio_vec *bvec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2434 | u64 start; |
| 2435 | u64 end; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2436 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2437 | |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 2438 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2439 | bio_for_each_segment_all(bvec, bio, i) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2440 | struct page *page = bvec->bv_page; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2441 | struct inode *inode = page->mapping->host; |
| 2442 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2443 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2444 | /* We always issue full-page reads, but if some block |
| 2445 | * in a page fails to read, blk_update_request() will |
| 2446 | * advance bv_offset and adjust bv_len to compensate. |
| 2447 | * Print a warning for nonzero offsets, and an error |
| 2448 | * if they don't add up to a full page. */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2449 | if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) { |
| 2450 | if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2451 | btrfs_err(fs_info, |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2452 | "partial page write in btrfs with offset %u and length %u", |
| 2453 | bvec->bv_offset, bvec->bv_len); |
| 2454 | else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2455 | btrfs_info(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2456 | "incomplete page write in btrfs with offset %u and length %u", |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2457 | bvec->bv_offset, bvec->bv_len); |
| 2458 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2459 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2460 | start = page_offset(page); |
| 2461 | end = start + bvec->bv_offset + bvec->bv_len - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2462 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2463 | end_extent_writepage(page, error, start, end); |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2464 | end_page_writeback(page); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2465 | } |
Chris Mason | 2b1f55b | 2008-09-24 11:48:04 -0400 | [diff] [blame] | 2466 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2467 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2468 | } |
| 2469 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2470 | static void |
| 2471 | endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len, |
| 2472 | int uptodate) |
| 2473 | { |
| 2474 | struct extent_state *cached = NULL; |
| 2475 | u64 end = start + len - 1; |
| 2476 | |
| 2477 | if (uptodate && tree->track_uptodate) |
| 2478 | set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC); |
David Sterba | d810a4b | 2017-12-07 18:52:54 +0100 | [diff] [blame] | 2479 | unlock_extent_cached_atomic(tree, start, end, &cached); |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2480 | } |
| 2481 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2482 | /* |
| 2483 | * after a readpage IO is done, we need to: |
| 2484 | * clear the uptodate bits on error |
| 2485 | * set the uptodate bits if things worked |
| 2486 | * set the page up to date if all extents in the tree are uptodate |
| 2487 | * clear the lock bit in the extent tree |
| 2488 | * unlock the page if there are no other extents locked for it |
| 2489 | * |
| 2490 | * Scheduling is not allowed, so the extent state tree is expected |
| 2491 | * to have one and only one object corresponding to this IO. |
| 2492 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2493 | static void end_bio_extent_readpage(struct bio *bio) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2494 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2495 | struct bio_vec *bvec; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2496 | int uptodate = !bio->bi_status; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2497 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2498 | struct extent_io_tree *tree, *failure_tree; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2499 | u64 offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2500 | u64 start; |
| 2501 | u64 end; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2502 | u64 len; |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2503 | u64 extent_start = 0; |
| 2504 | u64 extent_len = 0; |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2505 | int mirror; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2506 | int ret; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2507 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2508 | |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 2509 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2510 | bio_for_each_segment_all(bvec, bio, i) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2511 | struct page *page = bvec->bv_page; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2512 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2513 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2514 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2515 | btrfs_debug(fs_info, |
| 2516 | "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u", |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2517 | (u64)bio->bi_iter.bi_sector, bio->bi_status, |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2518 | io_bio->mirror_num); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2519 | tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2520 | failure_tree = &BTRFS_I(inode)->io_failure_tree; |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2521 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2522 | /* We always issue full-page reads, but if some block |
| 2523 | * in a page fails to read, blk_update_request() will |
| 2524 | * advance bv_offset and adjust bv_len to compensate. |
| 2525 | * Print a warning for nonzero offsets, and an error |
| 2526 | * if they don't add up to a full page. */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2527 | if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) { |
| 2528 | if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2529 | btrfs_err(fs_info, |
| 2530 | "partial page read in btrfs with offset %u and length %u", |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2531 | bvec->bv_offset, bvec->bv_len); |
| 2532 | else |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2533 | btrfs_info(fs_info, |
| 2534 | "incomplete page read in btrfs with offset %u and length %u", |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2535 | bvec->bv_offset, bvec->bv_len); |
| 2536 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2537 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2538 | start = page_offset(page); |
| 2539 | end = start + bvec->bv_offset + bvec->bv_len - 1; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2540 | len = bvec->bv_len; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2541 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2542 | mirror = io_bio->mirror_num; |
David Sterba | 20c9801 | 2017-02-17 15:59:35 +0100 | [diff] [blame] | 2543 | if (likely(uptodate && tree->ops)) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2544 | ret = tree->ops->readpage_end_io_hook(io_bio, offset, |
| 2545 | page, start, end, |
| 2546 | mirror); |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2547 | if (ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2548 | uptodate = 0; |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2549 | else |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2550 | clean_io_failure(BTRFS_I(inode)->root->fs_info, |
| 2551 | failure_tree, tree, start, |
| 2552 | page, |
| 2553 | btrfs_ino(BTRFS_I(inode)), 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2554 | } |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2555 | |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2556 | if (likely(uptodate)) |
| 2557 | goto readpage_ok; |
| 2558 | |
David Sterba | 20a7db8 | 2017-02-17 16:24:29 +0100 | [diff] [blame] | 2559 | if (tree->ops) { |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2560 | ret = tree->ops->readpage_io_failed_hook(page, mirror); |
Liu Bo | 9d0d1c8 | 2017-03-24 15:04:50 -0700 | [diff] [blame] | 2561 | if (ret == -EAGAIN) { |
| 2562 | /* |
| 2563 | * Data inode's readpage_io_failed_hook() always |
| 2564 | * returns -EAGAIN. |
| 2565 | * |
| 2566 | * The generic bio_readpage_error handles errors |
| 2567 | * the following way: If possible, new read |
| 2568 | * requests are created and submitted and will |
| 2569 | * end up in end_bio_extent_readpage as well (if |
| 2570 | * we're lucky, not in the !uptodate case). In |
| 2571 | * that case it returns 0 and we just go on with |
| 2572 | * the next page in our bio. If it can't handle |
| 2573 | * the error it will return -EIO and we remain |
| 2574 | * responsible for that page. |
| 2575 | */ |
| 2576 | ret = bio_readpage_error(bio, offset, page, |
| 2577 | start, end, mirror); |
| 2578 | if (ret == 0) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2579 | uptodate = !bio->bi_status; |
Liu Bo | 9d0d1c8 | 2017-03-24 15:04:50 -0700 | [diff] [blame] | 2580 | offset += len; |
| 2581 | continue; |
| 2582 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2583 | } |
Liu Bo | 9d0d1c8 | 2017-03-24 15:04:50 -0700 | [diff] [blame] | 2584 | |
| 2585 | /* |
| 2586 | * metadata's readpage_io_failed_hook() always returns |
| 2587 | * -EIO and fixes nothing. -EIO is also returned if |
| 2588 | * data inode error could not be fixed. |
| 2589 | */ |
| 2590 | ASSERT(ret == -EIO); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2591 | } |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2592 | readpage_ok: |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2593 | if (likely(uptodate)) { |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2594 | loff_t i_size = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2595 | pgoff_t end_index = i_size >> PAGE_SHIFT; |
Liu Bo | a583c02 | 2014-08-19 23:32:22 +0800 | [diff] [blame] | 2596 | unsigned off; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2597 | |
| 2598 | /* Zero out the end if this page straddles i_size */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2599 | off = i_size & (PAGE_SIZE-1); |
Liu Bo | a583c02 | 2014-08-19 23:32:22 +0800 | [diff] [blame] | 2600 | if (page->index == end_index && off) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2601 | zero_user_segment(page, off, PAGE_SIZE); |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2602 | SetPageUptodate(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2603 | } else { |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2604 | ClearPageUptodate(page); |
| 2605 | SetPageError(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2606 | } |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2607 | unlock_page(page); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2608 | offset += len; |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2609 | |
| 2610 | if (unlikely(!uptodate)) { |
| 2611 | if (extent_len) { |
| 2612 | endio_readpage_release_extent(tree, |
| 2613 | extent_start, |
| 2614 | extent_len, 1); |
| 2615 | extent_start = 0; |
| 2616 | extent_len = 0; |
| 2617 | } |
| 2618 | endio_readpage_release_extent(tree, start, |
| 2619 | end - start + 1, 0); |
| 2620 | } else if (!extent_len) { |
| 2621 | extent_start = start; |
| 2622 | extent_len = end + 1 - start; |
| 2623 | } else if (extent_start + extent_len == start) { |
| 2624 | extent_len += end + 1 - start; |
| 2625 | } else { |
| 2626 | endio_readpage_release_extent(tree, extent_start, |
| 2627 | extent_len, uptodate); |
| 2628 | extent_start = start; |
| 2629 | extent_len = end + 1 - start; |
| 2630 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2631 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2632 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2633 | if (extent_len) |
| 2634 | endio_readpage_release_extent(tree, extent_start, extent_len, |
| 2635 | uptodate); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2636 | if (io_bio->end_io) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2637 | io_bio->end_io(io_bio, blk_status_to_errno(bio->bi_status)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2638 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2639 | } |
| 2640 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2641 | /* |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2642 | * Initialize the members up to but not including 'bio'. Use after allocating a |
| 2643 | * new bio by bio_alloc_bioset as it does not initialize the bytes outside of |
| 2644 | * 'bio' because use of __GFP_ZERO is not supported. |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2645 | */ |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2646 | static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2647 | { |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2648 | memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio)); |
| 2649 | } |
| 2650 | |
| 2651 | /* |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2652 | * The following helpers allocate a bio. As it's backed by a bioset, it'll |
| 2653 | * never fail. We're returning a bio right now but you can call btrfs_io_bio |
| 2654 | * for the appropriate container_of magic |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2655 | */ |
David Sterba | c821e7f3 | 2017-06-02 18:35:36 +0200 | [diff] [blame] | 2656 | struct bio *btrfs_bio_alloc(struct block_device *bdev, u64 first_byte) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2657 | { |
| 2658 | struct bio *bio; |
| 2659 | |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 2660 | bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 2661 | bio_set_dev(bio, bdev); |
David Sterba | c821e7f3 | 2017-06-02 18:35:36 +0200 | [diff] [blame] | 2662 | bio->bi_iter.bi_sector = first_byte >> 9; |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2663 | btrfs_io_bio_init(btrfs_io_bio(bio)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2664 | return bio; |
| 2665 | } |
| 2666 | |
David Sterba | 8b6c1d5 | 2017-06-02 17:48:13 +0200 | [diff] [blame] | 2667 | struct bio *btrfs_bio_clone(struct bio *bio) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2668 | { |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 2669 | struct btrfs_io_bio *btrfs_bio; |
| 2670 | struct bio *new; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2671 | |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2672 | /* Bio allocation backed by a bioset does not fail */ |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 2673 | new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset); |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2674 | btrfs_bio = btrfs_io_bio(new); |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2675 | btrfs_io_bio_init(btrfs_bio); |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2676 | btrfs_bio->iter = bio->bi_iter; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 2677 | return new; |
| 2678 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2679 | |
David Sterba | c5e4c3d | 2017-06-12 17:29:41 +0200 | [diff] [blame] | 2680 | struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2681 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2682 | struct bio *bio; |
| 2683 | |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2684 | /* Bio allocation backed by a bioset does not fail */ |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 2685 | bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset); |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2686 | btrfs_io_bio_init(btrfs_io_bio(bio)); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2687 | return bio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2688 | } |
| 2689 | |
Liu Bo | e477094 | 2017-05-16 10:57:14 -0700 | [diff] [blame] | 2690 | struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size) |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 2691 | { |
| 2692 | struct bio *bio; |
| 2693 | struct btrfs_io_bio *btrfs_bio; |
| 2694 | |
| 2695 | /* this will never fail when it's backed by a bioset */ |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 2696 | bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset); |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 2697 | ASSERT(bio); |
| 2698 | |
| 2699 | btrfs_bio = btrfs_io_bio(bio); |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2700 | btrfs_io_bio_init(btrfs_bio); |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 2701 | |
| 2702 | bio_trim(bio, offset >> 9, size >> 9); |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 2703 | btrfs_bio->iter = bio->bi_iter; |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 2704 | return bio; |
| 2705 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2706 | |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2707 | static int __must_check submit_one_bio(struct bio *bio, int mirror_num, |
| 2708 | unsigned long bio_flags) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2709 | { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2710 | blk_status_t ret = 0; |
Ming Lei | c45a8f2 | 2017-12-18 20:22:05 +0800 | [diff] [blame] | 2711 | struct bio_vec *bvec = bio_last_bvec_all(bio); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2712 | struct page *page = bvec->bv_page; |
| 2713 | struct extent_io_tree *tree = bio->bi_private; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2714 | u64 start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2715 | |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2716 | start = page_offset(page) + bvec->bv_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2717 | |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2718 | bio->bi_private = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2719 | |
David Sterba | 20c9801 | 2017-02-17 15:59:35 +0100 | [diff] [blame] | 2720 | if (tree->ops) |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 2721 | ret = tree->ops->submit_bio_hook(tree->private_data, bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 2722 | mirror_num, bio_flags, start); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2723 | else |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 2724 | btrfsic_submit_bio(bio); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2725 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2726 | return blk_status_to_errno(ret); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2727 | } |
| 2728 | |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 2729 | /* |
| 2730 | * @opf: bio REQ_OP_* and REQ_* flags as one value |
David Sterba | b8b3d62 | 2017-06-12 19:50:41 +0200 | [diff] [blame] | 2731 | * @tree: tree so we can call our merge_bio hook |
| 2732 | * @wbc: optional writeback control for io accounting |
| 2733 | * @page: page to add to the bio |
| 2734 | * @pg_offset: offset of the new bio or to check whether we are adding |
| 2735 | * a contiguous page to the previous one |
| 2736 | * @size: portion of page that we want to write |
| 2737 | * @offset: starting offset in the page |
| 2738 | * @bdev: attach newly created bios to this bdev |
David Sterba | 5c2b1fd | 2017-06-06 19:22:55 +0200 | [diff] [blame] | 2739 | * @bio_ret: must be valid pointer, newly allocated bio will be stored there |
David Sterba | b8b3d62 | 2017-06-12 19:50:41 +0200 | [diff] [blame] | 2740 | * @end_io_func: end_io callback for new bio |
| 2741 | * @mirror_num: desired mirror to read/write |
| 2742 | * @prev_bio_flags: flags of previous bio to see if we can merge the current one |
| 2743 | * @bio_flags: flags of the current bio to see if we can merge them |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 2744 | */ |
| 2745 | static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree, |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2746 | struct writeback_control *wbc, |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 2747 | struct page *page, u64 offset, |
David Sterba | 6c5a4e2 | 2017-10-04 17:10:34 +0200 | [diff] [blame] | 2748 | size_t size, unsigned long pg_offset, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2749 | struct block_device *bdev, |
| 2750 | struct bio **bio_ret, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2751 | bio_end_io_t end_io_func, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2752 | int mirror_num, |
| 2753 | unsigned long prev_bio_flags, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2754 | unsigned long bio_flags, |
| 2755 | bool force_bio_submit) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2756 | { |
| 2757 | int ret = 0; |
| 2758 | struct bio *bio; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2759 | size_t page_size = min_t(size_t, size, PAGE_SIZE); |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 2760 | sector_t sector = offset >> 9; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2761 | |
David Sterba | 5c2b1fd | 2017-06-06 19:22:55 +0200 | [diff] [blame] | 2762 | ASSERT(bio_ret); |
| 2763 | |
| 2764 | if (*bio_ret) { |
David Sterba | 0c8508a | 2017-06-12 20:00:43 +0200 | [diff] [blame] | 2765 | bool contig; |
| 2766 | bool can_merge = true; |
| 2767 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2768 | bio = *bio_ret; |
David Sterba | 0c8508a | 2017-06-12 20:00:43 +0200 | [diff] [blame] | 2769 | if (prev_bio_flags & EXTENT_BIO_COMPRESSED) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2770 | contig = bio->bi_iter.bi_sector == sector; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2771 | else |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 2772 | contig = bio_end_sector(bio) == sector; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2773 | |
David Sterba | 00032d3 | 2018-07-18 19:28:09 +0200 | [diff] [blame] | 2774 | if (tree->ops && btrfs_merge_bio_hook(page, offset, page_size, |
| 2775 | bio, bio_flags)) |
David Sterba | 0c8508a | 2017-06-12 20:00:43 +0200 | [diff] [blame] | 2776 | can_merge = false; |
| 2777 | |
| 2778 | if (prev_bio_flags != bio_flags || !contig || !can_merge || |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2779 | force_bio_submit || |
David Sterba | 6c5a4e2 | 2017-10-04 17:10:34 +0200 | [diff] [blame] | 2780 | bio_add_page(bio, page, page_size, pg_offset) < page_size) { |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2781 | ret = submit_one_bio(bio, mirror_num, prev_bio_flags); |
Naohiro Aota | 289454a | 2015-01-06 01:01:03 +0900 | [diff] [blame] | 2782 | if (ret < 0) { |
| 2783 | *bio_ret = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2784 | return ret; |
Naohiro Aota | 289454a | 2015-01-06 01:01:03 +0900 | [diff] [blame] | 2785 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2786 | bio = NULL; |
| 2787 | } else { |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2788 | if (wbc) |
| 2789 | wbc_account_io(wbc, page, page_size); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2790 | return 0; |
| 2791 | } |
| 2792 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2793 | |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 2794 | bio = btrfs_bio_alloc(bdev, offset); |
David Sterba | 6c5a4e2 | 2017-10-04 17:10:34 +0200 | [diff] [blame] | 2795 | bio_add_page(bio, page, page_size, pg_offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2796 | bio->bi_end_io = end_io_func; |
| 2797 | bio->bi_private = tree; |
Jens Axboe | e6959b9 | 2017-06-27 11:51:28 -0600 | [diff] [blame] | 2798 | bio->bi_write_hint = page->mapping->host->i_write_hint; |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 2799 | bio->bi_opf = opf; |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2800 | if (wbc) { |
| 2801 | wbc_init_bio(wbc, bio); |
| 2802 | wbc_account_io(wbc, page, page_size); |
| 2803 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2804 | |
David Sterba | 5c2b1fd | 2017-06-06 19:22:55 +0200 | [diff] [blame] | 2805 | *bio_ret = bio; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2806 | |
| 2807 | return ret; |
| 2808 | } |
| 2809 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 2810 | static void attach_extent_buffer_page(struct extent_buffer *eb, |
| 2811 | struct page *page) |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 2812 | { |
| 2813 | if (!PagePrivate(page)) { |
| 2814 | SetPagePrivate(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2815 | get_page(page); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 2816 | set_page_private(page, (unsigned long)eb); |
| 2817 | } else { |
| 2818 | WARN_ON(page->private != (unsigned long)eb); |
| 2819 | } |
| 2820 | } |
| 2821 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2822 | void set_page_extent_mapped(struct page *page) |
| 2823 | { |
| 2824 | if (!PagePrivate(page)) { |
| 2825 | SetPagePrivate(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2826 | get_page(page); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2827 | set_page_private(page, EXTENT_PAGE_PRIVATE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2828 | } |
| 2829 | } |
| 2830 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2831 | static struct extent_map * |
| 2832 | __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset, |
| 2833 | u64 start, u64 len, get_extent_t *get_extent, |
| 2834 | struct extent_map **em_cached) |
| 2835 | { |
| 2836 | struct extent_map *em; |
| 2837 | |
| 2838 | if (em_cached && *em_cached) { |
| 2839 | em = *em_cached; |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 2840 | if (extent_map_in_tree(em) && start >= em->start && |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2841 | start < extent_map_end(em)) { |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 2842 | refcount_inc(&em->refs); |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2843 | return em; |
| 2844 | } |
| 2845 | |
| 2846 | free_extent_map(em); |
| 2847 | *em_cached = NULL; |
| 2848 | } |
| 2849 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 2850 | em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0); |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2851 | if (em_cached && !IS_ERR_OR_NULL(em)) { |
| 2852 | BUG_ON(*em_cached); |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 2853 | refcount_inc(&em->refs); |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2854 | *em_cached = em; |
| 2855 | } |
| 2856 | return em; |
| 2857 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2858 | /* |
| 2859 | * basic readpage implementation. Locked extent state structs are inserted |
| 2860 | * into the tree that are removed when the IO is done (by the end_io |
| 2861 | * handlers) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2862 | * XXX JDM: This needs looking at to ensure proper page locking |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 2863 | * return 0 on success, otherwise return error |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2864 | */ |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2865 | static int __do_readpage(struct extent_io_tree *tree, |
| 2866 | struct page *page, |
| 2867 | get_extent_t *get_extent, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2868 | struct extent_map **em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2869 | struct bio **bio, int mirror_num, |
David Sterba | f1c77c5 | 2017-06-06 19:03:49 +0200 | [diff] [blame] | 2870 | unsigned long *bio_flags, unsigned int read_flags, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2871 | u64 *prev_em_start) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2872 | { |
| 2873 | struct inode *inode = page->mapping->host; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2874 | u64 start = page_offset(page); |
David Sterba | 8eec829 | 2017-06-06 19:50:13 +0200 | [diff] [blame] | 2875 | const u64 end = start + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2876 | u64 cur = start; |
| 2877 | u64 extent_offset; |
| 2878 | u64 last_byte = i_size_read(inode); |
| 2879 | u64 block_start; |
| 2880 | u64 cur_end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2881 | struct extent_map *em; |
| 2882 | struct block_device *bdev; |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 2883 | int ret = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2884 | int nr = 0; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2885 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2886 | size_t iosize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2887 | size_t disk_io_size; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2888 | size_t blocksize = inode->i_sb->s_blocksize; |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 2889 | unsigned long this_bio_flag = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2890 | |
| 2891 | set_page_extent_mapped(page); |
| 2892 | |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 2893 | if (!PageUptodate(page)) { |
| 2894 | if (cleancache_get_page(page) == 0) { |
| 2895 | BUG_ON(blocksize != PAGE_SIZE); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2896 | unlock_extent(tree, start, end); |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 2897 | goto out; |
| 2898 | } |
| 2899 | } |
| 2900 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2901 | if (page->index == last_byte >> PAGE_SHIFT) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2902 | char *userpage; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2903 | size_t zero_offset = last_byte & (PAGE_SIZE - 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2904 | |
| 2905 | if (zero_offset) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2906 | iosize = PAGE_SIZE - zero_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2907 | userpage = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2908 | memset(userpage + zero_offset, 0, iosize); |
| 2909 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2910 | kunmap_atomic(userpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2911 | } |
| 2912 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2913 | while (cur <= end) { |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2914 | bool force_bio_submit = false; |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 2915 | u64 offset; |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 2916 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2917 | if (cur >= last_byte) { |
| 2918 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2919 | struct extent_state *cached = NULL; |
| 2920 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2921 | iosize = PAGE_SIZE - pg_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2922 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2923 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2924 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2925 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2926 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2927 | &cached, GFP_NOFS); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 2928 | unlock_extent_cached(tree, cur, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 2929 | cur + iosize - 1, &cached); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2930 | break; |
| 2931 | } |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2932 | em = __get_extent_map(inode, page, pg_offset, cur, |
| 2933 | end - cur + 1, get_extent, em_cached); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 2934 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2935 | SetPageError(page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 2936 | unlock_extent(tree, cur, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2937 | break; |
| 2938 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2939 | extent_offset = cur - em->start; |
| 2940 | BUG_ON(extent_map_end(em) <= cur); |
| 2941 | BUG_ON(end < cur); |
| 2942 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2943 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 2944 | this_bio_flag |= EXTENT_BIO_COMPRESSED; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2945 | extent_set_compress_type(&this_bio_flag, |
| 2946 | em->compress_type); |
| 2947 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2948 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2949 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
| 2950 | cur_end = min(extent_map_end(em) - 1, end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 2951 | iosize = ALIGN(iosize, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2952 | if (this_bio_flag & EXTENT_BIO_COMPRESSED) { |
| 2953 | disk_io_size = em->block_len; |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 2954 | offset = em->block_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2955 | } else { |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 2956 | offset = em->block_start + extent_offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2957 | disk_io_size = iosize; |
| 2958 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2959 | bdev = em->bdev; |
| 2960 | block_start = em->block_start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2961 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 2962 | block_start = EXTENT_MAP_HOLE; |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2963 | |
| 2964 | /* |
| 2965 | * If we have a file range that points to a compressed extent |
| 2966 | * and it's followed by a consecutive file range that points to |
| 2967 | * to the same compressed extent (possibly with a different |
| 2968 | * offset and/or length, so it either points to the whole extent |
| 2969 | * or only part of it), we must make sure we do not submit a |
| 2970 | * single bio to populate the pages for the 2 ranges because |
| 2971 | * this makes the compressed extent read zero out the pages |
| 2972 | * belonging to the 2nd range. Imagine the following scenario: |
| 2973 | * |
| 2974 | * File layout |
| 2975 | * [0 - 8K] [8K - 24K] |
| 2976 | * | | |
| 2977 | * | | |
| 2978 | * points to extent X, points to extent X, |
| 2979 | * offset 4K, length of 8K offset 0, length 16K |
| 2980 | * |
| 2981 | * [extent X, compressed length = 4K uncompressed length = 16K] |
| 2982 | * |
| 2983 | * If the bio to read the compressed extent covers both ranges, |
| 2984 | * it will decompress extent X into the pages belonging to the |
| 2985 | * first range and then it will stop, zeroing out the remaining |
| 2986 | * pages that belong to the other range that points to extent X. |
| 2987 | * So here we make sure we submit 2 bios, one for the first |
| 2988 | * range and another one for the third range. Both will target |
| 2989 | * the same physical extent from disk, but we can't currently |
| 2990 | * make the compressed bio endio callback populate the pages |
| 2991 | * for both ranges because each compressed bio is tightly |
| 2992 | * coupled with a single extent map, and each range can have |
| 2993 | * an extent map with a different offset value relative to the |
| 2994 | * uncompressed data of our extent and different lengths. This |
| 2995 | * is a corner case so we prioritize correctness over |
| 2996 | * non-optimal behavior (submitting 2 bios for the same extent). |
| 2997 | */ |
| 2998 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) && |
| 2999 | prev_em_start && *prev_em_start != (u64)-1 && |
| 3000 | *prev_em_start != em->orig_start) |
| 3001 | force_bio_submit = true; |
| 3002 | |
| 3003 | if (prev_em_start) |
| 3004 | *prev_em_start = em->orig_start; |
| 3005 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3006 | free_extent_map(em); |
| 3007 | em = NULL; |
| 3008 | |
| 3009 | /* we've found a hole, just zero and go on */ |
| 3010 | if (block_start == EXTENT_MAP_HOLE) { |
| 3011 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3012 | struct extent_state *cached = NULL; |
| 3013 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3014 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3015 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3016 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3017 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3018 | |
| 3019 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3020 | &cached, GFP_NOFS); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3021 | unlock_extent_cached(tree, cur, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 3022 | cur + iosize - 1, &cached); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3023 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3024 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3025 | continue; |
| 3026 | } |
| 3027 | /* the get_extent function already copied into the page */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3028 | if (test_range_bit(tree, cur, cur_end, |
| 3029 | EXTENT_UPTODATE, 1, NULL)) { |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 3030 | check_page_uptodate(tree, page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3031 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3032 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3033 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3034 | continue; |
| 3035 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3036 | /* we have an inline extent but it didn't get marked up |
| 3037 | * to date. Error out |
| 3038 | */ |
| 3039 | if (block_start == EXTENT_MAP_INLINE) { |
| 3040 | SetPageError(page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3041 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3042 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3043 | pg_offset += iosize; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3044 | continue; |
| 3045 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3046 | |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 3047 | ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL, |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 3048 | page, offset, disk_io_size, |
| 3049 | pg_offset, bdev, bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3050 | end_bio_extent_readpage, mirror_num, |
| 3051 | *bio_flags, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3052 | this_bio_flag, |
| 3053 | force_bio_submit); |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3054 | if (!ret) { |
| 3055 | nr++; |
| 3056 | *bio_flags = this_bio_flag; |
| 3057 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3058 | SetPageError(page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3059 | unlock_extent(tree, cur, cur + iosize - 1); |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 3060 | goto out; |
Josef Bacik | edd33c9 | 2012-10-05 16:40:32 -0400 | [diff] [blame] | 3061 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3062 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3063 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3064 | } |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 3065 | out: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3066 | if (!nr) { |
| 3067 | if (!PageError(page)) |
| 3068 | SetPageUptodate(page); |
| 3069 | unlock_page(page); |
| 3070 | } |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 3071 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3072 | } |
| 3073 | |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3074 | static inline void __do_contiguous_readpages(struct extent_io_tree *tree, |
| 3075 | struct page *pages[], int nr_pages, |
| 3076 | u64 start, u64 end, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3077 | struct extent_map **em_cached, |
Nikolay Borisov | d3fac6b | 2017-10-24 11:50:39 +0300 | [diff] [blame] | 3078 | struct bio **bio, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3079 | unsigned long *bio_flags, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3080 | u64 *prev_em_start) |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3081 | { |
| 3082 | struct inode *inode; |
| 3083 | struct btrfs_ordered_extent *ordered; |
| 3084 | int index; |
| 3085 | |
| 3086 | inode = pages[0]->mapping->host; |
| 3087 | while (1) { |
| 3088 | lock_extent(tree, start, end); |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 3089 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3090 | end - start + 1); |
| 3091 | if (!ordered) |
| 3092 | break; |
| 3093 | unlock_extent(tree, start, end); |
| 3094 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3095 | btrfs_put_ordered_extent(ordered); |
| 3096 | } |
| 3097 | |
| 3098 | for (index = 0; index < nr_pages; index++) { |
David Sterba | 4ef7769 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 3099 | __do_readpage(tree, pages[index], btrfs_get_extent, em_cached, |
Jens Axboe | 5e9d398 | 2018-08-17 15:45:39 -0700 | [diff] [blame] | 3100 | bio, 0, bio_flags, REQ_RAHEAD, prev_em_start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3101 | put_page(pages[index]); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3102 | } |
| 3103 | } |
| 3104 | |
| 3105 | static void __extent_readpages(struct extent_io_tree *tree, |
| 3106 | struct page *pages[], |
David Sterba | e4d17ef | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 3107 | int nr_pages, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3108 | struct extent_map **em_cached, |
Nikolay Borisov | d3fac6b | 2017-10-24 11:50:39 +0300 | [diff] [blame] | 3109 | struct bio **bio, unsigned long *bio_flags, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3110 | u64 *prev_em_start) |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3111 | { |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 3112 | u64 start = 0; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3113 | u64 end = 0; |
| 3114 | u64 page_start; |
| 3115 | int index; |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 3116 | int first_index = 0; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3117 | |
| 3118 | for (index = 0; index < nr_pages; index++) { |
| 3119 | page_start = page_offset(pages[index]); |
| 3120 | if (!end) { |
| 3121 | start = page_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3122 | end = start + PAGE_SIZE - 1; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3123 | first_index = index; |
| 3124 | } else if (end + 1 == page_start) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3125 | end += PAGE_SIZE; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3126 | } else { |
| 3127 | __do_contiguous_readpages(tree, &pages[first_index], |
| 3128 | index - first_index, start, |
David Sterba | 4ef7769 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 3129 | end, em_cached, |
Nikolay Borisov | d3fac6b | 2017-10-24 11:50:39 +0300 | [diff] [blame] | 3130 | bio, bio_flags, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3131 | prev_em_start); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3132 | start = page_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3133 | end = start + PAGE_SIZE - 1; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3134 | first_index = index; |
| 3135 | } |
| 3136 | } |
| 3137 | |
| 3138 | if (end) |
| 3139 | __do_contiguous_readpages(tree, &pages[first_index], |
| 3140 | index - first_index, start, |
David Sterba | 4ef7769 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 3141 | end, em_cached, bio, |
Nikolay Borisov | d3fac6b | 2017-10-24 11:50:39 +0300 | [diff] [blame] | 3142 | bio_flags, prev_em_start); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | static int __extent_read_full_page(struct extent_io_tree *tree, |
| 3146 | struct page *page, |
| 3147 | get_extent_t *get_extent, |
| 3148 | struct bio **bio, int mirror_num, |
David Sterba | f1c77c5 | 2017-06-06 19:03:49 +0200 | [diff] [blame] | 3149 | unsigned long *bio_flags, |
| 3150 | unsigned int read_flags) |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3151 | { |
| 3152 | struct inode *inode = page->mapping->host; |
| 3153 | struct btrfs_ordered_extent *ordered; |
| 3154 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3155 | u64 end = start + PAGE_SIZE - 1; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3156 | int ret; |
| 3157 | |
| 3158 | while (1) { |
| 3159 | lock_extent(tree, start, end); |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 3160 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3161 | PAGE_SIZE); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3162 | if (!ordered) |
| 3163 | break; |
| 3164 | unlock_extent(tree, start, end); |
| 3165 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3166 | btrfs_put_ordered_extent(ordered); |
| 3167 | } |
| 3168 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3169 | ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3170 | bio_flags, read_flags, NULL); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3171 | return ret; |
| 3172 | } |
| 3173 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3174 | int extent_read_full_page(struct extent_io_tree *tree, struct page *page, |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 3175 | get_extent_t *get_extent, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3176 | { |
| 3177 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3178 | unsigned long bio_flags = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3179 | int ret; |
| 3180 | |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 3181 | ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3182 | &bio_flags, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3183 | if (bio) |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3184 | ret = submit_one_bio(bio, mirror_num, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3185 | return ret; |
| 3186 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3187 | |
David Sterba | 3d4b949 | 2017-02-10 19:33:41 +0100 | [diff] [blame] | 3188 | static void update_nr_written(struct writeback_control *wbc, |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3189 | unsigned long nr_written) |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3190 | { |
| 3191 | wbc->nr_to_write -= nr_written; |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3192 | } |
| 3193 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3194 | /* |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3195 | * helper for __extent_writepage, doing all of the delayed allocation setup. |
| 3196 | * |
| 3197 | * This returns 1 if our fill_delalloc function did all the work required |
| 3198 | * to write the page (copy into inline extent). In this case the IO has |
| 3199 | * been started and the page is already unlocked. |
| 3200 | * |
| 3201 | * This returns 0 if all went well (page still locked) |
| 3202 | * This returns < 0 if there were errors (page still locked) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3203 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3204 | static noinline_for_stack int writepage_delalloc(struct inode *inode, |
| 3205 | struct page *page, struct writeback_control *wbc, |
| 3206 | struct extent_page_data *epd, |
| 3207 | u64 delalloc_start, |
| 3208 | unsigned long *nr_written) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3209 | { |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3210 | struct extent_io_tree *tree = epd->tree; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3211 | u64 page_end = delalloc_start + PAGE_SIZE - 1; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3212 | u64 nr_delalloc; |
| 3213 | u64 delalloc_to_write = 0; |
| 3214 | u64 delalloc_end = 0; |
| 3215 | int ret; |
| 3216 | int page_started = 0; |
| 3217 | |
| 3218 | if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc) |
| 3219 | return 0; |
| 3220 | |
| 3221 | while (delalloc_end < page_end) { |
| 3222 | nr_delalloc = find_lock_delalloc_range(inode, tree, |
| 3223 | page, |
| 3224 | &delalloc_start, |
| 3225 | &delalloc_end, |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 3226 | BTRFS_MAX_EXTENT_SIZE); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3227 | if (nr_delalloc == 0) { |
| 3228 | delalloc_start = delalloc_end + 1; |
| 3229 | continue; |
| 3230 | } |
| 3231 | ret = tree->ops->fill_delalloc(inode, page, |
| 3232 | delalloc_start, |
| 3233 | delalloc_end, |
| 3234 | &page_started, |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 3235 | nr_written, wbc); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3236 | /* File system has been set read-only */ |
| 3237 | if (ret) { |
| 3238 | SetPageError(page); |
| 3239 | /* fill_delalloc should be return < 0 for error |
| 3240 | * but just in case, we use > 0 here meaning the |
| 3241 | * IO is started, so we don't want to return > 0 |
| 3242 | * unless things are going well. |
| 3243 | */ |
| 3244 | ret = ret < 0 ? ret : -EIO; |
| 3245 | goto done; |
| 3246 | } |
| 3247 | /* |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 3248 | * delalloc_end is already one less than the total length, so |
| 3249 | * we don't subtract one from PAGE_SIZE |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3250 | */ |
| 3251 | delalloc_to_write += (delalloc_end - delalloc_start + |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 3252 | PAGE_SIZE) >> PAGE_SHIFT; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3253 | delalloc_start = delalloc_end + 1; |
| 3254 | } |
| 3255 | if (wbc->nr_to_write < delalloc_to_write) { |
| 3256 | int thresh = 8192; |
| 3257 | |
| 3258 | if (delalloc_to_write < thresh * 2) |
| 3259 | thresh = delalloc_to_write; |
| 3260 | wbc->nr_to_write = min_t(u64, delalloc_to_write, |
| 3261 | thresh); |
| 3262 | } |
| 3263 | |
| 3264 | /* did the fill delalloc function already unlock and start |
| 3265 | * the IO? |
| 3266 | */ |
| 3267 | if (page_started) { |
| 3268 | /* |
| 3269 | * we've unlocked the page, so we can't update |
| 3270 | * the mapping's writeback index, just update |
| 3271 | * nr_to_write. |
| 3272 | */ |
| 3273 | wbc->nr_to_write -= *nr_written; |
| 3274 | return 1; |
| 3275 | } |
| 3276 | |
| 3277 | ret = 0; |
| 3278 | |
| 3279 | done: |
| 3280 | return ret; |
| 3281 | } |
| 3282 | |
| 3283 | /* |
| 3284 | * helper for __extent_writepage. This calls the writepage start hooks, |
| 3285 | * and does the loop to map the page into extents and bios. |
| 3286 | * |
| 3287 | * We return 1 if the IO is started and the page is unlocked, |
| 3288 | * 0 if all went well (page still locked) |
| 3289 | * < 0 if there were errors (page still locked) |
| 3290 | */ |
| 3291 | static noinline_for_stack int __extent_writepage_io(struct inode *inode, |
| 3292 | struct page *page, |
| 3293 | struct writeback_control *wbc, |
| 3294 | struct extent_page_data *epd, |
| 3295 | loff_t i_size, |
| 3296 | unsigned long nr_written, |
David Sterba | f1c77c5 | 2017-06-06 19:03:49 +0200 | [diff] [blame] | 3297 | unsigned int write_flags, int *nr_ret) |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3298 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3299 | struct extent_io_tree *tree = epd->tree; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3300 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3301 | u64 page_end = start + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3302 | u64 end; |
| 3303 | u64 cur = start; |
| 3304 | u64 extent_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3305 | u64 block_start; |
| 3306 | u64 iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3307 | struct extent_map *em; |
| 3308 | struct block_device *bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3309 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3310 | size_t blocksize; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3311 | int ret = 0; |
| 3312 | int nr = 0; |
| 3313 | bool compressed; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3314 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3315 | if (tree->ops && tree->ops->writepage_start_hook) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3316 | ret = tree->ops->writepage_start_hook(page, start, |
| 3317 | page_end); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 3318 | if (ret) { |
| 3319 | /* Fixup worker will requeue */ |
| 3320 | if (ret == -EBUSY) |
| 3321 | wbc->pages_skipped++; |
| 3322 | else |
| 3323 | redirty_page_for_writepage(wbc, page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3324 | |
David Sterba | 3d4b949 | 2017-02-10 19:33:41 +0100 | [diff] [blame] | 3325 | update_nr_written(wbc, nr_written); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3326 | unlock_page(page); |
Liu Bo | bcf9348 | 2017-01-25 17:15:54 -0800 | [diff] [blame] | 3327 | return 1; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3328 | } |
| 3329 | } |
| 3330 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3331 | /* |
| 3332 | * we don't want to touch the inode after unlocking the page, |
| 3333 | * so we update the mapping writeback index now |
| 3334 | */ |
David Sterba | 3d4b949 | 2017-02-10 19:33:41 +0100 | [diff] [blame] | 3335 | update_nr_written(wbc, nr_written + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3336 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3337 | end = page_end; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3338 | if (i_size <= start) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3339 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 3340 | tree->ops->writepage_end_io_hook(page, start, |
| 3341 | page_end, NULL, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3342 | goto done; |
| 3343 | } |
| 3344 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3345 | blocksize = inode->i_sb->s_blocksize; |
| 3346 | |
| 3347 | while (cur <= end) { |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3348 | u64 em_end; |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 3349 | u64 offset; |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3350 | |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3351 | if (cur >= i_size) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3352 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 3353 | tree->ops->writepage_end_io_hook(page, cur, |
| 3354 | page_end, NULL, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3355 | break; |
| 3356 | } |
David Sterba | 3c98c62 | 2017-06-23 04:01:08 +0200 | [diff] [blame] | 3357 | em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, cur, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3358 | end - cur + 1, 1); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3359 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3360 | SetPageError(page); |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 3361 | ret = PTR_ERR_OR_ZERO(em); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3362 | break; |
| 3363 | } |
| 3364 | |
| 3365 | extent_offset = cur - em->start; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3366 | em_end = extent_map_end(em); |
| 3367 | BUG_ON(em_end <= cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3368 | BUG_ON(end < cur); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3369 | iosize = min(em_end - cur, end - cur + 1); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3370 | iosize = ALIGN(iosize, blocksize); |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 3371 | offset = em->block_start + extent_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3372 | bdev = em->bdev; |
| 3373 | block_start = em->block_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3374 | compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3375 | free_extent_map(em); |
| 3376 | em = NULL; |
| 3377 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3378 | /* |
| 3379 | * compressed and inline extents are written through other |
| 3380 | * paths in the FS |
| 3381 | */ |
| 3382 | if (compressed || block_start == EXTENT_MAP_HOLE || |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3383 | block_start == EXTENT_MAP_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3384 | /* |
| 3385 | * end_io notification does not happen here for |
| 3386 | * compressed extents |
| 3387 | */ |
| 3388 | if (!compressed && tree->ops && |
| 3389 | tree->ops->writepage_end_io_hook) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3390 | tree->ops->writepage_end_io_hook(page, cur, |
| 3391 | cur + iosize - 1, |
| 3392 | NULL, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3393 | else if (compressed) { |
| 3394 | /* we don't want to end_page_writeback on |
| 3395 | * a compressed extent. this happens |
| 3396 | * elsewhere |
| 3397 | */ |
| 3398 | nr++; |
| 3399 | } |
| 3400 | |
| 3401 | cur += iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3402 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3403 | continue; |
| 3404 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3405 | |
David Sterba | 5cdc84b | 2018-07-18 20:32:52 +0200 | [diff] [blame] | 3406 | btrfs_set_range_writeback(tree, cur, cur + iosize - 1); |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3407 | if (!PageWriteback(page)) { |
| 3408 | btrfs_err(BTRFS_I(inode)->root->fs_info, |
| 3409 | "page %lu not writeback, cur %llu end %llu", |
| 3410 | page->index, cur, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3411 | } |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3412 | |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 3413 | ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc, |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 3414 | page, offset, iosize, pg_offset, |
David Sterba | c2df8bb | 2017-02-10 19:29:38 +0100 | [diff] [blame] | 3415 | bdev, &epd->bio, |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3416 | end_bio_extent_writepage, |
| 3417 | 0, 0, 0, false); |
Takafumi Kubota | fe01aa6 | 2017-02-09 17:24:33 +0900 | [diff] [blame] | 3418 | if (ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3419 | SetPageError(page); |
Takafumi Kubota | fe01aa6 | 2017-02-09 17:24:33 +0900 | [diff] [blame] | 3420 | if (PageWriteback(page)) |
| 3421 | end_page_writeback(page); |
| 3422 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3423 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3424 | cur = cur + iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3425 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3426 | nr++; |
| 3427 | } |
| 3428 | done: |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3429 | *nr_ret = nr; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3430 | return ret; |
| 3431 | } |
| 3432 | |
| 3433 | /* |
| 3434 | * the writepage semantics are similar to regular writepage. extent |
| 3435 | * records are inserted to lock ranges in the tree, and as dirty areas |
| 3436 | * are found, they are marked writeback. Then the lock bits are removed |
| 3437 | * and the end_io handler clears the writeback ranges |
| 3438 | */ |
| 3439 | static int __extent_writepage(struct page *page, struct writeback_control *wbc, |
David Sterba | aab6e9e | 2017-11-30 18:00:02 +0100 | [diff] [blame] | 3440 | struct extent_page_data *epd) |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3441 | { |
| 3442 | struct inode *inode = page->mapping->host; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3443 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3444 | u64 page_end = start + PAGE_SIZE - 1; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3445 | int ret; |
| 3446 | int nr = 0; |
| 3447 | size_t pg_offset = 0; |
| 3448 | loff_t i_size = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3449 | unsigned long end_index = i_size >> PAGE_SHIFT; |
David Sterba | f1c77c5 | 2017-06-06 19:03:49 +0200 | [diff] [blame] | 3450 | unsigned int write_flags = 0; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3451 | unsigned long nr_written = 0; |
| 3452 | |
Liu Bo | ff40adf | 2017-08-24 18:19:48 -0600 | [diff] [blame] | 3453 | write_flags = wbc_to_write_flags(wbc); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3454 | |
| 3455 | trace___extent_writepage(page, inode, wbc); |
| 3456 | |
| 3457 | WARN_ON(!PageLocked(page)); |
| 3458 | |
| 3459 | ClearPageError(page); |
| 3460 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3461 | pg_offset = i_size & (PAGE_SIZE - 1); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3462 | if (page->index > end_index || |
| 3463 | (page->index == end_index && !pg_offset)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3464 | page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3465 | unlock_page(page); |
| 3466 | return 0; |
| 3467 | } |
| 3468 | |
| 3469 | if (page->index == end_index) { |
| 3470 | char *userpage; |
| 3471 | |
| 3472 | userpage = kmap_atomic(page); |
| 3473 | memset(userpage + pg_offset, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3474 | PAGE_SIZE - pg_offset); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3475 | kunmap_atomic(userpage); |
| 3476 | flush_dcache_page(page); |
| 3477 | } |
| 3478 | |
| 3479 | pg_offset = 0; |
| 3480 | |
| 3481 | set_page_extent_mapped(page); |
| 3482 | |
| 3483 | ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written); |
| 3484 | if (ret == 1) |
| 3485 | goto done_unlocked; |
| 3486 | if (ret) |
| 3487 | goto done; |
| 3488 | |
| 3489 | ret = __extent_writepage_io(inode, page, wbc, epd, |
| 3490 | i_size, nr_written, write_flags, &nr); |
| 3491 | if (ret == 1) |
| 3492 | goto done_unlocked; |
| 3493 | |
| 3494 | done: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3495 | if (nr == 0) { |
| 3496 | /* make sure the mapping tag for page dirty gets cleared */ |
| 3497 | set_page_writeback(page); |
| 3498 | end_page_writeback(page); |
| 3499 | } |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 3500 | if (PageError(page)) { |
| 3501 | ret = ret < 0 ? ret : -EIO; |
| 3502 | end_extent_writepage(page, ret, start, page_end); |
| 3503 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3504 | unlock_page(page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3505 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3506 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3507 | done_unlocked: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3508 | return 0; |
| 3509 | } |
| 3510 | |
Josef Bacik | fd8b2b6 | 2013-04-24 16:41:19 -0400 | [diff] [blame] | 3511 | void wait_on_extent_buffer_writeback(struct extent_buffer *eb) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3512 | { |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 3513 | wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK, |
| 3514 | TASK_UNINTERRUPTIBLE); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3515 | } |
| 3516 | |
Chris Mason | 0e378df | 2014-05-19 20:55:27 -0700 | [diff] [blame] | 3517 | static noinline_for_stack int |
| 3518 | lock_extent_buffer_for_io(struct extent_buffer *eb, |
| 3519 | struct btrfs_fs_info *fs_info, |
| 3520 | struct extent_page_data *epd) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3521 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 3522 | int i, num_pages; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3523 | int flush = 0; |
| 3524 | int ret = 0; |
| 3525 | |
| 3526 | if (!btrfs_try_tree_write_lock(eb)) { |
| 3527 | flush = 1; |
| 3528 | flush_write_bio(epd); |
| 3529 | btrfs_tree_lock(eb); |
| 3530 | } |
| 3531 | |
| 3532 | if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) { |
| 3533 | btrfs_tree_unlock(eb); |
| 3534 | if (!epd->sync_io) |
| 3535 | return 0; |
| 3536 | if (!flush) { |
| 3537 | flush_write_bio(epd); |
| 3538 | flush = 1; |
| 3539 | } |
Chris Mason | a098d8e8 | 2012-03-21 12:09:56 -0400 | [diff] [blame] | 3540 | while (1) { |
| 3541 | wait_on_extent_buffer_writeback(eb); |
| 3542 | btrfs_tree_lock(eb); |
| 3543 | if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) |
| 3544 | break; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3545 | btrfs_tree_unlock(eb); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3546 | } |
| 3547 | } |
| 3548 | |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3549 | /* |
| 3550 | * We need to do this to prevent races in people who check if the eb is |
| 3551 | * under IO since we can end up having no IO bits set for a short period |
| 3552 | * of time. |
| 3553 | */ |
| 3554 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3555 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) { |
| 3556 | set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3557 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3558 | btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 3559 | percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, |
| 3560 | -eb->len, |
| 3561 | fs_info->dirty_metadata_batch); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3562 | ret = 1; |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3563 | } else { |
| 3564 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3565 | } |
| 3566 | |
| 3567 | btrfs_tree_unlock(eb); |
| 3568 | |
| 3569 | if (!ret) |
| 3570 | return ret; |
| 3571 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 3572 | num_pages = num_extent_pages(eb); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3573 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 3574 | struct page *p = eb->pages[i]; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3575 | |
| 3576 | if (!trylock_page(p)) { |
| 3577 | if (!flush) { |
| 3578 | flush_write_bio(epd); |
| 3579 | flush = 1; |
| 3580 | } |
| 3581 | lock_page(p); |
| 3582 | } |
| 3583 | } |
| 3584 | |
| 3585 | return ret; |
| 3586 | } |
| 3587 | |
| 3588 | static void end_extent_buffer_writeback(struct extent_buffer *eb) |
| 3589 | { |
| 3590 | clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 3591 | smp_mb__after_atomic(); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3592 | wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK); |
| 3593 | } |
| 3594 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3595 | static void set_btree_ioerr(struct page *page) |
| 3596 | { |
| 3597 | struct extent_buffer *eb = (struct extent_buffer *)page->private; |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3598 | |
| 3599 | SetPageError(page); |
| 3600 | if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) |
| 3601 | return; |
| 3602 | |
| 3603 | /* |
| 3604 | * If writeback for a btree extent that doesn't belong to a log tree |
| 3605 | * failed, increment the counter transaction->eb_write_errors. |
| 3606 | * We do this because while the transaction is running and before it's |
| 3607 | * committing (when we call filemap_fdata[write|wait]_range against |
| 3608 | * the btree inode), we might have |
| 3609 | * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it |
| 3610 | * returns an error or an error happens during writeback, when we're |
| 3611 | * committing the transaction we wouldn't know about it, since the pages |
| 3612 | * can be no longer dirty nor marked anymore for writeback (if a |
| 3613 | * subsequent modification to the extent buffer didn't happen before the |
| 3614 | * transaction commit), which makes filemap_fdata[write|wait]_range not |
| 3615 | * able to find the pages tagged with SetPageError at transaction |
| 3616 | * commit time. So if this happens we must abort the transaction, |
| 3617 | * otherwise we commit a super block with btree roots that point to |
| 3618 | * btree nodes/leafs whose content on disk is invalid - either garbage |
| 3619 | * or the content of some node/leaf from a past generation that got |
| 3620 | * cowed or deleted and is no longer valid. |
| 3621 | * |
| 3622 | * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would |
| 3623 | * not be enough - we need to distinguish between log tree extents vs |
| 3624 | * non-log tree extents, and the next filemap_fdatawait_range() call |
| 3625 | * will catch and clear such errors in the mapping - and that call might |
| 3626 | * be from a log sync and not from a transaction commit. Also, checking |
| 3627 | * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is |
| 3628 | * not done and would not be reliable - the eb might have been released |
| 3629 | * from memory and reading it back again means that flag would not be |
| 3630 | * set (since it's a runtime flag, not persisted on disk). |
| 3631 | * |
| 3632 | * Using the flags below in the btree inode also makes us achieve the |
| 3633 | * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started |
| 3634 | * writeback for all dirty pages and before filemap_fdatawait_range() |
| 3635 | * is called, the writeback for all dirty pages had already finished |
| 3636 | * with errors - because we were not using AS_EIO/AS_ENOSPC, |
| 3637 | * filemap_fdatawait_range() would return success, as it could not know |
| 3638 | * that writeback errors happened (the pages were no longer tagged for |
| 3639 | * writeback). |
| 3640 | */ |
| 3641 | switch (eb->log_index) { |
| 3642 | case -1: |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 3643 | set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3644 | break; |
| 3645 | case 0: |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 3646 | set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3647 | break; |
| 3648 | case 1: |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 3649 | set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3650 | break; |
| 3651 | default: |
| 3652 | BUG(); /* unexpected, logic error */ |
| 3653 | } |
| 3654 | } |
| 3655 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3656 | static void end_bio_extent_buffer_writepage(struct bio *bio) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3657 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3658 | struct bio_vec *bvec; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3659 | struct extent_buffer *eb; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3660 | int i, done; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3661 | |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 3662 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3663 | bio_for_each_segment_all(bvec, bio, i) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3664 | struct page *page = bvec->bv_page; |
| 3665 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3666 | eb = (struct extent_buffer *)page->private; |
| 3667 | BUG_ON(!eb); |
| 3668 | done = atomic_dec_and_test(&eb->io_pages); |
| 3669 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 3670 | if (bio->bi_status || |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3671 | test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3672 | ClearPageUptodate(page); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3673 | set_btree_ioerr(page); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3674 | } |
| 3675 | |
| 3676 | end_page_writeback(page); |
| 3677 | |
| 3678 | if (!done) |
| 3679 | continue; |
| 3680 | |
| 3681 | end_extent_buffer_writeback(eb); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3682 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3683 | |
| 3684 | bio_put(bio); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3685 | } |
| 3686 | |
Chris Mason | 0e378df | 2014-05-19 20:55:27 -0700 | [diff] [blame] | 3687 | static noinline_for_stack int write_one_eb(struct extent_buffer *eb, |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3688 | struct btrfs_fs_info *fs_info, |
| 3689 | struct writeback_control *wbc, |
| 3690 | struct extent_page_data *epd) |
| 3691 | { |
| 3692 | struct block_device *bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 3693 | struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3694 | u64 offset = eb->start; |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3695 | u32 nritems; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 3696 | int i, num_pages; |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3697 | unsigned long start, end; |
Liu Bo | ff40adf | 2017-08-24 18:19:48 -0600 | [diff] [blame] | 3698 | unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META; |
Josef Bacik | d7dbe9e | 2012-04-23 14:00:51 -0400 | [diff] [blame] | 3699 | int ret = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3700 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3701 | clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags); |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 3702 | num_pages = num_extent_pages(eb); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3703 | atomic_set(&eb->io_pages, num_pages); |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3704 | |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3705 | /* set btree blocks beyond nritems with 0 to avoid stale content. */ |
| 3706 | nritems = btrfs_header_nritems(eb); |
Liu Bo | 3eb548e | 2016-09-14 17:22:57 -0700 | [diff] [blame] | 3707 | if (btrfs_header_level(eb) > 0) { |
Liu Bo | 3eb548e | 2016-09-14 17:22:57 -0700 | [diff] [blame] | 3708 | end = btrfs_node_key_ptr_offset(nritems); |
| 3709 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3710 | memzero_extent_buffer(eb, end, eb->len - end); |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3711 | } else { |
| 3712 | /* |
| 3713 | * leaf: |
| 3714 | * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0 |
| 3715 | */ |
| 3716 | start = btrfs_item_nr_offset(nritems); |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3717 | end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, eb); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3718 | memzero_extent_buffer(eb, start, end - start); |
Liu Bo | 3eb548e | 2016-09-14 17:22:57 -0700 | [diff] [blame] | 3719 | } |
| 3720 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3721 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 3722 | struct page *p = eb->pages[i]; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3723 | |
| 3724 | clear_page_dirty_for_io(p); |
| 3725 | set_page_writeback(p); |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 3726 | ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc, |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 3727 | p, offset, PAGE_SIZE, 0, bdev, |
David Sterba | c2df8bb | 2017-02-10 19:29:38 +0100 | [diff] [blame] | 3728 | &epd->bio, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3729 | end_bio_extent_buffer_writepage, |
Liu Bo | 18fdc67 | 2017-09-13 12:18:22 -0600 | [diff] [blame] | 3730 | 0, 0, 0, false); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3731 | if (ret) { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3732 | set_btree_ioerr(p); |
Takafumi Kubota | fe01aa6 | 2017-02-09 17:24:33 +0900 | [diff] [blame] | 3733 | if (PageWriteback(p)) |
| 3734 | end_page_writeback(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3735 | if (atomic_sub_and_test(num_pages - i, &eb->io_pages)) |
| 3736 | end_extent_buffer_writeback(eb); |
| 3737 | ret = -EIO; |
| 3738 | break; |
| 3739 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3740 | offset += PAGE_SIZE; |
David Sterba | 3d4b949 | 2017-02-10 19:33:41 +0100 | [diff] [blame] | 3741 | update_nr_written(wbc, 1); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3742 | unlock_page(p); |
| 3743 | } |
| 3744 | |
| 3745 | if (unlikely(ret)) { |
| 3746 | for (; i < num_pages; i++) { |
Chris Mason | bbf65cf | 2014-10-04 09:56:45 -0700 | [diff] [blame] | 3747 | struct page *p = eb->pages[i]; |
Liu Bo | 8146502 | 2014-09-23 22:22:33 +0800 | [diff] [blame] | 3748 | clear_page_dirty_for_io(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3749 | unlock_page(p); |
| 3750 | } |
| 3751 | } |
| 3752 | |
| 3753 | return ret; |
| 3754 | } |
| 3755 | |
| 3756 | int btree_write_cache_pages(struct address_space *mapping, |
| 3757 | struct writeback_control *wbc) |
| 3758 | { |
| 3759 | struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree; |
| 3760 | struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info; |
| 3761 | struct extent_buffer *eb, *prev_eb = NULL; |
| 3762 | struct extent_page_data epd = { |
| 3763 | .bio = NULL, |
| 3764 | .tree = tree, |
| 3765 | .extent_locked = 0, |
| 3766 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
| 3767 | }; |
| 3768 | int ret = 0; |
| 3769 | int done = 0; |
| 3770 | int nr_to_write_done = 0; |
| 3771 | struct pagevec pvec; |
| 3772 | int nr_pages; |
| 3773 | pgoff_t index; |
| 3774 | pgoff_t end; /* Inclusive */ |
| 3775 | int scanned = 0; |
| 3776 | int tag; |
| 3777 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 3778 | pagevec_init(&pvec); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3779 | if (wbc->range_cyclic) { |
| 3780 | index = mapping->writeback_index; /* Start from prev offset */ |
| 3781 | end = -1; |
| 3782 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3783 | index = wbc->range_start >> PAGE_SHIFT; |
| 3784 | end = wbc->range_end >> PAGE_SHIFT; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3785 | scanned = 1; |
| 3786 | } |
| 3787 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3788 | tag = PAGECACHE_TAG_TOWRITE; |
| 3789 | else |
| 3790 | tag = PAGECACHE_TAG_DIRTY; |
| 3791 | retry: |
| 3792 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3793 | tag_pages_for_writeback(mapping, index, end); |
| 3794 | while (!done && !nr_to_write_done && (index <= end) && |
Jan Kara | 4006f43 | 2017-11-15 17:34:37 -0800 | [diff] [blame] | 3795 | (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end, |
Jan Kara | 67fd707 | 2017-11-15 17:35:19 -0800 | [diff] [blame] | 3796 | tag))) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3797 | unsigned i; |
| 3798 | |
| 3799 | scanned = 1; |
| 3800 | for (i = 0; i < nr_pages; i++) { |
| 3801 | struct page *page = pvec.pages[i]; |
| 3802 | |
| 3803 | if (!PagePrivate(page)) |
| 3804 | continue; |
| 3805 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3806 | spin_lock(&mapping->private_lock); |
| 3807 | if (!PagePrivate(page)) { |
| 3808 | spin_unlock(&mapping->private_lock); |
| 3809 | continue; |
| 3810 | } |
| 3811 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3812 | eb = (struct extent_buffer *)page->private; |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3813 | |
| 3814 | /* |
| 3815 | * Shouldn't happen and normally this would be a BUG_ON |
| 3816 | * but no sense in crashing the users box for something |
| 3817 | * we can survive anyway. |
| 3818 | */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3819 | if (WARN_ON(!eb)) { |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3820 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3821 | continue; |
| 3822 | } |
| 3823 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3824 | if (eb == prev_eb) { |
| 3825 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3826 | continue; |
| 3827 | } |
| 3828 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3829 | ret = atomic_inc_not_zero(&eb->refs); |
| 3830 | spin_unlock(&mapping->private_lock); |
| 3831 | if (!ret) |
| 3832 | continue; |
| 3833 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3834 | prev_eb = eb; |
| 3835 | ret = lock_extent_buffer_for_io(eb, fs_info, &epd); |
| 3836 | if (!ret) { |
| 3837 | free_extent_buffer(eb); |
| 3838 | continue; |
| 3839 | } |
| 3840 | |
| 3841 | ret = write_one_eb(eb, fs_info, wbc, &epd); |
| 3842 | if (ret) { |
| 3843 | done = 1; |
| 3844 | free_extent_buffer(eb); |
| 3845 | break; |
| 3846 | } |
| 3847 | free_extent_buffer(eb); |
| 3848 | |
| 3849 | /* |
| 3850 | * the filesystem may choose to bump up nr_to_write. |
| 3851 | * We have to make sure to honor the new nr_to_write |
| 3852 | * at any time |
| 3853 | */ |
| 3854 | nr_to_write_done = wbc->nr_to_write <= 0; |
| 3855 | } |
| 3856 | pagevec_release(&pvec); |
| 3857 | cond_resched(); |
| 3858 | } |
| 3859 | if (!scanned && !done) { |
| 3860 | /* |
| 3861 | * We hit the last page and there is more work to be done: wrap |
| 3862 | * back to the start of the file |
| 3863 | */ |
| 3864 | scanned = 1; |
| 3865 | index = 0; |
| 3866 | goto retry; |
| 3867 | } |
| 3868 | flush_write_bio(&epd); |
| 3869 | return ret; |
| 3870 | } |
| 3871 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3872 | /** |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3873 | * write_cache_pages - walk the list of dirty pages of the given address space and write all of them. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3874 | * @mapping: address space structure to write |
| 3875 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
David Sterba | 935db85 | 2017-06-23 04:30:28 +0200 | [diff] [blame] | 3876 | * @data: data passed to __extent_writepage function |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3877 | * |
| 3878 | * If a page is already under I/O, write_cache_pages() skips it, even |
| 3879 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, |
| 3880 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() |
| 3881 | * and msync() need to guarantee that all the data which was dirty at the time |
| 3882 | * the call was made get new I/O started against them. If wbc->sync_mode is |
| 3883 | * WB_SYNC_ALL then we were called for data integrity and we must wait for |
| 3884 | * existing IO to complete. |
| 3885 | */ |
David Sterba | 4242b64 | 2017-02-10 19:38:24 +0100 | [diff] [blame] | 3886 | static int extent_write_cache_pages(struct address_space *mapping, |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3887 | struct writeback_control *wbc, |
David Sterba | aab6e9e | 2017-11-30 18:00:02 +0100 | [diff] [blame] | 3888 | struct extent_page_data *epd) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3889 | { |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 3890 | struct inode *inode = mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3891 | int ret = 0; |
| 3892 | int done = 0; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3893 | int nr_to_write_done = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3894 | struct pagevec pvec; |
| 3895 | int nr_pages; |
| 3896 | pgoff_t index; |
| 3897 | pgoff_t end; /* Inclusive */ |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3898 | pgoff_t done_index; |
| 3899 | int range_whole = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3900 | int scanned = 0; |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3901 | int tag; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3902 | |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 3903 | /* |
| 3904 | * We have to hold onto the inode so that ordered extents can do their |
| 3905 | * work when the IO finishes. The alternative to this is failing to add |
| 3906 | * an ordered extent if the igrab() fails there and that is a huge pain |
| 3907 | * to deal with, so instead just hold onto the inode throughout the |
| 3908 | * writepages operation. If it fails here we are freeing up the inode |
| 3909 | * anyway and we'd rather not waste our time writing out stuff that is |
| 3910 | * going to be truncated anyway. |
| 3911 | */ |
| 3912 | if (!igrab(inode)) |
| 3913 | return 0; |
| 3914 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 3915 | pagevec_init(&pvec); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3916 | if (wbc->range_cyclic) { |
| 3917 | index = mapping->writeback_index; /* Start from prev offset */ |
| 3918 | end = -1; |
| 3919 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3920 | index = wbc->range_start >> PAGE_SHIFT; |
| 3921 | end = wbc->range_end >> PAGE_SHIFT; |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3922 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 3923 | range_whole = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3924 | scanned = 1; |
| 3925 | } |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3926 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3927 | tag = PAGECACHE_TAG_TOWRITE; |
| 3928 | else |
| 3929 | tag = PAGECACHE_TAG_DIRTY; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3930 | retry: |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3931 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3932 | tag_pages_for_writeback(mapping, index, end); |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3933 | done_index = index; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3934 | while (!done && !nr_to_write_done && (index <= end) && |
Jan Kara | 67fd707 | 2017-11-15 17:35:19 -0800 | [diff] [blame] | 3935 | (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, |
| 3936 | &index, end, tag))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3937 | unsigned i; |
| 3938 | |
| 3939 | scanned = 1; |
| 3940 | for (i = 0; i < nr_pages; i++) { |
| 3941 | struct page *page = pvec.pages[i]; |
| 3942 | |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3943 | done_index = page->index; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3944 | /* |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 3945 | * At this point we hold neither the i_pages lock nor |
| 3946 | * the page lock: the page may be truncated or |
| 3947 | * invalidated (changing page->mapping to NULL), |
| 3948 | * or even swizzled back from swapper_space to |
| 3949 | * tmpfs file mapping |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3950 | */ |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3951 | if (!trylock_page(page)) { |
David Sterba | aab6e9e | 2017-11-30 18:00:02 +0100 | [diff] [blame] | 3952 | flush_write_bio(epd); |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3953 | lock_page(page); |
Chris Mason | 01d658f | 2011-11-01 10:08:06 -0400 | [diff] [blame] | 3954 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3955 | |
| 3956 | if (unlikely(page->mapping != mapping)) { |
| 3957 | unlock_page(page); |
| 3958 | continue; |
| 3959 | } |
| 3960 | |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3961 | if (wbc->sync_mode != WB_SYNC_NONE) { |
Chris Mason | 0e6bd95 | 2008-11-20 10:46:35 -0500 | [diff] [blame] | 3962 | if (PageWriteback(page)) |
David Sterba | aab6e9e | 2017-11-30 18:00:02 +0100 | [diff] [blame] | 3963 | flush_write_bio(epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3964 | wait_on_page_writeback(page); |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3965 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3966 | |
| 3967 | if (PageWriteback(page) || |
| 3968 | !clear_page_dirty_for_io(page)) { |
| 3969 | unlock_page(page); |
| 3970 | continue; |
| 3971 | } |
| 3972 | |
David Sterba | aab6e9e | 2017-11-30 18:00:02 +0100 | [diff] [blame] | 3973 | ret = __extent_writepage(page, wbc, epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3974 | |
| 3975 | if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) { |
| 3976 | unlock_page(page); |
| 3977 | ret = 0; |
| 3978 | } |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3979 | if (ret < 0) { |
| 3980 | /* |
| 3981 | * done_index is set past this page, |
| 3982 | * so media errors will not choke |
| 3983 | * background writeout for the entire |
| 3984 | * file. This has consequences for |
| 3985 | * range_cyclic semantics (ie. it may |
| 3986 | * not be suitable for data integrity |
| 3987 | * writeout). |
| 3988 | */ |
| 3989 | done_index = page->index + 1; |
| 3990 | done = 1; |
| 3991 | break; |
| 3992 | } |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3993 | |
| 3994 | /* |
| 3995 | * the filesystem may choose to bump up nr_to_write. |
| 3996 | * We have to make sure to honor the new nr_to_write |
| 3997 | * at any time |
| 3998 | */ |
| 3999 | nr_to_write_done = wbc->nr_to_write <= 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4000 | } |
| 4001 | pagevec_release(&pvec); |
| 4002 | cond_resched(); |
| 4003 | } |
Liu Bo | 894b36e | 2016-03-07 16:56:22 -0800 | [diff] [blame] | 4004 | if (!scanned && !done) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4005 | /* |
| 4006 | * We hit the last page and there is more work to be done: wrap |
| 4007 | * back to the start of the file |
| 4008 | */ |
| 4009 | scanned = 1; |
| 4010 | index = 0; |
| 4011 | goto retry; |
| 4012 | } |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 4013 | |
| 4014 | if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole)) |
| 4015 | mapping->writeback_index = done_index; |
| 4016 | |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 4017 | btrfs_add_delayed_iput(inode); |
Liu Bo | 894b36e | 2016-03-07 16:56:22 -0800 | [diff] [blame] | 4018 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4019 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4020 | |
David Sterba | aab6e9e | 2017-11-30 18:00:02 +0100 | [diff] [blame] | 4021 | static void flush_write_bio(struct extent_page_data *epd) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4022 | { |
| 4023 | if (epd->bio) { |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 4024 | int ret; |
| 4025 | |
Liu Bo | 18fdc67 | 2017-09-13 12:18:22 -0600 | [diff] [blame] | 4026 | ret = submit_one_bio(epd->bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4027 | BUG_ON(ret < 0); /* -ENOMEM */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4028 | epd->bio = NULL; |
| 4029 | } |
| 4030 | } |
| 4031 | |
Nikolay Borisov | 0a9b0e5 | 2017-12-08 15:55:59 +0200 | [diff] [blame] | 4032 | int extent_write_full_page(struct page *page, struct writeback_control *wbc) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4033 | { |
| 4034 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4035 | struct extent_page_data epd = { |
| 4036 | .bio = NULL, |
Nikolay Borisov | 0a9b0e5 | 2017-12-08 15:55:59 +0200 | [diff] [blame] | 4037 | .tree = &BTRFS_I(page->mapping->host)->io_tree, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4038 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4039 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4040 | }; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4041 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4042 | ret = __extent_writepage(page, wbc, &epd); |
| 4043 | |
David Sterba | e2932ee | 2017-06-23 04:16:17 +0200 | [diff] [blame] | 4044 | flush_write_bio(&epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4045 | return ret; |
| 4046 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4047 | |
Nikolay Borisov | 5e3ee23 | 2017-12-08 15:55:58 +0200 | [diff] [blame] | 4048 | int extent_write_locked_range(struct inode *inode, u64 start, u64 end, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4049 | int mode) |
| 4050 | { |
| 4051 | int ret = 0; |
| 4052 | struct address_space *mapping = inode->i_mapping; |
Nikolay Borisov | 5e3ee23 | 2017-12-08 15:55:58 +0200 | [diff] [blame] | 4053 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4054 | struct page *page; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4055 | unsigned long nr_pages = (end - start + PAGE_SIZE) >> |
| 4056 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4057 | |
| 4058 | struct extent_page_data epd = { |
| 4059 | .bio = NULL, |
| 4060 | .tree = tree, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4061 | .extent_locked = 1, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4062 | .sync_io = mode == WB_SYNC_ALL, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4063 | }; |
| 4064 | struct writeback_control wbc_writepages = { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4065 | .sync_mode = mode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4066 | .nr_to_write = nr_pages * 2, |
| 4067 | .range_start = start, |
| 4068 | .range_end = end + 1, |
| 4069 | }; |
| 4070 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4071 | while (start <= end) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4072 | page = find_get_page(mapping, start >> PAGE_SHIFT); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4073 | if (clear_page_dirty_for_io(page)) |
| 4074 | ret = __extent_writepage(page, &wbc_writepages, &epd); |
| 4075 | else { |
| 4076 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 4077 | tree->ops->writepage_end_io_hook(page, start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4078 | start + PAGE_SIZE - 1, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4079 | NULL, 1); |
| 4080 | unlock_page(page); |
| 4081 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4082 | put_page(page); |
| 4083 | start += PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4084 | } |
| 4085 | |
David Sterba | e2932ee | 2017-06-23 04:16:17 +0200 | [diff] [blame] | 4086 | flush_write_bio(&epd); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4087 | return ret; |
| 4088 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4089 | |
Nikolay Borisov | 8ae225a | 2018-04-19 10:46:38 +0300 | [diff] [blame] | 4090 | int extent_writepages(struct address_space *mapping, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4091 | struct writeback_control *wbc) |
| 4092 | { |
| 4093 | int ret = 0; |
| 4094 | struct extent_page_data epd = { |
| 4095 | .bio = NULL, |
Nikolay Borisov | 8ae225a | 2018-04-19 10:46:38 +0300 | [diff] [blame] | 4096 | .tree = &BTRFS_I(mapping->host)->io_tree, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4097 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4098 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4099 | }; |
| 4100 | |
David Sterba | 935db85 | 2017-06-23 04:30:28 +0200 | [diff] [blame] | 4101 | ret = extent_write_cache_pages(mapping, wbc, &epd); |
David Sterba | e2932ee | 2017-06-23 04:16:17 +0200 | [diff] [blame] | 4102 | flush_write_bio(&epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4103 | return ret; |
| 4104 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4105 | |
Nikolay Borisov | 2a3ff0a | 2018-04-19 10:46:36 +0300 | [diff] [blame] | 4106 | int extent_readpages(struct address_space *mapping, struct list_head *pages, |
| 4107 | unsigned nr_pages) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4108 | { |
| 4109 | struct bio *bio = NULL; |
| 4110 | unsigned page_idx; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4111 | unsigned long bio_flags = 0; |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4112 | struct page *pagepool[16]; |
| 4113 | struct page *page; |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4114 | struct extent_map *em_cached = NULL; |
Nikolay Borisov | 2a3ff0a | 2018-04-19 10:46:36 +0300 | [diff] [blame] | 4115 | struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree; |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4116 | int nr = 0; |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 4117 | u64 prev_em_start = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4118 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4119 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4120 | page = list_entry(pages->prev, struct page, lru); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4121 | |
| 4122 | prefetchw(&page->flags); |
| 4123 | list_del(&page->lru); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4124 | if (add_to_page_cache_lru(page, mapping, |
Michal Hocko | 8a5c743 | 2016-07-26 15:24:53 -0700 | [diff] [blame] | 4125 | page->index, |
| 4126 | readahead_gfp_mask(mapping))) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4127 | put_page(page); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4128 | continue; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4129 | } |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4130 | |
| 4131 | pagepool[nr++] = page; |
| 4132 | if (nr < ARRAY_SIZE(pagepool)) |
| 4133 | continue; |
David Sterba | e4d17ef | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 4134 | __extent_readpages(tree, pagepool, nr, &em_cached, &bio, |
| 4135 | &bio_flags, &prev_em_start); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4136 | nr = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4137 | } |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 4138 | if (nr) |
David Sterba | e4d17ef | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 4139 | __extent_readpages(tree, pagepool, nr, &em_cached, &bio, |
| 4140 | &bio_flags, &prev_em_start); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4141 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4142 | if (em_cached) |
| 4143 | free_extent_map(em_cached); |
| 4144 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4145 | BUG_ON(!list_empty(pages)); |
| 4146 | if (bio) |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 4147 | return submit_one_bio(bio, 0, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4148 | return 0; |
| 4149 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4150 | |
| 4151 | /* |
| 4152 | * basic invalidatepage code, this waits on any locked or writeback |
| 4153 | * ranges corresponding to the page, and then deletes any extent state |
| 4154 | * records from the tree |
| 4155 | */ |
| 4156 | int extent_invalidatepage(struct extent_io_tree *tree, |
| 4157 | struct page *page, unsigned long offset) |
| 4158 | { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4159 | struct extent_state *cached_state = NULL; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4160 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4161 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4162 | size_t blocksize = page->mapping->host->i_sb->s_blocksize; |
| 4163 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4164 | start += ALIGN(offset, blocksize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4165 | if (start > end) |
| 4166 | return 0; |
| 4167 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 4168 | lock_extent_bits(tree, start, end, &cached_state); |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 4169 | wait_on_page_writeback(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4170 | clear_extent_bit(tree, start, end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 4171 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4172 | EXTENT_DO_ACCOUNTING, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 4173 | 1, 1, &cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4174 | return 0; |
| 4175 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4176 | |
| 4177 | /* |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4178 | * a helper for releasepage, this tests for areas of the page that |
| 4179 | * are locked or under IO and drops the related state bits if it is safe |
| 4180 | * to drop the page. |
| 4181 | */ |
Nikolay Borisov | 29c68b2d | 2018-04-19 10:46:35 +0300 | [diff] [blame] | 4182 | static int try_release_extent_state(struct extent_io_tree *tree, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 4183 | struct page *page, gfp_t mask) |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4184 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4185 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4186 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4187 | int ret = 1; |
| 4188 | |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 4189 | if (test_range_bit(tree, start, end, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4190 | EXTENT_IOBITS, 0, NULL)) |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4191 | ret = 0; |
| 4192 | else { |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 4193 | /* |
| 4194 | * at this point we can safely clear everything except the |
| 4195 | * locked bit and the nodatasum bit |
| 4196 | */ |
David Sterba | 66b0c88 | 2017-10-31 16:30:47 +0100 | [diff] [blame] | 4197 | ret = __clear_extent_bit(tree, start, end, |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 4198 | ~(EXTENT_LOCKED | EXTENT_NODATASUM), |
David Sterba | 66b0c88 | 2017-10-31 16:30:47 +0100 | [diff] [blame] | 4199 | 0, 0, NULL, mask, NULL); |
Chris Mason | e3f24cc | 2011-02-14 12:52:08 -0500 | [diff] [blame] | 4200 | |
| 4201 | /* if clear_extent_bit failed for enomem reasons, |
| 4202 | * we can't allow the release to continue. |
| 4203 | */ |
| 4204 | if (ret < 0) |
| 4205 | ret = 0; |
| 4206 | else |
| 4207 | ret = 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4208 | } |
| 4209 | return ret; |
| 4210 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4211 | |
| 4212 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4213 | * a helper for releasepage. As long as there are no locked extents |
| 4214 | * in the range corresponding to the page, both state records and extent |
| 4215 | * map records are removed |
| 4216 | */ |
Nikolay Borisov | 477a30b | 2018-04-19 10:46:34 +0300 | [diff] [blame] | 4217 | int try_release_extent_mapping(struct page *page, gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4218 | { |
| 4219 | struct extent_map *em; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4220 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4221 | u64 end = start + PAGE_SIZE - 1; |
Filipe Manana | bd3599a | 2018-07-12 01:36:43 +0100 | [diff] [blame] | 4222 | struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host); |
| 4223 | struct extent_io_tree *tree = &btrfs_inode->io_tree; |
| 4224 | struct extent_map_tree *map = &btrfs_inode->extent_tree; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4225 | |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 4226 | if (gfpflags_allow_blocking(mask) && |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4227 | page->mapping->host->i_size > SZ_16M) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4228 | u64 len; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4229 | while (start <= end) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4230 | len = end - start + 1; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4231 | write_lock(&map->lock); |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4232 | em = lookup_extent_mapping(map, start, len); |
Tsutomu Itoh | 285190d | 2012-02-16 16:23:58 +0900 | [diff] [blame] | 4233 | if (!em) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4234 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4235 | break; |
| 4236 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 4237 | if (test_bit(EXTENT_FLAG_PINNED, &em->flags) || |
| 4238 | em->start != start) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4239 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4240 | free_extent_map(em); |
| 4241 | break; |
| 4242 | } |
| 4243 | if (!test_range_bit(tree, em->start, |
| 4244 | extent_map_end(em) - 1, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4245 | EXTENT_LOCKED | EXTENT_WRITEBACK, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 4246 | 0, NULL)) { |
Filipe Manana | bd3599a | 2018-07-12 01:36:43 +0100 | [diff] [blame] | 4247 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4248 | &btrfs_inode->runtime_flags); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4249 | remove_extent_mapping(map, em); |
| 4250 | /* once for the rb tree */ |
| 4251 | free_extent_map(em); |
| 4252 | } |
| 4253 | start = extent_map_end(em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4254 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4255 | |
| 4256 | /* once for us */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4257 | free_extent_map(em); |
| 4258 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4259 | } |
Nikolay Borisov | 29c68b2d | 2018-04-19 10:46:35 +0300 | [diff] [blame] | 4260 | return try_release_extent_state(tree, page, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4261 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4262 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4263 | /* |
| 4264 | * helper function for fiemap, which doesn't want to see any holes. |
| 4265 | * This maps until we find something past 'last' |
| 4266 | */ |
| 4267 | static struct extent_map *get_extent_skip_holes(struct inode *inode, |
David Sterba | e3350e1 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 4268 | u64 offset, u64 last) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4269 | { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4270 | u64 sectorsize = btrfs_inode_sectorsize(inode); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4271 | struct extent_map *em; |
| 4272 | u64 len; |
| 4273 | |
| 4274 | if (offset >= last) |
| 4275 | return NULL; |
| 4276 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 4277 | while (1) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4278 | len = last - offset; |
| 4279 | if (len == 0) |
| 4280 | break; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4281 | len = ALIGN(len, sectorsize); |
David Sterba | e3350e1 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 4282 | em = btrfs_get_extent_fiemap(BTRFS_I(inode), NULL, 0, offset, |
| 4283 | len, 0); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 4284 | if (IS_ERR_OR_NULL(em)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4285 | return em; |
| 4286 | |
| 4287 | /* if this isn't a hole return it */ |
Nikolay Borisov | 4a2d25c | 2017-11-23 10:51:43 +0200 | [diff] [blame] | 4288 | if (em->block_start != EXTENT_MAP_HOLE) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4289 | return em; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4290 | |
| 4291 | /* this is a hole, advance to the next extent */ |
| 4292 | offset = extent_map_end(em); |
| 4293 | free_extent_map(em); |
| 4294 | if (offset >= last) |
| 4295 | break; |
| 4296 | } |
| 4297 | return NULL; |
| 4298 | } |
| 4299 | |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4300 | /* |
| 4301 | * To cache previous fiemap extent |
| 4302 | * |
| 4303 | * Will be used for merging fiemap extent |
| 4304 | */ |
| 4305 | struct fiemap_cache { |
| 4306 | u64 offset; |
| 4307 | u64 phys; |
| 4308 | u64 len; |
| 4309 | u32 flags; |
| 4310 | bool cached; |
| 4311 | }; |
| 4312 | |
| 4313 | /* |
| 4314 | * Helper to submit fiemap extent. |
| 4315 | * |
| 4316 | * Will try to merge current fiemap extent specified by @offset, @phys, |
| 4317 | * @len and @flags with cached one. |
| 4318 | * And only when we fails to merge, cached one will be submitted as |
| 4319 | * fiemap extent. |
| 4320 | * |
| 4321 | * Return value is the same as fiemap_fill_next_extent(). |
| 4322 | */ |
| 4323 | static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo, |
| 4324 | struct fiemap_cache *cache, |
| 4325 | u64 offset, u64 phys, u64 len, u32 flags) |
| 4326 | { |
| 4327 | int ret = 0; |
| 4328 | |
| 4329 | if (!cache->cached) |
| 4330 | goto assign; |
| 4331 | |
| 4332 | /* |
| 4333 | * Sanity check, extent_fiemap() should have ensured that new |
| 4334 | * fiemap extent won't overlap with cahced one. |
| 4335 | * Not recoverable. |
| 4336 | * |
| 4337 | * NOTE: Physical address can overlap, due to compression |
| 4338 | */ |
| 4339 | if (cache->offset + cache->len > offset) { |
| 4340 | WARN_ON(1); |
| 4341 | return -EINVAL; |
| 4342 | } |
| 4343 | |
| 4344 | /* |
| 4345 | * Only merges fiemap extents if |
| 4346 | * 1) Their logical addresses are continuous |
| 4347 | * |
| 4348 | * 2) Their physical addresses are continuous |
| 4349 | * So truly compressed (physical size smaller than logical size) |
| 4350 | * extents won't get merged with each other |
| 4351 | * |
| 4352 | * 3) Share same flags except FIEMAP_EXTENT_LAST |
| 4353 | * So regular extent won't get merged with prealloc extent |
| 4354 | */ |
| 4355 | if (cache->offset + cache->len == offset && |
| 4356 | cache->phys + cache->len == phys && |
| 4357 | (cache->flags & ~FIEMAP_EXTENT_LAST) == |
| 4358 | (flags & ~FIEMAP_EXTENT_LAST)) { |
| 4359 | cache->len += len; |
| 4360 | cache->flags |= flags; |
| 4361 | goto try_submit_last; |
| 4362 | } |
| 4363 | |
| 4364 | /* Not mergeable, need to submit cached one */ |
| 4365 | ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys, |
| 4366 | cache->len, cache->flags); |
| 4367 | cache->cached = false; |
| 4368 | if (ret) |
| 4369 | return ret; |
| 4370 | assign: |
| 4371 | cache->cached = true; |
| 4372 | cache->offset = offset; |
| 4373 | cache->phys = phys; |
| 4374 | cache->len = len; |
| 4375 | cache->flags = flags; |
| 4376 | try_submit_last: |
| 4377 | if (cache->flags & FIEMAP_EXTENT_LAST) { |
| 4378 | ret = fiemap_fill_next_extent(fieinfo, cache->offset, |
| 4379 | cache->phys, cache->len, cache->flags); |
| 4380 | cache->cached = false; |
| 4381 | } |
| 4382 | return ret; |
| 4383 | } |
| 4384 | |
| 4385 | /* |
Qu Wenruo | 848c23b | 2017-06-22 10:01:21 +0800 | [diff] [blame] | 4386 | * Emit last fiemap cache |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4387 | * |
Qu Wenruo | 848c23b | 2017-06-22 10:01:21 +0800 | [diff] [blame] | 4388 | * The last fiemap cache may still be cached in the following case: |
| 4389 | * 0 4k 8k |
| 4390 | * |<- Fiemap range ->| |
| 4391 | * |<------------ First extent ----------->| |
| 4392 | * |
| 4393 | * In this case, the first extent range will be cached but not emitted. |
| 4394 | * So we must emit it before ending extent_fiemap(). |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4395 | */ |
Qu Wenruo | 848c23b | 2017-06-22 10:01:21 +0800 | [diff] [blame] | 4396 | static int emit_last_fiemap_cache(struct btrfs_fs_info *fs_info, |
| 4397 | struct fiemap_extent_info *fieinfo, |
| 4398 | struct fiemap_cache *cache) |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4399 | { |
| 4400 | int ret; |
| 4401 | |
| 4402 | if (!cache->cached) |
| 4403 | return 0; |
| 4404 | |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4405 | ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys, |
| 4406 | cache->len, cache->flags); |
| 4407 | cache->cached = false; |
| 4408 | if (ret > 0) |
| 4409 | ret = 0; |
| 4410 | return ret; |
| 4411 | } |
| 4412 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4413 | int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
David Sterba | 2135fb9 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 4414 | __u64 start, __u64 len) |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4415 | { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4416 | int ret = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4417 | u64 off = start; |
| 4418 | u64 max = start + len; |
| 4419 | u32 flags = 0; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4420 | u32 found_type; |
| 4421 | u64 last; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4422 | u64 last_for_get_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4423 | u64 disko = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4424 | u64 isize = i_size_read(inode); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4425 | struct btrfs_key found_key; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4426 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4427 | struct extent_state *cached_state = NULL; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4428 | struct btrfs_path *path; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4429 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4430 | struct fiemap_cache cache = { 0 }; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4431 | int end = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4432 | u64 em_start = 0; |
| 4433 | u64 em_len = 0; |
| 4434 | u64 em_end = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4435 | |
| 4436 | if (len == 0) |
| 4437 | return -EINVAL; |
| 4438 | |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4439 | path = btrfs_alloc_path(); |
| 4440 | if (!path) |
| 4441 | return -ENOMEM; |
| 4442 | path->leave_spinning = 1; |
| 4443 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4444 | start = round_down(start, btrfs_inode_sectorsize(inode)); |
| 4445 | len = round_up(max, btrfs_inode_sectorsize(inode)) - start; |
Josef Bacik | 4d479cf | 2011-11-17 11:34:31 -0500 | [diff] [blame] | 4446 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4447 | /* |
| 4448 | * lookup the last file extent. We're not using i_size here |
| 4449 | * because there might be preallocation past i_size |
| 4450 | */ |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4451 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 4452 | btrfs_ino(BTRFS_I(inode)), -1, 0); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4453 | if (ret < 0) { |
| 4454 | btrfs_free_path(path); |
| 4455 | return ret; |
Liu Bo | 2d324f5 | 2016-05-17 17:21:48 -0700 | [diff] [blame] | 4456 | } else { |
| 4457 | WARN_ON(!ret); |
| 4458 | if (ret == 1) |
| 4459 | ret = 0; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4460 | } |
Liu Bo | 2d324f5 | 2016-05-17 17:21:48 -0700 | [diff] [blame] | 4461 | |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4462 | path->slots[0]--; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4463 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4464 | found_type = found_key.type; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4465 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4466 | /* No extents, but there might be delalloc bits */ |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4467 | if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) || |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4468 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4469 | /* have to trust i_size as the end */ |
| 4470 | last = (u64)-1; |
| 4471 | last_for_get_extent = isize; |
| 4472 | } else { |
| 4473 | /* |
| 4474 | * remember the start of the last extent. There are a |
| 4475 | * bunch of different factors that go into the length of the |
| 4476 | * extent, so its much less complex to remember where it started |
| 4477 | */ |
| 4478 | last = found_key.offset; |
| 4479 | last_for_get_extent = last + 1; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4480 | } |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4481 | btrfs_release_path(path); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4482 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4483 | /* |
| 4484 | * we might have some extents allocated but more delalloc past those |
| 4485 | * extents. so, we trust isize unless the start of the last extent is |
| 4486 | * beyond isize |
| 4487 | */ |
| 4488 | if (last < isize) { |
| 4489 | last = (u64)-1; |
| 4490 | last_for_get_extent = isize; |
| 4491 | } |
| 4492 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 4493 | lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4494 | &cached_state); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4495 | |
David Sterba | e3350e1 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 4496 | em = get_extent_skip_holes(inode, start, last_for_get_extent); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4497 | if (!em) |
| 4498 | goto out; |
| 4499 | if (IS_ERR(em)) { |
| 4500 | ret = PTR_ERR(em); |
| 4501 | goto out; |
| 4502 | } |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4503 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4504 | while (!end) { |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4505 | u64 offset_in_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4506 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4507 | /* break if the extent we found is outside the range */ |
| 4508 | if (em->start >= max || extent_map_end(em) < off) |
| 4509 | break; |
| 4510 | |
| 4511 | /* |
| 4512 | * get_extent may return an extent that starts before our |
| 4513 | * requested range. We have to make sure the ranges |
| 4514 | * we return to fiemap always move forward and don't |
| 4515 | * overlap, so adjust the offsets here |
| 4516 | */ |
| 4517 | em_start = max(em->start, off); |
| 4518 | |
| 4519 | /* |
| 4520 | * record the offset from the start of the extent |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4521 | * for adjusting the disk offset below. Only do this if the |
| 4522 | * extent isn't compressed since our in ram offset may be past |
| 4523 | * what we have actually allocated on disk. |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4524 | */ |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4525 | if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 4526 | offset_in_extent = em_start - em->start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4527 | em_end = extent_map_end(em); |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4528 | em_len = em_end - em_start; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4529 | flags = 0; |
Filipe Manana | f098631 | 2018-06-20 10:02:30 +0100 | [diff] [blame] | 4530 | if (em->block_start < EXTENT_MAP_LAST_BYTE) |
| 4531 | disko = em->block_start + offset_in_extent; |
| 4532 | else |
| 4533 | disko = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4534 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4535 | /* |
| 4536 | * bump off for our next call to get_extent |
| 4537 | */ |
| 4538 | off = extent_map_end(em); |
| 4539 | if (off >= max) |
| 4540 | end = 1; |
| 4541 | |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4542 | if (em->block_start == EXTENT_MAP_LAST_BYTE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4543 | end = 1; |
| 4544 | flags |= FIEMAP_EXTENT_LAST; |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4545 | } else if (em->block_start == EXTENT_MAP_INLINE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4546 | flags |= (FIEMAP_EXTENT_DATA_INLINE | |
| 4547 | FIEMAP_EXTENT_NOT_ALIGNED); |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4548 | } else if (em->block_start == EXTENT_MAP_DELALLOC) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4549 | flags |= (FIEMAP_EXTENT_DELALLOC | |
| 4550 | FIEMAP_EXTENT_UNKNOWN); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4551 | } else if (fieinfo->fi_extents_max) { |
| 4552 | u64 bytenr = em->block_start - |
| 4553 | (em->start - em->orig_start); |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4554 | |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4555 | /* |
| 4556 | * As btrfs supports shared space, this information |
| 4557 | * can be exported to userspace tools via |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4558 | * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0 |
| 4559 | * then we're just getting a count and we can skip the |
| 4560 | * lookup stuff. |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4561 | */ |
Edmund Nadolski | bb739cf | 2017-06-28 21:56:58 -0600 | [diff] [blame] | 4562 | ret = btrfs_check_shared(root, |
| 4563 | btrfs_ino(BTRFS_I(inode)), |
| 4564 | bytenr); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4565 | if (ret < 0) |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4566 | goto out_free; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4567 | if (ret) |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4568 | flags |= FIEMAP_EXTENT_SHARED; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4569 | ret = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4570 | } |
| 4571 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 4572 | flags |= FIEMAP_EXTENT_ENCODED; |
Josef Bacik | 0d2b237 | 2015-05-19 10:44:04 -0400 | [diff] [blame] | 4573 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 4574 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4575 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4576 | free_extent_map(em); |
| 4577 | em = NULL; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4578 | if ((em_start >= last) || em_len == (u64)-1 || |
| 4579 | (last == (u64)-1 && isize <= em_end)) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4580 | flags |= FIEMAP_EXTENT_LAST; |
| 4581 | end = 1; |
| 4582 | } |
| 4583 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4584 | /* now scan forward to see if this is really the last extent. */ |
David Sterba | e3350e1 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 4585 | em = get_extent_skip_holes(inode, off, last_for_get_extent); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4586 | if (IS_ERR(em)) { |
| 4587 | ret = PTR_ERR(em); |
| 4588 | goto out; |
| 4589 | } |
| 4590 | if (!em) { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4591 | flags |= FIEMAP_EXTENT_LAST; |
| 4592 | end = 1; |
| 4593 | } |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4594 | ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko, |
| 4595 | em_len, flags); |
Chengyu Song | 26e726a | 2015-03-24 18:12:56 -0400 | [diff] [blame] | 4596 | if (ret) { |
| 4597 | if (ret == 1) |
| 4598 | ret = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4599 | goto out_free; |
Chengyu Song | 26e726a | 2015-03-24 18:12:56 -0400 | [diff] [blame] | 4600 | } |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4601 | } |
| 4602 | out_free: |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4603 | if (!ret) |
Qu Wenruo | 848c23b | 2017-06-22 10:01:21 +0800 | [diff] [blame] | 4604 | ret = emit_last_fiemap_cache(root->fs_info, fieinfo, &cache); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4605 | free_extent_map(em); |
| 4606 | out: |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4607 | btrfs_free_path(path); |
Liu Bo | a52f4cd | 2013-05-01 16:23:41 +0000 | [diff] [blame] | 4608 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4609 | &cached_state); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4610 | return ret; |
| 4611 | } |
| 4612 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4613 | static void __free_extent_buffer(struct extent_buffer *eb) |
| 4614 | { |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 4615 | btrfs_leak_debug_del(&eb->leak_list); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4616 | kmem_cache_free(extent_buffer_cache, eb); |
| 4617 | } |
| 4618 | |
Josef Bacik | a26e8c9 | 2014-03-28 17:07:27 -0400 | [diff] [blame] | 4619 | int extent_buffer_under_io(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4620 | { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4621 | return (atomic_read(&eb->io_pages) || |
| 4622 | test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) || |
| 4623 | test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4624 | } |
| 4625 | |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4626 | /* |
David Sterba | 55ac013 | 2018-07-19 17:24:32 +0200 | [diff] [blame] | 4627 | * Release all pages attached to the extent buffer. |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4628 | */ |
David Sterba | 55ac013 | 2018-07-19 17:24:32 +0200 | [diff] [blame] | 4629 | static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb) |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4630 | { |
Nikolay Borisov | d64766f | 2018-06-27 16:38:22 +0300 | [diff] [blame] | 4631 | int i; |
| 4632 | int num_pages; |
Nikolay Borisov | b0132a3 | 2018-06-27 16:38:24 +0300 | [diff] [blame] | 4633 | int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4634 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4635 | BUG_ON(extent_buffer_under_io(eb)); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4636 | |
Nikolay Borisov | d64766f | 2018-06-27 16:38:22 +0300 | [diff] [blame] | 4637 | num_pages = num_extent_pages(eb); |
| 4638 | for (i = 0; i < num_pages; i++) { |
| 4639 | struct page *page = eb->pages[i]; |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4640 | |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4641 | if (!page) |
| 4642 | continue; |
| 4643 | if (mapped) |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4644 | spin_lock(&page->mapping->private_lock); |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4645 | /* |
| 4646 | * We do this since we'll remove the pages after we've |
| 4647 | * removed the eb from the radix tree, so we could race |
| 4648 | * and have this page now attached to the new eb. So |
| 4649 | * only clear page_private if it's still connected to |
| 4650 | * this eb. |
| 4651 | */ |
| 4652 | if (PagePrivate(page) && |
| 4653 | page->private == (unsigned long)eb) { |
| 4654 | BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
| 4655 | BUG_ON(PageDirty(page)); |
| 4656 | BUG_ON(PageWriteback(page)); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4657 | /* |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4658 | * We need to make sure we haven't be attached |
| 4659 | * to a new eb. |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4660 | */ |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4661 | ClearPagePrivate(page); |
| 4662 | set_page_private(page, 0); |
| 4663 | /* One for the page private */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4664 | put_page(page); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4665 | } |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4666 | |
| 4667 | if (mapped) |
| 4668 | spin_unlock(&page->mapping->private_lock); |
| 4669 | |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 4670 | /* One for when we allocated the page */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4671 | put_page(page); |
Nikolay Borisov | d64766f | 2018-06-27 16:38:22 +0300 | [diff] [blame] | 4672 | } |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4673 | } |
| 4674 | |
| 4675 | /* |
| 4676 | * Helper for releasing the extent buffer. |
| 4677 | */ |
| 4678 | static inline void btrfs_release_extent_buffer(struct extent_buffer *eb) |
| 4679 | { |
David Sterba | 55ac013 | 2018-07-19 17:24:32 +0200 | [diff] [blame] | 4680 | btrfs_release_extent_buffer_pages(eb); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4681 | __free_extent_buffer(eb); |
| 4682 | } |
| 4683 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4684 | static struct extent_buffer * |
| 4685 | __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, |
David Sterba | 23d79d8 | 2014-06-15 02:55:29 +0200 | [diff] [blame] | 4686 | unsigned long len) |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4687 | { |
| 4688 | struct extent_buffer *eb = NULL; |
| 4689 | |
Michal Hocko | d1b5c56 | 2015-08-19 14:17:40 +0200 | [diff] [blame] | 4690 | eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4691 | eb->start = start; |
| 4692 | eb->len = len; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4693 | eb->fs_info = fs_info; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4694 | eb->bflags = 0; |
| 4695 | rwlock_init(&eb->lock); |
| 4696 | atomic_set(&eb->write_locks, 0); |
| 4697 | atomic_set(&eb->read_locks, 0); |
| 4698 | atomic_set(&eb->blocking_readers, 0); |
| 4699 | atomic_set(&eb->blocking_writers, 0); |
| 4700 | atomic_set(&eb->spinning_readers, 0); |
| 4701 | atomic_set(&eb->spinning_writers, 0); |
| 4702 | eb->lock_nested = 0; |
| 4703 | init_waitqueue_head(&eb->write_lock_wq); |
| 4704 | init_waitqueue_head(&eb->read_lock_wq); |
| 4705 | |
| 4706 | btrfs_leak_debug_add(&eb->leak_list, &buffers); |
| 4707 | |
| 4708 | spin_lock_init(&eb->refs_lock); |
| 4709 | atomic_set(&eb->refs, 1); |
| 4710 | atomic_set(&eb->io_pages, 0); |
| 4711 | |
| 4712 | /* |
| 4713 | * Sanity checks, currently the maximum is 64k covered by 16x 4k pages |
| 4714 | */ |
| 4715 | BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE |
| 4716 | > MAX_INLINE_EXTENT_BUFFER_SIZE); |
| 4717 | BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE); |
| 4718 | |
| 4719 | return eb; |
| 4720 | } |
| 4721 | |
| 4722 | struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src) |
| 4723 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 4724 | int i; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4725 | struct page *p; |
| 4726 | struct extent_buffer *new; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 4727 | int num_pages = num_extent_pages(src); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4728 | |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4729 | new = __alloc_extent_buffer(src->fs_info, src->start, src->len); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4730 | if (new == NULL) |
| 4731 | return NULL; |
| 4732 | |
| 4733 | for (i = 0; i < num_pages; i++) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 4734 | p = alloc_page(GFP_NOFS); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4735 | if (!p) { |
| 4736 | btrfs_release_extent_buffer(new); |
| 4737 | return NULL; |
| 4738 | } |
| 4739 | attach_extent_buffer_page(new, p); |
| 4740 | WARN_ON(PageDirty(p)); |
| 4741 | SetPageUptodate(p); |
| 4742 | new->pages[i] = p; |
David Sterba | fba1acf | 2016-11-08 17:56:24 +0100 | [diff] [blame] | 4743 | copy_page(page_address(p), page_address(src->pages[i])); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4744 | } |
| 4745 | |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4746 | set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags); |
Nikolay Borisov | b0132a3 | 2018-06-27 16:38:24 +0300 | [diff] [blame] | 4747 | set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4748 | |
| 4749 | return new; |
| 4750 | } |
| 4751 | |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4752 | struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, |
| 4753 | u64 start, unsigned long len) |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4754 | { |
| 4755 | struct extent_buffer *eb; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 4756 | int num_pages; |
| 4757 | int i; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4758 | |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4759 | eb = __alloc_extent_buffer(fs_info, start, len); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4760 | if (!eb) |
| 4761 | return NULL; |
| 4762 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 4763 | num_pages = num_extent_pages(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4764 | for (i = 0; i < num_pages; i++) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 4765 | eb->pages[i] = alloc_page(GFP_NOFS); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4766 | if (!eb->pages[i]) |
| 4767 | goto err; |
| 4768 | } |
| 4769 | set_extent_buffer_uptodate(eb); |
| 4770 | btrfs_set_header_nritems(eb, 0); |
Nikolay Borisov | b0132a3 | 2018-06-27 16:38:24 +0300 | [diff] [blame] | 4771 | set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4772 | |
| 4773 | return eb; |
| 4774 | err: |
| 4775 | for (; i > 0; i--) |
| 4776 | __free_page(eb->pages[i - 1]); |
| 4777 | __free_extent_buffer(eb); |
| 4778 | return NULL; |
| 4779 | } |
| 4780 | |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4781 | struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4782 | u64 start) |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4783 | { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4784 | return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize); |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4785 | } |
| 4786 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4787 | static void check_buffer_tree_ref(struct extent_buffer *eb) |
| 4788 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4789 | int refs; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4790 | /* the ref bit is tricky. We have to make sure it is set |
| 4791 | * if we have the buffer dirty. Otherwise the |
| 4792 | * code to free a buffer can end up dropping a dirty |
| 4793 | * page |
| 4794 | * |
| 4795 | * Once the ref bit is set, it won't go away while the |
| 4796 | * buffer is dirty or in writeback, and it also won't |
| 4797 | * go away while we have the reference count on the |
| 4798 | * eb bumped. |
| 4799 | * |
| 4800 | * We can't just set the ref bit without bumping the |
| 4801 | * ref on the eb because free_extent_buffer might |
| 4802 | * see the ref bit and try to clear it. If this happens |
| 4803 | * free_extent_buffer might end up dropping our original |
| 4804 | * ref by mistake and freeing the page before we are able |
| 4805 | * to add one more ref. |
| 4806 | * |
| 4807 | * So bump the ref count first, then set the bit. If someone |
| 4808 | * beat us to it, drop the ref we added. |
| 4809 | */ |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4810 | refs = atomic_read(&eb->refs); |
| 4811 | if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 4812 | return; |
| 4813 | |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4814 | spin_lock(&eb->refs_lock); |
| 4815 | if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4816 | atomic_inc(&eb->refs); |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4817 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4818 | } |
| 4819 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4820 | static void mark_extent_buffer_accessed(struct extent_buffer *eb, |
| 4821 | struct page *accessed) |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4822 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 4823 | int num_pages, i; |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4824 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4825 | check_buffer_tree_ref(eb); |
| 4826 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 4827 | num_pages = num_extent_pages(eb); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4828 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 4829 | struct page *p = eb->pages[i]; |
| 4830 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4831 | if (p != accessed) |
| 4832 | mark_page_accessed(p); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4833 | } |
| 4834 | } |
| 4835 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4836 | struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info, |
| 4837 | u64 start) |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4838 | { |
| 4839 | struct extent_buffer *eb; |
| 4840 | |
| 4841 | rcu_read_lock(); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4842 | eb = radix_tree_lookup(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4843 | start >> PAGE_SHIFT); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4844 | if (eb && atomic_inc_not_zero(&eb->refs)) { |
| 4845 | rcu_read_unlock(); |
Filipe Manana | 062c19e | 2015-04-23 11:28:48 +0100 | [diff] [blame] | 4846 | /* |
| 4847 | * Lock our eb's refs_lock to avoid races with |
| 4848 | * free_extent_buffer. When we get our eb it might be flagged |
| 4849 | * with EXTENT_BUFFER_STALE and another task running |
| 4850 | * free_extent_buffer might have seen that flag set, |
| 4851 | * eb->refs == 2, that the buffer isn't under IO (dirty and |
| 4852 | * writeback flags not set) and it's still in the tree (flag |
| 4853 | * EXTENT_BUFFER_TREE_REF set), therefore being in the process |
| 4854 | * of decrementing the extent buffer's reference count twice. |
| 4855 | * So here we could race and increment the eb's reference count, |
| 4856 | * clear its stale flag, mark it as dirty and drop our reference |
| 4857 | * before the other task finishes executing free_extent_buffer, |
| 4858 | * which would later result in an attempt to free an extent |
| 4859 | * buffer that is dirty. |
| 4860 | */ |
| 4861 | if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) { |
| 4862 | spin_lock(&eb->refs_lock); |
| 4863 | spin_unlock(&eb->refs_lock); |
| 4864 | } |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4865 | mark_extent_buffer_accessed(eb, NULL); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4866 | return eb; |
| 4867 | } |
| 4868 | rcu_read_unlock(); |
| 4869 | |
| 4870 | return NULL; |
| 4871 | } |
| 4872 | |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4873 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 4874 | struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4875 | u64 start) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4876 | { |
| 4877 | struct extent_buffer *eb, *exists = NULL; |
| 4878 | int ret; |
| 4879 | |
| 4880 | eb = find_extent_buffer(fs_info, start); |
| 4881 | if (eb) |
| 4882 | return eb; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4883 | eb = alloc_dummy_extent_buffer(fs_info, start); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4884 | if (!eb) |
| 4885 | return NULL; |
| 4886 | eb->fs_info = fs_info; |
| 4887 | again: |
David Sterba | e1860a7 | 2016-05-09 14:11:38 +0200 | [diff] [blame] | 4888 | ret = radix_tree_preload(GFP_NOFS); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4889 | if (ret) |
| 4890 | goto free_eb; |
| 4891 | spin_lock(&fs_info->buffer_lock); |
| 4892 | ret = radix_tree_insert(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4893 | start >> PAGE_SHIFT, eb); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4894 | spin_unlock(&fs_info->buffer_lock); |
| 4895 | radix_tree_preload_end(); |
| 4896 | if (ret == -EEXIST) { |
| 4897 | exists = find_extent_buffer(fs_info, start); |
| 4898 | if (exists) |
| 4899 | goto free_eb; |
| 4900 | else |
| 4901 | goto again; |
| 4902 | } |
| 4903 | check_buffer_tree_ref(eb); |
| 4904 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
| 4905 | |
| 4906 | /* |
| 4907 | * We will free dummy extent buffer's if they come into |
| 4908 | * free_extent_buffer with a ref count of 2, but if we are using this we |
| 4909 | * want the buffers to stay in memory until we're done with them, so |
| 4910 | * bump the ref count again. |
| 4911 | */ |
| 4912 | atomic_inc(&eb->refs); |
| 4913 | return eb; |
| 4914 | free_eb: |
| 4915 | btrfs_release_extent_buffer(eb); |
| 4916 | return exists; |
| 4917 | } |
| 4918 | #endif |
| 4919 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4920 | struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, |
David Sterba | ce3e698 | 2014-06-15 03:00:04 +0200 | [diff] [blame] | 4921 | u64 start) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4922 | { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4923 | unsigned long len = fs_info->nodesize; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 4924 | int num_pages; |
| 4925 | int i; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4926 | unsigned long index = start >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4927 | struct extent_buffer *eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4928 | struct extent_buffer *exists = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4929 | struct page *p; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4930 | struct address_space *mapping = fs_info->btree_inode->i_mapping; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4931 | int uptodate = 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4932 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4933 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4934 | if (!IS_ALIGNED(start, fs_info->sectorsize)) { |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4935 | btrfs_err(fs_info, "bad tree block start %llu", start); |
| 4936 | return ERR_PTR(-EINVAL); |
| 4937 | } |
| 4938 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4939 | eb = find_extent_buffer(fs_info, start); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4940 | if (eb) |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4941 | return eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4942 | |
David Sterba | 23d79d8 | 2014-06-15 02:55:29 +0200 | [diff] [blame] | 4943 | eb = __alloc_extent_buffer(fs_info, start, len); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 4944 | if (!eb) |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4945 | return ERR_PTR(-ENOMEM); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4946 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 4947 | num_pages = num_extent_pages(eb); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4948 | for (i = 0; i < num_pages; i++, index++) { |
Michal Hocko | d1b5c56 | 2015-08-19 14:17:40 +0200 | [diff] [blame] | 4949 | p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4950 | if (!p) { |
| 4951 | exists = ERR_PTR(-ENOMEM); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4952 | goto free_eb; |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4953 | } |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4954 | |
| 4955 | spin_lock(&mapping->private_lock); |
| 4956 | if (PagePrivate(p)) { |
| 4957 | /* |
| 4958 | * We could have already allocated an eb for this page |
| 4959 | * and attached one so lets see if we can get a ref on |
| 4960 | * the existing eb, and if we can we know it's good and |
| 4961 | * we can just return that one, else we know we can just |
| 4962 | * overwrite page->private. |
| 4963 | */ |
| 4964 | exists = (struct extent_buffer *)p->private; |
| 4965 | if (atomic_inc_not_zero(&exists->refs)) { |
| 4966 | spin_unlock(&mapping->private_lock); |
| 4967 | unlock_page(p); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4968 | put_page(p); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4969 | mark_extent_buffer_accessed(exists, p); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4970 | goto free_eb; |
| 4971 | } |
Omar Sandoval | 5ca64f4 | 2015-02-24 02:47:05 -0800 | [diff] [blame] | 4972 | exists = NULL; |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4973 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4974 | /* |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4975 | * Do this so attach doesn't complain and we need to |
| 4976 | * drop the ref the old guy had. |
| 4977 | */ |
| 4978 | ClearPagePrivate(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4979 | WARN_ON(PageDirty(p)); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4980 | put_page(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4981 | } |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4982 | attach_extent_buffer_page(eb, p); |
| 4983 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4984 | WARN_ON(PageDirty(p)); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4985 | eb->pages[i] = p; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4986 | if (!PageUptodate(p)) |
| 4987 | uptodate = 0; |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 4988 | |
| 4989 | /* |
Nikolay Borisov | b16d011 | 2018-07-04 10:24:52 +0300 | [diff] [blame] | 4990 | * We can't unlock the pages just yet since the extent buffer |
| 4991 | * hasn't been properly inserted in the radix tree, this |
| 4992 | * opens a race with btree_releasepage which can free a page |
| 4993 | * while we are still filling in all pages for the buffer and |
| 4994 | * we could crash. |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 4995 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4996 | } |
| 4997 | if (uptodate) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4998 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 4999 | again: |
David Sterba | e1860a7 | 2016-05-09 14:11:38 +0200 | [diff] [blame] | 5000 | ret = radix_tree_preload(GFP_NOFS); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 5001 | if (ret) { |
| 5002 | exists = ERR_PTR(ret); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5003 | goto free_eb; |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 5004 | } |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5005 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5006 | spin_lock(&fs_info->buffer_lock); |
| 5007 | ret = radix_tree_insert(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5008 | start >> PAGE_SHIFT, eb); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5009 | spin_unlock(&fs_info->buffer_lock); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 5010 | radix_tree_preload_end(); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5011 | if (ret == -EEXIST) { |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5012 | exists = find_extent_buffer(fs_info, start); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 5013 | if (exists) |
| 5014 | goto free_eb; |
| 5015 | else |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 5016 | goto again; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5017 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5018 | /* add one reference for the tree */ |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5019 | check_buffer_tree_ref(eb); |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 5020 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5021 | |
| 5022 | /* |
Nikolay Borisov | b16d011 | 2018-07-04 10:24:52 +0300 | [diff] [blame] | 5023 | * Now it's safe to unlock the pages because any calls to |
| 5024 | * btree_releasepage will correctly detect that a page belongs to a |
| 5025 | * live buffer and won't free them prematurely. |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5026 | */ |
Nikolay Borisov | 28187ae | 2018-07-04 10:24:51 +0300 | [diff] [blame] | 5027 | for (i = 0; i < num_pages; i++) |
| 5028 | unlock_page(eb->pages[i]); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5029 | return eb; |
| 5030 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5031 | free_eb: |
Omar Sandoval | 5ca64f4 | 2015-02-24 02:47:05 -0800 | [diff] [blame] | 5032 | WARN_ON(!atomic_dec_and_test(&eb->refs)); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5033 | for (i = 0; i < num_pages; i++) { |
| 5034 | if (eb->pages[i]) |
| 5035 | unlock_page(eb->pages[i]); |
| 5036 | } |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5037 | |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5038 | btrfs_release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5039 | return exists; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5040 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5041 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5042 | static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head) |
| 5043 | { |
| 5044 | struct extent_buffer *eb = |
| 5045 | container_of(head, struct extent_buffer, rcu_head); |
| 5046 | |
| 5047 | __free_extent_buffer(eb); |
| 5048 | } |
| 5049 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5050 | static int release_extent_buffer(struct extent_buffer *eb) |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5051 | { |
Nikolay Borisov | 07e21c4 | 2018-06-27 16:38:23 +0300 | [diff] [blame] | 5052 | lockdep_assert_held(&eb->refs_lock); |
| 5053 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5054 | WARN_ON(atomic_read(&eb->refs) == 0); |
| 5055 | if (atomic_dec_and_test(&eb->refs)) { |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 5056 | if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) { |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5057 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5058 | |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 5059 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5060 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5061 | spin_lock(&fs_info->buffer_lock); |
| 5062 | radix_tree_delete(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5063 | eb->start >> PAGE_SHIFT); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5064 | spin_unlock(&fs_info->buffer_lock); |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 5065 | } else { |
| 5066 | spin_unlock(&eb->refs_lock); |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 5067 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5068 | |
| 5069 | /* Should be safe to release our pages at this point */ |
David Sterba | 55ac013 | 2018-07-19 17:24:32 +0200 | [diff] [blame] | 5070 | btrfs_release_extent_buffer_pages(eb); |
Josef Bacik | bcb7e44 | 2015-03-16 17:38:02 -0400 | [diff] [blame] | 5071 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
Nikolay Borisov | b0132a3 | 2018-06-27 16:38:24 +0300 | [diff] [blame] | 5072 | if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) { |
Josef Bacik | bcb7e44 | 2015-03-16 17:38:02 -0400 | [diff] [blame] | 5073 | __free_extent_buffer(eb); |
| 5074 | return 1; |
| 5075 | } |
| 5076 | #endif |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5077 | call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 5078 | return 1; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5079 | } |
| 5080 | spin_unlock(&eb->refs_lock); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 5081 | |
| 5082 | return 0; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5083 | } |
| 5084 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5085 | void free_extent_buffer(struct extent_buffer *eb) |
| 5086 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 5087 | int refs; |
| 5088 | int old; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5089 | if (!eb) |
| 5090 | return; |
| 5091 | |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 5092 | while (1) { |
| 5093 | refs = atomic_read(&eb->refs); |
| 5094 | if (refs <= 3) |
| 5095 | break; |
| 5096 | old = atomic_cmpxchg(&eb->refs, refs, refs - 1); |
| 5097 | if (old == refs) |
| 5098 | return; |
| 5099 | } |
| 5100 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5101 | spin_lock(&eb->refs_lock); |
| 5102 | if (atomic_read(&eb->refs) == 2 && |
Nikolay Borisov | b0132a3 | 2018-06-27 16:38:24 +0300 | [diff] [blame] | 5103 | test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags)) |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 5104 | atomic_dec(&eb->refs); |
| 5105 | |
| 5106 | if (atomic_read(&eb->refs) == 2 && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5107 | test_bit(EXTENT_BUFFER_STALE, &eb->bflags) && |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5108 | !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5109 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 5110 | atomic_dec(&eb->refs); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5111 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5112 | /* |
| 5113 | * I know this is terrible, but it's temporary until we stop tracking |
| 5114 | * the uptodate bits and such for the extent buffers. |
| 5115 | */ |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5116 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5117 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5118 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5119 | void free_extent_buffer_stale(struct extent_buffer *eb) |
| 5120 | { |
| 5121 | if (!eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5122 | return; |
| 5123 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5124 | spin_lock(&eb->refs_lock); |
| 5125 | set_bit(EXTENT_BUFFER_STALE, &eb->bflags); |
| 5126 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5127 | if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5128 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 5129 | atomic_dec(&eb->refs); |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5130 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5131 | } |
| 5132 | |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 5133 | void clear_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5134 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5135 | int i; |
| 5136 | int num_pages; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5137 | struct page *page; |
| 5138 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5139 | num_pages = num_extent_pages(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5140 | |
| 5141 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5142 | page = eb->pages[i]; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5143 | if (!PageDirty(page)) |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 5144 | continue; |
| 5145 | |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 5146 | lock_page(page); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5147 | WARN_ON(!PagePrivate(page)); |
| 5148 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5149 | clear_page_dirty_for_io(page); |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 5150 | xa_lock_irq(&page->mapping->i_pages); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5151 | if (!PageDirty(page)) { |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 5152 | radix_tree_tag_clear(&page->mapping->i_pages, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5153 | page_index(page), |
| 5154 | PAGECACHE_TAG_DIRTY); |
| 5155 | } |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 5156 | xa_unlock_irq(&page->mapping->i_pages); |
Chris Mason | bf0da8c | 2011-11-04 12:29:37 -0400 | [diff] [blame] | 5157 | ClearPageError(page); |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 5158 | unlock_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5159 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5160 | WARN_ON(atomic_read(&eb->refs) == 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5161 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5162 | |
Liu Bo | abb57ef | 2018-09-14 01:44:42 +0800 | [diff] [blame^] | 5163 | bool set_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5164 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5165 | int i; |
| 5166 | int num_pages; |
Liu Bo | abb57ef | 2018-09-14 01:44:42 +0800 | [diff] [blame^] | 5167 | bool was_dirty; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5168 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5169 | check_buffer_tree_ref(eb); |
| 5170 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5171 | was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5172 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5173 | num_pages = num_extent_pages(eb); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5174 | WARN_ON(atomic_read(&eb->refs) == 0); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5175 | WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)); |
| 5176 | |
Liu Bo | abb57ef | 2018-09-14 01:44:42 +0800 | [diff] [blame^] | 5177 | if (!was_dirty) |
| 5178 | for (i = 0; i < num_pages; i++) |
| 5179 | set_page_dirty(eb->pages[i]); |
Liu Bo | 51995c3 | 2018-09-14 01:46:08 +0800 | [diff] [blame] | 5180 | |
| 5181 | #ifdef CONFIG_BTRFS_DEBUG |
| 5182 | for (i = 0; i < num_pages; i++) |
| 5183 | ASSERT(PageDirty(eb->pages[i])); |
| 5184 | #endif |
| 5185 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5186 | return was_dirty; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5187 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5188 | |
David Sterba | 69ba392 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 5189 | void clear_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5190 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5191 | int i; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5192 | struct page *page; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5193 | int num_pages; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5194 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5195 | clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5196 | num_pages = num_extent_pages(eb); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5197 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5198 | page = eb->pages[i]; |
Chris Mason | 33958dc | 2008-07-30 10:29:12 -0400 | [diff] [blame] | 5199 | if (page) |
| 5200 | ClearPageUptodate(page); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5201 | } |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5202 | } |
| 5203 | |
David Sterba | 09c25a8 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 5204 | void set_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5205 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5206 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5207 | struct page *page; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5208 | int num_pages; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5209 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5210 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5211 | num_pages = num_extent_pages(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5212 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5213 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5214 | SetPageUptodate(page); |
| 5215 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5216 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5217 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5218 | int read_extent_buffer_pages(struct extent_io_tree *tree, |
David Sterba | 6af49db | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 5219 | struct extent_buffer *eb, int wait, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5220 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5221 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5222 | struct page *page; |
| 5223 | int err; |
| 5224 | int ret = 0; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5225 | int locked_pages = 0; |
| 5226 | int all_uptodate = 1; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5227 | int num_pages; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5228 | unsigned long num_reads = 0; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5229 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5230 | unsigned long bio_flags = 0; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5231 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5232 | if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5233 | return 0; |
| 5234 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5235 | num_pages = num_extent_pages(eb); |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5236 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5237 | page = eb->pages[i]; |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 5238 | if (wait == WAIT_NONE) { |
David Woodhouse | 2db0496 | 2008-08-07 11:19:43 -0400 | [diff] [blame] | 5239 | if (!trylock_page(page)) |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5240 | goto unlock_exit; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5241 | } else { |
| 5242 | lock_page(page); |
| 5243 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5244 | locked_pages++; |
Liu Bo | 2571e73 | 2016-08-03 12:33:01 -0700 | [diff] [blame] | 5245 | } |
| 5246 | /* |
| 5247 | * We need to firstly lock all pages to make sure that |
| 5248 | * the uptodate bit of our pages won't be affected by |
| 5249 | * clear_extent_buffer_uptodate(). |
| 5250 | */ |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5251 | for (i = 0; i < num_pages; i++) { |
Liu Bo | 2571e73 | 2016-08-03 12:33:01 -0700 | [diff] [blame] | 5252 | page = eb->pages[i]; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5253 | if (!PageUptodate(page)) { |
| 5254 | num_reads++; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5255 | all_uptodate = 0; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5256 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5257 | } |
Liu Bo | 2571e73 | 2016-08-03 12:33:01 -0700 | [diff] [blame] | 5258 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5259 | if (all_uptodate) { |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5260 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5261 | goto unlock_exit; |
| 5262 | } |
| 5263 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 5264 | clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 5265 | eb->read_mirror = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5266 | atomic_set(&eb->io_pages, num_reads); |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5267 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5268 | page = eb->pages[i]; |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5269 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5270 | if (!PageUptodate(page)) { |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5271 | if (ret) { |
| 5272 | atomic_dec(&eb->io_pages); |
| 5273 | unlock_page(page); |
| 5274 | continue; |
| 5275 | } |
| 5276 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5277 | ClearPageError(page); |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5278 | err = __extent_read_full_page(tree, page, |
David Sterba | 6af49db | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 5279 | btree_get_extent, &bio, |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 5280 | mirror_num, &bio_flags, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 5281 | REQ_META); |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5282 | if (err) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5283 | ret = err; |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5284 | /* |
| 5285 | * We use &bio in above __extent_read_full_page, |
| 5286 | * so we ensure that if it returns error, the |
| 5287 | * current page fails to add itself to bio and |
| 5288 | * it's been unlocked. |
| 5289 | * |
| 5290 | * We must dec io_pages by ourselves. |
| 5291 | */ |
| 5292 | atomic_dec(&eb->io_pages); |
| 5293 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5294 | } else { |
| 5295 | unlock_page(page); |
| 5296 | } |
| 5297 | } |
| 5298 | |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 5299 | if (bio) { |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 5300 | err = submit_one_bio(bio, mirror_num, bio_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5301 | if (err) |
| 5302 | return err; |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 5303 | } |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5304 | |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 5305 | if (ret || wait != WAIT_COMPLETE) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5306 | return ret; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5307 | |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5308 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5309 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5310 | wait_on_page_locked(page); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5311 | if (!PageUptodate(page)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5312 | ret = -EIO; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5313 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5314 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5315 | return ret; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5316 | |
| 5317 | unlock_exit: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5318 | while (locked_pages > 0) { |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5319 | locked_pages--; |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5320 | page = eb->pages[locked_pages]; |
| 5321 | unlock_page(page); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5322 | } |
| 5323 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5324 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5325 | |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 5326 | void read_extent_buffer(const struct extent_buffer *eb, void *dstv, |
| 5327 | unsigned long start, unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5328 | { |
| 5329 | size_t cur; |
| 5330 | size_t offset; |
| 5331 | struct page *page; |
| 5332 | char *kaddr; |
| 5333 | char *dst = (char *)dstv; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5334 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5335 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5336 | |
Liu Bo | f716abd | 2017-08-09 11:10:16 -0600 | [diff] [blame] | 5337 | if (start + len > eb->len) { |
| 5338 | WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n", |
| 5339 | eb->start, eb->len, start, len); |
| 5340 | memset(dst, 0, len); |
| 5341 | return; |
| 5342 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5343 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5344 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5345 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5346 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5347 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5348 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5349 | cur = min(len, (PAGE_SIZE - offset)); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5350 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5351 | memcpy(dst, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5352 | |
| 5353 | dst += cur; |
| 5354 | len -= cur; |
| 5355 | offset = 0; |
| 5356 | i++; |
| 5357 | } |
| 5358 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5359 | |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 5360 | int read_extent_buffer_to_user(const struct extent_buffer *eb, |
| 5361 | void __user *dstv, |
| 5362 | unsigned long start, unsigned long len) |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5363 | { |
| 5364 | size_t cur; |
| 5365 | size_t offset; |
| 5366 | struct page *page; |
| 5367 | char *kaddr; |
| 5368 | char __user *dst = (char __user *)dstv; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5369 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5370 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5371 | int ret = 0; |
| 5372 | |
| 5373 | WARN_ON(start > eb->len); |
| 5374 | WARN_ON(start + len > eb->start + eb->len); |
| 5375 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5376 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5377 | |
| 5378 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5379 | page = eb->pages[i]; |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5380 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5381 | cur = min(len, (PAGE_SIZE - offset)); |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5382 | kaddr = page_address(page); |
| 5383 | if (copy_to_user(dst, kaddr + offset, cur)) { |
| 5384 | ret = -EFAULT; |
| 5385 | break; |
| 5386 | } |
| 5387 | |
| 5388 | dst += cur; |
| 5389 | len -= cur; |
| 5390 | offset = 0; |
| 5391 | i++; |
| 5392 | } |
| 5393 | |
| 5394 | return ret; |
| 5395 | } |
| 5396 | |
Liu Bo | 415b35a | 2016-06-17 19:16:21 -0700 | [diff] [blame] | 5397 | /* |
| 5398 | * return 0 if the item is found within a page. |
| 5399 | * return 1 if the item spans two pages. |
| 5400 | * return -EINVAL otherwise. |
| 5401 | */ |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 5402 | int map_private_extent_buffer(const struct extent_buffer *eb, |
| 5403 | unsigned long start, unsigned long min_len, |
| 5404 | char **map, unsigned long *map_start, |
| 5405 | unsigned long *map_len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5406 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5407 | size_t offset = start & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5408 | char *kaddr; |
| 5409 | struct page *p; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5410 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5411 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5412 | unsigned long end_i = (start_offset + start + min_len - 1) >> |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5413 | PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5414 | |
Liu Bo | f716abd | 2017-08-09 11:10:16 -0600 | [diff] [blame] | 5415 | if (start + min_len > eb->len) { |
| 5416 | WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n", |
| 5417 | eb->start, eb->len, start, min_len); |
| 5418 | return -EINVAL; |
| 5419 | } |
| 5420 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5421 | if (i != end_i) |
Liu Bo | 415b35a | 2016-06-17 19:16:21 -0700 | [diff] [blame] | 5422 | return 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5423 | |
| 5424 | if (i == 0) { |
| 5425 | offset = start_offset; |
| 5426 | *map_start = 0; |
| 5427 | } else { |
| 5428 | offset = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5429 | *map_start = ((u64)i << PAGE_SHIFT) - start_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5430 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5431 | |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5432 | p = eb->pages[i]; |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5433 | kaddr = page_address(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5434 | *map = kaddr + offset; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5435 | *map_len = PAGE_SIZE - offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5436 | return 0; |
| 5437 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5438 | |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 5439 | int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv, |
| 5440 | unsigned long start, unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5441 | { |
| 5442 | size_t cur; |
| 5443 | size_t offset; |
| 5444 | struct page *page; |
| 5445 | char *kaddr; |
| 5446 | char *ptr = (char *)ptrv; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5447 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5448 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5449 | int ret = 0; |
| 5450 | |
| 5451 | WARN_ON(start > eb->len); |
| 5452 | WARN_ON(start + len > eb->start + eb->len); |
| 5453 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5454 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5455 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5456 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5457 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5458 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5459 | cur = min(len, (PAGE_SIZE - offset)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5460 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5461 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5462 | ret = memcmp(ptr, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5463 | if (ret) |
| 5464 | break; |
| 5465 | |
| 5466 | ptr += cur; |
| 5467 | len -= cur; |
| 5468 | offset = 0; |
| 5469 | i++; |
| 5470 | } |
| 5471 | return ret; |
| 5472 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5473 | |
David Sterba | f157bf7 | 2016-11-09 17:43:38 +0100 | [diff] [blame] | 5474 | void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb, |
| 5475 | const void *srcv) |
| 5476 | { |
| 5477 | char *kaddr; |
| 5478 | |
| 5479 | WARN_ON(!PageUptodate(eb->pages[0])); |
| 5480 | kaddr = page_address(eb->pages[0]); |
| 5481 | memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv, |
| 5482 | BTRFS_FSID_SIZE); |
| 5483 | } |
| 5484 | |
| 5485 | void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv) |
| 5486 | { |
| 5487 | char *kaddr; |
| 5488 | |
| 5489 | WARN_ON(!PageUptodate(eb->pages[0])); |
| 5490 | kaddr = page_address(eb->pages[0]); |
| 5491 | memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv, |
| 5492 | BTRFS_FSID_SIZE); |
| 5493 | } |
| 5494 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5495 | void write_extent_buffer(struct extent_buffer *eb, const void *srcv, |
| 5496 | unsigned long start, unsigned long len) |
| 5497 | { |
| 5498 | size_t cur; |
| 5499 | size_t offset; |
| 5500 | struct page *page; |
| 5501 | char *kaddr; |
| 5502 | char *src = (char *)srcv; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5503 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5504 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5505 | |
| 5506 | WARN_ON(start > eb->len); |
| 5507 | WARN_ON(start + len > eb->start + eb->len); |
| 5508 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5509 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5510 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5511 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5512 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5513 | WARN_ON(!PageUptodate(page)); |
| 5514 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5515 | cur = min(len, PAGE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5516 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5517 | memcpy(kaddr + offset, src, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5518 | |
| 5519 | src += cur; |
| 5520 | len -= cur; |
| 5521 | offset = 0; |
| 5522 | i++; |
| 5523 | } |
| 5524 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5525 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 5526 | void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start, |
| 5527 | unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5528 | { |
| 5529 | size_t cur; |
| 5530 | size_t offset; |
| 5531 | struct page *page; |
| 5532 | char *kaddr; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5533 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5534 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5535 | |
| 5536 | WARN_ON(start > eb->len); |
| 5537 | WARN_ON(start + len > eb->start + eb->len); |
| 5538 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5539 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5540 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5541 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5542 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5543 | WARN_ON(!PageUptodate(page)); |
| 5544 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5545 | cur = min(len, PAGE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5546 | kaddr = page_address(page); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 5547 | memset(kaddr + offset, 0, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5548 | |
| 5549 | len -= cur; |
| 5550 | offset = 0; |
| 5551 | i++; |
| 5552 | } |
| 5553 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5554 | |
David Sterba | 58e8012 | 2016-11-08 18:30:31 +0100 | [diff] [blame] | 5555 | void copy_extent_buffer_full(struct extent_buffer *dst, |
| 5556 | struct extent_buffer *src) |
| 5557 | { |
| 5558 | int i; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5559 | int num_pages; |
David Sterba | 58e8012 | 2016-11-08 18:30:31 +0100 | [diff] [blame] | 5560 | |
| 5561 | ASSERT(dst->len == src->len); |
| 5562 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5563 | num_pages = num_extent_pages(dst); |
David Sterba | 58e8012 | 2016-11-08 18:30:31 +0100 | [diff] [blame] | 5564 | for (i = 0; i < num_pages; i++) |
| 5565 | copy_page(page_address(dst->pages[i]), |
| 5566 | page_address(src->pages[i])); |
| 5567 | } |
| 5568 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5569 | void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src, |
| 5570 | unsigned long dst_offset, unsigned long src_offset, |
| 5571 | unsigned long len) |
| 5572 | { |
| 5573 | u64 dst_len = dst->len; |
| 5574 | size_t cur; |
| 5575 | size_t offset; |
| 5576 | struct page *page; |
| 5577 | char *kaddr; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5578 | size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1); |
| 5579 | unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5580 | |
| 5581 | WARN_ON(src->len != dst_len); |
| 5582 | |
| 5583 | offset = (start_offset + dst_offset) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5584 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5585 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5586 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5587 | page = dst->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5588 | WARN_ON(!PageUptodate(page)); |
| 5589 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5590 | cur = min(len, (unsigned long)(PAGE_SIZE - offset)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5591 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5592 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5593 | read_extent_buffer(src, kaddr + offset, src_offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5594 | |
| 5595 | src_offset += cur; |
| 5596 | len -= cur; |
| 5597 | offset = 0; |
| 5598 | i++; |
| 5599 | } |
| 5600 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5601 | |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5602 | /* |
| 5603 | * eb_bitmap_offset() - calculate the page and offset of the byte containing the |
| 5604 | * given bit number |
| 5605 | * @eb: the extent buffer |
| 5606 | * @start: offset of the bitmap item in the extent buffer |
| 5607 | * @nr: bit number |
| 5608 | * @page_index: return index of the page in the extent buffer that contains the |
| 5609 | * given bit number |
| 5610 | * @page_offset: return offset into the page given by page_index |
| 5611 | * |
| 5612 | * This helper hides the ugliness of finding the byte in an extent buffer which |
| 5613 | * contains a given bit. |
| 5614 | */ |
| 5615 | static inline void eb_bitmap_offset(struct extent_buffer *eb, |
| 5616 | unsigned long start, unsigned long nr, |
| 5617 | unsigned long *page_index, |
| 5618 | size_t *page_offset) |
| 5619 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5620 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5621 | size_t byte_offset = BIT_BYTE(nr); |
| 5622 | size_t offset; |
| 5623 | |
| 5624 | /* |
| 5625 | * The byte we want is the offset of the extent buffer + the offset of |
| 5626 | * the bitmap item in the extent buffer + the offset of the byte in the |
| 5627 | * bitmap item. |
| 5628 | */ |
| 5629 | offset = start_offset + start + byte_offset; |
| 5630 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5631 | *page_index = offset >> PAGE_SHIFT; |
| 5632 | *page_offset = offset & (PAGE_SIZE - 1); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5633 | } |
| 5634 | |
| 5635 | /** |
| 5636 | * extent_buffer_test_bit - determine whether a bit in a bitmap item is set |
| 5637 | * @eb: the extent buffer |
| 5638 | * @start: offset of the bitmap item in the extent buffer |
| 5639 | * @nr: bit number to test |
| 5640 | */ |
| 5641 | int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start, |
| 5642 | unsigned long nr) |
| 5643 | { |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5644 | u8 *kaddr; |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5645 | struct page *page; |
| 5646 | unsigned long i; |
| 5647 | size_t offset; |
| 5648 | |
| 5649 | eb_bitmap_offset(eb, start, nr, &i, &offset); |
| 5650 | page = eb->pages[i]; |
| 5651 | WARN_ON(!PageUptodate(page)); |
| 5652 | kaddr = page_address(page); |
| 5653 | return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1))); |
| 5654 | } |
| 5655 | |
| 5656 | /** |
| 5657 | * extent_buffer_bitmap_set - set an area of a bitmap |
| 5658 | * @eb: the extent buffer |
| 5659 | * @start: offset of the bitmap item in the extent buffer |
| 5660 | * @pos: bit number of the first bit |
| 5661 | * @len: number of bits to set |
| 5662 | */ |
| 5663 | void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start, |
| 5664 | unsigned long pos, unsigned long len) |
| 5665 | { |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5666 | u8 *kaddr; |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5667 | struct page *page; |
| 5668 | unsigned long i; |
| 5669 | size_t offset; |
| 5670 | const unsigned int size = pos + len; |
| 5671 | int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE); |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5672 | u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5673 | |
| 5674 | eb_bitmap_offset(eb, start, pos, &i, &offset); |
| 5675 | page = eb->pages[i]; |
| 5676 | WARN_ON(!PageUptodate(page)); |
| 5677 | kaddr = page_address(page); |
| 5678 | |
| 5679 | while (len >= bits_to_set) { |
| 5680 | kaddr[offset] |= mask_to_set; |
| 5681 | len -= bits_to_set; |
| 5682 | bits_to_set = BITS_PER_BYTE; |
Dan Carpenter | 9c89469 | 2016-10-12 11:33:21 +0300 | [diff] [blame] | 5683 | mask_to_set = ~0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5684 | if (++offset >= PAGE_SIZE && len > 0) { |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5685 | offset = 0; |
| 5686 | page = eb->pages[++i]; |
| 5687 | WARN_ON(!PageUptodate(page)); |
| 5688 | kaddr = page_address(page); |
| 5689 | } |
| 5690 | } |
| 5691 | if (len) { |
| 5692 | mask_to_set &= BITMAP_LAST_BYTE_MASK(size); |
| 5693 | kaddr[offset] |= mask_to_set; |
| 5694 | } |
| 5695 | } |
| 5696 | |
| 5697 | |
| 5698 | /** |
| 5699 | * extent_buffer_bitmap_clear - clear an area of a bitmap |
| 5700 | * @eb: the extent buffer |
| 5701 | * @start: offset of the bitmap item in the extent buffer |
| 5702 | * @pos: bit number of the first bit |
| 5703 | * @len: number of bits to clear |
| 5704 | */ |
| 5705 | void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start, |
| 5706 | unsigned long pos, unsigned long len) |
| 5707 | { |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5708 | u8 *kaddr; |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5709 | struct page *page; |
| 5710 | unsigned long i; |
| 5711 | size_t offset; |
| 5712 | const unsigned int size = pos + len; |
| 5713 | int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE); |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5714 | u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5715 | |
| 5716 | eb_bitmap_offset(eb, start, pos, &i, &offset); |
| 5717 | page = eb->pages[i]; |
| 5718 | WARN_ON(!PageUptodate(page)); |
| 5719 | kaddr = page_address(page); |
| 5720 | |
| 5721 | while (len >= bits_to_clear) { |
| 5722 | kaddr[offset] &= ~mask_to_clear; |
| 5723 | len -= bits_to_clear; |
| 5724 | bits_to_clear = BITS_PER_BYTE; |
Dan Carpenter | 9c89469 | 2016-10-12 11:33:21 +0300 | [diff] [blame] | 5725 | mask_to_clear = ~0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5726 | if (++offset >= PAGE_SIZE && len > 0) { |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5727 | offset = 0; |
| 5728 | page = eb->pages[++i]; |
| 5729 | WARN_ON(!PageUptodate(page)); |
| 5730 | kaddr = page_address(page); |
| 5731 | } |
| 5732 | } |
| 5733 | if (len) { |
| 5734 | mask_to_clear &= BITMAP_LAST_BYTE_MASK(size); |
| 5735 | kaddr[offset] &= ~mask_to_clear; |
| 5736 | } |
| 5737 | } |
| 5738 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5739 | static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len) |
| 5740 | { |
| 5741 | unsigned long distance = (src > dst) ? src - dst : dst - src; |
| 5742 | return distance < len; |
| 5743 | } |
| 5744 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5745 | static void copy_pages(struct page *dst_page, struct page *src_page, |
| 5746 | unsigned long dst_off, unsigned long src_off, |
| 5747 | unsigned long len) |
| 5748 | { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5749 | char *dst_kaddr = page_address(dst_page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5750 | char *src_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5751 | int must_memmove = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5752 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5753 | if (dst_page != src_page) { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5754 | src_kaddr = page_address(src_page); |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5755 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5756 | src_kaddr = dst_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5757 | if (areas_overlap(src_off, dst_off, len)) |
| 5758 | must_memmove = 1; |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5759 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5760 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5761 | if (must_memmove) |
| 5762 | memmove(dst_kaddr + dst_off, src_kaddr + src_off, len); |
| 5763 | else |
| 5764 | memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5765 | } |
| 5766 | |
| 5767 | void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 5768 | unsigned long src_offset, unsigned long len) |
| 5769 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5770 | struct btrfs_fs_info *fs_info = dst->fs_info; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5771 | size_t cur; |
| 5772 | size_t dst_off_in_page; |
| 5773 | size_t src_off_in_page; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5774 | size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5775 | unsigned long dst_i; |
| 5776 | unsigned long src_i; |
| 5777 | |
| 5778 | if (src_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5779 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5780 | "memmove bogus src_offset %lu move len %lu dst len %lu", |
| 5781 | src_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5782 | BUG_ON(1); |
| 5783 | } |
| 5784 | if (dst_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5785 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5786 | "memmove bogus dst_offset %lu move len %lu dst len %lu", |
| 5787 | dst_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5788 | BUG_ON(1); |
| 5789 | } |
| 5790 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5791 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5792 | dst_off_in_page = (start_offset + dst_offset) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5793 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5794 | src_off_in_page = (start_offset + src_offset) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5795 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5796 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5797 | dst_i = (start_offset + dst_offset) >> PAGE_SHIFT; |
| 5798 | src_i = (start_offset + src_offset) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5799 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5800 | cur = min(len, (unsigned long)(PAGE_SIZE - |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5801 | src_off_in_page)); |
| 5802 | cur = min_t(unsigned long, cur, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5803 | (unsigned long)(PAGE_SIZE - dst_off_in_page)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5804 | |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5805 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5806 | dst_off_in_page, src_off_in_page, cur); |
| 5807 | |
| 5808 | src_offset += cur; |
| 5809 | dst_offset += cur; |
| 5810 | len -= cur; |
| 5811 | } |
| 5812 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5813 | |
| 5814 | void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 5815 | unsigned long src_offset, unsigned long len) |
| 5816 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5817 | struct btrfs_fs_info *fs_info = dst->fs_info; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5818 | size_t cur; |
| 5819 | size_t dst_off_in_page; |
| 5820 | size_t src_off_in_page; |
| 5821 | unsigned long dst_end = dst_offset + len - 1; |
| 5822 | unsigned long src_end = src_offset + len - 1; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5823 | size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5824 | unsigned long dst_i; |
| 5825 | unsigned long src_i; |
| 5826 | |
| 5827 | if (src_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5828 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5829 | "memmove bogus src_offset %lu move len %lu len %lu", |
| 5830 | src_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5831 | BUG_ON(1); |
| 5832 | } |
| 5833 | if (dst_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5834 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5835 | "memmove bogus dst_offset %lu move len %lu len %lu", |
| 5836 | dst_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5837 | BUG_ON(1); |
| 5838 | } |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5839 | if (dst_offset < src_offset) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5840 | memcpy_extent_buffer(dst, dst_offset, src_offset, len); |
| 5841 | return; |
| 5842 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5843 | while (len > 0) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5844 | dst_i = (start_offset + dst_end) >> PAGE_SHIFT; |
| 5845 | src_i = (start_offset + src_end) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5846 | |
| 5847 | dst_off_in_page = (start_offset + dst_end) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5848 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5849 | src_off_in_page = (start_offset + src_end) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5850 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5851 | |
| 5852 | cur = min_t(unsigned long, len, src_off_in_page + 1); |
| 5853 | cur = min(cur, dst_off_in_page + 1); |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5854 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5855 | dst_off_in_page - cur + 1, |
| 5856 | src_off_in_page - cur + 1, cur); |
| 5857 | |
| 5858 | dst_end -= cur; |
| 5859 | src_end -= cur; |
| 5860 | len -= cur; |
| 5861 | } |
| 5862 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5863 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5864 | int try_release_extent_buffer(struct page *page) |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5865 | { |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5866 | struct extent_buffer *eb; |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5867 | |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5868 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5869 | * We need to make sure nobody is attaching this page to an eb right |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5870 | * now. |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5871 | */ |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5872 | spin_lock(&page->mapping->private_lock); |
| 5873 | if (!PagePrivate(page)) { |
| 5874 | spin_unlock(&page->mapping->private_lock); |
| 5875 | return 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5876 | } |
| 5877 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5878 | eb = (struct extent_buffer *)page->private; |
| 5879 | BUG_ON(!eb); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5880 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5881 | /* |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5882 | * This is a little awful but should be ok, we need to make sure that |
| 5883 | * the eb doesn't disappear out from under us while we're looking at |
| 5884 | * this page. |
| 5885 | */ |
| 5886 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5887 | if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5888 | spin_unlock(&eb->refs_lock); |
| 5889 | spin_unlock(&page->mapping->private_lock); |
| 5890 | return 0; |
| 5891 | } |
| 5892 | spin_unlock(&page->mapping->private_lock); |
| 5893 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5894 | /* |
| 5895 | * If tree ref isn't set then we know the ref on this eb is a real ref, |
| 5896 | * so just return, this page will likely be freed soon anyway. |
| 5897 | */ |
| 5898 | if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { |
| 5899 | spin_unlock(&eb->refs_lock); |
| 5900 | return 0; |
| 5901 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5902 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5903 | return release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5904 | } |