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" |
Lu Fengqi | afce772 | 2016-06-13 09:36:46 +0800 | [diff] [blame] | 23 | #include "transaction.h" |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 24 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 25 | static struct kmem_cache *extent_state_cache; |
| 26 | static struct kmem_cache *extent_buffer_cache; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 27 | static struct bio_set *btrfs_bioset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 28 | |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 29 | static inline bool extent_state_in_tree(const struct extent_state *state) |
| 30 | { |
| 31 | return !RB_EMPTY_NODE(&state->rb_node); |
| 32 | } |
| 33 | |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 34 | #ifdef CONFIG_BTRFS_DEBUG |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 35 | static LIST_HEAD(buffers); |
| 36 | static LIST_HEAD(states); |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 37 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 38 | static DEFINE_SPINLOCK(leak_lock); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 39 | |
| 40 | static inline |
| 41 | void btrfs_leak_debug_add(struct list_head *new, struct list_head *head) |
| 42 | { |
| 43 | unsigned long flags; |
| 44 | |
| 45 | spin_lock_irqsave(&leak_lock, flags); |
| 46 | list_add(new, head); |
| 47 | spin_unlock_irqrestore(&leak_lock, flags); |
| 48 | } |
| 49 | |
| 50 | static inline |
| 51 | void btrfs_leak_debug_del(struct list_head *entry) |
| 52 | { |
| 53 | unsigned long flags; |
| 54 | |
| 55 | spin_lock_irqsave(&leak_lock, flags); |
| 56 | list_del(entry); |
| 57 | spin_unlock_irqrestore(&leak_lock, flags); |
| 58 | } |
| 59 | |
| 60 | static inline |
| 61 | void btrfs_leak_debug_check(void) |
| 62 | { |
| 63 | struct extent_state *state; |
| 64 | struct extent_buffer *eb; |
| 65 | |
| 66 | while (!list_empty(&states)) { |
| 67 | state = list_entry(states.next, struct extent_state, leak_list); |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 68 | 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] | 69 | state->start, state->end, state->state, |
| 70 | extent_state_in_tree(state), |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 71 | atomic_read(&state->refs)); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 72 | list_del(&state->leak_list); |
| 73 | kmem_cache_free(extent_state_cache, state); |
| 74 | } |
| 75 | |
| 76 | while (!list_empty(&buffers)) { |
| 77 | eb = list_entry(buffers.next, struct extent_buffer, leak_list); |
Jeff Mahoney | 62e8557 | 2016-09-20 10:05:01 -0400 | [diff] [blame] | 78 | pr_err("BTRFS: buffer leak start %llu len %lu refs %d\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 79 | eb->start, eb->len, atomic_read(&eb->refs)); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 80 | list_del(&eb->leak_list); |
| 81 | kmem_cache_free(extent_buffer_cache, eb); |
| 82 | } |
| 83 | } |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 84 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 85 | #define btrfs_debug_check_extent_io_range(tree, start, end) \ |
| 86 | __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end)) |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 87 | static inline void __btrfs_debug_check_extent_io_range(const char *caller, |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 88 | struct extent_io_tree *tree, u64 start, u64 end) |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 89 | { |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 90 | struct inode *inode; |
| 91 | u64 isize; |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 92 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 93 | if (!tree->mapping) |
| 94 | return; |
| 95 | |
| 96 | inode = tree->mapping->host; |
| 97 | isize = i_size_read(inode); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 98 | if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) { |
David Sterba | 9464732 | 2015-10-08 11:01:36 +0200 | [diff] [blame] | 99 | btrfs_debug_rl(BTRFS_I(inode)->root->fs_info, |
| 100 | "%s: ino %llu isize %llu odd range [%llu,%llu]", |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 101 | caller, btrfs_ino(BTRFS_I(inode)), isize, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 104 | #else |
| 105 | #define btrfs_leak_debug_add(new, head) do {} while (0) |
| 106 | #define btrfs_leak_debug_del(entry) do {} while (0) |
| 107 | #define btrfs_leak_debug_check() do {} while (0) |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 108 | #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] | 109 | #endif |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 110 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 111 | #define BUFFER_LRU_MAX 64 |
| 112 | |
| 113 | struct tree_entry { |
| 114 | u64 start; |
| 115 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 116 | struct rb_node rb_node; |
| 117 | }; |
| 118 | |
| 119 | struct extent_page_data { |
| 120 | struct bio *bio; |
| 121 | struct extent_io_tree *tree; |
| 122 | get_extent_t *get_extent; |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 123 | unsigned long bio_flags; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 124 | |
| 125 | /* tells writepage not to lock the state bits for this range |
| 126 | * it still does the unlocking |
| 127 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 128 | unsigned int extent_locked:1; |
| 129 | |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 130 | /* tells the submit_bio code to use REQ_SYNC */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 131 | unsigned int sync_io:1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 132 | }; |
| 133 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 134 | static void add_extent_changeset(struct extent_state *state, unsigned bits, |
| 135 | struct extent_changeset *changeset, |
| 136 | int set) |
| 137 | { |
| 138 | int ret; |
| 139 | |
| 140 | if (!changeset) |
| 141 | return; |
| 142 | if (set && (state->state & bits) == bits) |
| 143 | return; |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 144 | if (!set && (state->state & bits) == 0) |
| 145 | return; |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 146 | changeset->bytes_changed += state->end - state->start + 1; |
| 147 | ret = ulist_add(changeset->range_changed, state->start, state->end, |
| 148 | GFP_ATOMIC); |
| 149 | /* ENOMEM */ |
| 150 | BUG_ON(ret < 0); |
| 151 | } |
| 152 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 153 | static noinline void flush_write_bio(void *data); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 154 | static inline struct btrfs_fs_info * |
| 155 | tree_fs_info(struct extent_io_tree *tree) |
| 156 | { |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 157 | if (!tree->mapping) |
| 158 | return NULL; |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 159 | return btrfs_sb(tree->mapping->host->i_sb); |
| 160 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 161 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 162 | int __init extent_io_init(void) |
| 163 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 164 | extent_state_cache = kmem_cache_create("btrfs_extent_state", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 165 | sizeof(struct extent_state), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 166 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 167 | if (!extent_state_cache) |
| 168 | return -ENOMEM; |
| 169 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 170 | extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 171 | sizeof(struct extent_buffer), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 172 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 173 | if (!extent_buffer_cache) |
| 174 | goto free_state_cache; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 175 | |
| 176 | btrfs_bioset = bioset_create(BIO_POOL_SIZE, |
| 177 | offsetof(struct btrfs_io_bio, bio)); |
| 178 | if (!btrfs_bioset) |
| 179 | goto free_buffer_cache; |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 180 | |
| 181 | if (bioset_integrity_create(btrfs_bioset, BIO_POOL_SIZE)) |
| 182 | goto free_bioset; |
| 183 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 184 | return 0; |
| 185 | |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 186 | free_bioset: |
| 187 | bioset_free(btrfs_bioset); |
| 188 | btrfs_bioset = NULL; |
| 189 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 190 | free_buffer_cache: |
| 191 | kmem_cache_destroy(extent_buffer_cache); |
| 192 | extent_buffer_cache = NULL; |
| 193 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 194 | free_state_cache: |
| 195 | kmem_cache_destroy(extent_state_cache); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 196 | extent_state_cache = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 197 | return -ENOMEM; |
| 198 | } |
| 199 | |
| 200 | void extent_io_exit(void) |
| 201 | { |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 202 | btrfs_leak_debug_check(); |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * Make sure all delayed rcu free are flushed before we |
| 206 | * destroy caches. |
| 207 | */ |
| 208 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 209 | kmem_cache_destroy(extent_state_cache); |
| 210 | kmem_cache_destroy(extent_buffer_cache); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 211 | if (btrfs_bioset) |
| 212 | bioset_free(btrfs_bioset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void extent_io_tree_init(struct extent_io_tree *tree, |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 216 | struct address_space *mapping) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 217 | { |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 218 | tree->state = RB_ROOT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 219 | tree->ops = NULL; |
| 220 | tree->dirty_bytes = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 221 | spin_lock_init(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 222 | tree->mapping = mapping; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 223 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 224 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 225 | static struct extent_state *alloc_extent_state(gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 226 | { |
| 227 | struct extent_state *state; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 228 | |
Michal Hocko | 3ba7ab2 | 2017-01-09 15:39:02 +0100 | [diff] [blame] | 229 | /* |
| 230 | * The given mask might be not appropriate for the slab allocator, |
| 231 | * drop the unsupported bits |
| 232 | */ |
| 233 | mask &= ~(__GFP_DMA32|__GFP_HIGHMEM); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 234 | state = kmem_cache_alloc(extent_state_cache, mask); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 235 | if (!state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 236 | return state; |
| 237 | state->state = 0; |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 238 | state->failrec = NULL; |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 239 | RB_CLEAR_NODE(&state->rb_node); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 240 | btrfs_leak_debug_add(&state->leak_list, &states); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 241 | atomic_set(&state->refs, 1); |
| 242 | init_waitqueue_head(&state->wq); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 243 | trace_alloc_extent_state(state, mask, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 244 | return state; |
| 245 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 246 | |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 247 | void free_extent_state(struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 248 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 249 | if (!state) |
| 250 | return; |
| 251 | if (atomic_dec_and_test(&state->refs)) { |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 252 | WARN_ON(extent_state_in_tree(state)); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 253 | btrfs_leak_debug_del(&state->leak_list); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 254 | trace_free_extent_state(state, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 255 | kmem_cache_free(extent_state_cache, state); |
| 256 | } |
| 257 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 258 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 259 | static struct rb_node *tree_insert(struct rb_root *root, |
| 260 | struct rb_node *search_start, |
| 261 | u64 offset, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 262 | struct rb_node *node, |
| 263 | struct rb_node ***p_in, |
| 264 | struct rb_node **parent_in) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 265 | { |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 266 | struct rb_node **p; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 267 | struct rb_node *parent = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 268 | struct tree_entry *entry; |
| 269 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 270 | if (p_in && parent_in) { |
| 271 | p = *p_in; |
| 272 | parent = *parent_in; |
| 273 | goto do_insert; |
| 274 | } |
| 275 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 276 | p = search_start ? &search_start : &root->rb_node; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 277 | while (*p) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 278 | parent = *p; |
| 279 | entry = rb_entry(parent, struct tree_entry, rb_node); |
| 280 | |
| 281 | if (offset < entry->start) |
| 282 | p = &(*p)->rb_left; |
| 283 | else if (offset > entry->end) |
| 284 | p = &(*p)->rb_right; |
| 285 | else |
| 286 | return parent; |
| 287 | } |
| 288 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 289 | do_insert: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 290 | rb_link_node(node, parent, p); |
| 291 | rb_insert_color(node, root); |
| 292 | return NULL; |
| 293 | } |
| 294 | |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 295 | 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] | 296 | struct rb_node **prev_ret, |
| 297 | struct rb_node **next_ret, |
| 298 | struct rb_node ***p_ret, |
| 299 | struct rb_node **parent_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 300 | { |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 301 | struct rb_root *root = &tree->state; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 302 | struct rb_node **n = &root->rb_node; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 303 | struct rb_node *prev = NULL; |
| 304 | struct rb_node *orig_prev = NULL; |
| 305 | struct tree_entry *entry; |
| 306 | struct tree_entry *prev_entry = NULL; |
| 307 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 308 | while (*n) { |
| 309 | prev = *n; |
| 310 | entry = rb_entry(prev, struct tree_entry, rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 311 | prev_entry = entry; |
| 312 | |
| 313 | if (offset < entry->start) |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 314 | n = &(*n)->rb_left; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 315 | else if (offset > entry->end) |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 316 | n = &(*n)->rb_right; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 317 | else |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 318 | return *n; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 319 | } |
| 320 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 321 | if (p_ret) |
| 322 | *p_ret = n; |
| 323 | if (parent_ret) |
| 324 | *parent_ret = prev; |
| 325 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 326 | if (prev_ret) { |
| 327 | orig_prev = prev; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 328 | while (prev && offset > prev_entry->end) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 329 | prev = rb_next(prev); |
| 330 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| 331 | } |
| 332 | *prev_ret = prev; |
| 333 | prev = orig_prev; |
| 334 | } |
| 335 | |
| 336 | if (next_ret) { |
| 337 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 338 | while (prev && offset < prev_entry->start) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 339 | prev = rb_prev(prev); |
| 340 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| 341 | } |
| 342 | *next_ret = prev; |
| 343 | } |
| 344 | return NULL; |
| 345 | } |
| 346 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 347 | static inline struct rb_node * |
| 348 | tree_search_for_insert(struct extent_io_tree *tree, |
| 349 | u64 offset, |
| 350 | struct rb_node ***p_ret, |
| 351 | struct rb_node **parent_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 352 | { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 353 | struct rb_node *prev = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 354 | struct rb_node *ret; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 355 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 356 | ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 357 | if (!ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 358 | return prev; |
| 359 | return ret; |
| 360 | } |
| 361 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 362 | static inline struct rb_node *tree_search(struct extent_io_tree *tree, |
| 363 | u64 offset) |
| 364 | { |
| 365 | return tree_search_for_insert(tree, offset, NULL, NULL); |
| 366 | } |
| 367 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 368 | static void merge_cb(struct extent_io_tree *tree, struct extent_state *new, |
| 369 | struct extent_state *other) |
| 370 | { |
| 371 | if (tree->ops && tree->ops->merge_extent_hook) |
| 372 | tree->ops->merge_extent_hook(tree->mapping->host, new, |
| 373 | other); |
| 374 | } |
| 375 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 376 | /* |
| 377 | * utility function to look for merge candidates inside a given range. |
| 378 | * Any extents with matching state are merged together into a single |
| 379 | * extent in the tree. Extents with EXTENT_IO in their state field |
| 380 | * are not merged because the end_io handlers need to be able to do |
| 381 | * operations on them without sleeping (or doing allocations/splits). |
| 382 | * |
| 383 | * This should be called with the tree lock held. |
| 384 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 385 | static void merge_state(struct extent_io_tree *tree, |
| 386 | struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 387 | { |
| 388 | struct extent_state *other; |
| 389 | struct rb_node *other_node; |
| 390 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 391 | if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 392 | return; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 393 | |
| 394 | other_node = rb_prev(&state->rb_node); |
| 395 | if (other_node) { |
| 396 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 397 | if (other->end == state->start - 1 && |
| 398 | other->state == state->state) { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 399 | merge_cb(tree, state, other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 400 | state->start = other->start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [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); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 403 | free_extent_state(other); |
| 404 | } |
| 405 | } |
| 406 | other_node = rb_next(&state->rb_node); |
| 407 | if (other_node) { |
| 408 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 409 | if (other->start == state->end + 1 && |
| 410 | other->state == state->state) { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 411 | merge_cb(tree, state, other); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 412 | state->end = other->end; |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 413 | rb_erase(&other->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 414 | RB_CLEAR_NODE(&other->rb_node); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 415 | free_extent_state(other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 416 | } |
| 417 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 418 | } |
| 419 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 420 | static void set_state_cb(struct extent_io_tree *tree, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 421 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 422 | { |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 423 | if (tree->ops && tree->ops->set_bit_hook) |
| 424 | tree->ops->set_bit_hook(tree->mapping->host, state, bits); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static void clear_state_cb(struct extent_io_tree *tree, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 428 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 429 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 430 | if (tree->ops && tree->ops->clear_bit_hook) |
| 431 | tree->ops->clear_bit_hook(tree->mapping->host, state, bits); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 432 | } |
| 433 | |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 434 | static void set_state_bits(struct extent_io_tree *tree, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 435 | struct extent_state *state, unsigned *bits, |
| 436 | struct extent_changeset *changeset); |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 437 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 438 | /* |
| 439 | * insert an extent_state struct into the tree. 'bits' are set on the |
| 440 | * struct before it is inserted. |
| 441 | * |
| 442 | * This may return -EEXIST if the extent is already there, in which case the |
| 443 | * state struct is freed. |
| 444 | * |
| 445 | * The tree lock is not taken internally. This is a utility function and |
| 446 | * probably isn't what you want to call (see set/clear_extent_bit). |
| 447 | */ |
| 448 | static int insert_state(struct extent_io_tree *tree, |
| 449 | struct extent_state *state, u64 start, u64 end, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 450 | struct rb_node ***p, |
| 451 | struct rb_node **parent, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 452 | unsigned *bits, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 453 | { |
| 454 | struct rb_node *node; |
| 455 | |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 456 | if (end < start) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 457 | WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 458 | end, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 459 | state->start = start; |
| 460 | state->end = end; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 461 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 462 | set_state_bits(tree, state, bits, changeset); |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 463 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 464 | node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 465 | if (node) { |
| 466 | struct extent_state *found; |
| 467 | found = rb_entry(node, struct extent_state, rb_node); |
Jeff Mahoney | 62e8557 | 2016-09-20 10:05:01 -0400 | [diff] [blame] | 468 | 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] | 469 | found->start, found->end, start, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 470 | return -EEXIST; |
| 471 | } |
| 472 | merge_state(tree, state); |
| 473 | return 0; |
| 474 | } |
| 475 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 476 | 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] | 477 | u64 split) |
| 478 | { |
| 479 | if (tree->ops && tree->ops->split_extent_hook) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 480 | tree->ops->split_extent_hook(tree->mapping->host, orig, split); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 481 | } |
| 482 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 483 | /* |
| 484 | * split a given extent state struct in two, inserting the preallocated |
| 485 | * struct 'prealloc' as the newly created second half. 'split' indicates an |
| 486 | * offset inside 'orig' where it should be split. |
| 487 | * |
| 488 | * Before calling, |
| 489 | * the tree has 'orig' at [orig->start, orig->end]. After calling, there |
| 490 | * are two extent state structs in the tree: |
| 491 | * prealloc: [orig->start, split - 1] |
| 492 | * orig: [ split, orig->end ] |
| 493 | * |
| 494 | * The tree locks are not taken by this function. They need to be held |
| 495 | * by the caller. |
| 496 | */ |
| 497 | static int split_state(struct extent_io_tree *tree, struct extent_state *orig, |
| 498 | struct extent_state *prealloc, u64 split) |
| 499 | { |
| 500 | struct rb_node *node; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 501 | |
| 502 | split_cb(tree, orig, split); |
| 503 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 504 | prealloc->start = orig->start; |
| 505 | prealloc->end = split - 1; |
| 506 | prealloc->state = orig->state; |
| 507 | orig->start = split; |
| 508 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 509 | node = tree_insert(&tree->state, &orig->rb_node, prealloc->end, |
| 510 | &prealloc->rb_node, NULL, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 511 | if (node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 512 | free_extent_state(prealloc); |
| 513 | return -EEXIST; |
| 514 | } |
| 515 | return 0; |
| 516 | } |
| 517 | |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 518 | static struct extent_state *next_state(struct extent_state *state) |
| 519 | { |
| 520 | struct rb_node *next = rb_next(&state->rb_node); |
| 521 | if (next) |
| 522 | return rb_entry(next, struct extent_state, rb_node); |
| 523 | else |
| 524 | return NULL; |
| 525 | } |
| 526 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 527 | /* |
| 528 | * utility function to clear some bits in an extent state struct. |
Wang Sheng-Hui | 1b303fc | 2012-04-06 14:35:18 +0800 | [diff] [blame] | 529 | * 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] | 530 | * |
| 531 | * If no bits are set on the state struct after clearing things, the |
| 532 | * struct is freed and removed from the tree |
| 533 | */ |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 534 | static struct extent_state *clear_state_bit(struct extent_io_tree *tree, |
| 535 | struct extent_state *state, |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 536 | unsigned *bits, int wake, |
| 537 | struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 538 | { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 539 | struct extent_state *next; |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 540 | unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 541 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 542 | if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 543 | u64 range = state->end - state->start + 1; |
| 544 | WARN_ON(range > tree->dirty_bytes); |
| 545 | tree->dirty_bytes -= range; |
| 546 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 547 | clear_state_cb(tree, state, bits); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 548 | add_extent_changeset(state, bits_to_clear, changeset, 0); |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 549 | state->state &= ~bits_to_clear; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 550 | if (wake) |
| 551 | wake_up(&state->wq); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 552 | if (state->state == 0) { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 553 | next = next_state(state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 554 | if (extent_state_in_tree(state)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 555 | rb_erase(&state->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 556 | RB_CLEAR_NODE(&state->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 557 | free_extent_state(state); |
| 558 | } else { |
| 559 | WARN_ON(1); |
| 560 | } |
| 561 | } else { |
| 562 | merge_state(tree, state); |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 563 | next = next_state(state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 564 | } |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 565 | return next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 566 | } |
| 567 | |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 568 | static struct extent_state * |
| 569 | alloc_extent_state_atomic(struct extent_state *prealloc) |
| 570 | { |
| 571 | if (!prealloc) |
| 572 | prealloc = alloc_extent_state(GFP_ATOMIC); |
| 573 | |
| 574 | return prealloc; |
| 575 | } |
| 576 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 577 | 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] | 578 | { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 579 | btrfs_panic(tree_fs_info(tree), err, |
| 580 | "Locking error: Extent tree was modified by another thread while locked."); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 581 | } |
| 582 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 583 | /* |
| 584 | * clear some bits on a range in the tree. This may require splitting |
| 585 | * or inserting elements in the tree, so the gfp mask is used to |
| 586 | * indicate which allocations or sleeping are allowed. |
| 587 | * |
| 588 | * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove |
| 589 | * the given range from the tree regardless of state (ie for truncate). |
| 590 | * |
| 591 | * the range [start, end] is inclusive. |
| 592 | * |
Jeff Mahoney | 6763af8 | 2012-03-01 14:56:29 +0100 | [diff] [blame] | 593 | * 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] | 594 | */ |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 595 | static int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 596 | unsigned bits, int wake, int delete, |
| 597 | struct extent_state **cached_state, |
| 598 | gfp_t mask, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 599 | { |
| 600 | struct extent_state *state; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 601 | struct extent_state *cached; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 602 | struct extent_state *prealloc = NULL; |
| 603 | struct rb_node *node; |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 604 | u64 last_end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 605 | int err; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 606 | int clear = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 607 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 608 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 609 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 610 | if (bits & EXTENT_DELALLOC) |
| 611 | bits |= EXTENT_NORESERVE; |
| 612 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 613 | if (delete) |
| 614 | bits |= ~EXTENT_CTLBITS; |
| 615 | bits |= EXTENT_FIRST_DELALLOC; |
| 616 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 617 | if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY)) |
| 618 | clear = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 619 | again: |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 620 | if (!prealloc && gfpflags_allow_blocking(mask)) { |
Filipe Manana | c7bc631 | 2014-11-03 14:12:57 +0000 | [diff] [blame] | 621 | /* |
| 622 | * Don't care for allocation failure here because we might end |
| 623 | * up not needing the pre-allocated extent state at all, which |
| 624 | * is the case if we only have in the tree extent states that |
| 625 | * cover our input range and don't cover too any other range. |
| 626 | * If we end up needing a new extent state we allocate it later. |
| 627 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 628 | prealloc = alloc_extent_state(mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 629 | } |
| 630 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 631 | spin_lock(&tree->lock); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 632 | if (cached_state) { |
| 633 | cached = *cached_state; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 634 | |
| 635 | if (clear) { |
| 636 | *cached_state = NULL; |
| 637 | cached_state = NULL; |
| 638 | } |
| 639 | |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 640 | if (cached && extent_state_in_tree(cached) && |
| 641 | cached->start <= start && cached->end > start) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 642 | if (clear) |
| 643 | atomic_dec(&cached->refs); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 644 | state = cached; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 645 | goto hit_next; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 646 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 647 | if (clear) |
| 648 | free_extent_state(cached); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 649 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 650 | /* |
| 651 | * this search will find the extents that end after |
| 652 | * our range starts |
| 653 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 654 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 655 | if (!node) |
| 656 | goto out; |
| 657 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 658 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 659 | if (state->start > end) |
| 660 | goto out; |
| 661 | WARN_ON(state->end < start); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 662 | last_end = state->end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 663 | |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 664 | /* the state doesn't have the wanted bits, go ahead */ |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 665 | if (!(state->state & bits)) { |
| 666 | state = next_state(state); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 667 | goto next; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 668 | } |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 669 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 670 | /* |
| 671 | * | ---- desired range ---- | |
| 672 | * | state | or |
| 673 | * | ------------- state -------------- | |
| 674 | * |
| 675 | * We need to split the extent we found, and may flip |
| 676 | * bits on second half. |
| 677 | * |
| 678 | * If the extent we found extends past our range, we |
| 679 | * just split and search again. It'll get split again |
| 680 | * the next time though. |
| 681 | * |
| 682 | * If the extent we found is inside our range, we clear |
| 683 | * the desired bit on it. |
| 684 | */ |
| 685 | |
| 686 | if (state->start < start) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 687 | prealloc = alloc_extent_state_atomic(prealloc); |
| 688 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 689 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 690 | if (err) |
| 691 | extent_io_tree_panic(tree, err); |
| 692 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 693 | prealloc = NULL; |
| 694 | if (err) |
| 695 | goto out; |
| 696 | if (state->end <= end) { |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 697 | state = clear_state_bit(tree, state, &bits, wake, |
| 698 | changeset); |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 699 | goto next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 700 | } |
| 701 | goto search_again; |
| 702 | } |
| 703 | /* |
| 704 | * | ---- desired range ---- | |
| 705 | * | state | |
| 706 | * We need to split the extent, and clear the bit |
| 707 | * on the first half |
| 708 | */ |
| 709 | if (state->start <= end && state->end > end) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 710 | prealloc = alloc_extent_state_atomic(prealloc); |
| 711 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 712 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 713 | if (err) |
| 714 | extent_io_tree_panic(tree, err); |
| 715 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 716 | if (wake) |
| 717 | wake_up(&state->wq); |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 718 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 719 | clear_state_bit(tree, prealloc, &bits, wake, changeset); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 720 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 721 | prealloc = NULL; |
| 722 | goto out; |
| 723 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 724 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 725 | state = clear_state_bit(tree, state, &bits, wake, changeset); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 726 | next: |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 727 | if (last_end == (u64)-1) |
| 728 | goto out; |
| 729 | start = last_end + 1; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 730 | if (start <= end && state && !need_resched()) |
Liu Bo | 692e575 | 2012-02-16 18:34:36 +0800 | [diff] [blame] | 731 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 732 | |
| 733 | search_again: |
| 734 | if (start > end) |
| 735 | goto out; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 736 | spin_unlock(&tree->lock); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 737 | if (gfpflags_allow_blocking(mask)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 738 | cond_resched(); |
| 739 | goto again; |
David Sterba | 7ab5cb2 | 2016-04-27 01:02:15 +0200 | [diff] [blame] | 740 | |
| 741 | out: |
| 742 | spin_unlock(&tree->lock); |
| 743 | if (prealloc) |
| 744 | free_extent_state(prealloc); |
| 745 | |
| 746 | return 0; |
| 747 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 748 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 749 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 750 | static void wait_on_state(struct extent_io_tree *tree, |
| 751 | struct extent_state *state) |
Christoph Hellwig | 641f521 | 2008-12-02 06:36:10 -0500 | [diff] [blame] | 752 | __releases(tree->lock) |
| 753 | __acquires(tree->lock) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 754 | { |
| 755 | DEFINE_WAIT(wait); |
| 756 | prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 757 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 758 | schedule(); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 759 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 760 | finish_wait(&state->wq, &wait); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | /* |
| 764 | * waits for one or more bits to clear on a range in the state tree. |
| 765 | * The range [start, end] is inclusive. |
| 766 | * The tree lock is taken by this function |
| 767 | */ |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 768 | static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 769 | unsigned long bits) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 770 | { |
| 771 | struct extent_state *state; |
| 772 | struct rb_node *node; |
| 773 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 774 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 775 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 776 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 777 | again: |
| 778 | while (1) { |
| 779 | /* |
| 780 | * this search will find all the extents that end after |
| 781 | * our range starts |
| 782 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 783 | node = tree_search(tree, start); |
Filipe Manana | c50d3e7 | 2014-03-31 14:53:25 +0100 | [diff] [blame] | 784 | process_node: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 785 | if (!node) |
| 786 | break; |
| 787 | |
| 788 | state = rb_entry(node, struct extent_state, rb_node); |
| 789 | |
| 790 | if (state->start > end) |
| 791 | goto out; |
| 792 | |
| 793 | if (state->state & bits) { |
| 794 | start = state->start; |
| 795 | atomic_inc(&state->refs); |
| 796 | wait_on_state(tree, state); |
| 797 | free_extent_state(state); |
| 798 | goto again; |
| 799 | } |
| 800 | start = state->end + 1; |
| 801 | |
| 802 | if (start > end) |
| 803 | break; |
| 804 | |
Filipe Manana | c50d3e7 | 2014-03-31 14:53:25 +0100 | [diff] [blame] | 805 | if (!cond_resched_lock(&tree->lock)) { |
| 806 | node = rb_next(node); |
| 807 | goto process_node; |
| 808 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 809 | } |
| 810 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 811 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 812 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 813 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 814 | static void set_state_bits(struct extent_io_tree *tree, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 815 | struct extent_state *state, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 816 | unsigned *bits, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 817 | { |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 818 | unsigned bits_to_set = *bits & ~EXTENT_CTLBITS; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 819 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 820 | set_state_cb(tree, state, bits); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 821 | if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 822 | u64 range = state->end - state->start + 1; |
| 823 | tree->dirty_bytes += range; |
| 824 | } |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 825 | add_extent_changeset(state, bits_to_set, changeset, 1); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 826 | state->state |= bits_to_set; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 827 | } |
| 828 | |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 829 | static void cache_state_if_flags(struct extent_state *state, |
| 830 | struct extent_state **cached_ptr, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 831 | unsigned flags) |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 832 | { |
| 833 | if (cached_ptr && !(*cached_ptr)) { |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 834 | if (!flags || (state->state & flags)) { |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 835 | *cached_ptr = state; |
| 836 | atomic_inc(&state->refs); |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 841 | static void cache_state(struct extent_state *state, |
| 842 | struct extent_state **cached_ptr) |
| 843 | { |
| 844 | return cache_state_if_flags(state, cached_ptr, |
| 845 | EXTENT_IOBITS | EXTENT_BOUNDARY); |
| 846 | } |
| 847 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 848 | /* |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 849 | * set some bits on a range in the tree. This may require allocations or |
| 850 | * sleeping, so the gfp mask is used to indicate what is allowed. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 851 | * |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 852 | * If any of the exclusive bits are set, this will fail with -EEXIST if some |
| 853 | * part of the range already has the desired bits set. The start of the |
| 854 | * existing range is returned in failed_start in this case. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 855 | * |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 856 | * [start, end] is inclusive This takes the tree lock. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 857 | */ |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 858 | |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 859 | static int __must_check |
| 860 | __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 861 | unsigned bits, unsigned exclusive_bits, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 862 | u64 *failed_start, struct extent_state **cached_state, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 863 | gfp_t mask, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 864 | { |
| 865 | struct extent_state *state; |
| 866 | struct extent_state *prealloc = NULL; |
| 867 | struct rb_node *node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 868 | struct rb_node **p; |
| 869 | struct rb_node *parent; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 870 | int err = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 871 | u64 last_start; |
| 872 | u64 last_end; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 873 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 874 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 875 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 876 | bits |= EXTENT_FIRST_DELALLOC; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 877 | again: |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 878 | if (!prealloc && gfpflags_allow_blocking(mask)) { |
David Sterba | 059f791 | 2016-04-27 01:03:45 +0200 | [diff] [blame] | 879 | /* |
| 880 | * Don't care for allocation failure here because we might end |
| 881 | * up not needing the pre-allocated extent state at all, which |
| 882 | * is the case if we only have in the tree extent states that |
| 883 | * cover our input range and don't cover too any other range. |
| 884 | * If we end up needing a new extent state we allocate it later. |
| 885 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 886 | prealloc = alloc_extent_state(mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 887 | } |
| 888 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 889 | spin_lock(&tree->lock); |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 890 | if (cached_state && *cached_state) { |
| 891 | state = *cached_state; |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 892 | if (state->start <= start && state->end > start && |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 893 | extent_state_in_tree(state)) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 894 | node = &state->rb_node; |
| 895 | goto hit_next; |
| 896 | } |
| 897 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 898 | /* |
| 899 | * this search will find all the extents that end after |
| 900 | * our range starts. |
| 901 | */ |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 902 | node = tree_search_for_insert(tree, start, &p, &parent); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 903 | if (!node) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 904 | prealloc = alloc_extent_state_atomic(prealloc); |
| 905 | BUG_ON(!prealloc); |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 906 | err = insert_state(tree, prealloc, start, end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 907 | &p, &parent, &bits, changeset); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 908 | if (err) |
| 909 | extent_io_tree_panic(tree, err); |
| 910 | |
Filipe David Borba Manana | c42ac0b | 2013-11-26 15:01:34 +0000 | [diff] [blame] | 911 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 912 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 913 | goto out; |
| 914 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 915 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 40431d6 | 2009-08-05 12:57:59 -0400 | [diff] [blame] | 916 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 917 | last_start = state->start; |
| 918 | last_end = state->end; |
| 919 | |
| 920 | /* |
| 921 | * | ---- desired range ---- | |
| 922 | * | state | |
| 923 | * |
| 924 | * Just lock what we found and keep going |
| 925 | */ |
| 926 | if (state->start == start && state->end <= end) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 927 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 928 | *failed_start = state->start; |
| 929 | err = -EEXIST; |
| 930 | goto out; |
| 931 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 932 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 933 | set_state_bits(tree, state, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 934 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 935 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 936 | if (last_end == (u64)-1) |
| 937 | goto out; |
| 938 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 939 | state = next_state(state); |
| 940 | if (start < end && state && state->start == start && |
| 941 | !need_resched()) |
| 942 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 943 | goto search_again; |
| 944 | } |
| 945 | |
| 946 | /* |
| 947 | * | ---- desired range ---- | |
| 948 | * | state | |
| 949 | * or |
| 950 | * | ------------- state -------------- | |
| 951 | * |
| 952 | * We need to split the extent we found, and may flip bits on |
| 953 | * second half. |
| 954 | * |
| 955 | * If the extent we found extends past our |
| 956 | * range, we just split and search again. It'll get split |
| 957 | * again the next time though. |
| 958 | * |
| 959 | * If the extent we found is inside our range, we set the |
| 960 | * desired bit on it. |
| 961 | */ |
| 962 | if (state->start < start) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 963 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 964 | *failed_start = start; |
| 965 | err = -EEXIST; |
| 966 | goto out; |
| 967 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 968 | |
| 969 | prealloc = alloc_extent_state_atomic(prealloc); |
| 970 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 971 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 972 | if (err) |
| 973 | extent_io_tree_panic(tree, err); |
| 974 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 975 | prealloc = NULL; |
| 976 | if (err) |
| 977 | goto out; |
| 978 | if (state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 979 | set_state_bits(tree, state, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 980 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 981 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 982 | if (last_end == (u64)-1) |
| 983 | goto out; |
| 984 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 985 | state = next_state(state); |
| 986 | if (start < end && state && state->start == start && |
| 987 | !need_resched()) |
| 988 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 989 | } |
| 990 | goto search_again; |
| 991 | } |
| 992 | /* |
| 993 | * | ---- desired range ---- | |
| 994 | * | state | or | state | |
| 995 | * |
| 996 | * There's a hole, we need to insert something in it and |
| 997 | * ignore the extent we found. |
| 998 | */ |
| 999 | if (state->start > start) { |
| 1000 | u64 this_end; |
| 1001 | if (end < last_start) |
| 1002 | this_end = end; |
| 1003 | else |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1004 | this_end = last_start - 1; |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 1005 | |
| 1006 | prealloc = alloc_extent_state_atomic(prealloc); |
| 1007 | BUG_ON(!prealloc); |
Xiao Guangrong | c7f895a | 2011-04-20 06:45:49 +0000 | [diff] [blame] | 1008 | |
| 1009 | /* |
| 1010 | * Avoid to free 'prealloc' if it can be merged with |
| 1011 | * the later extent. |
| 1012 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1013 | err = insert_state(tree, prealloc, start, this_end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1014 | NULL, NULL, &bits, changeset); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1015 | if (err) |
| 1016 | extent_io_tree_panic(tree, err); |
| 1017 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1018 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1019 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1020 | start = this_end + 1; |
| 1021 | goto search_again; |
| 1022 | } |
| 1023 | /* |
| 1024 | * | ---- desired range ---- | |
| 1025 | * | state | |
| 1026 | * We need to split the extent, and set the bit |
| 1027 | * on the first half |
| 1028 | */ |
| 1029 | if (state->start <= end && state->end > end) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1030 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1031 | *failed_start = start; |
| 1032 | err = -EEXIST; |
| 1033 | goto out; |
| 1034 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 1035 | |
| 1036 | prealloc = alloc_extent_state_atomic(prealloc); |
| 1037 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1038 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1039 | if (err) |
| 1040 | extent_io_tree_panic(tree, err); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1041 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1042 | set_state_bits(tree, prealloc, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1043 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1044 | merge_state(tree, prealloc); |
| 1045 | prealloc = NULL; |
| 1046 | goto out; |
| 1047 | } |
| 1048 | |
David Sterba | b5a4ba1 | 2016-04-27 01:02:15 +0200 | [diff] [blame] | 1049 | search_again: |
| 1050 | if (start > end) |
| 1051 | goto out; |
| 1052 | spin_unlock(&tree->lock); |
| 1053 | if (gfpflags_allow_blocking(mask)) |
| 1054 | cond_resched(); |
| 1055 | goto again; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1056 | |
| 1057 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1058 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1059 | if (prealloc) |
| 1060 | free_extent_state(prealloc); |
| 1061 | |
| 1062 | return err; |
| 1063 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1064 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1065 | |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1066 | 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] | 1067 | unsigned bits, u64 * failed_start, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1068 | struct extent_state **cached_state, gfp_t mask) |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1069 | { |
| 1070 | return __set_extent_bit(tree, start, end, bits, 0, failed_start, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1071 | cached_state, mask, NULL); |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1075 | /** |
Liu Bo | 10983f2 | 2012-07-11 15:26:19 +0800 | [diff] [blame] | 1076 | * convert_extent_bit - convert all bits in a given range from one bit to |
| 1077 | * another |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1078 | * @tree: the io tree to search |
| 1079 | * @start: the start offset in bytes |
| 1080 | * @end: the end offset in bytes (inclusive) |
| 1081 | * @bits: the bits to set in this range |
| 1082 | * @clear_bits: the bits to clear in this range |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1083 | * @cached_state: state that we're going to cache |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1084 | * |
| 1085 | * This will go through and set bits for the given range. If any states exist |
| 1086 | * already in this range they are set with the given bit and cleared of the |
| 1087 | * clear_bits. This is only meant to be used by things that are mergeable, ie |
| 1088 | * converting from say DELALLOC to DIRTY. This is not meant to be used with |
| 1089 | * boundary bits like LOCK. |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1090 | * |
| 1091 | * All allocations are done with GFP_NOFS. |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1092 | */ |
| 1093 | 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] | 1094 | unsigned bits, unsigned clear_bits, |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1095 | struct extent_state **cached_state) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1096 | { |
| 1097 | struct extent_state *state; |
| 1098 | struct extent_state *prealloc = NULL; |
| 1099 | struct rb_node *node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1100 | struct rb_node **p; |
| 1101 | struct rb_node *parent; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1102 | int err = 0; |
| 1103 | u64 last_start; |
| 1104 | u64 last_end; |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1105 | bool first_iteration = true; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1106 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 1107 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 1108 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1109 | again: |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1110 | if (!prealloc) { |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1111 | /* |
| 1112 | * Best effort, don't worry if extent state allocation fails |
| 1113 | * here for the first iteration. We might have a cached state |
| 1114 | * that matches exactly the target range, in which case no |
| 1115 | * extent state allocations are needed. We'll only know this |
| 1116 | * after locking the tree. |
| 1117 | */ |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1118 | prealloc = alloc_extent_state(GFP_NOFS); |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1119 | if (!prealloc && !first_iteration) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1120 | return -ENOMEM; |
| 1121 | } |
| 1122 | |
| 1123 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1124 | if (cached_state && *cached_state) { |
| 1125 | state = *cached_state; |
| 1126 | if (state->start <= start && state->end > start && |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1127 | extent_state_in_tree(state)) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1128 | node = &state->rb_node; |
| 1129 | goto hit_next; |
| 1130 | } |
| 1131 | } |
| 1132 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1133 | /* |
| 1134 | * this search will find all the extents that end after |
| 1135 | * our range starts. |
| 1136 | */ |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1137 | node = tree_search_for_insert(tree, start, &p, &parent); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1138 | if (!node) { |
| 1139 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1140 | if (!prealloc) { |
| 1141 | err = -ENOMEM; |
| 1142 | goto out; |
| 1143 | } |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1144 | err = insert_state(tree, prealloc, start, end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1145 | &p, &parent, &bits, NULL); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1146 | if (err) |
| 1147 | extent_io_tree_panic(tree, err); |
Filipe David Borba Manana | c42ac0b | 2013-11-26 15:01:34 +0000 | [diff] [blame] | 1148 | cache_state(prealloc, cached_state); |
| 1149 | prealloc = NULL; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1150 | goto out; |
| 1151 | } |
| 1152 | state = rb_entry(node, struct extent_state, rb_node); |
| 1153 | hit_next: |
| 1154 | last_start = state->start; |
| 1155 | last_end = state->end; |
| 1156 | |
| 1157 | /* |
| 1158 | * | ---- desired range ---- | |
| 1159 | * | state | |
| 1160 | * |
| 1161 | * Just lock what we found and keep going |
| 1162 | */ |
| 1163 | if (state->start == start && state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1164 | set_state_bits(tree, state, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1165 | cache_state(state, cached_state); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1166 | state = clear_state_bit(tree, state, &clear_bits, 0, NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1167 | if (last_end == (u64)-1) |
| 1168 | goto out; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1169 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1170 | if (start < end && state && state->start == start && |
| 1171 | !need_resched()) |
| 1172 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1173 | goto search_again; |
| 1174 | } |
| 1175 | |
| 1176 | /* |
| 1177 | * | ---- desired range ---- | |
| 1178 | * | state | |
| 1179 | * or |
| 1180 | * | ------------- state -------------- | |
| 1181 | * |
| 1182 | * We need to split the extent we found, and may flip bits on |
| 1183 | * second half. |
| 1184 | * |
| 1185 | * If the extent we found extends past our |
| 1186 | * range, we just split and search again. It'll get split |
| 1187 | * again the next time though. |
| 1188 | * |
| 1189 | * If the extent we found is inside our range, we set the |
| 1190 | * desired bit on it. |
| 1191 | */ |
| 1192 | if (state->start < start) { |
| 1193 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1194 | if (!prealloc) { |
| 1195 | err = -ENOMEM; |
| 1196 | goto out; |
| 1197 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1198 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1199 | if (err) |
| 1200 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1201 | prealloc = NULL; |
| 1202 | if (err) |
| 1203 | goto out; |
| 1204 | if (state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1205 | set_state_bits(tree, state, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1206 | cache_state(state, cached_state); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1207 | state = clear_state_bit(tree, state, &clear_bits, 0, |
| 1208 | NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1209 | if (last_end == (u64)-1) |
| 1210 | goto out; |
| 1211 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1212 | if (start < end && state && state->start == start && |
| 1213 | !need_resched()) |
| 1214 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1215 | } |
| 1216 | goto search_again; |
| 1217 | } |
| 1218 | /* |
| 1219 | * | ---- desired range ---- | |
| 1220 | * | state | or | state | |
| 1221 | * |
| 1222 | * There's a hole, we need to insert something in it and |
| 1223 | * ignore the extent we found. |
| 1224 | */ |
| 1225 | if (state->start > start) { |
| 1226 | u64 this_end; |
| 1227 | if (end < last_start) |
| 1228 | this_end = end; |
| 1229 | else |
| 1230 | this_end = last_start - 1; |
| 1231 | |
| 1232 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1233 | if (!prealloc) { |
| 1234 | err = -ENOMEM; |
| 1235 | goto out; |
| 1236 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1237 | |
| 1238 | /* |
| 1239 | * Avoid to free 'prealloc' if it can be merged with |
| 1240 | * the later extent. |
| 1241 | */ |
| 1242 | err = insert_state(tree, prealloc, start, this_end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1243 | NULL, NULL, &bits, NULL); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1244 | if (err) |
| 1245 | extent_io_tree_panic(tree, err); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1246 | cache_state(prealloc, cached_state); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1247 | prealloc = NULL; |
| 1248 | start = this_end + 1; |
| 1249 | goto search_again; |
| 1250 | } |
| 1251 | /* |
| 1252 | * | ---- desired range ---- | |
| 1253 | * | state | |
| 1254 | * We need to split the extent, and set the bit |
| 1255 | * on the first half |
| 1256 | */ |
| 1257 | if (state->start <= end && state->end > end) { |
| 1258 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1259 | if (!prealloc) { |
| 1260 | err = -ENOMEM; |
| 1261 | goto out; |
| 1262 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1263 | |
| 1264 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1265 | if (err) |
| 1266 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1267 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1268 | set_state_bits(tree, prealloc, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1269 | cache_state(prealloc, cached_state); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1270 | clear_state_bit(tree, prealloc, &clear_bits, 0, NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1271 | prealloc = NULL; |
| 1272 | goto out; |
| 1273 | } |
| 1274 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1275 | search_again: |
| 1276 | if (start > end) |
| 1277 | goto out; |
| 1278 | spin_unlock(&tree->lock); |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1279 | cond_resched(); |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1280 | first_iteration = false; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1281 | goto again; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1282 | |
| 1283 | out: |
| 1284 | spin_unlock(&tree->lock); |
| 1285 | if (prealloc) |
| 1286 | free_extent_state(prealloc); |
| 1287 | |
| 1288 | return err; |
| 1289 | } |
| 1290 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1291 | /* wrappers around set/clear extent bit */ |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1292 | 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] | 1293 | unsigned bits, struct extent_changeset *changeset) |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1294 | { |
| 1295 | /* |
| 1296 | * We don't support EXTENT_LOCKED yet, as current changeset will |
| 1297 | * record any bits changed, so for EXTENT_LOCKED case, it will |
| 1298 | * either fail with -EEXIST or changeset will record the whole |
| 1299 | * range. |
| 1300 | */ |
| 1301 | BUG_ON(bits & EXTENT_LOCKED); |
| 1302 | |
David Sterba | 2c53b91 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1303 | 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] | 1304 | changeset); |
| 1305 | } |
| 1306 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1307 | int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 1308 | unsigned bits, int wake, int delete, |
| 1309 | struct extent_state **cached, gfp_t mask) |
| 1310 | { |
| 1311 | return __clear_extent_bit(tree, start, end, bits, wake, delete, |
| 1312 | cached, mask, NULL); |
| 1313 | } |
| 1314 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1315 | 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] | 1316 | unsigned bits, struct extent_changeset *changeset) |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1317 | { |
| 1318 | /* |
| 1319 | * Don't support EXTENT_LOCKED case, same reason as |
| 1320 | * set_record_extent_bits(). |
| 1321 | */ |
| 1322 | BUG_ON(bits & EXTENT_LOCKED); |
| 1323 | |
David Sterba | f734c44 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1324 | 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] | 1325 | changeset); |
| 1326 | } |
| 1327 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1328 | /* |
| 1329 | * either insert or lock state struct between start and end use mask to tell |
| 1330 | * us if waiting is desired. |
| 1331 | */ |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1332 | 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] | 1333 | struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1334 | { |
| 1335 | int err; |
| 1336 | u64 failed_start; |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1337 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1338 | while (1) { |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1339 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1340 | EXTENT_LOCKED, &failed_start, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1341 | cached_state, GFP_NOFS, NULL); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1342 | if (err == -EEXIST) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1343 | wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED); |
| 1344 | start = failed_start; |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1345 | } else |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1346 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1347 | WARN_ON(start > end); |
| 1348 | } |
| 1349 | return err; |
| 1350 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1351 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1352 | 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] | 1353 | { |
| 1354 | int err; |
| 1355 | u64 failed_start; |
| 1356 | |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1357 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1358 | &failed_start, NULL, GFP_NOFS, NULL); |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1359 | if (err == -EEXIST) { |
| 1360 | if (failed_start > start) |
| 1361 | clear_extent_bit(tree, start, failed_start - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1362 | EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1363 | return 0; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1364 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1365 | return 1; |
| 1366 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1367 | |
David Sterba | bd1fa4f | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 1368 | 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] | 1369 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1370 | unsigned long index = start >> PAGE_SHIFT; |
| 1371 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1372 | struct page *page; |
| 1373 | |
| 1374 | while (index <= end_index) { |
| 1375 | page = find_get_page(inode->i_mapping, index); |
| 1376 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
| 1377 | clear_page_dirty_for_io(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1378 | put_page(page); |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1379 | index++; |
| 1380 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1381 | } |
| 1382 | |
David Sterba | f631157 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 1383 | 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] | 1384 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1385 | unsigned long index = start >> PAGE_SHIFT; |
| 1386 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1387 | struct page *page; |
| 1388 | |
| 1389 | while (index <= end_index) { |
| 1390 | page = find_get_page(inode->i_mapping, index); |
| 1391 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1392 | __set_page_dirty_nobuffers(page); |
Konstantin Khebnikov | 8d38633 | 2015-02-11 15:26:55 -0800 | [diff] [blame] | 1393 | account_page_redirty(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1394 | put_page(page); |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1395 | index++; |
| 1396 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1397 | } |
| 1398 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1399 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1400 | * helper function to set both pages and extents in the tree writeback |
| 1401 | */ |
David Sterba | 35de6db | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 1402 | 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] | 1403 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1404 | unsigned long index = start >> PAGE_SHIFT; |
| 1405 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1406 | struct page *page; |
| 1407 | |
| 1408 | while (index <= end_index) { |
| 1409 | page = find_get_page(tree->mapping, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1410 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1411 | set_page_writeback(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1412 | put_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1413 | index++; |
| 1414 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1415 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1416 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1417 | /* find the first state struct with 'bits' set after 'start', and |
| 1418 | * return it. tree->lock must be held. NULL will returned if |
| 1419 | * nothing was found after 'start' |
| 1420 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1421 | static struct extent_state * |
| 1422 | find_first_extent_bit_state(struct extent_io_tree *tree, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1423 | u64 start, unsigned bits) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1424 | { |
| 1425 | struct rb_node *node; |
| 1426 | struct extent_state *state; |
| 1427 | |
| 1428 | /* |
| 1429 | * this search will find all the extents that end after |
| 1430 | * our range starts. |
| 1431 | */ |
| 1432 | node = tree_search(tree, start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1433 | if (!node) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1434 | goto out; |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1435 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1436 | while (1) { |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1437 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1438 | if (state->end >= start && (state->state & bits)) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1439 | return state; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1440 | |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1441 | node = rb_next(node); |
| 1442 | if (!node) |
| 1443 | break; |
| 1444 | } |
| 1445 | out: |
| 1446 | return NULL; |
| 1447 | } |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1448 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1449 | /* |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1450 | * find the first offset in the io tree with 'bits' set. zero is |
| 1451 | * returned if we find something, and *start_ret and *end_ret are |
| 1452 | * set to reflect the state struct that was found. |
| 1453 | * |
Wang Sheng-Hui | 477d7ea | 2012-04-06 14:35:47 +0800 | [diff] [blame] | 1454 | * If nothing was found, 1 is returned. If found something, return 0. |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1455 | */ |
| 1456 | int find_first_extent_bit(struct extent_io_tree *tree, u64 start, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1457 | u64 *start_ret, u64 *end_ret, unsigned bits, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1458 | struct extent_state **cached_state) |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1459 | { |
| 1460 | struct extent_state *state; |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1461 | struct rb_node *n; |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1462 | int ret = 1; |
| 1463 | |
| 1464 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1465 | if (cached_state && *cached_state) { |
| 1466 | state = *cached_state; |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1467 | if (state->end == start - 1 && extent_state_in_tree(state)) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1468 | n = rb_next(&state->rb_node); |
| 1469 | while (n) { |
| 1470 | state = rb_entry(n, struct extent_state, |
| 1471 | rb_node); |
| 1472 | if (state->state & bits) |
| 1473 | goto got_it; |
| 1474 | n = rb_next(n); |
| 1475 | } |
| 1476 | free_extent_state(*cached_state); |
| 1477 | *cached_state = NULL; |
| 1478 | goto out; |
| 1479 | } |
| 1480 | free_extent_state(*cached_state); |
| 1481 | *cached_state = NULL; |
| 1482 | } |
| 1483 | |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1484 | state = find_first_extent_bit_state(tree, start, bits); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1485 | got_it: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1486 | if (state) { |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 1487 | cache_state_if_flags(state, cached_state, 0); |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1488 | *start_ret = state->start; |
| 1489 | *end_ret = state->end; |
| 1490 | ret = 0; |
| 1491 | } |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1492 | out: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1493 | spin_unlock(&tree->lock); |
| 1494 | return ret; |
| 1495 | } |
| 1496 | |
| 1497 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1498 | * find a contiguous range of bytes in the file marked as delalloc, not |
| 1499 | * more than 'max_bytes'. start and end are used to return the range, |
| 1500 | * |
| 1501 | * 1 is returned if we find something, 0 if nothing was in the tree |
| 1502 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1503 | static noinline u64 find_delalloc_range(struct extent_io_tree *tree, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1504 | u64 *start, u64 *end, u64 max_bytes, |
| 1505 | struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1506 | { |
| 1507 | struct rb_node *node; |
| 1508 | struct extent_state *state; |
| 1509 | u64 cur_start = *start; |
| 1510 | u64 found = 0; |
| 1511 | u64 total_bytes = 0; |
| 1512 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1513 | spin_lock(&tree->lock); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1514 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1515 | /* |
| 1516 | * this search will find all the extents that end after |
| 1517 | * our range starts. |
| 1518 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1519 | node = tree_search(tree, cur_start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1520 | if (!node) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1521 | if (!found) |
| 1522 | *end = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1523 | goto out; |
| 1524 | } |
| 1525 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1526 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1527 | state = rb_entry(node, struct extent_state, rb_node); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1528 | if (found && (state->start != cur_start || |
| 1529 | (state->state & EXTENT_BOUNDARY))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1530 | goto out; |
| 1531 | } |
| 1532 | if (!(state->state & EXTENT_DELALLOC)) { |
| 1533 | if (!found) |
| 1534 | *end = state->end; |
| 1535 | goto out; |
| 1536 | } |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1537 | if (!found) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1538 | *start = state->start; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1539 | *cached_state = state; |
| 1540 | atomic_inc(&state->refs); |
| 1541 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1542 | found++; |
| 1543 | *end = state->end; |
| 1544 | cur_start = state->end + 1; |
| 1545 | node = rb_next(node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1546 | total_bytes += state->end - state->start + 1; |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 1547 | if (total_bytes >= max_bytes) |
Josef Bacik | 573aeca | 2013-08-30 14:38:49 -0400 | [diff] [blame] | 1548 | break; |
Josef Bacik | 573aeca | 2013-08-30 14:38:49 -0400 | [diff] [blame] | 1549 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1550 | break; |
| 1551 | } |
| 1552 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1553 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1554 | return found; |
| 1555 | } |
| 1556 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame^] | 1557 | static int __process_pages_contig(struct address_space *mapping, |
| 1558 | struct page *locked_page, |
| 1559 | pgoff_t start_index, pgoff_t end_index, |
| 1560 | unsigned long page_ops, pgoff_t *index_ret); |
| 1561 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1562 | static noinline void __unlock_for_delalloc(struct inode *inode, |
| 1563 | struct page *locked_page, |
| 1564 | u64 start, u64 end) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1565 | { |
| 1566 | int ret; |
| 1567 | struct page *pages[16]; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1568 | unsigned long index = start >> PAGE_SHIFT; |
| 1569 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1570 | unsigned long nr_pages = end_index - index + 1; |
| 1571 | int i; |
| 1572 | |
| 1573 | if (index == locked_page->index && end_index == index) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1574 | return; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1575 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1576 | while (nr_pages > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1577 | ret = find_get_pages_contig(inode->i_mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1578 | min_t(unsigned long, nr_pages, |
| 1579 | ARRAY_SIZE(pages)), pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1580 | for (i = 0; i < ret; i++) { |
| 1581 | if (pages[i] != locked_page) |
| 1582 | unlock_page(pages[i]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1583 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1584 | } |
| 1585 | nr_pages -= ret; |
| 1586 | index += ret; |
| 1587 | cond_resched(); |
| 1588 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | static noinline int lock_delalloc_pages(struct inode *inode, |
| 1592 | struct page *locked_page, |
| 1593 | u64 delalloc_start, |
| 1594 | u64 delalloc_end) |
| 1595 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1596 | unsigned long index = delalloc_start >> PAGE_SHIFT; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1597 | unsigned long start_index = index; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1598 | unsigned long end_index = delalloc_end >> PAGE_SHIFT; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1599 | unsigned long pages_locked = 0; |
| 1600 | struct page *pages[16]; |
| 1601 | unsigned long nrpages; |
| 1602 | int ret; |
| 1603 | int i; |
| 1604 | |
| 1605 | /* the caller is responsible for locking the start index */ |
| 1606 | if (index == locked_page->index && index == end_index) |
| 1607 | return 0; |
| 1608 | |
| 1609 | /* skip the page at the start index */ |
| 1610 | nrpages = end_index - index + 1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1611 | while (nrpages > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1612 | ret = find_get_pages_contig(inode->i_mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1613 | min_t(unsigned long, |
| 1614 | nrpages, ARRAY_SIZE(pages)), pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1615 | if (ret == 0) { |
| 1616 | ret = -EAGAIN; |
| 1617 | goto done; |
| 1618 | } |
| 1619 | /* now we have an array of pages, lock them all */ |
| 1620 | for (i = 0; i < ret; i++) { |
| 1621 | /* |
| 1622 | * the caller is taking responsibility for |
| 1623 | * locked_page |
| 1624 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1625 | if (pages[i] != locked_page) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1626 | lock_page(pages[i]); |
Chris Mason | f2b1c41 | 2008-11-10 07:31:30 -0500 | [diff] [blame] | 1627 | if (!PageDirty(pages[i]) || |
| 1628 | pages[i]->mapping != inode->i_mapping) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1629 | ret = -EAGAIN; |
| 1630 | unlock_page(pages[i]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1631 | put_page(pages[i]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1632 | goto done; |
| 1633 | } |
| 1634 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1635 | put_page(pages[i]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1636 | pages_locked++; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1637 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1638 | nrpages -= ret; |
| 1639 | index += ret; |
| 1640 | cond_resched(); |
| 1641 | } |
| 1642 | ret = 0; |
| 1643 | done: |
| 1644 | if (ret && pages_locked) { |
| 1645 | __unlock_for_delalloc(inode, locked_page, |
| 1646 | delalloc_start, |
| 1647 | ((u64)(start_index + pages_locked - 1)) << |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1648 | PAGE_SHIFT); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1649 | } |
| 1650 | return ret; |
| 1651 | } |
| 1652 | |
| 1653 | /* |
| 1654 | * find a contiguous range of bytes in the file marked as delalloc, not |
| 1655 | * more than 'max_bytes'. start and end are used to return the range, |
| 1656 | * |
| 1657 | * 1 is returned if we find something, 0 if nothing was in the tree |
| 1658 | */ |
Josef Bacik | 294e30f | 2013-10-09 12:00:56 -0400 | [diff] [blame] | 1659 | STATIC u64 find_lock_delalloc_range(struct inode *inode, |
| 1660 | struct extent_io_tree *tree, |
| 1661 | struct page *locked_page, u64 *start, |
| 1662 | u64 *end, u64 max_bytes) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1663 | { |
| 1664 | u64 delalloc_start; |
| 1665 | u64 delalloc_end; |
| 1666 | u64 found; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1667 | struct extent_state *cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1668 | int ret; |
| 1669 | int loops = 0; |
| 1670 | |
| 1671 | again: |
| 1672 | /* step one, find a bunch of delalloc bytes starting at start */ |
| 1673 | delalloc_start = *start; |
| 1674 | delalloc_end = 0; |
| 1675 | found = find_delalloc_range(tree, &delalloc_start, &delalloc_end, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1676 | max_bytes, &cached_state); |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1677 | if (!found || delalloc_end <= *start) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1678 | *start = delalloc_start; |
| 1679 | *end = delalloc_end; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1680 | free_extent_state(cached_state); |
Liu Bo | 385fe0b | 2013-10-01 23:49:49 +0800 | [diff] [blame] | 1681 | return 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | /* |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1685 | * start comes from the offset of locked_page. We have to lock |
| 1686 | * pages in order, so we can't process delalloc bytes before |
| 1687 | * locked_page |
| 1688 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1689 | if (delalloc_start < *start) |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1690 | delalloc_start = *start; |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1691 | |
| 1692 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1693 | * 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] | 1694 | */ |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 1695 | if (delalloc_end + 1 - delalloc_start > max_bytes) |
| 1696 | delalloc_end = delalloc_start + max_bytes - 1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1697 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1698 | /* step two, lock all the pages after the page that has start */ |
| 1699 | ret = lock_delalloc_pages(inode, locked_page, |
| 1700 | delalloc_start, delalloc_end); |
| 1701 | if (ret == -EAGAIN) { |
| 1702 | /* some of the pages are gone, lets avoid looping by |
| 1703 | * shortening the size of the delalloc range we're searching |
| 1704 | */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1705 | free_extent_state(cached_state); |
Chris Mason | 7d78874 | 2014-05-21 05:49:54 -0700 | [diff] [blame] | 1706 | cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1707 | if (!loops) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1708 | max_bytes = PAGE_SIZE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1709 | loops = 1; |
| 1710 | goto again; |
| 1711 | } else { |
| 1712 | found = 0; |
| 1713 | goto out_failed; |
| 1714 | } |
| 1715 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1716 | BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1717 | |
| 1718 | /* step three, lock the state bits for the whole range */ |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1719 | lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1720 | |
| 1721 | /* then test to make sure it is all still delalloc */ |
| 1722 | ret = test_range_bit(tree, delalloc_start, delalloc_end, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1723 | EXTENT_DELALLOC, 1, cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1724 | if (!ret) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1725 | unlock_extent_cached(tree, delalloc_start, delalloc_end, |
| 1726 | &cached_state, GFP_NOFS); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1727 | __unlock_for_delalloc(inode, locked_page, |
| 1728 | delalloc_start, delalloc_end); |
| 1729 | cond_resched(); |
| 1730 | goto again; |
| 1731 | } |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1732 | free_extent_state(cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1733 | *start = delalloc_start; |
| 1734 | *end = delalloc_end; |
| 1735 | out_failed: |
| 1736 | return found; |
| 1737 | } |
| 1738 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame^] | 1739 | static int __process_pages_contig(struct address_space *mapping, |
| 1740 | struct page *locked_page, |
| 1741 | pgoff_t start_index, pgoff_t end_index, |
| 1742 | unsigned long page_ops, pgoff_t *index_ret) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1743 | { |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1744 | unsigned long nr_pages = end_index - start_index + 1; |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame^] | 1745 | unsigned long pages_locked = 0; |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1746 | pgoff_t index = start_index; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1747 | struct page *pages[16]; |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1748 | unsigned ret; |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame^] | 1749 | int err = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1750 | int i; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1751 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame^] | 1752 | if (page_ops & PAGE_LOCK) { |
| 1753 | ASSERT(page_ops == PAGE_LOCK); |
| 1754 | ASSERT(index_ret && *index_ret == start_index); |
| 1755 | } |
| 1756 | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1757 | if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0) |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1758 | mapping_set_error(mapping, -EIO); |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1759 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1760 | while (nr_pages > 0) { |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1761 | ret = find_get_pages_contig(mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1762 | min_t(unsigned long, |
| 1763 | nr_pages, ARRAY_SIZE(pages)), pages); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame^] | 1764 | if (ret == 0) { |
| 1765 | /* |
| 1766 | * Only if we're going to lock these pages, |
| 1767 | * can we find nothing at @index. |
| 1768 | */ |
| 1769 | ASSERT(page_ops & PAGE_LOCK); |
| 1770 | return ret; |
| 1771 | } |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1772 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame^] | 1773 | for (i = 0; i < ret; i++) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1774 | if (page_ops & PAGE_SET_PRIVATE2) |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1775 | SetPagePrivate2(pages[i]); |
| 1776 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1777 | if (pages[i] == locked_page) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1778 | put_page(pages[i]); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame^] | 1779 | pages_locked++; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1780 | continue; |
| 1781 | } |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1782 | if (page_ops & PAGE_CLEAR_DIRTY) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1783 | clear_page_dirty_for_io(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1784 | if (page_ops & PAGE_SET_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1785 | set_page_writeback(pages[i]); |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1786 | if (page_ops & PAGE_SET_ERROR) |
| 1787 | SetPageError(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1788 | if (page_ops & PAGE_END_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1789 | end_page_writeback(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1790 | if (page_ops & PAGE_UNLOCK) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1791 | unlock_page(pages[i]); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame^] | 1792 | if (page_ops & PAGE_LOCK) { |
| 1793 | lock_page(pages[i]); |
| 1794 | if (!PageDirty(pages[i]) || |
| 1795 | pages[i]->mapping != mapping) { |
| 1796 | unlock_page(pages[i]); |
| 1797 | put_page(pages[i]); |
| 1798 | err = -EAGAIN; |
| 1799 | goto out; |
| 1800 | } |
| 1801 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1802 | put_page(pages[i]); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame^] | 1803 | pages_locked++; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1804 | } |
| 1805 | nr_pages -= ret; |
| 1806 | index += ret; |
| 1807 | cond_resched(); |
| 1808 | } |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame^] | 1809 | out: |
| 1810 | if (err && index_ret) |
| 1811 | *index_ret = start_index + pages_locked - 1; |
| 1812 | return err; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1813 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1814 | |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1815 | void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end, |
| 1816 | u64 delalloc_end, struct page *locked_page, |
| 1817 | unsigned clear_bits, |
| 1818 | unsigned long page_ops) |
| 1819 | { |
| 1820 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0, |
| 1821 | NULL, GFP_NOFS); |
| 1822 | |
| 1823 | __process_pages_contig(inode->i_mapping, locked_page, |
| 1824 | start >> PAGE_SHIFT, end >> PAGE_SHIFT, |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame^] | 1825 | page_ops, NULL); |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1826 | } |
| 1827 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1828 | /* |
| 1829 | * count the number of bytes in the tree that have a given bit(s) |
| 1830 | * set. This can be fairly slow, except for EXTENT_DIRTY which is |
| 1831 | * cached. The total number found is returned. |
| 1832 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1833 | u64 count_range_bits(struct extent_io_tree *tree, |
| 1834 | u64 *start, u64 search_end, u64 max_bytes, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1835 | unsigned bits, int contig) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1836 | { |
| 1837 | struct rb_node *node; |
| 1838 | struct extent_state *state; |
| 1839 | u64 cur_start = *start; |
| 1840 | u64 total_bytes = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1841 | u64 last = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1842 | int found = 0; |
| 1843 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 1844 | if (WARN_ON(search_end <= cur_start)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1845 | return 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1846 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1847 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1848 | if (cur_start == 0 && bits == EXTENT_DIRTY) { |
| 1849 | total_bytes = tree->dirty_bytes; |
| 1850 | goto out; |
| 1851 | } |
| 1852 | /* |
| 1853 | * this search will find all the extents that end after |
| 1854 | * our range starts. |
| 1855 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1856 | node = tree_search(tree, cur_start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1857 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1858 | goto out; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1859 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1860 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1861 | state = rb_entry(node, struct extent_state, rb_node); |
| 1862 | if (state->start > search_end) |
| 1863 | break; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1864 | if (contig && found && state->start > last + 1) |
| 1865 | break; |
| 1866 | if (state->end >= cur_start && (state->state & bits) == bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1867 | total_bytes += min(search_end, state->end) + 1 - |
| 1868 | max(cur_start, state->start); |
| 1869 | if (total_bytes >= max_bytes) |
| 1870 | break; |
| 1871 | if (!found) { |
Josef Bacik | af60bed | 2011-05-04 11:11:17 -0400 | [diff] [blame] | 1872 | *start = max(cur_start, state->start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1873 | found = 1; |
| 1874 | } |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1875 | last = state->end; |
| 1876 | } else if (contig && found) { |
| 1877 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1878 | } |
| 1879 | node = rb_next(node); |
| 1880 | if (!node) |
| 1881 | break; |
| 1882 | } |
| 1883 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1884 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1885 | return total_bytes; |
| 1886 | } |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1887 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1888 | /* |
| 1889 | * set the private field for a given byte offset in the tree. If there isn't |
| 1890 | * an extent_state there already, this does nothing. |
| 1891 | */ |
Arnd Bergmann | f827ba9 | 2016-02-22 22:53:20 +0100 | [diff] [blame] | 1892 | 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] | 1893 | struct io_failure_record *failrec) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1894 | { |
| 1895 | struct rb_node *node; |
| 1896 | struct extent_state *state; |
| 1897 | int ret = 0; |
| 1898 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1899 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1900 | /* |
| 1901 | * this search will find all the extents that end after |
| 1902 | * our range starts. |
| 1903 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1904 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1905 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1906 | ret = -ENOENT; |
| 1907 | goto out; |
| 1908 | } |
| 1909 | state = rb_entry(node, struct extent_state, rb_node); |
| 1910 | if (state->start != start) { |
| 1911 | ret = -ENOENT; |
| 1912 | goto out; |
| 1913 | } |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 1914 | state->failrec = failrec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1915 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1916 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1917 | return ret; |
| 1918 | } |
| 1919 | |
Arnd Bergmann | f827ba9 | 2016-02-22 22:53:20 +0100 | [diff] [blame] | 1920 | 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] | 1921 | struct io_failure_record **failrec) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1922 | { |
| 1923 | struct rb_node *node; |
| 1924 | struct extent_state *state; |
| 1925 | int ret = 0; |
| 1926 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1927 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1928 | /* |
| 1929 | * this search will find all the extents that end after |
| 1930 | * our range starts. |
| 1931 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1932 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1933 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1934 | ret = -ENOENT; |
| 1935 | goto out; |
| 1936 | } |
| 1937 | state = rb_entry(node, struct extent_state, rb_node); |
| 1938 | if (state->start != start) { |
| 1939 | ret = -ENOENT; |
| 1940 | goto out; |
| 1941 | } |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 1942 | *failrec = state->failrec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1943 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1944 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1945 | return ret; |
| 1946 | } |
| 1947 | |
| 1948 | /* |
| 1949 | * searches a range in the state tree for a given mask. |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1950 | * 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] | 1951 | * has the bits set. Otherwise, 1 is returned if any bit in the |
| 1952 | * range is found set. |
| 1953 | */ |
| 1954 | 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] | 1955 | unsigned bits, int filled, struct extent_state *cached) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1956 | { |
| 1957 | struct extent_state *state = NULL; |
| 1958 | struct rb_node *node; |
| 1959 | int bitset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1960 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1961 | spin_lock(&tree->lock); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1962 | if (cached && extent_state_in_tree(cached) && cached->start <= start && |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 1963 | cached->end > start) |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1964 | node = &cached->rb_node; |
| 1965 | else |
| 1966 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1967 | while (node && start <= end) { |
| 1968 | state = rb_entry(node, struct extent_state, rb_node); |
| 1969 | |
| 1970 | if (filled && state->start > start) { |
| 1971 | bitset = 0; |
| 1972 | break; |
| 1973 | } |
| 1974 | |
| 1975 | if (state->start > end) |
| 1976 | break; |
| 1977 | |
| 1978 | if (state->state & bits) { |
| 1979 | bitset = 1; |
| 1980 | if (!filled) |
| 1981 | break; |
| 1982 | } else if (filled) { |
| 1983 | bitset = 0; |
| 1984 | break; |
| 1985 | } |
Chris Mason | 46562ce | 2009-09-23 20:23:16 -0400 | [diff] [blame] | 1986 | |
| 1987 | if (state->end == (u64)-1) |
| 1988 | break; |
| 1989 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1990 | start = state->end + 1; |
| 1991 | if (start > end) |
| 1992 | break; |
| 1993 | node = rb_next(node); |
| 1994 | if (!node) { |
| 1995 | if (filled) |
| 1996 | bitset = 0; |
| 1997 | break; |
| 1998 | } |
| 1999 | } |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2000 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2001 | return bitset; |
| 2002 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2003 | |
| 2004 | /* |
| 2005 | * helper function to set a given page up to date if all the |
| 2006 | * extents in the tree for that page are up to date |
| 2007 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2008 | 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] | 2009 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2010 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2011 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2012 | if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2013 | SetPageUptodate(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2014 | } |
| 2015 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2016 | int free_io_failure(struct inode *inode, struct io_failure_record *rec) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2017 | { |
| 2018 | int ret; |
| 2019 | int err = 0; |
| 2020 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 2021 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2022 | set_state_failrec(failure_tree, rec->start, NULL); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2023 | ret = clear_extent_bits(failure_tree, rec->start, |
| 2024 | rec->start + rec->len - 1, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2025 | EXTENT_LOCKED | EXTENT_DIRTY); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2026 | if (ret) |
| 2027 | err = ret; |
| 2028 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2029 | ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start, |
| 2030 | rec->start + rec->len - 1, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2031 | EXTENT_DAMAGED); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2032 | if (ret && !err) |
| 2033 | err = ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2034 | |
| 2035 | kfree(rec); |
| 2036 | return err; |
| 2037 | } |
| 2038 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2039 | /* |
| 2040 | * this bypasses the standard btrfs submit functions deliberately, as |
| 2041 | * the standard behavior is to write all copies in a raid setup. here we only |
| 2042 | * want to write the one bad copy. so we do the mapping for ourselves and issue |
| 2043 | * submit_bio directly. |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2044 | * to avoid any synchronization issues, wait for the data after writing, which |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2045 | * actually prevents the read that triggered the error from finishing. |
| 2046 | * currently, there can be no more than two copies of every data bit. thus, |
| 2047 | * exactly one rewrite is required. |
| 2048 | */ |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2049 | int repair_io_failure(struct inode *inode, u64 start, u64 length, u64 logical, |
| 2050 | struct page *page, unsigned int pg_offset, int mirror_num) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2051 | { |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2052 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2053 | struct bio *bio; |
| 2054 | struct btrfs_device *dev; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2055 | u64 map_length = 0; |
| 2056 | u64 sector; |
| 2057 | struct btrfs_bio *bbio = NULL; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2058 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2059 | int ret; |
| 2060 | |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2061 | ASSERT(!(fs_info->sb->s_flags & MS_RDONLY)); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2062 | BUG_ON(!mirror_num); |
| 2063 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2064 | /* we can't repair anything in raid56 yet */ |
| 2065 | if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num)) |
| 2066 | return 0; |
| 2067 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2068 | bio = btrfs_io_bio_alloc(GFP_NOFS, 1); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2069 | if (!bio) |
| 2070 | return -EIO; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2071 | bio->bi_iter.bi_size = 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2072 | map_length = length; |
| 2073 | |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2074 | /* |
| 2075 | * Avoid races with device replace and make sure our bbio has devices |
| 2076 | * associated to its stripes that don't go away while we are doing the |
| 2077 | * read repair operation. |
| 2078 | */ |
| 2079 | btrfs_bio_counter_inc_blocked(fs_info); |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 2080 | ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical, |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2081 | &map_length, &bbio, mirror_num); |
| 2082 | if (ret) { |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2083 | btrfs_bio_counter_dec(fs_info); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2084 | bio_put(bio); |
| 2085 | return -EIO; |
| 2086 | } |
| 2087 | BUG_ON(mirror_num != bbio->mirror_num); |
| 2088 | sector = bbio->stripes[mirror_num-1].physical >> 9; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2089 | bio->bi_iter.bi_sector = sector; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2090 | dev = bbio->stripes[mirror_num-1].dev; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 2091 | btrfs_put_bbio(bbio); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2092 | if (!dev || !dev->bdev || !dev->writeable) { |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2093 | btrfs_bio_counter_dec(fs_info); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2094 | bio_put(bio); |
| 2095 | return -EIO; |
| 2096 | } |
| 2097 | bio->bi_bdev = dev->bdev; |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 2098 | bio->bi_opf = REQ_OP_WRITE | REQ_SYNC; |
Miao Xie | ffdd201 | 2014-09-12 18:44:00 +0800 | [diff] [blame] | 2099 | bio_add_page(bio, page, length, pg_offset); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2100 | |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 2101 | if (btrfsic_submit_bio_wait(bio)) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2102 | /* try to remap that extent elsewhere? */ |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2103 | btrfs_bio_counter_dec(fs_info); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2104 | bio_put(bio); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2105 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2106 | return -EIO; |
| 2107 | } |
| 2108 | |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 2109 | btrfs_info_rl_in_rcu(fs_info, |
| 2110 | "read error corrected: ino %llu off %llu (dev %s sector %llu)", |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2111 | btrfs_ino(BTRFS_I(inode)), start, |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2112 | rcu_str_deref(dev->name), sector); |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2113 | btrfs_bio_counter_dec(fs_info); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2114 | bio_put(bio); |
| 2115 | return 0; |
| 2116 | } |
| 2117 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2118 | int repair_eb_io_failure(struct btrfs_fs_info *fs_info, |
| 2119 | struct extent_buffer *eb, int mirror_num) |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2120 | { |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2121 | u64 start = eb->start; |
| 2122 | unsigned long i, num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | d95603b | 2012-04-12 15:55:15 -0400 | [diff] [blame] | 2123 | int ret = 0; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2124 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2125 | if (fs_info->sb->s_flags & MS_RDONLY) |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2126 | return -EROFS; |
| 2127 | |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2128 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 2129 | struct page *p = eb->pages[i]; |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2130 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2131 | ret = repair_io_failure(fs_info->btree_inode, start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2132 | PAGE_SIZE, start, p, |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2133 | start - page_offset(p), mirror_num); |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2134 | if (ret) |
| 2135 | break; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2136 | start += PAGE_SIZE; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | return ret; |
| 2140 | } |
| 2141 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2142 | /* |
| 2143 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 2144 | * to see if we need to process or clean up an io_failure_record |
| 2145 | */ |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2146 | int clean_io_failure(struct inode *inode, u64 start, struct page *page, |
| 2147 | unsigned int pg_offset) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2148 | { |
| 2149 | u64 private; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2150 | struct io_failure_record *failrec; |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2151 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2152 | struct extent_state *state; |
| 2153 | int num_copies; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2154 | int ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2155 | |
| 2156 | private = 0; |
| 2157 | ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
| 2158 | (u64)-1, 1, EXTENT_DIRTY, 0); |
| 2159 | if (!ret) |
| 2160 | return 0; |
| 2161 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2162 | ret = get_state_failrec(&BTRFS_I(inode)->io_failure_tree, start, |
| 2163 | &failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2164 | if (ret) |
| 2165 | return 0; |
| 2166 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2167 | BUG_ON(!failrec->this_mirror); |
| 2168 | |
| 2169 | if (failrec->in_validation) { |
| 2170 | /* there was no real error, just free the record */ |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2171 | btrfs_debug(fs_info, |
| 2172 | "clean_io_failure: freeing dummy error at %llu", |
| 2173 | failrec->start); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2174 | goto out; |
| 2175 | } |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2176 | if (fs_info->sb->s_flags & MS_RDONLY) |
| 2177 | goto out; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2178 | |
| 2179 | spin_lock(&BTRFS_I(inode)->io_tree.lock); |
| 2180 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 2181 | failrec->start, |
| 2182 | EXTENT_LOCKED); |
| 2183 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); |
| 2184 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2185 | if (state && state->start <= failrec->start && |
| 2186 | state->end >= failrec->start + failrec->len - 1) { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2187 | num_copies = btrfs_num_copies(fs_info, failrec->logical, |
| 2188 | failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2189 | if (num_copies > 1) { |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2190 | repair_io_failure(inode, start, failrec->len, |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2191 | failrec->logical, page, |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2192 | pg_offset, failrec->failed_mirror); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2193 | } |
| 2194 | } |
| 2195 | |
| 2196 | out: |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2197 | free_io_failure(inode, failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2198 | |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2199 | return 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2200 | } |
| 2201 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2202 | /* |
| 2203 | * Can be called when |
| 2204 | * - hold extent lock |
| 2205 | * - under ordered extent |
| 2206 | * - the inode is freeing |
| 2207 | */ |
| 2208 | void btrfs_free_io_failure_record(struct inode *inode, u64 start, u64 end) |
| 2209 | { |
| 2210 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 2211 | struct io_failure_record *failrec; |
| 2212 | struct extent_state *state, *next; |
| 2213 | |
| 2214 | if (RB_EMPTY_ROOT(&failure_tree->state)) |
| 2215 | return; |
| 2216 | |
| 2217 | spin_lock(&failure_tree->lock); |
| 2218 | state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY); |
| 2219 | while (state) { |
| 2220 | if (state->start > end) |
| 2221 | break; |
| 2222 | |
| 2223 | ASSERT(state->end <= end); |
| 2224 | |
| 2225 | next = next_state(state); |
| 2226 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2227 | failrec = state->failrec; |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2228 | free_extent_state(state); |
| 2229 | kfree(failrec); |
| 2230 | |
| 2231 | state = next; |
| 2232 | } |
| 2233 | spin_unlock(&failure_tree->lock); |
| 2234 | } |
| 2235 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2236 | 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] | 2237 | struct io_failure_record **failrec_ret) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2238 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2239 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2240 | struct io_failure_record *failrec; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2241 | struct extent_map *em; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2242 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 2243 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 2244 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2245 | int ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2246 | u64 logical; |
| 2247 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2248 | ret = get_state_failrec(failure_tree, start, &failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2249 | if (ret) { |
| 2250 | failrec = kzalloc(sizeof(*failrec), GFP_NOFS); |
| 2251 | if (!failrec) |
| 2252 | return -ENOMEM; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2253 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2254 | failrec->start = start; |
| 2255 | failrec->len = end - start + 1; |
| 2256 | failrec->this_mirror = 0; |
| 2257 | failrec->bio_flags = 0; |
| 2258 | failrec->in_validation = 0; |
| 2259 | |
| 2260 | read_lock(&em_tree->lock); |
| 2261 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 2262 | if (!em) { |
| 2263 | read_unlock(&em_tree->lock); |
| 2264 | kfree(failrec); |
| 2265 | return -EIO; |
| 2266 | } |
| 2267 | |
Filipe David Borba Manana | 68ba990 | 2013-11-25 03:22:07 +0000 | [diff] [blame] | 2268 | if (em->start > start || em->start + em->len <= start) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2269 | free_extent_map(em); |
| 2270 | em = NULL; |
| 2271 | } |
| 2272 | read_unlock(&em_tree->lock); |
Tsutomu Itoh | 7a2d6a6 | 2012-10-01 03:07:15 -0600 | [diff] [blame] | 2273 | if (!em) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2274 | kfree(failrec); |
| 2275 | return -EIO; |
| 2276 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2277 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2278 | logical = start - em->start; |
| 2279 | logical = em->block_start + logical; |
| 2280 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 2281 | logical = em->block_start; |
| 2282 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
| 2283 | extent_set_compress_type(&failrec->bio_flags, |
| 2284 | em->compress_type); |
| 2285 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2286 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2287 | btrfs_debug(fs_info, |
| 2288 | "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu", |
| 2289 | logical, start, failrec->len); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2290 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2291 | failrec->logical = logical; |
| 2292 | free_extent_map(em); |
| 2293 | |
| 2294 | /* set the bits in the private failure tree */ |
| 2295 | ret = set_extent_bits(failure_tree, start, end, |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2296 | EXTENT_LOCKED | EXTENT_DIRTY); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2297 | if (ret >= 0) |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2298 | ret = set_state_failrec(failure_tree, start, failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2299 | /* set the bits in the inode's tree */ |
| 2300 | if (ret >= 0) |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2301 | ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2302 | if (ret < 0) { |
| 2303 | kfree(failrec); |
| 2304 | return ret; |
| 2305 | } |
| 2306 | } else { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2307 | btrfs_debug(fs_info, |
| 2308 | "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d", |
| 2309 | failrec->logical, failrec->start, failrec->len, |
| 2310 | failrec->in_validation); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2311 | /* |
| 2312 | * when data can be on disk more than twice, add to failrec here |
| 2313 | * (e.g. with a list for failed_mirror) to make |
| 2314 | * clean_io_failure() clean all those errors at once. |
| 2315 | */ |
| 2316 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2317 | |
| 2318 | *failrec_ret = failrec; |
| 2319 | |
| 2320 | return 0; |
| 2321 | } |
| 2322 | |
| 2323 | int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio, |
| 2324 | struct io_failure_record *failrec, int failed_mirror) |
| 2325 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2326 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2327 | int num_copies; |
| 2328 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2329 | num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2330 | if (num_copies == 1) { |
| 2331 | /* |
| 2332 | * we only have a single copy of the data, so don't bother with |
| 2333 | * all the retry and error correction code that follows. no |
| 2334 | * matter what the error is, it is very likely to persist. |
| 2335 | */ |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2336 | btrfs_debug(fs_info, |
| 2337 | "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", |
| 2338 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2339 | return 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2340 | } |
| 2341 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2342 | /* |
| 2343 | * there are two premises: |
| 2344 | * a) deliver good data to the caller |
| 2345 | * b) correct the bad sectors on disk |
| 2346 | */ |
| 2347 | if (failed_bio->bi_vcnt > 1) { |
| 2348 | /* |
| 2349 | * to fulfill b), we need to know the exact failing sectors, as |
| 2350 | * we don't want to rewrite any more than the failed ones. thus, |
| 2351 | * we need separate read requests for the failed bio |
| 2352 | * |
| 2353 | * if the following BUG_ON triggers, our validation request got |
| 2354 | * merged. we need separate requests for our algorithm to work. |
| 2355 | */ |
| 2356 | BUG_ON(failrec->in_validation); |
| 2357 | failrec->in_validation = 1; |
| 2358 | failrec->this_mirror = failed_mirror; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2359 | } else { |
| 2360 | /* |
| 2361 | * we're ready to fulfill a) and b) alongside. get a good copy |
| 2362 | * of the failed sector and if we succeed, we have setup |
| 2363 | * everything for repair_io_failure to do the rest for us. |
| 2364 | */ |
| 2365 | if (failrec->in_validation) { |
| 2366 | BUG_ON(failrec->this_mirror != failed_mirror); |
| 2367 | failrec->in_validation = 0; |
| 2368 | failrec->this_mirror = 0; |
| 2369 | } |
| 2370 | failrec->failed_mirror = failed_mirror; |
| 2371 | failrec->this_mirror++; |
| 2372 | if (failrec->this_mirror == failed_mirror) |
| 2373 | failrec->this_mirror++; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2374 | } |
| 2375 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2376 | if (failrec->this_mirror > num_copies) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2377 | btrfs_debug(fs_info, |
| 2378 | "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", |
| 2379 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2380 | return 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2381 | } |
| 2382 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2383 | return 1; |
| 2384 | } |
| 2385 | |
| 2386 | |
| 2387 | struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio, |
| 2388 | struct io_failure_record *failrec, |
| 2389 | struct page *page, int pg_offset, int icsum, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2390 | bio_end_io_t *endio_func, void *data) |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2391 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2392 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2393 | struct bio *bio; |
| 2394 | struct btrfs_io_bio *btrfs_failed_bio; |
| 2395 | struct btrfs_io_bio *btrfs_bio; |
| 2396 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2397 | bio = btrfs_io_bio_alloc(GFP_NOFS, 1); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2398 | if (!bio) |
| 2399 | return NULL; |
| 2400 | |
| 2401 | bio->bi_end_io = endio_func; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2402 | bio->bi_iter.bi_sector = failrec->logical >> 9; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2403 | bio->bi_bdev = fs_info->fs_devices->latest_bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2404 | bio->bi_iter.bi_size = 0; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2405 | bio->bi_private = data; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2406 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2407 | btrfs_failed_bio = btrfs_io_bio(failed_bio); |
| 2408 | if (btrfs_failed_bio->csum) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2409 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
| 2410 | |
| 2411 | btrfs_bio = btrfs_io_bio(bio); |
| 2412 | btrfs_bio->csum = btrfs_bio->csum_inline; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2413 | icsum *= csum_size; |
| 2414 | memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum, |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2415 | csum_size); |
| 2416 | } |
| 2417 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2418 | bio_add_page(bio, page, failrec->len, pg_offset); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2419 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2420 | return bio; |
| 2421 | } |
| 2422 | |
| 2423 | /* |
| 2424 | * this is a generic handler for readpage errors (default |
| 2425 | * readpage_io_failed_hook). if other copies exist, read those and write back |
| 2426 | * good data to the failed position. does not investigate in remapping the |
| 2427 | * failed extent elsewhere, hoping the device will be smart enough to do this as |
| 2428 | * needed |
| 2429 | */ |
| 2430 | |
| 2431 | static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset, |
| 2432 | struct page *page, u64 start, u64 end, |
| 2433 | int failed_mirror) |
| 2434 | { |
| 2435 | struct io_failure_record *failrec; |
| 2436 | struct inode *inode = page->mapping->host; |
| 2437 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 2438 | struct bio *bio; |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 2439 | int read_mode = 0; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2440 | int ret; |
| 2441 | |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2442 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2443 | |
| 2444 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 2445 | if (ret) |
| 2446 | return ret; |
| 2447 | |
| 2448 | ret = btrfs_check_repairable(inode, failed_bio, failrec, failed_mirror); |
| 2449 | if (!ret) { |
| 2450 | free_io_failure(inode, failrec); |
| 2451 | return -EIO; |
| 2452 | } |
| 2453 | |
| 2454 | if (failed_bio->bi_vcnt > 1) |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 2455 | read_mode |= REQ_FAILFAST_DEV; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2456 | |
| 2457 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
| 2458 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
| 2459 | start - page_offset(page), |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2460 | (int)phy_offset, failed_bio->bi_end_io, |
| 2461 | NULL); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2462 | if (!bio) { |
| 2463 | free_io_failure(inode, failrec); |
| 2464 | return -EIO; |
| 2465 | } |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2466 | bio_set_op_attrs(bio, REQ_OP_READ, read_mode); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2467 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2468 | btrfs_debug(btrfs_sb(inode->i_sb), |
| 2469 | "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d", |
| 2470 | read_mode, failrec->this_mirror, failrec->in_validation); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2471 | |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 2472 | ret = tree->ops->submit_bio_hook(inode, bio, failrec->this_mirror, |
Tsutomu Itoh | 013bd4c | 2012-02-16 10:11:40 +0900 | [diff] [blame] | 2473 | failrec->bio_flags, 0); |
Miao Xie | 6c387ab | 2014-09-12 18:43:57 +0800 | [diff] [blame] | 2474 | if (ret) { |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2475 | free_io_failure(inode, failrec); |
Miao Xie | 6c387ab | 2014-09-12 18:43:57 +0800 | [diff] [blame] | 2476 | bio_put(bio); |
| 2477 | } |
| 2478 | |
Tsutomu Itoh | 013bd4c | 2012-02-16 10:11:40 +0900 | [diff] [blame] | 2479 | return ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2480 | } |
| 2481 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2482 | /* lots and lots of room for performance fixes in the end_bio funcs */ |
| 2483 | |
David Sterba | b5227c0 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 2484 | 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] | 2485 | { |
| 2486 | int uptodate = (err == 0); |
| 2487 | struct extent_io_tree *tree; |
Eric Sandeen | 3e2426b | 2014-06-12 00:39:58 -0500 | [diff] [blame] | 2488 | int ret = 0; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2489 | |
| 2490 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 2491 | |
| 2492 | if (tree->ops && tree->ops->writepage_end_io_hook) { |
| 2493 | ret = tree->ops->writepage_end_io_hook(page, start, |
| 2494 | end, NULL, uptodate); |
| 2495 | if (ret) |
| 2496 | uptodate = 0; |
| 2497 | } |
| 2498 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2499 | if (!uptodate) { |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2500 | ClearPageUptodate(page); |
| 2501 | SetPageError(page); |
Liu Bo | 5dca6ee | 2014-05-12 12:47:36 +0800 | [diff] [blame] | 2502 | ret = ret < 0 ? ret : -EIO; |
| 2503 | mapping_set_error(page->mapping, ret); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2504 | } |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2505 | } |
| 2506 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2507 | /* |
| 2508 | * after a writepage IO is done, we need to: |
| 2509 | * clear the uptodate bits on error |
| 2510 | * clear the writeback bits in the extent tree for this IO |
| 2511 | * end_page_writeback if the page has no more pending IO |
| 2512 | * |
| 2513 | * Scheduling is not allowed, so the extent state tree is expected |
| 2514 | * to have one and only one object corresponding to this IO. |
| 2515 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2516 | static void end_bio_extent_writepage(struct bio *bio) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2517 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2518 | struct bio_vec *bvec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2519 | u64 start; |
| 2520 | u64 end; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2521 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2522 | |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2523 | bio_for_each_segment_all(bvec, bio, i) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2524 | struct page *page = bvec->bv_page; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2525 | struct inode *inode = page->mapping->host; |
| 2526 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2527 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2528 | /* We always issue full-page reads, but if some block |
| 2529 | * in a page fails to read, blk_update_request() will |
| 2530 | * advance bv_offset and adjust bv_len to compensate. |
| 2531 | * Print a warning for nonzero offsets, and an error |
| 2532 | * if they don't add up to a full page. */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2533 | if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) { |
| 2534 | if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2535 | btrfs_err(fs_info, |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2536 | "partial page write in btrfs with offset %u and length %u", |
| 2537 | bvec->bv_offset, bvec->bv_len); |
| 2538 | else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2539 | btrfs_info(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2540 | "incomplete page write in btrfs with offset %u and length %u", |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2541 | bvec->bv_offset, bvec->bv_len); |
| 2542 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2543 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2544 | start = page_offset(page); |
| 2545 | end = start + bvec->bv_offset + bvec->bv_len - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2546 | |
David Sterba | b5227c0 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 2547 | end_extent_writepage(page, bio->bi_error, start, end); |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2548 | end_page_writeback(page); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2549 | } |
Chris Mason | 2b1f55b | 2008-09-24 11:48:04 -0400 | [diff] [blame] | 2550 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2551 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2552 | } |
| 2553 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2554 | static void |
| 2555 | endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len, |
| 2556 | int uptodate) |
| 2557 | { |
| 2558 | struct extent_state *cached = NULL; |
| 2559 | u64 end = start + len - 1; |
| 2560 | |
| 2561 | if (uptodate && tree->track_uptodate) |
| 2562 | set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC); |
| 2563 | unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC); |
| 2564 | } |
| 2565 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2566 | /* |
| 2567 | * after a readpage IO is done, we need to: |
| 2568 | * clear the uptodate bits on error |
| 2569 | * set the uptodate bits if things worked |
| 2570 | * set the page up to date if all extents in the tree are uptodate |
| 2571 | * clear the lock bit in the extent tree |
| 2572 | * unlock the page if there are no other extents locked for it |
| 2573 | * |
| 2574 | * Scheduling is not allowed, so the extent state tree is expected |
| 2575 | * to have one and only one object corresponding to this IO. |
| 2576 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2577 | static void end_bio_extent_readpage(struct bio *bio) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2578 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2579 | struct bio_vec *bvec; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2580 | int uptodate = !bio->bi_error; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2581 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2582 | struct extent_io_tree *tree; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2583 | u64 offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2584 | u64 start; |
| 2585 | u64 end; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2586 | u64 len; |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2587 | u64 extent_start = 0; |
| 2588 | u64 extent_len = 0; |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2589 | int mirror; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2590 | int ret; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2591 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2592 | |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2593 | bio_for_each_segment_all(bvec, bio, i) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2594 | struct page *page = bvec->bv_page; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2595 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2596 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2597 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2598 | btrfs_debug(fs_info, |
| 2599 | "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u", |
| 2600 | (u64)bio->bi_iter.bi_sector, bio->bi_error, |
| 2601 | io_bio->mirror_num); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2602 | tree = &BTRFS_I(inode)->io_tree; |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2603 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2604 | /* We always issue full-page reads, but if some block |
| 2605 | * in a page fails to read, blk_update_request() will |
| 2606 | * advance bv_offset and adjust bv_len to compensate. |
| 2607 | * Print a warning for nonzero offsets, and an error |
| 2608 | * if they don't add up to a full page. */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2609 | if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) { |
| 2610 | if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2611 | btrfs_err(fs_info, |
| 2612 | "partial page read in btrfs with offset %u and length %u", |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2613 | bvec->bv_offset, bvec->bv_len); |
| 2614 | else |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2615 | btrfs_info(fs_info, |
| 2616 | "incomplete page read in btrfs with offset %u and length %u", |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2617 | bvec->bv_offset, bvec->bv_len); |
| 2618 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2619 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2620 | start = page_offset(page); |
| 2621 | end = start + bvec->bv_offset + bvec->bv_len - 1; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2622 | len = bvec->bv_len; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2623 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2624 | mirror = io_bio->mirror_num; |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2625 | if (likely(uptodate && tree->ops && |
| 2626 | tree->ops->readpage_end_io_hook)) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2627 | ret = tree->ops->readpage_end_io_hook(io_bio, offset, |
| 2628 | page, start, end, |
| 2629 | mirror); |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2630 | if (ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2631 | uptodate = 0; |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2632 | else |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2633 | clean_io_failure(inode, start, page, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2634 | } |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2635 | |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2636 | if (likely(uptodate)) |
| 2637 | goto readpage_ok; |
| 2638 | |
| 2639 | if (tree->ops && tree->ops->readpage_io_failed_hook) { |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2640 | ret = tree->ops->readpage_io_failed_hook(page, mirror); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2641 | if (!ret && !bio->bi_error) |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2642 | uptodate = 1; |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2643 | } else { |
Jan Schmidt | f4a8e65 | 2011-12-01 09:30:36 -0500 | [diff] [blame] | 2644 | /* |
| 2645 | * The generic bio_readpage_error handles errors the |
| 2646 | * following way: If possible, new read requests are |
| 2647 | * created and submitted and will end up in |
| 2648 | * end_bio_extent_readpage as well (if we're lucky, not |
| 2649 | * in the !uptodate case). In that case it returns 0 and |
| 2650 | * we just go on with the next page in our bio. If it |
| 2651 | * can't handle the error it will return -EIO and we |
| 2652 | * remain responsible for that page. |
| 2653 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2654 | ret = bio_readpage_error(bio, offset, page, start, end, |
| 2655 | mirror); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2656 | if (ret == 0) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2657 | uptodate = !bio->bi_error; |
Liu Bo | 38c1c2e | 2014-08-19 23:33:13 +0800 | [diff] [blame] | 2658 | offset += len; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2659 | continue; |
| 2660 | } |
| 2661 | } |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2662 | readpage_ok: |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2663 | if (likely(uptodate)) { |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2664 | loff_t i_size = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2665 | pgoff_t end_index = i_size >> PAGE_SHIFT; |
Liu Bo | a583c02 | 2014-08-19 23:32:22 +0800 | [diff] [blame] | 2666 | unsigned off; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2667 | |
| 2668 | /* Zero out the end if this page straddles i_size */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2669 | off = i_size & (PAGE_SIZE-1); |
Liu Bo | a583c02 | 2014-08-19 23:32:22 +0800 | [diff] [blame] | 2670 | if (page->index == end_index && off) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2671 | zero_user_segment(page, off, PAGE_SIZE); |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2672 | SetPageUptodate(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2673 | } else { |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2674 | ClearPageUptodate(page); |
| 2675 | SetPageError(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2676 | } |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2677 | unlock_page(page); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2678 | offset += len; |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2679 | |
| 2680 | if (unlikely(!uptodate)) { |
| 2681 | if (extent_len) { |
| 2682 | endio_readpage_release_extent(tree, |
| 2683 | extent_start, |
| 2684 | extent_len, 1); |
| 2685 | extent_start = 0; |
| 2686 | extent_len = 0; |
| 2687 | } |
| 2688 | endio_readpage_release_extent(tree, start, |
| 2689 | end - start + 1, 0); |
| 2690 | } else if (!extent_len) { |
| 2691 | extent_start = start; |
| 2692 | extent_len = end + 1 - start; |
| 2693 | } else if (extent_start + extent_len == start) { |
| 2694 | extent_len += end + 1 - start; |
| 2695 | } else { |
| 2696 | endio_readpage_release_extent(tree, extent_start, |
| 2697 | extent_len, uptodate); |
| 2698 | extent_start = start; |
| 2699 | extent_len = end + 1 - start; |
| 2700 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2701 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2702 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2703 | if (extent_len) |
| 2704 | endio_readpage_release_extent(tree, extent_start, extent_len, |
| 2705 | uptodate); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2706 | if (io_bio->end_io) |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2707 | io_bio->end_io(io_bio, bio->bi_error); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2708 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2709 | } |
| 2710 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2711 | /* |
| 2712 | * this allocates from the btrfs_bioset. We're returning a bio right now |
| 2713 | * but you can call btrfs_io_bio for the appropriate container_of magic |
| 2714 | */ |
Miao Xie | 88f794e | 2010-11-22 03:02:55 +0000 | [diff] [blame] | 2715 | struct bio * |
| 2716 | btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs, |
| 2717 | gfp_t gfp_flags) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2718 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2719 | struct btrfs_io_bio *btrfs_bio; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2720 | struct bio *bio; |
| 2721 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2722 | bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2723 | |
| 2724 | if (bio == NULL && (current->flags & PF_MEMALLOC)) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2725 | while (!bio && (nr_vecs /= 2)) { |
| 2726 | bio = bio_alloc_bioset(gfp_flags, |
| 2727 | nr_vecs, btrfs_bioset); |
| 2728 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2729 | } |
| 2730 | |
| 2731 | if (bio) { |
| 2732 | bio->bi_bdev = bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2733 | bio->bi_iter.bi_sector = first_sector; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2734 | btrfs_bio = btrfs_io_bio(bio); |
| 2735 | btrfs_bio->csum = NULL; |
| 2736 | btrfs_bio->csum_allocated = NULL; |
| 2737 | btrfs_bio->end_io = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2738 | } |
| 2739 | return bio; |
| 2740 | } |
| 2741 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2742 | struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask) |
| 2743 | { |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 2744 | struct btrfs_io_bio *btrfs_bio; |
| 2745 | struct bio *new; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2746 | |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 2747 | new = bio_clone_bioset(bio, gfp_mask, btrfs_bioset); |
| 2748 | if (new) { |
| 2749 | btrfs_bio = btrfs_io_bio(new); |
| 2750 | btrfs_bio->csum = NULL; |
| 2751 | btrfs_bio->csum_allocated = NULL; |
| 2752 | btrfs_bio->end_io = NULL; |
| 2753 | } |
| 2754 | return new; |
| 2755 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2756 | |
| 2757 | /* this also allocates from the btrfs_bioset */ |
| 2758 | struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs) |
| 2759 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2760 | struct btrfs_io_bio *btrfs_bio; |
| 2761 | struct bio *bio; |
| 2762 | |
| 2763 | bio = bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset); |
| 2764 | if (bio) { |
| 2765 | btrfs_bio = btrfs_io_bio(bio); |
| 2766 | btrfs_bio->csum = NULL; |
| 2767 | btrfs_bio->csum_allocated = NULL; |
| 2768 | btrfs_bio->end_io = NULL; |
| 2769 | } |
| 2770 | return bio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2771 | } |
| 2772 | |
| 2773 | |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2774 | static int __must_check submit_one_bio(struct bio *bio, int mirror_num, |
| 2775 | unsigned long bio_flags) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2776 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2777 | int ret = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2778 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 2779 | struct page *page = bvec->bv_page; |
| 2780 | struct extent_io_tree *tree = bio->bi_private; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2781 | u64 start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2782 | |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2783 | start = page_offset(page) + bvec->bv_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2784 | |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2785 | bio->bi_private = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2786 | bio_get(bio); |
| 2787 | |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2788 | if (tree->ops && tree->ops->submit_bio_hook) |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 2789 | ret = tree->ops->submit_bio_hook(page->mapping->host, bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 2790 | mirror_num, bio_flags, start); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2791 | else |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 2792 | btrfsic_submit_bio(bio); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2793 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2794 | bio_put(bio); |
| 2795 | return ret; |
| 2796 | } |
| 2797 | |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2798 | static int merge_bio(struct extent_io_tree *tree, struct page *page, |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 2799 | unsigned long offset, size_t size, struct bio *bio, |
| 2800 | unsigned long bio_flags) |
| 2801 | { |
| 2802 | int ret = 0; |
| 2803 | if (tree->ops && tree->ops->merge_bio_hook) |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 2804 | ret = tree->ops->merge_bio_hook(page, offset, size, bio, |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 2805 | bio_flags); |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 2806 | return ret; |
| 2807 | |
| 2808 | } |
| 2809 | |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2810 | static int submit_extent_page(int op, int op_flags, struct extent_io_tree *tree, |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2811 | struct writeback_control *wbc, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2812 | struct page *page, sector_t sector, |
| 2813 | size_t size, unsigned long offset, |
| 2814 | struct block_device *bdev, |
| 2815 | struct bio **bio_ret, |
| 2816 | unsigned long max_pages, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2817 | bio_end_io_t end_io_func, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2818 | int mirror_num, |
| 2819 | unsigned long prev_bio_flags, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2820 | unsigned long bio_flags, |
| 2821 | bool force_bio_submit) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2822 | { |
| 2823 | int ret = 0; |
| 2824 | struct bio *bio; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2825 | int contig = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2826 | int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2827 | size_t page_size = min_t(size_t, size, PAGE_SIZE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2828 | |
| 2829 | if (bio_ret && *bio_ret) { |
| 2830 | bio = *bio_ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2831 | if (old_compressed) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2832 | contig = bio->bi_iter.bi_sector == sector; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2833 | else |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 2834 | contig = bio_end_sector(bio) == sector; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2835 | |
| 2836 | if (prev_bio_flags != bio_flags || !contig || |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2837 | force_bio_submit || |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2838 | merge_bio(tree, page, offset, page_size, bio, bio_flags) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2839 | bio_add_page(bio, page, page_size, offset) < page_size) { |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2840 | ret = submit_one_bio(bio, mirror_num, prev_bio_flags); |
Naohiro Aota | 289454a | 2015-01-06 01:01:03 +0900 | [diff] [blame] | 2841 | if (ret < 0) { |
| 2842 | *bio_ret = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2843 | return ret; |
Naohiro Aota | 289454a | 2015-01-06 01:01:03 +0900 | [diff] [blame] | 2844 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2845 | bio = NULL; |
| 2846 | } else { |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2847 | if (wbc) |
| 2848 | wbc_account_io(wbc, page, page_size); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2849 | return 0; |
| 2850 | } |
| 2851 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2852 | |
Kent Overstreet | b54ffb7 | 2015-05-19 14:31:01 +0200 | [diff] [blame] | 2853 | bio = btrfs_bio_alloc(bdev, sector, BIO_MAX_PAGES, |
| 2854 | GFP_NOFS | __GFP_HIGH); |
Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 2855 | if (!bio) |
| 2856 | return -ENOMEM; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2857 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2858 | bio_add_page(bio, page, page_size, offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2859 | bio->bi_end_io = end_io_func; |
| 2860 | bio->bi_private = tree; |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2861 | bio_set_op_attrs(bio, op, op_flags); |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2862 | if (wbc) { |
| 2863 | wbc_init_bio(wbc, bio); |
| 2864 | wbc_account_io(wbc, page, page_size); |
| 2865 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2866 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2867 | if (bio_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2868 | *bio_ret = bio; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2869 | else |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2870 | ret = submit_one_bio(bio, mirror_num, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2871 | |
| 2872 | return ret; |
| 2873 | } |
| 2874 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 2875 | static void attach_extent_buffer_page(struct extent_buffer *eb, |
| 2876 | struct page *page) |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 2877 | { |
| 2878 | if (!PagePrivate(page)) { |
| 2879 | SetPagePrivate(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2880 | get_page(page); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 2881 | set_page_private(page, (unsigned long)eb); |
| 2882 | } else { |
| 2883 | WARN_ON(page->private != (unsigned long)eb); |
| 2884 | } |
| 2885 | } |
| 2886 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2887 | void set_page_extent_mapped(struct page *page) |
| 2888 | { |
| 2889 | if (!PagePrivate(page)) { |
| 2890 | SetPagePrivate(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2891 | get_page(page); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2892 | set_page_private(page, EXTENT_PAGE_PRIVATE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2893 | } |
| 2894 | } |
| 2895 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2896 | static struct extent_map * |
| 2897 | __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset, |
| 2898 | u64 start, u64 len, get_extent_t *get_extent, |
| 2899 | struct extent_map **em_cached) |
| 2900 | { |
| 2901 | struct extent_map *em; |
| 2902 | |
| 2903 | if (em_cached && *em_cached) { |
| 2904 | em = *em_cached; |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 2905 | if (extent_map_in_tree(em) && start >= em->start && |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2906 | start < extent_map_end(em)) { |
| 2907 | atomic_inc(&em->refs); |
| 2908 | return em; |
| 2909 | } |
| 2910 | |
| 2911 | free_extent_map(em); |
| 2912 | *em_cached = NULL; |
| 2913 | } |
| 2914 | |
| 2915 | em = get_extent(inode, page, pg_offset, start, len, 0); |
| 2916 | if (em_cached && !IS_ERR_OR_NULL(em)) { |
| 2917 | BUG_ON(*em_cached); |
| 2918 | atomic_inc(&em->refs); |
| 2919 | *em_cached = em; |
| 2920 | } |
| 2921 | return em; |
| 2922 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2923 | /* |
| 2924 | * basic readpage implementation. Locked extent state structs are inserted |
| 2925 | * into the tree that are removed when the IO is done (by the end_io |
| 2926 | * handlers) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2927 | * XXX JDM: This needs looking at to ensure proper page locking |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 2928 | * return 0 on success, otherwise return error |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2929 | */ |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2930 | static int __do_readpage(struct extent_io_tree *tree, |
| 2931 | struct page *page, |
| 2932 | get_extent_t *get_extent, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2933 | struct extent_map **em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2934 | struct bio **bio, int mirror_num, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2935 | unsigned long *bio_flags, int read_flags, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2936 | u64 *prev_em_start) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2937 | { |
| 2938 | struct inode *inode = page->mapping->host; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2939 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2940 | u64 page_end = start + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2941 | u64 end; |
| 2942 | u64 cur = start; |
| 2943 | u64 extent_offset; |
| 2944 | u64 last_byte = i_size_read(inode); |
| 2945 | u64 block_start; |
| 2946 | u64 cur_end; |
| 2947 | sector_t sector; |
| 2948 | struct extent_map *em; |
| 2949 | struct block_device *bdev; |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 2950 | int ret = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2951 | int nr = 0; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2952 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2953 | size_t iosize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2954 | size_t disk_io_size; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2955 | size_t blocksize = inode->i_sb->s_blocksize; |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 2956 | unsigned long this_bio_flag = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2957 | |
| 2958 | set_page_extent_mapped(page); |
| 2959 | |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2960 | end = page_end; |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 2961 | if (!PageUptodate(page)) { |
| 2962 | if (cleancache_get_page(page) == 0) { |
| 2963 | BUG_ON(blocksize != PAGE_SIZE); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2964 | unlock_extent(tree, start, end); |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 2965 | goto out; |
| 2966 | } |
| 2967 | } |
| 2968 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2969 | if (page->index == last_byte >> PAGE_SHIFT) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2970 | char *userpage; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2971 | size_t zero_offset = last_byte & (PAGE_SIZE - 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2972 | |
| 2973 | if (zero_offset) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2974 | iosize = PAGE_SIZE - zero_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2975 | userpage = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2976 | memset(userpage + zero_offset, 0, iosize); |
| 2977 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2978 | kunmap_atomic(userpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2979 | } |
| 2980 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2981 | while (cur <= end) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2982 | unsigned long pnr = (last_byte >> PAGE_SHIFT) + 1; |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2983 | bool force_bio_submit = false; |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 2984 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2985 | if (cur >= last_byte) { |
| 2986 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2987 | struct extent_state *cached = NULL; |
| 2988 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2989 | iosize = PAGE_SIZE - pg_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2990 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2991 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2992 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2993 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2994 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2995 | &cached, GFP_NOFS); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 2996 | unlock_extent_cached(tree, cur, |
| 2997 | cur + iosize - 1, |
| 2998 | &cached, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2999 | break; |
| 3000 | } |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3001 | em = __get_extent_map(inode, page, pg_offset, cur, |
| 3002 | end - cur + 1, get_extent, em_cached); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3003 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3004 | SetPageError(page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3005 | unlock_extent(tree, cur, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3006 | break; |
| 3007 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3008 | extent_offset = cur - em->start; |
| 3009 | BUG_ON(extent_map_end(em) <= cur); |
| 3010 | BUG_ON(end < cur); |
| 3011 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3012 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 3013 | this_bio_flag |= EXTENT_BIO_COMPRESSED; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3014 | extent_set_compress_type(&this_bio_flag, |
| 3015 | em->compress_type); |
| 3016 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3017 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3018 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
| 3019 | cur_end = min(extent_map_end(em) - 1, end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3020 | iosize = ALIGN(iosize, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3021 | if (this_bio_flag & EXTENT_BIO_COMPRESSED) { |
| 3022 | disk_io_size = em->block_len; |
| 3023 | sector = em->block_start >> 9; |
| 3024 | } else { |
| 3025 | sector = (em->block_start + extent_offset) >> 9; |
| 3026 | disk_io_size = iosize; |
| 3027 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3028 | bdev = em->bdev; |
| 3029 | block_start = em->block_start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3030 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 3031 | block_start = EXTENT_MAP_HOLE; |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3032 | |
| 3033 | /* |
| 3034 | * If we have a file range that points to a compressed extent |
| 3035 | * and it's followed by a consecutive file range that points to |
| 3036 | * to the same compressed extent (possibly with a different |
| 3037 | * offset and/or length, so it either points to the whole extent |
| 3038 | * or only part of it), we must make sure we do not submit a |
| 3039 | * single bio to populate the pages for the 2 ranges because |
| 3040 | * this makes the compressed extent read zero out the pages |
| 3041 | * belonging to the 2nd range. Imagine the following scenario: |
| 3042 | * |
| 3043 | * File layout |
| 3044 | * [0 - 8K] [8K - 24K] |
| 3045 | * | | |
| 3046 | * | | |
| 3047 | * points to extent X, points to extent X, |
| 3048 | * offset 4K, length of 8K offset 0, length 16K |
| 3049 | * |
| 3050 | * [extent X, compressed length = 4K uncompressed length = 16K] |
| 3051 | * |
| 3052 | * If the bio to read the compressed extent covers both ranges, |
| 3053 | * it will decompress extent X into the pages belonging to the |
| 3054 | * first range and then it will stop, zeroing out the remaining |
| 3055 | * pages that belong to the other range that points to extent X. |
| 3056 | * So here we make sure we submit 2 bios, one for the first |
| 3057 | * range and another one for the third range. Both will target |
| 3058 | * the same physical extent from disk, but we can't currently |
| 3059 | * make the compressed bio endio callback populate the pages |
| 3060 | * for both ranges because each compressed bio is tightly |
| 3061 | * coupled with a single extent map, and each range can have |
| 3062 | * an extent map with a different offset value relative to the |
| 3063 | * uncompressed data of our extent and different lengths. This |
| 3064 | * is a corner case so we prioritize correctness over |
| 3065 | * non-optimal behavior (submitting 2 bios for the same extent). |
| 3066 | */ |
| 3067 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) && |
| 3068 | prev_em_start && *prev_em_start != (u64)-1 && |
| 3069 | *prev_em_start != em->orig_start) |
| 3070 | force_bio_submit = true; |
| 3071 | |
| 3072 | if (prev_em_start) |
| 3073 | *prev_em_start = em->orig_start; |
| 3074 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3075 | free_extent_map(em); |
| 3076 | em = NULL; |
| 3077 | |
| 3078 | /* we've found a hole, just zero and go on */ |
| 3079 | if (block_start == EXTENT_MAP_HOLE) { |
| 3080 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3081 | struct extent_state *cached = NULL; |
| 3082 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3083 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3084 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3085 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3086 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3087 | |
| 3088 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3089 | &cached, GFP_NOFS); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3090 | unlock_extent_cached(tree, cur, |
| 3091 | cur + iosize - 1, |
| 3092 | &cached, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3093 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3094 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3095 | continue; |
| 3096 | } |
| 3097 | /* the get_extent function already copied into the page */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3098 | if (test_range_bit(tree, cur, cur_end, |
| 3099 | EXTENT_UPTODATE, 1, NULL)) { |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 3100 | check_page_uptodate(tree, page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3101 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3102 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3103 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3104 | continue; |
| 3105 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3106 | /* we have an inline extent but it didn't get marked up |
| 3107 | * to date. Error out |
| 3108 | */ |
| 3109 | if (block_start == EXTENT_MAP_INLINE) { |
| 3110 | SetPageError(page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3111 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3112 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3113 | pg_offset += iosize; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3114 | continue; |
| 3115 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3116 | |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3117 | pnr -= page->index; |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3118 | ret = submit_extent_page(REQ_OP_READ, read_flags, tree, NULL, |
| 3119 | page, sector, disk_io_size, pg_offset, |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3120 | bdev, bio, pnr, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3121 | end_bio_extent_readpage, mirror_num, |
| 3122 | *bio_flags, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3123 | this_bio_flag, |
| 3124 | force_bio_submit); |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3125 | if (!ret) { |
| 3126 | nr++; |
| 3127 | *bio_flags = this_bio_flag; |
| 3128 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3129 | SetPageError(page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3130 | unlock_extent(tree, cur, cur + iosize - 1); |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 3131 | goto out; |
Josef Bacik | edd33c9 | 2012-10-05 16:40:32 -0400 | [diff] [blame] | 3132 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3133 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3134 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3135 | } |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 3136 | out: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3137 | if (!nr) { |
| 3138 | if (!PageError(page)) |
| 3139 | SetPageUptodate(page); |
| 3140 | unlock_page(page); |
| 3141 | } |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 3142 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3143 | } |
| 3144 | |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3145 | static inline void __do_contiguous_readpages(struct extent_io_tree *tree, |
| 3146 | struct page *pages[], int nr_pages, |
| 3147 | u64 start, u64 end, |
| 3148 | get_extent_t *get_extent, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3149 | struct extent_map **em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3150 | struct bio **bio, int mirror_num, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3151 | unsigned long *bio_flags, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3152 | u64 *prev_em_start) |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3153 | { |
| 3154 | struct inode *inode; |
| 3155 | struct btrfs_ordered_extent *ordered; |
| 3156 | int index; |
| 3157 | |
| 3158 | inode = pages[0]->mapping->host; |
| 3159 | while (1) { |
| 3160 | lock_extent(tree, start, end); |
| 3161 | ordered = btrfs_lookup_ordered_range(inode, start, |
| 3162 | end - start + 1); |
| 3163 | if (!ordered) |
| 3164 | break; |
| 3165 | unlock_extent(tree, start, end); |
| 3166 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3167 | btrfs_put_ordered_extent(ordered); |
| 3168 | } |
| 3169 | |
| 3170 | for (index = 0; index < nr_pages; index++) { |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3171 | __do_readpage(tree, pages[index], get_extent, em_cached, bio, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3172 | mirror_num, bio_flags, 0, prev_em_start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3173 | put_page(pages[index]); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3174 | } |
| 3175 | } |
| 3176 | |
| 3177 | static void __extent_readpages(struct extent_io_tree *tree, |
| 3178 | struct page *pages[], |
| 3179 | int nr_pages, get_extent_t *get_extent, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3180 | struct extent_map **em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3181 | struct bio **bio, int mirror_num, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3182 | unsigned long *bio_flags, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3183 | u64 *prev_em_start) |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3184 | { |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 3185 | u64 start = 0; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3186 | u64 end = 0; |
| 3187 | u64 page_start; |
| 3188 | int index; |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 3189 | int first_index = 0; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3190 | |
| 3191 | for (index = 0; index < nr_pages; index++) { |
| 3192 | page_start = page_offset(pages[index]); |
| 3193 | if (!end) { |
| 3194 | start = page_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3195 | end = start + PAGE_SIZE - 1; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3196 | first_index = index; |
| 3197 | } else if (end + 1 == page_start) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3198 | end += PAGE_SIZE; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3199 | } else { |
| 3200 | __do_contiguous_readpages(tree, &pages[first_index], |
| 3201 | index - first_index, start, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3202 | end, get_extent, em_cached, |
| 3203 | bio, mirror_num, bio_flags, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3204 | prev_em_start); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3205 | start = page_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3206 | end = start + PAGE_SIZE - 1; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3207 | first_index = index; |
| 3208 | } |
| 3209 | } |
| 3210 | |
| 3211 | if (end) |
| 3212 | __do_contiguous_readpages(tree, &pages[first_index], |
| 3213 | index - first_index, start, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3214 | end, get_extent, em_cached, bio, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3215 | mirror_num, bio_flags, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3216 | prev_em_start); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3217 | } |
| 3218 | |
| 3219 | static int __extent_read_full_page(struct extent_io_tree *tree, |
| 3220 | struct page *page, |
| 3221 | get_extent_t *get_extent, |
| 3222 | struct bio **bio, int mirror_num, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3223 | unsigned long *bio_flags, int read_flags) |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3224 | { |
| 3225 | struct inode *inode = page->mapping->host; |
| 3226 | struct btrfs_ordered_extent *ordered; |
| 3227 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3228 | u64 end = start + PAGE_SIZE - 1; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3229 | int ret; |
| 3230 | |
| 3231 | while (1) { |
| 3232 | lock_extent(tree, start, end); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 3233 | ordered = btrfs_lookup_ordered_range(inode, start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3234 | PAGE_SIZE); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3235 | if (!ordered) |
| 3236 | break; |
| 3237 | unlock_extent(tree, start, end); |
| 3238 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3239 | btrfs_put_ordered_extent(ordered); |
| 3240 | } |
| 3241 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3242 | ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3243 | bio_flags, read_flags, NULL); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3244 | return ret; |
| 3245 | } |
| 3246 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3247 | 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] | 3248 | get_extent_t *get_extent, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3249 | { |
| 3250 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3251 | unsigned long bio_flags = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3252 | int ret; |
| 3253 | |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 3254 | ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3255 | &bio_flags, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3256 | if (bio) |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3257 | ret = submit_one_bio(bio, mirror_num, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3258 | return ret; |
| 3259 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3260 | |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3261 | static void update_nr_written(struct page *page, struct writeback_control *wbc, |
| 3262 | unsigned long nr_written) |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3263 | { |
| 3264 | wbc->nr_to_write -= nr_written; |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3265 | } |
| 3266 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3267 | /* |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3268 | * helper for __extent_writepage, doing all of the delayed allocation setup. |
| 3269 | * |
| 3270 | * This returns 1 if our fill_delalloc function did all the work required |
| 3271 | * to write the page (copy into inline extent). In this case the IO has |
| 3272 | * been started and the page is already unlocked. |
| 3273 | * |
| 3274 | * This returns 0 if all went well (page still locked) |
| 3275 | * This returns < 0 if there were errors (page still locked) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3276 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3277 | static noinline_for_stack int writepage_delalloc(struct inode *inode, |
| 3278 | struct page *page, struct writeback_control *wbc, |
| 3279 | struct extent_page_data *epd, |
| 3280 | u64 delalloc_start, |
| 3281 | unsigned long *nr_written) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3282 | { |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3283 | struct extent_io_tree *tree = epd->tree; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3284 | u64 page_end = delalloc_start + PAGE_SIZE - 1; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3285 | u64 nr_delalloc; |
| 3286 | u64 delalloc_to_write = 0; |
| 3287 | u64 delalloc_end = 0; |
| 3288 | int ret; |
| 3289 | int page_started = 0; |
| 3290 | |
| 3291 | if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc) |
| 3292 | return 0; |
| 3293 | |
| 3294 | while (delalloc_end < page_end) { |
| 3295 | nr_delalloc = find_lock_delalloc_range(inode, tree, |
| 3296 | page, |
| 3297 | &delalloc_start, |
| 3298 | &delalloc_end, |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 3299 | BTRFS_MAX_EXTENT_SIZE); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3300 | if (nr_delalloc == 0) { |
| 3301 | delalloc_start = delalloc_end + 1; |
| 3302 | continue; |
| 3303 | } |
| 3304 | ret = tree->ops->fill_delalloc(inode, page, |
| 3305 | delalloc_start, |
| 3306 | delalloc_end, |
| 3307 | &page_started, |
| 3308 | nr_written); |
| 3309 | /* File system has been set read-only */ |
| 3310 | if (ret) { |
| 3311 | SetPageError(page); |
| 3312 | /* fill_delalloc should be return < 0 for error |
| 3313 | * but just in case, we use > 0 here meaning the |
| 3314 | * IO is started, so we don't want to return > 0 |
| 3315 | * unless things are going well. |
| 3316 | */ |
| 3317 | ret = ret < 0 ? ret : -EIO; |
| 3318 | goto done; |
| 3319 | } |
| 3320 | /* |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 3321 | * delalloc_end is already one less than the total length, so |
| 3322 | * we don't subtract one from PAGE_SIZE |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3323 | */ |
| 3324 | delalloc_to_write += (delalloc_end - delalloc_start + |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 3325 | PAGE_SIZE) >> PAGE_SHIFT; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3326 | delalloc_start = delalloc_end + 1; |
| 3327 | } |
| 3328 | if (wbc->nr_to_write < delalloc_to_write) { |
| 3329 | int thresh = 8192; |
| 3330 | |
| 3331 | if (delalloc_to_write < thresh * 2) |
| 3332 | thresh = delalloc_to_write; |
| 3333 | wbc->nr_to_write = min_t(u64, delalloc_to_write, |
| 3334 | thresh); |
| 3335 | } |
| 3336 | |
| 3337 | /* did the fill delalloc function already unlock and start |
| 3338 | * the IO? |
| 3339 | */ |
| 3340 | if (page_started) { |
| 3341 | /* |
| 3342 | * we've unlocked the page, so we can't update |
| 3343 | * the mapping's writeback index, just update |
| 3344 | * nr_to_write. |
| 3345 | */ |
| 3346 | wbc->nr_to_write -= *nr_written; |
| 3347 | return 1; |
| 3348 | } |
| 3349 | |
| 3350 | ret = 0; |
| 3351 | |
| 3352 | done: |
| 3353 | return ret; |
| 3354 | } |
| 3355 | |
| 3356 | /* |
| 3357 | * helper for __extent_writepage. This calls the writepage start hooks, |
| 3358 | * and does the loop to map the page into extents and bios. |
| 3359 | * |
| 3360 | * We return 1 if the IO is started and the page is unlocked, |
| 3361 | * 0 if all went well (page still locked) |
| 3362 | * < 0 if there were errors (page still locked) |
| 3363 | */ |
| 3364 | static noinline_for_stack int __extent_writepage_io(struct inode *inode, |
| 3365 | struct page *page, |
| 3366 | struct writeback_control *wbc, |
| 3367 | struct extent_page_data *epd, |
| 3368 | loff_t i_size, |
| 3369 | unsigned long nr_written, |
| 3370 | int write_flags, int *nr_ret) |
| 3371 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3372 | struct extent_io_tree *tree = epd->tree; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3373 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3374 | u64 page_end = start + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3375 | u64 end; |
| 3376 | u64 cur = start; |
| 3377 | u64 extent_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3378 | u64 block_start; |
| 3379 | u64 iosize; |
| 3380 | sector_t sector; |
| 3381 | struct extent_map *em; |
| 3382 | struct block_device *bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3383 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3384 | size_t blocksize; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3385 | int ret = 0; |
| 3386 | int nr = 0; |
| 3387 | bool compressed; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3388 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3389 | if (tree->ops && tree->ops->writepage_start_hook) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3390 | ret = tree->ops->writepage_start_hook(page, start, |
| 3391 | page_end); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 3392 | if (ret) { |
| 3393 | /* Fixup worker will requeue */ |
| 3394 | if (ret == -EBUSY) |
| 3395 | wbc->pages_skipped++; |
| 3396 | else |
| 3397 | redirty_page_for_writepage(wbc, page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3398 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3399 | update_nr_written(page, wbc, nr_written); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3400 | unlock_page(page); |
Liu Bo | bcf9348 | 2017-01-25 17:15:54 -0800 | [diff] [blame] | 3401 | return 1; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3402 | } |
| 3403 | } |
| 3404 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3405 | /* |
| 3406 | * we don't want to touch the inode after unlocking the page, |
| 3407 | * so we update the mapping writeback index now |
| 3408 | */ |
| 3409 | update_nr_written(page, wbc, nr_written + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3410 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3411 | end = page_end; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3412 | if (i_size <= start) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3413 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 3414 | tree->ops->writepage_end_io_hook(page, start, |
| 3415 | page_end, NULL, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3416 | goto done; |
| 3417 | } |
| 3418 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3419 | blocksize = inode->i_sb->s_blocksize; |
| 3420 | |
| 3421 | while (cur <= end) { |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3422 | u64 em_end; |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3423 | unsigned long max_nr; |
| 3424 | |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3425 | if (cur >= i_size) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3426 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 3427 | tree->ops->writepage_end_io_hook(page, cur, |
| 3428 | page_end, NULL, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3429 | break; |
| 3430 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3431 | em = epd->get_extent(inode, page, pg_offset, cur, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3432 | end - cur + 1, 1); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3433 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3434 | SetPageError(page); |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 3435 | ret = PTR_ERR_OR_ZERO(em); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3436 | break; |
| 3437 | } |
| 3438 | |
| 3439 | extent_offset = cur - em->start; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3440 | em_end = extent_map_end(em); |
| 3441 | BUG_ON(em_end <= cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3442 | BUG_ON(end < cur); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3443 | iosize = min(em_end - cur, end - cur + 1); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3444 | iosize = ALIGN(iosize, blocksize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3445 | sector = (em->block_start + extent_offset) >> 9; |
| 3446 | bdev = em->bdev; |
| 3447 | block_start = em->block_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3448 | compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3449 | free_extent_map(em); |
| 3450 | em = NULL; |
| 3451 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3452 | /* |
| 3453 | * compressed and inline extents are written through other |
| 3454 | * paths in the FS |
| 3455 | */ |
| 3456 | if (compressed || block_start == EXTENT_MAP_HOLE || |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3457 | block_start == EXTENT_MAP_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3458 | /* |
| 3459 | * end_io notification does not happen here for |
| 3460 | * compressed extents |
| 3461 | */ |
| 3462 | if (!compressed && tree->ops && |
| 3463 | tree->ops->writepage_end_io_hook) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3464 | tree->ops->writepage_end_io_hook(page, cur, |
| 3465 | cur + iosize - 1, |
| 3466 | NULL, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3467 | else if (compressed) { |
| 3468 | /* we don't want to end_page_writeback on |
| 3469 | * a compressed extent. this happens |
| 3470 | * elsewhere |
| 3471 | */ |
| 3472 | nr++; |
| 3473 | } |
| 3474 | |
| 3475 | cur += iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3476 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3477 | continue; |
| 3478 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3479 | |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3480 | max_nr = (i_size >> PAGE_SHIFT) + 1; |
| 3481 | |
| 3482 | set_range_writeback(tree, cur, cur + iosize - 1); |
| 3483 | if (!PageWriteback(page)) { |
| 3484 | btrfs_err(BTRFS_I(inode)->root->fs_info, |
| 3485 | "page %lu not writeback, cur %llu end %llu", |
| 3486 | page->index, cur, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3487 | } |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3488 | |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3489 | ret = submit_extent_page(REQ_OP_WRITE, write_flags, tree, wbc, |
| 3490 | page, sector, iosize, pg_offset, |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3491 | bdev, &epd->bio, max_nr, |
| 3492 | end_bio_extent_writepage, |
| 3493 | 0, 0, 0, false); |
| 3494 | if (ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3495 | SetPageError(page); |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3496 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3497 | cur = cur + iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3498 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3499 | nr++; |
| 3500 | } |
| 3501 | done: |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3502 | *nr_ret = nr; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3503 | return ret; |
| 3504 | } |
| 3505 | |
| 3506 | /* |
| 3507 | * the writepage semantics are similar to regular writepage. extent |
| 3508 | * records are inserted to lock ranges in the tree, and as dirty areas |
| 3509 | * are found, they are marked writeback. Then the lock bits are removed |
| 3510 | * and the end_io handler clears the writeback ranges |
| 3511 | */ |
| 3512 | static int __extent_writepage(struct page *page, struct writeback_control *wbc, |
| 3513 | void *data) |
| 3514 | { |
| 3515 | struct inode *inode = page->mapping->host; |
| 3516 | struct extent_page_data *epd = data; |
| 3517 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3518 | u64 page_end = start + PAGE_SIZE - 1; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3519 | int ret; |
| 3520 | int nr = 0; |
| 3521 | size_t pg_offset = 0; |
| 3522 | loff_t i_size = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3523 | unsigned long end_index = i_size >> PAGE_SHIFT; |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3524 | int write_flags = 0; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3525 | unsigned long nr_written = 0; |
| 3526 | |
| 3527 | if (wbc->sync_mode == WB_SYNC_ALL) |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 3528 | write_flags = REQ_SYNC; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3529 | |
| 3530 | trace___extent_writepage(page, inode, wbc); |
| 3531 | |
| 3532 | WARN_ON(!PageLocked(page)); |
| 3533 | |
| 3534 | ClearPageError(page); |
| 3535 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3536 | pg_offset = i_size & (PAGE_SIZE - 1); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3537 | if (page->index > end_index || |
| 3538 | (page->index == end_index && !pg_offset)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3539 | page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3540 | unlock_page(page); |
| 3541 | return 0; |
| 3542 | } |
| 3543 | |
| 3544 | if (page->index == end_index) { |
| 3545 | char *userpage; |
| 3546 | |
| 3547 | userpage = kmap_atomic(page); |
| 3548 | memset(userpage + pg_offset, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3549 | PAGE_SIZE - pg_offset); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3550 | kunmap_atomic(userpage); |
| 3551 | flush_dcache_page(page); |
| 3552 | } |
| 3553 | |
| 3554 | pg_offset = 0; |
| 3555 | |
| 3556 | set_page_extent_mapped(page); |
| 3557 | |
| 3558 | ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written); |
| 3559 | if (ret == 1) |
| 3560 | goto done_unlocked; |
| 3561 | if (ret) |
| 3562 | goto done; |
| 3563 | |
| 3564 | ret = __extent_writepage_io(inode, page, wbc, epd, |
| 3565 | i_size, nr_written, write_flags, &nr); |
| 3566 | if (ret == 1) |
| 3567 | goto done_unlocked; |
| 3568 | |
| 3569 | done: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3570 | if (nr == 0) { |
| 3571 | /* make sure the mapping tag for page dirty gets cleared */ |
| 3572 | set_page_writeback(page); |
| 3573 | end_page_writeback(page); |
| 3574 | } |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 3575 | if (PageError(page)) { |
| 3576 | ret = ret < 0 ? ret : -EIO; |
| 3577 | end_extent_writepage(page, ret, start, page_end); |
| 3578 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3579 | unlock_page(page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3580 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3581 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3582 | done_unlocked: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3583 | return 0; |
| 3584 | } |
| 3585 | |
Josef Bacik | fd8b2b6 | 2013-04-24 16:41:19 -0400 | [diff] [blame] | 3586 | void wait_on_extent_buffer_writeback(struct extent_buffer *eb) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3587 | { |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 3588 | wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK, |
| 3589 | TASK_UNINTERRUPTIBLE); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3590 | } |
| 3591 | |
Chris Mason | 0e378df | 2014-05-19 20:55:27 -0700 | [diff] [blame] | 3592 | static noinline_for_stack int |
| 3593 | lock_extent_buffer_for_io(struct extent_buffer *eb, |
| 3594 | struct btrfs_fs_info *fs_info, |
| 3595 | struct extent_page_data *epd) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3596 | { |
| 3597 | unsigned long i, num_pages; |
| 3598 | int flush = 0; |
| 3599 | int ret = 0; |
| 3600 | |
| 3601 | if (!btrfs_try_tree_write_lock(eb)) { |
| 3602 | flush = 1; |
| 3603 | flush_write_bio(epd); |
| 3604 | btrfs_tree_lock(eb); |
| 3605 | } |
| 3606 | |
| 3607 | if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) { |
| 3608 | btrfs_tree_unlock(eb); |
| 3609 | if (!epd->sync_io) |
| 3610 | return 0; |
| 3611 | if (!flush) { |
| 3612 | flush_write_bio(epd); |
| 3613 | flush = 1; |
| 3614 | } |
Chris Mason | a098d8e | 2012-03-21 12:09:56 -0400 | [diff] [blame] | 3615 | while (1) { |
| 3616 | wait_on_extent_buffer_writeback(eb); |
| 3617 | btrfs_tree_lock(eb); |
| 3618 | if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) |
| 3619 | break; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3620 | btrfs_tree_unlock(eb); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3621 | } |
| 3622 | } |
| 3623 | |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3624 | /* |
| 3625 | * We need to do this to prevent races in people who check if the eb is |
| 3626 | * under IO since we can end up having no IO bits set for a short period |
| 3627 | * of time. |
| 3628 | */ |
| 3629 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3630 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) { |
| 3631 | set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3632 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3633 | btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); |
Miao Xie | e2d8452 | 2013-01-29 10:09:20 +0000 | [diff] [blame] | 3634 | __percpu_counter_add(&fs_info->dirty_metadata_bytes, |
| 3635 | -eb->len, |
| 3636 | fs_info->dirty_metadata_batch); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3637 | ret = 1; |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3638 | } else { |
| 3639 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3640 | } |
| 3641 | |
| 3642 | btrfs_tree_unlock(eb); |
| 3643 | |
| 3644 | if (!ret) |
| 3645 | return ret; |
| 3646 | |
| 3647 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3648 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 3649 | struct page *p = eb->pages[i]; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3650 | |
| 3651 | if (!trylock_page(p)) { |
| 3652 | if (!flush) { |
| 3653 | flush_write_bio(epd); |
| 3654 | flush = 1; |
| 3655 | } |
| 3656 | lock_page(p); |
| 3657 | } |
| 3658 | } |
| 3659 | |
| 3660 | return ret; |
| 3661 | } |
| 3662 | |
| 3663 | static void end_extent_buffer_writeback(struct extent_buffer *eb) |
| 3664 | { |
| 3665 | clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 3666 | smp_mb__after_atomic(); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3667 | wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK); |
| 3668 | } |
| 3669 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3670 | static void set_btree_ioerr(struct page *page) |
| 3671 | { |
| 3672 | struct extent_buffer *eb = (struct extent_buffer *)page->private; |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3673 | |
| 3674 | SetPageError(page); |
| 3675 | if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) |
| 3676 | return; |
| 3677 | |
| 3678 | /* |
| 3679 | * If writeback for a btree extent that doesn't belong to a log tree |
| 3680 | * failed, increment the counter transaction->eb_write_errors. |
| 3681 | * We do this because while the transaction is running and before it's |
| 3682 | * committing (when we call filemap_fdata[write|wait]_range against |
| 3683 | * the btree inode), we might have |
| 3684 | * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it |
| 3685 | * returns an error or an error happens during writeback, when we're |
| 3686 | * committing the transaction we wouldn't know about it, since the pages |
| 3687 | * can be no longer dirty nor marked anymore for writeback (if a |
| 3688 | * subsequent modification to the extent buffer didn't happen before the |
| 3689 | * transaction commit), which makes filemap_fdata[write|wait]_range not |
| 3690 | * able to find the pages tagged with SetPageError at transaction |
| 3691 | * commit time. So if this happens we must abort the transaction, |
| 3692 | * otherwise we commit a super block with btree roots that point to |
| 3693 | * btree nodes/leafs whose content on disk is invalid - either garbage |
| 3694 | * or the content of some node/leaf from a past generation that got |
| 3695 | * cowed or deleted and is no longer valid. |
| 3696 | * |
| 3697 | * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would |
| 3698 | * not be enough - we need to distinguish between log tree extents vs |
| 3699 | * non-log tree extents, and the next filemap_fdatawait_range() call |
| 3700 | * will catch and clear such errors in the mapping - and that call might |
| 3701 | * be from a log sync and not from a transaction commit. Also, checking |
| 3702 | * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is |
| 3703 | * not done and would not be reliable - the eb might have been released |
| 3704 | * from memory and reading it back again means that flag would not be |
| 3705 | * set (since it's a runtime flag, not persisted on disk). |
| 3706 | * |
| 3707 | * Using the flags below in the btree inode also makes us achieve the |
| 3708 | * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started |
| 3709 | * writeback for all dirty pages and before filemap_fdatawait_range() |
| 3710 | * is called, the writeback for all dirty pages had already finished |
| 3711 | * with errors - because we were not using AS_EIO/AS_ENOSPC, |
| 3712 | * filemap_fdatawait_range() would return success, as it could not know |
| 3713 | * that writeback errors happened (the pages were no longer tagged for |
| 3714 | * writeback). |
| 3715 | */ |
| 3716 | switch (eb->log_index) { |
| 3717 | case -1: |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 3718 | set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3719 | break; |
| 3720 | case 0: |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 3721 | set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3722 | break; |
| 3723 | case 1: |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 3724 | set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3725 | break; |
| 3726 | default: |
| 3727 | BUG(); /* unexpected, logic error */ |
| 3728 | } |
| 3729 | } |
| 3730 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3731 | static void end_bio_extent_buffer_writepage(struct bio *bio) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3732 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3733 | struct bio_vec *bvec; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3734 | struct extent_buffer *eb; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3735 | int i, done; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3736 | |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3737 | bio_for_each_segment_all(bvec, bio, i) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3738 | struct page *page = bvec->bv_page; |
| 3739 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3740 | eb = (struct extent_buffer *)page->private; |
| 3741 | BUG_ON(!eb); |
| 3742 | done = atomic_dec_and_test(&eb->io_pages); |
| 3743 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3744 | if (bio->bi_error || |
| 3745 | test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3746 | ClearPageUptodate(page); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3747 | set_btree_ioerr(page); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3748 | } |
| 3749 | |
| 3750 | end_page_writeback(page); |
| 3751 | |
| 3752 | if (!done) |
| 3753 | continue; |
| 3754 | |
| 3755 | end_extent_buffer_writeback(eb); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3756 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3757 | |
| 3758 | bio_put(bio); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3759 | } |
| 3760 | |
Chris Mason | 0e378df | 2014-05-19 20:55:27 -0700 | [diff] [blame] | 3761 | static noinline_for_stack int write_one_eb(struct extent_buffer *eb, |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3762 | struct btrfs_fs_info *fs_info, |
| 3763 | struct writeback_control *wbc, |
| 3764 | struct extent_page_data *epd) |
| 3765 | { |
| 3766 | struct block_device *bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 3767 | 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] | 3768 | u64 offset = eb->start; |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3769 | u32 nritems; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3770 | unsigned long i, num_pages; |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3771 | unsigned long bio_flags = 0; |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3772 | unsigned long start, end; |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 3773 | int write_flags = (epd->sync_io ? REQ_SYNC : 0) | REQ_META; |
Josef Bacik | d7dbe9e | 2012-04-23 14:00:51 -0400 | [diff] [blame] | 3774 | int ret = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3775 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3776 | clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3777 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3778 | atomic_set(&eb->io_pages, num_pages); |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3779 | if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID) |
| 3780 | bio_flags = EXTENT_BIO_TREE_LOG; |
| 3781 | |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3782 | /* set btree blocks beyond nritems with 0 to avoid stale content. */ |
| 3783 | nritems = btrfs_header_nritems(eb); |
Liu Bo | 3eb548e | 2016-09-14 17:22:57 -0700 | [diff] [blame] | 3784 | if (btrfs_header_level(eb) > 0) { |
Liu Bo | 3eb548e | 2016-09-14 17:22:57 -0700 | [diff] [blame] | 3785 | end = btrfs_node_key_ptr_offset(nritems); |
| 3786 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3787 | memzero_extent_buffer(eb, end, eb->len - end); |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3788 | } else { |
| 3789 | /* |
| 3790 | * leaf: |
| 3791 | * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0 |
| 3792 | */ |
| 3793 | start = btrfs_item_nr_offset(nritems); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3794 | end = btrfs_leaf_data(eb) + leaf_data_end(fs_info, eb); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3795 | memzero_extent_buffer(eb, start, end - start); |
Liu Bo | 3eb548e | 2016-09-14 17:22:57 -0700 | [diff] [blame] | 3796 | } |
| 3797 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3798 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 3799 | struct page *p = eb->pages[i]; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3800 | |
| 3801 | clear_page_dirty_for_io(p); |
| 3802 | set_page_writeback(p); |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3803 | ret = submit_extent_page(REQ_OP_WRITE, write_flags, tree, wbc, |
| 3804 | p, offset >> 9, PAGE_SIZE, 0, bdev, |
| 3805 | &epd->bio, -1, |
| 3806 | end_bio_extent_buffer_writepage, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3807 | 0, epd->bio_flags, bio_flags, false); |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3808 | epd->bio_flags = bio_flags; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3809 | if (ret) { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3810 | set_btree_ioerr(p); |
Filipe Manana | 55e3bd2 | 2014-09-22 17:41:04 +0100 | [diff] [blame] | 3811 | end_page_writeback(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3812 | if (atomic_sub_and_test(num_pages - i, &eb->io_pages)) |
| 3813 | end_extent_buffer_writeback(eb); |
| 3814 | ret = -EIO; |
| 3815 | break; |
| 3816 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3817 | offset += PAGE_SIZE; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3818 | update_nr_written(p, wbc, 1); |
| 3819 | unlock_page(p); |
| 3820 | } |
| 3821 | |
| 3822 | if (unlikely(ret)) { |
| 3823 | for (; i < num_pages; i++) { |
Chris Mason | bbf65cf | 2014-10-04 09:56:45 -0700 | [diff] [blame] | 3824 | struct page *p = eb->pages[i]; |
Liu Bo | 8146502 | 2014-09-23 22:22:33 +0800 | [diff] [blame] | 3825 | clear_page_dirty_for_io(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3826 | unlock_page(p); |
| 3827 | } |
| 3828 | } |
| 3829 | |
| 3830 | return ret; |
| 3831 | } |
| 3832 | |
| 3833 | int btree_write_cache_pages(struct address_space *mapping, |
| 3834 | struct writeback_control *wbc) |
| 3835 | { |
| 3836 | struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree; |
| 3837 | struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info; |
| 3838 | struct extent_buffer *eb, *prev_eb = NULL; |
| 3839 | struct extent_page_data epd = { |
| 3840 | .bio = NULL, |
| 3841 | .tree = tree, |
| 3842 | .extent_locked = 0, |
| 3843 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3844 | .bio_flags = 0, |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3845 | }; |
| 3846 | int ret = 0; |
| 3847 | int done = 0; |
| 3848 | int nr_to_write_done = 0; |
| 3849 | struct pagevec pvec; |
| 3850 | int nr_pages; |
| 3851 | pgoff_t index; |
| 3852 | pgoff_t end; /* Inclusive */ |
| 3853 | int scanned = 0; |
| 3854 | int tag; |
| 3855 | |
| 3856 | pagevec_init(&pvec, 0); |
| 3857 | if (wbc->range_cyclic) { |
| 3858 | index = mapping->writeback_index; /* Start from prev offset */ |
| 3859 | end = -1; |
| 3860 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3861 | index = wbc->range_start >> PAGE_SHIFT; |
| 3862 | end = wbc->range_end >> PAGE_SHIFT; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3863 | scanned = 1; |
| 3864 | } |
| 3865 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3866 | tag = PAGECACHE_TAG_TOWRITE; |
| 3867 | else |
| 3868 | tag = PAGECACHE_TAG_DIRTY; |
| 3869 | retry: |
| 3870 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3871 | tag_pages_for_writeback(mapping, index, end); |
| 3872 | while (!done && !nr_to_write_done && (index <= end) && |
| 3873 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
| 3874 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { |
| 3875 | unsigned i; |
| 3876 | |
| 3877 | scanned = 1; |
| 3878 | for (i = 0; i < nr_pages; i++) { |
| 3879 | struct page *page = pvec.pages[i]; |
| 3880 | |
| 3881 | if (!PagePrivate(page)) |
| 3882 | continue; |
| 3883 | |
| 3884 | if (!wbc->range_cyclic && page->index > end) { |
| 3885 | done = 1; |
| 3886 | break; |
| 3887 | } |
| 3888 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3889 | spin_lock(&mapping->private_lock); |
| 3890 | if (!PagePrivate(page)) { |
| 3891 | spin_unlock(&mapping->private_lock); |
| 3892 | continue; |
| 3893 | } |
| 3894 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3895 | eb = (struct extent_buffer *)page->private; |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3896 | |
| 3897 | /* |
| 3898 | * Shouldn't happen and normally this would be a BUG_ON |
| 3899 | * but no sense in crashing the users box for something |
| 3900 | * we can survive anyway. |
| 3901 | */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3902 | if (WARN_ON(!eb)) { |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3903 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3904 | continue; |
| 3905 | } |
| 3906 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3907 | if (eb == prev_eb) { |
| 3908 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3909 | continue; |
| 3910 | } |
| 3911 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3912 | ret = atomic_inc_not_zero(&eb->refs); |
| 3913 | spin_unlock(&mapping->private_lock); |
| 3914 | if (!ret) |
| 3915 | continue; |
| 3916 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3917 | prev_eb = eb; |
| 3918 | ret = lock_extent_buffer_for_io(eb, fs_info, &epd); |
| 3919 | if (!ret) { |
| 3920 | free_extent_buffer(eb); |
| 3921 | continue; |
| 3922 | } |
| 3923 | |
| 3924 | ret = write_one_eb(eb, fs_info, wbc, &epd); |
| 3925 | if (ret) { |
| 3926 | done = 1; |
| 3927 | free_extent_buffer(eb); |
| 3928 | break; |
| 3929 | } |
| 3930 | free_extent_buffer(eb); |
| 3931 | |
| 3932 | /* |
| 3933 | * the filesystem may choose to bump up nr_to_write. |
| 3934 | * We have to make sure to honor the new nr_to_write |
| 3935 | * at any time |
| 3936 | */ |
| 3937 | nr_to_write_done = wbc->nr_to_write <= 0; |
| 3938 | } |
| 3939 | pagevec_release(&pvec); |
| 3940 | cond_resched(); |
| 3941 | } |
| 3942 | if (!scanned && !done) { |
| 3943 | /* |
| 3944 | * We hit the last page and there is more work to be done: wrap |
| 3945 | * back to the start of the file |
| 3946 | */ |
| 3947 | scanned = 1; |
| 3948 | index = 0; |
| 3949 | goto retry; |
| 3950 | } |
| 3951 | flush_write_bio(&epd); |
| 3952 | return ret; |
| 3953 | } |
| 3954 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3955 | /** |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3956 | * 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] | 3957 | * @mapping: address space structure to write |
| 3958 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
| 3959 | * @writepage: function called for each page |
| 3960 | * @data: data passed to writepage function |
| 3961 | * |
| 3962 | * If a page is already under I/O, write_cache_pages() skips it, even |
| 3963 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, |
| 3964 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() |
| 3965 | * and msync() need to guarantee that all the data which was dirty at the time |
| 3966 | * the call was made get new I/O started against them. If wbc->sync_mode is |
| 3967 | * WB_SYNC_ALL then we were called for data integrity and we must wait for |
| 3968 | * existing IO to complete. |
| 3969 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 3970 | static int extent_write_cache_pages(struct extent_io_tree *tree, |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3971 | struct address_space *mapping, |
| 3972 | struct writeback_control *wbc, |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3973 | writepage_t writepage, void *data, |
| 3974 | void (*flush_fn)(void *)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3975 | { |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 3976 | struct inode *inode = mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3977 | int ret = 0; |
| 3978 | int done = 0; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3979 | int nr_to_write_done = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3980 | struct pagevec pvec; |
| 3981 | int nr_pages; |
| 3982 | pgoff_t index; |
| 3983 | pgoff_t end; /* Inclusive */ |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3984 | pgoff_t done_index; |
| 3985 | int range_whole = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3986 | int scanned = 0; |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3987 | int tag; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3988 | |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 3989 | /* |
| 3990 | * We have to hold onto the inode so that ordered extents can do their |
| 3991 | * work when the IO finishes. The alternative to this is failing to add |
| 3992 | * an ordered extent if the igrab() fails there and that is a huge pain |
| 3993 | * to deal with, so instead just hold onto the inode throughout the |
| 3994 | * writepages operation. If it fails here we are freeing up the inode |
| 3995 | * anyway and we'd rather not waste our time writing out stuff that is |
| 3996 | * going to be truncated anyway. |
| 3997 | */ |
| 3998 | if (!igrab(inode)) |
| 3999 | return 0; |
| 4000 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4001 | pagevec_init(&pvec, 0); |
| 4002 | if (wbc->range_cyclic) { |
| 4003 | index = mapping->writeback_index; /* Start from prev offset */ |
| 4004 | end = -1; |
| 4005 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4006 | index = wbc->range_start >> PAGE_SHIFT; |
| 4007 | end = wbc->range_end >> PAGE_SHIFT; |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 4008 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 4009 | range_whole = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4010 | scanned = 1; |
| 4011 | } |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 4012 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 4013 | tag = PAGECACHE_TAG_TOWRITE; |
| 4014 | else |
| 4015 | tag = PAGECACHE_TAG_DIRTY; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4016 | retry: |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 4017 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 4018 | tag_pages_for_writeback(mapping, index, end); |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 4019 | done_index = index; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 4020 | while (!done && !nr_to_write_done && (index <= end) && |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 4021 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
| 4022 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4023 | unsigned i; |
| 4024 | |
| 4025 | scanned = 1; |
| 4026 | for (i = 0; i < nr_pages; i++) { |
| 4027 | struct page *page = pvec.pages[i]; |
| 4028 | |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 4029 | done_index = page->index; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4030 | /* |
| 4031 | * At this point we hold neither mapping->tree_lock nor |
| 4032 | * lock on the page itself: the page may be truncated or |
| 4033 | * invalidated (changing page->mapping to NULL), or even |
| 4034 | * swizzled back from swapper_space to tmpfs file |
| 4035 | * mapping |
| 4036 | */ |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 4037 | if (!trylock_page(page)) { |
| 4038 | flush_fn(data); |
| 4039 | lock_page(page); |
Chris Mason | 01d658f | 2011-11-01 10:08:06 -0400 | [diff] [blame] | 4040 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4041 | |
| 4042 | if (unlikely(page->mapping != mapping)) { |
| 4043 | unlock_page(page); |
| 4044 | continue; |
| 4045 | } |
| 4046 | |
| 4047 | if (!wbc->range_cyclic && page->index > end) { |
| 4048 | done = 1; |
| 4049 | unlock_page(page); |
| 4050 | continue; |
| 4051 | } |
| 4052 | |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4053 | if (wbc->sync_mode != WB_SYNC_NONE) { |
Chris Mason | 0e6bd95 | 2008-11-20 10:46:35 -0500 | [diff] [blame] | 4054 | if (PageWriteback(page)) |
| 4055 | flush_fn(data); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4056 | wait_on_page_writeback(page); |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4057 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4058 | |
| 4059 | if (PageWriteback(page) || |
| 4060 | !clear_page_dirty_for_io(page)) { |
| 4061 | unlock_page(page); |
| 4062 | continue; |
| 4063 | } |
| 4064 | |
| 4065 | ret = (*writepage)(page, wbc, data); |
| 4066 | |
| 4067 | if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) { |
| 4068 | unlock_page(page); |
| 4069 | ret = 0; |
| 4070 | } |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 4071 | if (ret < 0) { |
| 4072 | /* |
| 4073 | * done_index is set past this page, |
| 4074 | * so media errors will not choke |
| 4075 | * background writeout for the entire |
| 4076 | * file. This has consequences for |
| 4077 | * range_cyclic semantics (ie. it may |
| 4078 | * not be suitable for data integrity |
| 4079 | * writeout). |
| 4080 | */ |
| 4081 | done_index = page->index + 1; |
| 4082 | done = 1; |
| 4083 | break; |
| 4084 | } |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 4085 | |
| 4086 | /* |
| 4087 | * the filesystem may choose to bump up nr_to_write. |
| 4088 | * We have to make sure to honor the new nr_to_write |
| 4089 | * at any time |
| 4090 | */ |
| 4091 | nr_to_write_done = wbc->nr_to_write <= 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4092 | } |
| 4093 | pagevec_release(&pvec); |
| 4094 | cond_resched(); |
| 4095 | } |
Liu Bo | 894b36e | 2016-03-07 16:56:22 -0800 | [diff] [blame] | 4096 | if (!scanned && !done) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4097 | /* |
| 4098 | * We hit the last page and there is more work to be done: wrap |
| 4099 | * back to the start of the file |
| 4100 | */ |
| 4101 | scanned = 1; |
| 4102 | index = 0; |
| 4103 | goto retry; |
| 4104 | } |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 4105 | |
| 4106 | if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole)) |
| 4107 | mapping->writeback_index = done_index; |
| 4108 | |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 4109 | btrfs_add_delayed_iput(inode); |
Liu Bo | 894b36e | 2016-03-07 16:56:22 -0800 | [diff] [blame] | 4110 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4111 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4112 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4113 | static void flush_epd_write_bio(struct extent_page_data *epd) |
| 4114 | { |
| 4115 | if (epd->bio) { |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 4116 | int ret; |
| 4117 | |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 4118 | bio_set_op_attrs(epd->bio, REQ_OP_WRITE, |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 4119 | epd->sync_io ? REQ_SYNC : 0); |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 4120 | |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 4121 | ret = submit_one_bio(epd->bio, 0, epd->bio_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4122 | BUG_ON(ret < 0); /* -ENOMEM */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4123 | epd->bio = NULL; |
| 4124 | } |
| 4125 | } |
| 4126 | |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4127 | static noinline void flush_write_bio(void *data) |
| 4128 | { |
| 4129 | struct extent_page_data *epd = data; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4130 | flush_epd_write_bio(epd); |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4131 | } |
| 4132 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4133 | int extent_write_full_page(struct extent_io_tree *tree, struct page *page, |
| 4134 | get_extent_t *get_extent, |
| 4135 | struct writeback_control *wbc) |
| 4136 | { |
| 4137 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4138 | struct extent_page_data epd = { |
| 4139 | .bio = NULL, |
| 4140 | .tree = tree, |
| 4141 | .get_extent = get_extent, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4142 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4143 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 4144 | .bio_flags = 0, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4145 | }; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4146 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4147 | ret = __extent_writepage(page, wbc, &epd); |
| 4148 | |
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 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4153 | int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode, |
| 4154 | u64 start, u64 end, get_extent_t *get_extent, |
| 4155 | int mode) |
| 4156 | { |
| 4157 | int ret = 0; |
| 4158 | struct address_space *mapping = inode->i_mapping; |
| 4159 | struct page *page; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4160 | unsigned long nr_pages = (end - start + PAGE_SIZE) >> |
| 4161 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4162 | |
| 4163 | struct extent_page_data epd = { |
| 4164 | .bio = NULL, |
| 4165 | .tree = tree, |
| 4166 | .get_extent = get_extent, |
| 4167 | .extent_locked = 1, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4168 | .sync_io = mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 4169 | .bio_flags = 0, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4170 | }; |
| 4171 | struct writeback_control wbc_writepages = { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4172 | .sync_mode = mode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4173 | .nr_to_write = nr_pages * 2, |
| 4174 | .range_start = start, |
| 4175 | .range_end = end + 1, |
| 4176 | }; |
| 4177 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4178 | while (start <= end) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4179 | page = find_get_page(mapping, start >> PAGE_SHIFT); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4180 | if (clear_page_dirty_for_io(page)) |
| 4181 | ret = __extent_writepage(page, &wbc_writepages, &epd); |
| 4182 | else { |
| 4183 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 4184 | tree->ops->writepage_end_io_hook(page, start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4185 | start + PAGE_SIZE - 1, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4186 | NULL, 1); |
| 4187 | unlock_page(page); |
| 4188 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4189 | put_page(page); |
| 4190 | start += PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4191 | } |
| 4192 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4193 | flush_epd_write_bio(&epd); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4194 | return ret; |
| 4195 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4196 | |
| 4197 | int extent_writepages(struct extent_io_tree *tree, |
| 4198 | struct address_space *mapping, |
| 4199 | get_extent_t *get_extent, |
| 4200 | struct writeback_control *wbc) |
| 4201 | { |
| 4202 | int ret = 0; |
| 4203 | struct extent_page_data epd = { |
| 4204 | .bio = NULL, |
| 4205 | .tree = tree, |
| 4206 | .get_extent = get_extent, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4207 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4208 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 4209 | .bio_flags = 0, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4210 | }; |
| 4211 | |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 4212 | ret = extent_write_cache_pages(tree, mapping, wbc, |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4213 | __extent_writepage, &epd, |
| 4214 | flush_write_bio); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4215 | flush_epd_write_bio(&epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4216 | return ret; |
| 4217 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4218 | |
| 4219 | int extent_readpages(struct extent_io_tree *tree, |
| 4220 | struct address_space *mapping, |
| 4221 | struct list_head *pages, unsigned nr_pages, |
| 4222 | get_extent_t get_extent) |
| 4223 | { |
| 4224 | struct bio *bio = NULL; |
| 4225 | unsigned page_idx; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4226 | unsigned long bio_flags = 0; |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4227 | struct page *pagepool[16]; |
| 4228 | struct page *page; |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4229 | struct extent_map *em_cached = NULL; |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4230 | int nr = 0; |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 4231 | u64 prev_em_start = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4232 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4233 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4234 | page = list_entry(pages->prev, struct page, lru); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4235 | |
| 4236 | prefetchw(&page->flags); |
| 4237 | list_del(&page->lru); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4238 | if (add_to_page_cache_lru(page, mapping, |
Michal Hocko | 8a5c743 | 2016-07-26 15:24:53 -0700 | [diff] [blame] | 4239 | page->index, |
| 4240 | readahead_gfp_mask(mapping))) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4241 | put_page(page); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4242 | continue; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4243 | } |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4244 | |
| 4245 | pagepool[nr++] = page; |
| 4246 | if (nr < ARRAY_SIZE(pagepool)) |
| 4247 | continue; |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4248 | __extent_readpages(tree, pagepool, nr, get_extent, &em_cached, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 4249 | &bio, 0, &bio_flags, &prev_em_start); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4250 | nr = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4251 | } |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 4252 | if (nr) |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4253 | __extent_readpages(tree, pagepool, nr, get_extent, &em_cached, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 4254 | &bio, 0, &bio_flags, &prev_em_start); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4255 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4256 | if (em_cached) |
| 4257 | free_extent_map(em_cached); |
| 4258 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4259 | BUG_ON(!list_empty(pages)); |
| 4260 | if (bio) |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 4261 | return submit_one_bio(bio, 0, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4262 | return 0; |
| 4263 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4264 | |
| 4265 | /* |
| 4266 | * basic invalidatepage code, this waits on any locked or writeback |
| 4267 | * ranges corresponding to the page, and then deletes any extent state |
| 4268 | * records from the tree |
| 4269 | */ |
| 4270 | int extent_invalidatepage(struct extent_io_tree *tree, |
| 4271 | struct page *page, unsigned long offset) |
| 4272 | { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4273 | struct extent_state *cached_state = NULL; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4274 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4275 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4276 | size_t blocksize = page->mapping->host->i_sb->s_blocksize; |
| 4277 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4278 | start += ALIGN(offset, blocksize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4279 | if (start > end) |
| 4280 | return 0; |
| 4281 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 4282 | lock_extent_bits(tree, start, end, &cached_state); |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 4283 | wait_on_page_writeback(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4284 | clear_extent_bit(tree, start, end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 4285 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4286 | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4287 | 1, 1, &cached_state, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4288 | return 0; |
| 4289 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4290 | |
| 4291 | /* |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4292 | * a helper for releasepage, this tests for areas of the page that |
| 4293 | * are locked or under IO and drops the related state bits if it is safe |
| 4294 | * to drop the page. |
| 4295 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 4296 | static int try_release_extent_state(struct extent_map_tree *map, |
| 4297 | struct extent_io_tree *tree, |
| 4298 | struct page *page, gfp_t mask) |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4299 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4300 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4301 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4302 | int ret = 1; |
| 4303 | |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 4304 | if (test_range_bit(tree, start, end, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4305 | EXTENT_IOBITS, 0, NULL)) |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4306 | ret = 0; |
| 4307 | else { |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 4308 | /* |
| 4309 | * at this point we can safely clear everything except the |
| 4310 | * locked bit and the nodatasum bit |
| 4311 | */ |
Chris Mason | e3f24cc | 2011-02-14 12:52:08 -0500 | [diff] [blame] | 4312 | ret = clear_extent_bit(tree, start, end, |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 4313 | ~(EXTENT_LOCKED | EXTENT_NODATASUM), |
| 4314 | 0, 0, NULL, mask); |
Chris Mason | e3f24cc | 2011-02-14 12:52:08 -0500 | [diff] [blame] | 4315 | |
| 4316 | /* if clear_extent_bit failed for enomem reasons, |
| 4317 | * we can't allow the release to continue. |
| 4318 | */ |
| 4319 | if (ret < 0) |
| 4320 | ret = 0; |
| 4321 | else |
| 4322 | ret = 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4323 | } |
| 4324 | return ret; |
| 4325 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4326 | |
| 4327 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4328 | * a helper for releasepage. As long as there are no locked extents |
| 4329 | * in the range corresponding to the page, both state records and extent |
| 4330 | * map records are removed |
| 4331 | */ |
| 4332 | int try_release_extent_mapping(struct extent_map_tree *map, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4333 | struct extent_io_tree *tree, struct page *page, |
| 4334 | gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4335 | { |
| 4336 | struct extent_map *em; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4337 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4338 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4339 | |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 4340 | if (gfpflags_allow_blocking(mask) && |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4341 | page->mapping->host->i_size > SZ_16M) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4342 | u64 len; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4343 | while (start <= end) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4344 | len = end - start + 1; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4345 | write_lock(&map->lock); |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4346 | em = lookup_extent_mapping(map, start, len); |
Tsutomu Itoh | 285190d | 2012-02-16 16:23:58 +0900 | [diff] [blame] | 4347 | if (!em) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4348 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4349 | break; |
| 4350 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 4351 | if (test_bit(EXTENT_FLAG_PINNED, &em->flags) || |
| 4352 | em->start != start) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4353 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4354 | free_extent_map(em); |
| 4355 | break; |
| 4356 | } |
| 4357 | if (!test_range_bit(tree, em->start, |
| 4358 | extent_map_end(em) - 1, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4359 | EXTENT_LOCKED | EXTENT_WRITEBACK, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 4360 | 0, NULL)) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4361 | remove_extent_mapping(map, em); |
| 4362 | /* once for the rb tree */ |
| 4363 | free_extent_map(em); |
| 4364 | } |
| 4365 | start = extent_map_end(em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4366 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4367 | |
| 4368 | /* once for us */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4369 | free_extent_map(em); |
| 4370 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4371 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4372 | return try_release_extent_state(map, tree, page, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4373 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4374 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4375 | /* |
| 4376 | * helper function for fiemap, which doesn't want to see any holes. |
| 4377 | * This maps until we find something past 'last' |
| 4378 | */ |
| 4379 | static struct extent_map *get_extent_skip_holes(struct inode *inode, |
| 4380 | u64 offset, |
| 4381 | u64 last, |
| 4382 | get_extent_t *get_extent) |
| 4383 | { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4384 | u64 sectorsize = btrfs_inode_sectorsize(inode); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4385 | struct extent_map *em; |
| 4386 | u64 len; |
| 4387 | |
| 4388 | if (offset >= last) |
| 4389 | return NULL; |
| 4390 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 4391 | while (1) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4392 | len = last - offset; |
| 4393 | if (len == 0) |
| 4394 | break; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4395 | len = ALIGN(len, sectorsize); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4396 | em = get_extent(inode, NULL, 0, offset, len, 0); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 4397 | if (IS_ERR_OR_NULL(em)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4398 | return em; |
| 4399 | |
| 4400 | /* if this isn't a hole return it */ |
| 4401 | if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) && |
| 4402 | em->block_start != EXTENT_MAP_HOLE) { |
| 4403 | return em; |
| 4404 | } |
| 4405 | |
| 4406 | /* this is a hole, advance to the next extent */ |
| 4407 | offset = extent_map_end(em); |
| 4408 | free_extent_map(em); |
| 4409 | if (offset >= last) |
| 4410 | break; |
| 4411 | } |
| 4412 | return NULL; |
| 4413 | } |
| 4414 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4415 | int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4416 | __u64 start, __u64 len, get_extent_t *get_extent) |
| 4417 | { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4418 | int ret = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4419 | u64 off = start; |
| 4420 | u64 max = start + len; |
| 4421 | u32 flags = 0; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4422 | u32 found_type; |
| 4423 | u64 last; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4424 | u64 last_for_get_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4425 | u64 disko = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4426 | u64 isize = i_size_read(inode); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4427 | struct btrfs_key found_key; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4428 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4429 | struct extent_state *cached_state = NULL; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4430 | struct btrfs_path *path; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4431 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4432 | int end = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4433 | u64 em_start = 0; |
| 4434 | u64 em_len = 0; |
| 4435 | u64 em_end = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4436 | |
| 4437 | if (len == 0) |
| 4438 | return -EINVAL; |
| 4439 | |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4440 | path = btrfs_alloc_path(); |
| 4441 | if (!path) |
| 4442 | return -ENOMEM; |
| 4443 | path->leave_spinning = 1; |
| 4444 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4445 | start = round_down(start, btrfs_inode_sectorsize(inode)); |
| 4446 | len = round_up(max, btrfs_inode_sectorsize(inode)) - start; |
Josef Bacik | 4d479cf | 2011-11-17 11:34:31 -0500 | [diff] [blame] | 4447 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4448 | /* |
| 4449 | * lookup the last file extent. We're not using i_size here |
| 4450 | * because there might be preallocation past i_size |
| 4451 | */ |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4452 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 4453 | btrfs_ino(BTRFS_I(inode)), -1, 0); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4454 | if (ret < 0) { |
| 4455 | btrfs_free_path(path); |
| 4456 | return ret; |
Liu Bo | 2d324f5 | 2016-05-17 17:21:48 -0700 | [diff] [blame] | 4457 | } else { |
| 4458 | WARN_ON(!ret); |
| 4459 | if (ret == 1) |
| 4460 | ret = 0; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4461 | } |
Liu Bo | 2d324f5 | 2016-05-17 17:21:48 -0700 | [diff] [blame] | 4462 | |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4463 | path->slots[0]--; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4464 | 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] | 4465 | found_type = found_key.type; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4466 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4467 | /* No extents, but there might be delalloc bits */ |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4468 | if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) || |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4469 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4470 | /* have to trust i_size as the end */ |
| 4471 | last = (u64)-1; |
| 4472 | last_for_get_extent = isize; |
| 4473 | } else { |
| 4474 | /* |
| 4475 | * remember the start of the last extent. There are a |
| 4476 | * bunch of different factors that go into the length of the |
| 4477 | * extent, so its much less complex to remember where it started |
| 4478 | */ |
| 4479 | last = found_key.offset; |
| 4480 | last_for_get_extent = last + 1; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4481 | } |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4482 | btrfs_release_path(path); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4483 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4484 | /* |
| 4485 | * we might have some extents allocated but more delalloc past those |
| 4486 | * extents. so, we trust isize unless the start of the last extent is |
| 4487 | * beyond isize |
| 4488 | */ |
| 4489 | if (last < isize) { |
| 4490 | last = (u64)-1; |
| 4491 | last_for_get_extent = isize; |
| 4492 | } |
| 4493 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 4494 | lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4495 | &cached_state); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4496 | |
Josef Bacik | 4d479cf | 2011-11-17 11:34:31 -0500 | [diff] [blame] | 4497 | em = get_extent_skip_holes(inode, start, last_for_get_extent, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4498 | get_extent); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4499 | if (!em) |
| 4500 | goto out; |
| 4501 | if (IS_ERR(em)) { |
| 4502 | ret = PTR_ERR(em); |
| 4503 | goto out; |
| 4504 | } |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4505 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4506 | while (!end) { |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4507 | u64 offset_in_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4508 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4509 | /* break if the extent we found is outside the range */ |
| 4510 | if (em->start >= max || extent_map_end(em) < off) |
| 4511 | break; |
| 4512 | |
| 4513 | /* |
| 4514 | * get_extent may return an extent that starts before our |
| 4515 | * requested range. We have to make sure the ranges |
| 4516 | * we return to fiemap always move forward and don't |
| 4517 | * overlap, so adjust the offsets here |
| 4518 | */ |
| 4519 | em_start = max(em->start, off); |
| 4520 | |
| 4521 | /* |
| 4522 | * record the offset from the start of the extent |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4523 | * for adjusting the disk offset below. Only do this if the |
| 4524 | * extent isn't compressed since our in ram offset may be past |
| 4525 | * what we have actually allocated on disk. |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4526 | */ |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4527 | if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 4528 | offset_in_extent = em_start - em->start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4529 | em_end = extent_map_end(em); |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4530 | em_len = em_end - em_start; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4531 | disko = 0; |
| 4532 | flags = 0; |
| 4533 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4534 | /* |
| 4535 | * bump off for our next call to get_extent |
| 4536 | */ |
| 4537 | off = extent_map_end(em); |
| 4538 | if (off >= max) |
| 4539 | end = 1; |
| 4540 | |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4541 | if (em->block_start == EXTENT_MAP_LAST_BYTE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4542 | end = 1; |
| 4543 | flags |= FIEMAP_EXTENT_LAST; |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4544 | } else if (em->block_start == EXTENT_MAP_INLINE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4545 | flags |= (FIEMAP_EXTENT_DATA_INLINE | |
| 4546 | FIEMAP_EXTENT_NOT_ALIGNED); |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4547 | } else if (em->block_start == EXTENT_MAP_DELALLOC) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4548 | flags |= (FIEMAP_EXTENT_DELALLOC | |
| 4549 | FIEMAP_EXTENT_UNKNOWN); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4550 | } else if (fieinfo->fi_extents_max) { |
Lu Fengqi | afce772 | 2016-06-13 09:36:46 +0800 | [diff] [blame] | 4551 | struct btrfs_trans_handle *trans; |
| 4552 | |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4553 | u64 bytenr = em->block_start - |
| 4554 | (em->start - em->orig_start); |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4555 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4556 | disko = em->block_start + offset_in_extent; |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4557 | |
| 4558 | /* |
Lu Fengqi | afce772 | 2016-06-13 09:36:46 +0800 | [diff] [blame] | 4559 | * We need a trans handle to get delayed refs |
| 4560 | */ |
| 4561 | trans = btrfs_join_transaction(root); |
| 4562 | /* |
| 4563 | * It's OK if we can't start a trans we can still check |
| 4564 | * from commit_root |
| 4565 | */ |
| 4566 | if (IS_ERR(trans)) |
| 4567 | trans = NULL; |
| 4568 | |
| 4569 | /* |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4570 | * As btrfs supports shared space, this information |
| 4571 | * can be exported to userspace tools via |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4572 | * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0 |
| 4573 | * then we're just getting a count and we can skip the |
| 4574 | * lookup stuff. |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4575 | */ |
Lu Fengqi | afce772 | 2016-06-13 09:36:46 +0800 | [diff] [blame] | 4576 | ret = btrfs_check_shared(trans, root->fs_info, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4577 | root->objectid, |
| 4578 | btrfs_ino(BTRFS_I(inode)), bytenr); |
Lu Fengqi | afce772 | 2016-06-13 09:36:46 +0800 | [diff] [blame] | 4579 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4580 | btrfs_end_transaction(trans); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4581 | if (ret < 0) |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4582 | goto out_free; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4583 | if (ret) |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4584 | flags |= FIEMAP_EXTENT_SHARED; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4585 | ret = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4586 | } |
| 4587 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 4588 | flags |= FIEMAP_EXTENT_ENCODED; |
Josef Bacik | 0d2b237 | 2015-05-19 10:44:04 -0400 | [diff] [blame] | 4589 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 4590 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4591 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4592 | free_extent_map(em); |
| 4593 | em = NULL; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4594 | if ((em_start >= last) || em_len == (u64)-1 || |
| 4595 | (last == (u64)-1 && isize <= em_end)) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4596 | flags |= FIEMAP_EXTENT_LAST; |
| 4597 | end = 1; |
| 4598 | } |
| 4599 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4600 | /* now scan forward to see if this is really the last extent. */ |
| 4601 | em = get_extent_skip_holes(inode, off, last_for_get_extent, |
| 4602 | get_extent); |
| 4603 | if (IS_ERR(em)) { |
| 4604 | ret = PTR_ERR(em); |
| 4605 | goto out; |
| 4606 | } |
| 4607 | if (!em) { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4608 | flags |= FIEMAP_EXTENT_LAST; |
| 4609 | end = 1; |
| 4610 | } |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4611 | ret = fiemap_fill_next_extent(fieinfo, em_start, disko, |
| 4612 | em_len, flags); |
Chengyu Song | 26e726a | 2015-03-24 18:12:56 -0400 | [diff] [blame] | 4613 | if (ret) { |
| 4614 | if (ret == 1) |
| 4615 | ret = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4616 | goto out_free; |
Chengyu Song | 26e726a | 2015-03-24 18:12:56 -0400 | [diff] [blame] | 4617 | } |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4618 | } |
| 4619 | out_free: |
| 4620 | free_extent_map(em); |
| 4621 | out: |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4622 | btrfs_free_path(path); |
Liu Bo | a52f4cd | 2013-05-01 16:23:41 +0000 | [diff] [blame] | 4623 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4624 | &cached_state, GFP_NOFS); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4625 | return ret; |
| 4626 | } |
| 4627 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4628 | static void __free_extent_buffer(struct extent_buffer *eb) |
| 4629 | { |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 4630 | btrfs_leak_debug_del(&eb->leak_list); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4631 | kmem_cache_free(extent_buffer_cache, eb); |
| 4632 | } |
| 4633 | |
Josef Bacik | a26e8c9 | 2014-03-28 17:07:27 -0400 | [diff] [blame] | 4634 | int extent_buffer_under_io(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4635 | { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4636 | return (atomic_read(&eb->io_pages) || |
| 4637 | test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) || |
| 4638 | test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4639 | } |
| 4640 | |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4641 | /* |
| 4642 | * Helper for releasing extent buffer page. |
| 4643 | */ |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4644 | static void btrfs_release_extent_buffer_page(struct extent_buffer *eb) |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4645 | { |
| 4646 | unsigned long index; |
| 4647 | struct page *page; |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4648 | int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4649 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4650 | BUG_ON(extent_buffer_under_io(eb)); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4651 | |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4652 | index = num_extent_pages(eb->start, eb->len); |
| 4653 | if (index == 0) |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4654 | return; |
| 4655 | |
| 4656 | do { |
| 4657 | index--; |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 4658 | page = eb->pages[index]; |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4659 | if (!page) |
| 4660 | continue; |
| 4661 | if (mapped) |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4662 | spin_lock(&page->mapping->private_lock); |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4663 | /* |
| 4664 | * We do this since we'll remove the pages after we've |
| 4665 | * removed the eb from the radix tree, so we could race |
| 4666 | * and have this page now attached to the new eb. So |
| 4667 | * only clear page_private if it's still connected to |
| 4668 | * this eb. |
| 4669 | */ |
| 4670 | if (PagePrivate(page) && |
| 4671 | page->private == (unsigned long)eb) { |
| 4672 | BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
| 4673 | BUG_ON(PageDirty(page)); |
| 4674 | BUG_ON(PageWriteback(page)); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4675 | /* |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4676 | * We need to make sure we haven't be attached |
| 4677 | * to a new eb. |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4678 | */ |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4679 | ClearPagePrivate(page); |
| 4680 | set_page_private(page, 0); |
| 4681 | /* One for the page private */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4682 | put_page(page); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4683 | } |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4684 | |
| 4685 | if (mapped) |
| 4686 | spin_unlock(&page->mapping->private_lock); |
| 4687 | |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 4688 | /* One for when we allocated the page */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4689 | put_page(page); |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4690 | } while (index != 0); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4691 | } |
| 4692 | |
| 4693 | /* |
| 4694 | * Helper for releasing the extent buffer. |
| 4695 | */ |
| 4696 | static inline void btrfs_release_extent_buffer(struct extent_buffer *eb) |
| 4697 | { |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4698 | btrfs_release_extent_buffer_page(eb); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4699 | __free_extent_buffer(eb); |
| 4700 | } |
| 4701 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4702 | static struct extent_buffer * |
| 4703 | __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, |
David Sterba | 23d79d8 | 2014-06-15 02:55:29 +0200 | [diff] [blame] | 4704 | unsigned long len) |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4705 | { |
| 4706 | struct extent_buffer *eb = NULL; |
| 4707 | |
Michal Hocko | d1b5c56 | 2015-08-19 14:17:40 +0200 | [diff] [blame] | 4708 | eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4709 | eb->start = start; |
| 4710 | eb->len = len; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4711 | eb->fs_info = fs_info; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4712 | eb->bflags = 0; |
| 4713 | rwlock_init(&eb->lock); |
| 4714 | atomic_set(&eb->write_locks, 0); |
| 4715 | atomic_set(&eb->read_locks, 0); |
| 4716 | atomic_set(&eb->blocking_readers, 0); |
| 4717 | atomic_set(&eb->blocking_writers, 0); |
| 4718 | atomic_set(&eb->spinning_readers, 0); |
| 4719 | atomic_set(&eb->spinning_writers, 0); |
| 4720 | eb->lock_nested = 0; |
| 4721 | init_waitqueue_head(&eb->write_lock_wq); |
| 4722 | init_waitqueue_head(&eb->read_lock_wq); |
| 4723 | |
| 4724 | btrfs_leak_debug_add(&eb->leak_list, &buffers); |
| 4725 | |
| 4726 | spin_lock_init(&eb->refs_lock); |
| 4727 | atomic_set(&eb->refs, 1); |
| 4728 | atomic_set(&eb->io_pages, 0); |
| 4729 | |
| 4730 | /* |
| 4731 | * Sanity checks, currently the maximum is 64k covered by 16x 4k pages |
| 4732 | */ |
| 4733 | BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE |
| 4734 | > MAX_INLINE_EXTENT_BUFFER_SIZE); |
| 4735 | BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE); |
| 4736 | |
| 4737 | return eb; |
| 4738 | } |
| 4739 | |
| 4740 | struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src) |
| 4741 | { |
| 4742 | unsigned long i; |
| 4743 | struct page *p; |
| 4744 | struct extent_buffer *new; |
| 4745 | unsigned long num_pages = num_extent_pages(src->start, src->len); |
| 4746 | |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4747 | new = __alloc_extent_buffer(src->fs_info, src->start, src->len); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4748 | if (new == NULL) |
| 4749 | return NULL; |
| 4750 | |
| 4751 | for (i = 0; i < num_pages; i++) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 4752 | p = alloc_page(GFP_NOFS); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4753 | if (!p) { |
| 4754 | btrfs_release_extent_buffer(new); |
| 4755 | return NULL; |
| 4756 | } |
| 4757 | attach_extent_buffer_page(new, p); |
| 4758 | WARN_ON(PageDirty(p)); |
| 4759 | SetPageUptodate(p); |
| 4760 | new->pages[i] = p; |
David Sterba | fba1acf | 2016-11-08 17:56:24 +0100 | [diff] [blame] | 4761 | copy_page(page_address(p), page_address(src->pages[i])); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4762 | } |
| 4763 | |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4764 | set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags); |
| 4765 | set_bit(EXTENT_BUFFER_DUMMY, &new->bflags); |
| 4766 | |
| 4767 | return new; |
| 4768 | } |
| 4769 | |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4770 | struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, |
| 4771 | u64 start, unsigned long len) |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4772 | { |
| 4773 | struct extent_buffer *eb; |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4774 | unsigned long num_pages; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4775 | unsigned long i; |
| 4776 | |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4777 | num_pages = num_extent_pages(start, len); |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4778 | |
| 4779 | eb = __alloc_extent_buffer(fs_info, start, len); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4780 | if (!eb) |
| 4781 | return NULL; |
| 4782 | |
| 4783 | for (i = 0; i < num_pages; i++) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 4784 | eb->pages[i] = alloc_page(GFP_NOFS); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4785 | if (!eb->pages[i]) |
| 4786 | goto err; |
| 4787 | } |
| 4788 | set_extent_buffer_uptodate(eb); |
| 4789 | btrfs_set_header_nritems(eb, 0); |
| 4790 | set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags); |
| 4791 | |
| 4792 | return eb; |
| 4793 | err: |
| 4794 | for (; i > 0; i--) |
| 4795 | __free_page(eb->pages[i - 1]); |
| 4796 | __free_extent_buffer(eb); |
| 4797 | return NULL; |
| 4798 | } |
| 4799 | |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4800 | 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] | 4801 | u64 start) |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4802 | { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4803 | return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize); |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4804 | } |
| 4805 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4806 | static void check_buffer_tree_ref(struct extent_buffer *eb) |
| 4807 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4808 | int refs; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4809 | /* the ref bit is tricky. We have to make sure it is set |
| 4810 | * if we have the buffer dirty. Otherwise the |
| 4811 | * code to free a buffer can end up dropping a dirty |
| 4812 | * page |
| 4813 | * |
| 4814 | * Once the ref bit is set, it won't go away while the |
| 4815 | * buffer is dirty or in writeback, and it also won't |
| 4816 | * go away while we have the reference count on the |
| 4817 | * eb bumped. |
| 4818 | * |
| 4819 | * We can't just set the ref bit without bumping the |
| 4820 | * ref on the eb because free_extent_buffer might |
| 4821 | * see the ref bit and try to clear it. If this happens |
| 4822 | * free_extent_buffer might end up dropping our original |
| 4823 | * ref by mistake and freeing the page before we are able |
| 4824 | * to add one more ref. |
| 4825 | * |
| 4826 | * So bump the ref count first, then set the bit. If someone |
| 4827 | * beat us to it, drop the ref we added. |
| 4828 | */ |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4829 | refs = atomic_read(&eb->refs); |
| 4830 | if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 4831 | return; |
| 4832 | |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4833 | spin_lock(&eb->refs_lock); |
| 4834 | if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4835 | atomic_inc(&eb->refs); |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4836 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4837 | } |
| 4838 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4839 | static void mark_extent_buffer_accessed(struct extent_buffer *eb, |
| 4840 | struct page *accessed) |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4841 | { |
| 4842 | unsigned long num_pages, i; |
| 4843 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4844 | check_buffer_tree_ref(eb); |
| 4845 | |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4846 | num_pages = num_extent_pages(eb->start, eb->len); |
| 4847 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 4848 | struct page *p = eb->pages[i]; |
| 4849 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4850 | if (p != accessed) |
| 4851 | mark_page_accessed(p); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4852 | } |
| 4853 | } |
| 4854 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4855 | struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info, |
| 4856 | u64 start) |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4857 | { |
| 4858 | struct extent_buffer *eb; |
| 4859 | |
| 4860 | rcu_read_lock(); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4861 | eb = radix_tree_lookup(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4862 | start >> PAGE_SHIFT); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4863 | if (eb && atomic_inc_not_zero(&eb->refs)) { |
| 4864 | rcu_read_unlock(); |
Filipe Manana | 062c19e | 2015-04-23 11:28:48 +0100 | [diff] [blame] | 4865 | /* |
| 4866 | * Lock our eb's refs_lock to avoid races with |
| 4867 | * free_extent_buffer. When we get our eb it might be flagged |
| 4868 | * with EXTENT_BUFFER_STALE and another task running |
| 4869 | * free_extent_buffer might have seen that flag set, |
| 4870 | * eb->refs == 2, that the buffer isn't under IO (dirty and |
| 4871 | * writeback flags not set) and it's still in the tree (flag |
| 4872 | * EXTENT_BUFFER_TREE_REF set), therefore being in the process |
| 4873 | * of decrementing the extent buffer's reference count twice. |
| 4874 | * So here we could race and increment the eb's reference count, |
| 4875 | * clear its stale flag, mark it as dirty and drop our reference |
| 4876 | * before the other task finishes executing free_extent_buffer, |
| 4877 | * which would later result in an attempt to free an extent |
| 4878 | * buffer that is dirty. |
| 4879 | */ |
| 4880 | if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) { |
| 4881 | spin_lock(&eb->refs_lock); |
| 4882 | spin_unlock(&eb->refs_lock); |
| 4883 | } |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4884 | mark_extent_buffer_accessed(eb, NULL); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4885 | return eb; |
| 4886 | } |
| 4887 | rcu_read_unlock(); |
| 4888 | |
| 4889 | return NULL; |
| 4890 | } |
| 4891 | |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4892 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 4893 | 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] | 4894 | u64 start) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4895 | { |
| 4896 | struct extent_buffer *eb, *exists = NULL; |
| 4897 | int ret; |
| 4898 | |
| 4899 | eb = find_extent_buffer(fs_info, start); |
| 4900 | if (eb) |
| 4901 | return eb; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4902 | eb = alloc_dummy_extent_buffer(fs_info, start); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4903 | if (!eb) |
| 4904 | return NULL; |
| 4905 | eb->fs_info = fs_info; |
| 4906 | again: |
David Sterba | e1860a7 | 2016-05-09 14:11:38 +0200 | [diff] [blame] | 4907 | ret = radix_tree_preload(GFP_NOFS); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4908 | if (ret) |
| 4909 | goto free_eb; |
| 4910 | spin_lock(&fs_info->buffer_lock); |
| 4911 | ret = radix_tree_insert(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4912 | start >> PAGE_SHIFT, eb); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4913 | spin_unlock(&fs_info->buffer_lock); |
| 4914 | radix_tree_preload_end(); |
| 4915 | if (ret == -EEXIST) { |
| 4916 | exists = find_extent_buffer(fs_info, start); |
| 4917 | if (exists) |
| 4918 | goto free_eb; |
| 4919 | else |
| 4920 | goto again; |
| 4921 | } |
| 4922 | check_buffer_tree_ref(eb); |
| 4923 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
| 4924 | |
| 4925 | /* |
| 4926 | * We will free dummy extent buffer's if they come into |
| 4927 | * free_extent_buffer with a ref count of 2, but if we are using this we |
| 4928 | * want the buffers to stay in memory until we're done with them, so |
| 4929 | * bump the ref count again. |
| 4930 | */ |
| 4931 | atomic_inc(&eb->refs); |
| 4932 | return eb; |
| 4933 | free_eb: |
| 4934 | btrfs_release_extent_buffer(eb); |
| 4935 | return exists; |
| 4936 | } |
| 4937 | #endif |
| 4938 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4939 | struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, |
David Sterba | ce3e698 | 2014-06-15 03:00:04 +0200 | [diff] [blame] | 4940 | u64 start) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4941 | { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4942 | unsigned long len = fs_info->nodesize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4943 | unsigned long num_pages = num_extent_pages(start, len); |
| 4944 | unsigned long i; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4945 | unsigned long index = start >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4946 | struct extent_buffer *eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4947 | struct extent_buffer *exists = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4948 | struct page *p; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4949 | struct address_space *mapping = fs_info->btree_inode->i_mapping; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4950 | int uptodate = 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4951 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4952 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4953 | if (!IS_ALIGNED(start, fs_info->sectorsize)) { |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4954 | btrfs_err(fs_info, "bad tree block start %llu", start); |
| 4955 | return ERR_PTR(-EINVAL); |
| 4956 | } |
| 4957 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4958 | eb = find_extent_buffer(fs_info, start); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4959 | if (eb) |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4960 | return eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4961 | |
David Sterba | 23d79d8 | 2014-06-15 02:55:29 +0200 | [diff] [blame] | 4962 | eb = __alloc_extent_buffer(fs_info, start, len); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 4963 | if (!eb) |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4964 | return ERR_PTR(-ENOMEM); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4965 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4966 | for (i = 0; i < num_pages; i++, index++) { |
Michal Hocko | d1b5c56 | 2015-08-19 14:17:40 +0200 | [diff] [blame] | 4967 | p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4968 | if (!p) { |
| 4969 | exists = ERR_PTR(-ENOMEM); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4970 | goto free_eb; |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4971 | } |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4972 | |
| 4973 | spin_lock(&mapping->private_lock); |
| 4974 | if (PagePrivate(p)) { |
| 4975 | /* |
| 4976 | * We could have already allocated an eb for this page |
| 4977 | * and attached one so lets see if we can get a ref on |
| 4978 | * the existing eb, and if we can we know it's good and |
| 4979 | * we can just return that one, else we know we can just |
| 4980 | * overwrite page->private. |
| 4981 | */ |
| 4982 | exists = (struct extent_buffer *)p->private; |
| 4983 | if (atomic_inc_not_zero(&exists->refs)) { |
| 4984 | spin_unlock(&mapping->private_lock); |
| 4985 | unlock_page(p); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4986 | put_page(p); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4987 | mark_extent_buffer_accessed(exists, p); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4988 | goto free_eb; |
| 4989 | } |
Omar Sandoval | 5ca64f4 | 2015-02-24 02:47:05 -0800 | [diff] [blame] | 4990 | exists = NULL; |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4991 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4992 | /* |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4993 | * Do this so attach doesn't complain and we need to |
| 4994 | * drop the ref the old guy had. |
| 4995 | */ |
| 4996 | ClearPagePrivate(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4997 | WARN_ON(PageDirty(p)); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4998 | put_page(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4999 | } |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 5000 | attach_extent_buffer_page(eb, p); |
| 5001 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5002 | WARN_ON(PageDirty(p)); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5003 | eb->pages[i] = p; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5004 | if (!PageUptodate(p)) |
| 5005 | uptodate = 0; |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5006 | |
| 5007 | /* |
| 5008 | * see below about how we avoid a nasty race with release page |
| 5009 | * and why we unlock later |
| 5010 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5011 | } |
| 5012 | if (uptodate) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5013 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 5014 | again: |
David Sterba | e1860a7 | 2016-05-09 14:11:38 +0200 | [diff] [blame] | 5015 | ret = radix_tree_preload(GFP_NOFS); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 5016 | if (ret) { |
| 5017 | exists = ERR_PTR(ret); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5018 | goto free_eb; |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 5019 | } |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5020 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5021 | spin_lock(&fs_info->buffer_lock); |
| 5022 | ret = radix_tree_insert(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5023 | start >> PAGE_SHIFT, eb); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5024 | spin_unlock(&fs_info->buffer_lock); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 5025 | radix_tree_preload_end(); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5026 | if (ret == -EEXIST) { |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5027 | exists = find_extent_buffer(fs_info, start); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 5028 | if (exists) |
| 5029 | goto free_eb; |
| 5030 | else |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 5031 | goto again; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5032 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5033 | /* add one reference for the tree */ |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5034 | check_buffer_tree_ref(eb); |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 5035 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5036 | |
| 5037 | /* |
| 5038 | * there is a race where release page may have |
| 5039 | * tried to find this extent buffer in the radix |
| 5040 | * but failed. It will tell the VM it is safe to |
| 5041 | * reclaim the, and it will clear the page private bit. |
| 5042 | * We must make sure to set the page private bit properly |
| 5043 | * after the extent buffer is in the radix tree so |
| 5044 | * it doesn't get lost |
| 5045 | */ |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5046 | SetPageChecked(eb->pages[0]); |
| 5047 | for (i = 1; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5048 | p = eb->pages[i]; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5049 | ClearPageChecked(p); |
| 5050 | unlock_page(p); |
| 5051 | } |
| 5052 | unlock_page(eb->pages[0]); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5053 | return eb; |
| 5054 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5055 | free_eb: |
Omar Sandoval | 5ca64f4 | 2015-02-24 02:47:05 -0800 | [diff] [blame] | 5056 | WARN_ON(!atomic_dec_and_test(&eb->refs)); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5057 | for (i = 0; i < num_pages; i++) { |
| 5058 | if (eb->pages[i]) |
| 5059 | unlock_page(eb->pages[i]); |
| 5060 | } |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5061 | |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5062 | btrfs_release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5063 | return exists; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5064 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5065 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5066 | static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head) |
| 5067 | { |
| 5068 | struct extent_buffer *eb = |
| 5069 | container_of(head, struct extent_buffer, rcu_head); |
| 5070 | |
| 5071 | __free_extent_buffer(eb); |
| 5072 | } |
| 5073 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5074 | /* Expects to have eb->eb_lock already held */ |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5075 | static int release_extent_buffer(struct extent_buffer *eb) |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5076 | { |
| 5077 | WARN_ON(atomic_read(&eb->refs) == 0); |
| 5078 | if (atomic_dec_and_test(&eb->refs)) { |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 5079 | if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) { |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5080 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5081 | |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 5082 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5083 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5084 | spin_lock(&fs_info->buffer_lock); |
| 5085 | radix_tree_delete(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5086 | eb->start >> PAGE_SHIFT); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5087 | spin_unlock(&fs_info->buffer_lock); |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 5088 | } else { |
| 5089 | spin_unlock(&eb->refs_lock); |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 5090 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5091 | |
| 5092 | /* Should be safe to release our pages at this point */ |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 5093 | btrfs_release_extent_buffer_page(eb); |
Josef Bacik | bcb7e44 | 2015-03-16 17:38:02 -0400 | [diff] [blame] | 5094 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 5095 | if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) { |
| 5096 | __free_extent_buffer(eb); |
| 5097 | return 1; |
| 5098 | } |
| 5099 | #endif |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5100 | call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 5101 | return 1; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5102 | } |
| 5103 | spin_unlock(&eb->refs_lock); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 5104 | |
| 5105 | return 0; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5106 | } |
| 5107 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5108 | void free_extent_buffer(struct extent_buffer *eb) |
| 5109 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 5110 | int refs; |
| 5111 | int old; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5112 | if (!eb) |
| 5113 | return; |
| 5114 | |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 5115 | while (1) { |
| 5116 | refs = atomic_read(&eb->refs); |
| 5117 | if (refs <= 3) |
| 5118 | break; |
| 5119 | old = atomic_cmpxchg(&eb->refs, refs, refs - 1); |
| 5120 | if (old == refs) |
| 5121 | return; |
| 5122 | } |
| 5123 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5124 | spin_lock(&eb->refs_lock); |
| 5125 | if (atomic_read(&eb->refs) == 2 && |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 5126 | test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) |
| 5127 | atomic_dec(&eb->refs); |
| 5128 | |
| 5129 | if (atomic_read(&eb->refs) == 2 && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5130 | test_bit(EXTENT_BUFFER_STALE, &eb->bflags) && |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5131 | !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5132 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 5133 | atomic_dec(&eb->refs); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5134 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5135 | /* |
| 5136 | * I know this is terrible, but it's temporary until we stop tracking |
| 5137 | * the uptodate bits and such for the extent buffers. |
| 5138 | */ |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5139 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5140 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5141 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5142 | void free_extent_buffer_stale(struct extent_buffer *eb) |
| 5143 | { |
| 5144 | if (!eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5145 | return; |
| 5146 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5147 | spin_lock(&eb->refs_lock); |
| 5148 | set_bit(EXTENT_BUFFER_STALE, &eb->bflags); |
| 5149 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5150 | if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5151 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 5152 | atomic_dec(&eb->refs); |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5153 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5154 | } |
| 5155 | |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 5156 | void clear_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5157 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5158 | unsigned long i; |
| 5159 | unsigned long num_pages; |
| 5160 | struct page *page; |
| 5161 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5162 | num_pages = num_extent_pages(eb->start, eb->len); |
| 5163 | |
| 5164 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5165 | page = eb->pages[i]; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5166 | if (!PageDirty(page)) |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 5167 | continue; |
| 5168 | |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 5169 | lock_page(page); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5170 | WARN_ON(!PagePrivate(page)); |
| 5171 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5172 | clear_page_dirty_for_io(page); |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 5173 | spin_lock_irq(&page->mapping->tree_lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5174 | if (!PageDirty(page)) { |
| 5175 | radix_tree_tag_clear(&page->mapping->page_tree, |
| 5176 | page_index(page), |
| 5177 | PAGECACHE_TAG_DIRTY); |
| 5178 | } |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 5179 | spin_unlock_irq(&page->mapping->tree_lock); |
Chris Mason | bf0da8c | 2011-11-04 12:29:37 -0400 | [diff] [blame] | 5180 | ClearPageError(page); |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 5181 | unlock_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5182 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5183 | WARN_ON(atomic_read(&eb->refs) == 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5184 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5185 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5186 | int set_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5187 | { |
| 5188 | unsigned long i; |
| 5189 | unsigned long num_pages; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5190 | int was_dirty = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5191 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5192 | check_buffer_tree_ref(eb); |
| 5193 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5194 | was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5195 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5196 | num_pages = num_extent_pages(eb->start, eb->len); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5197 | WARN_ON(atomic_read(&eb->refs) == 0); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5198 | WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)); |
| 5199 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5200 | for (i = 0; i < num_pages; i++) |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5201 | set_page_dirty(eb->pages[i]); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5202 | return was_dirty; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5203 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5204 | |
David Sterba | 69ba392 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 5205 | void clear_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5206 | { |
| 5207 | unsigned long i; |
| 5208 | struct page *page; |
| 5209 | unsigned long num_pages; |
| 5210 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5211 | clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5212 | num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5213 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5214 | page = eb->pages[i]; |
Chris Mason | 33958dc | 2008-07-30 10:29:12 -0400 | [diff] [blame] | 5215 | if (page) |
| 5216 | ClearPageUptodate(page); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5217 | } |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5218 | } |
| 5219 | |
David Sterba | 09c25a8 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 5220 | void set_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5221 | { |
| 5222 | unsigned long i; |
| 5223 | struct page *page; |
| 5224 | unsigned long num_pages; |
| 5225 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5226 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5227 | num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5228 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5229 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5230 | SetPageUptodate(page); |
| 5231 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5232 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5233 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5234 | int extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5235 | { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5236 | return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5237 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5238 | |
| 5239 | int read_extent_buffer_pages(struct extent_io_tree *tree, |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5240 | struct extent_buffer *eb, int wait, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5241 | get_extent_t *get_extent, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5242 | { |
| 5243 | unsigned long i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5244 | struct page *page; |
| 5245 | int err; |
| 5246 | int ret = 0; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5247 | int locked_pages = 0; |
| 5248 | int all_uptodate = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5249 | unsigned long num_pages; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5250 | unsigned long num_reads = 0; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5251 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5252 | unsigned long bio_flags = 0; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5253 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5254 | if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5255 | return 0; |
| 5256 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5257 | num_pages = num_extent_pages(eb->start, eb->len); |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5258 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5259 | page = eb->pages[i]; |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 5260 | if (wait == WAIT_NONE) { |
David Woodhouse | 2db0496 | 2008-08-07 11:19:43 -0400 | [diff] [blame] | 5261 | if (!trylock_page(page)) |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5262 | goto unlock_exit; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5263 | } else { |
| 5264 | lock_page(page); |
| 5265 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5266 | locked_pages++; |
Liu Bo | 2571e73 | 2016-08-03 12:33:01 -0700 | [diff] [blame] | 5267 | } |
| 5268 | /* |
| 5269 | * We need to firstly lock all pages to make sure that |
| 5270 | * the uptodate bit of our pages won't be affected by |
| 5271 | * clear_extent_buffer_uptodate(). |
| 5272 | */ |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5273 | for (i = 0; i < num_pages; i++) { |
Liu Bo | 2571e73 | 2016-08-03 12:33:01 -0700 | [diff] [blame] | 5274 | page = eb->pages[i]; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5275 | if (!PageUptodate(page)) { |
| 5276 | num_reads++; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5277 | all_uptodate = 0; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5278 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5279 | } |
Liu Bo | 2571e73 | 2016-08-03 12:33:01 -0700 | [diff] [blame] | 5280 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5281 | if (all_uptodate) { |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5282 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5283 | goto unlock_exit; |
| 5284 | } |
| 5285 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 5286 | clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 5287 | eb->read_mirror = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5288 | atomic_set(&eb->io_pages, num_reads); |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5289 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5290 | page = eb->pages[i]; |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5291 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5292 | if (!PageUptodate(page)) { |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5293 | if (ret) { |
| 5294 | atomic_dec(&eb->io_pages); |
| 5295 | unlock_page(page); |
| 5296 | continue; |
| 5297 | } |
| 5298 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5299 | ClearPageError(page); |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5300 | err = __extent_read_full_page(tree, page, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5301 | get_extent, &bio, |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 5302 | mirror_num, &bio_flags, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 5303 | REQ_META); |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5304 | if (err) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5305 | ret = err; |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5306 | /* |
| 5307 | * We use &bio in above __extent_read_full_page, |
| 5308 | * so we ensure that if it returns error, the |
| 5309 | * current page fails to add itself to bio and |
| 5310 | * it's been unlocked. |
| 5311 | * |
| 5312 | * We must dec io_pages by ourselves. |
| 5313 | */ |
| 5314 | atomic_dec(&eb->io_pages); |
| 5315 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5316 | } else { |
| 5317 | unlock_page(page); |
| 5318 | } |
| 5319 | } |
| 5320 | |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 5321 | if (bio) { |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 5322 | err = submit_one_bio(bio, mirror_num, bio_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5323 | if (err) |
| 5324 | return err; |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 5325 | } |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5326 | |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 5327 | if (ret || wait != WAIT_COMPLETE) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5328 | return ret; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5329 | |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5330 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5331 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5332 | wait_on_page_locked(page); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5333 | if (!PageUptodate(page)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5334 | ret = -EIO; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5335 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5336 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5337 | return ret; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5338 | |
| 5339 | unlock_exit: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5340 | while (locked_pages > 0) { |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5341 | locked_pages--; |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5342 | page = eb->pages[locked_pages]; |
| 5343 | unlock_page(page); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5344 | } |
| 5345 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5346 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5347 | |
| 5348 | void read_extent_buffer(struct extent_buffer *eb, void *dstv, |
| 5349 | unsigned long start, |
| 5350 | unsigned long len) |
| 5351 | { |
| 5352 | size_t cur; |
| 5353 | size_t offset; |
| 5354 | struct page *page; |
| 5355 | char *kaddr; |
| 5356 | char *dst = (char *)dstv; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5357 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5358 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5359 | |
| 5360 | WARN_ON(start > eb->len); |
| 5361 | WARN_ON(start + len > eb->start + eb->len); |
| 5362 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5363 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5364 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5365 | while (len > 0) { |
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 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5368 | cur = min(len, (PAGE_SIZE - offset)); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5369 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5370 | memcpy(dst, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5371 | |
| 5372 | dst += cur; |
| 5373 | len -= cur; |
| 5374 | offset = 0; |
| 5375 | i++; |
| 5376 | } |
| 5377 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5378 | |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5379 | int read_extent_buffer_to_user(struct extent_buffer *eb, void __user *dstv, |
| 5380 | unsigned long start, |
| 5381 | unsigned long len) |
| 5382 | { |
| 5383 | size_t cur; |
| 5384 | size_t offset; |
| 5385 | struct page *page; |
| 5386 | char *kaddr; |
| 5387 | char __user *dst = (char __user *)dstv; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5388 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5389 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5390 | int ret = 0; |
| 5391 | |
| 5392 | WARN_ON(start > eb->len); |
| 5393 | WARN_ON(start + len > eb->start + eb->len); |
| 5394 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5395 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5396 | |
| 5397 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5398 | page = eb->pages[i]; |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5399 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5400 | cur = min(len, (PAGE_SIZE - offset)); |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5401 | kaddr = page_address(page); |
| 5402 | if (copy_to_user(dst, kaddr + offset, cur)) { |
| 5403 | ret = -EFAULT; |
| 5404 | break; |
| 5405 | } |
| 5406 | |
| 5407 | dst += cur; |
| 5408 | len -= cur; |
| 5409 | offset = 0; |
| 5410 | i++; |
| 5411 | } |
| 5412 | |
| 5413 | return ret; |
| 5414 | } |
| 5415 | |
Liu Bo | 415b35a | 2016-06-17 19:16:21 -0700 | [diff] [blame] | 5416 | /* |
| 5417 | * return 0 if the item is found within a page. |
| 5418 | * return 1 if the item spans two pages. |
| 5419 | * return -EINVAL otherwise. |
| 5420 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5421 | int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start, |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5422 | unsigned long min_len, char **map, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5423 | unsigned long *map_start, |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5424 | unsigned long *map_len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5425 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5426 | size_t offset = start & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5427 | char *kaddr; |
| 5428 | struct page *p; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5429 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5430 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5431 | unsigned long end_i = (start_offset + start + min_len - 1) >> |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5432 | PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5433 | |
| 5434 | if (i != end_i) |
Liu Bo | 415b35a | 2016-06-17 19:16:21 -0700 | [diff] [blame] | 5435 | return 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5436 | |
| 5437 | if (i == 0) { |
| 5438 | offset = start_offset; |
| 5439 | *map_start = 0; |
| 5440 | } else { |
| 5441 | offset = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5442 | *map_start = ((u64)i << PAGE_SHIFT) - start_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5443 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5444 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5445 | if (start + min_len > eb->len) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5446 | WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 5447 | eb->start, eb->len, start, min_len); |
Josef Bacik | 85026533 | 2011-03-15 14:52:12 -0400 | [diff] [blame] | 5448 | return -EINVAL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5449 | } |
| 5450 | |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5451 | p = eb->pages[i]; |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5452 | kaddr = page_address(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5453 | *map = kaddr + offset; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5454 | *map_len = PAGE_SIZE - offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5455 | return 0; |
| 5456 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5457 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5458 | int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv, |
| 5459 | unsigned long start, |
| 5460 | unsigned long len) |
| 5461 | { |
| 5462 | size_t cur; |
| 5463 | size_t offset; |
| 5464 | struct page *page; |
| 5465 | char *kaddr; |
| 5466 | char *ptr = (char *)ptrv; |
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 | int ret = 0; |
| 5470 | |
| 5471 | WARN_ON(start > eb->len); |
| 5472 | WARN_ON(start + len > eb->start + eb->len); |
| 5473 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5474 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5475 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5476 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5477 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5478 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5479 | cur = min(len, (PAGE_SIZE - offset)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5480 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5481 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5482 | ret = memcmp(ptr, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5483 | if (ret) |
| 5484 | break; |
| 5485 | |
| 5486 | ptr += cur; |
| 5487 | len -= cur; |
| 5488 | offset = 0; |
| 5489 | i++; |
| 5490 | } |
| 5491 | return ret; |
| 5492 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5493 | |
David Sterba | f157bf7 | 2016-11-09 17:43:38 +0100 | [diff] [blame] | 5494 | void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb, |
| 5495 | const void *srcv) |
| 5496 | { |
| 5497 | char *kaddr; |
| 5498 | |
| 5499 | WARN_ON(!PageUptodate(eb->pages[0])); |
| 5500 | kaddr = page_address(eb->pages[0]); |
| 5501 | memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv, |
| 5502 | BTRFS_FSID_SIZE); |
| 5503 | } |
| 5504 | |
| 5505 | void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv) |
| 5506 | { |
| 5507 | char *kaddr; |
| 5508 | |
| 5509 | WARN_ON(!PageUptodate(eb->pages[0])); |
| 5510 | kaddr = page_address(eb->pages[0]); |
| 5511 | memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv, |
| 5512 | BTRFS_FSID_SIZE); |
| 5513 | } |
| 5514 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5515 | void write_extent_buffer(struct extent_buffer *eb, const void *srcv, |
| 5516 | unsigned long start, unsigned long len) |
| 5517 | { |
| 5518 | size_t cur; |
| 5519 | size_t offset; |
| 5520 | struct page *page; |
| 5521 | char *kaddr; |
| 5522 | char *src = (char *)srcv; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5523 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5524 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5525 | |
| 5526 | WARN_ON(start > eb->len); |
| 5527 | WARN_ON(start + len > eb->start + eb->len); |
| 5528 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5529 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5530 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5531 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5532 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5533 | WARN_ON(!PageUptodate(page)); |
| 5534 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5535 | cur = min(len, PAGE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5536 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5537 | memcpy(kaddr + offset, src, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5538 | |
| 5539 | src += cur; |
| 5540 | len -= cur; |
| 5541 | offset = 0; |
| 5542 | i++; |
| 5543 | } |
| 5544 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5545 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 5546 | void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start, |
| 5547 | unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5548 | { |
| 5549 | size_t cur; |
| 5550 | size_t offset; |
| 5551 | struct page *page; |
| 5552 | char *kaddr; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5553 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5554 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5555 | |
| 5556 | WARN_ON(start > eb->len); |
| 5557 | WARN_ON(start + len > eb->start + eb->len); |
| 5558 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5559 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5560 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5561 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5562 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5563 | WARN_ON(!PageUptodate(page)); |
| 5564 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5565 | cur = min(len, PAGE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5566 | kaddr = page_address(page); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 5567 | memset(kaddr + offset, 0, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5568 | |
| 5569 | len -= cur; |
| 5570 | offset = 0; |
| 5571 | i++; |
| 5572 | } |
| 5573 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5574 | |
David Sterba | 58e8012 | 2016-11-08 18:30:31 +0100 | [diff] [blame] | 5575 | void copy_extent_buffer_full(struct extent_buffer *dst, |
| 5576 | struct extent_buffer *src) |
| 5577 | { |
| 5578 | int i; |
| 5579 | unsigned num_pages; |
| 5580 | |
| 5581 | ASSERT(dst->len == src->len); |
| 5582 | |
| 5583 | num_pages = num_extent_pages(dst->start, dst->len); |
| 5584 | for (i = 0; i < num_pages; i++) |
| 5585 | copy_page(page_address(dst->pages[i]), |
| 5586 | page_address(src->pages[i])); |
| 5587 | } |
| 5588 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5589 | void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src, |
| 5590 | unsigned long dst_offset, unsigned long src_offset, |
| 5591 | unsigned long len) |
| 5592 | { |
| 5593 | u64 dst_len = dst->len; |
| 5594 | size_t cur; |
| 5595 | size_t offset; |
| 5596 | struct page *page; |
| 5597 | char *kaddr; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5598 | size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1); |
| 5599 | unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5600 | |
| 5601 | WARN_ON(src->len != dst_len); |
| 5602 | |
| 5603 | offset = (start_offset + dst_offset) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5604 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5605 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5606 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5607 | page = dst->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5608 | WARN_ON(!PageUptodate(page)); |
| 5609 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5610 | cur = min(len, (unsigned long)(PAGE_SIZE - offset)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5611 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5612 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5613 | read_extent_buffer(src, kaddr + offset, src_offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5614 | |
| 5615 | src_offset += cur; |
| 5616 | len -= cur; |
| 5617 | offset = 0; |
| 5618 | i++; |
| 5619 | } |
| 5620 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5621 | |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5622 | void le_bitmap_set(u8 *map, unsigned int start, int len) |
| 5623 | { |
| 5624 | u8 *p = map + BIT_BYTE(start); |
| 5625 | const unsigned int size = start + len; |
| 5626 | int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE); |
| 5627 | u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start); |
| 5628 | |
| 5629 | while (len - bits_to_set >= 0) { |
| 5630 | *p |= mask_to_set; |
| 5631 | len -= bits_to_set; |
| 5632 | bits_to_set = BITS_PER_BYTE; |
Dan Carpenter | 9c89469 | 2016-10-12 11:33:21 +0300 | [diff] [blame] | 5633 | mask_to_set = ~0; |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5634 | p++; |
| 5635 | } |
| 5636 | if (len) { |
| 5637 | mask_to_set &= BITMAP_LAST_BYTE_MASK(size); |
| 5638 | *p |= mask_to_set; |
| 5639 | } |
| 5640 | } |
| 5641 | |
| 5642 | void le_bitmap_clear(u8 *map, unsigned int start, int len) |
| 5643 | { |
| 5644 | u8 *p = map + BIT_BYTE(start); |
| 5645 | const unsigned int size = start + len; |
| 5646 | int bits_to_clear = BITS_PER_BYTE - (start % BITS_PER_BYTE); |
| 5647 | u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(start); |
| 5648 | |
| 5649 | while (len - bits_to_clear >= 0) { |
| 5650 | *p &= ~mask_to_clear; |
| 5651 | len -= bits_to_clear; |
| 5652 | bits_to_clear = BITS_PER_BYTE; |
Dan Carpenter | 9c89469 | 2016-10-12 11:33:21 +0300 | [diff] [blame] | 5653 | mask_to_clear = ~0; |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5654 | p++; |
| 5655 | } |
| 5656 | if (len) { |
| 5657 | mask_to_clear &= BITMAP_LAST_BYTE_MASK(size); |
| 5658 | *p &= ~mask_to_clear; |
| 5659 | } |
| 5660 | } |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5661 | |
| 5662 | /* |
| 5663 | * eb_bitmap_offset() - calculate the page and offset of the byte containing the |
| 5664 | * given bit number |
| 5665 | * @eb: the extent buffer |
| 5666 | * @start: offset of the bitmap item in the extent buffer |
| 5667 | * @nr: bit number |
| 5668 | * @page_index: return index of the page in the extent buffer that contains the |
| 5669 | * given bit number |
| 5670 | * @page_offset: return offset into the page given by page_index |
| 5671 | * |
| 5672 | * This helper hides the ugliness of finding the byte in an extent buffer which |
| 5673 | * contains a given bit. |
| 5674 | */ |
| 5675 | static inline void eb_bitmap_offset(struct extent_buffer *eb, |
| 5676 | unsigned long start, unsigned long nr, |
| 5677 | unsigned long *page_index, |
| 5678 | size_t *page_offset) |
| 5679 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5680 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5681 | size_t byte_offset = BIT_BYTE(nr); |
| 5682 | size_t offset; |
| 5683 | |
| 5684 | /* |
| 5685 | * The byte we want is the offset of the extent buffer + the offset of |
| 5686 | * the bitmap item in the extent buffer + the offset of the byte in the |
| 5687 | * bitmap item. |
| 5688 | */ |
| 5689 | offset = start_offset + start + byte_offset; |
| 5690 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5691 | *page_index = offset >> PAGE_SHIFT; |
| 5692 | *page_offset = offset & (PAGE_SIZE - 1); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5693 | } |
| 5694 | |
| 5695 | /** |
| 5696 | * extent_buffer_test_bit - determine whether a bit in a bitmap item is set |
| 5697 | * @eb: the extent buffer |
| 5698 | * @start: offset of the bitmap item in the extent buffer |
| 5699 | * @nr: bit number to test |
| 5700 | */ |
| 5701 | int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start, |
| 5702 | unsigned long nr) |
| 5703 | { |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5704 | u8 *kaddr; |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5705 | struct page *page; |
| 5706 | unsigned long i; |
| 5707 | size_t offset; |
| 5708 | |
| 5709 | eb_bitmap_offset(eb, start, nr, &i, &offset); |
| 5710 | page = eb->pages[i]; |
| 5711 | WARN_ON(!PageUptodate(page)); |
| 5712 | kaddr = page_address(page); |
| 5713 | return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1))); |
| 5714 | } |
| 5715 | |
| 5716 | /** |
| 5717 | * extent_buffer_bitmap_set - set an area of a bitmap |
| 5718 | * @eb: the extent buffer |
| 5719 | * @start: offset of the bitmap item in the extent buffer |
| 5720 | * @pos: bit number of the first bit |
| 5721 | * @len: number of bits to set |
| 5722 | */ |
| 5723 | void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start, |
| 5724 | unsigned long pos, unsigned long len) |
| 5725 | { |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5726 | u8 *kaddr; |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5727 | struct page *page; |
| 5728 | unsigned long i; |
| 5729 | size_t offset; |
| 5730 | const unsigned int size = pos + len; |
| 5731 | int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE); |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5732 | u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5733 | |
| 5734 | eb_bitmap_offset(eb, start, pos, &i, &offset); |
| 5735 | page = eb->pages[i]; |
| 5736 | WARN_ON(!PageUptodate(page)); |
| 5737 | kaddr = page_address(page); |
| 5738 | |
| 5739 | while (len >= bits_to_set) { |
| 5740 | kaddr[offset] |= mask_to_set; |
| 5741 | len -= bits_to_set; |
| 5742 | bits_to_set = BITS_PER_BYTE; |
Dan Carpenter | 9c89469 | 2016-10-12 11:33:21 +0300 | [diff] [blame] | 5743 | mask_to_set = ~0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5744 | if (++offset >= PAGE_SIZE && len > 0) { |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5745 | offset = 0; |
| 5746 | page = eb->pages[++i]; |
| 5747 | WARN_ON(!PageUptodate(page)); |
| 5748 | kaddr = page_address(page); |
| 5749 | } |
| 5750 | } |
| 5751 | if (len) { |
| 5752 | mask_to_set &= BITMAP_LAST_BYTE_MASK(size); |
| 5753 | kaddr[offset] |= mask_to_set; |
| 5754 | } |
| 5755 | } |
| 5756 | |
| 5757 | |
| 5758 | /** |
| 5759 | * extent_buffer_bitmap_clear - clear an area of a bitmap |
| 5760 | * @eb: the extent buffer |
| 5761 | * @start: offset of the bitmap item in the extent buffer |
| 5762 | * @pos: bit number of the first bit |
| 5763 | * @len: number of bits to clear |
| 5764 | */ |
| 5765 | void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start, |
| 5766 | unsigned long pos, unsigned long len) |
| 5767 | { |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5768 | u8 *kaddr; |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5769 | struct page *page; |
| 5770 | unsigned long i; |
| 5771 | size_t offset; |
| 5772 | const unsigned int size = pos + len; |
| 5773 | int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE); |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5774 | u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5775 | |
| 5776 | eb_bitmap_offset(eb, start, pos, &i, &offset); |
| 5777 | page = eb->pages[i]; |
| 5778 | WARN_ON(!PageUptodate(page)); |
| 5779 | kaddr = page_address(page); |
| 5780 | |
| 5781 | while (len >= bits_to_clear) { |
| 5782 | kaddr[offset] &= ~mask_to_clear; |
| 5783 | len -= bits_to_clear; |
| 5784 | bits_to_clear = BITS_PER_BYTE; |
Dan Carpenter | 9c89469 | 2016-10-12 11:33:21 +0300 | [diff] [blame] | 5785 | mask_to_clear = ~0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5786 | if (++offset >= PAGE_SIZE && len > 0) { |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5787 | offset = 0; |
| 5788 | page = eb->pages[++i]; |
| 5789 | WARN_ON(!PageUptodate(page)); |
| 5790 | kaddr = page_address(page); |
| 5791 | } |
| 5792 | } |
| 5793 | if (len) { |
| 5794 | mask_to_clear &= BITMAP_LAST_BYTE_MASK(size); |
| 5795 | kaddr[offset] &= ~mask_to_clear; |
| 5796 | } |
| 5797 | } |
| 5798 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5799 | static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len) |
| 5800 | { |
| 5801 | unsigned long distance = (src > dst) ? src - dst : dst - src; |
| 5802 | return distance < len; |
| 5803 | } |
| 5804 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5805 | static void copy_pages(struct page *dst_page, struct page *src_page, |
| 5806 | unsigned long dst_off, unsigned long src_off, |
| 5807 | unsigned long len) |
| 5808 | { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5809 | char *dst_kaddr = page_address(dst_page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5810 | char *src_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5811 | int must_memmove = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5812 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5813 | if (dst_page != src_page) { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5814 | src_kaddr = page_address(src_page); |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5815 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5816 | src_kaddr = dst_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5817 | if (areas_overlap(src_off, dst_off, len)) |
| 5818 | must_memmove = 1; |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5819 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5820 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5821 | if (must_memmove) |
| 5822 | memmove(dst_kaddr + dst_off, src_kaddr + src_off, len); |
| 5823 | else |
| 5824 | memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5825 | } |
| 5826 | |
| 5827 | void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 5828 | unsigned long src_offset, unsigned long len) |
| 5829 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5830 | struct btrfs_fs_info *fs_info = dst->fs_info; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5831 | size_t cur; |
| 5832 | size_t dst_off_in_page; |
| 5833 | size_t src_off_in_page; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5834 | size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5835 | unsigned long dst_i; |
| 5836 | unsigned long src_i; |
| 5837 | |
| 5838 | if (src_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5839 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5840 | "memmove bogus src_offset %lu move len %lu dst len %lu", |
| 5841 | src_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5842 | BUG_ON(1); |
| 5843 | } |
| 5844 | if (dst_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5845 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5846 | "memmove bogus dst_offset %lu move len %lu dst len %lu", |
| 5847 | dst_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5848 | BUG_ON(1); |
| 5849 | } |
| 5850 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5851 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5852 | dst_off_in_page = (start_offset + dst_offset) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5853 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5854 | src_off_in_page = (start_offset + src_offset) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5855 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5856 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5857 | dst_i = (start_offset + dst_offset) >> PAGE_SHIFT; |
| 5858 | src_i = (start_offset + src_offset) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5859 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5860 | cur = min(len, (unsigned long)(PAGE_SIZE - |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5861 | src_off_in_page)); |
| 5862 | cur = min_t(unsigned long, cur, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5863 | (unsigned long)(PAGE_SIZE - dst_off_in_page)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5864 | |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5865 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5866 | dst_off_in_page, src_off_in_page, cur); |
| 5867 | |
| 5868 | src_offset += cur; |
| 5869 | dst_offset += cur; |
| 5870 | len -= cur; |
| 5871 | } |
| 5872 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5873 | |
| 5874 | void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 5875 | unsigned long src_offset, unsigned long len) |
| 5876 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5877 | struct btrfs_fs_info *fs_info = dst->fs_info; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5878 | size_t cur; |
| 5879 | size_t dst_off_in_page; |
| 5880 | size_t src_off_in_page; |
| 5881 | unsigned long dst_end = dst_offset + len - 1; |
| 5882 | unsigned long src_end = src_offset + len - 1; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5883 | size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5884 | unsigned long dst_i; |
| 5885 | unsigned long src_i; |
| 5886 | |
| 5887 | if (src_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5888 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5889 | "memmove bogus src_offset %lu move len %lu len %lu", |
| 5890 | src_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5891 | BUG_ON(1); |
| 5892 | } |
| 5893 | if (dst_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5894 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5895 | "memmove bogus dst_offset %lu move len %lu len %lu", |
| 5896 | dst_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5897 | BUG_ON(1); |
| 5898 | } |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5899 | if (dst_offset < src_offset) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5900 | memcpy_extent_buffer(dst, dst_offset, src_offset, len); |
| 5901 | return; |
| 5902 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5903 | while (len > 0) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5904 | dst_i = (start_offset + dst_end) >> PAGE_SHIFT; |
| 5905 | src_i = (start_offset + src_end) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5906 | |
| 5907 | dst_off_in_page = (start_offset + dst_end) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5908 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5909 | src_off_in_page = (start_offset + src_end) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5910 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5911 | |
| 5912 | cur = min_t(unsigned long, len, src_off_in_page + 1); |
| 5913 | cur = min(cur, dst_off_in_page + 1); |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5914 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5915 | dst_off_in_page - cur + 1, |
| 5916 | src_off_in_page - cur + 1, cur); |
| 5917 | |
| 5918 | dst_end -= cur; |
| 5919 | src_end -= cur; |
| 5920 | len -= cur; |
| 5921 | } |
| 5922 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5923 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5924 | int try_release_extent_buffer(struct page *page) |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5925 | { |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5926 | struct extent_buffer *eb; |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5927 | |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5928 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5929 | * 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] | 5930 | * now. |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5931 | */ |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5932 | spin_lock(&page->mapping->private_lock); |
| 5933 | if (!PagePrivate(page)) { |
| 5934 | spin_unlock(&page->mapping->private_lock); |
| 5935 | return 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5936 | } |
| 5937 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5938 | eb = (struct extent_buffer *)page->private; |
| 5939 | BUG_ON(!eb); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5940 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5941 | /* |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5942 | * This is a little awful but should be ok, we need to make sure that |
| 5943 | * the eb doesn't disappear out from under us while we're looking at |
| 5944 | * this page. |
| 5945 | */ |
| 5946 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5947 | if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5948 | spin_unlock(&eb->refs_lock); |
| 5949 | spin_unlock(&page->mapping->private_lock); |
| 5950 | return 0; |
| 5951 | } |
| 5952 | spin_unlock(&page->mapping->private_lock); |
| 5953 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5954 | /* |
| 5955 | * If tree ref isn't set then we know the ref on this eb is a real ref, |
| 5956 | * so just return, this page will likely be freed soon anyway. |
| 5957 | */ |
| 5958 | if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { |
| 5959 | spin_unlock(&eb->refs_lock); |
| 5960 | return 0; |
| 5961 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5962 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5963 | return release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5964 | } |