Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1 | #include <linux/bitops.h> |
| 2 | #include <linux/slab.h> |
| 3 | #include <linux/bio.h> |
| 4 | #include <linux/mm.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5 | #include <linux/pagemap.h> |
| 6 | #include <linux/page-flags.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7 | #include <linux/spinlock.h> |
| 8 | #include <linux/blkdev.h> |
| 9 | #include <linux/swap.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 10 | #include <linux/writeback.h> |
| 11 | #include <linux/pagevec.h> |
Linus Torvalds | 268bb0c | 2011-05-20 12:50:29 -0700 | [diff] [blame] | 12 | #include <linux/prefetch.h> |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 13 | #include <linux/cleancache.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 14 | #include "extent_io.h" |
| 15 | #include "extent_map.h" |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 16 | #include "ctree.h" |
| 17 | #include "btrfs_inode.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 18 | #include "volumes.h" |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 19 | #include "check-integrity.h" |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 20 | #include "locking.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 21 | #include "rcu-string.h" |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 22 | #include "backref.h" |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 23 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 24 | static struct kmem_cache *extent_state_cache; |
| 25 | static struct kmem_cache *extent_buffer_cache; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 26 | static struct bio_set *btrfs_bioset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 27 | |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 28 | static inline bool extent_state_in_tree(const struct extent_state *state) |
| 29 | { |
| 30 | return !RB_EMPTY_NODE(&state->rb_node); |
| 31 | } |
| 32 | |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 33 | #ifdef CONFIG_BTRFS_DEBUG |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 34 | static LIST_HEAD(buffers); |
| 35 | static LIST_HEAD(states); |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 36 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 37 | static DEFINE_SPINLOCK(leak_lock); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 38 | |
| 39 | static inline |
| 40 | void btrfs_leak_debug_add(struct list_head *new, struct list_head *head) |
| 41 | { |
| 42 | unsigned long flags; |
| 43 | |
| 44 | spin_lock_irqsave(&leak_lock, flags); |
| 45 | list_add(new, head); |
| 46 | spin_unlock_irqrestore(&leak_lock, flags); |
| 47 | } |
| 48 | |
| 49 | static inline |
| 50 | void btrfs_leak_debug_del(struct list_head *entry) |
| 51 | { |
| 52 | unsigned long flags; |
| 53 | |
| 54 | spin_lock_irqsave(&leak_lock, flags); |
| 55 | list_del(entry); |
| 56 | spin_unlock_irqrestore(&leak_lock, flags); |
| 57 | } |
| 58 | |
| 59 | static inline |
| 60 | void btrfs_leak_debug_check(void) |
| 61 | { |
| 62 | struct extent_state *state; |
| 63 | struct extent_buffer *eb; |
| 64 | |
| 65 | while (!list_empty(&states)) { |
| 66 | state = list_entry(states.next, struct extent_state, leak_list); |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 67 | 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] | 68 | state->start, state->end, state->state, |
| 69 | extent_state_in_tree(state), |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 70 | refcount_read(&state->refs)); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 71 | list_del(&state->leak_list); |
| 72 | kmem_cache_free(extent_state_cache, state); |
| 73 | } |
| 74 | |
| 75 | while (!list_empty(&buffers)) { |
| 76 | eb = list_entry(buffers.next, struct extent_buffer, leak_list); |
Jeff Mahoney | 62e8557 | 2016-09-20 10:05:01 -0400 | [diff] [blame] | 77 | pr_err("BTRFS: buffer leak start %llu len %lu refs %d\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 78 | eb->start, eb->len, atomic_read(&eb->refs)); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 79 | list_del(&eb->leak_list); |
| 80 | kmem_cache_free(extent_buffer_cache, eb); |
| 81 | } |
| 82 | } |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 83 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 84 | #define btrfs_debug_check_extent_io_range(tree, start, end) \ |
| 85 | __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end)) |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 86 | static inline void __btrfs_debug_check_extent_io_range(const char *caller, |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 87 | struct extent_io_tree *tree, u64 start, u64 end) |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 88 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 89 | if (tree->ops && tree->ops->check_extent_io_range) |
| 90 | tree->ops->check_extent_io_range(tree->private_data, caller, |
| 91 | start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 92 | } |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 93 | #else |
| 94 | #define btrfs_leak_debug_add(new, head) do {} while (0) |
| 95 | #define btrfs_leak_debug_del(entry) do {} while (0) |
| 96 | #define btrfs_leak_debug_check() do {} while (0) |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 97 | #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] | 98 | #endif |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 99 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 100 | #define BUFFER_LRU_MAX 64 |
| 101 | |
| 102 | struct tree_entry { |
| 103 | u64 start; |
| 104 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 105 | struct rb_node rb_node; |
| 106 | }; |
| 107 | |
| 108 | struct extent_page_data { |
| 109 | struct bio *bio; |
| 110 | struct extent_io_tree *tree; |
| 111 | get_extent_t *get_extent; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 112 | |
| 113 | /* tells writepage not to lock the state bits for this range |
| 114 | * it still does the unlocking |
| 115 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 116 | unsigned int extent_locked:1; |
| 117 | |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 118 | /* tells the submit_bio code to use REQ_SYNC */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 119 | unsigned int sync_io:1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 120 | }; |
| 121 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 122 | static void add_extent_changeset(struct extent_state *state, unsigned bits, |
| 123 | struct extent_changeset *changeset, |
| 124 | int set) |
| 125 | { |
| 126 | int ret; |
| 127 | |
| 128 | if (!changeset) |
| 129 | return; |
| 130 | if (set && (state->state & bits) == bits) |
| 131 | return; |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 132 | if (!set && (state->state & bits) == 0) |
| 133 | return; |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 134 | changeset->bytes_changed += state->end - state->start + 1; |
David Sterba | 53d3235 | 2017-02-13 13:42:29 +0100 | [diff] [blame] | 135 | ret = ulist_add(&changeset->range_changed, state->start, state->end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 136 | GFP_ATOMIC); |
| 137 | /* ENOMEM */ |
| 138 | BUG_ON(ret < 0); |
| 139 | } |
| 140 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 141 | static noinline void flush_write_bio(void *data); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 142 | static inline struct btrfs_fs_info * |
| 143 | tree_fs_info(struct extent_io_tree *tree) |
| 144 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 145 | if (tree->ops) |
| 146 | return tree->ops->tree_fs_info(tree->private_data); |
| 147 | return NULL; |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 148 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 149 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 150 | int __init extent_io_init(void) |
| 151 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 152 | extent_state_cache = kmem_cache_create("btrfs_extent_state", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 153 | sizeof(struct extent_state), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 154 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 155 | if (!extent_state_cache) |
| 156 | return -ENOMEM; |
| 157 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 158 | extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 159 | sizeof(struct extent_buffer), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 160 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 161 | if (!extent_buffer_cache) |
| 162 | goto free_state_cache; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 163 | |
| 164 | btrfs_bioset = bioset_create(BIO_POOL_SIZE, |
NeilBrown | 011067b | 2017-06-18 14:38:57 +1000 | [diff] [blame] | 165 | offsetof(struct btrfs_io_bio, bio), |
| 166 | BIOSET_NEED_BVECS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 167 | if (!btrfs_bioset) |
| 168 | goto free_buffer_cache; |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 169 | |
| 170 | if (bioset_integrity_create(btrfs_bioset, BIO_POOL_SIZE)) |
| 171 | goto free_bioset; |
| 172 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 173 | return 0; |
| 174 | |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 175 | free_bioset: |
| 176 | bioset_free(btrfs_bioset); |
| 177 | btrfs_bioset = NULL; |
| 178 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 179 | free_buffer_cache: |
| 180 | kmem_cache_destroy(extent_buffer_cache); |
| 181 | extent_buffer_cache = NULL; |
| 182 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 183 | free_state_cache: |
| 184 | kmem_cache_destroy(extent_state_cache); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 185 | extent_state_cache = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 186 | return -ENOMEM; |
| 187 | } |
| 188 | |
| 189 | void extent_io_exit(void) |
| 190 | { |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 191 | btrfs_leak_debug_check(); |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 192 | |
| 193 | /* |
| 194 | * Make sure all delayed rcu free are flushed before we |
| 195 | * destroy caches. |
| 196 | */ |
| 197 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 198 | kmem_cache_destroy(extent_state_cache); |
| 199 | kmem_cache_destroy(extent_buffer_cache); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 200 | if (btrfs_bioset) |
| 201 | bioset_free(btrfs_bioset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void extent_io_tree_init(struct extent_io_tree *tree, |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 205 | void *private_data) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 206 | { |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 207 | tree->state = RB_ROOT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 208 | tree->ops = NULL; |
| 209 | tree->dirty_bytes = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 210 | spin_lock_init(&tree->lock); |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 211 | tree->private_data = private_data; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 212 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 213 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 214 | static struct extent_state *alloc_extent_state(gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 215 | { |
| 216 | struct extent_state *state; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 217 | |
Michal Hocko | 3ba7ab2 | 2017-01-09 15:39:02 +0100 | [diff] [blame] | 218 | /* |
| 219 | * The given mask might be not appropriate for the slab allocator, |
| 220 | * drop the unsupported bits |
| 221 | */ |
| 222 | mask &= ~(__GFP_DMA32|__GFP_HIGHMEM); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 223 | state = kmem_cache_alloc(extent_state_cache, mask); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 224 | if (!state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 225 | return state; |
| 226 | state->state = 0; |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 227 | state->failrec = NULL; |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 228 | RB_CLEAR_NODE(&state->rb_node); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 229 | btrfs_leak_debug_add(&state->leak_list, &states); |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 230 | refcount_set(&state->refs, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 231 | init_waitqueue_head(&state->wq); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 232 | trace_alloc_extent_state(state, mask, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 233 | return state; |
| 234 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 235 | |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 236 | void free_extent_state(struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 237 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 238 | if (!state) |
| 239 | return; |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 240 | if (refcount_dec_and_test(&state->refs)) { |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 241 | WARN_ON(extent_state_in_tree(state)); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 242 | btrfs_leak_debug_del(&state->leak_list); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 243 | trace_free_extent_state(state, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 244 | kmem_cache_free(extent_state_cache, state); |
| 245 | } |
| 246 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 247 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 248 | static struct rb_node *tree_insert(struct rb_root *root, |
| 249 | struct rb_node *search_start, |
| 250 | u64 offset, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 251 | struct rb_node *node, |
| 252 | struct rb_node ***p_in, |
| 253 | struct rb_node **parent_in) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 254 | { |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 255 | struct rb_node **p; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 256 | struct rb_node *parent = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 257 | struct tree_entry *entry; |
| 258 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 259 | if (p_in && parent_in) { |
| 260 | p = *p_in; |
| 261 | parent = *parent_in; |
| 262 | goto do_insert; |
| 263 | } |
| 264 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 265 | p = search_start ? &search_start : &root->rb_node; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 266 | while (*p) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 267 | parent = *p; |
| 268 | entry = rb_entry(parent, struct tree_entry, rb_node); |
| 269 | |
| 270 | if (offset < entry->start) |
| 271 | p = &(*p)->rb_left; |
| 272 | else if (offset > entry->end) |
| 273 | p = &(*p)->rb_right; |
| 274 | else |
| 275 | return parent; |
| 276 | } |
| 277 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 278 | do_insert: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 279 | rb_link_node(node, parent, p); |
| 280 | rb_insert_color(node, root); |
| 281 | return NULL; |
| 282 | } |
| 283 | |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 284 | 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] | 285 | struct rb_node **prev_ret, |
| 286 | struct rb_node **next_ret, |
| 287 | struct rb_node ***p_ret, |
| 288 | struct rb_node **parent_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 289 | { |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 290 | struct rb_root *root = &tree->state; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 291 | struct rb_node **n = &root->rb_node; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 292 | struct rb_node *prev = NULL; |
| 293 | struct rb_node *orig_prev = NULL; |
| 294 | struct tree_entry *entry; |
| 295 | struct tree_entry *prev_entry = NULL; |
| 296 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 297 | while (*n) { |
| 298 | prev = *n; |
| 299 | entry = rb_entry(prev, struct tree_entry, rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 300 | prev_entry = entry; |
| 301 | |
| 302 | if (offset < entry->start) |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 303 | n = &(*n)->rb_left; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 304 | else if (offset > entry->end) |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 305 | n = &(*n)->rb_right; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 306 | else |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 307 | return *n; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 308 | } |
| 309 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 310 | if (p_ret) |
| 311 | *p_ret = n; |
| 312 | if (parent_ret) |
| 313 | *parent_ret = prev; |
| 314 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 315 | if (prev_ret) { |
| 316 | orig_prev = prev; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 317 | while (prev && offset > prev_entry->end) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 318 | prev = rb_next(prev); |
| 319 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| 320 | } |
| 321 | *prev_ret = prev; |
| 322 | prev = orig_prev; |
| 323 | } |
| 324 | |
| 325 | if (next_ret) { |
| 326 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 327 | while (prev && offset < prev_entry->start) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 328 | prev = rb_prev(prev); |
| 329 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| 330 | } |
| 331 | *next_ret = prev; |
| 332 | } |
| 333 | return NULL; |
| 334 | } |
| 335 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 336 | static inline struct rb_node * |
| 337 | tree_search_for_insert(struct extent_io_tree *tree, |
| 338 | u64 offset, |
| 339 | struct rb_node ***p_ret, |
| 340 | struct rb_node **parent_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 341 | { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 342 | struct rb_node *prev = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 343 | struct rb_node *ret; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 344 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 345 | ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 346 | if (!ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 347 | return prev; |
| 348 | return ret; |
| 349 | } |
| 350 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 351 | static inline struct rb_node *tree_search(struct extent_io_tree *tree, |
| 352 | u64 offset) |
| 353 | { |
| 354 | return tree_search_for_insert(tree, offset, NULL, NULL); |
| 355 | } |
| 356 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 357 | static void merge_cb(struct extent_io_tree *tree, struct extent_state *new, |
| 358 | struct extent_state *other) |
| 359 | { |
| 360 | if (tree->ops && tree->ops->merge_extent_hook) |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 361 | tree->ops->merge_extent_hook(tree->private_data, new, other); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 362 | } |
| 363 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 364 | /* |
| 365 | * utility function to look for merge candidates inside a given range. |
| 366 | * Any extents with matching state are merged together into a single |
| 367 | * extent in the tree. Extents with EXTENT_IO in their state field |
| 368 | * are not merged because the end_io handlers need to be able to do |
| 369 | * operations on them without sleeping (or doing allocations/splits). |
| 370 | * |
| 371 | * This should be called with the tree lock held. |
| 372 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 373 | static void merge_state(struct extent_io_tree *tree, |
| 374 | struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 375 | { |
| 376 | struct extent_state *other; |
| 377 | struct rb_node *other_node; |
| 378 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 379 | if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 380 | return; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 381 | |
| 382 | other_node = rb_prev(&state->rb_node); |
| 383 | if (other_node) { |
| 384 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 385 | if (other->end == state->start - 1 && |
| 386 | other->state == state->state) { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 387 | merge_cb(tree, state, other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 388 | state->start = other->start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 389 | rb_erase(&other->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 390 | RB_CLEAR_NODE(&other->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 391 | free_extent_state(other); |
| 392 | } |
| 393 | } |
| 394 | other_node = rb_next(&state->rb_node); |
| 395 | if (other_node) { |
| 396 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 397 | if (other->start == state->end + 1 && |
| 398 | other->state == state->state) { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 399 | merge_cb(tree, state, other); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 400 | state->end = other->end; |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 401 | rb_erase(&other->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 402 | RB_CLEAR_NODE(&other->rb_node); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 403 | free_extent_state(other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 404 | } |
| 405 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 406 | } |
| 407 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 408 | static void set_state_cb(struct extent_io_tree *tree, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 409 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 410 | { |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 411 | if (tree->ops && tree->ops->set_bit_hook) |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 412 | tree->ops->set_bit_hook(tree->private_data, state, bits); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | static void clear_state_cb(struct extent_io_tree *tree, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 416 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 417 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 418 | if (tree->ops && tree->ops->clear_bit_hook) |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 419 | tree->ops->clear_bit_hook(tree->private_data, state, bits); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 420 | } |
| 421 | |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 422 | static void set_state_bits(struct extent_io_tree *tree, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 423 | struct extent_state *state, unsigned *bits, |
| 424 | struct extent_changeset *changeset); |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 425 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 426 | /* |
| 427 | * insert an extent_state struct into the tree. 'bits' are set on the |
| 428 | * struct before it is inserted. |
| 429 | * |
| 430 | * This may return -EEXIST if the extent is already there, in which case the |
| 431 | * state struct is freed. |
| 432 | * |
| 433 | * The tree lock is not taken internally. This is a utility function and |
| 434 | * probably isn't what you want to call (see set/clear_extent_bit). |
| 435 | */ |
| 436 | static int insert_state(struct extent_io_tree *tree, |
| 437 | struct extent_state *state, u64 start, u64 end, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 438 | struct rb_node ***p, |
| 439 | struct rb_node **parent, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 440 | unsigned *bits, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 441 | { |
| 442 | struct rb_node *node; |
| 443 | |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 444 | if (end < start) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 445 | WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 446 | end, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 447 | state->start = start; |
| 448 | state->end = end; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 449 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 450 | set_state_bits(tree, state, bits, changeset); |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 451 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 452 | node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 453 | if (node) { |
| 454 | struct extent_state *found; |
| 455 | found = rb_entry(node, struct extent_state, rb_node); |
Jeff Mahoney | 62e8557 | 2016-09-20 10:05:01 -0400 | [diff] [blame] | 456 | 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] | 457 | found->start, found->end, start, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 458 | return -EEXIST; |
| 459 | } |
| 460 | merge_state(tree, state); |
| 461 | return 0; |
| 462 | } |
| 463 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 464 | 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] | 465 | u64 split) |
| 466 | { |
| 467 | if (tree->ops && tree->ops->split_extent_hook) |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 468 | tree->ops->split_extent_hook(tree->private_data, orig, split); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 469 | } |
| 470 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 471 | /* |
| 472 | * split a given extent state struct in two, inserting the preallocated |
| 473 | * struct 'prealloc' as the newly created second half. 'split' indicates an |
| 474 | * offset inside 'orig' where it should be split. |
| 475 | * |
| 476 | * Before calling, |
| 477 | * the tree has 'orig' at [orig->start, orig->end]. After calling, there |
| 478 | * are two extent state structs in the tree: |
| 479 | * prealloc: [orig->start, split - 1] |
| 480 | * orig: [ split, orig->end ] |
| 481 | * |
| 482 | * The tree locks are not taken by this function. They need to be held |
| 483 | * by the caller. |
| 484 | */ |
| 485 | static int split_state(struct extent_io_tree *tree, struct extent_state *orig, |
| 486 | struct extent_state *prealloc, u64 split) |
| 487 | { |
| 488 | struct rb_node *node; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 489 | |
| 490 | split_cb(tree, orig, split); |
| 491 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 492 | prealloc->start = orig->start; |
| 493 | prealloc->end = split - 1; |
| 494 | prealloc->state = orig->state; |
| 495 | orig->start = split; |
| 496 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 497 | node = tree_insert(&tree->state, &orig->rb_node, prealloc->end, |
| 498 | &prealloc->rb_node, NULL, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 499 | if (node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 500 | free_extent_state(prealloc); |
| 501 | return -EEXIST; |
| 502 | } |
| 503 | return 0; |
| 504 | } |
| 505 | |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 506 | static struct extent_state *next_state(struct extent_state *state) |
| 507 | { |
| 508 | struct rb_node *next = rb_next(&state->rb_node); |
| 509 | if (next) |
| 510 | return rb_entry(next, struct extent_state, rb_node); |
| 511 | else |
| 512 | return NULL; |
| 513 | } |
| 514 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 515 | /* |
| 516 | * utility function to clear some bits in an extent state struct. |
Wang Sheng-Hui | 1b303fc | 2012-04-06 14:35:18 +0800 | [diff] [blame] | 517 | * 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] | 518 | * |
| 519 | * If no bits are set on the state struct after clearing things, the |
| 520 | * struct is freed and removed from the tree |
| 521 | */ |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 522 | static struct extent_state *clear_state_bit(struct extent_io_tree *tree, |
| 523 | struct extent_state *state, |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 524 | unsigned *bits, int wake, |
| 525 | struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 526 | { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 527 | struct extent_state *next; |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 528 | unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 529 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 530 | if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 531 | u64 range = state->end - state->start + 1; |
| 532 | WARN_ON(range > tree->dirty_bytes); |
| 533 | tree->dirty_bytes -= range; |
| 534 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 535 | clear_state_cb(tree, state, bits); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 536 | add_extent_changeset(state, bits_to_clear, changeset, 0); |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 537 | state->state &= ~bits_to_clear; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 538 | if (wake) |
| 539 | wake_up(&state->wq); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 540 | if (state->state == 0) { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 541 | next = next_state(state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 542 | if (extent_state_in_tree(state)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 543 | rb_erase(&state->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 544 | RB_CLEAR_NODE(&state->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 545 | free_extent_state(state); |
| 546 | } else { |
| 547 | WARN_ON(1); |
| 548 | } |
| 549 | } else { |
| 550 | merge_state(tree, state); |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 551 | next = next_state(state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 552 | } |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 553 | return next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 554 | } |
| 555 | |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 556 | static struct extent_state * |
| 557 | alloc_extent_state_atomic(struct extent_state *prealloc) |
| 558 | { |
| 559 | if (!prealloc) |
| 560 | prealloc = alloc_extent_state(GFP_ATOMIC); |
| 561 | |
| 562 | return prealloc; |
| 563 | } |
| 564 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 565 | 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] | 566 | { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 567 | btrfs_panic(tree_fs_info(tree), err, |
| 568 | "Locking error: Extent tree was modified by another thread while locked."); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 569 | } |
| 570 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 571 | /* |
| 572 | * clear some bits on a range in the tree. This may require splitting |
| 573 | * or inserting elements in the tree, so the gfp mask is used to |
| 574 | * indicate which allocations or sleeping are allowed. |
| 575 | * |
| 576 | * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove |
| 577 | * the given range from the tree regardless of state (ie for truncate). |
| 578 | * |
| 579 | * the range [start, end] is inclusive. |
| 580 | * |
Jeff Mahoney | 6763af8 | 2012-03-01 14:56:29 +0100 | [diff] [blame] | 581 | * 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] | 582 | */ |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 583 | static int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 584 | unsigned bits, int wake, int delete, |
| 585 | struct extent_state **cached_state, |
| 586 | gfp_t mask, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 587 | { |
| 588 | struct extent_state *state; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 589 | struct extent_state *cached; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 590 | struct extent_state *prealloc = NULL; |
| 591 | struct rb_node *node; |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 592 | u64 last_end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 593 | int err; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 594 | int clear = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 595 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 596 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 597 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 598 | if (bits & EXTENT_DELALLOC) |
| 599 | bits |= EXTENT_NORESERVE; |
| 600 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 601 | if (delete) |
| 602 | bits |= ~EXTENT_CTLBITS; |
| 603 | bits |= EXTENT_FIRST_DELALLOC; |
| 604 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 605 | if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY)) |
| 606 | clear = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 607 | again: |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 608 | if (!prealloc && gfpflags_allow_blocking(mask)) { |
Filipe Manana | c7bc631 | 2014-11-03 14:12:57 +0000 | [diff] [blame] | 609 | /* |
| 610 | * Don't care for allocation failure here because we might end |
| 611 | * up not needing the pre-allocated extent state at all, which |
| 612 | * is the case if we only have in the tree extent states that |
| 613 | * cover our input range and don't cover too any other range. |
| 614 | * If we end up needing a new extent state we allocate it later. |
| 615 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 616 | prealloc = alloc_extent_state(mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 617 | } |
| 618 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 619 | spin_lock(&tree->lock); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 620 | if (cached_state) { |
| 621 | cached = *cached_state; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 622 | |
| 623 | if (clear) { |
| 624 | *cached_state = NULL; |
| 625 | cached_state = NULL; |
| 626 | } |
| 627 | |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 628 | if (cached && extent_state_in_tree(cached) && |
| 629 | cached->start <= start && cached->end > start) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 630 | if (clear) |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 631 | refcount_dec(&cached->refs); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 632 | state = cached; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 633 | goto hit_next; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 634 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 635 | if (clear) |
| 636 | free_extent_state(cached); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 637 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 638 | /* |
| 639 | * this search will find the extents that end after |
| 640 | * our range starts |
| 641 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 642 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 643 | if (!node) |
| 644 | goto out; |
| 645 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 646 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 647 | if (state->start > end) |
| 648 | goto out; |
| 649 | WARN_ON(state->end < start); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 650 | last_end = state->end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 651 | |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 652 | /* the state doesn't have the wanted bits, go ahead */ |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 653 | if (!(state->state & bits)) { |
| 654 | state = next_state(state); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 655 | goto next; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 656 | } |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 657 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 658 | /* |
| 659 | * | ---- desired range ---- | |
| 660 | * | state | or |
| 661 | * | ------------- state -------------- | |
| 662 | * |
| 663 | * We need to split the extent we found, and may flip |
| 664 | * bits on second half. |
| 665 | * |
| 666 | * If the extent we found extends past our range, we |
| 667 | * just split and search again. It'll get split again |
| 668 | * the next time though. |
| 669 | * |
| 670 | * If the extent we found is inside our range, we clear |
| 671 | * the desired bit on it. |
| 672 | */ |
| 673 | |
| 674 | if (state->start < start) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 675 | prealloc = alloc_extent_state_atomic(prealloc); |
| 676 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 677 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 678 | if (err) |
| 679 | extent_io_tree_panic(tree, err); |
| 680 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 681 | prealloc = NULL; |
| 682 | if (err) |
| 683 | goto out; |
| 684 | if (state->end <= end) { |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 685 | state = clear_state_bit(tree, state, &bits, wake, |
| 686 | changeset); |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 687 | goto next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 688 | } |
| 689 | goto search_again; |
| 690 | } |
| 691 | /* |
| 692 | * | ---- desired range ---- | |
| 693 | * | state | |
| 694 | * We need to split the extent, and clear the bit |
| 695 | * on the first half |
| 696 | */ |
| 697 | if (state->start <= end && state->end > end) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 698 | prealloc = alloc_extent_state_atomic(prealloc); |
| 699 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 700 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 701 | if (err) |
| 702 | extent_io_tree_panic(tree, err); |
| 703 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 704 | if (wake) |
| 705 | wake_up(&state->wq); |
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 | clear_state_bit(tree, prealloc, &bits, wake, changeset); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 708 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 709 | prealloc = NULL; |
| 710 | goto out; |
| 711 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 712 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 713 | state = clear_state_bit(tree, state, &bits, wake, changeset); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 714 | next: |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 715 | if (last_end == (u64)-1) |
| 716 | goto out; |
| 717 | start = last_end + 1; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 718 | if (start <= end && state && !need_resched()) |
Liu Bo | 692e575 | 2012-02-16 18:34:36 +0800 | [diff] [blame] | 719 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 720 | |
| 721 | search_again: |
| 722 | if (start > end) |
| 723 | goto out; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 724 | spin_unlock(&tree->lock); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 725 | if (gfpflags_allow_blocking(mask)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 726 | cond_resched(); |
| 727 | goto again; |
David Sterba | 7ab5cb2 | 2016-04-27 01:02:15 +0200 | [diff] [blame] | 728 | |
| 729 | out: |
| 730 | spin_unlock(&tree->lock); |
| 731 | if (prealloc) |
| 732 | free_extent_state(prealloc); |
| 733 | |
| 734 | return 0; |
| 735 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 736 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 737 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 738 | static void wait_on_state(struct extent_io_tree *tree, |
| 739 | struct extent_state *state) |
Christoph Hellwig | 641f521 | 2008-12-02 06:36:10 -0500 | [diff] [blame] | 740 | __releases(tree->lock) |
| 741 | __acquires(tree->lock) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 742 | { |
| 743 | DEFINE_WAIT(wait); |
| 744 | prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 745 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 746 | schedule(); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 747 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 748 | finish_wait(&state->wq, &wait); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | /* |
| 752 | * waits for one or more bits to clear on a range in the state tree. |
| 753 | * The range [start, end] is inclusive. |
| 754 | * The tree lock is taken by this function |
| 755 | */ |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 756 | static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 757 | unsigned long bits) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 758 | { |
| 759 | struct extent_state *state; |
| 760 | struct rb_node *node; |
| 761 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 762 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 763 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 764 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 765 | again: |
| 766 | while (1) { |
| 767 | /* |
| 768 | * this search will find all the extents that end after |
| 769 | * our range starts |
| 770 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 771 | node = tree_search(tree, start); |
Filipe Manana | c50d3e7 | 2014-03-31 14:53:25 +0100 | [diff] [blame] | 772 | process_node: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 773 | if (!node) |
| 774 | break; |
| 775 | |
| 776 | state = rb_entry(node, struct extent_state, rb_node); |
| 777 | |
| 778 | if (state->start > end) |
| 779 | goto out; |
| 780 | |
| 781 | if (state->state & bits) { |
| 782 | start = state->start; |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 783 | refcount_inc(&state->refs); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 784 | wait_on_state(tree, state); |
| 785 | free_extent_state(state); |
| 786 | goto again; |
| 787 | } |
| 788 | start = state->end + 1; |
| 789 | |
| 790 | if (start > end) |
| 791 | break; |
| 792 | |
Filipe Manana | c50d3e7 | 2014-03-31 14:53:25 +0100 | [diff] [blame] | 793 | if (!cond_resched_lock(&tree->lock)) { |
| 794 | node = rb_next(node); |
| 795 | goto process_node; |
| 796 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 797 | } |
| 798 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 799 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 800 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 801 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 802 | static void set_state_bits(struct extent_io_tree *tree, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 803 | struct extent_state *state, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 804 | unsigned *bits, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 805 | { |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 806 | unsigned bits_to_set = *bits & ~EXTENT_CTLBITS; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 807 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 808 | set_state_cb(tree, state, bits); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 809 | if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 810 | u64 range = state->end - state->start + 1; |
| 811 | tree->dirty_bytes += range; |
| 812 | } |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 813 | add_extent_changeset(state, bits_to_set, changeset, 1); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 814 | state->state |= bits_to_set; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 815 | } |
| 816 | |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 817 | static void cache_state_if_flags(struct extent_state *state, |
| 818 | struct extent_state **cached_ptr, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 819 | unsigned flags) |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 820 | { |
| 821 | if (cached_ptr && !(*cached_ptr)) { |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 822 | if (!flags || (state->state & flags)) { |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 823 | *cached_ptr = state; |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 824 | refcount_inc(&state->refs); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | } |
| 828 | |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 829 | static void cache_state(struct extent_state *state, |
| 830 | struct extent_state **cached_ptr) |
| 831 | { |
| 832 | return cache_state_if_flags(state, cached_ptr, |
| 833 | EXTENT_IOBITS | EXTENT_BOUNDARY); |
| 834 | } |
| 835 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 836 | /* |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 837 | * set some bits on a range in the tree. This may require allocations or |
| 838 | * sleeping, so the gfp mask is used to indicate what is allowed. |
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 | * If any of the exclusive bits are set, this will fail with -EEXIST if some |
| 841 | * part of the range already has the desired bits set. The start of the |
| 842 | * existing range is returned in failed_start in this case. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 843 | * |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 844 | * [start, end] is inclusive This takes the tree lock. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 845 | */ |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 846 | |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 847 | static int __must_check |
| 848 | __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 849 | unsigned bits, unsigned exclusive_bits, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 850 | u64 *failed_start, struct extent_state **cached_state, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 851 | gfp_t mask, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 852 | { |
| 853 | struct extent_state *state; |
| 854 | struct extent_state *prealloc = NULL; |
| 855 | struct rb_node *node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 856 | struct rb_node **p; |
| 857 | struct rb_node *parent; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 858 | int err = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 859 | u64 last_start; |
| 860 | u64 last_end; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 861 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 862 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 863 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 864 | bits |= EXTENT_FIRST_DELALLOC; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 865 | again: |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 866 | if (!prealloc && gfpflags_allow_blocking(mask)) { |
David Sterba | 059f791 | 2016-04-27 01:03:45 +0200 | [diff] [blame] | 867 | /* |
| 868 | * Don't care for allocation failure here because we might end |
| 869 | * up not needing the pre-allocated extent state at all, which |
| 870 | * is the case if we only have in the tree extent states that |
| 871 | * cover our input range and don't cover too any other range. |
| 872 | * If we end up needing a new extent state we allocate it later. |
| 873 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 874 | prealloc = alloc_extent_state(mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 875 | } |
| 876 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 877 | spin_lock(&tree->lock); |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 878 | if (cached_state && *cached_state) { |
| 879 | state = *cached_state; |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 880 | if (state->start <= start && state->end > start && |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 881 | extent_state_in_tree(state)) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 882 | node = &state->rb_node; |
| 883 | goto hit_next; |
| 884 | } |
| 885 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 886 | /* |
| 887 | * this search will find all the extents that end after |
| 888 | * our range starts. |
| 889 | */ |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 890 | node = tree_search_for_insert(tree, start, &p, &parent); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 891 | if (!node) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 892 | prealloc = alloc_extent_state_atomic(prealloc); |
| 893 | BUG_ON(!prealloc); |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 894 | err = insert_state(tree, prealloc, start, end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 895 | &p, &parent, &bits, changeset); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 896 | if (err) |
| 897 | extent_io_tree_panic(tree, err); |
| 898 | |
Filipe David Borba Manana | c42ac0b | 2013-11-26 15:01:34 +0000 | [diff] [blame] | 899 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 900 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 901 | goto out; |
| 902 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 903 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 40431d6 | 2009-08-05 12:57:59 -0400 | [diff] [blame] | 904 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 905 | last_start = state->start; |
| 906 | last_end = state->end; |
| 907 | |
| 908 | /* |
| 909 | * | ---- desired range ---- | |
| 910 | * | state | |
| 911 | * |
| 912 | * Just lock what we found and keep going |
| 913 | */ |
| 914 | if (state->start == start && state->end <= end) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 915 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 916 | *failed_start = state->start; |
| 917 | err = -EEXIST; |
| 918 | goto out; |
| 919 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 920 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 921 | set_state_bits(tree, state, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 922 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 923 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 924 | if (last_end == (u64)-1) |
| 925 | goto out; |
| 926 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 927 | state = next_state(state); |
| 928 | if (start < end && state && state->start == start && |
| 929 | !need_resched()) |
| 930 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 931 | goto search_again; |
| 932 | } |
| 933 | |
| 934 | /* |
| 935 | * | ---- desired range ---- | |
| 936 | * | state | |
| 937 | * or |
| 938 | * | ------------- state -------------- | |
| 939 | * |
| 940 | * We need to split the extent we found, and may flip bits on |
| 941 | * second half. |
| 942 | * |
| 943 | * If the extent we found extends past our |
| 944 | * range, we just split and search again. It'll get split |
| 945 | * again the next time though. |
| 946 | * |
| 947 | * If the extent we found is inside our range, we set the |
| 948 | * desired bit on it. |
| 949 | */ |
| 950 | if (state->start < start) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 951 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 952 | *failed_start = start; |
| 953 | err = -EEXIST; |
| 954 | goto out; |
| 955 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 956 | |
| 957 | prealloc = alloc_extent_state_atomic(prealloc); |
| 958 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 959 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 960 | if (err) |
| 961 | extent_io_tree_panic(tree, err); |
| 962 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 963 | prealloc = NULL; |
| 964 | if (err) |
| 965 | goto out; |
| 966 | if (state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 967 | set_state_bits(tree, state, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 968 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 969 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 970 | if (last_end == (u64)-1) |
| 971 | goto out; |
| 972 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 973 | state = next_state(state); |
| 974 | if (start < end && state && state->start == start && |
| 975 | !need_resched()) |
| 976 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 977 | } |
| 978 | goto search_again; |
| 979 | } |
| 980 | /* |
| 981 | * | ---- desired range ---- | |
| 982 | * | state | or | state | |
| 983 | * |
| 984 | * There's a hole, we need to insert something in it and |
| 985 | * ignore the extent we found. |
| 986 | */ |
| 987 | if (state->start > start) { |
| 988 | u64 this_end; |
| 989 | if (end < last_start) |
| 990 | this_end = end; |
| 991 | else |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 992 | this_end = last_start - 1; |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 993 | |
| 994 | prealloc = alloc_extent_state_atomic(prealloc); |
| 995 | BUG_ON(!prealloc); |
Xiao Guangrong | c7f895a | 2011-04-20 06:45:49 +0000 | [diff] [blame] | 996 | |
| 997 | /* |
| 998 | * Avoid to free 'prealloc' if it can be merged with |
| 999 | * the later extent. |
| 1000 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1001 | err = insert_state(tree, prealloc, start, this_end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1002 | NULL, NULL, &bits, changeset); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1003 | if (err) |
| 1004 | extent_io_tree_panic(tree, err); |
| 1005 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1006 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1007 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1008 | start = this_end + 1; |
| 1009 | goto search_again; |
| 1010 | } |
| 1011 | /* |
| 1012 | * | ---- desired range ---- | |
| 1013 | * | state | |
| 1014 | * We need to split the extent, and set the bit |
| 1015 | * on the first half |
| 1016 | */ |
| 1017 | if (state->start <= end && state->end > end) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1018 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1019 | *failed_start = start; |
| 1020 | err = -EEXIST; |
| 1021 | goto out; |
| 1022 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 1023 | |
| 1024 | prealloc = alloc_extent_state_atomic(prealloc); |
| 1025 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1026 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1027 | if (err) |
| 1028 | extent_io_tree_panic(tree, err); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1029 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1030 | set_state_bits(tree, prealloc, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1031 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1032 | merge_state(tree, prealloc); |
| 1033 | prealloc = NULL; |
| 1034 | goto out; |
| 1035 | } |
| 1036 | |
David Sterba | b5a4ba14 | 2016-04-27 01:02:15 +0200 | [diff] [blame] | 1037 | search_again: |
| 1038 | if (start > end) |
| 1039 | goto out; |
| 1040 | spin_unlock(&tree->lock); |
| 1041 | if (gfpflags_allow_blocking(mask)) |
| 1042 | cond_resched(); |
| 1043 | goto again; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1044 | |
| 1045 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1046 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1047 | if (prealloc) |
| 1048 | free_extent_state(prealloc); |
| 1049 | |
| 1050 | return err; |
| 1051 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1052 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1053 | |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1054 | 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] | 1055 | unsigned bits, u64 * failed_start, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1056 | struct extent_state **cached_state, gfp_t mask) |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1057 | { |
| 1058 | return __set_extent_bit(tree, start, end, bits, 0, failed_start, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1059 | cached_state, mask, NULL); |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1063 | /** |
Liu Bo | 10983f2 | 2012-07-11 15:26:19 +0800 | [diff] [blame] | 1064 | * convert_extent_bit - convert all bits in a given range from one bit to |
| 1065 | * another |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1066 | * @tree: the io tree to search |
| 1067 | * @start: the start offset in bytes |
| 1068 | * @end: the end offset in bytes (inclusive) |
| 1069 | * @bits: the bits to set in this range |
| 1070 | * @clear_bits: the bits to clear in this range |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1071 | * @cached_state: state that we're going to cache |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1072 | * |
| 1073 | * This will go through and set bits for the given range. If any states exist |
| 1074 | * already in this range they are set with the given bit and cleared of the |
| 1075 | * clear_bits. This is only meant to be used by things that are mergeable, ie |
| 1076 | * converting from say DELALLOC to DIRTY. This is not meant to be used with |
| 1077 | * boundary bits like LOCK. |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1078 | * |
| 1079 | * All allocations are done with GFP_NOFS. |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1080 | */ |
| 1081 | 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] | 1082 | unsigned bits, unsigned clear_bits, |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1083 | struct extent_state **cached_state) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1084 | { |
| 1085 | struct extent_state *state; |
| 1086 | struct extent_state *prealloc = NULL; |
| 1087 | struct rb_node *node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1088 | struct rb_node **p; |
| 1089 | struct rb_node *parent; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1090 | int err = 0; |
| 1091 | u64 last_start; |
| 1092 | u64 last_end; |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1093 | bool first_iteration = true; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1094 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 1095 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 1096 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1097 | again: |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1098 | if (!prealloc) { |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1099 | /* |
| 1100 | * Best effort, don't worry if extent state allocation fails |
| 1101 | * here for the first iteration. We might have a cached state |
| 1102 | * that matches exactly the target range, in which case no |
| 1103 | * extent state allocations are needed. We'll only know this |
| 1104 | * after locking the tree. |
| 1105 | */ |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1106 | prealloc = alloc_extent_state(GFP_NOFS); |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1107 | if (!prealloc && !first_iteration) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1108 | return -ENOMEM; |
| 1109 | } |
| 1110 | |
| 1111 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1112 | if (cached_state && *cached_state) { |
| 1113 | state = *cached_state; |
| 1114 | if (state->start <= start && state->end > start && |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1115 | extent_state_in_tree(state)) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1116 | node = &state->rb_node; |
| 1117 | goto hit_next; |
| 1118 | } |
| 1119 | } |
| 1120 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1121 | /* |
| 1122 | * this search will find all the extents that end after |
| 1123 | * our range starts. |
| 1124 | */ |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1125 | node = tree_search_for_insert(tree, start, &p, &parent); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1126 | if (!node) { |
| 1127 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1128 | if (!prealloc) { |
| 1129 | err = -ENOMEM; |
| 1130 | goto out; |
| 1131 | } |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1132 | err = insert_state(tree, prealloc, start, end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1133 | &p, &parent, &bits, NULL); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1134 | if (err) |
| 1135 | extent_io_tree_panic(tree, err); |
Filipe David Borba Manana | c42ac0b | 2013-11-26 15:01:34 +0000 | [diff] [blame] | 1136 | cache_state(prealloc, cached_state); |
| 1137 | prealloc = NULL; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1138 | goto out; |
| 1139 | } |
| 1140 | state = rb_entry(node, struct extent_state, rb_node); |
| 1141 | hit_next: |
| 1142 | last_start = state->start; |
| 1143 | last_end = state->end; |
| 1144 | |
| 1145 | /* |
| 1146 | * | ---- desired range ---- | |
| 1147 | * | state | |
| 1148 | * |
| 1149 | * Just lock what we found and keep going |
| 1150 | */ |
| 1151 | if (state->start == start && state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1152 | set_state_bits(tree, state, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1153 | cache_state(state, cached_state); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1154 | state = clear_state_bit(tree, state, &clear_bits, 0, NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1155 | if (last_end == (u64)-1) |
| 1156 | goto out; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1157 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1158 | if (start < end && state && state->start == start && |
| 1159 | !need_resched()) |
| 1160 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1161 | goto search_again; |
| 1162 | } |
| 1163 | |
| 1164 | /* |
| 1165 | * | ---- desired range ---- | |
| 1166 | * | state | |
| 1167 | * or |
| 1168 | * | ------------- state -------------- | |
| 1169 | * |
| 1170 | * We need to split the extent we found, and may flip bits on |
| 1171 | * second half. |
| 1172 | * |
| 1173 | * If the extent we found extends past our |
| 1174 | * range, we just split and search again. It'll get split |
| 1175 | * again the next time though. |
| 1176 | * |
| 1177 | * If the extent we found is inside our range, we set the |
| 1178 | * desired bit on it. |
| 1179 | */ |
| 1180 | if (state->start < start) { |
| 1181 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1182 | if (!prealloc) { |
| 1183 | err = -ENOMEM; |
| 1184 | goto out; |
| 1185 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1186 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1187 | if (err) |
| 1188 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1189 | prealloc = NULL; |
| 1190 | if (err) |
| 1191 | goto out; |
| 1192 | if (state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1193 | set_state_bits(tree, state, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1194 | cache_state(state, cached_state); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1195 | state = clear_state_bit(tree, state, &clear_bits, 0, |
| 1196 | NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1197 | if (last_end == (u64)-1) |
| 1198 | goto out; |
| 1199 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1200 | if (start < end && state && state->start == start && |
| 1201 | !need_resched()) |
| 1202 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1203 | } |
| 1204 | goto search_again; |
| 1205 | } |
| 1206 | /* |
| 1207 | * | ---- desired range ---- | |
| 1208 | * | state | or | state | |
| 1209 | * |
| 1210 | * There's a hole, we need to insert something in it and |
| 1211 | * ignore the extent we found. |
| 1212 | */ |
| 1213 | if (state->start > start) { |
| 1214 | u64 this_end; |
| 1215 | if (end < last_start) |
| 1216 | this_end = end; |
| 1217 | else |
| 1218 | this_end = last_start - 1; |
| 1219 | |
| 1220 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1221 | if (!prealloc) { |
| 1222 | err = -ENOMEM; |
| 1223 | goto out; |
| 1224 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1225 | |
| 1226 | /* |
| 1227 | * Avoid to free 'prealloc' if it can be merged with |
| 1228 | * the later extent. |
| 1229 | */ |
| 1230 | err = insert_state(tree, prealloc, start, this_end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1231 | NULL, NULL, &bits, NULL); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1232 | if (err) |
| 1233 | extent_io_tree_panic(tree, err); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1234 | cache_state(prealloc, cached_state); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1235 | prealloc = NULL; |
| 1236 | start = this_end + 1; |
| 1237 | goto search_again; |
| 1238 | } |
| 1239 | /* |
| 1240 | * | ---- desired range ---- | |
| 1241 | * | state | |
| 1242 | * We need to split the extent, and set the bit |
| 1243 | * on the first half |
| 1244 | */ |
| 1245 | if (state->start <= end && state->end > end) { |
| 1246 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1247 | if (!prealloc) { |
| 1248 | err = -ENOMEM; |
| 1249 | goto out; |
| 1250 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1251 | |
| 1252 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1253 | if (err) |
| 1254 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1255 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1256 | set_state_bits(tree, prealloc, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1257 | cache_state(prealloc, cached_state); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1258 | clear_state_bit(tree, prealloc, &clear_bits, 0, NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1259 | prealloc = NULL; |
| 1260 | goto out; |
| 1261 | } |
| 1262 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1263 | search_again: |
| 1264 | if (start > end) |
| 1265 | goto out; |
| 1266 | spin_unlock(&tree->lock); |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1267 | cond_resched(); |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1268 | first_iteration = false; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1269 | goto again; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1270 | |
| 1271 | out: |
| 1272 | spin_unlock(&tree->lock); |
| 1273 | if (prealloc) |
| 1274 | free_extent_state(prealloc); |
| 1275 | |
| 1276 | return err; |
| 1277 | } |
| 1278 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1279 | /* wrappers around set/clear extent bit */ |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1280 | 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] | 1281 | unsigned bits, struct extent_changeset *changeset) |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1282 | { |
| 1283 | /* |
| 1284 | * We don't support EXTENT_LOCKED yet, as current changeset will |
| 1285 | * record any bits changed, so for EXTENT_LOCKED case, it will |
| 1286 | * either fail with -EEXIST or changeset will record the whole |
| 1287 | * range. |
| 1288 | */ |
| 1289 | BUG_ON(bits & EXTENT_LOCKED); |
| 1290 | |
David Sterba | 2c53b91 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1291 | 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] | 1292 | changeset); |
| 1293 | } |
| 1294 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1295 | int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 1296 | unsigned bits, int wake, int delete, |
| 1297 | struct extent_state **cached, gfp_t mask) |
| 1298 | { |
| 1299 | return __clear_extent_bit(tree, start, end, bits, wake, delete, |
| 1300 | cached, mask, NULL); |
| 1301 | } |
| 1302 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1303 | 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] | 1304 | unsigned bits, struct extent_changeset *changeset) |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1305 | { |
| 1306 | /* |
| 1307 | * Don't support EXTENT_LOCKED case, same reason as |
| 1308 | * set_record_extent_bits(). |
| 1309 | */ |
| 1310 | BUG_ON(bits & EXTENT_LOCKED); |
| 1311 | |
David Sterba | f734c44 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1312 | 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] | 1313 | changeset); |
| 1314 | } |
| 1315 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1316 | /* |
| 1317 | * either insert or lock state struct between start and end use mask to tell |
| 1318 | * us if waiting is desired. |
| 1319 | */ |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1320 | 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] | 1321 | struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1322 | { |
| 1323 | int err; |
| 1324 | u64 failed_start; |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1325 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1326 | while (1) { |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1327 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1328 | EXTENT_LOCKED, &failed_start, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1329 | cached_state, GFP_NOFS, NULL); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1330 | if (err == -EEXIST) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1331 | wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED); |
| 1332 | start = failed_start; |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1333 | } else |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1334 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1335 | WARN_ON(start > end); |
| 1336 | } |
| 1337 | return err; |
| 1338 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1339 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1340 | 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] | 1341 | { |
| 1342 | int err; |
| 1343 | u64 failed_start; |
| 1344 | |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1345 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1346 | &failed_start, NULL, GFP_NOFS, NULL); |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1347 | if (err == -EEXIST) { |
| 1348 | if (failed_start > start) |
| 1349 | clear_extent_bit(tree, start, failed_start - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1350 | EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1351 | return 0; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1352 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1353 | return 1; |
| 1354 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1355 | |
David Sterba | bd1fa4f | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 1356 | 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] | 1357 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1358 | unsigned long index = start >> PAGE_SHIFT; |
| 1359 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1360 | struct page *page; |
| 1361 | |
| 1362 | while (index <= end_index) { |
| 1363 | page = find_get_page(inode->i_mapping, index); |
| 1364 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
| 1365 | clear_page_dirty_for_io(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1366 | put_page(page); |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1367 | index++; |
| 1368 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1369 | } |
| 1370 | |
David Sterba | f631157 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 1371 | 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] | 1372 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1373 | unsigned long index = start >> PAGE_SHIFT; |
| 1374 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1375 | struct page *page; |
| 1376 | |
| 1377 | while (index <= end_index) { |
| 1378 | page = find_get_page(inode->i_mapping, index); |
| 1379 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1380 | __set_page_dirty_nobuffers(page); |
Konstantin Khebnikov | 8d38633 | 2015-02-11 15:26:55 -0800 | [diff] [blame] | 1381 | account_page_redirty(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1382 | put_page(page); |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1383 | index++; |
| 1384 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1385 | } |
| 1386 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1387 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1388 | * helper function to set both pages and extents in the tree writeback |
| 1389 | */ |
David Sterba | 35de6db | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 1390 | static void set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1391 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1392 | tree->ops->set_range_writeback(tree->private_data, start, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1393 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1394 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1395 | /* find the first state struct with 'bits' set after 'start', and |
| 1396 | * return it. tree->lock must be held. NULL will returned if |
| 1397 | * nothing was found after 'start' |
| 1398 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1399 | static struct extent_state * |
| 1400 | find_first_extent_bit_state(struct extent_io_tree *tree, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1401 | u64 start, unsigned bits) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1402 | { |
| 1403 | struct rb_node *node; |
| 1404 | struct extent_state *state; |
| 1405 | |
| 1406 | /* |
| 1407 | * this search will find all the extents that end after |
| 1408 | * our range starts. |
| 1409 | */ |
| 1410 | node = tree_search(tree, start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1411 | if (!node) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1412 | goto out; |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1413 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1414 | while (1) { |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1415 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1416 | if (state->end >= start && (state->state & bits)) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1417 | return state; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1418 | |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1419 | node = rb_next(node); |
| 1420 | if (!node) |
| 1421 | break; |
| 1422 | } |
| 1423 | out: |
| 1424 | return NULL; |
| 1425 | } |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1426 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1427 | /* |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1428 | * find the first offset in the io tree with 'bits' set. zero is |
| 1429 | * returned if we find something, and *start_ret and *end_ret are |
| 1430 | * set to reflect the state struct that was found. |
| 1431 | * |
Wang Sheng-Hui | 477d7ea | 2012-04-06 14:35:47 +0800 | [diff] [blame] | 1432 | * If nothing was found, 1 is returned. If found something, return 0. |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1433 | */ |
| 1434 | int find_first_extent_bit(struct extent_io_tree *tree, u64 start, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1435 | u64 *start_ret, u64 *end_ret, unsigned bits, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1436 | struct extent_state **cached_state) |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1437 | { |
| 1438 | struct extent_state *state; |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1439 | struct rb_node *n; |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1440 | int ret = 1; |
| 1441 | |
| 1442 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1443 | if (cached_state && *cached_state) { |
| 1444 | state = *cached_state; |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1445 | if (state->end == start - 1 && extent_state_in_tree(state)) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1446 | n = rb_next(&state->rb_node); |
| 1447 | while (n) { |
| 1448 | state = rb_entry(n, struct extent_state, |
| 1449 | rb_node); |
| 1450 | if (state->state & bits) |
| 1451 | goto got_it; |
| 1452 | n = rb_next(n); |
| 1453 | } |
| 1454 | free_extent_state(*cached_state); |
| 1455 | *cached_state = NULL; |
| 1456 | goto out; |
| 1457 | } |
| 1458 | free_extent_state(*cached_state); |
| 1459 | *cached_state = NULL; |
| 1460 | } |
| 1461 | |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1462 | state = find_first_extent_bit_state(tree, start, bits); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1463 | got_it: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1464 | if (state) { |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 1465 | cache_state_if_flags(state, cached_state, 0); |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1466 | *start_ret = state->start; |
| 1467 | *end_ret = state->end; |
| 1468 | ret = 0; |
| 1469 | } |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1470 | out: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1471 | spin_unlock(&tree->lock); |
| 1472 | return ret; |
| 1473 | } |
| 1474 | |
| 1475 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1476 | * find a contiguous range of bytes in the file marked as delalloc, not |
| 1477 | * more than 'max_bytes'. start and end are used to return the range, |
| 1478 | * |
| 1479 | * 1 is returned if we find something, 0 if nothing was in the tree |
| 1480 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1481 | static noinline u64 find_delalloc_range(struct extent_io_tree *tree, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1482 | u64 *start, u64 *end, u64 max_bytes, |
| 1483 | struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1484 | { |
| 1485 | struct rb_node *node; |
| 1486 | struct extent_state *state; |
| 1487 | u64 cur_start = *start; |
| 1488 | u64 found = 0; |
| 1489 | u64 total_bytes = 0; |
| 1490 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1491 | spin_lock(&tree->lock); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1492 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1493 | /* |
| 1494 | * this search will find all the extents that end after |
| 1495 | * our range starts. |
| 1496 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1497 | node = tree_search(tree, cur_start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1498 | if (!node) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1499 | if (!found) |
| 1500 | *end = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1501 | goto out; |
| 1502 | } |
| 1503 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1504 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1505 | state = rb_entry(node, struct extent_state, rb_node); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1506 | if (found && (state->start != cur_start || |
| 1507 | (state->state & EXTENT_BOUNDARY))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1508 | goto out; |
| 1509 | } |
| 1510 | if (!(state->state & EXTENT_DELALLOC)) { |
| 1511 | if (!found) |
| 1512 | *end = state->end; |
| 1513 | goto out; |
| 1514 | } |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1515 | if (!found) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1516 | *start = state->start; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1517 | *cached_state = state; |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 1518 | refcount_inc(&state->refs); |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1519 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1520 | found++; |
| 1521 | *end = state->end; |
| 1522 | cur_start = state->end + 1; |
| 1523 | node = rb_next(node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1524 | total_bytes += state->end - state->start + 1; |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 1525 | if (total_bytes >= max_bytes) |
Josef Bacik | 573aeca | 2013-08-30 14:38:49 -0400 | [diff] [blame] | 1526 | break; |
Josef Bacik | 573aeca | 2013-08-30 14:38:49 -0400 | [diff] [blame] | 1527 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1528 | break; |
| 1529 | } |
| 1530 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1531 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1532 | return found; |
| 1533 | } |
| 1534 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1535 | static int __process_pages_contig(struct address_space *mapping, |
| 1536 | struct page *locked_page, |
| 1537 | pgoff_t start_index, pgoff_t end_index, |
| 1538 | unsigned long page_ops, pgoff_t *index_ret); |
| 1539 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1540 | static noinline void __unlock_for_delalloc(struct inode *inode, |
| 1541 | struct page *locked_page, |
| 1542 | u64 start, u64 end) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1543 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1544 | unsigned long index = start >> PAGE_SHIFT; |
| 1545 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1546 | |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1547 | ASSERT(locked_page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1548 | if (index == locked_page->index && end_index == index) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1549 | return; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1550 | |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1551 | __process_pages_contig(inode->i_mapping, locked_page, index, end_index, |
| 1552 | PAGE_UNLOCK, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | static noinline int lock_delalloc_pages(struct inode *inode, |
| 1556 | struct page *locked_page, |
| 1557 | u64 delalloc_start, |
| 1558 | u64 delalloc_end) |
| 1559 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1560 | unsigned long index = delalloc_start >> PAGE_SHIFT; |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1561 | unsigned long index_ret = index; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1562 | unsigned long end_index = delalloc_end >> PAGE_SHIFT; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1563 | int ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1564 | |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1565 | ASSERT(locked_page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1566 | if (index == locked_page->index && index == end_index) |
| 1567 | return 0; |
| 1568 | |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1569 | ret = __process_pages_contig(inode->i_mapping, locked_page, index, |
| 1570 | end_index, PAGE_LOCK, &index_ret); |
| 1571 | if (ret == -EAGAIN) |
| 1572 | __unlock_for_delalloc(inode, locked_page, delalloc_start, |
| 1573 | (u64)index_ret << PAGE_SHIFT); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1574 | return ret; |
| 1575 | } |
| 1576 | |
| 1577 | /* |
| 1578 | * find a contiguous range of bytes in the file marked as delalloc, not |
| 1579 | * more than 'max_bytes'. start and end are used to return the range, |
| 1580 | * |
| 1581 | * 1 is returned if we find something, 0 if nothing was in the tree |
| 1582 | */ |
Josef Bacik | 294e30f | 2013-10-09 12:00:56 -0400 | [diff] [blame] | 1583 | STATIC u64 find_lock_delalloc_range(struct inode *inode, |
| 1584 | struct extent_io_tree *tree, |
| 1585 | struct page *locked_page, u64 *start, |
| 1586 | u64 *end, u64 max_bytes) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1587 | { |
| 1588 | u64 delalloc_start; |
| 1589 | u64 delalloc_end; |
| 1590 | u64 found; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1591 | struct extent_state *cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1592 | int ret; |
| 1593 | int loops = 0; |
| 1594 | |
| 1595 | again: |
| 1596 | /* step one, find a bunch of delalloc bytes starting at start */ |
| 1597 | delalloc_start = *start; |
| 1598 | delalloc_end = 0; |
| 1599 | found = find_delalloc_range(tree, &delalloc_start, &delalloc_end, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1600 | max_bytes, &cached_state); |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1601 | if (!found || delalloc_end <= *start) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1602 | *start = delalloc_start; |
| 1603 | *end = delalloc_end; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1604 | free_extent_state(cached_state); |
Liu Bo | 385fe0b | 2013-10-01 23:49:49 +0800 | [diff] [blame] | 1605 | return 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | /* |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1609 | * start comes from the offset of locked_page. We have to lock |
| 1610 | * pages in order, so we can't process delalloc bytes before |
| 1611 | * locked_page |
| 1612 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1613 | if (delalloc_start < *start) |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1614 | delalloc_start = *start; |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1615 | |
| 1616 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1617 | * 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] | 1618 | */ |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 1619 | if (delalloc_end + 1 - delalloc_start > max_bytes) |
| 1620 | delalloc_end = delalloc_start + max_bytes - 1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1621 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1622 | /* step two, lock all the pages after the page that has start */ |
| 1623 | ret = lock_delalloc_pages(inode, locked_page, |
| 1624 | delalloc_start, delalloc_end); |
| 1625 | if (ret == -EAGAIN) { |
| 1626 | /* some of the pages are gone, lets avoid looping by |
| 1627 | * shortening the size of the delalloc range we're searching |
| 1628 | */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1629 | free_extent_state(cached_state); |
Chris Mason | 7d78874 | 2014-05-21 05:49:54 -0700 | [diff] [blame] | 1630 | cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1631 | if (!loops) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1632 | max_bytes = PAGE_SIZE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1633 | loops = 1; |
| 1634 | goto again; |
| 1635 | } else { |
| 1636 | found = 0; |
| 1637 | goto out_failed; |
| 1638 | } |
| 1639 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1640 | BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1641 | |
| 1642 | /* step three, lock the state bits for the whole range */ |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1643 | lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1644 | |
| 1645 | /* then test to make sure it is all still delalloc */ |
| 1646 | ret = test_range_bit(tree, delalloc_start, delalloc_end, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1647 | EXTENT_DELALLOC, 1, cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1648 | if (!ret) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1649 | unlock_extent_cached(tree, delalloc_start, delalloc_end, |
| 1650 | &cached_state, GFP_NOFS); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1651 | __unlock_for_delalloc(inode, locked_page, |
| 1652 | delalloc_start, delalloc_end); |
| 1653 | cond_resched(); |
| 1654 | goto again; |
| 1655 | } |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1656 | free_extent_state(cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1657 | *start = delalloc_start; |
| 1658 | *end = delalloc_end; |
| 1659 | out_failed: |
| 1660 | return found; |
| 1661 | } |
| 1662 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1663 | static int __process_pages_contig(struct address_space *mapping, |
| 1664 | struct page *locked_page, |
| 1665 | pgoff_t start_index, pgoff_t end_index, |
| 1666 | unsigned long page_ops, pgoff_t *index_ret) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1667 | { |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1668 | unsigned long nr_pages = end_index - start_index + 1; |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1669 | unsigned long pages_locked = 0; |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1670 | pgoff_t index = start_index; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1671 | struct page *pages[16]; |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1672 | unsigned ret; |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1673 | int err = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1674 | int i; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1675 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1676 | if (page_ops & PAGE_LOCK) { |
| 1677 | ASSERT(page_ops == PAGE_LOCK); |
| 1678 | ASSERT(index_ret && *index_ret == start_index); |
| 1679 | } |
| 1680 | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1681 | if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0) |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1682 | mapping_set_error(mapping, -EIO); |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1683 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1684 | while (nr_pages > 0) { |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1685 | ret = find_get_pages_contig(mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1686 | min_t(unsigned long, |
| 1687 | nr_pages, ARRAY_SIZE(pages)), pages); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1688 | if (ret == 0) { |
| 1689 | /* |
| 1690 | * Only if we're going to lock these pages, |
| 1691 | * can we find nothing at @index. |
| 1692 | */ |
| 1693 | ASSERT(page_ops & PAGE_LOCK); |
Liu Bo | 49d4a33 | 2017-03-06 18:20:56 -0800 | [diff] [blame] | 1694 | err = -EAGAIN; |
| 1695 | goto out; |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1696 | } |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1697 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1698 | for (i = 0; i < ret; i++) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1699 | if (page_ops & PAGE_SET_PRIVATE2) |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1700 | SetPagePrivate2(pages[i]); |
| 1701 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1702 | if (pages[i] == locked_page) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1703 | put_page(pages[i]); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1704 | pages_locked++; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1705 | continue; |
| 1706 | } |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1707 | if (page_ops & PAGE_CLEAR_DIRTY) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1708 | clear_page_dirty_for_io(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1709 | if (page_ops & PAGE_SET_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1710 | set_page_writeback(pages[i]); |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1711 | if (page_ops & PAGE_SET_ERROR) |
| 1712 | SetPageError(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1713 | if (page_ops & PAGE_END_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1714 | end_page_writeback(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1715 | if (page_ops & PAGE_UNLOCK) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1716 | unlock_page(pages[i]); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1717 | if (page_ops & PAGE_LOCK) { |
| 1718 | lock_page(pages[i]); |
| 1719 | if (!PageDirty(pages[i]) || |
| 1720 | pages[i]->mapping != mapping) { |
| 1721 | unlock_page(pages[i]); |
| 1722 | put_page(pages[i]); |
| 1723 | err = -EAGAIN; |
| 1724 | goto out; |
| 1725 | } |
| 1726 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1727 | put_page(pages[i]); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1728 | pages_locked++; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1729 | } |
| 1730 | nr_pages -= ret; |
| 1731 | index += ret; |
| 1732 | cond_resched(); |
| 1733 | } |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1734 | out: |
| 1735 | if (err && index_ret) |
| 1736 | *index_ret = start_index + pages_locked - 1; |
| 1737 | return err; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1738 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1739 | |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1740 | void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end, |
| 1741 | u64 delalloc_end, struct page *locked_page, |
| 1742 | unsigned clear_bits, |
| 1743 | unsigned long page_ops) |
| 1744 | { |
| 1745 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0, |
| 1746 | NULL, GFP_NOFS); |
| 1747 | |
| 1748 | __process_pages_contig(inode->i_mapping, locked_page, |
| 1749 | start >> PAGE_SHIFT, end >> PAGE_SHIFT, |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1750 | page_ops, NULL); |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1751 | } |
| 1752 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1753 | /* |
| 1754 | * count the number of bytes in the tree that have a given bit(s) |
| 1755 | * set. This can be fairly slow, except for EXTENT_DIRTY which is |
| 1756 | * cached. The total number found is returned. |
| 1757 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1758 | u64 count_range_bits(struct extent_io_tree *tree, |
| 1759 | u64 *start, u64 search_end, u64 max_bytes, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1760 | unsigned bits, int contig) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1761 | { |
| 1762 | struct rb_node *node; |
| 1763 | struct extent_state *state; |
| 1764 | u64 cur_start = *start; |
| 1765 | u64 total_bytes = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1766 | u64 last = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1767 | int found = 0; |
| 1768 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 1769 | if (WARN_ON(search_end <= cur_start)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1770 | return 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1771 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1772 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1773 | if (cur_start == 0 && bits == EXTENT_DIRTY) { |
| 1774 | total_bytes = tree->dirty_bytes; |
| 1775 | goto out; |
| 1776 | } |
| 1777 | /* |
| 1778 | * this search will find all the extents that end after |
| 1779 | * our range starts. |
| 1780 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1781 | node = tree_search(tree, cur_start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1782 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1783 | goto out; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1784 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1785 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1786 | state = rb_entry(node, struct extent_state, rb_node); |
| 1787 | if (state->start > search_end) |
| 1788 | break; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1789 | if (contig && found && state->start > last + 1) |
| 1790 | break; |
| 1791 | if (state->end >= cur_start && (state->state & bits) == bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1792 | total_bytes += min(search_end, state->end) + 1 - |
| 1793 | max(cur_start, state->start); |
| 1794 | if (total_bytes >= max_bytes) |
| 1795 | break; |
| 1796 | if (!found) { |
Josef Bacik | af60bed | 2011-05-04 11:11:17 -0400 | [diff] [blame] | 1797 | *start = max(cur_start, state->start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1798 | found = 1; |
| 1799 | } |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1800 | last = state->end; |
| 1801 | } else if (contig && found) { |
| 1802 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1803 | } |
| 1804 | node = rb_next(node); |
| 1805 | if (!node) |
| 1806 | break; |
| 1807 | } |
| 1808 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1809 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1810 | return total_bytes; |
| 1811 | } |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1812 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1813 | /* |
| 1814 | * set the private field for a given byte offset in the tree. If there isn't |
| 1815 | * an extent_state there already, this does nothing. |
| 1816 | */ |
Arnd Bergmann | f827ba9 | 2016-02-22 22:53:20 +0100 | [diff] [blame] | 1817 | 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] | 1818 | struct io_failure_record *failrec) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1819 | { |
| 1820 | struct rb_node *node; |
| 1821 | struct extent_state *state; |
| 1822 | int ret = 0; |
| 1823 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1824 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1825 | /* |
| 1826 | * this search will find all the extents that end after |
| 1827 | * our range starts. |
| 1828 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1829 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1830 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1831 | ret = -ENOENT; |
| 1832 | goto out; |
| 1833 | } |
| 1834 | state = rb_entry(node, struct extent_state, rb_node); |
| 1835 | if (state->start != start) { |
| 1836 | ret = -ENOENT; |
| 1837 | goto out; |
| 1838 | } |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 1839 | state->failrec = failrec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1840 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1841 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1842 | return ret; |
| 1843 | } |
| 1844 | |
Arnd Bergmann | f827ba9 | 2016-02-22 22:53:20 +0100 | [diff] [blame] | 1845 | 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] | 1846 | struct io_failure_record **failrec) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1847 | { |
| 1848 | struct rb_node *node; |
| 1849 | struct extent_state *state; |
| 1850 | int ret = 0; |
| 1851 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1852 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1853 | /* |
| 1854 | * this search will find all the extents that end after |
| 1855 | * our range starts. |
| 1856 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1857 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1858 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1859 | ret = -ENOENT; |
| 1860 | goto out; |
| 1861 | } |
| 1862 | state = rb_entry(node, struct extent_state, rb_node); |
| 1863 | if (state->start != start) { |
| 1864 | ret = -ENOENT; |
| 1865 | goto out; |
| 1866 | } |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 1867 | *failrec = state->failrec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1868 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1869 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1870 | return ret; |
| 1871 | } |
| 1872 | |
| 1873 | /* |
| 1874 | * searches a range in the state tree for a given mask. |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1875 | * 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] | 1876 | * has the bits set. Otherwise, 1 is returned if any bit in the |
| 1877 | * range is found set. |
| 1878 | */ |
| 1879 | 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] | 1880 | unsigned bits, int filled, struct extent_state *cached) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1881 | { |
| 1882 | struct extent_state *state = NULL; |
| 1883 | struct rb_node *node; |
| 1884 | int bitset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1885 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1886 | spin_lock(&tree->lock); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1887 | if (cached && extent_state_in_tree(cached) && cached->start <= start && |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 1888 | cached->end > start) |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1889 | node = &cached->rb_node; |
| 1890 | else |
| 1891 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1892 | while (node && start <= end) { |
| 1893 | state = rb_entry(node, struct extent_state, rb_node); |
| 1894 | |
| 1895 | if (filled && state->start > start) { |
| 1896 | bitset = 0; |
| 1897 | break; |
| 1898 | } |
| 1899 | |
| 1900 | if (state->start > end) |
| 1901 | break; |
| 1902 | |
| 1903 | if (state->state & bits) { |
| 1904 | bitset = 1; |
| 1905 | if (!filled) |
| 1906 | break; |
| 1907 | } else if (filled) { |
| 1908 | bitset = 0; |
| 1909 | break; |
| 1910 | } |
Chris Mason | 46562ce | 2009-09-23 20:23:16 -0400 | [diff] [blame] | 1911 | |
| 1912 | if (state->end == (u64)-1) |
| 1913 | break; |
| 1914 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1915 | start = state->end + 1; |
| 1916 | if (start > end) |
| 1917 | break; |
| 1918 | node = rb_next(node); |
| 1919 | if (!node) { |
| 1920 | if (filled) |
| 1921 | bitset = 0; |
| 1922 | break; |
| 1923 | } |
| 1924 | } |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1925 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1926 | return bitset; |
| 1927 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1928 | |
| 1929 | /* |
| 1930 | * helper function to set a given page up to date if all the |
| 1931 | * extents in the tree for that page are up to date |
| 1932 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1933 | 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] | 1934 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 1935 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1936 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1937 | if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1938 | SetPageUptodate(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1939 | } |
| 1940 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 1941 | int free_io_failure(struct extent_io_tree *failure_tree, |
| 1942 | struct extent_io_tree *io_tree, |
| 1943 | struct io_failure_record *rec) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1944 | { |
| 1945 | int ret; |
| 1946 | int err = 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1947 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 1948 | set_state_failrec(failure_tree, rec->start, NULL); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1949 | ret = clear_extent_bits(failure_tree, rec->start, |
| 1950 | rec->start + rec->len - 1, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1951 | EXTENT_LOCKED | EXTENT_DIRTY); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1952 | if (ret) |
| 1953 | err = ret; |
| 1954 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 1955 | ret = clear_extent_bits(io_tree, rec->start, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1956 | rec->start + rec->len - 1, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1957 | EXTENT_DAMAGED); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1958 | if (ret && !err) |
| 1959 | err = ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1960 | |
| 1961 | kfree(rec); |
| 1962 | return err; |
| 1963 | } |
| 1964 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1965 | /* |
| 1966 | * this bypasses the standard btrfs submit functions deliberately, as |
| 1967 | * the standard behavior is to write all copies in a raid setup. here we only |
| 1968 | * want to write the one bad copy. so we do the mapping for ourselves and issue |
| 1969 | * submit_bio directly. |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1970 | * to avoid any synchronization issues, wait for the data after writing, which |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1971 | * actually prevents the read that triggered the error from finishing. |
| 1972 | * currently, there can be no more than two copies of every data bit. thus, |
| 1973 | * exactly one rewrite is required. |
| 1974 | */ |
Josef Bacik | 6ec656b | 2017-05-05 11:57:14 -0400 | [diff] [blame] | 1975 | int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start, |
| 1976 | u64 length, u64 logical, struct page *page, |
| 1977 | unsigned int pg_offset, int mirror_num) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1978 | { |
| 1979 | struct bio *bio; |
| 1980 | struct btrfs_device *dev; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1981 | u64 map_length = 0; |
| 1982 | u64 sector; |
| 1983 | struct btrfs_bio *bbio = NULL; |
| 1984 | int ret; |
| 1985 | |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 1986 | ASSERT(!(fs_info->sb->s_flags & MS_RDONLY)); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1987 | BUG_ON(!mirror_num); |
| 1988 | |
David Sterba | c5e4c3d | 2017-06-12 17:29:41 +0200 | [diff] [blame] | 1989 | bio = btrfs_io_bio_alloc(1); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1990 | bio->bi_iter.bi_size = 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1991 | map_length = length; |
| 1992 | |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 1993 | /* |
| 1994 | * Avoid races with device replace and make sure our bbio has devices |
| 1995 | * associated to its stripes that don't go away while we are doing the |
| 1996 | * read repair operation. |
| 1997 | */ |
| 1998 | btrfs_bio_counter_inc_blocked(fs_info); |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 1999 | if (btrfs_is_parity_mirror(fs_info, logical, length)) { |
Liu Bo | c725328 | 2017-03-29 10:53:58 -0700 | [diff] [blame] | 2000 | /* |
| 2001 | * Note that we don't use BTRFS_MAP_WRITE because it's supposed |
| 2002 | * to update all raid stripes, but here we just want to correct |
| 2003 | * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad |
| 2004 | * stripe's dev and sector. |
| 2005 | */ |
| 2006 | ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical, |
| 2007 | &map_length, &bbio, 0); |
| 2008 | if (ret) { |
| 2009 | btrfs_bio_counter_dec(fs_info); |
| 2010 | bio_put(bio); |
| 2011 | return -EIO; |
| 2012 | } |
| 2013 | ASSERT(bbio->mirror_num == 1); |
| 2014 | } else { |
| 2015 | ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical, |
| 2016 | &map_length, &bbio, mirror_num); |
| 2017 | if (ret) { |
| 2018 | btrfs_bio_counter_dec(fs_info); |
| 2019 | bio_put(bio); |
| 2020 | return -EIO; |
| 2021 | } |
| 2022 | BUG_ON(mirror_num != bbio->mirror_num); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2023 | } |
Liu Bo | c725328 | 2017-03-29 10:53:58 -0700 | [diff] [blame] | 2024 | |
| 2025 | sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2026 | bio->bi_iter.bi_sector = sector; |
Liu Bo | c725328 | 2017-03-29 10:53:58 -0700 | [diff] [blame] | 2027 | dev = bbio->stripes[bbio->mirror_num - 1].dev; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 2028 | btrfs_put_bbio(bbio); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2029 | if (!dev || !dev->bdev || !dev->writeable) { |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2030 | btrfs_bio_counter_dec(fs_info); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2031 | bio_put(bio); |
| 2032 | return -EIO; |
| 2033 | } |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 2034 | bio_set_dev(bio, dev->bdev); |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 2035 | bio->bi_opf = REQ_OP_WRITE | REQ_SYNC; |
Miao Xie | ffdd201 | 2014-09-12 18:44:00 +0800 | [diff] [blame] | 2036 | bio_add_page(bio, page, length, pg_offset); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2037 | |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 2038 | if (btrfsic_submit_bio_wait(bio)) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2039 | /* try to remap that extent elsewhere? */ |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2040 | btrfs_bio_counter_dec(fs_info); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2041 | bio_put(bio); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2042 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2043 | return -EIO; |
| 2044 | } |
| 2045 | |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 2046 | btrfs_info_rl_in_rcu(fs_info, |
| 2047 | "read error corrected: ino %llu off %llu (dev %s sector %llu)", |
Josef Bacik | 6ec656b | 2017-05-05 11:57:14 -0400 | [diff] [blame] | 2048 | ino, start, |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2049 | rcu_str_deref(dev->name), sector); |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2050 | btrfs_bio_counter_dec(fs_info); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2051 | bio_put(bio); |
| 2052 | return 0; |
| 2053 | } |
| 2054 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2055 | int repair_eb_io_failure(struct btrfs_fs_info *fs_info, |
| 2056 | struct extent_buffer *eb, int mirror_num) |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2057 | { |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2058 | u64 start = eb->start; |
| 2059 | unsigned long i, num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | d95603b | 2012-04-12 15:55:15 -0400 | [diff] [blame] | 2060 | int ret = 0; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2061 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 2062 | if (sb_rdonly(fs_info->sb)) |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2063 | return -EROFS; |
| 2064 | |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2065 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 2066 | struct page *p = eb->pages[i]; |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2067 | |
Josef Bacik | 6ec656b | 2017-05-05 11:57:14 -0400 | [diff] [blame] | 2068 | ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p, |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2069 | start - page_offset(p), mirror_num); |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2070 | if (ret) |
| 2071 | break; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2072 | start += PAGE_SIZE; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | return ret; |
| 2076 | } |
| 2077 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2078 | /* |
| 2079 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 2080 | * to see if we need to process or clean up an io_failure_record |
| 2081 | */ |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2082 | int clean_io_failure(struct btrfs_fs_info *fs_info, |
| 2083 | struct extent_io_tree *failure_tree, |
| 2084 | struct extent_io_tree *io_tree, u64 start, |
| 2085 | struct page *page, u64 ino, unsigned int pg_offset) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2086 | { |
| 2087 | u64 private; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2088 | struct io_failure_record *failrec; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2089 | struct extent_state *state; |
| 2090 | int num_copies; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2091 | int ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2092 | |
| 2093 | private = 0; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2094 | ret = count_range_bits(failure_tree, &private, (u64)-1, 1, |
| 2095 | EXTENT_DIRTY, 0); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2096 | if (!ret) |
| 2097 | return 0; |
| 2098 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2099 | ret = get_state_failrec(failure_tree, start, &failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2100 | if (ret) |
| 2101 | return 0; |
| 2102 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2103 | BUG_ON(!failrec->this_mirror); |
| 2104 | |
| 2105 | if (failrec->in_validation) { |
| 2106 | /* there was no real error, just free the record */ |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2107 | btrfs_debug(fs_info, |
| 2108 | "clean_io_failure: freeing dummy error at %llu", |
| 2109 | failrec->start); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2110 | goto out; |
| 2111 | } |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 2112 | if (sb_rdonly(fs_info->sb)) |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2113 | goto out; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2114 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2115 | spin_lock(&io_tree->lock); |
| 2116 | state = find_first_extent_bit_state(io_tree, |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2117 | failrec->start, |
| 2118 | EXTENT_LOCKED); |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2119 | spin_unlock(&io_tree->lock); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2120 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2121 | if (state && state->start <= failrec->start && |
| 2122 | state->end >= failrec->start + failrec->len - 1) { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2123 | num_copies = btrfs_num_copies(fs_info, failrec->logical, |
| 2124 | failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2125 | if (num_copies > 1) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2126 | repair_io_failure(fs_info, ino, start, failrec->len, |
| 2127 | failrec->logical, page, pg_offset, |
| 2128 | failrec->failed_mirror); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2129 | } |
| 2130 | } |
| 2131 | |
| 2132 | out: |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2133 | free_io_failure(failure_tree, io_tree, failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2134 | |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2135 | return 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2136 | } |
| 2137 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2138 | /* |
| 2139 | * Can be called when |
| 2140 | * - hold extent lock |
| 2141 | * - under ordered extent |
| 2142 | * - the inode is freeing |
| 2143 | */ |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 2144 | 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] | 2145 | { |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 2146 | struct extent_io_tree *failure_tree = &inode->io_failure_tree; |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2147 | struct io_failure_record *failrec; |
| 2148 | struct extent_state *state, *next; |
| 2149 | |
| 2150 | if (RB_EMPTY_ROOT(&failure_tree->state)) |
| 2151 | return; |
| 2152 | |
| 2153 | spin_lock(&failure_tree->lock); |
| 2154 | state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY); |
| 2155 | while (state) { |
| 2156 | if (state->start > end) |
| 2157 | break; |
| 2158 | |
| 2159 | ASSERT(state->end <= end); |
| 2160 | |
| 2161 | next = next_state(state); |
| 2162 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2163 | failrec = state->failrec; |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2164 | free_extent_state(state); |
| 2165 | kfree(failrec); |
| 2166 | |
| 2167 | state = next; |
| 2168 | } |
| 2169 | spin_unlock(&failure_tree->lock); |
| 2170 | } |
| 2171 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2172 | 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] | 2173 | struct io_failure_record **failrec_ret) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2174 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2175 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2176 | struct io_failure_record *failrec; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2177 | struct extent_map *em; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2178 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 2179 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 2180 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2181 | int ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2182 | u64 logical; |
| 2183 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2184 | ret = get_state_failrec(failure_tree, start, &failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2185 | if (ret) { |
| 2186 | failrec = kzalloc(sizeof(*failrec), GFP_NOFS); |
| 2187 | if (!failrec) |
| 2188 | return -ENOMEM; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2189 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2190 | failrec->start = start; |
| 2191 | failrec->len = end - start + 1; |
| 2192 | failrec->this_mirror = 0; |
| 2193 | failrec->bio_flags = 0; |
| 2194 | failrec->in_validation = 0; |
| 2195 | |
| 2196 | read_lock(&em_tree->lock); |
| 2197 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 2198 | if (!em) { |
| 2199 | read_unlock(&em_tree->lock); |
| 2200 | kfree(failrec); |
| 2201 | return -EIO; |
| 2202 | } |
| 2203 | |
Filipe David Borba Manana | 68ba990 | 2013-11-25 03:22:07 +0000 | [diff] [blame] | 2204 | if (em->start > start || em->start + em->len <= start) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2205 | free_extent_map(em); |
| 2206 | em = NULL; |
| 2207 | } |
| 2208 | read_unlock(&em_tree->lock); |
Tsutomu Itoh | 7a2d6a6 | 2012-10-01 03:07:15 -0600 | [diff] [blame] | 2209 | if (!em) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2210 | kfree(failrec); |
| 2211 | return -EIO; |
| 2212 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2213 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2214 | logical = start - em->start; |
| 2215 | logical = em->block_start + logical; |
| 2216 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 2217 | logical = em->block_start; |
| 2218 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
| 2219 | extent_set_compress_type(&failrec->bio_flags, |
| 2220 | em->compress_type); |
| 2221 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2222 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2223 | btrfs_debug(fs_info, |
| 2224 | "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu", |
| 2225 | logical, start, failrec->len); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2226 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2227 | failrec->logical = logical; |
| 2228 | free_extent_map(em); |
| 2229 | |
| 2230 | /* set the bits in the private failure tree */ |
| 2231 | ret = set_extent_bits(failure_tree, start, end, |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2232 | EXTENT_LOCKED | EXTENT_DIRTY); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2233 | if (ret >= 0) |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2234 | ret = set_state_failrec(failure_tree, start, failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2235 | /* set the bits in the inode's tree */ |
| 2236 | if (ret >= 0) |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2237 | ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2238 | if (ret < 0) { |
| 2239 | kfree(failrec); |
| 2240 | return ret; |
| 2241 | } |
| 2242 | } else { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2243 | btrfs_debug(fs_info, |
| 2244 | "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d", |
| 2245 | failrec->logical, failrec->start, failrec->len, |
| 2246 | failrec->in_validation); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2247 | /* |
| 2248 | * when data can be on disk more than twice, add to failrec here |
| 2249 | * (e.g. with a list for failed_mirror) to make |
| 2250 | * clean_io_failure() clean all those errors at once. |
| 2251 | */ |
| 2252 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2253 | |
| 2254 | *failrec_ret = failrec; |
| 2255 | |
| 2256 | return 0; |
| 2257 | } |
| 2258 | |
Liu Bo | c3cfb65 | 2017-07-13 15:00:50 -0700 | [diff] [blame] | 2259 | bool btrfs_check_repairable(struct inode *inode, struct bio *failed_bio, |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2260 | struct io_failure_record *failrec, int failed_mirror) |
| 2261 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2262 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2263 | int num_copies; |
| 2264 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2265 | num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2266 | if (num_copies == 1) { |
| 2267 | /* |
| 2268 | * we only have a single copy of the data, so don't bother with |
| 2269 | * all the retry and error correction code that follows. no |
| 2270 | * matter what the error is, it is very likely to persist. |
| 2271 | */ |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2272 | btrfs_debug(fs_info, |
| 2273 | "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", |
| 2274 | num_copies, failrec->this_mirror, failed_mirror); |
Liu Bo | c3cfb65 | 2017-07-13 15:00:50 -0700 | [diff] [blame] | 2275 | return false; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2276 | } |
| 2277 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2278 | /* |
| 2279 | * there are two premises: |
| 2280 | * a) deliver good data to the caller |
| 2281 | * b) correct the bad sectors on disk |
| 2282 | */ |
| 2283 | if (failed_bio->bi_vcnt > 1) { |
| 2284 | /* |
| 2285 | * to fulfill b), we need to know the exact failing sectors, as |
| 2286 | * we don't want to rewrite any more than the failed ones. thus, |
| 2287 | * we need separate read requests for the failed bio |
| 2288 | * |
| 2289 | * if the following BUG_ON triggers, our validation request got |
| 2290 | * merged. we need separate requests for our algorithm to work. |
| 2291 | */ |
| 2292 | BUG_ON(failrec->in_validation); |
| 2293 | failrec->in_validation = 1; |
| 2294 | failrec->this_mirror = failed_mirror; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2295 | } else { |
| 2296 | /* |
| 2297 | * we're ready to fulfill a) and b) alongside. get a good copy |
| 2298 | * of the failed sector and if we succeed, we have setup |
| 2299 | * everything for repair_io_failure to do the rest for us. |
| 2300 | */ |
| 2301 | if (failrec->in_validation) { |
| 2302 | BUG_ON(failrec->this_mirror != failed_mirror); |
| 2303 | failrec->in_validation = 0; |
| 2304 | failrec->this_mirror = 0; |
| 2305 | } |
| 2306 | failrec->failed_mirror = failed_mirror; |
| 2307 | failrec->this_mirror++; |
| 2308 | if (failrec->this_mirror == failed_mirror) |
| 2309 | failrec->this_mirror++; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2310 | } |
| 2311 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2312 | if (failrec->this_mirror > num_copies) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2313 | btrfs_debug(fs_info, |
| 2314 | "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", |
| 2315 | num_copies, failrec->this_mirror, failed_mirror); |
Liu Bo | c3cfb65 | 2017-07-13 15:00:50 -0700 | [diff] [blame] | 2316 | return false; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2317 | } |
| 2318 | |
Liu Bo | c3cfb65 | 2017-07-13 15:00:50 -0700 | [diff] [blame] | 2319 | return true; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2320 | } |
| 2321 | |
| 2322 | |
| 2323 | struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio, |
| 2324 | struct io_failure_record *failrec, |
| 2325 | struct page *page, int pg_offset, int icsum, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2326 | bio_end_io_t *endio_func, void *data) |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2327 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2328 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2329 | struct bio *bio; |
| 2330 | struct btrfs_io_bio *btrfs_failed_bio; |
| 2331 | struct btrfs_io_bio *btrfs_bio; |
| 2332 | |
David Sterba | c5e4c3d | 2017-06-12 17:29:41 +0200 | [diff] [blame] | 2333 | bio = btrfs_io_bio_alloc(1); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2334 | bio->bi_end_io = endio_func; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2335 | bio->bi_iter.bi_sector = failrec->logical >> 9; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 2336 | bio_set_dev(bio, fs_info->fs_devices->latest_bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2337 | bio->bi_iter.bi_size = 0; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2338 | bio->bi_private = data; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2339 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2340 | btrfs_failed_bio = btrfs_io_bio(failed_bio); |
| 2341 | if (btrfs_failed_bio->csum) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2342 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
| 2343 | |
| 2344 | btrfs_bio = btrfs_io_bio(bio); |
| 2345 | btrfs_bio->csum = btrfs_bio->csum_inline; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2346 | icsum *= csum_size; |
| 2347 | memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum, |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2348 | csum_size); |
| 2349 | } |
| 2350 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2351 | bio_add_page(bio, page, failrec->len, pg_offset); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2352 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2353 | return bio; |
| 2354 | } |
| 2355 | |
| 2356 | /* |
| 2357 | * this is a generic handler for readpage errors (default |
| 2358 | * readpage_io_failed_hook). if other copies exist, read those and write back |
| 2359 | * good data to the failed position. does not investigate in remapping the |
| 2360 | * failed extent elsewhere, hoping the device will be smart enough to do this as |
| 2361 | * needed |
| 2362 | */ |
| 2363 | |
| 2364 | static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset, |
| 2365 | struct page *page, u64 start, u64 end, |
| 2366 | int failed_mirror) |
| 2367 | { |
| 2368 | struct io_failure_record *failrec; |
| 2369 | struct inode *inode = page->mapping->host; |
| 2370 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2371 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2372 | struct bio *bio; |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 2373 | int read_mode = 0; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2374 | blk_status_t status; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2375 | int ret; |
| 2376 | |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2377 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2378 | |
| 2379 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 2380 | if (ret) |
| 2381 | return ret; |
| 2382 | |
Liu Bo | c3cfb65 | 2017-07-13 15:00:50 -0700 | [diff] [blame] | 2383 | if (!btrfs_check_repairable(inode, failed_bio, failrec, |
| 2384 | failed_mirror)) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2385 | free_io_failure(failure_tree, tree, failrec); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2386 | return -EIO; |
| 2387 | } |
| 2388 | |
| 2389 | if (failed_bio->bi_vcnt > 1) |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 2390 | read_mode |= REQ_FAILFAST_DEV; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2391 | |
| 2392 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
| 2393 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
| 2394 | start - page_offset(page), |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2395 | (int)phy_offset, failed_bio->bi_end_io, |
| 2396 | NULL); |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2397 | bio_set_op_attrs(bio, REQ_OP_READ, read_mode); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2398 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2399 | btrfs_debug(btrfs_sb(inode->i_sb), |
| 2400 | "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d", |
| 2401 | read_mode, failrec->this_mirror, failrec->in_validation); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2402 | |
Linus Torvalds | 8c27cb3 | 2017-07-05 16:41:23 -0700 | [diff] [blame] | 2403 | 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] | 2404 | failrec->bio_flags, 0); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2405 | if (status) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2406 | free_io_failure(failure_tree, tree, failrec); |
Miao Xie | 6c387ab | 2014-09-12 18:43:57 +0800 | [diff] [blame] | 2407 | bio_put(bio); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2408 | ret = blk_status_to_errno(status); |
Miao Xie | 6c387ab | 2014-09-12 18:43:57 +0800 | [diff] [blame] | 2409 | } |
| 2410 | |
Tsutomu Itoh | 013bd4c | 2012-02-16 10:11:40 +0900 | [diff] [blame] | 2411 | return ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2412 | } |
| 2413 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2414 | /* lots and lots of room for performance fixes in the end_bio funcs */ |
| 2415 | |
David Sterba | b5227c0 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 2416 | 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] | 2417 | { |
| 2418 | int uptodate = (err == 0); |
| 2419 | struct extent_io_tree *tree; |
Eric Sandeen | 3e2426b | 2014-06-12 00:39:58 -0500 | [diff] [blame] | 2420 | int ret = 0; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2421 | |
| 2422 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 2423 | |
David Sterba | c3988d6 | 2017-02-17 15:18:32 +0100 | [diff] [blame] | 2424 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 2425 | tree->ops->writepage_end_io_hook(page, start, end, NULL, |
| 2426 | uptodate); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2427 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2428 | if (!uptodate) { |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2429 | ClearPageUptodate(page); |
| 2430 | SetPageError(page); |
Colin Ian King | bff5baf | 2017-05-09 18:14:01 +0100 | [diff] [blame] | 2431 | ret = err < 0 ? err : -EIO; |
Liu Bo | 5dca6ee | 2014-05-12 12:47:36 +0800 | [diff] [blame] | 2432 | mapping_set_error(page->mapping, ret); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2433 | } |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2434 | } |
| 2435 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2436 | /* |
| 2437 | * after a writepage IO is done, we need to: |
| 2438 | * clear the uptodate bits on error |
| 2439 | * clear the writeback bits in the extent tree for this IO |
| 2440 | * end_page_writeback if the page has no more pending IO |
| 2441 | * |
| 2442 | * Scheduling is not allowed, so the extent state tree is expected |
| 2443 | * to have one and only one object corresponding to this IO. |
| 2444 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2445 | static void end_bio_extent_writepage(struct bio *bio) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2446 | { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2447 | int error = blk_status_to_errno(bio->bi_status); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2448 | struct bio_vec *bvec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2449 | u64 start; |
| 2450 | u64 end; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2451 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2452 | |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 2453 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2454 | bio_for_each_segment_all(bvec, bio, i) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2455 | struct page *page = bvec->bv_page; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2456 | struct inode *inode = page->mapping->host; |
| 2457 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2458 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2459 | /* We always issue full-page reads, but if some block |
| 2460 | * in a page fails to read, blk_update_request() will |
| 2461 | * advance bv_offset and adjust bv_len to compensate. |
| 2462 | * Print a warning for nonzero offsets, and an error |
| 2463 | * if they don't add up to a full page. */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2464 | if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) { |
| 2465 | if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2466 | btrfs_err(fs_info, |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2467 | "partial page write in btrfs with offset %u and length %u", |
| 2468 | bvec->bv_offset, bvec->bv_len); |
| 2469 | else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2470 | btrfs_info(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2471 | "incomplete page write in btrfs with offset %u and length %u", |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2472 | bvec->bv_offset, bvec->bv_len); |
| 2473 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2474 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2475 | start = page_offset(page); |
| 2476 | end = start + bvec->bv_offset + bvec->bv_len - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2477 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2478 | end_extent_writepage(page, error, start, end); |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2479 | end_page_writeback(page); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2480 | } |
Chris Mason | 2b1f55b | 2008-09-24 11:48:04 -0400 | [diff] [blame] | 2481 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2482 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2483 | } |
| 2484 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2485 | static void |
| 2486 | endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len, |
| 2487 | int uptodate) |
| 2488 | { |
| 2489 | struct extent_state *cached = NULL; |
| 2490 | u64 end = start + len - 1; |
| 2491 | |
| 2492 | if (uptodate && tree->track_uptodate) |
| 2493 | set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC); |
| 2494 | unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC); |
| 2495 | } |
| 2496 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2497 | /* |
| 2498 | * after a readpage IO is done, we need to: |
| 2499 | * clear the uptodate bits on error |
| 2500 | * set the uptodate bits if things worked |
| 2501 | * set the page up to date if all extents in the tree are uptodate |
| 2502 | * clear the lock bit in the extent tree |
| 2503 | * unlock the page if there are no other extents locked for it |
| 2504 | * |
| 2505 | * Scheduling is not allowed, so the extent state tree is expected |
| 2506 | * to have one and only one object corresponding to this IO. |
| 2507 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2508 | static void end_bio_extent_readpage(struct bio *bio) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2509 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2510 | struct bio_vec *bvec; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2511 | int uptodate = !bio->bi_status; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2512 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2513 | struct extent_io_tree *tree, *failure_tree; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2514 | u64 offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2515 | u64 start; |
| 2516 | u64 end; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2517 | u64 len; |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2518 | u64 extent_start = 0; |
| 2519 | u64 extent_len = 0; |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2520 | int mirror; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2521 | int ret; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2522 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2523 | |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 2524 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2525 | bio_for_each_segment_all(bvec, bio, i) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2526 | struct page *page = bvec->bv_page; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2527 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2528 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2529 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2530 | btrfs_debug(fs_info, |
| 2531 | "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u", |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2532 | (u64)bio->bi_iter.bi_sector, bio->bi_status, |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2533 | io_bio->mirror_num); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2534 | tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2535 | failure_tree = &BTRFS_I(inode)->io_failure_tree; |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2536 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2537 | /* We always issue full-page reads, but if some block |
| 2538 | * in a page fails to read, blk_update_request() will |
| 2539 | * advance bv_offset and adjust bv_len to compensate. |
| 2540 | * Print a warning for nonzero offsets, and an error |
| 2541 | * if they don't add up to a full page. */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2542 | if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) { |
| 2543 | if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2544 | btrfs_err(fs_info, |
| 2545 | "partial page read in btrfs with offset %u and length %u", |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2546 | bvec->bv_offset, bvec->bv_len); |
| 2547 | else |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2548 | btrfs_info(fs_info, |
| 2549 | "incomplete page read in btrfs with offset %u and length %u", |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2550 | bvec->bv_offset, bvec->bv_len); |
| 2551 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2552 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2553 | start = page_offset(page); |
| 2554 | end = start + bvec->bv_offset + bvec->bv_len - 1; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2555 | len = bvec->bv_len; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2556 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2557 | mirror = io_bio->mirror_num; |
David Sterba | 20c9801 | 2017-02-17 15:59:35 +0100 | [diff] [blame] | 2558 | if (likely(uptodate && tree->ops)) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2559 | ret = tree->ops->readpage_end_io_hook(io_bio, offset, |
| 2560 | page, start, end, |
| 2561 | mirror); |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2562 | if (ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2563 | uptodate = 0; |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2564 | else |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2565 | clean_io_failure(BTRFS_I(inode)->root->fs_info, |
| 2566 | failure_tree, tree, start, |
| 2567 | page, |
| 2568 | btrfs_ino(BTRFS_I(inode)), 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2569 | } |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2570 | |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2571 | if (likely(uptodate)) |
| 2572 | goto readpage_ok; |
| 2573 | |
David Sterba | 20a7db8 | 2017-02-17 16:24:29 +0100 | [diff] [blame] | 2574 | if (tree->ops) { |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2575 | ret = tree->ops->readpage_io_failed_hook(page, mirror); |
Liu Bo | 9d0d1c8 | 2017-03-24 15:04:50 -0700 | [diff] [blame] | 2576 | if (ret == -EAGAIN) { |
| 2577 | /* |
| 2578 | * Data inode's readpage_io_failed_hook() always |
| 2579 | * returns -EAGAIN. |
| 2580 | * |
| 2581 | * The generic bio_readpage_error handles errors |
| 2582 | * the following way: If possible, new read |
| 2583 | * requests are created and submitted and will |
| 2584 | * end up in end_bio_extent_readpage as well (if |
| 2585 | * we're lucky, not in the !uptodate case). In |
| 2586 | * that case it returns 0 and we just go on with |
| 2587 | * the next page in our bio. If it can't handle |
| 2588 | * the error it will return -EIO and we remain |
| 2589 | * responsible for that page. |
| 2590 | */ |
| 2591 | ret = bio_readpage_error(bio, offset, page, |
| 2592 | start, end, mirror); |
| 2593 | if (ret == 0) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2594 | uptodate = !bio->bi_status; |
Liu Bo | 9d0d1c8 | 2017-03-24 15:04:50 -0700 | [diff] [blame] | 2595 | offset += len; |
| 2596 | continue; |
| 2597 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2598 | } |
Liu Bo | 9d0d1c8 | 2017-03-24 15:04:50 -0700 | [diff] [blame] | 2599 | |
| 2600 | /* |
| 2601 | * metadata's readpage_io_failed_hook() always returns |
| 2602 | * -EIO and fixes nothing. -EIO is also returned if |
| 2603 | * data inode error could not be fixed. |
| 2604 | */ |
| 2605 | ASSERT(ret == -EIO); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2606 | } |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2607 | readpage_ok: |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2608 | if (likely(uptodate)) { |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2609 | loff_t i_size = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2610 | pgoff_t end_index = i_size >> PAGE_SHIFT; |
Liu Bo | a583c02 | 2014-08-19 23:32:22 +0800 | [diff] [blame] | 2611 | unsigned off; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2612 | |
| 2613 | /* Zero out the end if this page straddles i_size */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2614 | off = i_size & (PAGE_SIZE-1); |
Liu Bo | a583c02 | 2014-08-19 23:32:22 +0800 | [diff] [blame] | 2615 | if (page->index == end_index && off) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2616 | zero_user_segment(page, off, PAGE_SIZE); |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2617 | SetPageUptodate(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2618 | } else { |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2619 | ClearPageUptodate(page); |
| 2620 | SetPageError(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2621 | } |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2622 | unlock_page(page); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2623 | offset += len; |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2624 | |
| 2625 | if (unlikely(!uptodate)) { |
| 2626 | if (extent_len) { |
| 2627 | endio_readpage_release_extent(tree, |
| 2628 | extent_start, |
| 2629 | extent_len, 1); |
| 2630 | extent_start = 0; |
| 2631 | extent_len = 0; |
| 2632 | } |
| 2633 | endio_readpage_release_extent(tree, start, |
| 2634 | end - start + 1, 0); |
| 2635 | } else if (!extent_len) { |
| 2636 | extent_start = start; |
| 2637 | extent_len = end + 1 - start; |
| 2638 | } else if (extent_start + extent_len == start) { |
| 2639 | extent_len += end + 1 - start; |
| 2640 | } else { |
| 2641 | endio_readpage_release_extent(tree, extent_start, |
| 2642 | extent_len, uptodate); |
| 2643 | extent_start = start; |
| 2644 | extent_len = end + 1 - start; |
| 2645 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2646 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2647 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2648 | if (extent_len) |
| 2649 | endio_readpage_release_extent(tree, extent_start, extent_len, |
| 2650 | uptodate); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2651 | if (io_bio->end_io) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2652 | 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] | 2653 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2654 | } |
| 2655 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2656 | /* |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2657 | * Initialize the members up to but not including 'bio'. Use after allocating a |
| 2658 | * new bio by bio_alloc_bioset as it does not initialize the bytes outside of |
| 2659 | * 'bio' because use of __GFP_ZERO is not supported. |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2660 | */ |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2661 | 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] | 2662 | { |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2663 | memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio)); |
| 2664 | } |
| 2665 | |
| 2666 | /* |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2667 | * The following helpers allocate a bio. As it's backed by a bioset, it'll |
| 2668 | * never fail. We're returning a bio right now but you can call btrfs_io_bio |
| 2669 | * for the appropriate container_of magic |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2670 | */ |
David Sterba | c821e7f3 | 2017-06-02 18:35:36 +0200 | [diff] [blame] | 2671 | struct bio *btrfs_bio_alloc(struct block_device *bdev, u64 first_byte) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2672 | { |
| 2673 | struct bio *bio; |
| 2674 | |
David Sterba | 9f2179a | 2017-06-02 17:55:44 +0200 | [diff] [blame] | 2675 | bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, btrfs_bioset); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 2676 | bio_set_dev(bio, bdev); |
David Sterba | c821e7f3 | 2017-06-02 18:35:36 +0200 | [diff] [blame] | 2677 | bio->bi_iter.bi_sector = first_byte >> 9; |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2678 | btrfs_io_bio_init(btrfs_io_bio(bio)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2679 | return bio; |
| 2680 | } |
| 2681 | |
David Sterba | 8b6c1d5 | 2017-06-02 17:48:13 +0200 | [diff] [blame] | 2682 | struct bio *btrfs_bio_clone(struct bio *bio) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2683 | { |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 2684 | struct btrfs_io_bio *btrfs_bio; |
| 2685 | struct bio *new; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2686 | |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2687 | /* Bio allocation backed by a bioset does not fail */ |
David Sterba | 8b6c1d5 | 2017-06-02 17:48:13 +0200 | [diff] [blame] | 2688 | new = bio_clone_fast(bio, GFP_NOFS, btrfs_bioset); |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2689 | btrfs_bio = btrfs_io_bio(new); |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2690 | btrfs_io_bio_init(btrfs_bio); |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2691 | btrfs_bio->iter = bio->bi_iter; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 2692 | return new; |
| 2693 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2694 | |
David Sterba | c5e4c3d | 2017-06-12 17:29:41 +0200 | [diff] [blame] | 2695 | struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2696 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2697 | struct bio *bio; |
| 2698 | |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2699 | /* Bio allocation backed by a bioset does not fail */ |
David Sterba | c5e4c3d | 2017-06-12 17:29:41 +0200 | [diff] [blame] | 2700 | bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, btrfs_bioset); |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2701 | btrfs_io_bio_init(btrfs_io_bio(bio)); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2702 | return bio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2703 | } |
| 2704 | |
Liu Bo | e477094 | 2017-05-16 10:57:14 -0700 | [diff] [blame] | 2705 | 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] | 2706 | { |
| 2707 | struct bio *bio; |
| 2708 | struct btrfs_io_bio *btrfs_bio; |
| 2709 | |
| 2710 | /* this will never fail when it's backed by a bioset */ |
Liu Bo | e477094 | 2017-05-16 10:57:14 -0700 | [diff] [blame] | 2711 | bio = bio_clone_fast(orig, GFP_NOFS, btrfs_bioset); |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 2712 | ASSERT(bio); |
| 2713 | |
| 2714 | btrfs_bio = btrfs_io_bio(bio); |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2715 | btrfs_io_bio_init(btrfs_bio); |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 2716 | |
| 2717 | bio_trim(bio, offset >> 9, size >> 9); |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 2718 | btrfs_bio->iter = bio->bi_iter; |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 2719 | return bio; |
| 2720 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2721 | |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2722 | static int __must_check submit_one_bio(struct bio *bio, int mirror_num, |
| 2723 | unsigned long bio_flags) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2724 | { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2725 | blk_status_t ret = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2726 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 2727 | struct page *page = bvec->bv_page; |
| 2728 | struct extent_io_tree *tree = bio->bi_private; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2729 | u64 start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2730 | |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2731 | start = page_offset(page) + bvec->bv_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2732 | |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2733 | bio->bi_private = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2734 | bio_get(bio); |
| 2735 | |
David Sterba | 20c9801 | 2017-02-17 15:59:35 +0100 | [diff] [blame] | 2736 | if (tree->ops) |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 2737 | ret = tree->ops->submit_bio_hook(tree->private_data, bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 2738 | mirror_num, bio_flags, start); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2739 | else |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 2740 | btrfsic_submit_bio(bio); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2741 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2742 | bio_put(bio); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2743 | return blk_status_to_errno(ret); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2744 | } |
| 2745 | |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2746 | static int merge_bio(struct extent_io_tree *tree, struct page *page, |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 2747 | unsigned long offset, size_t size, struct bio *bio, |
| 2748 | unsigned long bio_flags) |
| 2749 | { |
| 2750 | int ret = 0; |
David Sterba | 20c9801 | 2017-02-17 15:59:35 +0100 | [diff] [blame] | 2751 | if (tree->ops) |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 2752 | ret = tree->ops->merge_bio_hook(page, offset, size, bio, |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 2753 | bio_flags); |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 2754 | return ret; |
| 2755 | |
| 2756 | } |
| 2757 | |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 2758 | /* |
| 2759 | * @opf: bio REQ_OP_* and REQ_* flags as one value |
| 2760 | */ |
| 2761 | 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] | 2762 | struct writeback_control *wbc, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2763 | struct page *page, sector_t sector, |
| 2764 | size_t size, unsigned long offset, |
| 2765 | struct block_device *bdev, |
| 2766 | struct bio **bio_ret, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2767 | bio_end_io_t end_io_func, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2768 | int mirror_num, |
| 2769 | unsigned long prev_bio_flags, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2770 | unsigned long bio_flags, |
| 2771 | bool force_bio_submit) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2772 | { |
| 2773 | int ret = 0; |
| 2774 | struct bio *bio; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2775 | int contig = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2776 | int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2777 | size_t page_size = min_t(size_t, size, PAGE_SIZE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2778 | |
| 2779 | if (bio_ret && *bio_ret) { |
| 2780 | bio = *bio_ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2781 | if (old_compressed) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2782 | contig = bio->bi_iter.bi_sector == sector; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2783 | else |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 2784 | contig = bio_end_sector(bio) == sector; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2785 | |
| 2786 | if (prev_bio_flags != bio_flags || !contig || |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2787 | force_bio_submit || |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2788 | merge_bio(tree, page, offset, page_size, bio, bio_flags) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2789 | bio_add_page(bio, page, page_size, offset) < page_size) { |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2790 | ret = submit_one_bio(bio, mirror_num, prev_bio_flags); |
Naohiro Aota | 289454a | 2015-01-06 01:01:03 +0900 | [diff] [blame] | 2791 | if (ret < 0) { |
| 2792 | *bio_ret = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2793 | return ret; |
Naohiro Aota | 289454a | 2015-01-06 01:01:03 +0900 | [diff] [blame] | 2794 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2795 | bio = NULL; |
| 2796 | } else { |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2797 | if (wbc) |
| 2798 | wbc_account_io(wbc, page, page_size); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2799 | return 0; |
| 2800 | } |
| 2801 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2802 | |
Goffredo Baroncelli | 2d8ce70 | 2017-10-03 19:31:10 +0200 | [diff] [blame] | 2803 | bio = btrfs_bio_alloc(bdev, (u64)sector << 9); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2804 | bio_add_page(bio, page, page_size, offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2805 | bio->bi_end_io = end_io_func; |
| 2806 | bio->bi_private = tree; |
Jens Axboe | e6959b9 | 2017-06-27 11:51:28 -0600 | [diff] [blame] | 2807 | bio->bi_write_hint = page->mapping->host->i_write_hint; |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 2808 | bio->bi_opf = opf; |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2809 | if (wbc) { |
| 2810 | wbc_init_bio(wbc, bio); |
| 2811 | wbc_account_io(wbc, page, page_size); |
| 2812 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2813 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2814 | if (bio_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2815 | *bio_ret = bio; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2816 | else |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2817 | ret = submit_one_bio(bio, mirror_num, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2818 | |
| 2819 | return ret; |
| 2820 | } |
| 2821 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 2822 | static void attach_extent_buffer_page(struct extent_buffer *eb, |
| 2823 | struct page *page) |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 2824 | { |
| 2825 | if (!PagePrivate(page)) { |
| 2826 | SetPagePrivate(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2827 | get_page(page); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 2828 | set_page_private(page, (unsigned long)eb); |
| 2829 | } else { |
| 2830 | WARN_ON(page->private != (unsigned long)eb); |
| 2831 | } |
| 2832 | } |
| 2833 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2834 | void set_page_extent_mapped(struct page *page) |
| 2835 | { |
| 2836 | if (!PagePrivate(page)) { |
| 2837 | SetPagePrivate(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2838 | get_page(page); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2839 | set_page_private(page, EXTENT_PAGE_PRIVATE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2840 | } |
| 2841 | } |
| 2842 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2843 | static struct extent_map * |
| 2844 | __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset, |
| 2845 | u64 start, u64 len, get_extent_t *get_extent, |
| 2846 | struct extent_map **em_cached) |
| 2847 | { |
| 2848 | struct extent_map *em; |
| 2849 | |
| 2850 | if (em_cached && *em_cached) { |
| 2851 | em = *em_cached; |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 2852 | if (extent_map_in_tree(em) && start >= em->start && |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2853 | start < extent_map_end(em)) { |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 2854 | refcount_inc(&em->refs); |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2855 | return em; |
| 2856 | } |
| 2857 | |
| 2858 | free_extent_map(em); |
| 2859 | *em_cached = NULL; |
| 2860 | } |
| 2861 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 2862 | em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0); |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2863 | if (em_cached && !IS_ERR_OR_NULL(em)) { |
| 2864 | BUG_ON(*em_cached); |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 2865 | refcount_inc(&em->refs); |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2866 | *em_cached = em; |
| 2867 | } |
| 2868 | return em; |
| 2869 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2870 | /* |
| 2871 | * basic readpage implementation. Locked extent state structs are inserted |
| 2872 | * into the tree that are removed when the IO is done (by the end_io |
| 2873 | * handlers) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2874 | * XXX JDM: This needs looking at to ensure proper page locking |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 2875 | * return 0 on success, otherwise return error |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2876 | */ |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2877 | static int __do_readpage(struct extent_io_tree *tree, |
| 2878 | struct page *page, |
| 2879 | get_extent_t *get_extent, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2880 | struct extent_map **em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2881 | struct bio **bio, int mirror_num, |
David Sterba | f1c77c5 | 2017-06-06 19:03:49 +0200 | [diff] [blame] | 2882 | unsigned long *bio_flags, unsigned int read_flags, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2883 | u64 *prev_em_start) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2884 | { |
| 2885 | struct inode *inode = page->mapping->host; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2886 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2887 | u64 page_end = start + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2888 | u64 end; |
| 2889 | u64 cur = start; |
| 2890 | u64 extent_offset; |
| 2891 | u64 last_byte = i_size_read(inode); |
| 2892 | u64 block_start; |
| 2893 | u64 cur_end; |
| 2894 | sector_t sector; |
| 2895 | struct extent_map *em; |
| 2896 | struct block_device *bdev; |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 2897 | int ret = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2898 | int nr = 0; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2899 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2900 | size_t iosize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2901 | size_t disk_io_size; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2902 | size_t blocksize = inode->i_sb->s_blocksize; |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 2903 | unsigned long this_bio_flag = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2904 | |
| 2905 | set_page_extent_mapped(page); |
| 2906 | |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2907 | end = page_end; |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 2908 | if (!PageUptodate(page)) { |
| 2909 | if (cleancache_get_page(page) == 0) { |
| 2910 | BUG_ON(blocksize != PAGE_SIZE); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2911 | unlock_extent(tree, start, end); |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 2912 | goto out; |
| 2913 | } |
| 2914 | } |
| 2915 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2916 | if (page->index == last_byte >> PAGE_SHIFT) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2917 | char *userpage; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2918 | size_t zero_offset = last_byte & (PAGE_SIZE - 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2919 | |
| 2920 | if (zero_offset) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2921 | iosize = PAGE_SIZE - zero_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2922 | userpage = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2923 | memset(userpage + zero_offset, 0, iosize); |
| 2924 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2925 | kunmap_atomic(userpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2926 | } |
| 2927 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2928 | while (cur <= end) { |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2929 | bool force_bio_submit = false; |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 2930 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2931 | if (cur >= last_byte) { |
| 2932 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2933 | struct extent_state *cached = NULL; |
| 2934 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2935 | iosize = PAGE_SIZE - pg_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2936 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2937 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2938 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2939 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2940 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2941 | &cached, GFP_NOFS); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 2942 | unlock_extent_cached(tree, cur, |
| 2943 | cur + iosize - 1, |
| 2944 | &cached, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2945 | break; |
| 2946 | } |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2947 | em = __get_extent_map(inode, page, pg_offset, cur, |
| 2948 | end - cur + 1, get_extent, em_cached); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 2949 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2950 | SetPageError(page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 2951 | unlock_extent(tree, cur, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2952 | break; |
| 2953 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2954 | extent_offset = cur - em->start; |
| 2955 | BUG_ON(extent_map_end(em) <= cur); |
| 2956 | BUG_ON(end < cur); |
| 2957 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2958 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 2959 | this_bio_flag |= EXTENT_BIO_COMPRESSED; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2960 | extent_set_compress_type(&this_bio_flag, |
| 2961 | em->compress_type); |
| 2962 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2963 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2964 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
| 2965 | cur_end = min(extent_map_end(em) - 1, end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 2966 | iosize = ALIGN(iosize, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2967 | if (this_bio_flag & EXTENT_BIO_COMPRESSED) { |
| 2968 | disk_io_size = em->block_len; |
| 2969 | sector = em->block_start >> 9; |
| 2970 | } else { |
| 2971 | sector = (em->block_start + extent_offset) >> 9; |
| 2972 | disk_io_size = iosize; |
| 2973 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2974 | bdev = em->bdev; |
| 2975 | block_start = em->block_start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2976 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 2977 | block_start = EXTENT_MAP_HOLE; |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2978 | |
| 2979 | /* |
| 2980 | * If we have a file range that points to a compressed extent |
| 2981 | * and it's followed by a consecutive file range that points to |
| 2982 | * to the same compressed extent (possibly with a different |
| 2983 | * offset and/or length, so it either points to the whole extent |
| 2984 | * or only part of it), we must make sure we do not submit a |
| 2985 | * single bio to populate the pages for the 2 ranges because |
| 2986 | * this makes the compressed extent read zero out the pages |
| 2987 | * belonging to the 2nd range. Imagine the following scenario: |
| 2988 | * |
| 2989 | * File layout |
| 2990 | * [0 - 8K] [8K - 24K] |
| 2991 | * | | |
| 2992 | * | | |
| 2993 | * points to extent X, points to extent X, |
| 2994 | * offset 4K, length of 8K offset 0, length 16K |
| 2995 | * |
| 2996 | * [extent X, compressed length = 4K uncompressed length = 16K] |
| 2997 | * |
| 2998 | * If the bio to read the compressed extent covers both ranges, |
| 2999 | * it will decompress extent X into the pages belonging to the |
| 3000 | * first range and then it will stop, zeroing out the remaining |
| 3001 | * pages that belong to the other range that points to extent X. |
| 3002 | * So here we make sure we submit 2 bios, one for the first |
| 3003 | * range and another one for the third range. Both will target |
| 3004 | * the same physical extent from disk, but we can't currently |
| 3005 | * make the compressed bio endio callback populate the pages |
| 3006 | * for both ranges because each compressed bio is tightly |
| 3007 | * coupled with a single extent map, and each range can have |
| 3008 | * an extent map with a different offset value relative to the |
| 3009 | * uncompressed data of our extent and different lengths. This |
| 3010 | * is a corner case so we prioritize correctness over |
| 3011 | * non-optimal behavior (submitting 2 bios for the same extent). |
| 3012 | */ |
| 3013 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) && |
| 3014 | prev_em_start && *prev_em_start != (u64)-1 && |
| 3015 | *prev_em_start != em->orig_start) |
| 3016 | force_bio_submit = true; |
| 3017 | |
| 3018 | if (prev_em_start) |
| 3019 | *prev_em_start = em->orig_start; |
| 3020 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3021 | free_extent_map(em); |
| 3022 | em = NULL; |
| 3023 | |
| 3024 | /* we've found a hole, just zero and go on */ |
| 3025 | if (block_start == EXTENT_MAP_HOLE) { |
| 3026 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3027 | struct extent_state *cached = NULL; |
| 3028 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3029 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3030 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3031 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3032 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3033 | |
| 3034 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3035 | &cached, GFP_NOFS); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3036 | unlock_extent_cached(tree, cur, |
| 3037 | cur + iosize - 1, |
| 3038 | &cached, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3039 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3040 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3041 | continue; |
| 3042 | } |
| 3043 | /* the get_extent function already copied into the page */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3044 | if (test_range_bit(tree, cur, cur_end, |
| 3045 | EXTENT_UPTODATE, 1, NULL)) { |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 3046 | check_page_uptodate(tree, page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3047 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3048 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3049 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3050 | continue; |
| 3051 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3052 | /* we have an inline extent but it didn't get marked up |
| 3053 | * to date. Error out |
| 3054 | */ |
| 3055 | if (block_start == EXTENT_MAP_INLINE) { |
| 3056 | SetPageError(page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3057 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3058 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3059 | pg_offset += iosize; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3060 | continue; |
| 3061 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3062 | |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 3063 | ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3064 | page, sector, disk_io_size, pg_offset, |
David Sterba | c2df8bb | 2017-02-10 19:29:38 +0100 | [diff] [blame] | 3065 | bdev, bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3066 | end_bio_extent_readpage, mirror_num, |
| 3067 | *bio_flags, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3068 | this_bio_flag, |
| 3069 | force_bio_submit); |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3070 | if (!ret) { |
| 3071 | nr++; |
| 3072 | *bio_flags = this_bio_flag; |
| 3073 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3074 | SetPageError(page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3075 | unlock_extent(tree, cur, cur + iosize - 1); |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 3076 | goto out; |
Josef Bacik | edd33c9 | 2012-10-05 16:40:32 -0400 | [diff] [blame] | 3077 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3078 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3079 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3080 | } |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 3081 | out: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3082 | if (!nr) { |
| 3083 | if (!PageError(page)) |
| 3084 | SetPageUptodate(page); |
| 3085 | unlock_page(page); |
| 3086 | } |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 3087 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3088 | } |
| 3089 | |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3090 | static inline void __do_contiguous_readpages(struct extent_io_tree *tree, |
| 3091 | struct page *pages[], int nr_pages, |
| 3092 | u64 start, u64 end, |
| 3093 | get_extent_t *get_extent, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3094 | struct extent_map **em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3095 | struct bio **bio, int mirror_num, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3096 | unsigned long *bio_flags, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3097 | u64 *prev_em_start) |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3098 | { |
| 3099 | struct inode *inode; |
| 3100 | struct btrfs_ordered_extent *ordered; |
| 3101 | int index; |
| 3102 | |
| 3103 | inode = pages[0]->mapping->host; |
| 3104 | while (1) { |
| 3105 | lock_extent(tree, start, end); |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 3106 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3107 | end - start + 1); |
| 3108 | if (!ordered) |
| 3109 | break; |
| 3110 | unlock_extent(tree, start, end); |
| 3111 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3112 | btrfs_put_ordered_extent(ordered); |
| 3113 | } |
| 3114 | |
| 3115 | for (index = 0; index < nr_pages; index++) { |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3116 | __do_readpage(tree, pages[index], get_extent, em_cached, bio, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3117 | mirror_num, bio_flags, 0, prev_em_start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3118 | put_page(pages[index]); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3119 | } |
| 3120 | } |
| 3121 | |
| 3122 | static void __extent_readpages(struct extent_io_tree *tree, |
| 3123 | struct page *pages[], |
| 3124 | int nr_pages, get_extent_t *get_extent, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3125 | struct extent_map **em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3126 | struct bio **bio, int mirror_num, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3127 | unsigned long *bio_flags, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3128 | u64 *prev_em_start) |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3129 | { |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 3130 | u64 start = 0; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3131 | u64 end = 0; |
| 3132 | u64 page_start; |
| 3133 | int index; |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 3134 | int first_index = 0; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3135 | |
| 3136 | for (index = 0; index < nr_pages; index++) { |
| 3137 | page_start = page_offset(pages[index]); |
| 3138 | if (!end) { |
| 3139 | start = page_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3140 | end = start + PAGE_SIZE - 1; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3141 | first_index = index; |
| 3142 | } else if (end + 1 == page_start) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3143 | end += PAGE_SIZE; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3144 | } else { |
| 3145 | __do_contiguous_readpages(tree, &pages[first_index], |
| 3146 | index - first_index, start, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3147 | end, get_extent, em_cached, |
| 3148 | bio, mirror_num, bio_flags, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3149 | prev_em_start); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3150 | start = page_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3151 | end = start + PAGE_SIZE - 1; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3152 | first_index = index; |
| 3153 | } |
| 3154 | } |
| 3155 | |
| 3156 | if (end) |
| 3157 | __do_contiguous_readpages(tree, &pages[first_index], |
| 3158 | index - first_index, start, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3159 | end, get_extent, em_cached, bio, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3160 | mirror_num, bio_flags, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3161 | prev_em_start); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3162 | } |
| 3163 | |
| 3164 | static int __extent_read_full_page(struct extent_io_tree *tree, |
| 3165 | struct page *page, |
| 3166 | get_extent_t *get_extent, |
| 3167 | struct bio **bio, int mirror_num, |
David Sterba | f1c77c5 | 2017-06-06 19:03:49 +0200 | [diff] [blame] | 3168 | unsigned long *bio_flags, |
| 3169 | unsigned int read_flags) |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3170 | { |
| 3171 | struct inode *inode = page->mapping->host; |
| 3172 | struct btrfs_ordered_extent *ordered; |
| 3173 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3174 | u64 end = start + PAGE_SIZE - 1; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3175 | int ret; |
| 3176 | |
| 3177 | while (1) { |
| 3178 | lock_extent(tree, start, end); |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 3179 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3180 | PAGE_SIZE); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3181 | if (!ordered) |
| 3182 | break; |
| 3183 | unlock_extent(tree, start, end); |
| 3184 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3185 | btrfs_put_ordered_extent(ordered); |
| 3186 | } |
| 3187 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3188 | ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3189 | bio_flags, read_flags, NULL); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3190 | return ret; |
| 3191 | } |
| 3192 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3193 | 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] | 3194 | get_extent_t *get_extent, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3195 | { |
| 3196 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3197 | unsigned long bio_flags = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3198 | int ret; |
| 3199 | |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 3200 | ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3201 | &bio_flags, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3202 | if (bio) |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3203 | ret = submit_one_bio(bio, mirror_num, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3204 | return ret; |
| 3205 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3206 | |
David Sterba | 3d4b949 | 2017-02-10 19:33:41 +0100 | [diff] [blame] | 3207 | static void update_nr_written(struct writeback_control *wbc, |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3208 | unsigned long nr_written) |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3209 | { |
| 3210 | wbc->nr_to_write -= nr_written; |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3211 | } |
| 3212 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3213 | /* |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3214 | * helper for __extent_writepage, doing all of the delayed allocation setup. |
| 3215 | * |
| 3216 | * This returns 1 if our fill_delalloc function did all the work required |
| 3217 | * to write the page (copy into inline extent). In this case the IO has |
| 3218 | * been started and the page is already unlocked. |
| 3219 | * |
| 3220 | * This returns 0 if all went well (page still locked) |
| 3221 | * This returns < 0 if there were errors (page still locked) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3222 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3223 | static noinline_for_stack int writepage_delalloc(struct inode *inode, |
| 3224 | struct page *page, struct writeback_control *wbc, |
| 3225 | struct extent_page_data *epd, |
| 3226 | u64 delalloc_start, |
| 3227 | unsigned long *nr_written) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3228 | { |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3229 | struct extent_io_tree *tree = epd->tree; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3230 | u64 page_end = delalloc_start + PAGE_SIZE - 1; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3231 | u64 nr_delalloc; |
| 3232 | u64 delalloc_to_write = 0; |
| 3233 | u64 delalloc_end = 0; |
| 3234 | int ret; |
| 3235 | int page_started = 0; |
| 3236 | |
| 3237 | if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc) |
| 3238 | return 0; |
| 3239 | |
| 3240 | while (delalloc_end < page_end) { |
| 3241 | nr_delalloc = find_lock_delalloc_range(inode, tree, |
| 3242 | page, |
| 3243 | &delalloc_start, |
| 3244 | &delalloc_end, |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 3245 | BTRFS_MAX_EXTENT_SIZE); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3246 | if (nr_delalloc == 0) { |
| 3247 | delalloc_start = delalloc_end + 1; |
| 3248 | continue; |
| 3249 | } |
| 3250 | ret = tree->ops->fill_delalloc(inode, page, |
| 3251 | delalloc_start, |
| 3252 | delalloc_end, |
| 3253 | &page_started, |
| 3254 | nr_written); |
| 3255 | /* File system has been set read-only */ |
| 3256 | if (ret) { |
| 3257 | SetPageError(page); |
| 3258 | /* fill_delalloc should be return < 0 for error |
| 3259 | * but just in case, we use > 0 here meaning the |
| 3260 | * IO is started, so we don't want to return > 0 |
| 3261 | * unless things are going well. |
| 3262 | */ |
| 3263 | ret = ret < 0 ? ret : -EIO; |
| 3264 | goto done; |
| 3265 | } |
| 3266 | /* |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 3267 | * delalloc_end is already one less than the total length, so |
| 3268 | * we don't subtract one from PAGE_SIZE |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3269 | */ |
| 3270 | delalloc_to_write += (delalloc_end - delalloc_start + |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 3271 | PAGE_SIZE) >> PAGE_SHIFT; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3272 | delalloc_start = delalloc_end + 1; |
| 3273 | } |
| 3274 | if (wbc->nr_to_write < delalloc_to_write) { |
| 3275 | int thresh = 8192; |
| 3276 | |
| 3277 | if (delalloc_to_write < thresh * 2) |
| 3278 | thresh = delalloc_to_write; |
| 3279 | wbc->nr_to_write = min_t(u64, delalloc_to_write, |
| 3280 | thresh); |
| 3281 | } |
| 3282 | |
| 3283 | /* did the fill delalloc function already unlock and start |
| 3284 | * the IO? |
| 3285 | */ |
| 3286 | if (page_started) { |
| 3287 | /* |
| 3288 | * we've unlocked the page, so we can't update |
| 3289 | * the mapping's writeback index, just update |
| 3290 | * nr_to_write. |
| 3291 | */ |
| 3292 | wbc->nr_to_write -= *nr_written; |
| 3293 | return 1; |
| 3294 | } |
| 3295 | |
| 3296 | ret = 0; |
| 3297 | |
| 3298 | done: |
| 3299 | return ret; |
| 3300 | } |
| 3301 | |
| 3302 | /* |
| 3303 | * helper for __extent_writepage. This calls the writepage start hooks, |
| 3304 | * and does the loop to map the page into extents and bios. |
| 3305 | * |
| 3306 | * We return 1 if the IO is started and the page is unlocked, |
| 3307 | * 0 if all went well (page still locked) |
| 3308 | * < 0 if there were errors (page still locked) |
| 3309 | */ |
| 3310 | static noinline_for_stack int __extent_writepage_io(struct inode *inode, |
| 3311 | struct page *page, |
| 3312 | struct writeback_control *wbc, |
| 3313 | struct extent_page_data *epd, |
| 3314 | loff_t i_size, |
| 3315 | unsigned long nr_written, |
David Sterba | f1c77c5 | 2017-06-06 19:03:49 +0200 | [diff] [blame] | 3316 | unsigned int write_flags, int *nr_ret) |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3317 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3318 | struct extent_io_tree *tree = epd->tree; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3319 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3320 | u64 page_end = start + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3321 | u64 end; |
| 3322 | u64 cur = start; |
| 3323 | u64 extent_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3324 | u64 block_start; |
| 3325 | u64 iosize; |
| 3326 | sector_t sector; |
| 3327 | struct extent_map *em; |
| 3328 | struct block_device *bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3329 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3330 | size_t blocksize; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3331 | int ret = 0; |
| 3332 | int nr = 0; |
| 3333 | bool compressed; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3334 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3335 | if (tree->ops && tree->ops->writepage_start_hook) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3336 | ret = tree->ops->writepage_start_hook(page, start, |
| 3337 | page_end); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 3338 | if (ret) { |
| 3339 | /* Fixup worker will requeue */ |
| 3340 | if (ret == -EBUSY) |
| 3341 | wbc->pages_skipped++; |
| 3342 | else |
| 3343 | redirty_page_for_writepage(wbc, page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3344 | |
David Sterba | 3d4b949 | 2017-02-10 19:33:41 +0100 | [diff] [blame] | 3345 | update_nr_written(wbc, nr_written); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3346 | unlock_page(page); |
Liu Bo | bcf9348 | 2017-01-25 17:15:54 -0800 | [diff] [blame] | 3347 | return 1; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3348 | } |
| 3349 | } |
| 3350 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3351 | /* |
| 3352 | * we don't want to touch the inode after unlocking the page, |
| 3353 | * so we update the mapping writeback index now |
| 3354 | */ |
David Sterba | 3d4b949 | 2017-02-10 19:33:41 +0100 | [diff] [blame] | 3355 | update_nr_written(wbc, nr_written + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3356 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3357 | end = page_end; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3358 | if (i_size <= start) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3359 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 3360 | tree->ops->writepage_end_io_hook(page, start, |
| 3361 | page_end, NULL, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3362 | goto done; |
| 3363 | } |
| 3364 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3365 | blocksize = inode->i_sb->s_blocksize; |
| 3366 | |
| 3367 | while (cur <= end) { |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3368 | u64 em_end; |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3369 | |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3370 | if (cur >= i_size) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3371 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 3372 | tree->ops->writepage_end_io_hook(page, cur, |
| 3373 | page_end, NULL, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3374 | break; |
| 3375 | } |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 3376 | em = epd->get_extent(BTRFS_I(inode), page, pg_offset, cur, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3377 | end - cur + 1, 1); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3378 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3379 | SetPageError(page); |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 3380 | ret = PTR_ERR_OR_ZERO(em); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3381 | break; |
| 3382 | } |
| 3383 | |
| 3384 | extent_offset = cur - em->start; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3385 | em_end = extent_map_end(em); |
| 3386 | BUG_ON(em_end <= cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3387 | BUG_ON(end < cur); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3388 | iosize = min(em_end - cur, end - cur + 1); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3389 | iosize = ALIGN(iosize, blocksize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3390 | sector = (em->block_start + extent_offset) >> 9; |
| 3391 | bdev = em->bdev; |
| 3392 | block_start = em->block_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3393 | compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3394 | free_extent_map(em); |
| 3395 | em = NULL; |
| 3396 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3397 | /* |
| 3398 | * compressed and inline extents are written through other |
| 3399 | * paths in the FS |
| 3400 | */ |
| 3401 | if (compressed || block_start == EXTENT_MAP_HOLE || |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3402 | block_start == EXTENT_MAP_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3403 | /* |
| 3404 | * end_io notification does not happen here for |
| 3405 | * compressed extents |
| 3406 | */ |
| 3407 | if (!compressed && tree->ops && |
| 3408 | tree->ops->writepage_end_io_hook) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3409 | tree->ops->writepage_end_io_hook(page, cur, |
| 3410 | cur + iosize - 1, |
| 3411 | NULL, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3412 | else if (compressed) { |
| 3413 | /* we don't want to end_page_writeback on |
| 3414 | * a compressed extent. this happens |
| 3415 | * elsewhere |
| 3416 | */ |
| 3417 | nr++; |
| 3418 | } |
| 3419 | |
| 3420 | cur += iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3421 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3422 | continue; |
| 3423 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3424 | |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3425 | set_range_writeback(tree, cur, cur + iosize - 1); |
| 3426 | if (!PageWriteback(page)) { |
| 3427 | btrfs_err(BTRFS_I(inode)->root->fs_info, |
| 3428 | "page %lu not writeback, cur %llu end %llu", |
| 3429 | page->index, cur, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3430 | } |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3431 | |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 3432 | ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3433 | page, sector, iosize, pg_offset, |
David Sterba | c2df8bb | 2017-02-10 19:29:38 +0100 | [diff] [blame] | 3434 | bdev, &epd->bio, |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3435 | end_bio_extent_writepage, |
| 3436 | 0, 0, 0, false); |
Takafumi Kubota | fe01aa6 | 2017-02-09 17:24:33 +0900 | [diff] [blame] | 3437 | if (ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3438 | SetPageError(page); |
Takafumi Kubota | fe01aa6 | 2017-02-09 17:24:33 +0900 | [diff] [blame] | 3439 | if (PageWriteback(page)) |
| 3440 | end_page_writeback(page); |
| 3441 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3442 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3443 | cur = cur + iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3444 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3445 | nr++; |
| 3446 | } |
| 3447 | done: |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3448 | *nr_ret = nr; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3449 | return ret; |
| 3450 | } |
| 3451 | |
| 3452 | /* |
| 3453 | * the writepage semantics are similar to regular writepage. extent |
| 3454 | * records are inserted to lock ranges in the tree, and as dirty areas |
| 3455 | * are found, they are marked writeback. Then the lock bits are removed |
| 3456 | * and the end_io handler clears the writeback ranges |
| 3457 | */ |
| 3458 | static int __extent_writepage(struct page *page, struct writeback_control *wbc, |
| 3459 | void *data) |
| 3460 | { |
| 3461 | struct inode *inode = page->mapping->host; |
| 3462 | struct extent_page_data *epd = data; |
| 3463 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3464 | u64 page_end = start + PAGE_SIZE - 1; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3465 | int ret; |
| 3466 | int nr = 0; |
| 3467 | size_t pg_offset = 0; |
| 3468 | loff_t i_size = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3469 | unsigned long end_index = i_size >> PAGE_SHIFT; |
David Sterba | f1c77c5 | 2017-06-06 19:03:49 +0200 | [diff] [blame] | 3470 | unsigned int write_flags = 0; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3471 | unsigned long nr_written = 0; |
| 3472 | |
Liu Bo | ff40adf | 2017-08-24 18:19:48 -0600 | [diff] [blame] | 3473 | write_flags = wbc_to_write_flags(wbc); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3474 | |
| 3475 | trace___extent_writepage(page, inode, wbc); |
| 3476 | |
| 3477 | WARN_ON(!PageLocked(page)); |
| 3478 | |
| 3479 | ClearPageError(page); |
| 3480 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3481 | pg_offset = i_size & (PAGE_SIZE - 1); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3482 | if (page->index > end_index || |
| 3483 | (page->index == end_index && !pg_offset)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3484 | page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3485 | unlock_page(page); |
| 3486 | return 0; |
| 3487 | } |
| 3488 | |
| 3489 | if (page->index == end_index) { |
| 3490 | char *userpage; |
| 3491 | |
| 3492 | userpage = kmap_atomic(page); |
| 3493 | memset(userpage + pg_offset, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3494 | PAGE_SIZE - pg_offset); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3495 | kunmap_atomic(userpage); |
| 3496 | flush_dcache_page(page); |
| 3497 | } |
| 3498 | |
| 3499 | pg_offset = 0; |
| 3500 | |
| 3501 | set_page_extent_mapped(page); |
| 3502 | |
| 3503 | ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written); |
| 3504 | if (ret == 1) |
| 3505 | goto done_unlocked; |
| 3506 | if (ret) |
| 3507 | goto done; |
| 3508 | |
| 3509 | ret = __extent_writepage_io(inode, page, wbc, epd, |
| 3510 | i_size, nr_written, write_flags, &nr); |
| 3511 | if (ret == 1) |
| 3512 | goto done_unlocked; |
| 3513 | |
| 3514 | done: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3515 | if (nr == 0) { |
| 3516 | /* make sure the mapping tag for page dirty gets cleared */ |
| 3517 | set_page_writeback(page); |
| 3518 | end_page_writeback(page); |
| 3519 | } |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 3520 | if (PageError(page)) { |
| 3521 | ret = ret < 0 ? ret : -EIO; |
| 3522 | end_extent_writepage(page, ret, start, page_end); |
| 3523 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3524 | unlock_page(page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3525 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3526 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3527 | done_unlocked: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3528 | return 0; |
| 3529 | } |
| 3530 | |
Josef Bacik | fd8b2b6 | 2013-04-24 16:41:19 -0400 | [diff] [blame] | 3531 | void wait_on_extent_buffer_writeback(struct extent_buffer *eb) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3532 | { |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 3533 | wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK, |
| 3534 | TASK_UNINTERRUPTIBLE); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3535 | } |
| 3536 | |
Chris Mason | 0e378df | 2014-05-19 20:55:27 -0700 | [diff] [blame] | 3537 | static noinline_for_stack int |
| 3538 | lock_extent_buffer_for_io(struct extent_buffer *eb, |
| 3539 | struct btrfs_fs_info *fs_info, |
| 3540 | struct extent_page_data *epd) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3541 | { |
| 3542 | unsigned long i, num_pages; |
| 3543 | int flush = 0; |
| 3544 | int ret = 0; |
| 3545 | |
| 3546 | if (!btrfs_try_tree_write_lock(eb)) { |
| 3547 | flush = 1; |
| 3548 | flush_write_bio(epd); |
| 3549 | btrfs_tree_lock(eb); |
| 3550 | } |
| 3551 | |
| 3552 | if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) { |
| 3553 | btrfs_tree_unlock(eb); |
| 3554 | if (!epd->sync_io) |
| 3555 | return 0; |
| 3556 | if (!flush) { |
| 3557 | flush_write_bio(epd); |
| 3558 | flush = 1; |
| 3559 | } |
Chris Mason | a098d8e8 | 2012-03-21 12:09:56 -0400 | [diff] [blame] | 3560 | while (1) { |
| 3561 | wait_on_extent_buffer_writeback(eb); |
| 3562 | btrfs_tree_lock(eb); |
| 3563 | if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) |
| 3564 | break; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3565 | btrfs_tree_unlock(eb); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3566 | } |
| 3567 | } |
| 3568 | |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3569 | /* |
| 3570 | * We need to do this to prevent races in people who check if the eb is |
| 3571 | * under IO since we can end up having no IO bits set for a short period |
| 3572 | * of time. |
| 3573 | */ |
| 3574 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3575 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) { |
| 3576 | set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3577 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3578 | btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 3579 | percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, |
| 3580 | -eb->len, |
| 3581 | fs_info->dirty_metadata_batch); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3582 | ret = 1; |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3583 | } else { |
| 3584 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3585 | } |
| 3586 | |
| 3587 | btrfs_tree_unlock(eb); |
| 3588 | |
| 3589 | if (!ret) |
| 3590 | return ret; |
| 3591 | |
| 3592 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3593 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 3594 | struct page *p = eb->pages[i]; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3595 | |
| 3596 | if (!trylock_page(p)) { |
| 3597 | if (!flush) { |
| 3598 | flush_write_bio(epd); |
| 3599 | flush = 1; |
| 3600 | } |
| 3601 | lock_page(p); |
| 3602 | } |
| 3603 | } |
| 3604 | |
| 3605 | return ret; |
| 3606 | } |
| 3607 | |
| 3608 | static void end_extent_buffer_writeback(struct extent_buffer *eb) |
| 3609 | { |
| 3610 | clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 3611 | smp_mb__after_atomic(); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3612 | wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK); |
| 3613 | } |
| 3614 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3615 | static void set_btree_ioerr(struct page *page) |
| 3616 | { |
| 3617 | struct extent_buffer *eb = (struct extent_buffer *)page->private; |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3618 | |
| 3619 | SetPageError(page); |
| 3620 | if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) |
| 3621 | return; |
| 3622 | |
| 3623 | /* |
| 3624 | * If writeback for a btree extent that doesn't belong to a log tree |
| 3625 | * failed, increment the counter transaction->eb_write_errors. |
| 3626 | * We do this because while the transaction is running and before it's |
| 3627 | * committing (when we call filemap_fdata[write|wait]_range against |
| 3628 | * the btree inode), we might have |
| 3629 | * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it |
| 3630 | * returns an error or an error happens during writeback, when we're |
| 3631 | * committing the transaction we wouldn't know about it, since the pages |
| 3632 | * can be no longer dirty nor marked anymore for writeback (if a |
| 3633 | * subsequent modification to the extent buffer didn't happen before the |
| 3634 | * transaction commit), which makes filemap_fdata[write|wait]_range not |
| 3635 | * able to find the pages tagged with SetPageError at transaction |
| 3636 | * commit time. So if this happens we must abort the transaction, |
| 3637 | * otherwise we commit a super block with btree roots that point to |
| 3638 | * btree nodes/leafs whose content on disk is invalid - either garbage |
| 3639 | * or the content of some node/leaf from a past generation that got |
| 3640 | * cowed or deleted and is no longer valid. |
| 3641 | * |
| 3642 | * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would |
| 3643 | * not be enough - we need to distinguish between log tree extents vs |
| 3644 | * non-log tree extents, and the next filemap_fdatawait_range() call |
| 3645 | * will catch and clear such errors in the mapping - and that call might |
| 3646 | * be from a log sync and not from a transaction commit. Also, checking |
| 3647 | * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is |
| 3648 | * not done and would not be reliable - the eb might have been released |
| 3649 | * from memory and reading it back again means that flag would not be |
| 3650 | * set (since it's a runtime flag, not persisted on disk). |
| 3651 | * |
| 3652 | * Using the flags below in the btree inode also makes us achieve the |
| 3653 | * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started |
| 3654 | * writeback for all dirty pages and before filemap_fdatawait_range() |
| 3655 | * is called, the writeback for all dirty pages had already finished |
| 3656 | * with errors - because we were not using AS_EIO/AS_ENOSPC, |
| 3657 | * filemap_fdatawait_range() would return success, as it could not know |
| 3658 | * that writeback errors happened (the pages were no longer tagged for |
| 3659 | * writeback). |
| 3660 | */ |
| 3661 | switch (eb->log_index) { |
| 3662 | case -1: |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 3663 | set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3664 | break; |
| 3665 | case 0: |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 3666 | set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3667 | break; |
| 3668 | case 1: |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 3669 | set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3670 | break; |
| 3671 | default: |
| 3672 | BUG(); /* unexpected, logic error */ |
| 3673 | } |
| 3674 | } |
| 3675 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3676 | static void end_bio_extent_buffer_writepage(struct bio *bio) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3677 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3678 | struct bio_vec *bvec; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3679 | struct extent_buffer *eb; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3680 | int i, done; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3681 | |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 3682 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3683 | bio_for_each_segment_all(bvec, bio, i) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3684 | struct page *page = bvec->bv_page; |
| 3685 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3686 | eb = (struct extent_buffer *)page->private; |
| 3687 | BUG_ON(!eb); |
| 3688 | done = atomic_dec_and_test(&eb->io_pages); |
| 3689 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 3690 | if (bio->bi_status || |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3691 | test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3692 | ClearPageUptodate(page); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3693 | set_btree_ioerr(page); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3694 | } |
| 3695 | |
| 3696 | end_page_writeback(page); |
| 3697 | |
| 3698 | if (!done) |
| 3699 | continue; |
| 3700 | |
| 3701 | end_extent_buffer_writeback(eb); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3702 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3703 | |
| 3704 | bio_put(bio); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3705 | } |
| 3706 | |
Chris Mason | 0e378df | 2014-05-19 20:55:27 -0700 | [diff] [blame] | 3707 | static noinline_for_stack int write_one_eb(struct extent_buffer *eb, |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3708 | struct btrfs_fs_info *fs_info, |
| 3709 | struct writeback_control *wbc, |
| 3710 | struct extent_page_data *epd) |
| 3711 | { |
| 3712 | struct block_device *bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 3713 | 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] | 3714 | u64 offset = eb->start; |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3715 | u32 nritems; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3716 | unsigned long i, num_pages; |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3717 | unsigned long start, end; |
Liu Bo | ff40adf | 2017-08-24 18:19:48 -0600 | [diff] [blame] | 3718 | unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META; |
Josef Bacik | d7dbe9e | 2012-04-23 14:00:51 -0400 | [diff] [blame] | 3719 | int ret = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3720 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3721 | clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3722 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3723 | atomic_set(&eb->io_pages, num_pages); |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3724 | |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3725 | /* set btree blocks beyond nritems with 0 to avoid stale content. */ |
| 3726 | nritems = btrfs_header_nritems(eb); |
Liu Bo | 3eb548e | 2016-09-14 17:22:57 -0700 | [diff] [blame] | 3727 | if (btrfs_header_level(eb) > 0) { |
Liu Bo | 3eb548e | 2016-09-14 17:22:57 -0700 | [diff] [blame] | 3728 | end = btrfs_node_key_ptr_offset(nritems); |
| 3729 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3730 | memzero_extent_buffer(eb, end, eb->len - end); |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3731 | } else { |
| 3732 | /* |
| 3733 | * leaf: |
| 3734 | * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0 |
| 3735 | */ |
| 3736 | start = btrfs_item_nr_offset(nritems); |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3737 | end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, eb); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3738 | memzero_extent_buffer(eb, start, end - start); |
Liu Bo | 3eb548e | 2016-09-14 17:22:57 -0700 | [diff] [blame] | 3739 | } |
| 3740 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3741 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 3742 | struct page *p = eb->pages[i]; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3743 | |
| 3744 | clear_page_dirty_for_io(p); |
| 3745 | set_page_writeback(p); |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 3746 | ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3747 | p, offset >> 9, PAGE_SIZE, 0, bdev, |
David Sterba | c2df8bb | 2017-02-10 19:29:38 +0100 | [diff] [blame] | 3748 | &epd->bio, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3749 | end_bio_extent_buffer_writepage, |
Liu Bo | 18fdc67 | 2017-09-13 12:18:22 -0600 | [diff] [blame^] | 3750 | 0, 0, 0, false); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3751 | if (ret) { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3752 | set_btree_ioerr(p); |
Takafumi Kubota | fe01aa6 | 2017-02-09 17:24:33 +0900 | [diff] [blame] | 3753 | if (PageWriteback(p)) |
| 3754 | end_page_writeback(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3755 | if (atomic_sub_and_test(num_pages - i, &eb->io_pages)) |
| 3756 | end_extent_buffer_writeback(eb); |
| 3757 | ret = -EIO; |
| 3758 | break; |
| 3759 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3760 | offset += PAGE_SIZE; |
David Sterba | 3d4b949 | 2017-02-10 19:33:41 +0100 | [diff] [blame] | 3761 | update_nr_written(wbc, 1); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3762 | unlock_page(p); |
| 3763 | } |
| 3764 | |
| 3765 | if (unlikely(ret)) { |
| 3766 | for (; i < num_pages; i++) { |
Chris Mason | bbf65cf | 2014-10-04 09:56:45 -0700 | [diff] [blame] | 3767 | struct page *p = eb->pages[i]; |
Liu Bo | 8146502 | 2014-09-23 22:22:33 +0800 | [diff] [blame] | 3768 | clear_page_dirty_for_io(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3769 | unlock_page(p); |
| 3770 | } |
| 3771 | } |
| 3772 | |
| 3773 | return ret; |
| 3774 | } |
| 3775 | |
| 3776 | int btree_write_cache_pages(struct address_space *mapping, |
| 3777 | struct writeback_control *wbc) |
| 3778 | { |
| 3779 | struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree; |
| 3780 | struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info; |
| 3781 | struct extent_buffer *eb, *prev_eb = NULL; |
| 3782 | struct extent_page_data epd = { |
| 3783 | .bio = NULL, |
| 3784 | .tree = tree, |
| 3785 | .extent_locked = 0, |
| 3786 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
| 3787 | }; |
| 3788 | int ret = 0; |
| 3789 | int done = 0; |
| 3790 | int nr_to_write_done = 0; |
| 3791 | struct pagevec pvec; |
| 3792 | int nr_pages; |
| 3793 | pgoff_t index; |
| 3794 | pgoff_t end; /* Inclusive */ |
| 3795 | int scanned = 0; |
| 3796 | int tag; |
| 3797 | |
| 3798 | pagevec_init(&pvec, 0); |
| 3799 | if (wbc->range_cyclic) { |
| 3800 | index = mapping->writeback_index; /* Start from prev offset */ |
| 3801 | end = -1; |
| 3802 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3803 | index = wbc->range_start >> PAGE_SHIFT; |
| 3804 | end = wbc->range_end >> PAGE_SHIFT; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3805 | scanned = 1; |
| 3806 | } |
| 3807 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3808 | tag = PAGECACHE_TAG_TOWRITE; |
| 3809 | else |
| 3810 | tag = PAGECACHE_TAG_DIRTY; |
| 3811 | retry: |
| 3812 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3813 | tag_pages_for_writeback(mapping, index, end); |
| 3814 | while (!done && !nr_to_write_done && (index <= end) && |
| 3815 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
| 3816 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { |
| 3817 | unsigned i; |
| 3818 | |
| 3819 | scanned = 1; |
| 3820 | for (i = 0; i < nr_pages; i++) { |
| 3821 | struct page *page = pvec.pages[i]; |
| 3822 | |
| 3823 | if (!PagePrivate(page)) |
| 3824 | continue; |
| 3825 | |
| 3826 | if (!wbc->range_cyclic && page->index > end) { |
| 3827 | done = 1; |
| 3828 | break; |
| 3829 | } |
| 3830 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3831 | spin_lock(&mapping->private_lock); |
| 3832 | if (!PagePrivate(page)) { |
| 3833 | spin_unlock(&mapping->private_lock); |
| 3834 | continue; |
| 3835 | } |
| 3836 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3837 | eb = (struct extent_buffer *)page->private; |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3838 | |
| 3839 | /* |
| 3840 | * Shouldn't happen and normally this would be a BUG_ON |
| 3841 | * but no sense in crashing the users box for something |
| 3842 | * we can survive anyway. |
| 3843 | */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3844 | if (WARN_ON(!eb)) { |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3845 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3846 | continue; |
| 3847 | } |
| 3848 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3849 | if (eb == prev_eb) { |
| 3850 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3851 | continue; |
| 3852 | } |
| 3853 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3854 | ret = atomic_inc_not_zero(&eb->refs); |
| 3855 | spin_unlock(&mapping->private_lock); |
| 3856 | if (!ret) |
| 3857 | continue; |
| 3858 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3859 | prev_eb = eb; |
| 3860 | ret = lock_extent_buffer_for_io(eb, fs_info, &epd); |
| 3861 | if (!ret) { |
| 3862 | free_extent_buffer(eb); |
| 3863 | continue; |
| 3864 | } |
| 3865 | |
| 3866 | ret = write_one_eb(eb, fs_info, wbc, &epd); |
| 3867 | if (ret) { |
| 3868 | done = 1; |
| 3869 | free_extent_buffer(eb); |
| 3870 | break; |
| 3871 | } |
| 3872 | free_extent_buffer(eb); |
| 3873 | |
| 3874 | /* |
| 3875 | * the filesystem may choose to bump up nr_to_write. |
| 3876 | * We have to make sure to honor the new nr_to_write |
| 3877 | * at any time |
| 3878 | */ |
| 3879 | nr_to_write_done = wbc->nr_to_write <= 0; |
| 3880 | } |
| 3881 | pagevec_release(&pvec); |
| 3882 | cond_resched(); |
| 3883 | } |
| 3884 | if (!scanned && !done) { |
| 3885 | /* |
| 3886 | * We hit the last page and there is more work to be done: wrap |
| 3887 | * back to the start of the file |
| 3888 | */ |
| 3889 | scanned = 1; |
| 3890 | index = 0; |
| 3891 | goto retry; |
| 3892 | } |
| 3893 | flush_write_bio(&epd); |
| 3894 | return ret; |
| 3895 | } |
| 3896 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3897 | /** |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3898 | * 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] | 3899 | * @mapping: address space structure to write |
| 3900 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
| 3901 | * @writepage: function called for each page |
| 3902 | * @data: data passed to writepage function |
| 3903 | * |
| 3904 | * If a page is already under I/O, write_cache_pages() skips it, even |
| 3905 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, |
| 3906 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() |
| 3907 | * and msync() need to guarantee that all the data which was dirty at the time |
| 3908 | * the call was made get new I/O started against them. If wbc->sync_mode is |
| 3909 | * WB_SYNC_ALL then we were called for data integrity and we must wait for |
| 3910 | * existing IO to complete. |
| 3911 | */ |
David Sterba | 4242b64 | 2017-02-10 19:38:24 +0100 | [diff] [blame] | 3912 | static int extent_write_cache_pages(struct address_space *mapping, |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3913 | struct writeback_control *wbc, |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3914 | writepage_t writepage, void *data, |
| 3915 | void (*flush_fn)(void *)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3916 | { |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 3917 | struct inode *inode = mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3918 | int ret = 0; |
| 3919 | int done = 0; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3920 | int nr_to_write_done = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3921 | struct pagevec pvec; |
| 3922 | int nr_pages; |
| 3923 | pgoff_t index; |
| 3924 | pgoff_t end; /* Inclusive */ |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3925 | pgoff_t done_index; |
| 3926 | int range_whole = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3927 | int scanned = 0; |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3928 | int tag; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3929 | |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 3930 | /* |
| 3931 | * We have to hold onto the inode so that ordered extents can do their |
| 3932 | * work when the IO finishes. The alternative to this is failing to add |
| 3933 | * an ordered extent if the igrab() fails there and that is a huge pain |
| 3934 | * to deal with, so instead just hold onto the inode throughout the |
| 3935 | * writepages operation. If it fails here we are freeing up the inode |
| 3936 | * anyway and we'd rather not waste our time writing out stuff that is |
| 3937 | * going to be truncated anyway. |
| 3938 | */ |
| 3939 | if (!igrab(inode)) |
| 3940 | return 0; |
| 3941 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3942 | pagevec_init(&pvec, 0); |
| 3943 | if (wbc->range_cyclic) { |
| 3944 | index = mapping->writeback_index; /* Start from prev offset */ |
| 3945 | end = -1; |
| 3946 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3947 | index = wbc->range_start >> PAGE_SHIFT; |
| 3948 | end = wbc->range_end >> PAGE_SHIFT; |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3949 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 3950 | range_whole = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3951 | scanned = 1; |
| 3952 | } |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3953 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3954 | tag = PAGECACHE_TAG_TOWRITE; |
| 3955 | else |
| 3956 | tag = PAGECACHE_TAG_DIRTY; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3957 | retry: |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3958 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3959 | tag_pages_for_writeback(mapping, index, end); |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3960 | done_index = index; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3961 | while (!done && !nr_to_write_done && (index <= end) && |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3962 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
| 3963 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3964 | unsigned i; |
| 3965 | |
| 3966 | scanned = 1; |
| 3967 | for (i = 0; i < nr_pages; i++) { |
| 3968 | struct page *page = pvec.pages[i]; |
| 3969 | |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3970 | done_index = page->index; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3971 | /* |
| 3972 | * At this point we hold neither mapping->tree_lock nor |
| 3973 | * lock on the page itself: the page may be truncated or |
| 3974 | * invalidated (changing page->mapping to NULL), or even |
| 3975 | * swizzled back from swapper_space to tmpfs file |
| 3976 | * mapping |
| 3977 | */ |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3978 | if (!trylock_page(page)) { |
| 3979 | flush_fn(data); |
| 3980 | lock_page(page); |
Chris Mason | 01d658f | 2011-11-01 10:08:06 -0400 | [diff] [blame] | 3981 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3982 | |
| 3983 | if (unlikely(page->mapping != mapping)) { |
| 3984 | unlock_page(page); |
| 3985 | continue; |
| 3986 | } |
| 3987 | |
| 3988 | if (!wbc->range_cyclic && page->index > end) { |
| 3989 | done = 1; |
| 3990 | unlock_page(page); |
| 3991 | continue; |
| 3992 | } |
| 3993 | |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3994 | if (wbc->sync_mode != WB_SYNC_NONE) { |
Chris Mason | 0e6bd95 | 2008-11-20 10:46:35 -0500 | [diff] [blame] | 3995 | if (PageWriteback(page)) |
| 3996 | flush_fn(data); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3997 | wait_on_page_writeback(page); |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3998 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3999 | |
| 4000 | if (PageWriteback(page) || |
| 4001 | !clear_page_dirty_for_io(page)) { |
| 4002 | unlock_page(page); |
| 4003 | continue; |
| 4004 | } |
| 4005 | |
| 4006 | ret = (*writepage)(page, wbc, data); |
| 4007 | |
| 4008 | if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) { |
| 4009 | unlock_page(page); |
| 4010 | ret = 0; |
| 4011 | } |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 4012 | if (ret < 0) { |
| 4013 | /* |
| 4014 | * done_index is set past this page, |
| 4015 | * so media errors will not choke |
| 4016 | * background writeout for the entire |
| 4017 | * file. This has consequences for |
| 4018 | * range_cyclic semantics (ie. it may |
| 4019 | * not be suitable for data integrity |
| 4020 | * writeout). |
| 4021 | */ |
| 4022 | done_index = page->index + 1; |
| 4023 | done = 1; |
| 4024 | break; |
| 4025 | } |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 4026 | |
| 4027 | /* |
| 4028 | * the filesystem may choose to bump up nr_to_write. |
| 4029 | * We have to make sure to honor the new nr_to_write |
| 4030 | * at any time |
| 4031 | */ |
| 4032 | nr_to_write_done = wbc->nr_to_write <= 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4033 | } |
| 4034 | pagevec_release(&pvec); |
| 4035 | cond_resched(); |
| 4036 | } |
Liu Bo | 894b36e | 2016-03-07 16:56:22 -0800 | [diff] [blame] | 4037 | if (!scanned && !done) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4038 | /* |
| 4039 | * We hit the last page and there is more work to be done: wrap |
| 4040 | * back to the start of the file |
| 4041 | */ |
| 4042 | scanned = 1; |
| 4043 | index = 0; |
| 4044 | goto retry; |
| 4045 | } |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 4046 | |
| 4047 | if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole)) |
| 4048 | mapping->writeback_index = done_index; |
| 4049 | |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 4050 | btrfs_add_delayed_iput(inode); |
Liu Bo | 894b36e | 2016-03-07 16:56:22 -0800 | [diff] [blame] | 4051 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4052 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4053 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4054 | static void flush_epd_write_bio(struct extent_page_data *epd) |
| 4055 | { |
| 4056 | if (epd->bio) { |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 4057 | int ret; |
| 4058 | |
Liu Bo | 18fdc67 | 2017-09-13 12:18:22 -0600 | [diff] [blame^] | 4059 | ret = submit_one_bio(epd->bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4060 | BUG_ON(ret < 0); /* -ENOMEM */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4061 | epd->bio = NULL; |
| 4062 | } |
| 4063 | } |
| 4064 | |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4065 | static noinline void flush_write_bio(void *data) |
| 4066 | { |
| 4067 | struct extent_page_data *epd = data; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4068 | flush_epd_write_bio(epd); |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4069 | } |
| 4070 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4071 | int extent_write_full_page(struct extent_io_tree *tree, struct page *page, |
| 4072 | get_extent_t *get_extent, |
| 4073 | struct writeback_control *wbc) |
| 4074 | { |
| 4075 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4076 | struct extent_page_data epd = { |
| 4077 | .bio = NULL, |
| 4078 | .tree = tree, |
| 4079 | .get_extent = get_extent, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4080 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4081 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4082 | }; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4083 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4084 | ret = __extent_writepage(page, wbc, &epd); |
| 4085 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4086 | flush_epd_write_bio(&epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4087 | return ret; |
| 4088 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4089 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4090 | int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode, |
| 4091 | u64 start, u64 end, get_extent_t *get_extent, |
| 4092 | int mode) |
| 4093 | { |
| 4094 | int ret = 0; |
| 4095 | struct address_space *mapping = inode->i_mapping; |
| 4096 | struct page *page; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4097 | unsigned long nr_pages = (end - start + PAGE_SIZE) >> |
| 4098 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4099 | |
| 4100 | struct extent_page_data epd = { |
| 4101 | .bio = NULL, |
| 4102 | .tree = tree, |
| 4103 | .get_extent = get_extent, |
| 4104 | .extent_locked = 1, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4105 | .sync_io = mode == WB_SYNC_ALL, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4106 | }; |
| 4107 | struct writeback_control wbc_writepages = { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4108 | .sync_mode = mode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4109 | .nr_to_write = nr_pages * 2, |
| 4110 | .range_start = start, |
| 4111 | .range_end = end + 1, |
| 4112 | }; |
| 4113 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4114 | while (start <= end) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4115 | page = find_get_page(mapping, start >> PAGE_SHIFT); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4116 | if (clear_page_dirty_for_io(page)) |
| 4117 | ret = __extent_writepage(page, &wbc_writepages, &epd); |
| 4118 | else { |
| 4119 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 4120 | tree->ops->writepage_end_io_hook(page, start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4121 | start + PAGE_SIZE - 1, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4122 | NULL, 1); |
| 4123 | unlock_page(page); |
| 4124 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4125 | put_page(page); |
| 4126 | start += PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4127 | } |
| 4128 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4129 | flush_epd_write_bio(&epd); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4130 | return ret; |
| 4131 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4132 | |
| 4133 | int extent_writepages(struct extent_io_tree *tree, |
| 4134 | struct address_space *mapping, |
| 4135 | get_extent_t *get_extent, |
| 4136 | struct writeback_control *wbc) |
| 4137 | { |
| 4138 | int ret = 0; |
| 4139 | struct extent_page_data epd = { |
| 4140 | .bio = NULL, |
| 4141 | .tree = tree, |
| 4142 | .get_extent = get_extent, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4143 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4144 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4145 | }; |
| 4146 | |
David Sterba | 4242b64 | 2017-02-10 19:38:24 +0100 | [diff] [blame] | 4147 | ret = extent_write_cache_pages(mapping, wbc, __extent_writepage, &epd, |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4148 | flush_write_bio); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4149 | flush_epd_write_bio(&epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4150 | return ret; |
| 4151 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4152 | |
| 4153 | int extent_readpages(struct extent_io_tree *tree, |
| 4154 | struct address_space *mapping, |
| 4155 | struct list_head *pages, unsigned nr_pages, |
| 4156 | get_extent_t get_extent) |
| 4157 | { |
| 4158 | struct bio *bio = NULL; |
| 4159 | unsigned page_idx; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4160 | unsigned long bio_flags = 0; |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4161 | struct page *pagepool[16]; |
| 4162 | struct page *page; |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4163 | struct extent_map *em_cached = NULL; |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4164 | int nr = 0; |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 4165 | u64 prev_em_start = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4166 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4167 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4168 | page = list_entry(pages->prev, struct page, lru); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4169 | |
| 4170 | prefetchw(&page->flags); |
| 4171 | list_del(&page->lru); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4172 | if (add_to_page_cache_lru(page, mapping, |
Michal Hocko | 8a5c743 | 2016-07-26 15:24:53 -0700 | [diff] [blame] | 4173 | page->index, |
| 4174 | readahead_gfp_mask(mapping))) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4175 | put_page(page); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4176 | continue; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4177 | } |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4178 | |
| 4179 | pagepool[nr++] = page; |
| 4180 | if (nr < ARRAY_SIZE(pagepool)) |
| 4181 | continue; |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4182 | __extent_readpages(tree, pagepool, nr, get_extent, &em_cached, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 4183 | &bio, 0, &bio_flags, &prev_em_start); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4184 | nr = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4185 | } |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 4186 | if (nr) |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4187 | __extent_readpages(tree, pagepool, nr, get_extent, &em_cached, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 4188 | &bio, 0, &bio_flags, &prev_em_start); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4189 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4190 | if (em_cached) |
| 4191 | free_extent_map(em_cached); |
| 4192 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4193 | BUG_ON(!list_empty(pages)); |
| 4194 | if (bio) |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 4195 | return submit_one_bio(bio, 0, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4196 | return 0; |
| 4197 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4198 | |
| 4199 | /* |
| 4200 | * basic invalidatepage code, this waits on any locked or writeback |
| 4201 | * ranges corresponding to the page, and then deletes any extent state |
| 4202 | * records from the tree |
| 4203 | */ |
| 4204 | int extent_invalidatepage(struct extent_io_tree *tree, |
| 4205 | struct page *page, unsigned long offset) |
| 4206 | { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4207 | struct extent_state *cached_state = NULL; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4208 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4209 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4210 | size_t blocksize = page->mapping->host->i_sb->s_blocksize; |
| 4211 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4212 | start += ALIGN(offset, blocksize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4213 | if (start > end) |
| 4214 | return 0; |
| 4215 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 4216 | lock_extent_bits(tree, start, end, &cached_state); |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 4217 | wait_on_page_writeback(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4218 | clear_extent_bit(tree, start, end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 4219 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4220 | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4221 | 1, 1, &cached_state, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4222 | return 0; |
| 4223 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4224 | |
| 4225 | /* |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4226 | * a helper for releasepage, this tests for areas of the page that |
| 4227 | * are locked or under IO and drops the related state bits if it is safe |
| 4228 | * to drop the page. |
| 4229 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 4230 | static int try_release_extent_state(struct extent_map_tree *map, |
| 4231 | struct extent_io_tree *tree, |
| 4232 | struct page *page, gfp_t mask) |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4233 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4234 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4235 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4236 | int ret = 1; |
| 4237 | |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 4238 | if (test_range_bit(tree, start, end, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4239 | EXTENT_IOBITS, 0, NULL)) |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4240 | ret = 0; |
| 4241 | else { |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 4242 | /* |
| 4243 | * at this point we can safely clear everything except the |
| 4244 | * locked bit and the nodatasum bit |
| 4245 | */ |
Chris Mason | e3f24cc | 2011-02-14 12:52:08 -0500 | [diff] [blame] | 4246 | ret = clear_extent_bit(tree, start, end, |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 4247 | ~(EXTENT_LOCKED | EXTENT_NODATASUM), |
| 4248 | 0, 0, NULL, mask); |
Chris Mason | e3f24cc | 2011-02-14 12:52:08 -0500 | [diff] [blame] | 4249 | |
| 4250 | /* if clear_extent_bit failed for enomem reasons, |
| 4251 | * we can't allow the release to continue. |
| 4252 | */ |
| 4253 | if (ret < 0) |
| 4254 | ret = 0; |
| 4255 | else |
| 4256 | ret = 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4257 | } |
| 4258 | return ret; |
| 4259 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4260 | |
| 4261 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4262 | * a helper for releasepage. As long as there are no locked extents |
| 4263 | * in the range corresponding to the page, both state records and extent |
| 4264 | * map records are removed |
| 4265 | */ |
| 4266 | int try_release_extent_mapping(struct extent_map_tree *map, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4267 | struct extent_io_tree *tree, struct page *page, |
| 4268 | gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4269 | { |
| 4270 | struct extent_map *em; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4271 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4272 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4273 | |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 4274 | if (gfpflags_allow_blocking(mask) && |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4275 | page->mapping->host->i_size > SZ_16M) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4276 | u64 len; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4277 | while (start <= end) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4278 | len = end - start + 1; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4279 | write_lock(&map->lock); |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4280 | em = lookup_extent_mapping(map, start, len); |
Tsutomu Itoh | 285190d | 2012-02-16 16:23:58 +0900 | [diff] [blame] | 4281 | if (!em) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4282 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4283 | break; |
| 4284 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 4285 | if (test_bit(EXTENT_FLAG_PINNED, &em->flags) || |
| 4286 | em->start != start) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4287 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4288 | free_extent_map(em); |
| 4289 | break; |
| 4290 | } |
| 4291 | if (!test_range_bit(tree, em->start, |
| 4292 | extent_map_end(em) - 1, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4293 | EXTENT_LOCKED | EXTENT_WRITEBACK, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 4294 | 0, NULL)) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4295 | remove_extent_mapping(map, em); |
| 4296 | /* once for the rb tree */ |
| 4297 | free_extent_map(em); |
| 4298 | } |
| 4299 | start = extent_map_end(em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4300 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4301 | |
| 4302 | /* once for us */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4303 | free_extent_map(em); |
| 4304 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4305 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4306 | return try_release_extent_state(map, tree, page, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4307 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4308 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4309 | /* |
| 4310 | * helper function for fiemap, which doesn't want to see any holes. |
| 4311 | * This maps until we find something past 'last' |
| 4312 | */ |
| 4313 | static struct extent_map *get_extent_skip_holes(struct inode *inode, |
| 4314 | u64 offset, |
| 4315 | u64 last, |
| 4316 | get_extent_t *get_extent) |
| 4317 | { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4318 | u64 sectorsize = btrfs_inode_sectorsize(inode); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4319 | struct extent_map *em; |
| 4320 | u64 len; |
| 4321 | |
| 4322 | if (offset >= last) |
| 4323 | return NULL; |
| 4324 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 4325 | while (1) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4326 | len = last - offset; |
| 4327 | if (len == 0) |
| 4328 | break; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4329 | len = ALIGN(len, sectorsize); |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 4330 | em = get_extent(BTRFS_I(inode), NULL, 0, offset, len, 0); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 4331 | if (IS_ERR_OR_NULL(em)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4332 | return em; |
| 4333 | |
| 4334 | /* if this isn't a hole return it */ |
| 4335 | if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) && |
| 4336 | em->block_start != EXTENT_MAP_HOLE) { |
| 4337 | return em; |
| 4338 | } |
| 4339 | |
| 4340 | /* this is a hole, advance to the next extent */ |
| 4341 | offset = extent_map_end(em); |
| 4342 | free_extent_map(em); |
| 4343 | if (offset >= last) |
| 4344 | break; |
| 4345 | } |
| 4346 | return NULL; |
| 4347 | } |
| 4348 | |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4349 | /* |
| 4350 | * To cache previous fiemap extent |
| 4351 | * |
| 4352 | * Will be used for merging fiemap extent |
| 4353 | */ |
| 4354 | struct fiemap_cache { |
| 4355 | u64 offset; |
| 4356 | u64 phys; |
| 4357 | u64 len; |
| 4358 | u32 flags; |
| 4359 | bool cached; |
| 4360 | }; |
| 4361 | |
| 4362 | /* |
| 4363 | * Helper to submit fiemap extent. |
| 4364 | * |
| 4365 | * Will try to merge current fiemap extent specified by @offset, @phys, |
| 4366 | * @len and @flags with cached one. |
| 4367 | * And only when we fails to merge, cached one will be submitted as |
| 4368 | * fiemap extent. |
| 4369 | * |
| 4370 | * Return value is the same as fiemap_fill_next_extent(). |
| 4371 | */ |
| 4372 | static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo, |
| 4373 | struct fiemap_cache *cache, |
| 4374 | u64 offset, u64 phys, u64 len, u32 flags) |
| 4375 | { |
| 4376 | int ret = 0; |
| 4377 | |
| 4378 | if (!cache->cached) |
| 4379 | goto assign; |
| 4380 | |
| 4381 | /* |
| 4382 | * Sanity check, extent_fiemap() should have ensured that new |
| 4383 | * fiemap extent won't overlap with cahced one. |
| 4384 | * Not recoverable. |
| 4385 | * |
| 4386 | * NOTE: Physical address can overlap, due to compression |
| 4387 | */ |
| 4388 | if (cache->offset + cache->len > offset) { |
| 4389 | WARN_ON(1); |
| 4390 | return -EINVAL; |
| 4391 | } |
| 4392 | |
| 4393 | /* |
| 4394 | * Only merges fiemap extents if |
| 4395 | * 1) Their logical addresses are continuous |
| 4396 | * |
| 4397 | * 2) Their physical addresses are continuous |
| 4398 | * So truly compressed (physical size smaller than logical size) |
| 4399 | * extents won't get merged with each other |
| 4400 | * |
| 4401 | * 3) Share same flags except FIEMAP_EXTENT_LAST |
| 4402 | * So regular extent won't get merged with prealloc extent |
| 4403 | */ |
| 4404 | if (cache->offset + cache->len == offset && |
| 4405 | cache->phys + cache->len == phys && |
| 4406 | (cache->flags & ~FIEMAP_EXTENT_LAST) == |
| 4407 | (flags & ~FIEMAP_EXTENT_LAST)) { |
| 4408 | cache->len += len; |
| 4409 | cache->flags |= flags; |
| 4410 | goto try_submit_last; |
| 4411 | } |
| 4412 | |
| 4413 | /* Not mergeable, need to submit cached one */ |
| 4414 | ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys, |
| 4415 | cache->len, cache->flags); |
| 4416 | cache->cached = false; |
| 4417 | if (ret) |
| 4418 | return ret; |
| 4419 | assign: |
| 4420 | cache->cached = true; |
| 4421 | cache->offset = offset; |
| 4422 | cache->phys = phys; |
| 4423 | cache->len = len; |
| 4424 | cache->flags = flags; |
| 4425 | try_submit_last: |
| 4426 | if (cache->flags & FIEMAP_EXTENT_LAST) { |
| 4427 | ret = fiemap_fill_next_extent(fieinfo, cache->offset, |
| 4428 | cache->phys, cache->len, cache->flags); |
| 4429 | cache->cached = false; |
| 4430 | } |
| 4431 | return ret; |
| 4432 | } |
| 4433 | |
| 4434 | /* |
Qu Wenruo | 848c23b | 2017-06-22 10:01:21 +0800 | [diff] [blame] | 4435 | * Emit last fiemap cache |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4436 | * |
Qu Wenruo | 848c23b | 2017-06-22 10:01:21 +0800 | [diff] [blame] | 4437 | * The last fiemap cache may still be cached in the following case: |
| 4438 | * 0 4k 8k |
| 4439 | * |<- Fiemap range ->| |
| 4440 | * |<------------ First extent ----------->| |
| 4441 | * |
| 4442 | * In this case, the first extent range will be cached but not emitted. |
| 4443 | * So we must emit it before ending extent_fiemap(). |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4444 | */ |
Qu Wenruo | 848c23b | 2017-06-22 10:01:21 +0800 | [diff] [blame] | 4445 | static int emit_last_fiemap_cache(struct btrfs_fs_info *fs_info, |
| 4446 | struct fiemap_extent_info *fieinfo, |
| 4447 | struct fiemap_cache *cache) |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4448 | { |
| 4449 | int ret; |
| 4450 | |
| 4451 | if (!cache->cached) |
| 4452 | return 0; |
| 4453 | |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4454 | ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys, |
| 4455 | cache->len, cache->flags); |
| 4456 | cache->cached = false; |
| 4457 | if (ret > 0) |
| 4458 | ret = 0; |
| 4459 | return ret; |
| 4460 | } |
| 4461 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4462 | int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4463 | __u64 start, __u64 len, get_extent_t *get_extent) |
| 4464 | { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4465 | int ret = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4466 | u64 off = start; |
| 4467 | u64 max = start + len; |
| 4468 | u32 flags = 0; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4469 | u32 found_type; |
| 4470 | u64 last; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4471 | u64 last_for_get_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4472 | u64 disko = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4473 | u64 isize = i_size_read(inode); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4474 | struct btrfs_key found_key; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4475 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4476 | struct extent_state *cached_state = NULL; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4477 | struct btrfs_path *path; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4478 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4479 | struct fiemap_cache cache = { 0 }; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4480 | int end = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4481 | u64 em_start = 0; |
| 4482 | u64 em_len = 0; |
| 4483 | u64 em_end = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4484 | |
| 4485 | if (len == 0) |
| 4486 | return -EINVAL; |
| 4487 | |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4488 | path = btrfs_alloc_path(); |
| 4489 | if (!path) |
| 4490 | return -ENOMEM; |
| 4491 | path->leave_spinning = 1; |
| 4492 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4493 | start = round_down(start, btrfs_inode_sectorsize(inode)); |
| 4494 | len = round_up(max, btrfs_inode_sectorsize(inode)) - start; |
Josef Bacik | 4d479cf | 2011-11-17 11:34:31 -0500 | [diff] [blame] | 4495 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4496 | /* |
| 4497 | * lookup the last file extent. We're not using i_size here |
| 4498 | * because there might be preallocation past i_size |
| 4499 | */ |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4500 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 4501 | btrfs_ino(BTRFS_I(inode)), -1, 0); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4502 | if (ret < 0) { |
| 4503 | btrfs_free_path(path); |
| 4504 | return ret; |
Liu Bo | 2d324f5 | 2016-05-17 17:21:48 -0700 | [diff] [blame] | 4505 | } else { |
| 4506 | WARN_ON(!ret); |
| 4507 | if (ret == 1) |
| 4508 | ret = 0; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4509 | } |
Liu Bo | 2d324f5 | 2016-05-17 17:21:48 -0700 | [diff] [blame] | 4510 | |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4511 | path->slots[0]--; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4512 | 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] | 4513 | found_type = found_key.type; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4514 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4515 | /* No extents, but there might be delalloc bits */ |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4516 | if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) || |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4517 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4518 | /* have to trust i_size as the end */ |
| 4519 | last = (u64)-1; |
| 4520 | last_for_get_extent = isize; |
| 4521 | } else { |
| 4522 | /* |
| 4523 | * remember the start of the last extent. There are a |
| 4524 | * bunch of different factors that go into the length of the |
| 4525 | * extent, so its much less complex to remember where it started |
| 4526 | */ |
| 4527 | last = found_key.offset; |
| 4528 | last_for_get_extent = last + 1; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4529 | } |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4530 | btrfs_release_path(path); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4531 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4532 | /* |
| 4533 | * we might have some extents allocated but more delalloc past those |
| 4534 | * extents. so, we trust isize unless the start of the last extent is |
| 4535 | * beyond isize |
| 4536 | */ |
| 4537 | if (last < isize) { |
| 4538 | last = (u64)-1; |
| 4539 | last_for_get_extent = isize; |
| 4540 | } |
| 4541 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 4542 | lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4543 | &cached_state); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4544 | |
Josef Bacik | 4d479cf | 2011-11-17 11:34:31 -0500 | [diff] [blame] | 4545 | em = get_extent_skip_holes(inode, start, last_for_get_extent, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4546 | get_extent); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4547 | if (!em) |
| 4548 | goto out; |
| 4549 | if (IS_ERR(em)) { |
| 4550 | ret = PTR_ERR(em); |
| 4551 | goto out; |
| 4552 | } |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4553 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4554 | while (!end) { |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4555 | u64 offset_in_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4556 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4557 | /* break if the extent we found is outside the range */ |
| 4558 | if (em->start >= max || extent_map_end(em) < off) |
| 4559 | break; |
| 4560 | |
| 4561 | /* |
| 4562 | * get_extent may return an extent that starts before our |
| 4563 | * requested range. We have to make sure the ranges |
| 4564 | * we return to fiemap always move forward and don't |
| 4565 | * overlap, so adjust the offsets here |
| 4566 | */ |
| 4567 | em_start = max(em->start, off); |
| 4568 | |
| 4569 | /* |
| 4570 | * record the offset from the start of the extent |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4571 | * for adjusting the disk offset below. Only do this if the |
| 4572 | * extent isn't compressed since our in ram offset may be past |
| 4573 | * what we have actually allocated on disk. |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4574 | */ |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4575 | if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 4576 | offset_in_extent = em_start - em->start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4577 | em_end = extent_map_end(em); |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4578 | em_len = em_end - em_start; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4579 | disko = 0; |
| 4580 | flags = 0; |
| 4581 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4582 | /* |
| 4583 | * bump off for our next call to get_extent |
| 4584 | */ |
| 4585 | off = extent_map_end(em); |
| 4586 | if (off >= max) |
| 4587 | end = 1; |
| 4588 | |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4589 | if (em->block_start == EXTENT_MAP_LAST_BYTE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4590 | end = 1; |
| 4591 | flags |= FIEMAP_EXTENT_LAST; |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4592 | } else if (em->block_start == EXTENT_MAP_INLINE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4593 | flags |= (FIEMAP_EXTENT_DATA_INLINE | |
| 4594 | FIEMAP_EXTENT_NOT_ALIGNED); |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4595 | } else if (em->block_start == EXTENT_MAP_DELALLOC) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4596 | flags |= (FIEMAP_EXTENT_DELALLOC | |
| 4597 | FIEMAP_EXTENT_UNKNOWN); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4598 | } else if (fieinfo->fi_extents_max) { |
| 4599 | u64 bytenr = em->block_start - |
| 4600 | (em->start - em->orig_start); |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4601 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4602 | disko = em->block_start + offset_in_extent; |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4603 | |
| 4604 | /* |
| 4605 | * As btrfs supports shared space, this information |
| 4606 | * can be exported to userspace tools via |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4607 | * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0 |
| 4608 | * then we're just getting a count and we can skip the |
| 4609 | * lookup stuff. |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4610 | */ |
Edmund Nadolski | bb739cf | 2017-06-28 21:56:58 -0600 | [diff] [blame] | 4611 | ret = btrfs_check_shared(root, |
| 4612 | btrfs_ino(BTRFS_I(inode)), |
| 4613 | bytenr); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4614 | if (ret < 0) |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4615 | goto out_free; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4616 | if (ret) |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4617 | flags |= FIEMAP_EXTENT_SHARED; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4618 | ret = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4619 | } |
| 4620 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 4621 | flags |= FIEMAP_EXTENT_ENCODED; |
Josef Bacik | 0d2b237 | 2015-05-19 10:44:04 -0400 | [diff] [blame] | 4622 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 4623 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4624 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4625 | free_extent_map(em); |
| 4626 | em = NULL; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4627 | if ((em_start >= last) || em_len == (u64)-1 || |
| 4628 | (last == (u64)-1 && isize <= em_end)) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4629 | flags |= FIEMAP_EXTENT_LAST; |
| 4630 | end = 1; |
| 4631 | } |
| 4632 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4633 | /* now scan forward to see if this is really the last extent. */ |
| 4634 | em = get_extent_skip_holes(inode, off, last_for_get_extent, |
| 4635 | get_extent); |
| 4636 | if (IS_ERR(em)) { |
| 4637 | ret = PTR_ERR(em); |
| 4638 | goto out; |
| 4639 | } |
| 4640 | if (!em) { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4641 | flags |= FIEMAP_EXTENT_LAST; |
| 4642 | end = 1; |
| 4643 | } |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4644 | ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko, |
| 4645 | em_len, flags); |
Chengyu Song | 26e726a | 2015-03-24 18:12:56 -0400 | [diff] [blame] | 4646 | if (ret) { |
| 4647 | if (ret == 1) |
| 4648 | ret = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4649 | goto out_free; |
Chengyu Song | 26e726a | 2015-03-24 18:12:56 -0400 | [diff] [blame] | 4650 | } |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4651 | } |
| 4652 | out_free: |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4653 | if (!ret) |
Qu Wenruo | 848c23b | 2017-06-22 10:01:21 +0800 | [diff] [blame] | 4654 | ret = emit_last_fiemap_cache(root->fs_info, fieinfo, &cache); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4655 | free_extent_map(em); |
| 4656 | out: |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4657 | btrfs_free_path(path); |
Liu Bo | a52f4cd | 2013-05-01 16:23:41 +0000 | [diff] [blame] | 4658 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4659 | &cached_state, GFP_NOFS); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4660 | return ret; |
| 4661 | } |
| 4662 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4663 | static void __free_extent_buffer(struct extent_buffer *eb) |
| 4664 | { |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 4665 | btrfs_leak_debug_del(&eb->leak_list); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4666 | kmem_cache_free(extent_buffer_cache, eb); |
| 4667 | } |
| 4668 | |
Josef Bacik | a26e8c9 | 2014-03-28 17:07:27 -0400 | [diff] [blame] | 4669 | int extent_buffer_under_io(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4670 | { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4671 | return (atomic_read(&eb->io_pages) || |
| 4672 | test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) || |
| 4673 | test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4674 | } |
| 4675 | |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4676 | /* |
| 4677 | * Helper for releasing extent buffer page. |
| 4678 | */ |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4679 | static void btrfs_release_extent_buffer_page(struct extent_buffer *eb) |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4680 | { |
| 4681 | unsigned long index; |
| 4682 | struct page *page; |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4683 | int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4684 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4685 | BUG_ON(extent_buffer_under_io(eb)); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4686 | |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4687 | index = num_extent_pages(eb->start, eb->len); |
| 4688 | if (index == 0) |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4689 | return; |
| 4690 | |
| 4691 | do { |
| 4692 | index--; |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 4693 | page = eb->pages[index]; |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4694 | if (!page) |
| 4695 | continue; |
| 4696 | if (mapped) |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4697 | spin_lock(&page->mapping->private_lock); |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4698 | /* |
| 4699 | * We do this since we'll remove the pages after we've |
| 4700 | * removed the eb from the radix tree, so we could race |
| 4701 | * and have this page now attached to the new eb. So |
| 4702 | * only clear page_private if it's still connected to |
| 4703 | * this eb. |
| 4704 | */ |
| 4705 | if (PagePrivate(page) && |
| 4706 | page->private == (unsigned long)eb) { |
| 4707 | BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
| 4708 | BUG_ON(PageDirty(page)); |
| 4709 | BUG_ON(PageWriteback(page)); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4710 | /* |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4711 | * We need to make sure we haven't be attached |
| 4712 | * to a new eb. |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4713 | */ |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4714 | ClearPagePrivate(page); |
| 4715 | set_page_private(page, 0); |
| 4716 | /* One for the page private */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4717 | put_page(page); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4718 | } |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4719 | |
| 4720 | if (mapped) |
| 4721 | spin_unlock(&page->mapping->private_lock); |
| 4722 | |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 4723 | /* One for when we allocated the page */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4724 | put_page(page); |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4725 | } while (index != 0); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4726 | } |
| 4727 | |
| 4728 | /* |
| 4729 | * Helper for releasing the extent buffer. |
| 4730 | */ |
| 4731 | static inline void btrfs_release_extent_buffer(struct extent_buffer *eb) |
| 4732 | { |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4733 | btrfs_release_extent_buffer_page(eb); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4734 | __free_extent_buffer(eb); |
| 4735 | } |
| 4736 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4737 | static struct extent_buffer * |
| 4738 | __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, |
David Sterba | 23d79d8 | 2014-06-15 02:55:29 +0200 | [diff] [blame] | 4739 | unsigned long len) |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4740 | { |
| 4741 | struct extent_buffer *eb = NULL; |
| 4742 | |
Michal Hocko | d1b5c56 | 2015-08-19 14:17:40 +0200 | [diff] [blame] | 4743 | eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4744 | eb->start = start; |
| 4745 | eb->len = len; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4746 | eb->fs_info = fs_info; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4747 | eb->bflags = 0; |
| 4748 | rwlock_init(&eb->lock); |
| 4749 | atomic_set(&eb->write_locks, 0); |
| 4750 | atomic_set(&eb->read_locks, 0); |
| 4751 | atomic_set(&eb->blocking_readers, 0); |
| 4752 | atomic_set(&eb->blocking_writers, 0); |
| 4753 | atomic_set(&eb->spinning_readers, 0); |
| 4754 | atomic_set(&eb->spinning_writers, 0); |
| 4755 | eb->lock_nested = 0; |
| 4756 | init_waitqueue_head(&eb->write_lock_wq); |
| 4757 | init_waitqueue_head(&eb->read_lock_wq); |
| 4758 | |
| 4759 | btrfs_leak_debug_add(&eb->leak_list, &buffers); |
| 4760 | |
| 4761 | spin_lock_init(&eb->refs_lock); |
| 4762 | atomic_set(&eb->refs, 1); |
| 4763 | atomic_set(&eb->io_pages, 0); |
| 4764 | |
| 4765 | /* |
| 4766 | * Sanity checks, currently the maximum is 64k covered by 16x 4k pages |
| 4767 | */ |
| 4768 | BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE |
| 4769 | > MAX_INLINE_EXTENT_BUFFER_SIZE); |
| 4770 | BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE); |
| 4771 | |
| 4772 | return eb; |
| 4773 | } |
| 4774 | |
| 4775 | struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src) |
| 4776 | { |
| 4777 | unsigned long i; |
| 4778 | struct page *p; |
| 4779 | struct extent_buffer *new; |
| 4780 | unsigned long num_pages = num_extent_pages(src->start, src->len); |
| 4781 | |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4782 | new = __alloc_extent_buffer(src->fs_info, src->start, src->len); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4783 | if (new == NULL) |
| 4784 | return NULL; |
| 4785 | |
| 4786 | for (i = 0; i < num_pages; i++) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 4787 | p = alloc_page(GFP_NOFS); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4788 | if (!p) { |
| 4789 | btrfs_release_extent_buffer(new); |
| 4790 | return NULL; |
| 4791 | } |
| 4792 | attach_extent_buffer_page(new, p); |
| 4793 | WARN_ON(PageDirty(p)); |
| 4794 | SetPageUptodate(p); |
| 4795 | new->pages[i] = p; |
David Sterba | fba1acf | 2016-11-08 17:56:24 +0100 | [diff] [blame] | 4796 | copy_page(page_address(p), page_address(src->pages[i])); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4797 | } |
| 4798 | |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4799 | set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags); |
| 4800 | set_bit(EXTENT_BUFFER_DUMMY, &new->bflags); |
| 4801 | |
| 4802 | return new; |
| 4803 | } |
| 4804 | |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4805 | struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, |
| 4806 | u64 start, unsigned long len) |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4807 | { |
| 4808 | struct extent_buffer *eb; |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4809 | unsigned long num_pages; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4810 | unsigned long i; |
| 4811 | |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4812 | num_pages = num_extent_pages(start, len); |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4813 | |
| 4814 | eb = __alloc_extent_buffer(fs_info, start, len); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4815 | if (!eb) |
| 4816 | return NULL; |
| 4817 | |
| 4818 | for (i = 0; i < num_pages; i++) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 4819 | eb->pages[i] = alloc_page(GFP_NOFS); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4820 | if (!eb->pages[i]) |
| 4821 | goto err; |
| 4822 | } |
| 4823 | set_extent_buffer_uptodate(eb); |
| 4824 | btrfs_set_header_nritems(eb, 0); |
| 4825 | set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags); |
| 4826 | |
| 4827 | return eb; |
| 4828 | err: |
| 4829 | for (; i > 0; i--) |
| 4830 | __free_page(eb->pages[i - 1]); |
| 4831 | __free_extent_buffer(eb); |
| 4832 | return NULL; |
| 4833 | } |
| 4834 | |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4835 | 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] | 4836 | u64 start) |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4837 | { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4838 | return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize); |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4839 | } |
| 4840 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4841 | static void check_buffer_tree_ref(struct extent_buffer *eb) |
| 4842 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4843 | int refs; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4844 | /* the ref bit is tricky. We have to make sure it is set |
| 4845 | * if we have the buffer dirty. Otherwise the |
| 4846 | * code to free a buffer can end up dropping a dirty |
| 4847 | * page |
| 4848 | * |
| 4849 | * Once the ref bit is set, it won't go away while the |
| 4850 | * buffer is dirty or in writeback, and it also won't |
| 4851 | * go away while we have the reference count on the |
| 4852 | * eb bumped. |
| 4853 | * |
| 4854 | * We can't just set the ref bit without bumping the |
| 4855 | * ref on the eb because free_extent_buffer might |
| 4856 | * see the ref bit and try to clear it. If this happens |
| 4857 | * free_extent_buffer might end up dropping our original |
| 4858 | * ref by mistake and freeing the page before we are able |
| 4859 | * to add one more ref. |
| 4860 | * |
| 4861 | * So bump the ref count first, then set the bit. If someone |
| 4862 | * beat us to it, drop the ref we added. |
| 4863 | */ |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4864 | refs = atomic_read(&eb->refs); |
| 4865 | if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 4866 | return; |
| 4867 | |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4868 | spin_lock(&eb->refs_lock); |
| 4869 | if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4870 | atomic_inc(&eb->refs); |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4871 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4872 | } |
| 4873 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4874 | static void mark_extent_buffer_accessed(struct extent_buffer *eb, |
| 4875 | struct page *accessed) |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4876 | { |
| 4877 | unsigned long num_pages, i; |
| 4878 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4879 | check_buffer_tree_ref(eb); |
| 4880 | |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4881 | num_pages = num_extent_pages(eb->start, eb->len); |
| 4882 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 4883 | struct page *p = eb->pages[i]; |
| 4884 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4885 | if (p != accessed) |
| 4886 | mark_page_accessed(p); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4887 | } |
| 4888 | } |
| 4889 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4890 | struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info, |
| 4891 | u64 start) |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4892 | { |
| 4893 | struct extent_buffer *eb; |
| 4894 | |
| 4895 | rcu_read_lock(); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4896 | eb = radix_tree_lookup(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4897 | start >> PAGE_SHIFT); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4898 | if (eb && atomic_inc_not_zero(&eb->refs)) { |
| 4899 | rcu_read_unlock(); |
Filipe Manana | 062c19e | 2015-04-23 11:28:48 +0100 | [diff] [blame] | 4900 | /* |
| 4901 | * Lock our eb's refs_lock to avoid races with |
| 4902 | * free_extent_buffer. When we get our eb it might be flagged |
| 4903 | * with EXTENT_BUFFER_STALE and another task running |
| 4904 | * free_extent_buffer might have seen that flag set, |
| 4905 | * eb->refs == 2, that the buffer isn't under IO (dirty and |
| 4906 | * writeback flags not set) and it's still in the tree (flag |
| 4907 | * EXTENT_BUFFER_TREE_REF set), therefore being in the process |
| 4908 | * of decrementing the extent buffer's reference count twice. |
| 4909 | * So here we could race and increment the eb's reference count, |
| 4910 | * clear its stale flag, mark it as dirty and drop our reference |
| 4911 | * before the other task finishes executing free_extent_buffer, |
| 4912 | * which would later result in an attempt to free an extent |
| 4913 | * buffer that is dirty. |
| 4914 | */ |
| 4915 | if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) { |
| 4916 | spin_lock(&eb->refs_lock); |
| 4917 | spin_unlock(&eb->refs_lock); |
| 4918 | } |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4919 | mark_extent_buffer_accessed(eb, NULL); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4920 | return eb; |
| 4921 | } |
| 4922 | rcu_read_unlock(); |
| 4923 | |
| 4924 | return NULL; |
| 4925 | } |
| 4926 | |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4927 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 4928 | 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] | 4929 | u64 start) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4930 | { |
| 4931 | struct extent_buffer *eb, *exists = NULL; |
| 4932 | int ret; |
| 4933 | |
| 4934 | eb = find_extent_buffer(fs_info, start); |
| 4935 | if (eb) |
| 4936 | return eb; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4937 | eb = alloc_dummy_extent_buffer(fs_info, start); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4938 | if (!eb) |
| 4939 | return NULL; |
| 4940 | eb->fs_info = fs_info; |
| 4941 | again: |
David Sterba | e1860a7 | 2016-05-09 14:11:38 +0200 | [diff] [blame] | 4942 | ret = radix_tree_preload(GFP_NOFS); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4943 | if (ret) |
| 4944 | goto free_eb; |
| 4945 | spin_lock(&fs_info->buffer_lock); |
| 4946 | ret = radix_tree_insert(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4947 | start >> PAGE_SHIFT, eb); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4948 | spin_unlock(&fs_info->buffer_lock); |
| 4949 | radix_tree_preload_end(); |
| 4950 | if (ret == -EEXIST) { |
| 4951 | exists = find_extent_buffer(fs_info, start); |
| 4952 | if (exists) |
| 4953 | goto free_eb; |
| 4954 | else |
| 4955 | goto again; |
| 4956 | } |
| 4957 | check_buffer_tree_ref(eb); |
| 4958 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
| 4959 | |
| 4960 | /* |
| 4961 | * We will free dummy extent buffer's if they come into |
| 4962 | * free_extent_buffer with a ref count of 2, but if we are using this we |
| 4963 | * want the buffers to stay in memory until we're done with them, so |
| 4964 | * bump the ref count again. |
| 4965 | */ |
| 4966 | atomic_inc(&eb->refs); |
| 4967 | return eb; |
| 4968 | free_eb: |
| 4969 | btrfs_release_extent_buffer(eb); |
| 4970 | return exists; |
| 4971 | } |
| 4972 | #endif |
| 4973 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4974 | struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, |
David Sterba | ce3e698 | 2014-06-15 03:00:04 +0200 | [diff] [blame] | 4975 | u64 start) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4976 | { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4977 | unsigned long len = fs_info->nodesize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4978 | unsigned long num_pages = num_extent_pages(start, len); |
| 4979 | unsigned long i; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4980 | unsigned long index = start >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4981 | struct extent_buffer *eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4982 | struct extent_buffer *exists = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4983 | struct page *p; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4984 | struct address_space *mapping = fs_info->btree_inode->i_mapping; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4985 | int uptodate = 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4986 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4987 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4988 | if (!IS_ALIGNED(start, fs_info->sectorsize)) { |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4989 | btrfs_err(fs_info, "bad tree block start %llu", start); |
| 4990 | return ERR_PTR(-EINVAL); |
| 4991 | } |
| 4992 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4993 | eb = find_extent_buffer(fs_info, start); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4994 | if (eb) |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4995 | return eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4996 | |
David Sterba | 23d79d8 | 2014-06-15 02:55:29 +0200 | [diff] [blame] | 4997 | eb = __alloc_extent_buffer(fs_info, start, len); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 4998 | if (!eb) |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4999 | return ERR_PTR(-ENOMEM); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5000 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5001 | for (i = 0; i < num_pages; i++, index++) { |
Michal Hocko | d1b5c56 | 2015-08-19 14:17:40 +0200 | [diff] [blame] | 5002 | p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 5003 | if (!p) { |
| 5004 | exists = ERR_PTR(-ENOMEM); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5005 | goto free_eb; |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 5006 | } |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 5007 | |
| 5008 | spin_lock(&mapping->private_lock); |
| 5009 | if (PagePrivate(p)) { |
| 5010 | /* |
| 5011 | * We could have already allocated an eb for this page |
| 5012 | * and attached one so lets see if we can get a ref on |
| 5013 | * the existing eb, and if we can we know it's good and |
| 5014 | * we can just return that one, else we know we can just |
| 5015 | * overwrite page->private. |
| 5016 | */ |
| 5017 | exists = (struct extent_buffer *)p->private; |
| 5018 | if (atomic_inc_not_zero(&exists->refs)) { |
| 5019 | spin_unlock(&mapping->private_lock); |
| 5020 | unlock_page(p); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5021 | put_page(p); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 5022 | mark_extent_buffer_accessed(exists, p); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 5023 | goto free_eb; |
| 5024 | } |
Omar Sandoval | 5ca64f4 | 2015-02-24 02:47:05 -0800 | [diff] [blame] | 5025 | exists = NULL; |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 5026 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5027 | /* |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 5028 | * Do this so attach doesn't complain and we need to |
| 5029 | * drop the ref the old guy had. |
| 5030 | */ |
| 5031 | ClearPagePrivate(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5032 | WARN_ON(PageDirty(p)); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5033 | put_page(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5034 | } |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 5035 | attach_extent_buffer_page(eb, p); |
| 5036 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5037 | WARN_ON(PageDirty(p)); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5038 | eb->pages[i] = p; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5039 | if (!PageUptodate(p)) |
| 5040 | uptodate = 0; |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5041 | |
| 5042 | /* |
| 5043 | * see below about how we avoid a nasty race with release page |
| 5044 | * and why we unlock later |
| 5045 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5046 | } |
| 5047 | if (uptodate) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5048 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 5049 | again: |
David Sterba | e1860a7 | 2016-05-09 14:11:38 +0200 | [diff] [blame] | 5050 | ret = radix_tree_preload(GFP_NOFS); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 5051 | if (ret) { |
| 5052 | exists = ERR_PTR(ret); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5053 | goto free_eb; |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 5054 | } |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5055 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5056 | spin_lock(&fs_info->buffer_lock); |
| 5057 | ret = radix_tree_insert(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5058 | start >> PAGE_SHIFT, eb); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5059 | spin_unlock(&fs_info->buffer_lock); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 5060 | radix_tree_preload_end(); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5061 | if (ret == -EEXIST) { |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5062 | exists = find_extent_buffer(fs_info, start); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 5063 | if (exists) |
| 5064 | goto free_eb; |
| 5065 | else |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 5066 | goto again; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5067 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5068 | /* add one reference for the tree */ |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5069 | check_buffer_tree_ref(eb); |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 5070 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5071 | |
| 5072 | /* |
| 5073 | * there is a race where release page may have |
| 5074 | * tried to find this extent buffer in the radix |
| 5075 | * but failed. It will tell the VM it is safe to |
| 5076 | * reclaim the, and it will clear the page private bit. |
| 5077 | * We must make sure to set the page private bit properly |
| 5078 | * after the extent buffer is in the radix tree so |
| 5079 | * it doesn't get lost |
| 5080 | */ |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5081 | SetPageChecked(eb->pages[0]); |
| 5082 | for (i = 1; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5083 | p = eb->pages[i]; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5084 | ClearPageChecked(p); |
| 5085 | unlock_page(p); |
| 5086 | } |
| 5087 | unlock_page(eb->pages[0]); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5088 | return eb; |
| 5089 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5090 | free_eb: |
Omar Sandoval | 5ca64f4 | 2015-02-24 02:47:05 -0800 | [diff] [blame] | 5091 | WARN_ON(!atomic_dec_and_test(&eb->refs)); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5092 | for (i = 0; i < num_pages; i++) { |
| 5093 | if (eb->pages[i]) |
| 5094 | unlock_page(eb->pages[i]); |
| 5095 | } |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5096 | |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5097 | btrfs_release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5098 | return exists; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5099 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5100 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5101 | static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head) |
| 5102 | { |
| 5103 | struct extent_buffer *eb = |
| 5104 | container_of(head, struct extent_buffer, rcu_head); |
| 5105 | |
| 5106 | __free_extent_buffer(eb); |
| 5107 | } |
| 5108 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5109 | /* Expects to have eb->eb_lock already held */ |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5110 | static int release_extent_buffer(struct extent_buffer *eb) |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5111 | { |
| 5112 | WARN_ON(atomic_read(&eb->refs) == 0); |
| 5113 | if (atomic_dec_and_test(&eb->refs)) { |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 5114 | if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) { |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5115 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5116 | |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 5117 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5118 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5119 | spin_lock(&fs_info->buffer_lock); |
| 5120 | radix_tree_delete(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5121 | eb->start >> PAGE_SHIFT); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5122 | spin_unlock(&fs_info->buffer_lock); |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 5123 | } else { |
| 5124 | spin_unlock(&eb->refs_lock); |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 5125 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5126 | |
| 5127 | /* Should be safe to release our pages at this point */ |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 5128 | btrfs_release_extent_buffer_page(eb); |
Josef Bacik | bcb7e44 | 2015-03-16 17:38:02 -0400 | [diff] [blame] | 5129 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 5130 | if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) { |
| 5131 | __free_extent_buffer(eb); |
| 5132 | return 1; |
| 5133 | } |
| 5134 | #endif |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5135 | call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 5136 | return 1; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5137 | } |
| 5138 | spin_unlock(&eb->refs_lock); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 5139 | |
| 5140 | return 0; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5141 | } |
| 5142 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5143 | void free_extent_buffer(struct extent_buffer *eb) |
| 5144 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 5145 | int refs; |
| 5146 | int old; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5147 | if (!eb) |
| 5148 | return; |
| 5149 | |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 5150 | while (1) { |
| 5151 | refs = atomic_read(&eb->refs); |
| 5152 | if (refs <= 3) |
| 5153 | break; |
| 5154 | old = atomic_cmpxchg(&eb->refs, refs, refs - 1); |
| 5155 | if (old == refs) |
| 5156 | return; |
| 5157 | } |
| 5158 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5159 | spin_lock(&eb->refs_lock); |
| 5160 | if (atomic_read(&eb->refs) == 2 && |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 5161 | test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) |
| 5162 | atomic_dec(&eb->refs); |
| 5163 | |
| 5164 | if (atomic_read(&eb->refs) == 2 && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5165 | test_bit(EXTENT_BUFFER_STALE, &eb->bflags) && |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5166 | !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5167 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 5168 | atomic_dec(&eb->refs); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5169 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5170 | /* |
| 5171 | * I know this is terrible, but it's temporary until we stop tracking |
| 5172 | * the uptodate bits and such for the extent buffers. |
| 5173 | */ |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5174 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5175 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5176 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5177 | void free_extent_buffer_stale(struct extent_buffer *eb) |
| 5178 | { |
| 5179 | if (!eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5180 | return; |
| 5181 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5182 | spin_lock(&eb->refs_lock); |
| 5183 | set_bit(EXTENT_BUFFER_STALE, &eb->bflags); |
| 5184 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5185 | if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5186 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 5187 | atomic_dec(&eb->refs); |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5188 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5189 | } |
| 5190 | |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 5191 | void clear_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5192 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5193 | unsigned long i; |
| 5194 | unsigned long num_pages; |
| 5195 | struct page *page; |
| 5196 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5197 | num_pages = num_extent_pages(eb->start, eb->len); |
| 5198 | |
| 5199 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5200 | page = eb->pages[i]; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5201 | if (!PageDirty(page)) |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 5202 | continue; |
| 5203 | |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 5204 | lock_page(page); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5205 | WARN_ON(!PagePrivate(page)); |
| 5206 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5207 | clear_page_dirty_for_io(page); |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 5208 | spin_lock_irq(&page->mapping->tree_lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5209 | if (!PageDirty(page)) { |
| 5210 | radix_tree_tag_clear(&page->mapping->page_tree, |
| 5211 | page_index(page), |
| 5212 | PAGECACHE_TAG_DIRTY); |
| 5213 | } |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 5214 | spin_unlock_irq(&page->mapping->tree_lock); |
Chris Mason | bf0da8c | 2011-11-04 12:29:37 -0400 | [diff] [blame] | 5215 | ClearPageError(page); |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 5216 | unlock_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5217 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5218 | WARN_ON(atomic_read(&eb->refs) == 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5219 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5220 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5221 | int set_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5222 | { |
| 5223 | unsigned long i; |
| 5224 | unsigned long num_pages; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5225 | int was_dirty = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5226 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5227 | check_buffer_tree_ref(eb); |
| 5228 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5229 | was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5230 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5231 | num_pages = num_extent_pages(eb->start, eb->len); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5232 | WARN_ON(atomic_read(&eb->refs) == 0); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5233 | WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)); |
| 5234 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5235 | for (i = 0; i < num_pages; i++) |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5236 | set_page_dirty(eb->pages[i]); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5237 | return was_dirty; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5238 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5239 | |
David Sterba | 69ba392 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 5240 | void clear_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5241 | { |
| 5242 | unsigned long i; |
| 5243 | struct page *page; |
| 5244 | unsigned long num_pages; |
| 5245 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5246 | clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5247 | num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5248 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5249 | page = eb->pages[i]; |
Chris Mason | 33958dc | 2008-07-30 10:29:12 -0400 | [diff] [blame] | 5250 | if (page) |
| 5251 | ClearPageUptodate(page); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5252 | } |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5253 | } |
| 5254 | |
David Sterba | 09c25a8 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 5255 | void set_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5256 | { |
| 5257 | unsigned long i; |
| 5258 | struct page *page; |
| 5259 | unsigned long num_pages; |
| 5260 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5261 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5262 | num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5263 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5264 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5265 | SetPageUptodate(page); |
| 5266 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5267 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5268 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5269 | int extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5270 | { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5271 | return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5272 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5273 | |
| 5274 | int read_extent_buffer_pages(struct extent_io_tree *tree, |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5275 | struct extent_buffer *eb, int wait, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5276 | get_extent_t *get_extent, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5277 | { |
| 5278 | unsigned long i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5279 | struct page *page; |
| 5280 | int err; |
| 5281 | int ret = 0; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5282 | int locked_pages = 0; |
| 5283 | int all_uptodate = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5284 | unsigned long num_pages; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5285 | unsigned long num_reads = 0; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5286 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5287 | unsigned long bio_flags = 0; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5288 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5289 | if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5290 | return 0; |
| 5291 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5292 | num_pages = num_extent_pages(eb->start, eb->len); |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5293 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5294 | page = eb->pages[i]; |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 5295 | if (wait == WAIT_NONE) { |
David Woodhouse | 2db0496 | 2008-08-07 11:19:43 -0400 | [diff] [blame] | 5296 | if (!trylock_page(page)) |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5297 | goto unlock_exit; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5298 | } else { |
| 5299 | lock_page(page); |
| 5300 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5301 | locked_pages++; |
Liu Bo | 2571e73 | 2016-08-03 12:33:01 -0700 | [diff] [blame] | 5302 | } |
| 5303 | /* |
| 5304 | * We need to firstly lock all pages to make sure that |
| 5305 | * the uptodate bit of our pages won't be affected by |
| 5306 | * clear_extent_buffer_uptodate(). |
| 5307 | */ |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5308 | for (i = 0; i < num_pages; i++) { |
Liu Bo | 2571e73 | 2016-08-03 12:33:01 -0700 | [diff] [blame] | 5309 | page = eb->pages[i]; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5310 | if (!PageUptodate(page)) { |
| 5311 | num_reads++; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5312 | all_uptodate = 0; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5313 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5314 | } |
Liu Bo | 2571e73 | 2016-08-03 12:33:01 -0700 | [diff] [blame] | 5315 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5316 | if (all_uptodate) { |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5317 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5318 | goto unlock_exit; |
| 5319 | } |
| 5320 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 5321 | clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 5322 | eb->read_mirror = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5323 | atomic_set(&eb->io_pages, num_reads); |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5324 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5325 | page = eb->pages[i]; |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5326 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5327 | if (!PageUptodate(page)) { |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5328 | if (ret) { |
| 5329 | atomic_dec(&eb->io_pages); |
| 5330 | unlock_page(page); |
| 5331 | continue; |
| 5332 | } |
| 5333 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5334 | ClearPageError(page); |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5335 | err = __extent_read_full_page(tree, page, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5336 | get_extent, &bio, |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 5337 | mirror_num, &bio_flags, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 5338 | REQ_META); |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5339 | if (err) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5340 | ret = err; |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5341 | /* |
| 5342 | * We use &bio in above __extent_read_full_page, |
| 5343 | * so we ensure that if it returns error, the |
| 5344 | * current page fails to add itself to bio and |
| 5345 | * it's been unlocked. |
| 5346 | * |
| 5347 | * We must dec io_pages by ourselves. |
| 5348 | */ |
| 5349 | atomic_dec(&eb->io_pages); |
| 5350 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5351 | } else { |
| 5352 | unlock_page(page); |
| 5353 | } |
| 5354 | } |
| 5355 | |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 5356 | if (bio) { |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 5357 | err = submit_one_bio(bio, mirror_num, bio_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5358 | if (err) |
| 5359 | return err; |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 5360 | } |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5361 | |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 5362 | if (ret || wait != WAIT_COMPLETE) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5363 | return ret; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5364 | |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5365 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5366 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5367 | wait_on_page_locked(page); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5368 | if (!PageUptodate(page)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5369 | ret = -EIO; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5370 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5371 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5372 | return ret; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5373 | |
| 5374 | unlock_exit: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5375 | while (locked_pages > 0) { |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5376 | locked_pages--; |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5377 | page = eb->pages[locked_pages]; |
| 5378 | unlock_page(page); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5379 | } |
| 5380 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5381 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5382 | |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 5383 | void read_extent_buffer(const struct extent_buffer *eb, void *dstv, |
| 5384 | unsigned long start, unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5385 | { |
| 5386 | size_t cur; |
| 5387 | size_t offset; |
| 5388 | struct page *page; |
| 5389 | char *kaddr; |
| 5390 | char *dst = (char *)dstv; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5391 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5392 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5393 | |
Liu Bo | f716abd | 2017-08-09 11:10:16 -0600 | [diff] [blame] | 5394 | if (start + len > eb->len) { |
| 5395 | WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n", |
| 5396 | eb->start, eb->len, start, len); |
| 5397 | memset(dst, 0, len); |
| 5398 | return; |
| 5399 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5400 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5401 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5402 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5403 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5404 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5405 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5406 | cur = min(len, (PAGE_SIZE - offset)); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5407 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5408 | memcpy(dst, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5409 | |
| 5410 | dst += cur; |
| 5411 | len -= cur; |
| 5412 | offset = 0; |
| 5413 | i++; |
| 5414 | } |
| 5415 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5416 | |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 5417 | int read_extent_buffer_to_user(const struct extent_buffer *eb, |
| 5418 | void __user *dstv, |
| 5419 | unsigned long start, unsigned long len) |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5420 | { |
| 5421 | size_t cur; |
| 5422 | size_t offset; |
| 5423 | struct page *page; |
| 5424 | char *kaddr; |
| 5425 | char __user *dst = (char __user *)dstv; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5426 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5427 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5428 | int ret = 0; |
| 5429 | |
| 5430 | WARN_ON(start > eb->len); |
| 5431 | WARN_ON(start + len > eb->start + eb->len); |
| 5432 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5433 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5434 | |
| 5435 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5436 | page = eb->pages[i]; |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5437 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5438 | cur = min(len, (PAGE_SIZE - offset)); |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5439 | kaddr = page_address(page); |
| 5440 | if (copy_to_user(dst, kaddr + offset, cur)) { |
| 5441 | ret = -EFAULT; |
| 5442 | break; |
| 5443 | } |
| 5444 | |
| 5445 | dst += cur; |
| 5446 | len -= cur; |
| 5447 | offset = 0; |
| 5448 | i++; |
| 5449 | } |
| 5450 | |
| 5451 | return ret; |
| 5452 | } |
| 5453 | |
Liu Bo | 415b35a | 2016-06-17 19:16:21 -0700 | [diff] [blame] | 5454 | /* |
| 5455 | * return 0 if the item is found within a page. |
| 5456 | * return 1 if the item spans two pages. |
| 5457 | * return -EINVAL otherwise. |
| 5458 | */ |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 5459 | int map_private_extent_buffer(const struct extent_buffer *eb, |
| 5460 | unsigned long start, unsigned long min_len, |
| 5461 | char **map, unsigned long *map_start, |
| 5462 | unsigned long *map_len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5463 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5464 | size_t offset = start & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5465 | char *kaddr; |
| 5466 | struct page *p; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5467 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5468 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5469 | unsigned long end_i = (start_offset + start + min_len - 1) >> |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5470 | PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5471 | |
Liu Bo | f716abd | 2017-08-09 11:10:16 -0600 | [diff] [blame] | 5472 | if (start + min_len > eb->len) { |
| 5473 | WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n", |
| 5474 | eb->start, eb->len, start, min_len); |
| 5475 | return -EINVAL; |
| 5476 | } |
| 5477 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5478 | if (i != end_i) |
Liu Bo | 415b35a | 2016-06-17 19:16:21 -0700 | [diff] [blame] | 5479 | return 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5480 | |
| 5481 | if (i == 0) { |
| 5482 | offset = start_offset; |
| 5483 | *map_start = 0; |
| 5484 | } else { |
| 5485 | offset = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5486 | *map_start = ((u64)i << PAGE_SHIFT) - start_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5487 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5488 | |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5489 | p = eb->pages[i]; |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5490 | kaddr = page_address(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5491 | *map = kaddr + offset; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5492 | *map_len = PAGE_SIZE - offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5493 | return 0; |
| 5494 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5495 | |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 5496 | int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv, |
| 5497 | unsigned long start, unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5498 | { |
| 5499 | size_t cur; |
| 5500 | size_t offset; |
| 5501 | struct page *page; |
| 5502 | char *kaddr; |
| 5503 | char *ptr = (char *)ptrv; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5504 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5505 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5506 | int ret = 0; |
| 5507 | |
| 5508 | WARN_ON(start > eb->len); |
| 5509 | WARN_ON(start + len > eb->start + eb->len); |
| 5510 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5511 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5512 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5513 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5514 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5515 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5516 | cur = min(len, (PAGE_SIZE - offset)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5517 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5518 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5519 | ret = memcmp(ptr, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5520 | if (ret) |
| 5521 | break; |
| 5522 | |
| 5523 | ptr += cur; |
| 5524 | len -= cur; |
| 5525 | offset = 0; |
| 5526 | i++; |
| 5527 | } |
| 5528 | return ret; |
| 5529 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5530 | |
David Sterba | f157bf7 | 2016-11-09 17:43:38 +0100 | [diff] [blame] | 5531 | void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb, |
| 5532 | const void *srcv) |
| 5533 | { |
| 5534 | char *kaddr; |
| 5535 | |
| 5536 | WARN_ON(!PageUptodate(eb->pages[0])); |
| 5537 | kaddr = page_address(eb->pages[0]); |
| 5538 | memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv, |
| 5539 | BTRFS_FSID_SIZE); |
| 5540 | } |
| 5541 | |
| 5542 | void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv) |
| 5543 | { |
| 5544 | char *kaddr; |
| 5545 | |
| 5546 | WARN_ON(!PageUptodate(eb->pages[0])); |
| 5547 | kaddr = page_address(eb->pages[0]); |
| 5548 | memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv, |
| 5549 | BTRFS_FSID_SIZE); |
| 5550 | } |
| 5551 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5552 | void write_extent_buffer(struct extent_buffer *eb, const void *srcv, |
| 5553 | unsigned long start, unsigned long len) |
| 5554 | { |
| 5555 | size_t cur; |
| 5556 | size_t offset; |
| 5557 | struct page *page; |
| 5558 | char *kaddr; |
| 5559 | char *src = (char *)srcv; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5560 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5561 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5562 | |
| 5563 | WARN_ON(start > eb->len); |
| 5564 | WARN_ON(start + len > eb->start + eb->len); |
| 5565 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5566 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5567 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5568 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5569 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5570 | WARN_ON(!PageUptodate(page)); |
| 5571 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5572 | cur = min(len, PAGE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5573 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5574 | memcpy(kaddr + offset, src, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5575 | |
| 5576 | src += cur; |
| 5577 | len -= cur; |
| 5578 | offset = 0; |
| 5579 | i++; |
| 5580 | } |
| 5581 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5582 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 5583 | void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start, |
| 5584 | unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5585 | { |
| 5586 | size_t cur; |
| 5587 | size_t offset; |
| 5588 | struct page *page; |
| 5589 | char *kaddr; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5590 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5591 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5592 | |
| 5593 | WARN_ON(start > eb->len); |
| 5594 | WARN_ON(start + len > eb->start + eb->len); |
| 5595 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5596 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5597 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5598 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5599 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5600 | WARN_ON(!PageUptodate(page)); |
| 5601 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5602 | cur = min(len, PAGE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5603 | kaddr = page_address(page); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 5604 | memset(kaddr + offset, 0, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5605 | |
| 5606 | len -= cur; |
| 5607 | offset = 0; |
| 5608 | i++; |
| 5609 | } |
| 5610 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5611 | |
David Sterba | 58e8012 | 2016-11-08 18:30:31 +0100 | [diff] [blame] | 5612 | void copy_extent_buffer_full(struct extent_buffer *dst, |
| 5613 | struct extent_buffer *src) |
| 5614 | { |
| 5615 | int i; |
| 5616 | unsigned num_pages; |
| 5617 | |
| 5618 | ASSERT(dst->len == src->len); |
| 5619 | |
| 5620 | num_pages = num_extent_pages(dst->start, dst->len); |
| 5621 | for (i = 0; i < num_pages; i++) |
| 5622 | copy_page(page_address(dst->pages[i]), |
| 5623 | page_address(src->pages[i])); |
| 5624 | } |
| 5625 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5626 | void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src, |
| 5627 | unsigned long dst_offset, unsigned long src_offset, |
| 5628 | unsigned long len) |
| 5629 | { |
| 5630 | u64 dst_len = dst->len; |
| 5631 | size_t cur; |
| 5632 | size_t offset; |
| 5633 | struct page *page; |
| 5634 | char *kaddr; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5635 | size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1); |
| 5636 | unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5637 | |
| 5638 | WARN_ON(src->len != dst_len); |
| 5639 | |
| 5640 | offset = (start_offset + dst_offset) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5641 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5642 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5643 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5644 | page = dst->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5645 | WARN_ON(!PageUptodate(page)); |
| 5646 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5647 | cur = min(len, (unsigned long)(PAGE_SIZE - offset)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5648 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5649 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5650 | read_extent_buffer(src, kaddr + offset, src_offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5651 | |
| 5652 | src_offset += cur; |
| 5653 | len -= cur; |
| 5654 | offset = 0; |
| 5655 | i++; |
| 5656 | } |
| 5657 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5658 | |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5659 | void le_bitmap_set(u8 *map, unsigned int start, int len) |
| 5660 | { |
| 5661 | u8 *p = map + BIT_BYTE(start); |
| 5662 | const unsigned int size = start + len; |
| 5663 | int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE); |
| 5664 | u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start); |
| 5665 | |
| 5666 | while (len - bits_to_set >= 0) { |
| 5667 | *p |= mask_to_set; |
| 5668 | len -= bits_to_set; |
| 5669 | bits_to_set = BITS_PER_BYTE; |
Dan Carpenter | 9c89469 | 2016-10-12 11:33:21 +0300 | [diff] [blame] | 5670 | mask_to_set = ~0; |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5671 | p++; |
| 5672 | } |
| 5673 | if (len) { |
| 5674 | mask_to_set &= BITMAP_LAST_BYTE_MASK(size); |
| 5675 | *p |= mask_to_set; |
| 5676 | } |
| 5677 | } |
| 5678 | |
| 5679 | void le_bitmap_clear(u8 *map, unsigned int start, int len) |
| 5680 | { |
| 5681 | u8 *p = map + BIT_BYTE(start); |
| 5682 | const unsigned int size = start + len; |
| 5683 | int bits_to_clear = BITS_PER_BYTE - (start % BITS_PER_BYTE); |
| 5684 | u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(start); |
| 5685 | |
| 5686 | while (len - bits_to_clear >= 0) { |
| 5687 | *p &= ~mask_to_clear; |
| 5688 | len -= bits_to_clear; |
| 5689 | bits_to_clear = BITS_PER_BYTE; |
Dan Carpenter | 9c89469 | 2016-10-12 11:33:21 +0300 | [diff] [blame] | 5690 | mask_to_clear = ~0; |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5691 | p++; |
| 5692 | } |
| 5693 | if (len) { |
| 5694 | mask_to_clear &= BITMAP_LAST_BYTE_MASK(size); |
| 5695 | *p &= ~mask_to_clear; |
| 5696 | } |
| 5697 | } |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5698 | |
| 5699 | /* |
| 5700 | * eb_bitmap_offset() - calculate the page and offset of the byte containing the |
| 5701 | * given bit number |
| 5702 | * @eb: the extent buffer |
| 5703 | * @start: offset of the bitmap item in the extent buffer |
| 5704 | * @nr: bit number |
| 5705 | * @page_index: return index of the page in the extent buffer that contains the |
| 5706 | * given bit number |
| 5707 | * @page_offset: return offset into the page given by page_index |
| 5708 | * |
| 5709 | * This helper hides the ugliness of finding the byte in an extent buffer which |
| 5710 | * contains a given bit. |
| 5711 | */ |
| 5712 | static inline void eb_bitmap_offset(struct extent_buffer *eb, |
| 5713 | unsigned long start, unsigned long nr, |
| 5714 | unsigned long *page_index, |
| 5715 | size_t *page_offset) |
| 5716 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5717 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5718 | size_t byte_offset = BIT_BYTE(nr); |
| 5719 | size_t offset; |
| 5720 | |
| 5721 | /* |
| 5722 | * The byte we want is the offset of the extent buffer + the offset of |
| 5723 | * the bitmap item in the extent buffer + the offset of the byte in the |
| 5724 | * bitmap item. |
| 5725 | */ |
| 5726 | offset = start_offset + start + byte_offset; |
| 5727 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5728 | *page_index = offset >> PAGE_SHIFT; |
| 5729 | *page_offset = offset & (PAGE_SIZE - 1); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5730 | } |
| 5731 | |
| 5732 | /** |
| 5733 | * extent_buffer_test_bit - determine whether a bit in a bitmap item is set |
| 5734 | * @eb: the extent buffer |
| 5735 | * @start: offset of the bitmap item in the extent buffer |
| 5736 | * @nr: bit number to test |
| 5737 | */ |
| 5738 | int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start, |
| 5739 | unsigned long nr) |
| 5740 | { |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5741 | u8 *kaddr; |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5742 | struct page *page; |
| 5743 | unsigned long i; |
| 5744 | size_t offset; |
| 5745 | |
| 5746 | eb_bitmap_offset(eb, start, nr, &i, &offset); |
| 5747 | page = eb->pages[i]; |
| 5748 | WARN_ON(!PageUptodate(page)); |
| 5749 | kaddr = page_address(page); |
| 5750 | return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1))); |
| 5751 | } |
| 5752 | |
| 5753 | /** |
| 5754 | * extent_buffer_bitmap_set - set an area of a bitmap |
| 5755 | * @eb: the extent buffer |
| 5756 | * @start: offset of the bitmap item in the extent buffer |
| 5757 | * @pos: bit number of the first bit |
| 5758 | * @len: number of bits to set |
| 5759 | */ |
| 5760 | void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start, |
| 5761 | unsigned long pos, unsigned long len) |
| 5762 | { |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5763 | u8 *kaddr; |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5764 | struct page *page; |
| 5765 | unsigned long i; |
| 5766 | size_t offset; |
| 5767 | const unsigned int size = pos + len; |
| 5768 | int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE); |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5769 | u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5770 | |
| 5771 | eb_bitmap_offset(eb, start, pos, &i, &offset); |
| 5772 | page = eb->pages[i]; |
| 5773 | WARN_ON(!PageUptodate(page)); |
| 5774 | kaddr = page_address(page); |
| 5775 | |
| 5776 | while (len >= bits_to_set) { |
| 5777 | kaddr[offset] |= mask_to_set; |
| 5778 | len -= bits_to_set; |
| 5779 | bits_to_set = BITS_PER_BYTE; |
Dan Carpenter | 9c89469 | 2016-10-12 11:33:21 +0300 | [diff] [blame] | 5780 | mask_to_set = ~0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5781 | if (++offset >= PAGE_SIZE && len > 0) { |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5782 | offset = 0; |
| 5783 | page = eb->pages[++i]; |
| 5784 | WARN_ON(!PageUptodate(page)); |
| 5785 | kaddr = page_address(page); |
| 5786 | } |
| 5787 | } |
| 5788 | if (len) { |
| 5789 | mask_to_set &= BITMAP_LAST_BYTE_MASK(size); |
| 5790 | kaddr[offset] |= mask_to_set; |
| 5791 | } |
| 5792 | } |
| 5793 | |
| 5794 | |
| 5795 | /** |
| 5796 | * extent_buffer_bitmap_clear - clear an area of a bitmap |
| 5797 | * @eb: the extent buffer |
| 5798 | * @start: offset of the bitmap item in the extent buffer |
| 5799 | * @pos: bit number of the first bit |
| 5800 | * @len: number of bits to clear |
| 5801 | */ |
| 5802 | void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start, |
| 5803 | unsigned long pos, unsigned long len) |
| 5804 | { |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5805 | u8 *kaddr; |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5806 | struct page *page; |
| 5807 | unsigned long i; |
| 5808 | size_t offset; |
| 5809 | const unsigned int size = pos + len; |
| 5810 | int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE); |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5811 | u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5812 | |
| 5813 | eb_bitmap_offset(eb, start, pos, &i, &offset); |
| 5814 | page = eb->pages[i]; |
| 5815 | WARN_ON(!PageUptodate(page)); |
| 5816 | kaddr = page_address(page); |
| 5817 | |
| 5818 | while (len >= bits_to_clear) { |
| 5819 | kaddr[offset] &= ~mask_to_clear; |
| 5820 | len -= bits_to_clear; |
| 5821 | bits_to_clear = BITS_PER_BYTE; |
Dan Carpenter | 9c89469 | 2016-10-12 11:33:21 +0300 | [diff] [blame] | 5822 | mask_to_clear = ~0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5823 | if (++offset >= PAGE_SIZE && len > 0) { |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5824 | offset = 0; |
| 5825 | page = eb->pages[++i]; |
| 5826 | WARN_ON(!PageUptodate(page)); |
| 5827 | kaddr = page_address(page); |
| 5828 | } |
| 5829 | } |
| 5830 | if (len) { |
| 5831 | mask_to_clear &= BITMAP_LAST_BYTE_MASK(size); |
| 5832 | kaddr[offset] &= ~mask_to_clear; |
| 5833 | } |
| 5834 | } |
| 5835 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5836 | static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len) |
| 5837 | { |
| 5838 | unsigned long distance = (src > dst) ? src - dst : dst - src; |
| 5839 | return distance < len; |
| 5840 | } |
| 5841 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5842 | static void copy_pages(struct page *dst_page, struct page *src_page, |
| 5843 | unsigned long dst_off, unsigned long src_off, |
| 5844 | unsigned long len) |
| 5845 | { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5846 | char *dst_kaddr = page_address(dst_page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5847 | char *src_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5848 | int must_memmove = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5849 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5850 | if (dst_page != src_page) { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5851 | src_kaddr = page_address(src_page); |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5852 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5853 | src_kaddr = dst_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5854 | if (areas_overlap(src_off, dst_off, len)) |
| 5855 | must_memmove = 1; |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5856 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5857 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5858 | if (must_memmove) |
| 5859 | memmove(dst_kaddr + dst_off, src_kaddr + src_off, len); |
| 5860 | else |
| 5861 | memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5862 | } |
| 5863 | |
| 5864 | void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 5865 | unsigned long src_offset, unsigned long len) |
| 5866 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5867 | struct btrfs_fs_info *fs_info = dst->fs_info; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5868 | size_t cur; |
| 5869 | size_t dst_off_in_page; |
| 5870 | size_t src_off_in_page; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5871 | size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5872 | unsigned long dst_i; |
| 5873 | unsigned long src_i; |
| 5874 | |
| 5875 | if (src_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5876 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5877 | "memmove bogus src_offset %lu move len %lu dst len %lu", |
| 5878 | src_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5879 | BUG_ON(1); |
| 5880 | } |
| 5881 | if (dst_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5882 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5883 | "memmove bogus dst_offset %lu move len %lu dst len %lu", |
| 5884 | dst_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5885 | BUG_ON(1); |
| 5886 | } |
| 5887 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5888 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5889 | dst_off_in_page = (start_offset + dst_offset) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5890 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5891 | src_off_in_page = (start_offset + src_offset) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5892 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5893 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5894 | dst_i = (start_offset + dst_offset) >> PAGE_SHIFT; |
| 5895 | src_i = (start_offset + src_offset) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5896 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5897 | cur = min(len, (unsigned long)(PAGE_SIZE - |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5898 | src_off_in_page)); |
| 5899 | cur = min_t(unsigned long, cur, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5900 | (unsigned long)(PAGE_SIZE - dst_off_in_page)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5901 | |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5902 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5903 | dst_off_in_page, src_off_in_page, cur); |
| 5904 | |
| 5905 | src_offset += cur; |
| 5906 | dst_offset += cur; |
| 5907 | len -= cur; |
| 5908 | } |
| 5909 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5910 | |
| 5911 | void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 5912 | unsigned long src_offset, unsigned long len) |
| 5913 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5914 | struct btrfs_fs_info *fs_info = dst->fs_info; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5915 | size_t cur; |
| 5916 | size_t dst_off_in_page; |
| 5917 | size_t src_off_in_page; |
| 5918 | unsigned long dst_end = dst_offset + len - 1; |
| 5919 | unsigned long src_end = src_offset + len - 1; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5920 | size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5921 | unsigned long dst_i; |
| 5922 | unsigned long src_i; |
| 5923 | |
| 5924 | if (src_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5925 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5926 | "memmove bogus src_offset %lu move len %lu len %lu", |
| 5927 | src_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5928 | BUG_ON(1); |
| 5929 | } |
| 5930 | if (dst_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5931 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5932 | "memmove bogus dst_offset %lu move len %lu len %lu", |
| 5933 | dst_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5934 | BUG_ON(1); |
| 5935 | } |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5936 | if (dst_offset < src_offset) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5937 | memcpy_extent_buffer(dst, dst_offset, src_offset, len); |
| 5938 | return; |
| 5939 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5940 | while (len > 0) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5941 | dst_i = (start_offset + dst_end) >> PAGE_SHIFT; |
| 5942 | src_i = (start_offset + src_end) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5943 | |
| 5944 | dst_off_in_page = (start_offset + dst_end) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5945 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5946 | src_off_in_page = (start_offset + src_end) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5947 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5948 | |
| 5949 | cur = min_t(unsigned long, len, src_off_in_page + 1); |
| 5950 | cur = min(cur, dst_off_in_page + 1); |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5951 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5952 | dst_off_in_page - cur + 1, |
| 5953 | src_off_in_page - cur + 1, cur); |
| 5954 | |
| 5955 | dst_end -= cur; |
| 5956 | src_end -= cur; |
| 5957 | len -= cur; |
| 5958 | } |
| 5959 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5960 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5961 | int try_release_extent_buffer(struct page *page) |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5962 | { |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5963 | struct extent_buffer *eb; |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5964 | |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5965 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5966 | * 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] | 5967 | * now. |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5968 | */ |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5969 | spin_lock(&page->mapping->private_lock); |
| 5970 | if (!PagePrivate(page)) { |
| 5971 | spin_unlock(&page->mapping->private_lock); |
| 5972 | return 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5973 | } |
| 5974 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5975 | eb = (struct extent_buffer *)page->private; |
| 5976 | BUG_ON(!eb); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5977 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5978 | /* |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5979 | * This is a little awful but should be ok, we need to make sure that |
| 5980 | * the eb doesn't disappear out from under us while we're looking at |
| 5981 | * this page. |
| 5982 | */ |
| 5983 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5984 | if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5985 | spin_unlock(&eb->refs_lock); |
| 5986 | spin_unlock(&page->mapping->private_lock); |
| 5987 | return 0; |
| 5988 | } |
| 5989 | spin_unlock(&page->mapping->private_lock); |
| 5990 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5991 | /* |
| 5992 | * If tree ref isn't set then we know the ref on this eb is a real ref, |
| 5993 | * so just return, this page will likely be freed soon anyway. |
| 5994 | */ |
| 5995 | if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { |
| 5996 | spin_unlock(&eb->refs_lock); |
| 5997 | return 0; |
| 5998 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5999 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 6000 | return release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 6001 | } |