Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1 | #include <linux/bitops.h> |
| 2 | #include <linux/slab.h> |
| 3 | #include <linux/bio.h> |
| 4 | #include <linux/mm.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5 | #include <linux/pagemap.h> |
| 6 | #include <linux/page-flags.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7 | #include <linux/spinlock.h> |
| 8 | #include <linux/blkdev.h> |
| 9 | #include <linux/swap.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 10 | #include <linux/writeback.h> |
| 11 | #include <linux/pagevec.h> |
Linus Torvalds | 268bb0c | 2011-05-20 12:50:29 -0700 | [diff] [blame] | 12 | #include <linux/prefetch.h> |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 13 | #include <linux/cleancache.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 14 | #include "extent_io.h" |
| 15 | #include "extent_map.h" |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 16 | #include "ctree.h" |
| 17 | #include "btrfs_inode.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 18 | #include "volumes.h" |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 19 | #include "check-integrity.h" |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 20 | #include "locking.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 21 | #include "rcu-string.h" |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 22 | #include "backref.h" |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 23 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 24 | static struct kmem_cache *extent_state_cache; |
| 25 | static struct kmem_cache *extent_buffer_cache; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 26 | static struct bio_set *btrfs_bioset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 27 | |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 28 | static inline bool extent_state_in_tree(const struct extent_state *state) |
| 29 | { |
| 30 | return !RB_EMPTY_NODE(&state->rb_node); |
| 31 | } |
| 32 | |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 33 | #ifdef CONFIG_BTRFS_DEBUG |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 34 | static LIST_HEAD(buffers); |
| 35 | static LIST_HEAD(states); |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 36 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 37 | static DEFINE_SPINLOCK(leak_lock); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 38 | |
| 39 | static inline |
| 40 | void btrfs_leak_debug_add(struct list_head *new, struct list_head *head) |
| 41 | { |
| 42 | unsigned long flags; |
| 43 | |
| 44 | spin_lock_irqsave(&leak_lock, flags); |
| 45 | list_add(new, head); |
| 46 | spin_unlock_irqrestore(&leak_lock, flags); |
| 47 | } |
| 48 | |
| 49 | static inline |
| 50 | void btrfs_leak_debug_del(struct list_head *entry) |
| 51 | { |
| 52 | unsigned long flags; |
| 53 | |
| 54 | spin_lock_irqsave(&leak_lock, flags); |
| 55 | list_del(entry); |
| 56 | spin_unlock_irqrestore(&leak_lock, flags); |
| 57 | } |
| 58 | |
| 59 | static inline |
| 60 | void btrfs_leak_debug_check(void) |
| 61 | { |
| 62 | struct extent_state *state; |
| 63 | struct extent_buffer *eb; |
| 64 | |
| 65 | while (!list_empty(&states)) { |
| 66 | state = list_entry(states.next, struct extent_state, leak_list); |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 67 | pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n", |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 68 | state->start, state->end, state->state, |
| 69 | extent_state_in_tree(state), |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 70 | atomic_read(&state->refs)); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 71 | list_del(&state->leak_list); |
| 72 | kmem_cache_free(extent_state_cache, state); |
| 73 | } |
| 74 | |
| 75 | while (!list_empty(&buffers)) { |
| 76 | eb = list_entry(buffers.next, struct extent_buffer, leak_list); |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 77 | printk(KERN_ERR "BTRFS: buffer leak start %llu len %lu " |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 78 | "refs %d\n", |
| 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]", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 101 | caller, btrfs_ino(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 | |
| 130 | /* tells the submit_bio code to use a WRITE_SYNC */ |
| 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, |
| 166 | SLAB_RECLAIM_ACCOUNT | 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, |
| 172 | SLAB_RECLAIM_ACCOUNT | 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 | |
| 229 | state = kmem_cache_alloc(extent_state_cache, mask); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 230 | if (!state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 231 | return state; |
| 232 | state->state = 0; |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 233 | state->failrec = NULL; |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 234 | RB_CLEAR_NODE(&state->rb_node); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 235 | btrfs_leak_debug_add(&state->leak_list, &states); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 236 | atomic_set(&state->refs, 1); |
| 237 | init_waitqueue_head(&state->wq); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 238 | trace_alloc_extent_state(state, mask, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 239 | return state; |
| 240 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 241 | |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 242 | void free_extent_state(struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 243 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 244 | if (!state) |
| 245 | return; |
| 246 | if (atomic_dec_and_test(&state->refs)) { |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 247 | WARN_ON(extent_state_in_tree(state)); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 248 | btrfs_leak_debug_del(&state->leak_list); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 249 | trace_free_extent_state(state, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 250 | kmem_cache_free(extent_state_cache, state); |
| 251 | } |
| 252 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 253 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 254 | static struct rb_node *tree_insert(struct rb_root *root, |
| 255 | struct rb_node *search_start, |
| 256 | u64 offset, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 257 | struct rb_node *node, |
| 258 | struct rb_node ***p_in, |
| 259 | struct rb_node **parent_in) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 260 | { |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 261 | struct rb_node **p; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 262 | struct rb_node *parent = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 263 | struct tree_entry *entry; |
| 264 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 265 | if (p_in && parent_in) { |
| 266 | p = *p_in; |
| 267 | parent = *parent_in; |
| 268 | goto do_insert; |
| 269 | } |
| 270 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 271 | p = search_start ? &search_start : &root->rb_node; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 272 | while (*p) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 273 | parent = *p; |
| 274 | entry = rb_entry(parent, struct tree_entry, rb_node); |
| 275 | |
| 276 | if (offset < entry->start) |
| 277 | p = &(*p)->rb_left; |
| 278 | else if (offset > entry->end) |
| 279 | p = &(*p)->rb_right; |
| 280 | else |
| 281 | return parent; |
| 282 | } |
| 283 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 284 | do_insert: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 285 | rb_link_node(node, parent, p); |
| 286 | rb_insert_color(node, root); |
| 287 | return NULL; |
| 288 | } |
| 289 | |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 290 | 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] | 291 | struct rb_node **prev_ret, |
| 292 | struct rb_node **next_ret, |
| 293 | struct rb_node ***p_ret, |
| 294 | struct rb_node **parent_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 295 | { |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 296 | struct rb_root *root = &tree->state; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 297 | struct rb_node **n = &root->rb_node; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 298 | struct rb_node *prev = NULL; |
| 299 | struct rb_node *orig_prev = NULL; |
| 300 | struct tree_entry *entry; |
| 301 | struct tree_entry *prev_entry = NULL; |
| 302 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 303 | while (*n) { |
| 304 | prev = *n; |
| 305 | entry = rb_entry(prev, struct tree_entry, rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 306 | prev_entry = entry; |
| 307 | |
| 308 | if (offset < entry->start) |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 309 | n = &(*n)->rb_left; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 310 | else if (offset > entry->end) |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 311 | n = &(*n)->rb_right; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 312 | else |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 313 | return *n; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 314 | } |
| 315 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 316 | if (p_ret) |
| 317 | *p_ret = n; |
| 318 | if (parent_ret) |
| 319 | *parent_ret = prev; |
| 320 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 321 | if (prev_ret) { |
| 322 | orig_prev = prev; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 323 | while (prev && offset > prev_entry->end) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 324 | prev = rb_next(prev); |
| 325 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| 326 | } |
| 327 | *prev_ret = prev; |
| 328 | prev = orig_prev; |
| 329 | } |
| 330 | |
| 331 | if (next_ret) { |
| 332 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 333 | while (prev && offset < prev_entry->start) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 334 | prev = rb_prev(prev); |
| 335 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| 336 | } |
| 337 | *next_ret = prev; |
| 338 | } |
| 339 | return NULL; |
| 340 | } |
| 341 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 342 | static inline struct rb_node * |
| 343 | tree_search_for_insert(struct extent_io_tree *tree, |
| 344 | u64 offset, |
| 345 | struct rb_node ***p_ret, |
| 346 | struct rb_node **parent_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 347 | { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 348 | struct rb_node *prev = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 349 | struct rb_node *ret; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 350 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 351 | ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 352 | if (!ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 353 | return prev; |
| 354 | return ret; |
| 355 | } |
| 356 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 357 | static inline struct rb_node *tree_search(struct extent_io_tree *tree, |
| 358 | u64 offset) |
| 359 | { |
| 360 | return tree_search_for_insert(tree, offset, NULL, NULL); |
| 361 | } |
| 362 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 363 | static void merge_cb(struct extent_io_tree *tree, struct extent_state *new, |
| 364 | struct extent_state *other) |
| 365 | { |
| 366 | if (tree->ops && tree->ops->merge_extent_hook) |
| 367 | tree->ops->merge_extent_hook(tree->mapping->host, new, |
| 368 | other); |
| 369 | } |
| 370 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 371 | /* |
| 372 | * utility function to look for merge candidates inside a given range. |
| 373 | * Any extents with matching state are merged together into a single |
| 374 | * extent in the tree. Extents with EXTENT_IO in their state field |
| 375 | * are not merged because the end_io handlers need to be able to do |
| 376 | * operations on them without sleeping (or doing allocations/splits). |
| 377 | * |
| 378 | * This should be called with the tree lock held. |
| 379 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 380 | static void merge_state(struct extent_io_tree *tree, |
| 381 | struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 382 | { |
| 383 | struct extent_state *other; |
| 384 | struct rb_node *other_node; |
| 385 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 386 | if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 387 | return; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 388 | |
| 389 | other_node = rb_prev(&state->rb_node); |
| 390 | if (other_node) { |
| 391 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 392 | if (other->end == state->start - 1 && |
| 393 | other->state == state->state) { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 394 | merge_cb(tree, state, other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 395 | state->start = other->start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 396 | rb_erase(&other->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 397 | RB_CLEAR_NODE(&other->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 398 | free_extent_state(other); |
| 399 | } |
| 400 | } |
| 401 | other_node = rb_next(&state->rb_node); |
| 402 | if (other_node) { |
| 403 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 404 | if (other->start == state->end + 1 && |
| 405 | other->state == state->state) { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 406 | merge_cb(tree, state, other); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 407 | state->end = other->end; |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 408 | rb_erase(&other->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 409 | RB_CLEAR_NODE(&other->rb_node); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 410 | free_extent_state(other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 411 | } |
| 412 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 413 | } |
| 414 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 415 | static void set_state_cb(struct extent_io_tree *tree, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 416 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 417 | { |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 418 | if (tree->ops && tree->ops->set_bit_hook) |
| 419 | tree->ops->set_bit_hook(tree->mapping->host, state, bits); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static void clear_state_cb(struct extent_io_tree *tree, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 423 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 424 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 425 | if (tree->ops && tree->ops->clear_bit_hook) |
| 426 | tree->ops->clear_bit_hook(tree->mapping->host, state, bits); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 427 | } |
| 428 | |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 429 | static void set_state_bits(struct extent_io_tree *tree, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 430 | struct extent_state *state, unsigned *bits, |
| 431 | struct extent_changeset *changeset); |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 432 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 433 | /* |
| 434 | * insert an extent_state struct into the tree. 'bits' are set on the |
| 435 | * struct before it is inserted. |
| 436 | * |
| 437 | * This may return -EEXIST if the extent is already there, in which case the |
| 438 | * state struct is freed. |
| 439 | * |
| 440 | * The tree lock is not taken internally. This is a utility function and |
| 441 | * probably isn't what you want to call (see set/clear_extent_bit). |
| 442 | */ |
| 443 | static int insert_state(struct extent_io_tree *tree, |
| 444 | struct extent_state *state, u64 start, u64 end, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 445 | struct rb_node ***p, |
| 446 | struct rb_node **parent, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 447 | unsigned *bits, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 448 | { |
| 449 | struct rb_node *node; |
| 450 | |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 451 | if (end < start) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 452 | WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 453 | end, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 454 | state->start = start; |
| 455 | state->end = end; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 456 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 457 | set_state_bits(tree, state, bits, changeset); |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 458 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 459 | node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 460 | if (node) { |
| 461 | struct extent_state *found; |
| 462 | found = rb_entry(node, struct extent_state, rb_node); |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 463 | printk(KERN_ERR "BTRFS: found node %llu %llu on insert of " |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 464 | "%llu %llu\n", |
| 465 | found->start, found->end, start, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 466 | return -EEXIST; |
| 467 | } |
| 468 | merge_state(tree, state); |
| 469 | return 0; |
| 470 | } |
| 471 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 472 | 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] | 473 | u64 split) |
| 474 | { |
| 475 | if (tree->ops && tree->ops->split_extent_hook) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 476 | tree->ops->split_extent_hook(tree->mapping->host, orig, split); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 477 | } |
| 478 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 479 | /* |
| 480 | * split a given extent state struct in two, inserting the preallocated |
| 481 | * struct 'prealloc' as the newly created second half. 'split' indicates an |
| 482 | * offset inside 'orig' where it should be split. |
| 483 | * |
| 484 | * Before calling, |
| 485 | * the tree has 'orig' at [orig->start, orig->end]. After calling, there |
| 486 | * are two extent state structs in the tree: |
| 487 | * prealloc: [orig->start, split - 1] |
| 488 | * orig: [ split, orig->end ] |
| 489 | * |
| 490 | * The tree locks are not taken by this function. They need to be held |
| 491 | * by the caller. |
| 492 | */ |
| 493 | static int split_state(struct extent_io_tree *tree, struct extent_state *orig, |
| 494 | struct extent_state *prealloc, u64 split) |
| 495 | { |
| 496 | struct rb_node *node; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 497 | |
| 498 | split_cb(tree, orig, split); |
| 499 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 500 | prealloc->start = orig->start; |
| 501 | prealloc->end = split - 1; |
| 502 | prealloc->state = orig->state; |
| 503 | orig->start = split; |
| 504 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 505 | node = tree_insert(&tree->state, &orig->rb_node, prealloc->end, |
| 506 | &prealloc->rb_node, NULL, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 507 | if (node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 508 | free_extent_state(prealloc); |
| 509 | return -EEXIST; |
| 510 | } |
| 511 | return 0; |
| 512 | } |
| 513 | |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 514 | static struct extent_state *next_state(struct extent_state *state) |
| 515 | { |
| 516 | struct rb_node *next = rb_next(&state->rb_node); |
| 517 | if (next) |
| 518 | return rb_entry(next, struct extent_state, rb_node); |
| 519 | else |
| 520 | return NULL; |
| 521 | } |
| 522 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 523 | /* |
| 524 | * utility function to clear some bits in an extent state struct. |
Wang Sheng-Hui | 1b303fc | 2012-04-06 14:35:18 +0800 | [diff] [blame] | 525 | * 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] | 526 | * |
| 527 | * If no bits are set on the state struct after clearing things, the |
| 528 | * struct is freed and removed from the tree |
| 529 | */ |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 530 | static struct extent_state *clear_state_bit(struct extent_io_tree *tree, |
| 531 | struct extent_state *state, |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 532 | unsigned *bits, int wake, |
| 533 | struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 534 | { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 535 | struct extent_state *next; |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 536 | unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 537 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 538 | if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 539 | u64 range = state->end - state->start + 1; |
| 540 | WARN_ON(range > tree->dirty_bytes); |
| 541 | tree->dirty_bytes -= range; |
| 542 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 543 | clear_state_cb(tree, state, bits); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 544 | add_extent_changeset(state, bits_to_clear, changeset, 0); |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 545 | state->state &= ~bits_to_clear; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 546 | if (wake) |
| 547 | wake_up(&state->wq); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 548 | if (state->state == 0) { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 549 | next = next_state(state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 550 | if (extent_state_in_tree(state)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 551 | rb_erase(&state->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 552 | RB_CLEAR_NODE(&state->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 553 | free_extent_state(state); |
| 554 | } else { |
| 555 | WARN_ON(1); |
| 556 | } |
| 557 | } else { |
| 558 | merge_state(tree, state); |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 559 | next = next_state(state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 560 | } |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 561 | return next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 562 | } |
| 563 | |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 564 | static struct extent_state * |
| 565 | alloc_extent_state_atomic(struct extent_state *prealloc) |
| 566 | { |
| 567 | if (!prealloc) |
| 568 | prealloc = alloc_extent_state(GFP_ATOMIC); |
| 569 | |
| 570 | return prealloc; |
| 571 | } |
| 572 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 573 | 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] | 574 | { |
| 575 | btrfs_panic(tree_fs_info(tree), err, "Locking error: " |
| 576 | "Extent tree was modified by another " |
| 577 | "thread while locked."); |
| 578 | } |
| 579 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 580 | /* |
| 581 | * clear some bits on a range in the tree. This may require splitting |
| 582 | * or inserting elements in the tree, so the gfp mask is used to |
| 583 | * indicate which allocations or sleeping are allowed. |
| 584 | * |
| 585 | * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove |
| 586 | * the given range from the tree regardless of state (ie for truncate). |
| 587 | * |
| 588 | * the range [start, end] is inclusive. |
| 589 | * |
Jeff Mahoney | 6763af8 | 2012-03-01 14:56:29 +0100 | [diff] [blame] | 590 | * 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] | 591 | */ |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 592 | static int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 593 | unsigned bits, int wake, int delete, |
| 594 | struct extent_state **cached_state, |
| 595 | gfp_t mask, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 596 | { |
| 597 | struct extent_state *state; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 598 | struct extent_state *cached; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 599 | struct extent_state *prealloc = NULL; |
| 600 | struct rb_node *node; |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 601 | u64 last_end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 602 | int err; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 603 | int clear = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 604 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 605 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 606 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 607 | if (bits & EXTENT_DELALLOC) |
| 608 | bits |= EXTENT_NORESERVE; |
| 609 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 610 | if (delete) |
| 611 | bits |= ~EXTENT_CTLBITS; |
| 612 | bits |= EXTENT_FIRST_DELALLOC; |
| 613 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 614 | if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY)) |
| 615 | clear = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 616 | again: |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 617 | if (!prealloc && gfpflags_allow_blocking(mask)) { |
Filipe Manana | c7bc631 | 2014-11-03 14:12:57 +0000 | [diff] [blame] | 618 | /* |
| 619 | * Don't care for allocation failure here because we might end |
| 620 | * up not needing the pre-allocated extent state at all, which |
| 621 | * is the case if we only have in the tree extent states that |
| 622 | * cover our input range and don't cover too any other range. |
| 623 | * If we end up needing a new extent state we allocate it later. |
| 624 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 625 | prealloc = alloc_extent_state(mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 626 | } |
| 627 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 628 | spin_lock(&tree->lock); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 629 | if (cached_state) { |
| 630 | cached = *cached_state; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 631 | |
| 632 | if (clear) { |
| 633 | *cached_state = NULL; |
| 634 | cached_state = NULL; |
| 635 | } |
| 636 | |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 637 | if (cached && extent_state_in_tree(cached) && |
| 638 | cached->start <= start && cached->end > start) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 639 | if (clear) |
| 640 | atomic_dec(&cached->refs); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 641 | state = cached; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 642 | goto hit_next; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 643 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 644 | if (clear) |
| 645 | free_extent_state(cached); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 646 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 647 | /* |
| 648 | * this search will find the extents that end after |
| 649 | * our range starts |
| 650 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 651 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 652 | if (!node) |
| 653 | goto out; |
| 654 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 655 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 656 | if (state->start > end) |
| 657 | goto out; |
| 658 | WARN_ON(state->end < start); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 659 | last_end = state->end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 660 | |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 661 | /* the state doesn't have the wanted bits, go ahead */ |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 662 | if (!(state->state & bits)) { |
| 663 | state = next_state(state); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 664 | goto next; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 665 | } |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 666 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 667 | /* |
| 668 | * | ---- desired range ---- | |
| 669 | * | state | or |
| 670 | * | ------------- state -------------- | |
| 671 | * |
| 672 | * We need to split the extent we found, and may flip |
| 673 | * bits on second half. |
| 674 | * |
| 675 | * If the extent we found extends past our range, we |
| 676 | * just split and search again. It'll get split again |
| 677 | * the next time though. |
| 678 | * |
| 679 | * If the extent we found is inside our range, we clear |
| 680 | * the desired bit on it. |
| 681 | */ |
| 682 | |
| 683 | if (state->start < start) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 684 | prealloc = alloc_extent_state_atomic(prealloc); |
| 685 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 686 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 687 | if (err) |
| 688 | extent_io_tree_panic(tree, err); |
| 689 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 690 | prealloc = NULL; |
| 691 | if (err) |
| 692 | goto out; |
| 693 | if (state->end <= end) { |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 694 | state = clear_state_bit(tree, state, &bits, wake, |
| 695 | changeset); |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 696 | goto next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 697 | } |
| 698 | goto search_again; |
| 699 | } |
| 700 | /* |
| 701 | * | ---- desired range ---- | |
| 702 | * | state | |
| 703 | * We need to split the extent, and clear the bit |
| 704 | * on the first half |
| 705 | */ |
| 706 | if (state->start <= end && state->end > end) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 707 | prealloc = alloc_extent_state_atomic(prealloc); |
| 708 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 709 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 710 | if (err) |
| 711 | extent_io_tree_panic(tree, err); |
| 712 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 713 | if (wake) |
| 714 | wake_up(&state->wq); |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 715 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 716 | clear_state_bit(tree, prealloc, &bits, wake, changeset); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 717 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 718 | prealloc = NULL; |
| 719 | goto out; |
| 720 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 721 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 722 | state = clear_state_bit(tree, state, &bits, wake, changeset); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 723 | next: |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 724 | if (last_end == (u64)-1) |
| 725 | goto out; |
| 726 | start = last_end + 1; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 727 | if (start <= end && state && !need_resched()) |
Liu Bo | 692e575 | 2012-02-16 18:34:36 +0800 | [diff] [blame] | 728 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 729 | goto search_again; |
| 730 | |
| 731 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 732 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 733 | if (prealloc) |
| 734 | free_extent_state(prealloc); |
| 735 | |
Jeff Mahoney | 6763af8 | 2012-03-01 14:56:29 +0100 | [diff] [blame] | 736 | return 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 737 | |
| 738 | search_again: |
| 739 | if (start > end) |
| 740 | goto out; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 741 | spin_unlock(&tree->lock); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 742 | if (gfpflags_allow_blocking(mask)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 743 | cond_resched(); |
| 744 | goto again; |
| 745 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 746 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 747 | static void wait_on_state(struct extent_io_tree *tree, |
| 748 | struct extent_state *state) |
Christoph Hellwig | 641f521 | 2008-12-02 06:36:10 -0500 | [diff] [blame] | 749 | __releases(tree->lock) |
| 750 | __acquires(tree->lock) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 751 | { |
| 752 | DEFINE_WAIT(wait); |
| 753 | prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 754 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 755 | schedule(); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 756 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 757 | finish_wait(&state->wq, &wait); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | /* |
| 761 | * waits for one or more bits to clear on a range in the state tree. |
| 762 | * The range [start, end] is inclusive. |
| 763 | * The tree lock is taken by this function |
| 764 | */ |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 765 | static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 766 | unsigned long bits) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 767 | { |
| 768 | struct extent_state *state; |
| 769 | struct rb_node *node; |
| 770 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 771 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 772 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 773 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 774 | again: |
| 775 | while (1) { |
| 776 | /* |
| 777 | * this search will find all the extents that end after |
| 778 | * our range starts |
| 779 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 780 | node = tree_search(tree, start); |
Filipe Manana | c50d3e7 | 2014-03-31 14:53:25 +0100 | [diff] [blame] | 781 | process_node: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 782 | if (!node) |
| 783 | break; |
| 784 | |
| 785 | state = rb_entry(node, struct extent_state, rb_node); |
| 786 | |
| 787 | if (state->start > end) |
| 788 | goto out; |
| 789 | |
| 790 | if (state->state & bits) { |
| 791 | start = state->start; |
| 792 | atomic_inc(&state->refs); |
| 793 | wait_on_state(tree, state); |
| 794 | free_extent_state(state); |
| 795 | goto again; |
| 796 | } |
| 797 | start = state->end + 1; |
| 798 | |
| 799 | if (start > end) |
| 800 | break; |
| 801 | |
Filipe Manana | c50d3e7 | 2014-03-31 14:53:25 +0100 | [diff] [blame] | 802 | if (!cond_resched_lock(&tree->lock)) { |
| 803 | node = rb_next(node); |
| 804 | goto process_node; |
| 805 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 806 | } |
| 807 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 808 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 809 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 810 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 811 | static void set_state_bits(struct extent_io_tree *tree, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 812 | struct extent_state *state, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 813 | unsigned *bits, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 814 | { |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 815 | unsigned bits_to_set = *bits & ~EXTENT_CTLBITS; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 816 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 817 | set_state_cb(tree, state, bits); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 818 | if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 819 | u64 range = state->end - state->start + 1; |
| 820 | tree->dirty_bytes += range; |
| 821 | } |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 822 | add_extent_changeset(state, bits_to_set, changeset, 1); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 823 | state->state |= bits_to_set; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 824 | } |
| 825 | |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 826 | static void cache_state_if_flags(struct extent_state *state, |
| 827 | struct extent_state **cached_ptr, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 828 | unsigned flags) |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 829 | { |
| 830 | if (cached_ptr && !(*cached_ptr)) { |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 831 | if (!flags || (state->state & flags)) { |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 832 | *cached_ptr = state; |
| 833 | atomic_inc(&state->refs); |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 838 | static void cache_state(struct extent_state *state, |
| 839 | struct extent_state **cached_ptr) |
| 840 | { |
| 841 | return cache_state_if_flags(state, cached_ptr, |
| 842 | EXTENT_IOBITS | EXTENT_BOUNDARY); |
| 843 | } |
| 844 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 845 | /* |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 846 | * set some bits on a range in the tree. This may require allocations or |
| 847 | * sleeping, so the gfp mask is used to indicate what is allowed. |
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 | * If any of the exclusive bits are set, this will fail with -EEXIST if some |
| 850 | * part of the range already has the desired bits set. The start of the |
| 851 | * existing range is returned in failed_start in this case. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 852 | * |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 853 | * [start, end] is inclusive This takes the tree lock. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 854 | */ |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 855 | |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 856 | static int __must_check |
| 857 | __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 858 | unsigned bits, unsigned exclusive_bits, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 859 | u64 *failed_start, struct extent_state **cached_state, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 860 | gfp_t mask, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 861 | { |
| 862 | struct extent_state *state; |
| 863 | struct extent_state *prealloc = NULL; |
| 864 | struct rb_node *node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 865 | struct rb_node **p; |
| 866 | struct rb_node *parent; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 867 | int err = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 868 | u64 last_start; |
| 869 | u64 last_end; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 870 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 871 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 872 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 873 | bits |= EXTENT_FIRST_DELALLOC; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 874 | again: |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 875 | if (!prealloc && gfpflags_allow_blocking(mask)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 876 | prealloc = alloc_extent_state(mask); |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 877 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 878 | } |
| 879 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 880 | spin_lock(&tree->lock); |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 881 | if (cached_state && *cached_state) { |
| 882 | state = *cached_state; |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 883 | if (state->start <= start && state->end > start && |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 884 | extent_state_in_tree(state)) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 885 | node = &state->rb_node; |
| 886 | goto hit_next; |
| 887 | } |
| 888 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 889 | /* |
| 890 | * this search will find all the extents that end after |
| 891 | * our range starts. |
| 892 | */ |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 893 | node = tree_search_for_insert(tree, start, &p, &parent); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 894 | if (!node) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 895 | prealloc = alloc_extent_state_atomic(prealloc); |
| 896 | BUG_ON(!prealloc); |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 897 | err = insert_state(tree, prealloc, start, end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 898 | &p, &parent, &bits, changeset); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 899 | if (err) |
| 900 | extent_io_tree_panic(tree, err); |
| 901 | |
Filipe David Borba Manana | c42ac0b | 2013-11-26 15:01:34 +0000 | [diff] [blame] | 902 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 903 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 904 | goto out; |
| 905 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 906 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 40431d6 | 2009-08-05 12:57:59 -0400 | [diff] [blame] | 907 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 908 | last_start = state->start; |
| 909 | last_end = state->end; |
| 910 | |
| 911 | /* |
| 912 | * | ---- desired range ---- | |
| 913 | * | state | |
| 914 | * |
| 915 | * Just lock what we found and keep going |
| 916 | */ |
| 917 | if (state->start == start && state->end <= end) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 918 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 919 | *failed_start = state->start; |
| 920 | err = -EEXIST; |
| 921 | goto out; |
| 922 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 923 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 924 | set_state_bits(tree, state, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 925 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 926 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 927 | if (last_end == (u64)-1) |
| 928 | goto out; |
| 929 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 930 | state = next_state(state); |
| 931 | if (start < end && state && state->start == start && |
| 932 | !need_resched()) |
| 933 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 934 | goto search_again; |
| 935 | } |
| 936 | |
| 937 | /* |
| 938 | * | ---- desired range ---- | |
| 939 | * | state | |
| 940 | * or |
| 941 | * | ------------- state -------------- | |
| 942 | * |
| 943 | * We need to split the extent we found, and may flip bits on |
| 944 | * second half. |
| 945 | * |
| 946 | * If the extent we found extends past our |
| 947 | * range, we just split and search again. It'll get split |
| 948 | * again the next time though. |
| 949 | * |
| 950 | * If the extent we found is inside our range, we set the |
| 951 | * desired bit on it. |
| 952 | */ |
| 953 | if (state->start < start) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 954 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 955 | *failed_start = start; |
| 956 | err = -EEXIST; |
| 957 | goto out; |
| 958 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 959 | |
| 960 | prealloc = alloc_extent_state_atomic(prealloc); |
| 961 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 962 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 963 | if (err) |
| 964 | extent_io_tree_panic(tree, err); |
| 965 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 966 | prealloc = NULL; |
| 967 | if (err) |
| 968 | goto out; |
| 969 | if (state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 970 | set_state_bits(tree, state, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 971 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 972 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 973 | if (last_end == (u64)-1) |
| 974 | goto out; |
| 975 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 976 | state = next_state(state); |
| 977 | if (start < end && state && state->start == start && |
| 978 | !need_resched()) |
| 979 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 980 | } |
| 981 | goto search_again; |
| 982 | } |
| 983 | /* |
| 984 | * | ---- desired range ---- | |
| 985 | * | state | or | state | |
| 986 | * |
| 987 | * There's a hole, we need to insert something in it and |
| 988 | * ignore the extent we found. |
| 989 | */ |
| 990 | if (state->start > start) { |
| 991 | u64 this_end; |
| 992 | if (end < last_start) |
| 993 | this_end = end; |
| 994 | else |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 995 | this_end = last_start - 1; |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 996 | |
| 997 | prealloc = alloc_extent_state_atomic(prealloc); |
| 998 | BUG_ON(!prealloc); |
Xiao Guangrong | c7f895a | 2011-04-20 06:45:49 +0000 | [diff] [blame] | 999 | |
| 1000 | /* |
| 1001 | * Avoid to free 'prealloc' if it can be merged with |
| 1002 | * the later extent. |
| 1003 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1004 | err = insert_state(tree, prealloc, start, this_end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1005 | NULL, NULL, &bits, changeset); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1006 | if (err) |
| 1007 | extent_io_tree_panic(tree, err); |
| 1008 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1009 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1010 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1011 | start = this_end + 1; |
| 1012 | goto search_again; |
| 1013 | } |
| 1014 | /* |
| 1015 | * | ---- desired range ---- | |
| 1016 | * | state | |
| 1017 | * We need to split the extent, and set the bit |
| 1018 | * on the first half |
| 1019 | */ |
| 1020 | if (state->start <= end && state->end > end) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1021 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1022 | *failed_start = start; |
| 1023 | err = -EEXIST; |
| 1024 | goto out; |
| 1025 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 1026 | |
| 1027 | prealloc = alloc_extent_state_atomic(prealloc); |
| 1028 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1029 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1030 | if (err) |
| 1031 | extent_io_tree_panic(tree, err); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1032 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1033 | set_state_bits(tree, prealloc, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1034 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1035 | merge_state(tree, prealloc); |
| 1036 | prealloc = NULL; |
| 1037 | goto out; |
| 1038 | } |
| 1039 | |
David Sterba | b5a4ba1 | 2016-04-27 01:02:15 +0200 | [diff] [blame^] | 1040 | search_again: |
| 1041 | if (start > end) |
| 1042 | goto out; |
| 1043 | spin_unlock(&tree->lock); |
| 1044 | if (gfpflags_allow_blocking(mask)) |
| 1045 | cond_resched(); |
| 1046 | goto again; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1047 | |
| 1048 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1049 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1050 | if (prealloc) |
| 1051 | free_extent_state(prealloc); |
| 1052 | |
| 1053 | return err; |
| 1054 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1055 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1056 | |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1057 | 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] | 1058 | unsigned bits, u64 * failed_start, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1059 | struct extent_state **cached_state, gfp_t mask) |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1060 | { |
| 1061 | return __set_extent_bit(tree, start, end, bits, 0, failed_start, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1062 | cached_state, mask, NULL); |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1066 | /** |
Liu Bo | 10983f2 | 2012-07-11 15:26:19 +0800 | [diff] [blame] | 1067 | * convert_extent_bit - convert all bits in a given range from one bit to |
| 1068 | * another |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1069 | * @tree: the io tree to search |
| 1070 | * @start: the start offset in bytes |
| 1071 | * @end: the end offset in bytes (inclusive) |
| 1072 | * @bits: the bits to set in this range |
| 1073 | * @clear_bits: the bits to clear in this range |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1074 | * @cached_state: state that we're going to cache |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1075 | * @mask: the allocation mask |
| 1076 | * |
| 1077 | * This will go through and set bits for the given range. If any states exist |
| 1078 | * already in this range they are set with the given bit and cleared of the |
| 1079 | * clear_bits. This is only meant to be used by things that are mergeable, ie |
| 1080 | * converting from say DELALLOC to DIRTY. This is not meant to be used with |
| 1081 | * boundary bits like LOCK. |
| 1082 | */ |
| 1083 | 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] | 1084 | unsigned bits, unsigned clear_bits, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1085 | struct extent_state **cached_state, gfp_t mask) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1086 | { |
| 1087 | struct extent_state *state; |
| 1088 | struct extent_state *prealloc = NULL; |
| 1089 | struct rb_node *node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1090 | struct rb_node **p; |
| 1091 | struct rb_node *parent; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1092 | int err = 0; |
| 1093 | u64 last_start; |
| 1094 | u64 last_end; |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1095 | bool first_iteration = true; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1096 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 1097 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 1098 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1099 | again: |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 1100 | if (!prealloc && gfpflags_allow_blocking(mask)) { |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1101 | /* |
| 1102 | * Best effort, don't worry if extent state allocation fails |
| 1103 | * here for the first iteration. We might have a cached state |
| 1104 | * that matches exactly the target range, in which case no |
| 1105 | * extent state allocations are needed. We'll only know this |
| 1106 | * after locking the tree. |
| 1107 | */ |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1108 | prealloc = alloc_extent_state(mask); |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1109 | if (!prealloc && !first_iteration) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1110 | return -ENOMEM; |
| 1111 | } |
| 1112 | |
| 1113 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1114 | if (cached_state && *cached_state) { |
| 1115 | state = *cached_state; |
| 1116 | if (state->start <= start && state->end > start && |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1117 | extent_state_in_tree(state)) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1118 | node = &state->rb_node; |
| 1119 | goto hit_next; |
| 1120 | } |
| 1121 | } |
| 1122 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1123 | /* |
| 1124 | * this search will find all the extents that end after |
| 1125 | * our range starts. |
| 1126 | */ |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1127 | node = tree_search_for_insert(tree, start, &p, &parent); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1128 | if (!node) { |
| 1129 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1130 | if (!prealloc) { |
| 1131 | err = -ENOMEM; |
| 1132 | goto out; |
| 1133 | } |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1134 | err = insert_state(tree, prealloc, start, end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1135 | &p, &parent, &bits, NULL); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1136 | if (err) |
| 1137 | extent_io_tree_panic(tree, err); |
Filipe David Borba Manana | c42ac0b | 2013-11-26 15:01:34 +0000 | [diff] [blame] | 1138 | cache_state(prealloc, cached_state); |
| 1139 | prealloc = NULL; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1140 | goto out; |
| 1141 | } |
| 1142 | state = rb_entry(node, struct extent_state, rb_node); |
| 1143 | hit_next: |
| 1144 | last_start = state->start; |
| 1145 | last_end = state->end; |
| 1146 | |
| 1147 | /* |
| 1148 | * | ---- desired range ---- | |
| 1149 | * | state | |
| 1150 | * |
| 1151 | * Just lock what we found and keep going |
| 1152 | */ |
| 1153 | if (state->start == start && state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1154 | set_state_bits(tree, state, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1155 | cache_state(state, cached_state); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1156 | state = clear_state_bit(tree, state, &clear_bits, 0, NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1157 | if (last_end == (u64)-1) |
| 1158 | goto out; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1159 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1160 | if (start < end && state && state->start == start && |
| 1161 | !need_resched()) |
| 1162 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1163 | goto search_again; |
| 1164 | } |
| 1165 | |
| 1166 | /* |
| 1167 | * | ---- desired range ---- | |
| 1168 | * | state | |
| 1169 | * or |
| 1170 | * | ------------- state -------------- | |
| 1171 | * |
| 1172 | * We need to split the extent we found, and may flip bits on |
| 1173 | * second half. |
| 1174 | * |
| 1175 | * If the extent we found extends past our |
| 1176 | * range, we just split and search again. It'll get split |
| 1177 | * again the next time though. |
| 1178 | * |
| 1179 | * If the extent we found is inside our range, we set the |
| 1180 | * desired bit on it. |
| 1181 | */ |
| 1182 | if (state->start < start) { |
| 1183 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1184 | if (!prealloc) { |
| 1185 | err = -ENOMEM; |
| 1186 | goto out; |
| 1187 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1188 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1189 | if (err) |
| 1190 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1191 | prealloc = NULL; |
| 1192 | if (err) |
| 1193 | goto out; |
| 1194 | if (state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1195 | set_state_bits(tree, state, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1196 | cache_state(state, cached_state); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1197 | state = clear_state_bit(tree, state, &clear_bits, 0, |
| 1198 | NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1199 | if (last_end == (u64)-1) |
| 1200 | goto out; |
| 1201 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1202 | if (start < end && state && state->start == start && |
| 1203 | !need_resched()) |
| 1204 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1205 | } |
| 1206 | goto search_again; |
| 1207 | } |
| 1208 | /* |
| 1209 | * | ---- desired range ---- | |
| 1210 | * | state | or | state | |
| 1211 | * |
| 1212 | * There's a hole, we need to insert something in it and |
| 1213 | * ignore the extent we found. |
| 1214 | */ |
| 1215 | if (state->start > start) { |
| 1216 | u64 this_end; |
| 1217 | if (end < last_start) |
| 1218 | this_end = end; |
| 1219 | else |
| 1220 | this_end = last_start - 1; |
| 1221 | |
| 1222 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1223 | if (!prealloc) { |
| 1224 | err = -ENOMEM; |
| 1225 | goto out; |
| 1226 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1227 | |
| 1228 | /* |
| 1229 | * Avoid to free 'prealloc' if it can be merged with |
| 1230 | * the later extent. |
| 1231 | */ |
| 1232 | err = insert_state(tree, prealloc, start, this_end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1233 | NULL, NULL, &bits, NULL); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1234 | if (err) |
| 1235 | extent_io_tree_panic(tree, err); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1236 | cache_state(prealloc, cached_state); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1237 | prealloc = NULL; |
| 1238 | start = this_end + 1; |
| 1239 | goto search_again; |
| 1240 | } |
| 1241 | /* |
| 1242 | * | ---- desired range ---- | |
| 1243 | * | state | |
| 1244 | * We need to split the extent, and set the bit |
| 1245 | * on the first half |
| 1246 | */ |
| 1247 | if (state->start <= end && state->end > end) { |
| 1248 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1249 | if (!prealloc) { |
| 1250 | err = -ENOMEM; |
| 1251 | goto out; |
| 1252 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1253 | |
| 1254 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1255 | if (err) |
| 1256 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1257 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1258 | set_state_bits(tree, prealloc, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1259 | cache_state(prealloc, cached_state); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1260 | clear_state_bit(tree, prealloc, &clear_bits, 0, NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1261 | prealloc = NULL; |
| 1262 | goto out; |
| 1263 | } |
| 1264 | |
| 1265 | goto search_again; |
| 1266 | |
| 1267 | out: |
| 1268 | spin_unlock(&tree->lock); |
| 1269 | if (prealloc) |
| 1270 | free_extent_state(prealloc); |
| 1271 | |
| 1272 | return err; |
| 1273 | |
| 1274 | search_again: |
| 1275 | if (start > end) |
| 1276 | goto out; |
| 1277 | spin_unlock(&tree->lock); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 1278 | if (gfpflags_allow_blocking(mask)) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [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; |
| 1282 | } |
| 1283 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1284 | /* wrappers around set/clear extent bit */ |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1285 | 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] | 1286 | unsigned bits, struct extent_changeset *changeset) |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1287 | { |
| 1288 | /* |
| 1289 | * We don't support EXTENT_LOCKED yet, as current changeset will |
| 1290 | * record any bits changed, so for EXTENT_LOCKED case, it will |
| 1291 | * either fail with -EEXIST or changeset will record the whole |
| 1292 | * range. |
| 1293 | */ |
| 1294 | BUG_ON(bits & EXTENT_LOCKED); |
| 1295 | |
David Sterba | 2c53b91 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1296 | 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] | 1297 | changeset); |
| 1298 | } |
| 1299 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1300 | int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 1301 | unsigned bits, int wake, int delete, |
| 1302 | struct extent_state **cached, gfp_t mask) |
| 1303 | { |
| 1304 | return __clear_extent_bit(tree, start, end, bits, wake, delete, |
| 1305 | cached, mask, NULL); |
| 1306 | } |
| 1307 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1308 | 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] | 1309 | unsigned bits, struct extent_changeset *changeset) |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1310 | { |
| 1311 | /* |
| 1312 | * Don't support EXTENT_LOCKED case, same reason as |
| 1313 | * set_record_extent_bits(). |
| 1314 | */ |
| 1315 | BUG_ON(bits & EXTENT_LOCKED); |
| 1316 | |
David Sterba | f734c44 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1317 | 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] | 1318 | changeset); |
| 1319 | } |
| 1320 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1321 | /* |
| 1322 | * either insert or lock state struct between start and end use mask to tell |
| 1323 | * us if waiting is desired. |
| 1324 | */ |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1325 | 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] | 1326 | struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1327 | { |
| 1328 | int err; |
| 1329 | u64 failed_start; |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1330 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1331 | while (1) { |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1332 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1333 | EXTENT_LOCKED, &failed_start, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1334 | cached_state, GFP_NOFS, NULL); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1335 | if (err == -EEXIST) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1336 | wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED); |
| 1337 | start = failed_start; |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1338 | } else |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1339 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1340 | WARN_ON(start > end); |
| 1341 | } |
| 1342 | return err; |
| 1343 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1344 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1345 | 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] | 1346 | { |
| 1347 | int err; |
| 1348 | u64 failed_start; |
| 1349 | |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1350 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1351 | &failed_start, NULL, GFP_NOFS, NULL); |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1352 | if (err == -EEXIST) { |
| 1353 | if (failed_start > start) |
| 1354 | clear_extent_bit(tree, start, failed_start - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1355 | EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1356 | return 0; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1357 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1358 | return 1; |
| 1359 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1360 | |
David Sterba | bd1fa4f | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 1361 | 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] | 1362 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1363 | unsigned long index = start >> PAGE_SHIFT; |
| 1364 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1365 | struct page *page; |
| 1366 | |
| 1367 | while (index <= end_index) { |
| 1368 | page = find_get_page(inode->i_mapping, index); |
| 1369 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
| 1370 | clear_page_dirty_for_io(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1371 | put_page(page); |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1372 | index++; |
| 1373 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1374 | } |
| 1375 | |
David Sterba | f631157 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 1376 | 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] | 1377 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1378 | unsigned long index = start >> PAGE_SHIFT; |
| 1379 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1380 | struct page *page; |
| 1381 | |
| 1382 | while (index <= end_index) { |
| 1383 | page = find_get_page(inode->i_mapping, index); |
| 1384 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1385 | __set_page_dirty_nobuffers(page); |
Konstantin Khebnikov | 8d38633 | 2015-02-11 15:26:55 -0800 | [diff] [blame] | 1386 | account_page_redirty(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1387 | put_page(page); |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1388 | index++; |
| 1389 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1390 | } |
| 1391 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1392 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1393 | * helper function to set both pages and extents in the tree writeback |
| 1394 | */ |
David Sterba | 35de6db | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 1395 | 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] | 1396 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1397 | unsigned long index = start >> PAGE_SHIFT; |
| 1398 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1399 | struct page *page; |
| 1400 | |
| 1401 | while (index <= end_index) { |
| 1402 | page = find_get_page(tree->mapping, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1403 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1404 | set_page_writeback(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1405 | put_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1406 | index++; |
| 1407 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1408 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1409 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1410 | /* find the first state struct with 'bits' set after 'start', and |
| 1411 | * return it. tree->lock must be held. NULL will returned if |
| 1412 | * nothing was found after 'start' |
| 1413 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1414 | static struct extent_state * |
| 1415 | find_first_extent_bit_state(struct extent_io_tree *tree, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1416 | u64 start, unsigned bits) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1417 | { |
| 1418 | struct rb_node *node; |
| 1419 | struct extent_state *state; |
| 1420 | |
| 1421 | /* |
| 1422 | * this search will find all the extents that end after |
| 1423 | * our range starts. |
| 1424 | */ |
| 1425 | node = tree_search(tree, start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1426 | if (!node) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1427 | goto out; |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1428 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1429 | while (1) { |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1430 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1431 | if (state->end >= start && (state->state & bits)) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1432 | return state; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1433 | |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1434 | node = rb_next(node); |
| 1435 | if (!node) |
| 1436 | break; |
| 1437 | } |
| 1438 | out: |
| 1439 | return NULL; |
| 1440 | } |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1441 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1442 | /* |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1443 | * find the first offset in the io tree with 'bits' set. zero is |
| 1444 | * returned if we find something, and *start_ret and *end_ret are |
| 1445 | * set to reflect the state struct that was found. |
| 1446 | * |
Wang Sheng-Hui | 477d7ea | 2012-04-06 14:35:47 +0800 | [diff] [blame] | 1447 | * If nothing was found, 1 is returned. If found something, return 0. |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1448 | */ |
| 1449 | int find_first_extent_bit(struct extent_io_tree *tree, u64 start, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1450 | u64 *start_ret, u64 *end_ret, unsigned bits, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1451 | struct extent_state **cached_state) |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1452 | { |
| 1453 | struct extent_state *state; |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1454 | struct rb_node *n; |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1455 | int ret = 1; |
| 1456 | |
| 1457 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1458 | if (cached_state && *cached_state) { |
| 1459 | state = *cached_state; |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1460 | if (state->end == start - 1 && extent_state_in_tree(state)) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1461 | n = rb_next(&state->rb_node); |
| 1462 | while (n) { |
| 1463 | state = rb_entry(n, struct extent_state, |
| 1464 | rb_node); |
| 1465 | if (state->state & bits) |
| 1466 | goto got_it; |
| 1467 | n = rb_next(n); |
| 1468 | } |
| 1469 | free_extent_state(*cached_state); |
| 1470 | *cached_state = NULL; |
| 1471 | goto out; |
| 1472 | } |
| 1473 | free_extent_state(*cached_state); |
| 1474 | *cached_state = NULL; |
| 1475 | } |
| 1476 | |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1477 | state = find_first_extent_bit_state(tree, start, bits); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1478 | got_it: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1479 | if (state) { |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 1480 | cache_state_if_flags(state, cached_state, 0); |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1481 | *start_ret = state->start; |
| 1482 | *end_ret = state->end; |
| 1483 | ret = 0; |
| 1484 | } |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1485 | out: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1486 | spin_unlock(&tree->lock); |
| 1487 | return ret; |
| 1488 | } |
| 1489 | |
| 1490 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1491 | * find a contiguous range of bytes in the file marked as delalloc, not |
| 1492 | * more than 'max_bytes'. start and end are used to return the range, |
| 1493 | * |
| 1494 | * 1 is returned if we find something, 0 if nothing was in the tree |
| 1495 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1496 | static noinline u64 find_delalloc_range(struct extent_io_tree *tree, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1497 | u64 *start, u64 *end, u64 max_bytes, |
| 1498 | struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1499 | { |
| 1500 | struct rb_node *node; |
| 1501 | struct extent_state *state; |
| 1502 | u64 cur_start = *start; |
| 1503 | u64 found = 0; |
| 1504 | u64 total_bytes = 0; |
| 1505 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1506 | spin_lock(&tree->lock); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1507 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1508 | /* |
| 1509 | * this search will find all the extents that end after |
| 1510 | * our range starts. |
| 1511 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1512 | node = tree_search(tree, cur_start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1513 | if (!node) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1514 | if (!found) |
| 1515 | *end = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1516 | goto out; |
| 1517 | } |
| 1518 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1519 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1520 | state = rb_entry(node, struct extent_state, rb_node); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1521 | if (found && (state->start != cur_start || |
| 1522 | (state->state & EXTENT_BOUNDARY))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1523 | goto out; |
| 1524 | } |
| 1525 | if (!(state->state & EXTENT_DELALLOC)) { |
| 1526 | if (!found) |
| 1527 | *end = state->end; |
| 1528 | goto out; |
| 1529 | } |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1530 | if (!found) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1531 | *start = state->start; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1532 | *cached_state = state; |
| 1533 | atomic_inc(&state->refs); |
| 1534 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1535 | found++; |
| 1536 | *end = state->end; |
| 1537 | cur_start = state->end + 1; |
| 1538 | node = rb_next(node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1539 | total_bytes += state->end - state->start + 1; |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 1540 | if (total_bytes >= max_bytes) |
Josef Bacik | 573aeca | 2013-08-30 14:38:49 -0400 | [diff] [blame] | 1541 | break; |
Josef Bacik | 573aeca | 2013-08-30 14:38:49 -0400 | [diff] [blame] | 1542 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1543 | break; |
| 1544 | } |
| 1545 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1546 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1547 | return found; |
| 1548 | } |
| 1549 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1550 | static noinline void __unlock_for_delalloc(struct inode *inode, |
| 1551 | struct page *locked_page, |
| 1552 | u64 start, u64 end) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1553 | { |
| 1554 | int ret; |
| 1555 | struct page *pages[16]; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1556 | unsigned long index = start >> PAGE_SHIFT; |
| 1557 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1558 | unsigned long nr_pages = end_index - index + 1; |
| 1559 | int i; |
| 1560 | |
| 1561 | if (index == locked_page->index && end_index == index) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1562 | return; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1563 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1564 | while (nr_pages > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1565 | ret = find_get_pages_contig(inode->i_mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1566 | min_t(unsigned long, nr_pages, |
| 1567 | ARRAY_SIZE(pages)), pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1568 | for (i = 0; i < ret; i++) { |
| 1569 | if (pages[i] != locked_page) |
| 1570 | unlock_page(pages[i]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1571 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1572 | } |
| 1573 | nr_pages -= ret; |
| 1574 | index += ret; |
| 1575 | cond_resched(); |
| 1576 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | static noinline int lock_delalloc_pages(struct inode *inode, |
| 1580 | struct page *locked_page, |
| 1581 | u64 delalloc_start, |
| 1582 | u64 delalloc_end) |
| 1583 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1584 | unsigned long index = delalloc_start >> PAGE_SHIFT; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1585 | unsigned long start_index = index; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1586 | unsigned long end_index = delalloc_end >> PAGE_SHIFT; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1587 | unsigned long pages_locked = 0; |
| 1588 | struct page *pages[16]; |
| 1589 | unsigned long nrpages; |
| 1590 | int ret; |
| 1591 | int i; |
| 1592 | |
| 1593 | /* the caller is responsible for locking the start index */ |
| 1594 | if (index == locked_page->index && index == end_index) |
| 1595 | return 0; |
| 1596 | |
| 1597 | /* skip the page at the start index */ |
| 1598 | nrpages = end_index - index + 1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1599 | while (nrpages > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1600 | ret = find_get_pages_contig(inode->i_mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1601 | min_t(unsigned long, |
| 1602 | nrpages, ARRAY_SIZE(pages)), pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1603 | if (ret == 0) { |
| 1604 | ret = -EAGAIN; |
| 1605 | goto done; |
| 1606 | } |
| 1607 | /* now we have an array of pages, lock them all */ |
| 1608 | for (i = 0; i < ret; i++) { |
| 1609 | /* |
| 1610 | * the caller is taking responsibility for |
| 1611 | * locked_page |
| 1612 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1613 | if (pages[i] != locked_page) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1614 | lock_page(pages[i]); |
Chris Mason | f2b1c41 | 2008-11-10 07:31:30 -0500 | [diff] [blame] | 1615 | if (!PageDirty(pages[i]) || |
| 1616 | pages[i]->mapping != inode->i_mapping) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1617 | ret = -EAGAIN; |
| 1618 | unlock_page(pages[i]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1619 | put_page(pages[i]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1620 | goto done; |
| 1621 | } |
| 1622 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1623 | put_page(pages[i]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1624 | pages_locked++; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1625 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1626 | nrpages -= ret; |
| 1627 | index += ret; |
| 1628 | cond_resched(); |
| 1629 | } |
| 1630 | ret = 0; |
| 1631 | done: |
| 1632 | if (ret && pages_locked) { |
| 1633 | __unlock_for_delalloc(inode, locked_page, |
| 1634 | delalloc_start, |
| 1635 | ((u64)(start_index + pages_locked - 1)) << |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1636 | PAGE_SHIFT); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1637 | } |
| 1638 | return ret; |
| 1639 | } |
| 1640 | |
| 1641 | /* |
| 1642 | * find a contiguous range of bytes in the file marked as delalloc, not |
| 1643 | * more than 'max_bytes'. start and end are used to return the range, |
| 1644 | * |
| 1645 | * 1 is returned if we find something, 0 if nothing was in the tree |
| 1646 | */ |
Josef Bacik | 294e30f | 2013-10-09 12:00:56 -0400 | [diff] [blame] | 1647 | STATIC u64 find_lock_delalloc_range(struct inode *inode, |
| 1648 | struct extent_io_tree *tree, |
| 1649 | struct page *locked_page, u64 *start, |
| 1650 | u64 *end, u64 max_bytes) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1651 | { |
| 1652 | u64 delalloc_start; |
| 1653 | u64 delalloc_end; |
| 1654 | u64 found; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1655 | struct extent_state *cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1656 | int ret; |
| 1657 | int loops = 0; |
| 1658 | |
| 1659 | again: |
| 1660 | /* step one, find a bunch of delalloc bytes starting at start */ |
| 1661 | delalloc_start = *start; |
| 1662 | delalloc_end = 0; |
| 1663 | found = find_delalloc_range(tree, &delalloc_start, &delalloc_end, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1664 | max_bytes, &cached_state); |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1665 | if (!found || delalloc_end <= *start) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1666 | *start = delalloc_start; |
| 1667 | *end = delalloc_end; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1668 | free_extent_state(cached_state); |
Liu Bo | 385fe0b | 2013-10-01 23:49:49 +0800 | [diff] [blame] | 1669 | return 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | /* |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1673 | * start comes from the offset of locked_page. We have to lock |
| 1674 | * pages in order, so we can't process delalloc bytes before |
| 1675 | * locked_page |
| 1676 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1677 | if (delalloc_start < *start) |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1678 | delalloc_start = *start; |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1679 | |
| 1680 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1681 | * 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] | 1682 | */ |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 1683 | if (delalloc_end + 1 - delalloc_start > max_bytes) |
| 1684 | delalloc_end = delalloc_start + max_bytes - 1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1685 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1686 | /* step two, lock all the pages after the page that has start */ |
| 1687 | ret = lock_delalloc_pages(inode, locked_page, |
| 1688 | delalloc_start, delalloc_end); |
| 1689 | if (ret == -EAGAIN) { |
| 1690 | /* some of the pages are gone, lets avoid looping by |
| 1691 | * shortening the size of the delalloc range we're searching |
| 1692 | */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1693 | free_extent_state(cached_state); |
Chris Mason | 7d78874 | 2014-05-21 05:49:54 -0700 | [diff] [blame] | 1694 | cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1695 | if (!loops) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1696 | max_bytes = PAGE_SIZE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1697 | loops = 1; |
| 1698 | goto again; |
| 1699 | } else { |
| 1700 | found = 0; |
| 1701 | goto out_failed; |
| 1702 | } |
| 1703 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1704 | BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1705 | |
| 1706 | /* step three, lock the state bits for the whole range */ |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1707 | lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1708 | |
| 1709 | /* then test to make sure it is all still delalloc */ |
| 1710 | ret = test_range_bit(tree, delalloc_start, delalloc_end, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1711 | EXTENT_DELALLOC, 1, cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1712 | if (!ret) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1713 | unlock_extent_cached(tree, delalloc_start, delalloc_end, |
| 1714 | &cached_state, GFP_NOFS); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1715 | __unlock_for_delalloc(inode, locked_page, |
| 1716 | delalloc_start, delalloc_end); |
| 1717 | cond_resched(); |
| 1718 | goto again; |
| 1719 | } |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1720 | free_extent_state(cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1721 | *start = delalloc_start; |
| 1722 | *end = delalloc_end; |
| 1723 | out_failed: |
| 1724 | return found; |
| 1725 | } |
| 1726 | |
David Sterba | a9d93e1 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 1727 | void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1728 | struct page *locked_page, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1729 | unsigned clear_bits, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1730 | unsigned long page_ops) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1731 | { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1732 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1733 | int ret; |
| 1734 | struct page *pages[16]; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1735 | unsigned long index = start >> PAGE_SHIFT; |
| 1736 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1737 | unsigned long nr_pages = end_index - index + 1; |
| 1738 | int i; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1739 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1740 | clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1741 | if (page_ops == 0) |
David Sterba | a9d93e1 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 1742 | return; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1743 | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1744 | if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0) |
| 1745 | mapping_set_error(inode->i_mapping, -EIO); |
| 1746 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1747 | while (nr_pages > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1748 | ret = find_get_pages_contig(inode->i_mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1749 | min_t(unsigned long, |
| 1750 | nr_pages, ARRAY_SIZE(pages)), pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1751 | for (i = 0; i < ret; i++) { |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1752 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1753 | if (page_ops & PAGE_SET_PRIVATE2) |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1754 | SetPagePrivate2(pages[i]); |
| 1755 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1756 | if (pages[i] == locked_page) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1757 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1758 | continue; |
| 1759 | } |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1760 | if (page_ops & PAGE_CLEAR_DIRTY) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1761 | clear_page_dirty_for_io(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1762 | if (page_ops & PAGE_SET_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1763 | set_page_writeback(pages[i]); |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1764 | if (page_ops & PAGE_SET_ERROR) |
| 1765 | SetPageError(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1766 | if (page_ops & PAGE_END_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1767 | end_page_writeback(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1768 | if (page_ops & PAGE_UNLOCK) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1769 | unlock_page(pages[i]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1770 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1771 | } |
| 1772 | nr_pages -= ret; |
| 1773 | index += ret; |
| 1774 | cond_resched(); |
| 1775 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1776 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1777 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1778 | /* |
| 1779 | * count the number of bytes in the tree that have a given bit(s) |
| 1780 | * set. This can be fairly slow, except for EXTENT_DIRTY which is |
| 1781 | * cached. The total number found is returned. |
| 1782 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1783 | u64 count_range_bits(struct extent_io_tree *tree, |
| 1784 | u64 *start, u64 search_end, u64 max_bytes, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1785 | unsigned bits, int contig) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1786 | { |
| 1787 | struct rb_node *node; |
| 1788 | struct extent_state *state; |
| 1789 | u64 cur_start = *start; |
| 1790 | u64 total_bytes = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1791 | u64 last = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1792 | int found = 0; |
| 1793 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 1794 | if (WARN_ON(search_end <= cur_start)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1795 | return 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1796 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1797 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1798 | if (cur_start == 0 && bits == EXTENT_DIRTY) { |
| 1799 | total_bytes = tree->dirty_bytes; |
| 1800 | goto out; |
| 1801 | } |
| 1802 | /* |
| 1803 | * this search will find all the extents that end after |
| 1804 | * our range starts. |
| 1805 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1806 | node = tree_search(tree, cur_start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1807 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1808 | goto out; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1809 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1810 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1811 | state = rb_entry(node, struct extent_state, rb_node); |
| 1812 | if (state->start > search_end) |
| 1813 | break; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1814 | if (contig && found && state->start > last + 1) |
| 1815 | break; |
| 1816 | if (state->end >= cur_start && (state->state & bits) == bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1817 | total_bytes += min(search_end, state->end) + 1 - |
| 1818 | max(cur_start, state->start); |
| 1819 | if (total_bytes >= max_bytes) |
| 1820 | break; |
| 1821 | if (!found) { |
Josef Bacik | af60bed | 2011-05-04 11:11:17 -0400 | [diff] [blame] | 1822 | *start = max(cur_start, state->start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1823 | found = 1; |
| 1824 | } |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1825 | last = state->end; |
| 1826 | } else if (contig && found) { |
| 1827 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1828 | } |
| 1829 | node = rb_next(node); |
| 1830 | if (!node) |
| 1831 | break; |
| 1832 | } |
| 1833 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1834 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1835 | return total_bytes; |
| 1836 | } |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1837 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1838 | /* |
| 1839 | * set the private field for a given byte offset in the tree. If there isn't |
| 1840 | * an extent_state there already, this does nothing. |
| 1841 | */ |
Arnd Bergmann | f827ba9 | 2016-02-22 22:53:20 +0100 | [diff] [blame] | 1842 | 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] | 1843 | struct io_failure_record *failrec) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1844 | { |
| 1845 | struct rb_node *node; |
| 1846 | struct extent_state *state; |
| 1847 | int ret = 0; |
| 1848 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1849 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1850 | /* |
| 1851 | * this search will find all the extents that end after |
| 1852 | * our range starts. |
| 1853 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1854 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1855 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1856 | ret = -ENOENT; |
| 1857 | goto out; |
| 1858 | } |
| 1859 | state = rb_entry(node, struct extent_state, rb_node); |
| 1860 | if (state->start != start) { |
| 1861 | ret = -ENOENT; |
| 1862 | goto out; |
| 1863 | } |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 1864 | state->failrec = failrec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1865 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1866 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1867 | return ret; |
| 1868 | } |
| 1869 | |
Arnd Bergmann | f827ba9 | 2016-02-22 22:53:20 +0100 | [diff] [blame] | 1870 | 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] | 1871 | struct io_failure_record **failrec) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1872 | { |
| 1873 | struct rb_node *node; |
| 1874 | struct extent_state *state; |
| 1875 | int ret = 0; |
| 1876 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1877 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1878 | /* |
| 1879 | * this search will find all the extents that end after |
| 1880 | * our range starts. |
| 1881 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1882 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1883 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1884 | ret = -ENOENT; |
| 1885 | goto out; |
| 1886 | } |
| 1887 | state = rb_entry(node, struct extent_state, rb_node); |
| 1888 | if (state->start != start) { |
| 1889 | ret = -ENOENT; |
| 1890 | goto out; |
| 1891 | } |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 1892 | *failrec = state->failrec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1893 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1894 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1895 | return ret; |
| 1896 | } |
| 1897 | |
| 1898 | /* |
| 1899 | * searches a range in the state tree for a given mask. |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1900 | * 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] | 1901 | * has the bits set. Otherwise, 1 is returned if any bit in the |
| 1902 | * range is found set. |
| 1903 | */ |
| 1904 | 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] | 1905 | unsigned bits, int filled, struct extent_state *cached) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1906 | { |
| 1907 | struct extent_state *state = NULL; |
| 1908 | struct rb_node *node; |
| 1909 | int bitset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1910 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1911 | spin_lock(&tree->lock); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1912 | if (cached && extent_state_in_tree(cached) && cached->start <= start && |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 1913 | cached->end > start) |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1914 | node = &cached->rb_node; |
| 1915 | else |
| 1916 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1917 | while (node && start <= end) { |
| 1918 | state = rb_entry(node, struct extent_state, rb_node); |
| 1919 | |
| 1920 | if (filled && state->start > start) { |
| 1921 | bitset = 0; |
| 1922 | break; |
| 1923 | } |
| 1924 | |
| 1925 | if (state->start > end) |
| 1926 | break; |
| 1927 | |
| 1928 | if (state->state & bits) { |
| 1929 | bitset = 1; |
| 1930 | if (!filled) |
| 1931 | break; |
| 1932 | } else if (filled) { |
| 1933 | bitset = 0; |
| 1934 | break; |
| 1935 | } |
Chris Mason | 46562cec | 2009-09-23 20:23:16 -0400 | [diff] [blame] | 1936 | |
| 1937 | if (state->end == (u64)-1) |
| 1938 | break; |
| 1939 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1940 | start = state->end + 1; |
| 1941 | if (start > end) |
| 1942 | break; |
| 1943 | node = rb_next(node); |
| 1944 | if (!node) { |
| 1945 | if (filled) |
| 1946 | bitset = 0; |
| 1947 | break; |
| 1948 | } |
| 1949 | } |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1950 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1951 | return bitset; |
| 1952 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1953 | |
| 1954 | /* |
| 1955 | * helper function to set a given page up to date if all the |
| 1956 | * extents in the tree for that page are up to date |
| 1957 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1958 | 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] | 1959 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 1960 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1961 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1962 | if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1963 | SetPageUptodate(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1964 | } |
| 1965 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 1966 | int free_io_failure(struct inode *inode, struct io_failure_record *rec) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1967 | { |
| 1968 | int ret; |
| 1969 | int err = 0; |
| 1970 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 1971 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 1972 | set_state_failrec(failure_tree, rec->start, NULL); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1973 | ret = clear_extent_bits(failure_tree, rec->start, |
| 1974 | rec->start + rec->len - 1, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1975 | EXTENT_LOCKED | EXTENT_DIRTY); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1976 | if (ret) |
| 1977 | err = ret; |
| 1978 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1979 | ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start, |
| 1980 | rec->start + rec->len - 1, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1981 | EXTENT_DAMAGED); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1982 | if (ret && !err) |
| 1983 | err = ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1984 | |
| 1985 | kfree(rec); |
| 1986 | return err; |
| 1987 | } |
| 1988 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1989 | /* |
| 1990 | * this bypasses the standard btrfs submit functions deliberately, as |
| 1991 | * the standard behavior is to write all copies in a raid setup. here we only |
| 1992 | * want to write the one bad copy. so we do the mapping for ourselves and issue |
| 1993 | * submit_bio directly. |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1994 | * to avoid any synchronization issues, wait for the data after writing, which |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1995 | * actually prevents the read that triggered the error from finishing. |
| 1996 | * currently, there can be no more than two copies of every data bit. thus, |
| 1997 | * exactly one rewrite is required. |
| 1998 | */ |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 1999 | int repair_io_failure(struct inode *inode, u64 start, u64 length, u64 logical, |
| 2000 | struct page *page, unsigned int pg_offset, int mirror_num) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2001 | { |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2002 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2003 | struct bio *bio; |
| 2004 | struct btrfs_device *dev; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2005 | u64 map_length = 0; |
| 2006 | u64 sector; |
| 2007 | struct btrfs_bio *bbio = NULL; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2008 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2009 | int ret; |
| 2010 | |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2011 | ASSERT(!(fs_info->sb->s_flags & MS_RDONLY)); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2012 | BUG_ON(!mirror_num); |
| 2013 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2014 | /* we can't repair anything in raid56 yet */ |
| 2015 | if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num)) |
| 2016 | return 0; |
| 2017 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2018 | bio = btrfs_io_bio_alloc(GFP_NOFS, 1); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2019 | if (!bio) |
| 2020 | return -EIO; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2021 | bio->bi_iter.bi_size = 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2022 | map_length = length; |
| 2023 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2024 | ret = btrfs_map_block(fs_info, WRITE, logical, |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2025 | &map_length, &bbio, mirror_num); |
| 2026 | if (ret) { |
| 2027 | bio_put(bio); |
| 2028 | return -EIO; |
| 2029 | } |
| 2030 | BUG_ON(mirror_num != bbio->mirror_num); |
| 2031 | sector = bbio->stripes[mirror_num-1].physical >> 9; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2032 | bio->bi_iter.bi_sector = sector; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2033 | dev = bbio->stripes[mirror_num-1].dev; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 2034 | btrfs_put_bbio(bbio); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2035 | if (!dev || !dev->bdev || !dev->writeable) { |
| 2036 | bio_put(bio); |
| 2037 | return -EIO; |
| 2038 | } |
| 2039 | bio->bi_bdev = dev->bdev; |
Miao Xie | ffdd201 | 2014-09-12 18:44:00 +0800 | [diff] [blame] | 2040 | bio_add_page(bio, page, length, pg_offset); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2041 | |
Kent Overstreet | 33879d4 | 2013-11-23 22:33:32 -0800 | [diff] [blame] | 2042 | if (btrfsic_submit_bio_wait(WRITE_SYNC, bio)) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2043 | /* try to remap that extent elsewhere? */ |
| 2044 | bio_put(bio); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2045 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2046 | return -EIO; |
| 2047 | } |
| 2048 | |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 2049 | btrfs_info_rl_in_rcu(fs_info, |
| 2050 | "read error corrected: ino %llu off %llu (dev %s sector %llu)", |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2051 | btrfs_ino(inode), start, |
| 2052 | rcu_str_deref(dev->name), sector); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2053 | bio_put(bio); |
| 2054 | return 0; |
| 2055 | } |
| 2056 | |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2057 | int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb, |
| 2058 | int mirror_num) |
| 2059 | { |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2060 | u64 start = eb->start; |
| 2061 | unsigned long i, num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | d95603b | 2012-04-12 15:55:15 -0400 | [diff] [blame] | 2062 | int ret = 0; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2063 | |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2064 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 2065 | return -EROFS; |
| 2066 | |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2067 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 2068 | struct page *p = eb->pages[i]; |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2069 | |
| 2070 | ret = repair_io_failure(root->fs_info->btree_inode, start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2071 | PAGE_SIZE, start, p, |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2072 | start - page_offset(p), mirror_num); |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2073 | if (ret) |
| 2074 | break; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2075 | start += PAGE_SIZE; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | return ret; |
| 2079 | } |
| 2080 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2081 | /* |
| 2082 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 2083 | * to see if we need to process or clean up an io_failure_record |
| 2084 | */ |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2085 | int clean_io_failure(struct inode *inode, u64 start, struct page *page, |
| 2086 | unsigned int pg_offset) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2087 | { |
| 2088 | u64 private; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2089 | struct io_failure_record *failrec; |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2090 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2091 | struct extent_state *state; |
| 2092 | int num_copies; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2093 | int ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2094 | |
| 2095 | private = 0; |
| 2096 | ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
| 2097 | (u64)-1, 1, EXTENT_DIRTY, 0); |
| 2098 | if (!ret) |
| 2099 | return 0; |
| 2100 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2101 | ret = get_state_failrec(&BTRFS_I(inode)->io_failure_tree, start, |
| 2102 | &failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2103 | if (ret) |
| 2104 | return 0; |
| 2105 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2106 | BUG_ON(!failrec->this_mirror); |
| 2107 | |
| 2108 | if (failrec->in_validation) { |
| 2109 | /* there was no real error, just free the record */ |
| 2110 | pr_debug("clean_io_failure: freeing dummy error at %llu\n", |
| 2111 | failrec->start); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2112 | goto out; |
| 2113 | } |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2114 | if (fs_info->sb->s_flags & MS_RDONLY) |
| 2115 | goto out; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2116 | |
| 2117 | spin_lock(&BTRFS_I(inode)->io_tree.lock); |
| 2118 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 2119 | failrec->start, |
| 2120 | EXTENT_LOCKED); |
| 2121 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); |
| 2122 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2123 | if (state && state->start <= failrec->start && |
| 2124 | state->end >= failrec->start + failrec->len - 1) { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2125 | num_copies = btrfs_num_copies(fs_info, failrec->logical, |
| 2126 | failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2127 | if (num_copies > 1) { |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2128 | repair_io_failure(inode, start, failrec->len, |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2129 | failrec->logical, page, |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2130 | pg_offset, failrec->failed_mirror); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2131 | } |
| 2132 | } |
| 2133 | |
| 2134 | out: |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2135 | free_io_failure(inode, failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2136 | |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2137 | return 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2138 | } |
| 2139 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2140 | /* |
| 2141 | * Can be called when |
| 2142 | * - hold extent lock |
| 2143 | * - under ordered extent |
| 2144 | * - the inode is freeing |
| 2145 | */ |
| 2146 | void btrfs_free_io_failure_record(struct inode *inode, u64 start, u64 end) |
| 2147 | { |
| 2148 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 2149 | struct io_failure_record *failrec; |
| 2150 | struct extent_state *state, *next; |
| 2151 | |
| 2152 | if (RB_EMPTY_ROOT(&failure_tree->state)) |
| 2153 | return; |
| 2154 | |
| 2155 | spin_lock(&failure_tree->lock); |
| 2156 | state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY); |
| 2157 | while (state) { |
| 2158 | if (state->start > end) |
| 2159 | break; |
| 2160 | |
| 2161 | ASSERT(state->end <= end); |
| 2162 | |
| 2163 | next = next_state(state); |
| 2164 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2165 | failrec = state->failrec; |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2166 | free_extent_state(state); |
| 2167 | kfree(failrec); |
| 2168 | |
| 2169 | state = next; |
| 2170 | } |
| 2171 | spin_unlock(&failure_tree->lock); |
| 2172 | } |
| 2173 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2174 | 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] | 2175 | struct io_failure_record **failrec_ret) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2176 | { |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2177 | struct io_failure_record *failrec; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2178 | struct extent_map *em; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2179 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 2180 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 2181 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2182 | int ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2183 | u64 logical; |
| 2184 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2185 | ret = get_state_failrec(failure_tree, start, &failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2186 | if (ret) { |
| 2187 | failrec = kzalloc(sizeof(*failrec), GFP_NOFS); |
| 2188 | if (!failrec) |
| 2189 | return -ENOMEM; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2190 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2191 | failrec->start = start; |
| 2192 | failrec->len = end - start + 1; |
| 2193 | failrec->this_mirror = 0; |
| 2194 | failrec->bio_flags = 0; |
| 2195 | failrec->in_validation = 0; |
| 2196 | |
| 2197 | read_lock(&em_tree->lock); |
| 2198 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 2199 | if (!em) { |
| 2200 | read_unlock(&em_tree->lock); |
| 2201 | kfree(failrec); |
| 2202 | return -EIO; |
| 2203 | } |
| 2204 | |
Filipe David Borba Manana | 68ba990 | 2013-11-25 03:22:07 +0000 | [diff] [blame] | 2205 | if (em->start > start || em->start + em->len <= start) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2206 | free_extent_map(em); |
| 2207 | em = NULL; |
| 2208 | } |
| 2209 | read_unlock(&em_tree->lock); |
Tsutomu Itoh | 7a2d6a6 | 2012-10-01 03:07:15 -0600 | [diff] [blame] | 2210 | if (!em) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2211 | kfree(failrec); |
| 2212 | return -EIO; |
| 2213 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2214 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2215 | logical = start - em->start; |
| 2216 | logical = em->block_start + logical; |
| 2217 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 2218 | logical = em->block_start; |
| 2219 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
| 2220 | extent_set_compress_type(&failrec->bio_flags, |
| 2221 | em->compress_type); |
| 2222 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2223 | |
| 2224 | pr_debug("Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu\n", |
| 2225 | logical, start, failrec->len); |
| 2226 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2227 | failrec->logical = logical; |
| 2228 | free_extent_map(em); |
| 2229 | |
| 2230 | /* set the bits in the private failure tree */ |
| 2231 | ret = set_extent_bits(failure_tree, start, end, |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2232 | EXTENT_LOCKED | EXTENT_DIRTY); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2233 | if (ret >= 0) |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2234 | ret = set_state_failrec(failure_tree, start, failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2235 | /* set the bits in the inode's tree */ |
| 2236 | if (ret >= 0) |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2237 | ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2238 | if (ret < 0) { |
| 2239 | kfree(failrec); |
| 2240 | return ret; |
| 2241 | } |
| 2242 | } else { |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2243 | pr_debug("Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d\n", |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2244 | failrec->logical, failrec->start, failrec->len, |
| 2245 | failrec->in_validation); |
| 2246 | /* |
| 2247 | * when data can be on disk more than twice, add to failrec here |
| 2248 | * (e.g. with a list for failed_mirror) to make |
| 2249 | * clean_io_failure() clean all those errors at once. |
| 2250 | */ |
| 2251 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2252 | |
| 2253 | *failrec_ret = failrec; |
| 2254 | |
| 2255 | return 0; |
| 2256 | } |
| 2257 | |
| 2258 | int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio, |
| 2259 | struct io_failure_record *failrec, int failed_mirror) |
| 2260 | { |
| 2261 | int num_copies; |
| 2262 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 2263 | num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info, |
| 2264 | failrec->logical, failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2265 | if (num_copies == 1) { |
| 2266 | /* |
| 2267 | * we only have a single copy of the data, so don't bother with |
| 2268 | * all the retry and error correction code that follows. no |
| 2269 | * matter what the error is, it is very likely to persist. |
| 2270 | */ |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2271 | pr_debug("Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n", |
Miao Xie | 09a7f7a | 2013-07-25 19:22:32 +0800 | [diff] [blame] | 2272 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2273 | return 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2274 | } |
| 2275 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2276 | /* |
| 2277 | * there are two premises: |
| 2278 | * a) deliver good data to the caller |
| 2279 | * b) correct the bad sectors on disk |
| 2280 | */ |
| 2281 | if (failed_bio->bi_vcnt > 1) { |
| 2282 | /* |
| 2283 | * to fulfill b), we need to know the exact failing sectors, as |
| 2284 | * we don't want to rewrite any more than the failed ones. thus, |
| 2285 | * we need separate read requests for the failed bio |
| 2286 | * |
| 2287 | * if the following BUG_ON triggers, our validation request got |
| 2288 | * merged. we need separate requests for our algorithm to work. |
| 2289 | */ |
| 2290 | BUG_ON(failrec->in_validation); |
| 2291 | failrec->in_validation = 1; |
| 2292 | failrec->this_mirror = failed_mirror; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2293 | } else { |
| 2294 | /* |
| 2295 | * we're ready to fulfill a) and b) alongside. get a good copy |
| 2296 | * of the failed sector and if we succeed, we have setup |
| 2297 | * everything for repair_io_failure to do the rest for us. |
| 2298 | */ |
| 2299 | if (failrec->in_validation) { |
| 2300 | BUG_ON(failrec->this_mirror != failed_mirror); |
| 2301 | failrec->in_validation = 0; |
| 2302 | failrec->this_mirror = 0; |
| 2303 | } |
| 2304 | failrec->failed_mirror = failed_mirror; |
| 2305 | failrec->this_mirror++; |
| 2306 | if (failrec->this_mirror == failed_mirror) |
| 2307 | failrec->this_mirror++; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2308 | } |
| 2309 | |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2310 | if (failrec->this_mirror > num_copies) { |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2311 | pr_debug("Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n", |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2312 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2313 | return 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2314 | } |
| 2315 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2316 | return 1; |
| 2317 | } |
| 2318 | |
| 2319 | |
| 2320 | struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio, |
| 2321 | struct io_failure_record *failrec, |
| 2322 | struct page *page, int pg_offset, int icsum, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2323 | bio_end_io_t *endio_func, void *data) |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2324 | { |
| 2325 | struct bio *bio; |
| 2326 | struct btrfs_io_bio *btrfs_failed_bio; |
| 2327 | struct btrfs_io_bio *btrfs_bio; |
| 2328 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2329 | bio = btrfs_io_bio_alloc(GFP_NOFS, 1); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2330 | if (!bio) |
| 2331 | return NULL; |
| 2332 | |
| 2333 | bio->bi_end_io = endio_func; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2334 | bio->bi_iter.bi_sector = failrec->logical >> 9; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2335 | bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2336 | bio->bi_iter.bi_size = 0; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2337 | bio->bi_private = data; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2338 | |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2339 | btrfs_failed_bio = btrfs_io_bio(failed_bio); |
| 2340 | if (btrfs_failed_bio->csum) { |
| 2341 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2342 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
| 2343 | |
| 2344 | btrfs_bio = btrfs_io_bio(bio); |
| 2345 | btrfs_bio->csum = btrfs_bio->csum_inline; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2346 | icsum *= csum_size; |
| 2347 | memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum, |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2348 | csum_size); |
| 2349 | } |
| 2350 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2351 | bio_add_page(bio, page, failrec->len, pg_offset); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2352 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2353 | return bio; |
| 2354 | } |
| 2355 | |
| 2356 | /* |
| 2357 | * this is a generic handler for readpage errors (default |
| 2358 | * readpage_io_failed_hook). if other copies exist, read those and write back |
| 2359 | * good data to the failed position. does not investigate in remapping the |
| 2360 | * failed extent elsewhere, hoping the device will be smart enough to do this as |
| 2361 | * needed |
| 2362 | */ |
| 2363 | |
| 2364 | static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset, |
| 2365 | struct page *page, u64 start, u64 end, |
| 2366 | int failed_mirror) |
| 2367 | { |
| 2368 | struct io_failure_record *failrec; |
| 2369 | struct inode *inode = page->mapping->host; |
| 2370 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 2371 | struct bio *bio; |
| 2372 | int read_mode; |
| 2373 | int ret; |
| 2374 | |
| 2375 | BUG_ON(failed_bio->bi_rw & REQ_WRITE); |
| 2376 | |
| 2377 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 2378 | if (ret) |
| 2379 | return ret; |
| 2380 | |
| 2381 | ret = btrfs_check_repairable(inode, failed_bio, failrec, failed_mirror); |
| 2382 | if (!ret) { |
| 2383 | free_io_failure(inode, failrec); |
| 2384 | return -EIO; |
| 2385 | } |
| 2386 | |
| 2387 | if (failed_bio->bi_vcnt > 1) |
| 2388 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; |
| 2389 | else |
| 2390 | read_mode = READ_SYNC; |
| 2391 | |
| 2392 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
| 2393 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
| 2394 | start - page_offset(page), |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2395 | (int)phy_offset, failed_bio->bi_end_io, |
| 2396 | NULL); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2397 | if (!bio) { |
| 2398 | free_io_failure(inode, failrec); |
| 2399 | return -EIO; |
| 2400 | } |
| 2401 | |
| 2402 | pr_debug("Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d\n", |
| 2403 | read_mode, failrec->this_mirror, failrec->in_validation); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2404 | |
Tsutomu Itoh | 013bd4c | 2012-02-16 10:11:40 +0900 | [diff] [blame] | 2405 | ret = tree->ops->submit_bio_hook(inode, read_mode, bio, |
| 2406 | failrec->this_mirror, |
| 2407 | failrec->bio_flags, 0); |
Miao Xie | 6c387ab | 2014-09-12 18:43:57 +0800 | [diff] [blame] | 2408 | if (ret) { |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2409 | free_io_failure(inode, failrec); |
Miao Xie | 6c387ab | 2014-09-12 18:43:57 +0800 | [diff] [blame] | 2410 | bio_put(bio); |
| 2411 | } |
| 2412 | |
Tsutomu Itoh | 013bd4c | 2012-02-16 10:11:40 +0900 | [diff] [blame] | 2413 | return ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2414 | } |
| 2415 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2416 | /* lots and lots of room for performance fixes in the end_bio funcs */ |
| 2417 | |
David Sterba | b5227c0 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 2418 | 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] | 2419 | { |
| 2420 | int uptodate = (err == 0); |
| 2421 | struct extent_io_tree *tree; |
Eric Sandeen | 3e2426b | 2014-06-12 00:39:58 -0500 | [diff] [blame] | 2422 | int ret = 0; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2423 | |
| 2424 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 2425 | |
| 2426 | if (tree->ops && tree->ops->writepage_end_io_hook) { |
| 2427 | ret = tree->ops->writepage_end_io_hook(page, start, |
| 2428 | end, NULL, uptodate); |
| 2429 | if (ret) |
| 2430 | uptodate = 0; |
| 2431 | } |
| 2432 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2433 | if (!uptodate) { |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2434 | ClearPageUptodate(page); |
| 2435 | SetPageError(page); |
Liu Bo | 5dca6ee | 2014-05-12 12:47:36 +0800 | [diff] [blame] | 2436 | ret = ret < 0 ? ret : -EIO; |
| 2437 | mapping_set_error(page->mapping, ret); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2438 | } |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2439 | } |
| 2440 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2441 | /* |
| 2442 | * after a writepage IO is done, we need to: |
| 2443 | * clear the uptodate bits on error |
| 2444 | * clear the writeback bits in the extent tree for this IO |
| 2445 | * end_page_writeback if the page has no more pending IO |
| 2446 | * |
| 2447 | * Scheduling is not allowed, so the extent state tree is expected |
| 2448 | * to have one and only one object corresponding to this IO. |
| 2449 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2450 | static void end_bio_extent_writepage(struct bio *bio) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2451 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2452 | struct bio_vec *bvec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2453 | u64 start; |
| 2454 | u64 end; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2455 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2456 | |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2457 | bio_for_each_segment_all(bvec, bio, i) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2458 | struct page *page = bvec->bv_page; |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2459 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2460 | /* We always issue full-page reads, but if some block |
| 2461 | * in a page fails to read, blk_update_request() will |
| 2462 | * advance bv_offset and adjust bv_len to compensate. |
| 2463 | * Print a warning for nonzero offsets, and an error |
| 2464 | * if they don't add up to a full page. */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2465 | if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) { |
| 2466 | if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2467 | btrfs_err(BTRFS_I(page->mapping->host)->root->fs_info, |
| 2468 | "partial page write in btrfs with offset %u and length %u", |
| 2469 | bvec->bv_offset, bvec->bv_len); |
| 2470 | else |
| 2471 | btrfs_info(BTRFS_I(page->mapping->host)->root->fs_info, |
| 2472 | "incomplete page write in btrfs with offset %u and " |
| 2473 | "length %u", |
| 2474 | bvec->bv_offset, bvec->bv_len); |
| 2475 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2476 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2477 | start = page_offset(page); |
| 2478 | end = start + bvec->bv_offset + bvec->bv_len - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2479 | |
David Sterba | b5227c0 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 2480 | end_extent_writepage(page, bio->bi_error, start, end); |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2481 | end_page_writeback(page); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2482 | } |
Chris Mason | 2b1f55b | 2008-09-24 11:48:04 -0400 | [diff] [blame] | 2483 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2484 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2485 | } |
| 2486 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2487 | static void |
| 2488 | endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len, |
| 2489 | int uptodate) |
| 2490 | { |
| 2491 | struct extent_state *cached = NULL; |
| 2492 | u64 end = start + len - 1; |
| 2493 | |
| 2494 | if (uptodate && tree->track_uptodate) |
| 2495 | set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC); |
| 2496 | unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC); |
| 2497 | } |
| 2498 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2499 | /* |
| 2500 | * after a readpage IO is done, we need to: |
| 2501 | * clear the uptodate bits on error |
| 2502 | * set the uptodate bits if things worked |
| 2503 | * set the page up to date if all extents in the tree are uptodate |
| 2504 | * clear the lock bit in the extent tree |
| 2505 | * unlock the page if there are no other extents locked for it |
| 2506 | * |
| 2507 | * Scheduling is not allowed, so the extent state tree is expected |
| 2508 | * to have one and only one object corresponding to this IO. |
| 2509 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2510 | static void end_bio_extent_readpage(struct bio *bio) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2511 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2512 | struct bio_vec *bvec; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2513 | int uptodate = !bio->bi_error; |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2514 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2515 | struct extent_io_tree *tree; |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2516 | u64 offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2517 | u64 start; |
| 2518 | u64 end; |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2519 | u64 len; |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2520 | u64 extent_start = 0; |
| 2521 | u64 extent_len = 0; |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2522 | int mirror; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2523 | int ret; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2524 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2525 | |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2526 | bio_for_each_segment_all(bvec, bio, i) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2527 | struct page *page = bvec->bv_page; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2528 | struct inode *inode = page->mapping->host; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2529 | |
Kent Overstreet | be3940c | 2012-09-11 14:23:05 -0600 | [diff] [blame] | 2530 | pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, " |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2531 | "mirror=%u\n", (u64)bio->bi_iter.bi_sector, |
| 2532 | bio->bi_error, io_bio->mirror_num); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2533 | tree = &BTRFS_I(inode)->io_tree; |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2534 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2535 | /* We always issue full-page reads, but if some block |
| 2536 | * in a page fails to read, blk_update_request() will |
| 2537 | * advance bv_offset and adjust bv_len to compensate. |
| 2538 | * Print a warning for nonzero offsets, and an error |
| 2539 | * if they don't add up to a full page. */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2540 | if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) { |
| 2541 | if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2542 | btrfs_err(BTRFS_I(page->mapping->host)->root->fs_info, |
| 2543 | "partial page read in btrfs with offset %u and length %u", |
| 2544 | bvec->bv_offset, bvec->bv_len); |
| 2545 | else |
| 2546 | btrfs_info(BTRFS_I(page->mapping->host)->root->fs_info, |
| 2547 | "incomplete page read in btrfs with offset %u and " |
| 2548 | "length %u", |
| 2549 | bvec->bv_offset, bvec->bv_len); |
| 2550 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2551 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2552 | start = page_offset(page); |
| 2553 | end = start + bvec->bv_offset + bvec->bv_len - 1; |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2554 | len = bvec->bv_len; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2555 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2556 | mirror = io_bio->mirror_num; |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2557 | if (likely(uptodate && tree->ops && |
| 2558 | tree->ops->readpage_end_io_hook)) { |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2559 | ret = tree->ops->readpage_end_io_hook(io_bio, offset, |
| 2560 | page, start, end, |
| 2561 | mirror); |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2562 | if (ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2563 | uptodate = 0; |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2564 | else |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2565 | clean_io_failure(inode, start, page, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2566 | } |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2567 | |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2568 | if (likely(uptodate)) |
| 2569 | goto readpage_ok; |
| 2570 | |
| 2571 | if (tree->ops && tree->ops->readpage_io_failed_hook) { |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2572 | ret = tree->ops->readpage_io_failed_hook(page, mirror); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2573 | if (!ret && !bio->bi_error) |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2574 | uptodate = 1; |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2575 | } else { |
Jan Schmidt | f4a8e65 | 2011-12-01 09:30:36 -0500 | [diff] [blame] | 2576 | /* |
| 2577 | * The generic bio_readpage_error handles errors the |
| 2578 | * following way: If possible, new read requests are |
| 2579 | * created and submitted and will end up in |
| 2580 | * end_bio_extent_readpage as well (if we're lucky, not |
| 2581 | * in the !uptodate case). In that case it returns 0 and |
| 2582 | * we just go on with the next page in our bio. If it |
| 2583 | * can't handle the error it will return -EIO and we |
| 2584 | * remain responsible for that page. |
| 2585 | */ |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2586 | ret = bio_readpage_error(bio, offset, page, start, end, |
| 2587 | mirror); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2588 | if (ret == 0) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2589 | uptodate = !bio->bi_error; |
Liu Bo | 38c1c2e | 2014-08-19 23:33:13 +0800 | [diff] [blame] | 2590 | offset += len; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2591 | continue; |
| 2592 | } |
| 2593 | } |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2594 | readpage_ok: |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2595 | if (likely(uptodate)) { |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2596 | loff_t i_size = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2597 | pgoff_t end_index = i_size >> PAGE_SHIFT; |
Liu Bo | a583c02 | 2014-08-19 23:32:22 +0800 | [diff] [blame] | 2598 | unsigned off; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2599 | |
| 2600 | /* Zero out the end if this page straddles i_size */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2601 | off = i_size & (PAGE_SIZE-1); |
Liu Bo | a583c02 | 2014-08-19 23:32:22 +0800 | [diff] [blame] | 2602 | if (page->index == end_index && off) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2603 | zero_user_segment(page, off, PAGE_SIZE); |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2604 | SetPageUptodate(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2605 | } else { |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2606 | ClearPageUptodate(page); |
| 2607 | SetPageError(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2608 | } |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2609 | unlock_page(page); |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2610 | offset += len; |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2611 | |
| 2612 | if (unlikely(!uptodate)) { |
| 2613 | if (extent_len) { |
| 2614 | endio_readpage_release_extent(tree, |
| 2615 | extent_start, |
| 2616 | extent_len, 1); |
| 2617 | extent_start = 0; |
| 2618 | extent_len = 0; |
| 2619 | } |
| 2620 | endio_readpage_release_extent(tree, start, |
| 2621 | end - start + 1, 0); |
| 2622 | } else if (!extent_len) { |
| 2623 | extent_start = start; |
| 2624 | extent_len = end + 1 - start; |
| 2625 | } else if (extent_start + extent_len == start) { |
| 2626 | extent_len += end + 1 - start; |
| 2627 | } else { |
| 2628 | endio_readpage_release_extent(tree, extent_start, |
| 2629 | extent_len, uptodate); |
| 2630 | extent_start = start; |
| 2631 | extent_len = end + 1 - start; |
| 2632 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2633 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2634 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2635 | if (extent_len) |
| 2636 | endio_readpage_release_extent(tree, extent_start, extent_len, |
| 2637 | uptodate); |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2638 | if (io_bio->end_io) |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2639 | io_bio->end_io(io_bio, bio->bi_error); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2640 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2641 | } |
| 2642 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2643 | /* |
| 2644 | * this allocates from the btrfs_bioset. We're returning a bio right now |
| 2645 | * but you can call btrfs_io_bio for the appropriate container_of magic |
| 2646 | */ |
Miao Xie | 88f794e | 2010-11-22 03:02:55 +0000 | [diff] [blame] | 2647 | struct bio * |
| 2648 | btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs, |
| 2649 | gfp_t gfp_flags) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2650 | { |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2651 | struct btrfs_io_bio *btrfs_bio; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2652 | struct bio *bio; |
| 2653 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2654 | bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2655 | |
| 2656 | if (bio == NULL && (current->flags & PF_MEMALLOC)) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2657 | while (!bio && (nr_vecs /= 2)) { |
| 2658 | bio = bio_alloc_bioset(gfp_flags, |
| 2659 | nr_vecs, btrfs_bioset); |
| 2660 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2661 | } |
| 2662 | |
| 2663 | if (bio) { |
| 2664 | bio->bi_bdev = bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2665 | bio->bi_iter.bi_sector = first_sector; |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2666 | btrfs_bio = btrfs_io_bio(bio); |
| 2667 | btrfs_bio->csum = NULL; |
| 2668 | btrfs_bio->csum_allocated = NULL; |
| 2669 | btrfs_bio->end_io = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2670 | } |
| 2671 | return bio; |
| 2672 | } |
| 2673 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2674 | struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask) |
| 2675 | { |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 2676 | struct btrfs_io_bio *btrfs_bio; |
| 2677 | struct bio *new; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2678 | |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 2679 | new = bio_clone_bioset(bio, gfp_mask, btrfs_bioset); |
| 2680 | if (new) { |
| 2681 | btrfs_bio = btrfs_io_bio(new); |
| 2682 | btrfs_bio->csum = NULL; |
| 2683 | btrfs_bio->csum_allocated = NULL; |
| 2684 | btrfs_bio->end_io = NULL; |
Chris Mason | 3a9508b | 2015-08-21 10:05:39 -0700 | [diff] [blame] | 2685 | |
| 2686 | #ifdef CONFIG_BLK_CGROUP |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2687 | /* FIXME, put this into bio_clone_bioset */ |
| 2688 | if (bio->bi_css) |
| 2689 | bio_associate_blkcg(new, bio->bi_css); |
Chris Mason | 3a9508b | 2015-08-21 10:05:39 -0700 | [diff] [blame] | 2690 | #endif |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 2691 | } |
| 2692 | return new; |
| 2693 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2694 | |
| 2695 | /* this also allocates from the btrfs_bioset */ |
| 2696 | struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs) |
| 2697 | { |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2698 | struct btrfs_io_bio *btrfs_bio; |
| 2699 | struct bio *bio; |
| 2700 | |
| 2701 | bio = bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset); |
| 2702 | if (bio) { |
| 2703 | btrfs_bio = btrfs_io_bio(bio); |
| 2704 | btrfs_bio->csum = NULL; |
| 2705 | btrfs_bio->csum_allocated = NULL; |
| 2706 | btrfs_bio->end_io = NULL; |
| 2707 | } |
| 2708 | return bio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2709 | } |
| 2710 | |
| 2711 | |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 2712 | static int __must_check submit_one_bio(int rw, struct bio *bio, |
| 2713 | int mirror_num, unsigned long bio_flags) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2714 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2715 | int ret = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2716 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 2717 | struct page *page = bvec->bv_page; |
| 2718 | struct extent_io_tree *tree = bio->bi_private; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2719 | u64 start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2720 | |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2721 | start = page_offset(page) + bvec->bv_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2722 | |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2723 | bio->bi_private = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2724 | |
| 2725 | bio_get(bio); |
| 2726 | |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2727 | if (tree->ops && tree->ops->submit_bio_hook) |
liubo | 6b82ce8 | 2011-01-26 06:21:39 +0000 | [diff] [blame] | 2728 | ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 2729 | mirror_num, bio_flags, start); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2730 | else |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 2731 | btrfsic_submit_bio(rw, bio); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2732 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2733 | bio_put(bio); |
| 2734 | return ret; |
| 2735 | } |
| 2736 | |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 2737 | static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page, |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 2738 | unsigned long offset, size_t size, struct bio *bio, |
| 2739 | unsigned long bio_flags) |
| 2740 | { |
| 2741 | int ret = 0; |
| 2742 | if (tree->ops && tree->ops->merge_bio_hook) |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 2743 | ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio, |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 2744 | bio_flags); |
| 2745 | BUG_ON(ret < 0); |
| 2746 | return ret; |
| 2747 | |
| 2748 | } |
| 2749 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2750 | static int submit_extent_page(int rw, struct extent_io_tree *tree, |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2751 | struct writeback_control *wbc, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2752 | struct page *page, sector_t sector, |
| 2753 | size_t size, unsigned long offset, |
| 2754 | struct block_device *bdev, |
| 2755 | struct bio **bio_ret, |
| 2756 | unsigned long max_pages, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2757 | bio_end_io_t end_io_func, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2758 | int mirror_num, |
| 2759 | unsigned long prev_bio_flags, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2760 | unsigned long bio_flags, |
| 2761 | bool force_bio_submit) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2762 | { |
| 2763 | int ret = 0; |
| 2764 | struct bio *bio; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2765 | int contig = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2766 | int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2767 | size_t page_size = min_t(size_t, size, PAGE_SIZE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2768 | |
| 2769 | if (bio_ret && *bio_ret) { |
| 2770 | bio = *bio_ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2771 | if (old_compressed) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2772 | contig = bio->bi_iter.bi_sector == sector; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2773 | else |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 2774 | contig = bio_end_sector(bio) == sector; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2775 | |
| 2776 | if (prev_bio_flags != bio_flags || !contig || |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2777 | force_bio_submit || |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 2778 | merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2779 | bio_add_page(bio, page, page_size, offset) < page_size) { |
| 2780 | ret = submit_one_bio(rw, bio, mirror_num, |
| 2781 | prev_bio_flags); |
Naohiro Aota | 289454a | 2015-01-06 01:01:03 +0900 | [diff] [blame] | 2782 | if (ret < 0) { |
| 2783 | *bio_ret = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2784 | return ret; |
Naohiro Aota | 289454a | 2015-01-06 01:01:03 +0900 | [diff] [blame] | 2785 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2786 | bio = NULL; |
| 2787 | } else { |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2788 | if (wbc) |
| 2789 | wbc_account_io(wbc, page, page_size); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2790 | return 0; |
| 2791 | } |
| 2792 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2793 | |
Kent Overstreet | b54ffb7 | 2015-05-19 14:31:01 +0200 | [diff] [blame] | 2794 | bio = btrfs_bio_alloc(bdev, sector, BIO_MAX_PAGES, |
| 2795 | GFP_NOFS | __GFP_HIGH); |
Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 2796 | if (!bio) |
| 2797 | return -ENOMEM; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2798 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2799 | bio_add_page(bio, page, page_size, offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2800 | bio->bi_end_io = end_io_func; |
| 2801 | bio->bi_private = tree; |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2802 | if (wbc) { |
| 2803 | wbc_init_bio(wbc, bio); |
| 2804 | wbc_account_io(wbc, page, page_size); |
| 2805 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2806 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2807 | if (bio_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2808 | *bio_ret = bio; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2809 | else |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2810 | ret = submit_one_bio(rw, bio, mirror_num, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2811 | |
| 2812 | return ret; |
| 2813 | } |
| 2814 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 2815 | static void attach_extent_buffer_page(struct extent_buffer *eb, |
| 2816 | struct page *page) |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 2817 | { |
| 2818 | if (!PagePrivate(page)) { |
| 2819 | SetPagePrivate(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2820 | get_page(page); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 2821 | set_page_private(page, (unsigned long)eb); |
| 2822 | } else { |
| 2823 | WARN_ON(page->private != (unsigned long)eb); |
| 2824 | } |
| 2825 | } |
| 2826 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2827 | void set_page_extent_mapped(struct page *page) |
| 2828 | { |
| 2829 | if (!PagePrivate(page)) { |
| 2830 | SetPagePrivate(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2831 | get_page(page); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2832 | set_page_private(page, EXTENT_PAGE_PRIVATE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2833 | } |
| 2834 | } |
| 2835 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2836 | static struct extent_map * |
| 2837 | __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset, |
| 2838 | u64 start, u64 len, get_extent_t *get_extent, |
| 2839 | struct extent_map **em_cached) |
| 2840 | { |
| 2841 | struct extent_map *em; |
| 2842 | |
| 2843 | if (em_cached && *em_cached) { |
| 2844 | em = *em_cached; |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 2845 | if (extent_map_in_tree(em) && start >= em->start && |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2846 | start < extent_map_end(em)) { |
| 2847 | atomic_inc(&em->refs); |
| 2848 | return em; |
| 2849 | } |
| 2850 | |
| 2851 | free_extent_map(em); |
| 2852 | *em_cached = NULL; |
| 2853 | } |
| 2854 | |
| 2855 | em = get_extent(inode, page, pg_offset, start, len, 0); |
| 2856 | if (em_cached && !IS_ERR_OR_NULL(em)) { |
| 2857 | BUG_ON(*em_cached); |
| 2858 | atomic_inc(&em->refs); |
| 2859 | *em_cached = em; |
| 2860 | } |
| 2861 | return em; |
| 2862 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2863 | /* |
| 2864 | * basic readpage implementation. Locked extent state structs are inserted |
| 2865 | * into the tree that are removed when the IO is done (by the end_io |
| 2866 | * handlers) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2867 | * XXX JDM: This needs looking at to ensure proper page locking |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2868 | */ |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2869 | static int __do_readpage(struct extent_io_tree *tree, |
| 2870 | struct page *page, |
| 2871 | get_extent_t *get_extent, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2872 | struct extent_map **em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2873 | struct bio **bio, int mirror_num, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2874 | unsigned long *bio_flags, int rw, |
| 2875 | u64 *prev_em_start) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2876 | { |
| 2877 | struct inode *inode = page->mapping->host; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2878 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2879 | u64 page_end = start + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2880 | u64 end; |
| 2881 | u64 cur = start; |
| 2882 | u64 extent_offset; |
| 2883 | u64 last_byte = i_size_read(inode); |
| 2884 | u64 block_start; |
| 2885 | u64 cur_end; |
| 2886 | sector_t sector; |
| 2887 | struct extent_map *em; |
| 2888 | struct block_device *bdev; |
| 2889 | int ret; |
| 2890 | int nr = 0; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2891 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2892 | size_t iosize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2893 | size_t disk_io_size; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2894 | size_t blocksize = inode->i_sb->s_blocksize; |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 2895 | unsigned long this_bio_flag = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2896 | |
| 2897 | set_page_extent_mapped(page); |
| 2898 | |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2899 | end = page_end; |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 2900 | if (!PageUptodate(page)) { |
| 2901 | if (cleancache_get_page(page) == 0) { |
| 2902 | BUG_ON(blocksize != PAGE_SIZE); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2903 | unlock_extent(tree, start, end); |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 2904 | goto out; |
| 2905 | } |
| 2906 | } |
| 2907 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2908 | if (page->index == last_byte >> PAGE_SHIFT) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2909 | char *userpage; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2910 | size_t zero_offset = last_byte & (PAGE_SIZE - 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2911 | |
| 2912 | if (zero_offset) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2913 | iosize = PAGE_SIZE - zero_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2914 | userpage = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2915 | memset(userpage + zero_offset, 0, iosize); |
| 2916 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2917 | kunmap_atomic(userpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2918 | } |
| 2919 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2920 | while (cur <= end) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2921 | unsigned long pnr = (last_byte >> PAGE_SHIFT) + 1; |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2922 | bool force_bio_submit = false; |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 2923 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2924 | if (cur >= last_byte) { |
| 2925 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2926 | struct extent_state *cached = NULL; |
| 2927 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2928 | iosize = PAGE_SIZE - pg_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2929 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2930 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2931 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2932 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2933 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2934 | &cached, GFP_NOFS); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 2935 | unlock_extent_cached(tree, cur, |
| 2936 | cur + iosize - 1, |
| 2937 | &cached, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2938 | break; |
| 2939 | } |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2940 | em = __get_extent_map(inode, page, pg_offset, cur, |
| 2941 | end - cur + 1, get_extent, em_cached); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 2942 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2943 | SetPageError(page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 2944 | unlock_extent(tree, cur, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2945 | break; |
| 2946 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2947 | extent_offset = cur - em->start; |
| 2948 | BUG_ON(extent_map_end(em) <= cur); |
| 2949 | BUG_ON(end < cur); |
| 2950 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2951 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 2952 | this_bio_flag |= EXTENT_BIO_COMPRESSED; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2953 | extent_set_compress_type(&this_bio_flag, |
| 2954 | em->compress_type); |
| 2955 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2956 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2957 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
| 2958 | cur_end = min(extent_map_end(em) - 1, end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 2959 | iosize = ALIGN(iosize, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2960 | if (this_bio_flag & EXTENT_BIO_COMPRESSED) { |
| 2961 | disk_io_size = em->block_len; |
| 2962 | sector = em->block_start >> 9; |
| 2963 | } else { |
| 2964 | sector = (em->block_start + extent_offset) >> 9; |
| 2965 | disk_io_size = iosize; |
| 2966 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2967 | bdev = em->bdev; |
| 2968 | block_start = em->block_start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2969 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 2970 | block_start = EXTENT_MAP_HOLE; |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2971 | |
| 2972 | /* |
| 2973 | * If we have a file range that points to a compressed extent |
| 2974 | * and it's followed by a consecutive file range that points to |
| 2975 | * to the same compressed extent (possibly with a different |
| 2976 | * offset and/or length, so it either points to the whole extent |
| 2977 | * or only part of it), we must make sure we do not submit a |
| 2978 | * single bio to populate the pages for the 2 ranges because |
| 2979 | * this makes the compressed extent read zero out the pages |
| 2980 | * belonging to the 2nd range. Imagine the following scenario: |
| 2981 | * |
| 2982 | * File layout |
| 2983 | * [0 - 8K] [8K - 24K] |
| 2984 | * | | |
| 2985 | * | | |
| 2986 | * points to extent X, points to extent X, |
| 2987 | * offset 4K, length of 8K offset 0, length 16K |
| 2988 | * |
| 2989 | * [extent X, compressed length = 4K uncompressed length = 16K] |
| 2990 | * |
| 2991 | * If the bio to read the compressed extent covers both ranges, |
| 2992 | * it will decompress extent X into the pages belonging to the |
| 2993 | * first range and then it will stop, zeroing out the remaining |
| 2994 | * pages that belong to the other range that points to extent X. |
| 2995 | * So here we make sure we submit 2 bios, one for the first |
| 2996 | * range and another one for the third range. Both will target |
| 2997 | * the same physical extent from disk, but we can't currently |
| 2998 | * make the compressed bio endio callback populate the pages |
| 2999 | * for both ranges because each compressed bio is tightly |
| 3000 | * coupled with a single extent map, and each range can have |
| 3001 | * an extent map with a different offset value relative to the |
| 3002 | * uncompressed data of our extent and different lengths. This |
| 3003 | * is a corner case so we prioritize correctness over |
| 3004 | * non-optimal behavior (submitting 2 bios for the same extent). |
| 3005 | */ |
| 3006 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) && |
| 3007 | prev_em_start && *prev_em_start != (u64)-1 && |
| 3008 | *prev_em_start != em->orig_start) |
| 3009 | force_bio_submit = true; |
| 3010 | |
| 3011 | if (prev_em_start) |
| 3012 | *prev_em_start = em->orig_start; |
| 3013 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3014 | free_extent_map(em); |
| 3015 | em = NULL; |
| 3016 | |
| 3017 | /* we've found a hole, just zero and go on */ |
| 3018 | if (block_start == EXTENT_MAP_HOLE) { |
| 3019 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3020 | struct extent_state *cached = NULL; |
| 3021 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3022 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3023 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3024 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3025 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3026 | |
| 3027 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3028 | &cached, GFP_NOFS); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3029 | unlock_extent_cached(tree, cur, |
| 3030 | cur + iosize - 1, |
| 3031 | &cached, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3032 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3033 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3034 | continue; |
| 3035 | } |
| 3036 | /* the get_extent function already copied into the page */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3037 | if (test_range_bit(tree, cur, cur_end, |
| 3038 | EXTENT_UPTODATE, 1, NULL)) { |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 3039 | check_page_uptodate(tree, page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3040 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3041 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3042 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3043 | continue; |
| 3044 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3045 | /* we have an inline extent but it didn't get marked up |
| 3046 | * to date. Error out |
| 3047 | */ |
| 3048 | if (block_start == EXTENT_MAP_INLINE) { |
| 3049 | SetPageError(page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3050 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3051 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3052 | pg_offset += iosize; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3053 | continue; |
| 3054 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3055 | |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3056 | pnr -= page->index; |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 3057 | ret = submit_extent_page(rw, tree, NULL, page, |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3058 | sector, disk_io_size, pg_offset, |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3059 | bdev, bio, pnr, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3060 | end_bio_extent_readpage, mirror_num, |
| 3061 | *bio_flags, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3062 | this_bio_flag, |
| 3063 | force_bio_submit); |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3064 | if (!ret) { |
| 3065 | nr++; |
| 3066 | *bio_flags = this_bio_flag; |
| 3067 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3068 | SetPageError(page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3069 | unlock_extent(tree, cur, cur + iosize - 1); |
Josef Bacik | edd33c9 | 2012-10-05 16:40:32 -0400 | [diff] [blame] | 3070 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3071 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3072 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3073 | } |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 3074 | out: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3075 | if (!nr) { |
| 3076 | if (!PageError(page)) |
| 3077 | SetPageUptodate(page); |
| 3078 | unlock_page(page); |
| 3079 | } |
| 3080 | return 0; |
| 3081 | } |
| 3082 | |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3083 | static inline void __do_contiguous_readpages(struct extent_io_tree *tree, |
| 3084 | struct page *pages[], int nr_pages, |
| 3085 | u64 start, u64 end, |
| 3086 | get_extent_t *get_extent, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3087 | struct extent_map **em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3088 | struct bio **bio, int mirror_num, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3089 | unsigned long *bio_flags, int rw, |
| 3090 | u64 *prev_em_start) |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3091 | { |
| 3092 | struct inode *inode; |
| 3093 | struct btrfs_ordered_extent *ordered; |
| 3094 | int index; |
| 3095 | |
| 3096 | inode = pages[0]->mapping->host; |
| 3097 | while (1) { |
| 3098 | lock_extent(tree, start, end); |
| 3099 | ordered = btrfs_lookup_ordered_range(inode, start, |
| 3100 | end - start + 1); |
| 3101 | if (!ordered) |
| 3102 | break; |
| 3103 | unlock_extent(tree, start, end); |
| 3104 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3105 | btrfs_put_ordered_extent(ordered); |
| 3106 | } |
| 3107 | |
| 3108 | for (index = 0; index < nr_pages; index++) { |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3109 | __do_readpage(tree, pages[index], get_extent, em_cached, bio, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3110 | mirror_num, bio_flags, rw, prev_em_start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3111 | put_page(pages[index]); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3112 | } |
| 3113 | } |
| 3114 | |
| 3115 | static void __extent_readpages(struct extent_io_tree *tree, |
| 3116 | struct page *pages[], |
| 3117 | int nr_pages, get_extent_t *get_extent, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3118 | struct extent_map **em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3119 | struct bio **bio, int mirror_num, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3120 | unsigned long *bio_flags, int rw, |
| 3121 | u64 *prev_em_start) |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3122 | { |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 3123 | u64 start = 0; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3124 | u64 end = 0; |
| 3125 | u64 page_start; |
| 3126 | int index; |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 3127 | int first_index = 0; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3128 | |
| 3129 | for (index = 0; index < nr_pages; index++) { |
| 3130 | page_start = page_offset(pages[index]); |
| 3131 | if (!end) { |
| 3132 | start = page_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3133 | end = start + PAGE_SIZE - 1; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3134 | first_index = index; |
| 3135 | } else if (end + 1 == page_start) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3136 | end += PAGE_SIZE; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3137 | } else { |
| 3138 | __do_contiguous_readpages(tree, &pages[first_index], |
| 3139 | index - first_index, start, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3140 | end, get_extent, em_cached, |
| 3141 | bio, mirror_num, bio_flags, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3142 | rw, prev_em_start); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3143 | start = page_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3144 | end = start + PAGE_SIZE - 1; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3145 | first_index = index; |
| 3146 | } |
| 3147 | } |
| 3148 | |
| 3149 | if (end) |
| 3150 | __do_contiguous_readpages(tree, &pages[first_index], |
| 3151 | index - first_index, start, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3152 | end, get_extent, em_cached, bio, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3153 | mirror_num, bio_flags, rw, |
| 3154 | prev_em_start); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3155 | } |
| 3156 | |
| 3157 | static int __extent_read_full_page(struct extent_io_tree *tree, |
| 3158 | struct page *page, |
| 3159 | get_extent_t *get_extent, |
| 3160 | struct bio **bio, int mirror_num, |
| 3161 | unsigned long *bio_flags, int rw) |
| 3162 | { |
| 3163 | struct inode *inode = page->mapping->host; |
| 3164 | struct btrfs_ordered_extent *ordered; |
| 3165 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3166 | u64 end = start + PAGE_SIZE - 1; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3167 | int ret; |
| 3168 | |
| 3169 | while (1) { |
| 3170 | lock_extent(tree, start, end); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 3171 | ordered = btrfs_lookup_ordered_range(inode, start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3172 | PAGE_SIZE); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3173 | if (!ordered) |
| 3174 | break; |
| 3175 | unlock_extent(tree, start, end); |
| 3176 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3177 | btrfs_put_ordered_extent(ordered); |
| 3178 | } |
| 3179 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3180 | ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3181 | bio_flags, rw, NULL); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3182 | return ret; |
| 3183 | } |
| 3184 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3185 | 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] | 3186 | get_extent_t *get_extent, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3187 | { |
| 3188 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3189 | unsigned long bio_flags = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3190 | int ret; |
| 3191 | |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 3192 | ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num, |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 3193 | &bio_flags, READ); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3194 | if (bio) |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 3195 | ret = submit_one_bio(READ, bio, mirror_num, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3196 | return ret; |
| 3197 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3198 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3199 | static noinline void update_nr_written(struct page *page, |
| 3200 | struct writeback_control *wbc, |
| 3201 | unsigned long nr_written) |
| 3202 | { |
| 3203 | wbc->nr_to_write -= nr_written; |
| 3204 | if (wbc->range_cyclic || (wbc->nr_to_write > 0 && |
| 3205 | wbc->range_start == 0 && wbc->range_end == LLONG_MAX)) |
| 3206 | page->mapping->writeback_index = page->index + nr_written; |
| 3207 | } |
| 3208 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3209 | /* |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3210 | * helper for __extent_writepage, doing all of the delayed allocation setup. |
| 3211 | * |
| 3212 | * This returns 1 if our fill_delalloc function did all the work required |
| 3213 | * to write the page (copy into inline extent). In this case the IO has |
| 3214 | * been started and the page is already unlocked. |
| 3215 | * |
| 3216 | * This returns 0 if all went well (page still locked) |
| 3217 | * This returns < 0 if there were errors (page still locked) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3218 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3219 | static noinline_for_stack int writepage_delalloc(struct inode *inode, |
| 3220 | struct page *page, struct writeback_control *wbc, |
| 3221 | struct extent_page_data *epd, |
| 3222 | u64 delalloc_start, |
| 3223 | unsigned long *nr_written) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3224 | { |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3225 | struct extent_io_tree *tree = epd->tree; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3226 | u64 page_end = delalloc_start + PAGE_SIZE - 1; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3227 | u64 nr_delalloc; |
| 3228 | u64 delalloc_to_write = 0; |
| 3229 | u64 delalloc_end = 0; |
| 3230 | int ret; |
| 3231 | int page_started = 0; |
| 3232 | |
| 3233 | if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc) |
| 3234 | return 0; |
| 3235 | |
| 3236 | while (delalloc_end < page_end) { |
| 3237 | nr_delalloc = find_lock_delalloc_range(inode, tree, |
| 3238 | page, |
| 3239 | &delalloc_start, |
| 3240 | &delalloc_end, |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 3241 | BTRFS_MAX_EXTENT_SIZE); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3242 | if (nr_delalloc == 0) { |
| 3243 | delalloc_start = delalloc_end + 1; |
| 3244 | continue; |
| 3245 | } |
| 3246 | ret = tree->ops->fill_delalloc(inode, page, |
| 3247 | delalloc_start, |
| 3248 | delalloc_end, |
| 3249 | &page_started, |
| 3250 | nr_written); |
| 3251 | /* File system has been set read-only */ |
| 3252 | if (ret) { |
| 3253 | SetPageError(page); |
| 3254 | /* fill_delalloc should be return < 0 for error |
| 3255 | * but just in case, we use > 0 here meaning the |
| 3256 | * IO is started, so we don't want to return > 0 |
| 3257 | * unless things are going well. |
| 3258 | */ |
| 3259 | ret = ret < 0 ? ret : -EIO; |
| 3260 | goto done; |
| 3261 | } |
| 3262 | /* |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 3263 | * delalloc_end is already one less than the total length, so |
| 3264 | * we don't subtract one from PAGE_SIZE |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3265 | */ |
| 3266 | delalloc_to_write += (delalloc_end - delalloc_start + |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 3267 | PAGE_SIZE) >> PAGE_SHIFT; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3268 | delalloc_start = delalloc_end + 1; |
| 3269 | } |
| 3270 | if (wbc->nr_to_write < delalloc_to_write) { |
| 3271 | int thresh = 8192; |
| 3272 | |
| 3273 | if (delalloc_to_write < thresh * 2) |
| 3274 | thresh = delalloc_to_write; |
| 3275 | wbc->nr_to_write = min_t(u64, delalloc_to_write, |
| 3276 | thresh); |
| 3277 | } |
| 3278 | |
| 3279 | /* did the fill delalloc function already unlock and start |
| 3280 | * the IO? |
| 3281 | */ |
| 3282 | if (page_started) { |
| 3283 | /* |
| 3284 | * we've unlocked the page, so we can't update |
| 3285 | * the mapping's writeback index, just update |
| 3286 | * nr_to_write. |
| 3287 | */ |
| 3288 | wbc->nr_to_write -= *nr_written; |
| 3289 | return 1; |
| 3290 | } |
| 3291 | |
| 3292 | ret = 0; |
| 3293 | |
| 3294 | done: |
| 3295 | return ret; |
| 3296 | } |
| 3297 | |
| 3298 | /* |
| 3299 | * helper for __extent_writepage. This calls the writepage start hooks, |
| 3300 | * and does the loop to map the page into extents and bios. |
| 3301 | * |
| 3302 | * We return 1 if the IO is started and the page is unlocked, |
| 3303 | * 0 if all went well (page still locked) |
| 3304 | * < 0 if there were errors (page still locked) |
| 3305 | */ |
| 3306 | static noinline_for_stack int __extent_writepage_io(struct inode *inode, |
| 3307 | struct page *page, |
| 3308 | struct writeback_control *wbc, |
| 3309 | struct extent_page_data *epd, |
| 3310 | loff_t i_size, |
| 3311 | unsigned long nr_written, |
| 3312 | int write_flags, int *nr_ret) |
| 3313 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3314 | struct extent_io_tree *tree = epd->tree; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3315 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3316 | u64 page_end = start + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3317 | u64 end; |
| 3318 | u64 cur = start; |
| 3319 | u64 extent_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3320 | u64 block_start; |
| 3321 | u64 iosize; |
| 3322 | sector_t sector; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 3323 | struct extent_state *cached_state = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3324 | struct extent_map *em; |
| 3325 | struct block_device *bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3326 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3327 | size_t blocksize; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3328 | int ret = 0; |
| 3329 | int nr = 0; |
| 3330 | bool compressed; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3331 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3332 | if (tree->ops && tree->ops->writepage_start_hook) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3333 | ret = tree->ops->writepage_start_hook(page, start, |
| 3334 | page_end); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 3335 | if (ret) { |
| 3336 | /* Fixup worker will requeue */ |
| 3337 | if (ret == -EBUSY) |
| 3338 | wbc->pages_skipped++; |
| 3339 | else |
| 3340 | redirty_page_for_writepage(wbc, page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3341 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3342 | update_nr_written(page, wbc, nr_written); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3343 | unlock_page(page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3344 | ret = 1; |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3345 | goto done_unlocked; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3346 | } |
| 3347 | } |
| 3348 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3349 | /* |
| 3350 | * we don't want to touch the inode after unlocking the page, |
| 3351 | * so we update the mapping writeback index now |
| 3352 | */ |
| 3353 | update_nr_written(page, wbc, nr_written + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3354 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3355 | end = page_end; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3356 | if (i_size <= start) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3357 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 3358 | tree->ops->writepage_end_io_hook(page, start, |
| 3359 | page_end, NULL, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3360 | goto done; |
| 3361 | } |
| 3362 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3363 | blocksize = inode->i_sb->s_blocksize; |
| 3364 | |
| 3365 | while (cur <= end) { |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3366 | u64 em_end; |
| 3367 | if (cur >= i_size) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3368 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 3369 | tree->ops->writepage_end_io_hook(page, cur, |
| 3370 | page_end, NULL, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3371 | break; |
| 3372 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3373 | em = epd->get_extent(inode, page, pg_offset, cur, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3374 | end - cur + 1, 1); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3375 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3376 | SetPageError(page); |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 3377 | ret = PTR_ERR_OR_ZERO(em); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3378 | break; |
| 3379 | } |
| 3380 | |
| 3381 | extent_offset = cur - em->start; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3382 | em_end = extent_map_end(em); |
| 3383 | BUG_ON(em_end <= cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3384 | BUG_ON(end < cur); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3385 | iosize = min(em_end - cur, end - cur + 1); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3386 | iosize = ALIGN(iosize, blocksize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3387 | sector = (em->block_start + extent_offset) >> 9; |
| 3388 | bdev = em->bdev; |
| 3389 | block_start = em->block_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3390 | compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3391 | free_extent_map(em); |
| 3392 | em = NULL; |
| 3393 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3394 | /* |
| 3395 | * compressed and inline extents are written through other |
| 3396 | * paths in the FS |
| 3397 | */ |
| 3398 | if (compressed || block_start == EXTENT_MAP_HOLE || |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3399 | block_start == EXTENT_MAP_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3400 | /* |
| 3401 | * end_io notification does not happen here for |
| 3402 | * compressed extents |
| 3403 | */ |
| 3404 | if (!compressed && tree->ops && |
| 3405 | tree->ops->writepage_end_io_hook) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3406 | tree->ops->writepage_end_io_hook(page, cur, |
| 3407 | cur + iosize - 1, |
| 3408 | NULL, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3409 | else if (compressed) { |
| 3410 | /* we don't want to end_page_writeback on |
| 3411 | * a compressed extent. this happens |
| 3412 | * elsewhere |
| 3413 | */ |
| 3414 | nr++; |
| 3415 | } |
| 3416 | |
| 3417 | cur += iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3418 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3419 | continue; |
| 3420 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3421 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3422 | if (tree->ops && tree->ops->writepage_io_hook) { |
| 3423 | ret = tree->ops->writepage_io_hook(page, cur, |
| 3424 | cur + iosize - 1); |
| 3425 | } else { |
| 3426 | ret = 0; |
| 3427 | } |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 3428 | if (ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3429 | SetPageError(page); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 3430 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3431 | unsigned long max_nr = (i_size >> PAGE_SHIFT) + 1; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3432 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3433 | set_range_writeback(tree, cur, cur + iosize - 1); |
| 3434 | if (!PageWriteback(page)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3435 | btrfs_err(BTRFS_I(inode)->root->fs_info, |
| 3436 | "page %lu not writeback, cur %llu end %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3437 | page->index, cur, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3438 | } |
| 3439 | |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 3440 | ret = submit_extent_page(write_flags, tree, wbc, page, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3441 | sector, iosize, pg_offset, |
| 3442 | bdev, &epd->bio, max_nr, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3443 | end_bio_extent_writepage, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3444 | 0, 0, 0, false); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3445 | if (ret) |
| 3446 | SetPageError(page); |
| 3447 | } |
| 3448 | cur = cur + iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3449 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3450 | nr++; |
| 3451 | } |
| 3452 | done: |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3453 | *nr_ret = nr; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3454 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3455 | done_unlocked: |
| 3456 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 3457 | /* drop our reference on any cached states */ |
| 3458 | free_extent_state(cached_state); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3459 | return ret; |
| 3460 | } |
| 3461 | |
| 3462 | /* |
| 3463 | * the writepage semantics are similar to regular writepage. extent |
| 3464 | * records are inserted to lock ranges in the tree, and as dirty areas |
| 3465 | * are found, they are marked writeback. Then the lock bits are removed |
| 3466 | * and the end_io handler clears the writeback ranges |
| 3467 | */ |
| 3468 | static int __extent_writepage(struct page *page, struct writeback_control *wbc, |
| 3469 | void *data) |
| 3470 | { |
| 3471 | struct inode *inode = page->mapping->host; |
| 3472 | struct extent_page_data *epd = data; |
| 3473 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3474 | u64 page_end = start + PAGE_SIZE - 1; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3475 | int ret; |
| 3476 | int nr = 0; |
| 3477 | size_t pg_offset = 0; |
| 3478 | loff_t i_size = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3479 | unsigned long end_index = i_size >> PAGE_SHIFT; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3480 | int write_flags; |
| 3481 | unsigned long nr_written = 0; |
| 3482 | |
| 3483 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3484 | write_flags = WRITE_SYNC; |
| 3485 | else |
| 3486 | write_flags = WRITE; |
| 3487 | |
| 3488 | trace___extent_writepage(page, inode, wbc); |
| 3489 | |
| 3490 | WARN_ON(!PageLocked(page)); |
| 3491 | |
| 3492 | ClearPageError(page); |
| 3493 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3494 | pg_offset = i_size & (PAGE_SIZE - 1); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3495 | if (page->index > end_index || |
| 3496 | (page->index == end_index && !pg_offset)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3497 | page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3498 | unlock_page(page); |
| 3499 | return 0; |
| 3500 | } |
| 3501 | |
| 3502 | if (page->index == end_index) { |
| 3503 | char *userpage; |
| 3504 | |
| 3505 | userpage = kmap_atomic(page); |
| 3506 | memset(userpage + pg_offset, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3507 | PAGE_SIZE - pg_offset); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3508 | kunmap_atomic(userpage); |
| 3509 | flush_dcache_page(page); |
| 3510 | } |
| 3511 | |
| 3512 | pg_offset = 0; |
| 3513 | |
| 3514 | set_page_extent_mapped(page); |
| 3515 | |
| 3516 | ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written); |
| 3517 | if (ret == 1) |
| 3518 | goto done_unlocked; |
| 3519 | if (ret) |
| 3520 | goto done; |
| 3521 | |
| 3522 | ret = __extent_writepage_io(inode, page, wbc, epd, |
| 3523 | i_size, nr_written, write_flags, &nr); |
| 3524 | if (ret == 1) |
| 3525 | goto done_unlocked; |
| 3526 | |
| 3527 | done: |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3528 | if (nr == 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3529 | /* make sure the mapping tag for page dirty gets cleared */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3530 | set_page_writeback(page); |
| 3531 | end_page_writeback(page); |
| 3532 | } |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 3533 | if (PageError(page)) { |
| 3534 | ret = ret < 0 ? ret : -EIO; |
| 3535 | end_extent_writepage(page, ret, start, page_end); |
| 3536 | } |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 3537 | unlock_page(page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3538 | return ret; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3539 | |
| 3540 | done_unlocked: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3541 | return 0; |
| 3542 | } |
| 3543 | |
Josef Bacik | fd8b2b6 | 2013-04-24 16:41:19 -0400 | [diff] [blame] | 3544 | void wait_on_extent_buffer_writeback(struct extent_buffer *eb) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3545 | { |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 3546 | wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK, |
| 3547 | TASK_UNINTERRUPTIBLE); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3548 | } |
| 3549 | |
Chris Mason | 0e378df | 2014-05-19 20:55:27 -0700 | [diff] [blame] | 3550 | static noinline_for_stack int |
| 3551 | lock_extent_buffer_for_io(struct extent_buffer *eb, |
| 3552 | struct btrfs_fs_info *fs_info, |
| 3553 | struct extent_page_data *epd) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3554 | { |
| 3555 | unsigned long i, num_pages; |
| 3556 | int flush = 0; |
| 3557 | int ret = 0; |
| 3558 | |
| 3559 | if (!btrfs_try_tree_write_lock(eb)) { |
| 3560 | flush = 1; |
| 3561 | flush_write_bio(epd); |
| 3562 | btrfs_tree_lock(eb); |
| 3563 | } |
| 3564 | |
| 3565 | if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) { |
| 3566 | btrfs_tree_unlock(eb); |
| 3567 | if (!epd->sync_io) |
| 3568 | return 0; |
| 3569 | if (!flush) { |
| 3570 | flush_write_bio(epd); |
| 3571 | flush = 1; |
| 3572 | } |
Chris Mason | a098d8e | 2012-03-21 12:09:56 -0400 | [diff] [blame] | 3573 | while (1) { |
| 3574 | wait_on_extent_buffer_writeback(eb); |
| 3575 | btrfs_tree_lock(eb); |
| 3576 | if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) |
| 3577 | break; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3578 | btrfs_tree_unlock(eb); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3579 | } |
| 3580 | } |
| 3581 | |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3582 | /* |
| 3583 | * We need to do this to prevent races in people who check if the eb is |
| 3584 | * under IO since we can end up having no IO bits set for a short period |
| 3585 | * of time. |
| 3586 | */ |
| 3587 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3588 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) { |
| 3589 | set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3590 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3591 | btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); |
Miao Xie | e2d8452 | 2013-01-29 10:09:20 +0000 | [diff] [blame] | 3592 | __percpu_counter_add(&fs_info->dirty_metadata_bytes, |
| 3593 | -eb->len, |
| 3594 | fs_info->dirty_metadata_batch); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3595 | ret = 1; |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3596 | } else { |
| 3597 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3598 | } |
| 3599 | |
| 3600 | btrfs_tree_unlock(eb); |
| 3601 | |
| 3602 | if (!ret) |
| 3603 | return ret; |
| 3604 | |
| 3605 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3606 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 3607 | struct page *p = eb->pages[i]; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3608 | |
| 3609 | if (!trylock_page(p)) { |
| 3610 | if (!flush) { |
| 3611 | flush_write_bio(epd); |
| 3612 | flush = 1; |
| 3613 | } |
| 3614 | lock_page(p); |
| 3615 | } |
| 3616 | } |
| 3617 | |
| 3618 | return ret; |
| 3619 | } |
| 3620 | |
| 3621 | static void end_extent_buffer_writeback(struct extent_buffer *eb) |
| 3622 | { |
| 3623 | clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 3624 | smp_mb__after_atomic(); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3625 | wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK); |
| 3626 | } |
| 3627 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3628 | static void set_btree_ioerr(struct page *page) |
| 3629 | { |
| 3630 | struct extent_buffer *eb = (struct extent_buffer *)page->private; |
| 3631 | struct btrfs_inode *btree_ino = BTRFS_I(eb->fs_info->btree_inode); |
| 3632 | |
| 3633 | SetPageError(page); |
| 3634 | if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) |
| 3635 | return; |
| 3636 | |
| 3637 | /* |
| 3638 | * If writeback for a btree extent that doesn't belong to a log tree |
| 3639 | * failed, increment the counter transaction->eb_write_errors. |
| 3640 | * We do this because while the transaction is running and before it's |
| 3641 | * committing (when we call filemap_fdata[write|wait]_range against |
| 3642 | * the btree inode), we might have |
| 3643 | * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it |
| 3644 | * returns an error or an error happens during writeback, when we're |
| 3645 | * committing the transaction we wouldn't know about it, since the pages |
| 3646 | * can be no longer dirty nor marked anymore for writeback (if a |
| 3647 | * subsequent modification to the extent buffer didn't happen before the |
| 3648 | * transaction commit), which makes filemap_fdata[write|wait]_range not |
| 3649 | * able to find the pages tagged with SetPageError at transaction |
| 3650 | * commit time. So if this happens we must abort the transaction, |
| 3651 | * otherwise we commit a super block with btree roots that point to |
| 3652 | * btree nodes/leafs whose content on disk is invalid - either garbage |
| 3653 | * or the content of some node/leaf from a past generation that got |
| 3654 | * cowed or deleted and is no longer valid. |
| 3655 | * |
| 3656 | * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would |
| 3657 | * not be enough - we need to distinguish between log tree extents vs |
| 3658 | * non-log tree extents, and the next filemap_fdatawait_range() call |
| 3659 | * will catch and clear such errors in the mapping - and that call might |
| 3660 | * be from a log sync and not from a transaction commit. Also, checking |
| 3661 | * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is |
| 3662 | * not done and would not be reliable - the eb might have been released |
| 3663 | * from memory and reading it back again means that flag would not be |
| 3664 | * set (since it's a runtime flag, not persisted on disk). |
| 3665 | * |
| 3666 | * Using the flags below in the btree inode also makes us achieve the |
| 3667 | * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started |
| 3668 | * writeback for all dirty pages and before filemap_fdatawait_range() |
| 3669 | * is called, the writeback for all dirty pages had already finished |
| 3670 | * with errors - because we were not using AS_EIO/AS_ENOSPC, |
| 3671 | * filemap_fdatawait_range() would return success, as it could not know |
| 3672 | * that writeback errors happened (the pages were no longer tagged for |
| 3673 | * writeback). |
| 3674 | */ |
| 3675 | switch (eb->log_index) { |
| 3676 | case -1: |
| 3677 | set_bit(BTRFS_INODE_BTREE_ERR, &btree_ino->runtime_flags); |
| 3678 | break; |
| 3679 | case 0: |
| 3680 | set_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags); |
| 3681 | break; |
| 3682 | case 1: |
| 3683 | set_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags); |
| 3684 | break; |
| 3685 | default: |
| 3686 | BUG(); /* unexpected, logic error */ |
| 3687 | } |
| 3688 | } |
| 3689 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3690 | static void end_bio_extent_buffer_writepage(struct bio *bio) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3691 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3692 | struct bio_vec *bvec; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3693 | struct extent_buffer *eb; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3694 | int i, done; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3695 | |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3696 | bio_for_each_segment_all(bvec, bio, i) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3697 | struct page *page = bvec->bv_page; |
| 3698 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3699 | eb = (struct extent_buffer *)page->private; |
| 3700 | BUG_ON(!eb); |
| 3701 | done = atomic_dec_and_test(&eb->io_pages); |
| 3702 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3703 | if (bio->bi_error || |
| 3704 | test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3705 | ClearPageUptodate(page); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3706 | set_btree_ioerr(page); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3707 | } |
| 3708 | |
| 3709 | end_page_writeback(page); |
| 3710 | |
| 3711 | if (!done) |
| 3712 | continue; |
| 3713 | |
| 3714 | end_extent_buffer_writeback(eb); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3715 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3716 | |
| 3717 | bio_put(bio); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3718 | } |
| 3719 | |
Chris Mason | 0e378df | 2014-05-19 20:55:27 -0700 | [diff] [blame] | 3720 | static noinline_for_stack int write_one_eb(struct extent_buffer *eb, |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3721 | struct btrfs_fs_info *fs_info, |
| 3722 | struct writeback_control *wbc, |
| 3723 | struct extent_page_data *epd) |
| 3724 | { |
| 3725 | struct block_device *bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 3726 | 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] | 3727 | u64 offset = eb->start; |
| 3728 | unsigned long i, num_pages; |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3729 | unsigned long bio_flags = 0; |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 3730 | int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META; |
Josef Bacik | d7dbe9e | 2012-04-23 14:00:51 -0400 | [diff] [blame] | 3731 | int ret = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3732 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3733 | clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3734 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3735 | atomic_set(&eb->io_pages, num_pages); |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3736 | if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID) |
| 3737 | bio_flags = EXTENT_BIO_TREE_LOG; |
| 3738 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3739 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 3740 | struct page *p = eb->pages[i]; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3741 | |
| 3742 | clear_page_dirty_for_io(p); |
| 3743 | set_page_writeback(p); |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 3744 | ret = submit_extent_page(rw, tree, wbc, p, offset >> 9, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3745 | PAGE_SIZE, 0, bdev, &epd->bio, |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3746 | -1, end_bio_extent_buffer_writepage, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3747 | 0, epd->bio_flags, bio_flags, false); |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3748 | epd->bio_flags = bio_flags; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3749 | if (ret) { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3750 | set_btree_ioerr(p); |
Filipe Manana | 55e3bd2 | 2014-09-22 17:41:04 +0100 | [diff] [blame] | 3751 | end_page_writeback(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3752 | if (atomic_sub_and_test(num_pages - i, &eb->io_pages)) |
| 3753 | end_extent_buffer_writeback(eb); |
| 3754 | ret = -EIO; |
| 3755 | break; |
| 3756 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3757 | offset += PAGE_SIZE; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3758 | update_nr_written(p, wbc, 1); |
| 3759 | unlock_page(p); |
| 3760 | } |
| 3761 | |
| 3762 | if (unlikely(ret)) { |
| 3763 | for (; i < num_pages; i++) { |
Chris Mason | bbf65cf | 2014-10-04 09:56:45 -0700 | [diff] [blame] | 3764 | struct page *p = eb->pages[i]; |
Liu Bo | 8146502 | 2014-09-23 22:22:33 +0800 | [diff] [blame] | 3765 | clear_page_dirty_for_io(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3766 | unlock_page(p); |
| 3767 | } |
| 3768 | } |
| 3769 | |
| 3770 | return ret; |
| 3771 | } |
| 3772 | |
| 3773 | int btree_write_cache_pages(struct address_space *mapping, |
| 3774 | struct writeback_control *wbc) |
| 3775 | { |
| 3776 | struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree; |
| 3777 | struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info; |
| 3778 | struct extent_buffer *eb, *prev_eb = NULL; |
| 3779 | struct extent_page_data epd = { |
| 3780 | .bio = NULL, |
| 3781 | .tree = tree, |
| 3782 | .extent_locked = 0, |
| 3783 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3784 | .bio_flags = 0, |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3785 | }; |
| 3786 | int ret = 0; |
| 3787 | int done = 0; |
| 3788 | int nr_to_write_done = 0; |
| 3789 | struct pagevec pvec; |
| 3790 | int nr_pages; |
| 3791 | pgoff_t index; |
| 3792 | pgoff_t end; /* Inclusive */ |
| 3793 | int scanned = 0; |
| 3794 | int tag; |
| 3795 | |
| 3796 | pagevec_init(&pvec, 0); |
| 3797 | if (wbc->range_cyclic) { |
| 3798 | index = mapping->writeback_index; /* Start from prev offset */ |
| 3799 | end = -1; |
| 3800 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3801 | index = wbc->range_start >> PAGE_SHIFT; |
| 3802 | end = wbc->range_end >> PAGE_SHIFT; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3803 | scanned = 1; |
| 3804 | } |
| 3805 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3806 | tag = PAGECACHE_TAG_TOWRITE; |
| 3807 | else |
| 3808 | tag = PAGECACHE_TAG_DIRTY; |
| 3809 | retry: |
| 3810 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3811 | tag_pages_for_writeback(mapping, index, end); |
| 3812 | while (!done && !nr_to_write_done && (index <= end) && |
| 3813 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
| 3814 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { |
| 3815 | unsigned i; |
| 3816 | |
| 3817 | scanned = 1; |
| 3818 | for (i = 0; i < nr_pages; i++) { |
| 3819 | struct page *page = pvec.pages[i]; |
| 3820 | |
| 3821 | if (!PagePrivate(page)) |
| 3822 | continue; |
| 3823 | |
| 3824 | if (!wbc->range_cyclic && page->index > end) { |
| 3825 | done = 1; |
| 3826 | break; |
| 3827 | } |
| 3828 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3829 | spin_lock(&mapping->private_lock); |
| 3830 | if (!PagePrivate(page)) { |
| 3831 | spin_unlock(&mapping->private_lock); |
| 3832 | continue; |
| 3833 | } |
| 3834 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3835 | eb = (struct extent_buffer *)page->private; |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3836 | |
| 3837 | /* |
| 3838 | * Shouldn't happen and normally this would be a BUG_ON |
| 3839 | * but no sense in crashing the users box for something |
| 3840 | * we can survive anyway. |
| 3841 | */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3842 | if (WARN_ON(!eb)) { |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3843 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3844 | continue; |
| 3845 | } |
| 3846 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3847 | if (eb == prev_eb) { |
| 3848 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3849 | continue; |
| 3850 | } |
| 3851 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3852 | ret = atomic_inc_not_zero(&eb->refs); |
| 3853 | spin_unlock(&mapping->private_lock); |
| 3854 | if (!ret) |
| 3855 | continue; |
| 3856 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3857 | prev_eb = eb; |
| 3858 | ret = lock_extent_buffer_for_io(eb, fs_info, &epd); |
| 3859 | if (!ret) { |
| 3860 | free_extent_buffer(eb); |
| 3861 | continue; |
| 3862 | } |
| 3863 | |
| 3864 | ret = write_one_eb(eb, fs_info, wbc, &epd); |
| 3865 | if (ret) { |
| 3866 | done = 1; |
| 3867 | free_extent_buffer(eb); |
| 3868 | break; |
| 3869 | } |
| 3870 | free_extent_buffer(eb); |
| 3871 | |
| 3872 | /* |
| 3873 | * the filesystem may choose to bump up nr_to_write. |
| 3874 | * We have to make sure to honor the new nr_to_write |
| 3875 | * at any time |
| 3876 | */ |
| 3877 | nr_to_write_done = wbc->nr_to_write <= 0; |
| 3878 | } |
| 3879 | pagevec_release(&pvec); |
| 3880 | cond_resched(); |
| 3881 | } |
| 3882 | if (!scanned && !done) { |
| 3883 | /* |
| 3884 | * We hit the last page and there is more work to be done: wrap |
| 3885 | * back to the start of the file |
| 3886 | */ |
| 3887 | scanned = 1; |
| 3888 | index = 0; |
| 3889 | goto retry; |
| 3890 | } |
| 3891 | flush_write_bio(&epd); |
| 3892 | return ret; |
| 3893 | } |
| 3894 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3895 | /** |
| 3896 | * write_cache_pages - walk the list of dirty pages of the given address space and write all of them. |
| 3897 | * @mapping: address space structure to write |
| 3898 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
| 3899 | * @writepage: function called for each page |
| 3900 | * @data: data passed to writepage function |
| 3901 | * |
| 3902 | * If a page is already under I/O, write_cache_pages() skips it, even |
| 3903 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, |
| 3904 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() |
| 3905 | * and msync() need to guarantee that all the data which was dirty at the time |
| 3906 | * the call was made get new I/O started against them. If wbc->sync_mode is |
| 3907 | * WB_SYNC_ALL then we were called for data integrity and we must wait for |
| 3908 | * existing IO to complete. |
| 3909 | */ |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3910 | static int extent_write_cache_pages(struct extent_io_tree *tree, |
| 3911 | struct address_space *mapping, |
| 3912 | struct writeback_control *wbc, |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3913 | writepage_t writepage, void *data, |
| 3914 | void (*flush_fn)(void *)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3915 | { |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 3916 | struct inode *inode = mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3917 | int ret = 0; |
| 3918 | int done = 0; |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 3919 | int err = 0; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3920 | int nr_to_write_done = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3921 | struct pagevec pvec; |
| 3922 | int nr_pages; |
| 3923 | pgoff_t index; |
| 3924 | pgoff_t end; /* Inclusive */ |
| 3925 | int scanned = 0; |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3926 | int tag; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3927 | |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 3928 | /* |
| 3929 | * We have to hold onto the inode so that ordered extents can do their |
| 3930 | * work when the IO finishes. The alternative to this is failing to add |
| 3931 | * an ordered extent if the igrab() fails there and that is a huge pain |
| 3932 | * to deal with, so instead just hold onto the inode throughout the |
| 3933 | * writepages operation. If it fails here we are freeing up the inode |
| 3934 | * anyway and we'd rather not waste our time writing out stuff that is |
| 3935 | * going to be truncated anyway. |
| 3936 | */ |
| 3937 | if (!igrab(inode)) |
| 3938 | return 0; |
| 3939 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3940 | pagevec_init(&pvec, 0); |
| 3941 | if (wbc->range_cyclic) { |
| 3942 | index = mapping->writeback_index; /* Start from prev offset */ |
| 3943 | end = -1; |
| 3944 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3945 | index = wbc->range_start >> PAGE_SHIFT; |
| 3946 | end = wbc->range_end >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3947 | scanned = 1; |
| 3948 | } |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3949 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3950 | tag = PAGECACHE_TAG_TOWRITE; |
| 3951 | else |
| 3952 | tag = PAGECACHE_TAG_DIRTY; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3953 | retry: |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3954 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3955 | tag_pages_for_writeback(mapping, index, end); |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3956 | while (!done && !nr_to_write_done && (index <= end) && |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3957 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
| 3958 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3959 | unsigned i; |
| 3960 | |
| 3961 | scanned = 1; |
| 3962 | for (i = 0; i < nr_pages; i++) { |
| 3963 | struct page *page = pvec.pages[i]; |
| 3964 | |
| 3965 | /* |
| 3966 | * At this point we hold neither mapping->tree_lock nor |
| 3967 | * lock on the page itself: the page may be truncated or |
| 3968 | * invalidated (changing page->mapping to NULL), or even |
| 3969 | * swizzled back from swapper_space to tmpfs file |
| 3970 | * mapping |
| 3971 | */ |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3972 | if (!trylock_page(page)) { |
| 3973 | flush_fn(data); |
| 3974 | lock_page(page); |
Chris Mason | 01d658f | 2011-11-01 10:08:06 -0400 | [diff] [blame] | 3975 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3976 | |
| 3977 | if (unlikely(page->mapping != mapping)) { |
| 3978 | unlock_page(page); |
| 3979 | continue; |
| 3980 | } |
| 3981 | |
| 3982 | if (!wbc->range_cyclic && page->index > end) { |
| 3983 | done = 1; |
| 3984 | unlock_page(page); |
| 3985 | continue; |
| 3986 | } |
| 3987 | |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3988 | if (wbc->sync_mode != WB_SYNC_NONE) { |
Chris Mason | 0e6bd95 | 2008-11-20 10:46:35 -0500 | [diff] [blame] | 3989 | if (PageWriteback(page)) |
| 3990 | flush_fn(data); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3991 | wait_on_page_writeback(page); |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3992 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3993 | |
| 3994 | if (PageWriteback(page) || |
| 3995 | !clear_page_dirty_for_io(page)) { |
| 3996 | unlock_page(page); |
| 3997 | continue; |
| 3998 | } |
| 3999 | |
| 4000 | ret = (*writepage)(page, wbc, data); |
| 4001 | |
| 4002 | if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) { |
| 4003 | unlock_page(page); |
| 4004 | ret = 0; |
| 4005 | } |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 4006 | if (!err && ret < 0) |
| 4007 | err = ret; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 4008 | |
| 4009 | /* |
| 4010 | * the filesystem may choose to bump up nr_to_write. |
| 4011 | * We have to make sure to honor the new nr_to_write |
| 4012 | * at any time |
| 4013 | */ |
| 4014 | nr_to_write_done = wbc->nr_to_write <= 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4015 | } |
| 4016 | pagevec_release(&pvec); |
| 4017 | cond_resched(); |
| 4018 | } |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 4019 | if (!scanned && !done && !err) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4020 | /* |
| 4021 | * We hit the last page and there is more work to be done: wrap |
| 4022 | * back to the start of the file |
| 4023 | */ |
| 4024 | scanned = 1; |
| 4025 | index = 0; |
| 4026 | goto retry; |
| 4027 | } |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 4028 | btrfs_add_delayed_iput(inode); |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 4029 | return err; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4030 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4031 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4032 | static void flush_epd_write_bio(struct extent_page_data *epd) |
| 4033 | { |
| 4034 | if (epd->bio) { |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 4035 | int rw = WRITE; |
| 4036 | int ret; |
| 4037 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4038 | if (epd->sync_io) |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 4039 | rw = WRITE_SYNC; |
| 4040 | |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 4041 | ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4042 | BUG_ON(ret < 0); /* -ENOMEM */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4043 | epd->bio = NULL; |
| 4044 | } |
| 4045 | } |
| 4046 | |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4047 | static noinline void flush_write_bio(void *data) |
| 4048 | { |
| 4049 | struct extent_page_data *epd = data; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4050 | flush_epd_write_bio(epd); |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4051 | } |
| 4052 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4053 | int extent_write_full_page(struct extent_io_tree *tree, struct page *page, |
| 4054 | get_extent_t *get_extent, |
| 4055 | struct writeback_control *wbc) |
| 4056 | { |
| 4057 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4058 | struct extent_page_data epd = { |
| 4059 | .bio = NULL, |
| 4060 | .tree = tree, |
| 4061 | .get_extent = get_extent, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4062 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4063 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 4064 | .bio_flags = 0, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4065 | }; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4066 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4067 | ret = __extent_writepage(page, wbc, &epd); |
| 4068 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4069 | flush_epd_write_bio(&epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4070 | return ret; |
| 4071 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4072 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4073 | int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode, |
| 4074 | u64 start, u64 end, get_extent_t *get_extent, |
| 4075 | int mode) |
| 4076 | { |
| 4077 | int ret = 0; |
| 4078 | struct address_space *mapping = inode->i_mapping; |
| 4079 | struct page *page; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4080 | unsigned long nr_pages = (end - start + PAGE_SIZE) >> |
| 4081 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4082 | |
| 4083 | struct extent_page_data epd = { |
| 4084 | .bio = NULL, |
| 4085 | .tree = tree, |
| 4086 | .get_extent = get_extent, |
| 4087 | .extent_locked = 1, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4088 | .sync_io = mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 4089 | .bio_flags = 0, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4090 | }; |
| 4091 | struct writeback_control wbc_writepages = { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4092 | .sync_mode = mode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4093 | .nr_to_write = nr_pages * 2, |
| 4094 | .range_start = start, |
| 4095 | .range_end = end + 1, |
| 4096 | }; |
| 4097 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4098 | while (start <= end) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4099 | page = find_get_page(mapping, start >> PAGE_SHIFT); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4100 | if (clear_page_dirty_for_io(page)) |
| 4101 | ret = __extent_writepage(page, &wbc_writepages, &epd); |
| 4102 | else { |
| 4103 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 4104 | tree->ops->writepage_end_io_hook(page, start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4105 | start + PAGE_SIZE - 1, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4106 | NULL, 1); |
| 4107 | unlock_page(page); |
| 4108 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4109 | put_page(page); |
| 4110 | start += PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4111 | } |
| 4112 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4113 | flush_epd_write_bio(&epd); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4114 | return ret; |
| 4115 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4116 | |
| 4117 | int extent_writepages(struct extent_io_tree *tree, |
| 4118 | struct address_space *mapping, |
| 4119 | get_extent_t *get_extent, |
| 4120 | struct writeback_control *wbc) |
| 4121 | { |
| 4122 | int ret = 0; |
| 4123 | struct extent_page_data epd = { |
| 4124 | .bio = NULL, |
| 4125 | .tree = tree, |
| 4126 | .get_extent = get_extent, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4127 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4128 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 4129 | .bio_flags = 0, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4130 | }; |
| 4131 | |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 4132 | ret = extent_write_cache_pages(tree, mapping, wbc, |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4133 | __extent_writepage, &epd, |
| 4134 | flush_write_bio); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4135 | flush_epd_write_bio(&epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4136 | return ret; |
| 4137 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4138 | |
| 4139 | int extent_readpages(struct extent_io_tree *tree, |
| 4140 | struct address_space *mapping, |
| 4141 | struct list_head *pages, unsigned nr_pages, |
| 4142 | get_extent_t get_extent) |
| 4143 | { |
| 4144 | struct bio *bio = NULL; |
| 4145 | unsigned page_idx; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4146 | unsigned long bio_flags = 0; |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4147 | struct page *pagepool[16]; |
| 4148 | struct page *page; |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4149 | struct extent_map *em_cached = NULL; |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4150 | int nr = 0; |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 4151 | u64 prev_em_start = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4152 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4153 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4154 | page = list_entry(pages->prev, struct page, lru); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4155 | |
| 4156 | prefetchw(&page->flags); |
| 4157 | list_del(&page->lru); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4158 | if (add_to_page_cache_lru(page, mapping, |
Itaru Kitayama | 43e817a | 2011-04-25 19:43:51 -0400 | [diff] [blame] | 4159 | page->index, GFP_NOFS)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4160 | put_page(page); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4161 | continue; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4162 | } |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4163 | |
| 4164 | pagepool[nr++] = page; |
| 4165 | if (nr < ARRAY_SIZE(pagepool)) |
| 4166 | continue; |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4167 | __extent_readpages(tree, pagepool, nr, get_extent, &em_cached, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 4168 | &bio, 0, &bio_flags, READ, &prev_em_start); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4169 | nr = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4170 | } |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 4171 | if (nr) |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4172 | __extent_readpages(tree, pagepool, nr, get_extent, &em_cached, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 4173 | &bio, 0, &bio_flags, READ, &prev_em_start); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4174 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4175 | if (em_cached) |
| 4176 | free_extent_map(em_cached); |
| 4177 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4178 | BUG_ON(!list_empty(pages)); |
| 4179 | if (bio) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4180 | return submit_one_bio(READ, bio, 0, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4181 | return 0; |
| 4182 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4183 | |
| 4184 | /* |
| 4185 | * basic invalidatepage code, this waits on any locked or writeback |
| 4186 | * ranges corresponding to the page, and then deletes any extent state |
| 4187 | * records from the tree |
| 4188 | */ |
| 4189 | int extent_invalidatepage(struct extent_io_tree *tree, |
| 4190 | struct page *page, unsigned long offset) |
| 4191 | { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4192 | struct extent_state *cached_state = NULL; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4193 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4194 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4195 | size_t blocksize = page->mapping->host->i_sb->s_blocksize; |
| 4196 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4197 | start += ALIGN(offset, blocksize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4198 | if (start > end) |
| 4199 | return 0; |
| 4200 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 4201 | lock_extent_bits(tree, start, end, &cached_state); |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 4202 | wait_on_page_writeback(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4203 | clear_extent_bit(tree, start, end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 4204 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4205 | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4206 | 1, 1, &cached_state, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4207 | return 0; |
| 4208 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4209 | |
| 4210 | /* |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4211 | * a helper for releasepage, this tests for areas of the page that |
| 4212 | * are locked or under IO and drops the related state bits if it is safe |
| 4213 | * to drop the page. |
| 4214 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 4215 | static int try_release_extent_state(struct extent_map_tree *map, |
| 4216 | struct extent_io_tree *tree, |
| 4217 | struct page *page, gfp_t mask) |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4218 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4219 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4220 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4221 | int ret = 1; |
| 4222 | |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 4223 | if (test_range_bit(tree, start, end, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4224 | EXTENT_IOBITS, 0, NULL)) |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4225 | ret = 0; |
| 4226 | else { |
| 4227 | if ((mask & GFP_NOFS) == GFP_NOFS) |
| 4228 | mask = GFP_NOFS; |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 4229 | /* |
| 4230 | * at this point we can safely clear everything except the |
| 4231 | * locked bit and the nodatasum bit |
| 4232 | */ |
Chris Mason | e3f24cc | 2011-02-14 12:52:08 -0500 | [diff] [blame] | 4233 | ret = clear_extent_bit(tree, start, end, |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 4234 | ~(EXTENT_LOCKED | EXTENT_NODATASUM), |
| 4235 | 0, 0, NULL, mask); |
Chris Mason | e3f24cc | 2011-02-14 12:52:08 -0500 | [diff] [blame] | 4236 | |
| 4237 | /* if clear_extent_bit failed for enomem reasons, |
| 4238 | * we can't allow the release to continue. |
| 4239 | */ |
| 4240 | if (ret < 0) |
| 4241 | ret = 0; |
| 4242 | else |
| 4243 | ret = 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4244 | } |
| 4245 | return ret; |
| 4246 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4247 | |
| 4248 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4249 | * a helper for releasepage. As long as there are no locked extents |
| 4250 | * in the range corresponding to the page, both state records and extent |
| 4251 | * map records are removed |
| 4252 | */ |
| 4253 | int try_release_extent_mapping(struct extent_map_tree *map, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4254 | struct extent_io_tree *tree, struct page *page, |
| 4255 | gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4256 | { |
| 4257 | struct extent_map *em; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4258 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4259 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4260 | |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 4261 | if (gfpflags_allow_blocking(mask) && |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4262 | page->mapping->host->i_size > SZ_16M) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4263 | u64 len; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4264 | while (start <= end) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4265 | len = end - start + 1; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4266 | write_lock(&map->lock); |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4267 | em = lookup_extent_mapping(map, start, len); |
Tsutomu Itoh | 285190d | 2012-02-16 16:23:58 +0900 | [diff] [blame] | 4268 | if (!em) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4269 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4270 | break; |
| 4271 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 4272 | if (test_bit(EXTENT_FLAG_PINNED, &em->flags) || |
| 4273 | em->start != start) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4274 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4275 | free_extent_map(em); |
| 4276 | break; |
| 4277 | } |
| 4278 | if (!test_range_bit(tree, em->start, |
| 4279 | extent_map_end(em) - 1, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4280 | EXTENT_LOCKED | EXTENT_WRITEBACK, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 4281 | 0, NULL)) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4282 | remove_extent_mapping(map, em); |
| 4283 | /* once for the rb tree */ |
| 4284 | free_extent_map(em); |
| 4285 | } |
| 4286 | start = extent_map_end(em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4287 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4288 | |
| 4289 | /* once for us */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4290 | free_extent_map(em); |
| 4291 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4292 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4293 | return try_release_extent_state(map, tree, page, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4294 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4295 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4296 | /* |
| 4297 | * helper function for fiemap, which doesn't want to see any holes. |
| 4298 | * This maps until we find something past 'last' |
| 4299 | */ |
| 4300 | static struct extent_map *get_extent_skip_holes(struct inode *inode, |
| 4301 | u64 offset, |
| 4302 | u64 last, |
| 4303 | get_extent_t *get_extent) |
| 4304 | { |
| 4305 | u64 sectorsize = BTRFS_I(inode)->root->sectorsize; |
| 4306 | struct extent_map *em; |
| 4307 | u64 len; |
| 4308 | |
| 4309 | if (offset >= last) |
| 4310 | return NULL; |
| 4311 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 4312 | while (1) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4313 | len = last - offset; |
| 4314 | if (len == 0) |
| 4315 | break; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4316 | len = ALIGN(len, sectorsize); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4317 | em = get_extent(inode, NULL, 0, offset, len, 0); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 4318 | if (IS_ERR_OR_NULL(em)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4319 | return em; |
| 4320 | |
| 4321 | /* if this isn't a hole return it */ |
| 4322 | if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) && |
| 4323 | em->block_start != EXTENT_MAP_HOLE) { |
| 4324 | return em; |
| 4325 | } |
| 4326 | |
| 4327 | /* this is a hole, advance to the next extent */ |
| 4328 | offset = extent_map_end(em); |
| 4329 | free_extent_map(em); |
| 4330 | if (offset >= last) |
| 4331 | break; |
| 4332 | } |
| 4333 | return NULL; |
| 4334 | } |
| 4335 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4336 | int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4337 | __u64 start, __u64 len, get_extent_t *get_extent) |
| 4338 | { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4339 | int ret = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4340 | u64 off = start; |
| 4341 | u64 max = start + len; |
| 4342 | u32 flags = 0; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4343 | u32 found_type; |
| 4344 | u64 last; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4345 | u64 last_for_get_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4346 | u64 disko = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4347 | u64 isize = i_size_read(inode); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4348 | struct btrfs_key found_key; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4349 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4350 | struct extent_state *cached_state = NULL; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4351 | struct btrfs_path *path; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4352 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4353 | int end = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4354 | u64 em_start = 0; |
| 4355 | u64 em_len = 0; |
| 4356 | u64 em_end = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4357 | |
| 4358 | if (len == 0) |
| 4359 | return -EINVAL; |
| 4360 | |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4361 | path = btrfs_alloc_path(); |
| 4362 | if (!path) |
| 4363 | return -ENOMEM; |
| 4364 | path->leave_spinning = 1; |
| 4365 | |
Qu Wenruo | 2c91943 | 2014-07-18 09:55:43 +0800 | [diff] [blame] | 4366 | start = round_down(start, BTRFS_I(inode)->root->sectorsize); |
| 4367 | len = round_up(max, BTRFS_I(inode)->root->sectorsize) - start; |
Josef Bacik | 4d479cf | 2011-11-17 11:34:31 -0500 | [diff] [blame] | 4368 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4369 | /* |
| 4370 | * lookup the last file extent. We're not using i_size here |
| 4371 | * because there might be preallocation past i_size |
| 4372 | */ |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4373 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), -1, |
| 4374 | 0); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4375 | if (ret < 0) { |
| 4376 | btrfs_free_path(path); |
| 4377 | return ret; |
| 4378 | } |
| 4379 | WARN_ON(!ret); |
| 4380 | path->slots[0]--; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4381 | 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] | 4382 | found_type = found_key.type; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4383 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4384 | /* No extents, but there might be delalloc bits */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4385 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4386 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4387 | /* have to trust i_size as the end */ |
| 4388 | last = (u64)-1; |
| 4389 | last_for_get_extent = isize; |
| 4390 | } else { |
| 4391 | /* |
| 4392 | * remember the start of the last extent. There are a |
| 4393 | * bunch of different factors that go into the length of the |
| 4394 | * extent, so its much less complex to remember where it started |
| 4395 | */ |
| 4396 | last = found_key.offset; |
| 4397 | last_for_get_extent = last + 1; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4398 | } |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4399 | btrfs_release_path(path); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4400 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4401 | /* |
| 4402 | * we might have some extents allocated but more delalloc past those |
| 4403 | * extents. so, we trust isize unless the start of the last extent is |
| 4404 | * beyond isize |
| 4405 | */ |
| 4406 | if (last < isize) { |
| 4407 | last = (u64)-1; |
| 4408 | last_for_get_extent = isize; |
| 4409 | } |
| 4410 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 4411 | lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4412 | &cached_state); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4413 | |
Josef Bacik | 4d479cf | 2011-11-17 11:34:31 -0500 | [diff] [blame] | 4414 | em = get_extent_skip_holes(inode, start, last_for_get_extent, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4415 | get_extent); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4416 | if (!em) |
| 4417 | goto out; |
| 4418 | if (IS_ERR(em)) { |
| 4419 | ret = PTR_ERR(em); |
| 4420 | goto out; |
| 4421 | } |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4422 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4423 | while (!end) { |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4424 | u64 offset_in_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4425 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4426 | /* break if the extent we found is outside the range */ |
| 4427 | if (em->start >= max || extent_map_end(em) < off) |
| 4428 | break; |
| 4429 | |
| 4430 | /* |
| 4431 | * get_extent may return an extent that starts before our |
| 4432 | * requested range. We have to make sure the ranges |
| 4433 | * we return to fiemap always move forward and don't |
| 4434 | * overlap, so adjust the offsets here |
| 4435 | */ |
| 4436 | em_start = max(em->start, off); |
| 4437 | |
| 4438 | /* |
| 4439 | * record the offset from the start of the extent |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4440 | * for adjusting the disk offset below. Only do this if the |
| 4441 | * extent isn't compressed since our in ram offset may be past |
| 4442 | * what we have actually allocated on disk. |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4443 | */ |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4444 | if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 4445 | offset_in_extent = em_start - em->start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4446 | em_end = extent_map_end(em); |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4447 | em_len = em_end - em_start; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4448 | disko = 0; |
| 4449 | flags = 0; |
| 4450 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4451 | /* |
| 4452 | * bump off for our next call to get_extent |
| 4453 | */ |
| 4454 | off = extent_map_end(em); |
| 4455 | if (off >= max) |
| 4456 | end = 1; |
| 4457 | |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4458 | if (em->block_start == EXTENT_MAP_LAST_BYTE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4459 | end = 1; |
| 4460 | flags |= FIEMAP_EXTENT_LAST; |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4461 | } else if (em->block_start == EXTENT_MAP_INLINE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4462 | flags |= (FIEMAP_EXTENT_DATA_INLINE | |
| 4463 | FIEMAP_EXTENT_NOT_ALIGNED); |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4464 | } else if (em->block_start == EXTENT_MAP_DELALLOC) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4465 | flags |= (FIEMAP_EXTENT_DELALLOC | |
| 4466 | FIEMAP_EXTENT_UNKNOWN); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4467 | } else if (fieinfo->fi_extents_max) { |
| 4468 | u64 bytenr = em->block_start - |
| 4469 | (em->start - em->orig_start); |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4470 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4471 | disko = em->block_start + offset_in_extent; |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4472 | |
| 4473 | /* |
| 4474 | * As btrfs supports shared space, this information |
| 4475 | * can be exported to userspace tools via |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4476 | * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0 |
| 4477 | * then we're just getting a count and we can skip the |
| 4478 | * lookup stuff. |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4479 | */ |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4480 | ret = btrfs_check_shared(NULL, root->fs_info, |
| 4481 | root->objectid, |
| 4482 | btrfs_ino(inode), bytenr); |
| 4483 | if (ret < 0) |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4484 | goto out_free; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4485 | if (ret) |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4486 | flags |= FIEMAP_EXTENT_SHARED; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4487 | ret = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4488 | } |
| 4489 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 4490 | flags |= FIEMAP_EXTENT_ENCODED; |
Josef Bacik | 0d2b237 | 2015-05-19 10:44:04 -0400 | [diff] [blame] | 4491 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 4492 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4493 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4494 | free_extent_map(em); |
| 4495 | em = NULL; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4496 | if ((em_start >= last) || em_len == (u64)-1 || |
| 4497 | (last == (u64)-1 && isize <= em_end)) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4498 | flags |= FIEMAP_EXTENT_LAST; |
| 4499 | end = 1; |
| 4500 | } |
| 4501 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4502 | /* now scan forward to see if this is really the last extent. */ |
| 4503 | em = get_extent_skip_holes(inode, off, last_for_get_extent, |
| 4504 | get_extent); |
| 4505 | if (IS_ERR(em)) { |
| 4506 | ret = PTR_ERR(em); |
| 4507 | goto out; |
| 4508 | } |
| 4509 | if (!em) { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4510 | flags |= FIEMAP_EXTENT_LAST; |
| 4511 | end = 1; |
| 4512 | } |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4513 | ret = fiemap_fill_next_extent(fieinfo, em_start, disko, |
| 4514 | em_len, flags); |
Chengyu Song | 26e726a | 2015-03-24 18:12:56 -0400 | [diff] [blame] | 4515 | if (ret) { |
| 4516 | if (ret == 1) |
| 4517 | ret = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4518 | goto out_free; |
Chengyu Song | 26e726a | 2015-03-24 18:12:56 -0400 | [diff] [blame] | 4519 | } |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4520 | } |
| 4521 | out_free: |
| 4522 | free_extent_map(em); |
| 4523 | out: |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4524 | btrfs_free_path(path); |
Liu Bo | a52f4cd | 2013-05-01 16:23:41 +0000 | [diff] [blame] | 4525 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4526 | &cached_state, GFP_NOFS); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4527 | return ret; |
| 4528 | } |
| 4529 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4530 | static void __free_extent_buffer(struct extent_buffer *eb) |
| 4531 | { |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 4532 | btrfs_leak_debug_del(&eb->leak_list); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4533 | kmem_cache_free(extent_buffer_cache, eb); |
| 4534 | } |
| 4535 | |
Josef Bacik | a26e8c9 | 2014-03-28 17:07:27 -0400 | [diff] [blame] | 4536 | int extent_buffer_under_io(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4537 | { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4538 | return (atomic_read(&eb->io_pages) || |
| 4539 | test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) || |
| 4540 | test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4541 | } |
| 4542 | |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4543 | /* |
| 4544 | * Helper for releasing extent buffer page. |
| 4545 | */ |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4546 | static void btrfs_release_extent_buffer_page(struct extent_buffer *eb) |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4547 | { |
| 4548 | unsigned long index; |
| 4549 | struct page *page; |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4550 | int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags); |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4551 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4552 | BUG_ON(extent_buffer_under_io(eb)); |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4553 | |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4554 | index = num_extent_pages(eb->start, eb->len); |
| 4555 | if (index == 0) |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4556 | return; |
| 4557 | |
| 4558 | do { |
| 4559 | index--; |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 4560 | page = eb->pages[index]; |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4561 | if (!page) |
| 4562 | continue; |
| 4563 | if (mapped) |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4564 | spin_lock(&page->mapping->private_lock); |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4565 | /* |
| 4566 | * We do this since we'll remove the pages after we've |
| 4567 | * removed the eb from the radix tree, so we could race |
| 4568 | * and have this page now attached to the new eb. So |
| 4569 | * only clear page_private if it's still connected to |
| 4570 | * this eb. |
| 4571 | */ |
| 4572 | if (PagePrivate(page) && |
| 4573 | page->private == (unsigned long)eb) { |
| 4574 | BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
| 4575 | BUG_ON(PageDirty(page)); |
| 4576 | BUG_ON(PageWriteback(page)); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4577 | /* |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4578 | * We need to make sure we haven't be attached |
| 4579 | * to a new eb. |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4580 | */ |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4581 | ClearPagePrivate(page); |
| 4582 | set_page_private(page, 0); |
| 4583 | /* One for the page private */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4584 | put_page(page); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4585 | } |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4586 | |
| 4587 | if (mapped) |
| 4588 | spin_unlock(&page->mapping->private_lock); |
| 4589 | |
| 4590 | /* One for when we alloced the page */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4591 | put_page(page); |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4592 | } while (index != 0); |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | /* |
| 4596 | * Helper for releasing the extent buffer. |
| 4597 | */ |
| 4598 | static inline void btrfs_release_extent_buffer(struct extent_buffer *eb) |
| 4599 | { |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4600 | btrfs_release_extent_buffer_page(eb); |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4601 | __free_extent_buffer(eb); |
| 4602 | } |
| 4603 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4604 | static struct extent_buffer * |
| 4605 | __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, |
David Sterba | 23d79d8 | 2014-06-15 02:55:29 +0200 | [diff] [blame] | 4606 | unsigned long len) |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4607 | { |
| 4608 | struct extent_buffer *eb = NULL; |
| 4609 | |
Michal Hocko | d1b5c56 | 2015-08-19 14:17:40 +0200 | [diff] [blame] | 4610 | eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4611 | eb->start = start; |
| 4612 | eb->len = len; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4613 | eb->fs_info = fs_info; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4614 | eb->bflags = 0; |
| 4615 | rwlock_init(&eb->lock); |
| 4616 | atomic_set(&eb->write_locks, 0); |
| 4617 | atomic_set(&eb->read_locks, 0); |
| 4618 | atomic_set(&eb->blocking_readers, 0); |
| 4619 | atomic_set(&eb->blocking_writers, 0); |
| 4620 | atomic_set(&eb->spinning_readers, 0); |
| 4621 | atomic_set(&eb->spinning_writers, 0); |
| 4622 | eb->lock_nested = 0; |
| 4623 | init_waitqueue_head(&eb->write_lock_wq); |
| 4624 | init_waitqueue_head(&eb->read_lock_wq); |
| 4625 | |
| 4626 | btrfs_leak_debug_add(&eb->leak_list, &buffers); |
| 4627 | |
| 4628 | spin_lock_init(&eb->refs_lock); |
| 4629 | atomic_set(&eb->refs, 1); |
| 4630 | atomic_set(&eb->io_pages, 0); |
| 4631 | |
| 4632 | /* |
| 4633 | * Sanity checks, currently the maximum is 64k covered by 16x 4k pages |
| 4634 | */ |
| 4635 | BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE |
| 4636 | > MAX_INLINE_EXTENT_BUFFER_SIZE); |
| 4637 | BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE); |
| 4638 | |
| 4639 | return eb; |
| 4640 | } |
| 4641 | |
| 4642 | struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src) |
| 4643 | { |
| 4644 | unsigned long i; |
| 4645 | struct page *p; |
| 4646 | struct extent_buffer *new; |
| 4647 | unsigned long num_pages = num_extent_pages(src->start, src->len); |
| 4648 | |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4649 | new = __alloc_extent_buffer(src->fs_info, src->start, src->len); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4650 | if (new == NULL) |
| 4651 | return NULL; |
| 4652 | |
| 4653 | for (i = 0; i < num_pages; i++) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 4654 | p = alloc_page(GFP_NOFS); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4655 | if (!p) { |
| 4656 | btrfs_release_extent_buffer(new); |
| 4657 | return NULL; |
| 4658 | } |
| 4659 | attach_extent_buffer_page(new, p); |
| 4660 | WARN_ON(PageDirty(p)); |
| 4661 | SetPageUptodate(p); |
| 4662 | new->pages[i] = p; |
| 4663 | } |
| 4664 | |
| 4665 | copy_extent_buffer(new, src, 0, 0, src->len); |
| 4666 | set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags); |
| 4667 | set_bit(EXTENT_BUFFER_DUMMY, &new->bflags); |
| 4668 | |
| 4669 | return new; |
| 4670 | } |
| 4671 | |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4672 | struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, |
| 4673 | u64 start, unsigned long len) |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4674 | { |
| 4675 | struct extent_buffer *eb; |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4676 | unsigned long num_pages; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4677 | unsigned long i; |
| 4678 | |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4679 | num_pages = num_extent_pages(start, len); |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4680 | |
| 4681 | eb = __alloc_extent_buffer(fs_info, start, len); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4682 | if (!eb) |
| 4683 | return NULL; |
| 4684 | |
| 4685 | for (i = 0; i < num_pages; i++) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 4686 | eb->pages[i] = alloc_page(GFP_NOFS); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4687 | if (!eb->pages[i]) |
| 4688 | goto err; |
| 4689 | } |
| 4690 | set_extent_buffer_uptodate(eb); |
| 4691 | btrfs_set_header_nritems(eb, 0); |
| 4692 | set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags); |
| 4693 | |
| 4694 | return eb; |
| 4695 | err: |
| 4696 | for (; i > 0; i--) |
| 4697 | __free_page(eb->pages[i - 1]); |
| 4698 | __free_extent_buffer(eb); |
| 4699 | return NULL; |
| 4700 | } |
| 4701 | |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4702 | struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, |
| 4703 | u64 start) |
| 4704 | { |
| 4705 | unsigned long len; |
| 4706 | |
| 4707 | if (!fs_info) { |
| 4708 | /* |
| 4709 | * Called only from tests that don't always have a fs_info |
| 4710 | * available, but we know that nodesize is 4096 |
| 4711 | */ |
| 4712 | len = 4096; |
| 4713 | } else { |
| 4714 | len = fs_info->tree_root->nodesize; |
| 4715 | } |
| 4716 | |
| 4717 | return __alloc_dummy_extent_buffer(fs_info, start, len); |
| 4718 | } |
| 4719 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4720 | static void check_buffer_tree_ref(struct extent_buffer *eb) |
| 4721 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4722 | int refs; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4723 | /* the ref bit is tricky. We have to make sure it is set |
| 4724 | * if we have the buffer dirty. Otherwise the |
| 4725 | * code to free a buffer can end up dropping a dirty |
| 4726 | * page |
| 4727 | * |
| 4728 | * Once the ref bit is set, it won't go away while the |
| 4729 | * buffer is dirty or in writeback, and it also won't |
| 4730 | * go away while we have the reference count on the |
| 4731 | * eb bumped. |
| 4732 | * |
| 4733 | * We can't just set the ref bit without bumping the |
| 4734 | * ref on the eb because free_extent_buffer might |
| 4735 | * see the ref bit and try to clear it. If this happens |
| 4736 | * free_extent_buffer might end up dropping our original |
| 4737 | * ref by mistake and freeing the page before we are able |
| 4738 | * to add one more ref. |
| 4739 | * |
| 4740 | * So bump the ref count first, then set the bit. If someone |
| 4741 | * beat us to it, drop the ref we added. |
| 4742 | */ |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4743 | refs = atomic_read(&eb->refs); |
| 4744 | if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 4745 | return; |
| 4746 | |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4747 | spin_lock(&eb->refs_lock); |
| 4748 | if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4749 | atomic_inc(&eb->refs); |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4750 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4751 | } |
| 4752 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4753 | static void mark_extent_buffer_accessed(struct extent_buffer *eb, |
| 4754 | struct page *accessed) |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4755 | { |
| 4756 | unsigned long num_pages, i; |
| 4757 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4758 | check_buffer_tree_ref(eb); |
| 4759 | |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4760 | num_pages = num_extent_pages(eb->start, eb->len); |
| 4761 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 4762 | struct page *p = eb->pages[i]; |
| 4763 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4764 | if (p != accessed) |
| 4765 | mark_page_accessed(p); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4766 | } |
| 4767 | } |
| 4768 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4769 | struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info, |
| 4770 | u64 start) |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4771 | { |
| 4772 | struct extent_buffer *eb; |
| 4773 | |
| 4774 | rcu_read_lock(); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4775 | eb = radix_tree_lookup(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4776 | start >> PAGE_SHIFT); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4777 | if (eb && atomic_inc_not_zero(&eb->refs)) { |
| 4778 | rcu_read_unlock(); |
Filipe Manana | 062c19e | 2015-04-23 11:28:48 +0100 | [diff] [blame] | 4779 | /* |
| 4780 | * Lock our eb's refs_lock to avoid races with |
| 4781 | * free_extent_buffer. When we get our eb it might be flagged |
| 4782 | * with EXTENT_BUFFER_STALE and another task running |
| 4783 | * free_extent_buffer might have seen that flag set, |
| 4784 | * eb->refs == 2, that the buffer isn't under IO (dirty and |
| 4785 | * writeback flags not set) and it's still in the tree (flag |
| 4786 | * EXTENT_BUFFER_TREE_REF set), therefore being in the process |
| 4787 | * of decrementing the extent buffer's reference count twice. |
| 4788 | * So here we could race and increment the eb's reference count, |
| 4789 | * clear its stale flag, mark it as dirty and drop our reference |
| 4790 | * before the other task finishes executing free_extent_buffer, |
| 4791 | * which would later result in an attempt to free an extent |
| 4792 | * buffer that is dirty. |
| 4793 | */ |
| 4794 | if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) { |
| 4795 | spin_lock(&eb->refs_lock); |
| 4796 | spin_unlock(&eb->refs_lock); |
| 4797 | } |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4798 | mark_extent_buffer_accessed(eb, NULL); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4799 | return eb; |
| 4800 | } |
| 4801 | rcu_read_unlock(); |
| 4802 | |
| 4803 | return NULL; |
| 4804 | } |
| 4805 | |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4806 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 4807 | struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info, |
David Sterba | ce3e698 | 2014-06-15 03:00:04 +0200 | [diff] [blame] | 4808 | u64 start) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4809 | { |
| 4810 | struct extent_buffer *eb, *exists = NULL; |
| 4811 | int ret; |
| 4812 | |
| 4813 | eb = find_extent_buffer(fs_info, start); |
| 4814 | if (eb) |
| 4815 | return eb; |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4816 | eb = alloc_dummy_extent_buffer(fs_info, start); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4817 | if (!eb) |
| 4818 | return NULL; |
| 4819 | eb->fs_info = fs_info; |
| 4820 | again: |
| 4821 | ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); |
| 4822 | if (ret) |
| 4823 | goto free_eb; |
| 4824 | spin_lock(&fs_info->buffer_lock); |
| 4825 | ret = radix_tree_insert(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4826 | start >> PAGE_SHIFT, eb); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4827 | spin_unlock(&fs_info->buffer_lock); |
| 4828 | radix_tree_preload_end(); |
| 4829 | if (ret == -EEXIST) { |
| 4830 | exists = find_extent_buffer(fs_info, start); |
| 4831 | if (exists) |
| 4832 | goto free_eb; |
| 4833 | else |
| 4834 | goto again; |
| 4835 | } |
| 4836 | check_buffer_tree_ref(eb); |
| 4837 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
| 4838 | |
| 4839 | /* |
| 4840 | * We will free dummy extent buffer's if they come into |
| 4841 | * free_extent_buffer with a ref count of 2, but if we are using this we |
| 4842 | * want the buffers to stay in memory until we're done with them, so |
| 4843 | * bump the ref count again. |
| 4844 | */ |
| 4845 | atomic_inc(&eb->refs); |
| 4846 | return eb; |
| 4847 | free_eb: |
| 4848 | btrfs_release_extent_buffer(eb); |
| 4849 | return exists; |
| 4850 | } |
| 4851 | #endif |
| 4852 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4853 | struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, |
David Sterba | ce3e698 | 2014-06-15 03:00:04 +0200 | [diff] [blame] | 4854 | u64 start) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4855 | { |
David Sterba | ce3e698 | 2014-06-15 03:00:04 +0200 | [diff] [blame] | 4856 | unsigned long len = fs_info->tree_root->nodesize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4857 | unsigned long num_pages = num_extent_pages(start, len); |
| 4858 | unsigned long i; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4859 | unsigned long index = start >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4860 | struct extent_buffer *eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4861 | struct extent_buffer *exists = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4862 | struct page *p; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4863 | struct address_space *mapping = fs_info->btree_inode->i_mapping; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4864 | int uptodate = 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4865 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4866 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4867 | eb = find_extent_buffer(fs_info, start); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4868 | if (eb) |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4869 | return eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4870 | |
David Sterba | 23d79d8 | 2014-06-15 02:55:29 +0200 | [diff] [blame] | 4871 | eb = __alloc_extent_buffer(fs_info, start, len); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 4872 | if (!eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4873 | return NULL; |
| 4874 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4875 | for (i = 0; i < num_pages; i++, index++) { |
Michal Hocko | d1b5c56 | 2015-08-19 14:17:40 +0200 | [diff] [blame] | 4876 | p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL); |
Josef Bacik | 4804b38 | 2012-10-05 16:43:45 -0400 | [diff] [blame] | 4877 | if (!p) |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4878 | goto free_eb; |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4879 | |
| 4880 | spin_lock(&mapping->private_lock); |
| 4881 | if (PagePrivate(p)) { |
| 4882 | /* |
| 4883 | * We could have already allocated an eb for this page |
| 4884 | * and attached one so lets see if we can get a ref on |
| 4885 | * the existing eb, and if we can we know it's good and |
| 4886 | * we can just return that one, else we know we can just |
| 4887 | * overwrite page->private. |
| 4888 | */ |
| 4889 | exists = (struct extent_buffer *)p->private; |
| 4890 | if (atomic_inc_not_zero(&exists->refs)) { |
| 4891 | spin_unlock(&mapping->private_lock); |
| 4892 | unlock_page(p); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4893 | put_page(p); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4894 | mark_extent_buffer_accessed(exists, p); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4895 | goto free_eb; |
| 4896 | } |
Omar Sandoval | 5ca64f4 | 2015-02-24 02:47:05 -0800 | [diff] [blame] | 4897 | exists = NULL; |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4898 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4899 | /* |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4900 | * Do this so attach doesn't complain and we need to |
| 4901 | * drop the ref the old guy had. |
| 4902 | */ |
| 4903 | ClearPagePrivate(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4904 | WARN_ON(PageDirty(p)); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4905 | put_page(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4906 | } |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4907 | attach_extent_buffer_page(eb, p); |
| 4908 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4909 | WARN_ON(PageDirty(p)); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4910 | eb->pages[i] = p; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4911 | if (!PageUptodate(p)) |
| 4912 | uptodate = 0; |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 4913 | |
| 4914 | /* |
| 4915 | * see below about how we avoid a nasty race with release page |
| 4916 | * and why we unlock later |
| 4917 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4918 | } |
| 4919 | if (uptodate) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4920 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 4921 | again: |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4922 | ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); |
| 4923 | if (ret) |
| 4924 | goto free_eb; |
| 4925 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4926 | spin_lock(&fs_info->buffer_lock); |
| 4927 | ret = radix_tree_insert(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4928 | start >> PAGE_SHIFT, eb); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4929 | spin_unlock(&fs_info->buffer_lock); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4930 | radix_tree_preload_end(); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4931 | if (ret == -EEXIST) { |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4932 | exists = find_extent_buffer(fs_info, start); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4933 | if (exists) |
| 4934 | goto free_eb; |
| 4935 | else |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 4936 | goto again; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4937 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4938 | /* add one reference for the tree */ |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4939 | check_buffer_tree_ref(eb); |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 4940 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 4941 | |
| 4942 | /* |
| 4943 | * there is a race where release page may have |
| 4944 | * tried to find this extent buffer in the radix |
| 4945 | * but failed. It will tell the VM it is safe to |
| 4946 | * reclaim the, and it will clear the page private bit. |
| 4947 | * We must make sure to set the page private bit properly |
| 4948 | * after the extent buffer is in the radix tree so |
| 4949 | * it doesn't get lost |
| 4950 | */ |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4951 | SetPageChecked(eb->pages[0]); |
| 4952 | for (i = 1; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 4953 | p = eb->pages[i]; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4954 | ClearPageChecked(p); |
| 4955 | unlock_page(p); |
| 4956 | } |
| 4957 | unlock_page(eb->pages[0]); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4958 | return eb; |
| 4959 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4960 | free_eb: |
Omar Sandoval | 5ca64f4 | 2015-02-24 02:47:05 -0800 | [diff] [blame] | 4961 | WARN_ON(!atomic_dec_and_test(&eb->refs)); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4962 | for (i = 0; i < num_pages; i++) { |
| 4963 | if (eb->pages[i]) |
| 4964 | unlock_page(eb->pages[i]); |
| 4965 | } |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 4966 | |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4967 | btrfs_release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4968 | return exists; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4969 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4970 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4971 | static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head) |
| 4972 | { |
| 4973 | struct extent_buffer *eb = |
| 4974 | container_of(head, struct extent_buffer, rcu_head); |
| 4975 | |
| 4976 | __free_extent_buffer(eb); |
| 4977 | } |
| 4978 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4979 | /* Expects to have eb->eb_lock already held */ |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 4980 | static int release_extent_buffer(struct extent_buffer *eb) |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4981 | { |
| 4982 | WARN_ON(atomic_read(&eb->refs) == 0); |
| 4983 | if (atomic_dec_and_test(&eb->refs)) { |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 4984 | if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) { |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4985 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4986 | |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4987 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4988 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4989 | spin_lock(&fs_info->buffer_lock); |
| 4990 | radix_tree_delete(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4991 | eb->start >> PAGE_SHIFT); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4992 | spin_unlock(&fs_info->buffer_lock); |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 4993 | } else { |
| 4994 | spin_unlock(&eb->refs_lock); |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4995 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4996 | |
| 4997 | /* Should be safe to release our pages at this point */ |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4998 | btrfs_release_extent_buffer_page(eb); |
Josef Bacik | bcb7e44 | 2015-03-16 17:38:02 -0400 | [diff] [blame] | 4999 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 5000 | if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) { |
| 5001 | __free_extent_buffer(eb); |
| 5002 | return 1; |
| 5003 | } |
| 5004 | #endif |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5005 | call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 5006 | return 1; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5007 | } |
| 5008 | spin_unlock(&eb->refs_lock); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 5009 | |
| 5010 | return 0; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5011 | } |
| 5012 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5013 | void free_extent_buffer(struct extent_buffer *eb) |
| 5014 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 5015 | int refs; |
| 5016 | int old; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5017 | if (!eb) |
| 5018 | return; |
| 5019 | |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 5020 | while (1) { |
| 5021 | refs = atomic_read(&eb->refs); |
| 5022 | if (refs <= 3) |
| 5023 | break; |
| 5024 | old = atomic_cmpxchg(&eb->refs, refs, refs - 1); |
| 5025 | if (old == refs) |
| 5026 | return; |
| 5027 | } |
| 5028 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5029 | spin_lock(&eb->refs_lock); |
| 5030 | if (atomic_read(&eb->refs) == 2 && |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 5031 | test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) |
| 5032 | atomic_dec(&eb->refs); |
| 5033 | |
| 5034 | if (atomic_read(&eb->refs) == 2 && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5035 | test_bit(EXTENT_BUFFER_STALE, &eb->bflags) && |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5036 | !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5037 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 5038 | atomic_dec(&eb->refs); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5039 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5040 | /* |
| 5041 | * I know this is terrible, but it's temporary until we stop tracking |
| 5042 | * the uptodate bits and such for the extent buffers. |
| 5043 | */ |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5044 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5045 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5046 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5047 | void free_extent_buffer_stale(struct extent_buffer *eb) |
| 5048 | { |
| 5049 | if (!eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5050 | return; |
| 5051 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5052 | spin_lock(&eb->refs_lock); |
| 5053 | set_bit(EXTENT_BUFFER_STALE, &eb->bflags); |
| 5054 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5055 | if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5056 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 5057 | atomic_dec(&eb->refs); |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5058 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5059 | } |
| 5060 | |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 5061 | void clear_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5062 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5063 | unsigned long i; |
| 5064 | unsigned long num_pages; |
| 5065 | struct page *page; |
| 5066 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5067 | num_pages = num_extent_pages(eb->start, eb->len); |
| 5068 | |
| 5069 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5070 | page = eb->pages[i]; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5071 | if (!PageDirty(page)) |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 5072 | continue; |
| 5073 | |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 5074 | lock_page(page); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5075 | WARN_ON(!PagePrivate(page)); |
| 5076 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5077 | clear_page_dirty_for_io(page); |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 5078 | spin_lock_irq(&page->mapping->tree_lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5079 | if (!PageDirty(page)) { |
| 5080 | radix_tree_tag_clear(&page->mapping->page_tree, |
| 5081 | page_index(page), |
| 5082 | PAGECACHE_TAG_DIRTY); |
| 5083 | } |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 5084 | spin_unlock_irq(&page->mapping->tree_lock); |
Chris Mason | bf0da8c | 2011-11-04 12:29:37 -0400 | [diff] [blame] | 5085 | ClearPageError(page); |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 5086 | unlock_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5087 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5088 | WARN_ON(atomic_read(&eb->refs) == 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5089 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5090 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5091 | int set_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5092 | { |
| 5093 | unsigned long i; |
| 5094 | unsigned long num_pages; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5095 | int was_dirty = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5096 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5097 | check_buffer_tree_ref(eb); |
| 5098 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5099 | was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5100 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5101 | num_pages = num_extent_pages(eb->start, eb->len); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5102 | WARN_ON(atomic_read(&eb->refs) == 0); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5103 | WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)); |
| 5104 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5105 | for (i = 0; i < num_pages; i++) |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5106 | set_page_dirty(eb->pages[i]); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5107 | return was_dirty; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5108 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5109 | |
David Sterba | 69ba392 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 5110 | void clear_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5111 | { |
| 5112 | unsigned long i; |
| 5113 | struct page *page; |
| 5114 | unsigned long num_pages; |
| 5115 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5116 | clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5117 | num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5118 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5119 | page = eb->pages[i]; |
Chris Mason | 33958dc | 2008-07-30 10:29:12 -0400 | [diff] [blame] | 5120 | if (page) |
| 5121 | ClearPageUptodate(page); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5122 | } |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5123 | } |
| 5124 | |
David Sterba | 09c25a8 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 5125 | void set_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5126 | { |
| 5127 | unsigned long i; |
| 5128 | struct page *page; |
| 5129 | unsigned long num_pages; |
| 5130 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5131 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5132 | num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5133 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5134 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5135 | SetPageUptodate(page); |
| 5136 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5137 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5138 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5139 | int extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5140 | { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5141 | return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5142 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5143 | |
| 5144 | int read_extent_buffer_pages(struct extent_io_tree *tree, |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 5145 | struct extent_buffer *eb, u64 start, int wait, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5146 | get_extent_t *get_extent, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5147 | { |
| 5148 | unsigned long i; |
| 5149 | unsigned long start_i; |
| 5150 | struct page *page; |
| 5151 | int err; |
| 5152 | int ret = 0; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5153 | int locked_pages = 0; |
| 5154 | int all_uptodate = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5155 | unsigned long num_pages; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5156 | unsigned long num_reads = 0; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5157 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5158 | unsigned long bio_flags = 0; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5159 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5160 | if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5161 | return 0; |
| 5162 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5163 | if (start) { |
| 5164 | WARN_ON(start < eb->start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5165 | start_i = (start >> PAGE_SHIFT) - |
| 5166 | (eb->start >> PAGE_SHIFT); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5167 | } else { |
| 5168 | start_i = 0; |
| 5169 | } |
| 5170 | |
| 5171 | num_pages = num_extent_pages(eb->start, eb->len); |
| 5172 | for (i = start_i; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5173 | page = eb->pages[i]; |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 5174 | if (wait == WAIT_NONE) { |
David Woodhouse | 2db0496 | 2008-08-07 11:19:43 -0400 | [diff] [blame] | 5175 | if (!trylock_page(page)) |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5176 | goto unlock_exit; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5177 | } else { |
| 5178 | lock_page(page); |
| 5179 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5180 | locked_pages++; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5181 | if (!PageUptodate(page)) { |
| 5182 | num_reads++; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5183 | all_uptodate = 0; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5184 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5185 | } |
| 5186 | if (all_uptodate) { |
| 5187 | if (start_i == 0) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5188 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5189 | goto unlock_exit; |
| 5190 | } |
| 5191 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 5192 | clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 5193 | eb->read_mirror = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5194 | atomic_set(&eb->io_pages, num_reads); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5195 | for (i = start_i; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5196 | page = eb->pages[i]; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5197 | if (!PageUptodate(page)) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5198 | ClearPageError(page); |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5199 | err = __extent_read_full_page(tree, page, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5200 | get_extent, &bio, |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 5201 | mirror_num, &bio_flags, |
| 5202 | READ | REQ_META); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5203 | if (err) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5204 | ret = err; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5205 | } else { |
| 5206 | unlock_page(page); |
| 5207 | } |
| 5208 | } |
| 5209 | |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 5210 | if (bio) { |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 5211 | err = submit_one_bio(READ | REQ_META, bio, mirror_num, |
| 5212 | bio_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5213 | if (err) |
| 5214 | return err; |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 5215 | } |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5216 | |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 5217 | if (ret || wait != WAIT_COMPLETE) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5218 | return ret; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5219 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5220 | for (i = start_i; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5221 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5222 | wait_on_page_locked(page); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5223 | if (!PageUptodate(page)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5224 | ret = -EIO; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5225 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5226 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5227 | return ret; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5228 | |
| 5229 | unlock_exit: |
| 5230 | i = start_i; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5231 | while (locked_pages > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5232 | page = eb->pages[i]; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5233 | i++; |
| 5234 | unlock_page(page); |
| 5235 | locked_pages--; |
| 5236 | } |
| 5237 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5238 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5239 | |
| 5240 | void read_extent_buffer(struct extent_buffer *eb, void *dstv, |
| 5241 | unsigned long start, |
| 5242 | unsigned long len) |
| 5243 | { |
| 5244 | size_t cur; |
| 5245 | size_t offset; |
| 5246 | struct page *page; |
| 5247 | char *kaddr; |
| 5248 | char *dst = (char *)dstv; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5249 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5250 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5251 | |
| 5252 | WARN_ON(start > eb->len); |
| 5253 | WARN_ON(start + len > eb->start + eb->len); |
| 5254 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5255 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5256 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5257 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5258 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5259 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5260 | cur = min(len, (PAGE_SIZE - offset)); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5261 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5262 | memcpy(dst, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5263 | |
| 5264 | dst += cur; |
| 5265 | len -= cur; |
| 5266 | offset = 0; |
| 5267 | i++; |
| 5268 | } |
| 5269 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5270 | |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5271 | int read_extent_buffer_to_user(struct extent_buffer *eb, void __user *dstv, |
| 5272 | unsigned long start, |
| 5273 | unsigned long len) |
| 5274 | { |
| 5275 | size_t cur; |
| 5276 | size_t offset; |
| 5277 | struct page *page; |
| 5278 | char *kaddr; |
| 5279 | char __user *dst = (char __user *)dstv; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5280 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5281 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5282 | int ret = 0; |
| 5283 | |
| 5284 | WARN_ON(start > eb->len); |
| 5285 | WARN_ON(start + len > eb->start + eb->len); |
| 5286 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5287 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5288 | |
| 5289 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5290 | page = eb->pages[i]; |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5291 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5292 | cur = min(len, (PAGE_SIZE - offset)); |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5293 | kaddr = page_address(page); |
| 5294 | if (copy_to_user(dst, kaddr + offset, cur)) { |
| 5295 | ret = -EFAULT; |
| 5296 | break; |
| 5297 | } |
| 5298 | |
| 5299 | dst += cur; |
| 5300 | len -= cur; |
| 5301 | offset = 0; |
| 5302 | i++; |
| 5303 | } |
| 5304 | |
| 5305 | return ret; |
| 5306 | } |
| 5307 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5308 | int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start, |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5309 | unsigned long min_len, char **map, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5310 | unsigned long *map_start, |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5311 | unsigned long *map_len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5312 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5313 | size_t offset = start & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5314 | char *kaddr; |
| 5315 | struct page *p; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5316 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5317 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5318 | unsigned long end_i = (start_offset + start + min_len - 1) >> |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5319 | PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5320 | |
| 5321 | if (i != end_i) |
| 5322 | return -EINVAL; |
| 5323 | |
| 5324 | if (i == 0) { |
| 5325 | offset = start_offset; |
| 5326 | *map_start = 0; |
| 5327 | } else { |
| 5328 | offset = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5329 | *map_start = ((u64)i << PAGE_SHIFT) - start_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5330 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5331 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5332 | if (start + min_len > eb->len) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 5333 | WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, " |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 5334 | "wanted %lu %lu\n", |
| 5335 | eb->start, eb->len, start, min_len); |
Josef Bacik | 85026533 | 2011-03-15 14:52:12 -0400 | [diff] [blame] | 5336 | return -EINVAL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5337 | } |
| 5338 | |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5339 | p = eb->pages[i]; |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5340 | kaddr = page_address(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5341 | *map = kaddr + offset; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5342 | *map_len = PAGE_SIZE - offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5343 | return 0; |
| 5344 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5345 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5346 | int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv, |
| 5347 | unsigned long start, |
| 5348 | unsigned long len) |
| 5349 | { |
| 5350 | size_t cur; |
| 5351 | size_t offset; |
| 5352 | struct page *page; |
| 5353 | char *kaddr; |
| 5354 | char *ptr = (char *)ptrv; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5355 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5356 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5357 | int ret = 0; |
| 5358 | |
| 5359 | WARN_ON(start > eb->len); |
| 5360 | WARN_ON(start + len > eb->start + eb->len); |
| 5361 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5362 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5363 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5364 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5365 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5366 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5367 | cur = min(len, (PAGE_SIZE - offset)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5368 | |
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 | ret = memcmp(ptr, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5371 | if (ret) |
| 5372 | break; |
| 5373 | |
| 5374 | ptr += cur; |
| 5375 | len -= cur; |
| 5376 | offset = 0; |
| 5377 | i++; |
| 5378 | } |
| 5379 | return ret; |
| 5380 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5381 | |
| 5382 | void write_extent_buffer(struct extent_buffer *eb, const void *srcv, |
| 5383 | unsigned long start, unsigned long len) |
| 5384 | { |
| 5385 | size_t cur; |
| 5386 | size_t offset; |
| 5387 | struct page *page; |
| 5388 | char *kaddr; |
| 5389 | char *src = (char *)srcv; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5390 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5391 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5392 | |
| 5393 | WARN_ON(start > eb->len); |
| 5394 | WARN_ON(start + len > eb->start + eb->len); |
| 5395 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5396 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5397 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5398 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5399 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5400 | WARN_ON(!PageUptodate(page)); |
| 5401 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5402 | cur = min(len, PAGE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5403 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5404 | memcpy(kaddr + offset, src, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5405 | |
| 5406 | src += cur; |
| 5407 | len -= cur; |
| 5408 | offset = 0; |
| 5409 | i++; |
| 5410 | } |
| 5411 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5412 | |
| 5413 | void memset_extent_buffer(struct extent_buffer *eb, char c, |
| 5414 | unsigned long start, unsigned long len) |
| 5415 | { |
| 5416 | size_t cur; |
| 5417 | size_t offset; |
| 5418 | struct page *page; |
| 5419 | char *kaddr; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5420 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
| 5421 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5422 | |
| 5423 | WARN_ON(start > eb->len); |
| 5424 | WARN_ON(start + len > eb->start + eb->len); |
| 5425 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5426 | offset = (start_offset + start) & (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5427 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5428 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5429 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5430 | WARN_ON(!PageUptodate(page)); |
| 5431 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5432 | cur = min(len, PAGE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5433 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5434 | memset(kaddr + offset, c, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5435 | |
| 5436 | len -= cur; |
| 5437 | offset = 0; |
| 5438 | i++; |
| 5439 | } |
| 5440 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5441 | |
| 5442 | void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src, |
| 5443 | unsigned long dst_offset, unsigned long src_offset, |
| 5444 | unsigned long len) |
| 5445 | { |
| 5446 | u64 dst_len = dst->len; |
| 5447 | size_t cur; |
| 5448 | size_t offset; |
| 5449 | struct page *page; |
| 5450 | char *kaddr; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5451 | size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1); |
| 5452 | unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5453 | |
| 5454 | WARN_ON(src->len != dst_len); |
| 5455 | |
| 5456 | offset = (start_offset + dst_offset) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5457 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5458 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5459 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5460 | page = dst->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5461 | WARN_ON(!PageUptodate(page)); |
| 5462 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5463 | cur = min(len, (unsigned long)(PAGE_SIZE - offset)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5464 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5465 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5466 | read_extent_buffer(src, kaddr + offset, src_offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5467 | |
| 5468 | src_offset += cur; |
| 5469 | len -= cur; |
| 5470 | offset = 0; |
| 5471 | i++; |
| 5472 | } |
| 5473 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5474 | |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5475 | /* |
| 5476 | * The extent buffer bitmap operations are done with byte granularity because |
| 5477 | * bitmap items are not guaranteed to be aligned to a word and therefore a |
| 5478 | * single word in a bitmap may straddle two pages in the extent buffer. |
| 5479 | */ |
| 5480 | #define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE) |
| 5481 | #define BYTE_MASK ((1 << BITS_PER_BYTE) - 1) |
| 5482 | #define BITMAP_FIRST_BYTE_MASK(start) \ |
| 5483 | ((BYTE_MASK << ((start) & (BITS_PER_BYTE - 1))) & BYTE_MASK) |
| 5484 | #define BITMAP_LAST_BYTE_MASK(nbits) \ |
| 5485 | (BYTE_MASK >> (-(nbits) & (BITS_PER_BYTE - 1))) |
| 5486 | |
| 5487 | /* |
| 5488 | * eb_bitmap_offset() - calculate the page and offset of the byte containing the |
| 5489 | * given bit number |
| 5490 | * @eb: the extent buffer |
| 5491 | * @start: offset of the bitmap item in the extent buffer |
| 5492 | * @nr: bit number |
| 5493 | * @page_index: return index of the page in the extent buffer that contains the |
| 5494 | * given bit number |
| 5495 | * @page_offset: return offset into the page given by page_index |
| 5496 | * |
| 5497 | * This helper hides the ugliness of finding the byte in an extent buffer which |
| 5498 | * contains a given bit. |
| 5499 | */ |
| 5500 | static inline void eb_bitmap_offset(struct extent_buffer *eb, |
| 5501 | unsigned long start, unsigned long nr, |
| 5502 | unsigned long *page_index, |
| 5503 | size_t *page_offset) |
| 5504 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5505 | size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5506 | size_t byte_offset = BIT_BYTE(nr); |
| 5507 | size_t offset; |
| 5508 | |
| 5509 | /* |
| 5510 | * The byte we want is the offset of the extent buffer + the offset of |
| 5511 | * the bitmap item in the extent buffer + the offset of the byte in the |
| 5512 | * bitmap item. |
| 5513 | */ |
| 5514 | offset = start_offset + start + byte_offset; |
| 5515 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5516 | *page_index = offset >> PAGE_SHIFT; |
| 5517 | *page_offset = offset & (PAGE_SIZE - 1); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5518 | } |
| 5519 | |
| 5520 | /** |
| 5521 | * extent_buffer_test_bit - determine whether a bit in a bitmap item is set |
| 5522 | * @eb: the extent buffer |
| 5523 | * @start: offset of the bitmap item in the extent buffer |
| 5524 | * @nr: bit number to test |
| 5525 | */ |
| 5526 | int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start, |
| 5527 | unsigned long nr) |
| 5528 | { |
| 5529 | char *kaddr; |
| 5530 | struct page *page; |
| 5531 | unsigned long i; |
| 5532 | size_t offset; |
| 5533 | |
| 5534 | eb_bitmap_offset(eb, start, nr, &i, &offset); |
| 5535 | page = eb->pages[i]; |
| 5536 | WARN_ON(!PageUptodate(page)); |
| 5537 | kaddr = page_address(page); |
| 5538 | return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1))); |
| 5539 | } |
| 5540 | |
| 5541 | /** |
| 5542 | * extent_buffer_bitmap_set - set an area of a bitmap |
| 5543 | * @eb: the extent buffer |
| 5544 | * @start: offset of the bitmap item in the extent buffer |
| 5545 | * @pos: bit number of the first bit |
| 5546 | * @len: number of bits to set |
| 5547 | */ |
| 5548 | void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start, |
| 5549 | unsigned long pos, unsigned long len) |
| 5550 | { |
| 5551 | char *kaddr; |
| 5552 | struct page *page; |
| 5553 | unsigned long i; |
| 5554 | size_t offset; |
| 5555 | const unsigned int size = pos + len; |
| 5556 | int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE); |
| 5557 | unsigned int mask_to_set = BITMAP_FIRST_BYTE_MASK(pos); |
| 5558 | |
| 5559 | eb_bitmap_offset(eb, start, pos, &i, &offset); |
| 5560 | page = eb->pages[i]; |
| 5561 | WARN_ON(!PageUptodate(page)); |
| 5562 | kaddr = page_address(page); |
| 5563 | |
| 5564 | while (len >= bits_to_set) { |
| 5565 | kaddr[offset] |= mask_to_set; |
| 5566 | len -= bits_to_set; |
| 5567 | bits_to_set = BITS_PER_BYTE; |
| 5568 | mask_to_set = ~0U; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5569 | if (++offset >= PAGE_SIZE && len > 0) { |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5570 | offset = 0; |
| 5571 | page = eb->pages[++i]; |
| 5572 | WARN_ON(!PageUptodate(page)); |
| 5573 | kaddr = page_address(page); |
| 5574 | } |
| 5575 | } |
| 5576 | if (len) { |
| 5577 | mask_to_set &= BITMAP_LAST_BYTE_MASK(size); |
| 5578 | kaddr[offset] |= mask_to_set; |
| 5579 | } |
| 5580 | } |
| 5581 | |
| 5582 | |
| 5583 | /** |
| 5584 | * extent_buffer_bitmap_clear - clear an area of a bitmap |
| 5585 | * @eb: the extent buffer |
| 5586 | * @start: offset of the bitmap item in the extent buffer |
| 5587 | * @pos: bit number of the first bit |
| 5588 | * @len: number of bits to clear |
| 5589 | */ |
| 5590 | void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start, |
| 5591 | unsigned long pos, unsigned long len) |
| 5592 | { |
| 5593 | char *kaddr; |
| 5594 | struct page *page; |
| 5595 | unsigned long i; |
| 5596 | size_t offset; |
| 5597 | const unsigned int size = pos + len; |
| 5598 | int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE); |
| 5599 | unsigned int mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos); |
| 5600 | |
| 5601 | eb_bitmap_offset(eb, start, pos, &i, &offset); |
| 5602 | page = eb->pages[i]; |
| 5603 | WARN_ON(!PageUptodate(page)); |
| 5604 | kaddr = page_address(page); |
| 5605 | |
| 5606 | while (len >= bits_to_clear) { |
| 5607 | kaddr[offset] &= ~mask_to_clear; |
| 5608 | len -= bits_to_clear; |
| 5609 | bits_to_clear = BITS_PER_BYTE; |
| 5610 | mask_to_clear = ~0U; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5611 | if (++offset >= PAGE_SIZE && len > 0) { |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5612 | offset = 0; |
| 5613 | page = eb->pages[++i]; |
| 5614 | WARN_ON(!PageUptodate(page)); |
| 5615 | kaddr = page_address(page); |
| 5616 | } |
| 5617 | } |
| 5618 | if (len) { |
| 5619 | mask_to_clear &= BITMAP_LAST_BYTE_MASK(size); |
| 5620 | kaddr[offset] &= ~mask_to_clear; |
| 5621 | } |
| 5622 | } |
| 5623 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5624 | static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len) |
| 5625 | { |
| 5626 | unsigned long distance = (src > dst) ? src - dst : dst - src; |
| 5627 | return distance < len; |
| 5628 | } |
| 5629 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5630 | static void copy_pages(struct page *dst_page, struct page *src_page, |
| 5631 | unsigned long dst_off, unsigned long src_off, |
| 5632 | unsigned long len) |
| 5633 | { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5634 | char *dst_kaddr = page_address(dst_page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5635 | char *src_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5636 | int must_memmove = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5637 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5638 | if (dst_page != src_page) { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5639 | src_kaddr = page_address(src_page); |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5640 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5641 | src_kaddr = dst_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5642 | if (areas_overlap(src_off, dst_off, len)) |
| 5643 | must_memmove = 1; |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5644 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5645 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5646 | if (must_memmove) |
| 5647 | memmove(dst_kaddr + dst_off, src_kaddr + src_off, len); |
| 5648 | else |
| 5649 | memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5650 | } |
| 5651 | |
| 5652 | void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 5653 | unsigned long src_offset, unsigned long len) |
| 5654 | { |
| 5655 | size_t cur; |
| 5656 | size_t dst_off_in_page; |
| 5657 | size_t src_off_in_page; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5658 | size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5659 | unsigned long dst_i; |
| 5660 | unsigned long src_i; |
| 5661 | |
| 5662 | if (src_offset + len > dst->len) { |
David Sterba | f14d104 | 2015-10-08 11:37:06 +0200 | [diff] [blame] | 5663 | btrfs_err(dst->fs_info, |
| 5664 | "memmove bogus src_offset %lu move " |
| 5665 | "len %lu dst len %lu", src_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5666 | BUG_ON(1); |
| 5667 | } |
| 5668 | if (dst_offset + len > dst->len) { |
David Sterba | f14d104 | 2015-10-08 11:37:06 +0200 | [diff] [blame] | 5669 | btrfs_err(dst->fs_info, |
| 5670 | "memmove bogus dst_offset %lu move " |
| 5671 | "len %lu dst len %lu", dst_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5672 | BUG_ON(1); |
| 5673 | } |
| 5674 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5675 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5676 | dst_off_in_page = (start_offset + dst_offset) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5677 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5678 | src_off_in_page = (start_offset + src_offset) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5679 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5680 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5681 | dst_i = (start_offset + dst_offset) >> PAGE_SHIFT; |
| 5682 | src_i = (start_offset + src_offset) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5683 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5684 | cur = min(len, (unsigned long)(PAGE_SIZE - |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5685 | src_off_in_page)); |
| 5686 | cur = min_t(unsigned long, cur, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5687 | (unsigned long)(PAGE_SIZE - dst_off_in_page)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5688 | |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5689 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5690 | dst_off_in_page, src_off_in_page, cur); |
| 5691 | |
| 5692 | src_offset += cur; |
| 5693 | dst_offset += cur; |
| 5694 | len -= cur; |
| 5695 | } |
| 5696 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5697 | |
| 5698 | void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 5699 | unsigned long src_offset, unsigned long len) |
| 5700 | { |
| 5701 | size_t cur; |
| 5702 | size_t dst_off_in_page; |
| 5703 | size_t src_off_in_page; |
| 5704 | unsigned long dst_end = dst_offset + len - 1; |
| 5705 | unsigned long src_end = src_offset + len - 1; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5706 | size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5707 | unsigned long dst_i; |
| 5708 | unsigned long src_i; |
| 5709 | |
| 5710 | if (src_offset + len > dst->len) { |
David Sterba | f14d104 | 2015-10-08 11:37:06 +0200 | [diff] [blame] | 5711 | btrfs_err(dst->fs_info, "memmove bogus src_offset %lu move " |
| 5712 | "len %lu len %lu", src_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5713 | BUG_ON(1); |
| 5714 | } |
| 5715 | if (dst_offset + len > dst->len) { |
David Sterba | f14d104 | 2015-10-08 11:37:06 +0200 | [diff] [blame] | 5716 | btrfs_err(dst->fs_info, "memmove bogus dst_offset %lu move " |
| 5717 | "len %lu len %lu", dst_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5718 | BUG_ON(1); |
| 5719 | } |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5720 | if (dst_offset < src_offset) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5721 | memcpy_extent_buffer(dst, dst_offset, src_offset, len); |
| 5722 | return; |
| 5723 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5724 | while (len > 0) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5725 | dst_i = (start_offset + dst_end) >> PAGE_SHIFT; |
| 5726 | src_i = (start_offset + src_end) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5727 | |
| 5728 | dst_off_in_page = (start_offset + dst_end) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5729 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5730 | src_off_in_page = (start_offset + src_end) & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5731 | (PAGE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5732 | |
| 5733 | cur = min_t(unsigned long, len, src_off_in_page + 1); |
| 5734 | cur = min(cur, dst_off_in_page + 1); |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5735 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5736 | dst_off_in_page - cur + 1, |
| 5737 | src_off_in_page - cur + 1, cur); |
| 5738 | |
| 5739 | dst_end -= cur; |
| 5740 | src_end -= cur; |
| 5741 | len -= cur; |
| 5742 | } |
| 5743 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5744 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5745 | int try_release_extent_buffer(struct page *page) |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5746 | { |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5747 | struct extent_buffer *eb; |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5748 | |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5749 | /* |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5750 | * We need to make sure noboody is attaching this page to an eb right |
| 5751 | * now. |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5752 | */ |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5753 | spin_lock(&page->mapping->private_lock); |
| 5754 | if (!PagePrivate(page)) { |
| 5755 | spin_unlock(&page->mapping->private_lock); |
| 5756 | return 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5757 | } |
| 5758 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5759 | eb = (struct extent_buffer *)page->private; |
| 5760 | BUG_ON(!eb); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5761 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5762 | /* |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5763 | * This is a little awful but should be ok, we need to make sure that |
| 5764 | * the eb doesn't disappear out from under us while we're looking at |
| 5765 | * this page. |
| 5766 | */ |
| 5767 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5768 | if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5769 | spin_unlock(&eb->refs_lock); |
| 5770 | spin_unlock(&page->mapping->private_lock); |
| 5771 | return 0; |
| 5772 | } |
| 5773 | spin_unlock(&page->mapping->private_lock); |
| 5774 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5775 | /* |
| 5776 | * If tree ref isn't set then we know the ref on this eb is a real ref, |
| 5777 | * so just return, this page will likely be freed soon anyway. |
| 5778 | */ |
| 5779 | if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { |
| 5780 | spin_unlock(&eb->refs_lock); |
| 5781 | return 0; |
| 5782 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5783 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5784 | return release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5785 | } |