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 | 2db0496 | 2008-08-07 11:19:43 -0400 | [diff] [blame] | 16 | #include "compat.h" |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 17 | #include "ctree.h" |
| 18 | #include "btrfs_inode.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 19 | #include "volumes.h" |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 20 | #include "check-integrity.h" |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 21 | #include "locking.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 22 | #include "rcu-string.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 | |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 28 | #ifdef CONFIG_BTRFS_DEBUG |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 29 | static LIST_HEAD(buffers); |
| 30 | static LIST_HEAD(states); |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 31 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 32 | static DEFINE_SPINLOCK(leak_lock); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 33 | |
| 34 | static inline |
| 35 | void btrfs_leak_debug_add(struct list_head *new, struct list_head *head) |
| 36 | { |
| 37 | unsigned long flags; |
| 38 | |
| 39 | spin_lock_irqsave(&leak_lock, flags); |
| 40 | list_add(new, head); |
| 41 | spin_unlock_irqrestore(&leak_lock, flags); |
| 42 | } |
| 43 | |
| 44 | static inline |
| 45 | void btrfs_leak_debug_del(struct list_head *entry) |
| 46 | { |
| 47 | unsigned long flags; |
| 48 | |
| 49 | spin_lock_irqsave(&leak_lock, flags); |
| 50 | list_del(entry); |
| 51 | spin_unlock_irqrestore(&leak_lock, flags); |
| 52 | } |
| 53 | |
| 54 | static inline |
| 55 | void btrfs_leak_debug_check(void) |
| 56 | { |
| 57 | struct extent_state *state; |
| 58 | struct extent_buffer *eb; |
| 59 | |
| 60 | while (!list_empty(&states)) { |
| 61 | state = list_entry(states.next, struct extent_state, leak_list); |
| 62 | printk(KERN_ERR "btrfs state leak: start %llu end %llu " |
| 63 | "state %lu in tree %p refs %d\n", |
| 64 | (unsigned long long)state->start, |
| 65 | (unsigned long long)state->end, |
| 66 | state->state, state->tree, atomic_read(&state->refs)); |
| 67 | list_del(&state->leak_list); |
| 68 | kmem_cache_free(extent_state_cache, state); |
| 69 | } |
| 70 | |
| 71 | while (!list_empty(&buffers)) { |
| 72 | eb = list_entry(buffers.next, struct extent_buffer, leak_list); |
| 73 | printk(KERN_ERR "btrfs buffer leak start %llu len %lu " |
| 74 | "refs %d\n", (unsigned long long)eb->start, |
| 75 | eb->len, atomic_read(&eb->refs)); |
| 76 | list_del(&eb->leak_list); |
| 77 | kmem_cache_free(extent_buffer_cache, eb); |
| 78 | } |
| 79 | } |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 80 | |
| 81 | #define btrfs_debug_check_extent_io_range(inode, start, end) \ |
| 82 | __btrfs_debug_check_extent_io_range(__func__, (inode), (start), (end)) |
| 83 | static inline void __btrfs_debug_check_extent_io_range(const char *caller, |
| 84 | struct inode *inode, u64 start, u64 end) |
| 85 | { |
| 86 | u64 isize = i_size_read(inode); |
| 87 | |
| 88 | if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) { |
| 89 | printk_ratelimited(KERN_DEBUG |
| 90 | "btrfs: %s: ino %llu isize %llu odd range [%llu,%llu]\n", |
| 91 | caller, |
| 92 | (unsigned long long)btrfs_ino(inode), |
| 93 | (unsigned long long)isize, |
| 94 | (unsigned long long)start, |
| 95 | (unsigned long long)end); |
| 96 | } |
| 97 | } |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 98 | #else |
| 99 | #define btrfs_leak_debug_add(new, head) do {} while (0) |
| 100 | #define btrfs_leak_debug_del(entry) do {} while (0) |
| 101 | #define btrfs_leak_debug_check() do {} while (0) |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 102 | #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] | 103 | #endif |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 104 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 105 | #define BUFFER_LRU_MAX 64 |
| 106 | |
| 107 | struct tree_entry { |
| 108 | u64 start; |
| 109 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 110 | struct rb_node rb_node; |
| 111 | }; |
| 112 | |
| 113 | struct extent_page_data { |
| 114 | struct bio *bio; |
| 115 | struct extent_io_tree *tree; |
| 116 | get_extent_t *get_extent; |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 117 | unsigned long bio_flags; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 118 | |
| 119 | /* tells writepage not to lock the state bits for this range |
| 120 | * it still does the unlocking |
| 121 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 122 | unsigned int extent_locked:1; |
| 123 | |
| 124 | /* tells the submit_bio code to use a WRITE_SYNC */ |
| 125 | unsigned int sync_io:1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 126 | }; |
| 127 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 128 | static noinline void flush_write_bio(void *data); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 129 | static inline struct btrfs_fs_info * |
| 130 | tree_fs_info(struct extent_io_tree *tree) |
| 131 | { |
| 132 | return btrfs_sb(tree->mapping->host->i_sb); |
| 133 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 134 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 135 | int __init extent_io_init(void) |
| 136 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 137 | extent_state_cache = kmem_cache_create("btrfs_extent_state", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 138 | sizeof(struct extent_state), 0, |
| 139 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 140 | if (!extent_state_cache) |
| 141 | return -ENOMEM; |
| 142 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 143 | extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 144 | sizeof(struct extent_buffer), 0, |
| 145 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 146 | if (!extent_buffer_cache) |
| 147 | goto free_state_cache; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 148 | |
| 149 | btrfs_bioset = bioset_create(BIO_POOL_SIZE, |
| 150 | offsetof(struct btrfs_io_bio, bio)); |
| 151 | if (!btrfs_bioset) |
| 152 | goto free_buffer_cache; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 153 | return 0; |
| 154 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 155 | free_buffer_cache: |
| 156 | kmem_cache_destroy(extent_buffer_cache); |
| 157 | extent_buffer_cache = NULL; |
| 158 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 159 | free_state_cache: |
| 160 | kmem_cache_destroy(extent_state_cache); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 161 | extent_state_cache = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 162 | return -ENOMEM; |
| 163 | } |
| 164 | |
| 165 | void extent_io_exit(void) |
| 166 | { |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 167 | btrfs_leak_debug_check(); |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 168 | |
| 169 | /* |
| 170 | * Make sure all delayed rcu free are flushed before we |
| 171 | * destroy caches. |
| 172 | */ |
| 173 | rcu_barrier(); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 174 | if (extent_state_cache) |
| 175 | kmem_cache_destroy(extent_state_cache); |
| 176 | if (extent_buffer_cache) |
| 177 | kmem_cache_destroy(extent_buffer_cache); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 178 | if (btrfs_bioset) |
| 179 | bioset_free(btrfs_bioset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void extent_io_tree_init(struct extent_io_tree *tree, |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 183 | struct address_space *mapping) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 184 | { |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 185 | tree->state = RB_ROOT; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 186 | INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 187 | tree->ops = NULL; |
| 188 | tree->dirty_bytes = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 189 | spin_lock_init(&tree->lock); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 190 | spin_lock_init(&tree->buffer_lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 191 | tree->mapping = mapping; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 192 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 193 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 194 | static struct extent_state *alloc_extent_state(gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 195 | { |
| 196 | struct extent_state *state; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 197 | |
| 198 | state = kmem_cache_alloc(extent_state_cache, mask); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 199 | if (!state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 200 | return state; |
| 201 | state->state = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 202 | state->private = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 203 | state->tree = NULL; |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 204 | btrfs_leak_debug_add(&state->leak_list, &states); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 205 | atomic_set(&state->refs, 1); |
| 206 | init_waitqueue_head(&state->wq); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 207 | trace_alloc_extent_state(state, mask, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 208 | return state; |
| 209 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 210 | |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 211 | void free_extent_state(struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 212 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 213 | if (!state) |
| 214 | return; |
| 215 | if (atomic_dec_and_test(&state->refs)) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 216 | WARN_ON(state->tree); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 217 | btrfs_leak_debug_del(&state->leak_list); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 218 | trace_free_extent_state(state, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 219 | kmem_cache_free(extent_state_cache, state); |
| 220 | } |
| 221 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 222 | |
| 223 | static struct rb_node *tree_insert(struct rb_root *root, u64 offset, |
| 224 | struct rb_node *node) |
| 225 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 226 | struct rb_node **p = &root->rb_node; |
| 227 | struct rb_node *parent = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 228 | struct tree_entry *entry; |
| 229 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 230 | while (*p) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 231 | parent = *p; |
| 232 | entry = rb_entry(parent, struct tree_entry, rb_node); |
| 233 | |
| 234 | if (offset < entry->start) |
| 235 | p = &(*p)->rb_left; |
| 236 | else if (offset > entry->end) |
| 237 | p = &(*p)->rb_right; |
| 238 | else |
| 239 | return parent; |
| 240 | } |
| 241 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 242 | rb_link_node(node, parent, p); |
| 243 | rb_insert_color(node, root); |
| 244 | return NULL; |
| 245 | } |
| 246 | |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 247 | static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 248 | struct rb_node **prev_ret, |
| 249 | struct rb_node **next_ret) |
| 250 | { |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 251 | struct rb_root *root = &tree->state; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 252 | struct rb_node *n = root->rb_node; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 253 | struct rb_node *prev = NULL; |
| 254 | struct rb_node *orig_prev = NULL; |
| 255 | struct tree_entry *entry; |
| 256 | struct tree_entry *prev_entry = NULL; |
| 257 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 258 | while (n) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 259 | entry = rb_entry(n, struct tree_entry, rb_node); |
| 260 | prev = n; |
| 261 | prev_entry = entry; |
| 262 | |
| 263 | if (offset < entry->start) |
| 264 | n = n->rb_left; |
| 265 | else if (offset > entry->end) |
| 266 | n = n->rb_right; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 267 | else |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 268 | return n; |
| 269 | } |
| 270 | |
| 271 | if (prev_ret) { |
| 272 | orig_prev = prev; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 273 | while (prev && offset > prev_entry->end) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 274 | prev = rb_next(prev); |
| 275 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| 276 | } |
| 277 | *prev_ret = prev; |
| 278 | prev = orig_prev; |
| 279 | } |
| 280 | |
| 281 | if (next_ret) { |
| 282 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 283 | while (prev && offset < prev_entry->start) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 284 | prev = rb_prev(prev); |
| 285 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| 286 | } |
| 287 | *next_ret = prev; |
| 288 | } |
| 289 | return NULL; |
| 290 | } |
| 291 | |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 292 | static inline struct rb_node *tree_search(struct extent_io_tree *tree, |
| 293 | u64 offset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 294 | { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 295 | struct rb_node *prev = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 296 | struct rb_node *ret; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 297 | |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 298 | ret = __etree_search(tree, offset, &prev, NULL); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 299 | if (!ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 300 | return prev; |
| 301 | return ret; |
| 302 | } |
| 303 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 304 | static void merge_cb(struct extent_io_tree *tree, struct extent_state *new, |
| 305 | struct extent_state *other) |
| 306 | { |
| 307 | if (tree->ops && tree->ops->merge_extent_hook) |
| 308 | tree->ops->merge_extent_hook(tree->mapping->host, new, |
| 309 | other); |
| 310 | } |
| 311 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 312 | /* |
| 313 | * utility function to look for merge candidates inside a given range. |
| 314 | * Any extents with matching state are merged together into a single |
| 315 | * extent in the tree. Extents with EXTENT_IO in their state field |
| 316 | * are not merged because the end_io handlers need to be able to do |
| 317 | * operations on them without sleeping (or doing allocations/splits). |
| 318 | * |
| 319 | * This should be called with the tree lock held. |
| 320 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 321 | static void merge_state(struct extent_io_tree *tree, |
| 322 | struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 323 | { |
| 324 | struct extent_state *other; |
| 325 | struct rb_node *other_node; |
| 326 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 327 | if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 328 | return; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 329 | |
| 330 | other_node = rb_prev(&state->rb_node); |
| 331 | if (other_node) { |
| 332 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 333 | if (other->end == state->start - 1 && |
| 334 | other->state == state->state) { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 335 | merge_cb(tree, state, other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 336 | state->start = other->start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 337 | other->tree = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 338 | rb_erase(&other->rb_node, &tree->state); |
| 339 | free_extent_state(other); |
| 340 | } |
| 341 | } |
| 342 | other_node = rb_next(&state->rb_node); |
| 343 | if (other_node) { |
| 344 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 345 | if (other->start == state->end + 1 && |
| 346 | other->state == state->state) { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 347 | merge_cb(tree, state, other); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 348 | state->end = other->end; |
| 349 | other->tree = NULL; |
| 350 | rb_erase(&other->rb_node, &tree->state); |
| 351 | free_extent_state(other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 352 | } |
| 353 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 354 | } |
| 355 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 356 | static void set_state_cb(struct extent_io_tree *tree, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 357 | struct extent_state *state, unsigned long *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 358 | { |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 359 | if (tree->ops && tree->ops->set_bit_hook) |
| 360 | tree->ops->set_bit_hook(tree->mapping->host, state, bits); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | static void clear_state_cb(struct extent_io_tree *tree, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 364 | struct extent_state *state, unsigned long *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 365 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 366 | if (tree->ops && tree->ops->clear_bit_hook) |
| 367 | tree->ops->clear_bit_hook(tree->mapping->host, state, bits); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 368 | } |
| 369 | |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 370 | static void set_state_bits(struct extent_io_tree *tree, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 371 | struct extent_state *state, unsigned long *bits); |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 372 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 373 | /* |
| 374 | * insert an extent_state struct into the tree. 'bits' are set on the |
| 375 | * struct before it is inserted. |
| 376 | * |
| 377 | * This may return -EEXIST if the extent is already there, in which case the |
| 378 | * state struct is freed. |
| 379 | * |
| 380 | * The tree lock is not taken internally. This is a utility function and |
| 381 | * probably isn't what you want to call (see set/clear_extent_bit). |
| 382 | */ |
| 383 | static int insert_state(struct extent_io_tree *tree, |
| 384 | struct extent_state *state, u64 start, u64 end, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 385 | unsigned long *bits) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 386 | { |
| 387 | struct rb_node *node; |
| 388 | |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 389 | if (end < start) |
| 390 | WARN(1, KERN_ERR "btrfs end < start %llu %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 391 | (unsigned long long)end, |
| 392 | (unsigned long long)start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 393 | state->start = start; |
| 394 | state->end = end; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 395 | |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 396 | set_state_bits(tree, state, bits); |
| 397 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 398 | node = tree_insert(&tree->state, end, &state->rb_node); |
| 399 | if (node) { |
| 400 | struct extent_state *found; |
| 401 | found = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 402 | printk(KERN_ERR "btrfs found node %llu %llu on insert of " |
| 403 | "%llu %llu\n", (unsigned long long)found->start, |
| 404 | (unsigned long long)found->end, |
| 405 | (unsigned long long)start, (unsigned long long)end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 406 | return -EEXIST; |
| 407 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 408 | state->tree = tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 409 | merge_state(tree, state); |
| 410 | return 0; |
| 411 | } |
| 412 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 413 | 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] | 414 | u64 split) |
| 415 | { |
| 416 | if (tree->ops && tree->ops->split_extent_hook) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 417 | tree->ops->split_extent_hook(tree->mapping->host, orig, split); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 418 | } |
| 419 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 420 | /* |
| 421 | * split a given extent state struct in two, inserting the preallocated |
| 422 | * struct 'prealloc' as the newly created second half. 'split' indicates an |
| 423 | * offset inside 'orig' where it should be split. |
| 424 | * |
| 425 | * Before calling, |
| 426 | * the tree has 'orig' at [orig->start, orig->end]. After calling, there |
| 427 | * are two extent state structs in the tree: |
| 428 | * prealloc: [orig->start, split - 1] |
| 429 | * orig: [ split, orig->end ] |
| 430 | * |
| 431 | * The tree locks are not taken by this function. They need to be held |
| 432 | * by the caller. |
| 433 | */ |
| 434 | static int split_state(struct extent_io_tree *tree, struct extent_state *orig, |
| 435 | struct extent_state *prealloc, u64 split) |
| 436 | { |
| 437 | struct rb_node *node; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 438 | |
| 439 | split_cb(tree, orig, split); |
| 440 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 441 | prealloc->start = orig->start; |
| 442 | prealloc->end = split - 1; |
| 443 | prealloc->state = orig->state; |
| 444 | orig->start = split; |
| 445 | |
| 446 | node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node); |
| 447 | if (node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 448 | free_extent_state(prealloc); |
| 449 | return -EEXIST; |
| 450 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 451 | prealloc->tree = tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 452 | return 0; |
| 453 | } |
| 454 | |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 455 | static struct extent_state *next_state(struct extent_state *state) |
| 456 | { |
| 457 | struct rb_node *next = rb_next(&state->rb_node); |
| 458 | if (next) |
| 459 | return rb_entry(next, struct extent_state, rb_node); |
| 460 | else |
| 461 | return NULL; |
| 462 | } |
| 463 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 464 | /* |
| 465 | * utility function to clear some bits in an extent state struct. |
Wang Sheng-Hui | 1b303fc | 2012-04-06 14:35:18 +0800 | [diff] [blame] | 466 | * 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] | 467 | * |
| 468 | * If no bits are set on the state struct after clearing things, the |
| 469 | * struct is freed and removed from the tree |
| 470 | */ |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 471 | static struct extent_state *clear_state_bit(struct extent_io_tree *tree, |
| 472 | struct extent_state *state, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 473 | unsigned long *bits, int wake) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 474 | { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 475 | struct extent_state *next; |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 476 | unsigned long bits_to_clear = *bits & ~EXTENT_CTLBITS; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 477 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 478 | if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 479 | u64 range = state->end - state->start + 1; |
| 480 | WARN_ON(range > tree->dirty_bytes); |
| 481 | tree->dirty_bytes -= range; |
| 482 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 483 | clear_state_cb(tree, state, bits); |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 484 | state->state &= ~bits_to_clear; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 485 | if (wake) |
| 486 | wake_up(&state->wq); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 487 | if (state->state == 0) { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 488 | next = next_state(state); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 489 | if (state->tree) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 490 | rb_erase(&state->rb_node, &tree->state); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 491 | state->tree = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 492 | free_extent_state(state); |
| 493 | } else { |
| 494 | WARN_ON(1); |
| 495 | } |
| 496 | } else { |
| 497 | merge_state(tree, state); |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 498 | next = next_state(state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 499 | } |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 500 | return next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 501 | } |
| 502 | |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 503 | static struct extent_state * |
| 504 | alloc_extent_state_atomic(struct extent_state *prealloc) |
| 505 | { |
| 506 | if (!prealloc) |
| 507 | prealloc = alloc_extent_state(GFP_ATOMIC); |
| 508 | |
| 509 | return prealloc; |
| 510 | } |
| 511 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 512 | 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] | 513 | { |
| 514 | btrfs_panic(tree_fs_info(tree), err, "Locking error: " |
| 515 | "Extent tree was modified by another " |
| 516 | "thread while locked."); |
| 517 | } |
| 518 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 519 | /* |
| 520 | * clear some bits on a range in the tree. This may require splitting |
| 521 | * or inserting elements in the tree, so the gfp mask is used to |
| 522 | * indicate which allocations or sleeping are allowed. |
| 523 | * |
| 524 | * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove |
| 525 | * the given range from the tree regardless of state (ie for truncate). |
| 526 | * |
| 527 | * the range [start, end] is inclusive. |
| 528 | * |
Jeff Mahoney | 6763af8 | 2012-03-01 14:56:29 +0100 | [diff] [blame] | 529 | * 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] | 530 | */ |
| 531 | int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 532 | unsigned long bits, int wake, int delete, |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 533 | struct extent_state **cached_state, |
| 534 | gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 535 | { |
| 536 | struct extent_state *state; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 537 | struct extent_state *cached; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 538 | struct extent_state *prealloc = NULL; |
| 539 | struct rb_node *node; |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 540 | u64 last_end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 541 | int err; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 542 | int clear = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 543 | |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 544 | btrfs_debug_check_extent_io_range(tree->mapping->host, start, end); |
| 545 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 546 | if (bits & EXTENT_DELALLOC) |
| 547 | bits |= EXTENT_NORESERVE; |
| 548 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 549 | if (delete) |
| 550 | bits |= ~EXTENT_CTLBITS; |
| 551 | bits |= EXTENT_FIRST_DELALLOC; |
| 552 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 553 | if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY)) |
| 554 | clear = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 555 | again: |
| 556 | if (!prealloc && (mask & __GFP_WAIT)) { |
| 557 | prealloc = alloc_extent_state(mask); |
| 558 | if (!prealloc) |
| 559 | return -ENOMEM; |
| 560 | } |
| 561 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 562 | spin_lock(&tree->lock); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 563 | if (cached_state) { |
| 564 | cached = *cached_state; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 565 | |
| 566 | if (clear) { |
| 567 | *cached_state = NULL; |
| 568 | cached_state = NULL; |
| 569 | } |
| 570 | |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 571 | if (cached && cached->tree && cached->start <= start && |
| 572 | cached->end > start) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 573 | if (clear) |
| 574 | atomic_dec(&cached->refs); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 575 | state = cached; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 576 | goto hit_next; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 577 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 578 | if (clear) |
| 579 | free_extent_state(cached); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 580 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 581 | /* |
| 582 | * this search will find the extents that end after |
| 583 | * our range starts |
| 584 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 585 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 586 | if (!node) |
| 587 | goto out; |
| 588 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 589 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 590 | if (state->start > end) |
| 591 | goto out; |
| 592 | WARN_ON(state->end < start); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 593 | last_end = state->end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 594 | |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 595 | /* the state doesn't have the wanted bits, go ahead */ |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 596 | if (!(state->state & bits)) { |
| 597 | state = next_state(state); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 598 | goto next; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 599 | } |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 600 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 601 | /* |
| 602 | * | ---- desired range ---- | |
| 603 | * | state | or |
| 604 | * | ------------- state -------------- | |
| 605 | * |
| 606 | * We need to split the extent we found, and may flip |
| 607 | * bits on second half. |
| 608 | * |
| 609 | * If the extent we found extends past our range, we |
| 610 | * just split and search again. It'll get split again |
| 611 | * the next time though. |
| 612 | * |
| 613 | * If the extent we found is inside our range, we clear |
| 614 | * the desired bit on it. |
| 615 | */ |
| 616 | |
| 617 | if (state->start < start) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 618 | prealloc = alloc_extent_state_atomic(prealloc); |
| 619 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 620 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 621 | if (err) |
| 622 | extent_io_tree_panic(tree, err); |
| 623 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 624 | prealloc = NULL; |
| 625 | if (err) |
| 626 | goto out; |
| 627 | if (state->end <= end) { |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 628 | state = clear_state_bit(tree, state, &bits, wake); |
| 629 | goto next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 630 | } |
| 631 | goto search_again; |
| 632 | } |
| 633 | /* |
| 634 | * | ---- desired range ---- | |
| 635 | * | state | |
| 636 | * We need to split the extent, and clear the bit |
| 637 | * on the first half |
| 638 | */ |
| 639 | if (state->start <= end && state->end > end) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 640 | prealloc = alloc_extent_state_atomic(prealloc); |
| 641 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 642 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 643 | if (err) |
| 644 | extent_io_tree_panic(tree, err); |
| 645 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 646 | if (wake) |
| 647 | wake_up(&state->wq); |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 648 | |
Jeff Mahoney | 6763af8 | 2012-03-01 14:56:29 +0100 | [diff] [blame] | 649 | clear_state_bit(tree, prealloc, &bits, wake); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 650 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 651 | prealloc = NULL; |
| 652 | goto out; |
| 653 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 654 | |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 655 | state = clear_state_bit(tree, state, &bits, wake); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 656 | next: |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 657 | if (last_end == (u64)-1) |
| 658 | goto out; |
| 659 | start = last_end + 1; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 660 | if (start <= end && state && !need_resched()) |
Liu Bo | 692e575 | 2012-02-16 18:34:36 +0800 | [diff] [blame] | 661 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 662 | goto search_again; |
| 663 | |
| 664 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 665 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 666 | if (prealloc) |
| 667 | free_extent_state(prealloc); |
| 668 | |
Jeff Mahoney | 6763af8 | 2012-03-01 14:56:29 +0100 | [diff] [blame] | 669 | return 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 670 | |
| 671 | search_again: |
| 672 | if (start > end) |
| 673 | goto out; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 674 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 675 | if (mask & __GFP_WAIT) |
| 676 | cond_resched(); |
| 677 | goto again; |
| 678 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 679 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 680 | static void wait_on_state(struct extent_io_tree *tree, |
| 681 | struct extent_state *state) |
Christoph Hellwig | 641f521 | 2008-12-02 06:36:10 -0500 | [diff] [blame] | 682 | __releases(tree->lock) |
| 683 | __acquires(tree->lock) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 684 | { |
| 685 | DEFINE_WAIT(wait); |
| 686 | prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 687 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 688 | schedule(); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 689 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 690 | finish_wait(&state->wq, &wait); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /* |
| 694 | * waits for one or more bits to clear on a range in the state tree. |
| 695 | * The range [start, end] is inclusive. |
| 696 | * The tree lock is taken by this function |
| 697 | */ |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 698 | static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 699 | unsigned long bits) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 700 | { |
| 701 | struct extent_state *state; |
| 702 | struct rb_node *node; |
| 703 | |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 704 | btrfs_debug_check_extent_io_range(tree->mapping->host, start, end); |
| 705 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 706 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 707 | again: |
| 708 | while (1) { |
| 709 | /* |
| 710 | * this search will find all the extents that end after |
| 711 | * our range starts |
| 712 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 713 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 714 | if (!node) |
| 715 | break; |
| 716 | |
| 717 | state = rb_entry(node, struct extent_state, rb_node); |
| 718 | |
| 719 | if (state->start > end) |
| 720 | goto out; |
| 721 | |
| 722 | if (state->state & bits) { |
| 723 | start = state->start; |
| 724 | atomic_inc(&state->refs); |
| 725 | wait_on_state(tree, state); |
| 726 | free_extent_state(state); |
| 727 | goto again; |
| 728 | } |
| 729 | start = state->end + 1; |
| 730 | |
| 731 | if (start > end) |
| 732 | break; |
| 733 | |
Xiao Guangrong | ded91f0 | 2011-07-14 03:19:27 +0000 | [diff] [blame] | 734 | cond_resched_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 735 | } |
| 736 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 737 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 738 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 739 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 740 | static void set_state_bits(struct extent_io_tree *tree, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 741 | struct extent_state *state, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 742 | unsigned long *bits) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 743 | { |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 744 | unsigned long bits_to_set = *bits & ~EXTENT_CTLBITS; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 745 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 746 | set_state_cb(tree, state, bits); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 747 | if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 748 | u64 range = state->end - state->start + 1; |
| 749 | tree->dirty_bytes += range; |
| 750 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 751 | state->state |= bits_to_set; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 752 | } |
| 753 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 754 | static void cache_state(struct extent_state *state, |
| 755 | struct extent_state **cached_ptr) |
| 756 | { |
| 757 | if (cached_ptr && !(*cached_ptr)) { |
| 758 | if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) { |
| 759 | *cached_ptr = state; |
| 760 | atomic_inc(&state->refs); |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 765 | static void uncache_state(struct extent_state **cached_ptr) |
| 766 | { |
| 767 | if (cached_ptr && (*cached_ptr)) { |
| 768 | struct extent_state *state = *cached_ptr; |
Chris Mason | 109b36a | 2011-04-12 13:57:39 -0400 | [diff] [blame] | 769 | *cached_ptr = NULL; |
| 770 | free_extent_state(state); |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 774 | /* |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 775 | * set some bits on a range in the tree. This may require allocations or |
| 776 | * sleeping, so the gfp mask is used to indicate what is allowed. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 777 | * |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 778 | * If any of the exclusive bits are set, this will fail with -EEXIST if some |
| 779 | * part of the range already has the desired bits set. The start of the |
| 780 | * existing range is returned in failed_start in this case. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 781 | * |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 782 | * [start, end] is inclusive This takes the tree lock. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 783 | */ |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 784 | |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 785 | static int __must_check |
| 786 | __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 787 | unsigned long bits, unsigned long exclusive_bits, |
| 788 | u64 *failed_start, struct extent_state **cached_state, |
| 789 | gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 790 | { |
| 791 | struct extent_state *state; |
| 792 | struct extent_state *prealloc = NULL; |
| 793 | struct rb_node *node; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 794 | int err = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 795 | u64 last_start; |
| 796 | u64 last_end; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 797 | |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 798 | btrfs_debug_check_extent_io_range(tree->mapping->host, start, end); |
| 799 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 800 | bits |= EXTENT_FIRST_DELALLOC; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 801 | again: |
| 802 | if (!prealloc && (mask & __GFP_WAIT)) { |
| 803 | prealloc = alloc_extent_state(mask); |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 804 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 805 | } |
| 806 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 807 | spin_lock(&tree->lock); |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 808 | if (cached_state && *cached_state) { |
| 809 | state = *cached_state; |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 810 | if (state->start <= start && state->end > start && |
| 811 | state->tree) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 812 | node = &state->rb_node; |
| 813 | goto hit_next; |
| 814 | } |
| 815 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 816 | /* |
| 817 | * this search will find all the extents that end after |
| 818 | * our range starts. |
| 819 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 820 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 821 | if (!node) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 822 | prealloc = alloc_extent_state_atomic(prealloc); |
| 823 | BUG_ON(!prealloc); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 824 | err = insert_state(tree, prealloc, start, end, &bits); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 825 | if (err) |
| 826 | extent_io_tree_panic(tree, err); |
| 827 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 828 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 829 | goto out; |
| 830 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 831 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 40431d6 | 2009-08-05 12:57:59 -0400 | [diff] [blame] | 832 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 833 | last_start = state->start; |
| 834 | last_end = state->end; |
| 835 | |
| 836 | /* |
| 837 | * | ---- desired range ---- | |
| 838 | * | state | |
| 839 | * |
| 840 | * Just lock what we found and keep going |
| 841 | */ |
| 842 | if (state->start == start && state->end <= end) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 843 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 844 | *failed_start = state->start; |
| 845 | err = -EEXIST; |
| 846 | goto out; |
| 847 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 848 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 849 | set_state_bits(tree, state, &bits); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 850 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 851 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 852 | if (last_end == (u64)-1) |
| 853 | goto out; |
| 854 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 855 | state = next_state(state); |
| 856 | if (start < end && state && state->start == start && |
| 857 | !need_resched()) |
| 858 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 859 | goto search_again; |
| 860 | } |
| 861 | |
| 862 | /* |
| 863 | * | ---- desired range ---- | |
| 864 | * | state | |
| 865 | * or |
| 866 | * | ------------- state -------------- | |
| 867 | * |
| 868 | * We need to split the extent we found, and may flip bits on |
| 869 | * second half. |
| 870 | * |
| 871 | * If the extent we found extends past our |
| 872 | * range, we just split and search again. It'll get split |
| 873 | * again the next time though. |
| 874 | * |
| 875 | * If the extent we found is inside our range, we set the |
| 876 | * desired bit on it. |
| 877 | */ |
| 878 | if (state->start < start) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 879 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 880 | *failed_start = start; |
| 881 | err = -EEXIST; |
| 882 | goto out; |
| 883 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 884 | |
| 885 | prealloc = alloc_extent_state_atomic(prealloc); |
| 886 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 887 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 888 | if (err) |
| 889 | extent_io_tree_panic(tree, err); |
| 890 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 891 | prealloc = NULL; |
| 892 | if (err) |
| 893 | goto out; |
| 894 | if (state->end <= end) { |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 895 | set_state_bits(tree, state, &bits); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 896 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 897 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 898 | if (last_end == (u64)-1) |
| 899 | goto out; |
| 900 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 901 | state = next_state(state); |
| 902 | if (start < end && state && state->start == start && |
| 903 | !need_resched()) |
| 904 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 905 | } |
| 906 | goto search_again; |
| 907 | } |
| 908 | /* |
| 909 | * | ---- desired range ---- | |
| 910 | * | state | or | state | |
| 911 | * |
| 912 | * There's a hole, we need to insert something in it and |
| 913 | * ignore the extent we found. |
| 914 | */ |
| 915 | if (state->start > start) { |
| 916 | u64 this_end; |
| 917 | if (end < last_start) |
| 918 | this_end = end; |
| 919 | else |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 920 | this_end = last_start - 1; |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 921 | |
| 922 | prealloc = alloc_extent_state_atomic(prealloc); |
| 923 | BUG_ON(!prealloc); |
Xiao Guangrong | c7f895a | 2011-04-20 06:45:49 +0000 | [diff] [blame] | 924 | |
| 925 | /* |
| 926 | * Avoid to free 'prealloc' if it can be merged with |
| 927 | * the later extent. |
| 928 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 929 | err = insert_state(tree, prealloc, start, this_end, |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 930 | &bits); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 931 | if (err) |
| 932 | extent_io_tree_panic(tree, err); |
| 933 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 934 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 935 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 936 | start = this_end + 1; |
| 937 | goto search_again; |
| 938 | } |
| 939 | /* |
| 940 | * | ---- desired range ---- | |
| 941 | * | state | |
| 942 | * We need to split the extent, and set the bit |
| 943 | * on the first half |
| 944 | */ |
| 945 | if (state->start <= end && state->end > end) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 946 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 947 | *failed_start = start; |
| 948 | err = -EEXIST; |
| 949 | goto out; |
| 950 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 951 | |
| 952 | prealloc = alloc_extent_state_atomic(prealloc); |
| 953 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 954 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 955 | if (err) |
| 956 | extent_io_tree_panic(tree, err); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 957 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 958 | set_state_bits(tree, prealloc, &bits); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 959 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 960 | merge_state(tree, prealloc); |
| 961 | prealloc = NULL; |
| 962 | goto out; |
| 963 | } |
| 964 | |
| 965 | goto search_again; |
| 966 | |
| 967 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 968 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 969 | if (prealloc) |
| 970 | free_extent_state(prealloc); |
| 971 | |
| 972 | return err; |
| 973 | |
| 974 | search_again: |
| 975 | if (start > end) |
| 976 | goto out; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 977 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 978 | if (mask & __GFP_WAIT) |
| 979 | cond_resched(); |
| 980 | goto again; |
| 981 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 982 | |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 983 | int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 984 | unsigned long bits, u64 * failed_start, |
| 985 | struct extent_state **cached_state, gfp_t mask) |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 986 | { |
| 987 | return __set_extent_bit(tree, start, end, bits, 0, failed_start, |
| 988 | cached_state, mask); |
| 989 | } |
| 990 | |
| 991 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 992 | /** |
Liu Bo | 10983f2 | 2012-07-11 15:26:19 +0800 | [diff] [blame] | 993 | * convert_extent_bit - convert all bits in a given range from one bit to |
| 994 | * another |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 995 | * @tree: the io tree to search |
| 996 | * @start: the start offset in bytes |
| 997 | * @end: the end offset in bytes (inclusive) |
| 998 | * @bits: the bits to set in this range |
| 999 | * @clear_bits: the bits to clear in this range |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1000 | * @cached_state: state that we're going to cache |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1001 | * @mask: the allocation mask |
| 1002 | * |
| 1003 | * This will go through and set bits for the given range. If any states exist |
| 1004 | * already in this range they are set with the given bit and cleared of the |
| 1005 | * clear_bits. This is only meant to be used by things that are mergeable, ie |
| 1006 | * converting from say DELALLOC to DIRTY. This is not meant to be used with |
| 1007 | * boundary bits like LOCK. |
| 1008 | */ |
| 1009 | int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1010 | unsigned long bits, unsigned long clear_bits, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1011 | struct extent_state **cached_state, gfp_t mask) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1012 | { |
| 1013 | struct extent_state *state; |
| 1014 | struct extent_state *prealloc = NULL; |
| 1015 | struct rb_node *node; |
| 1016 | int err = 0; |
| 1017 | u64 last_start; |
| 1018 | u64 last_end; |
| 1019 | |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 1020 | btrfs_debug_check_extent_io_range(tree->mapping->host, start, end); |
| 1021 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1022 | again: |
| 1023 | if (!prealloc && (mask & __GFP_WAIT)) { |
| 1024 | prealloc = alloc_extent_state(mask); |
| 1025 | if (!prealloc) |
| 1026 | return -ENOMEM; |
| 1027 | } |
| 1028 | |
| 1029 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1030 | if (cached_state && *cached_state) { |
| 1031 | state = *cached_state; |
| 1032 | if (state->start <= start && state->end > start && |
| 1033 | state->tree) { |
| 1034 | node = &state->rb_node; |
| 1035 | goto hit_next; |
| 1036 | } |
| 1037 | } |
| 1038 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1039 | /* |
| 1040 | * this search will find all the extents that end after |
| 1041 | * our range starts. |
| 1042 | */ |
| 1043 | node = tree_search(tree, start); |
| 1044 | if (!node) { |
| 1045 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1046 | if (!prealloc) { |
| 1047 | err = -ENOMEM; |
| 1048 | goto out; |
| 1049 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1050 | err = insert_state(tree, prealloc, start, end, &bits); |
| 1051 | prealloc = NULL; |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1052 | if (err) |
| 1053 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1054 | goto out; |
| 1055 | } |
| 1056 | state = rb_entry(node, struct extent_state, rb_node); |
| 1057 | hit_next: |
| 1058 | last_start = state->start; |
| 1059 | last_end = state->end; |
| 1060 | |
| 1061 | /* |
| 1062 | * | ---- desired range ---- | |
| 1063 | * | state | |
| 1064 | * |
| 1065 | * Just lock what we found and keep going |
| 1066 | */ |
| 1067 | if (state->start == start && state->end <= end) { |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1068 | set_state_bits(tree, state, &bits); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1069 | cache_state(state, cached_state); |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1070 | state = clear_state_bit(tree, state, &clear_bits, 0); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1071 | if (last_end == (u64)-1) |
| 1072 | goto out; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1073 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1074 | if (start < end && state && state->start == start && |
| 1075 | !need_resched()) |
| 1076 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1077 | goto search_again; |
| 1078 | } |
| 1079 | |
| 1080 | /* |
| 1081 | * | ---- desired range ---- | |
| 1082 | * | state | |
| 1083 | * or |
| 1084 | * | ------------- state -------------- | |
| 1085 | * |
| 1086 | * We need to split the extent we found, and may flip bits on |
| 1087 | * second half. |
| 1088 | * |
| 1089 | * If the extent we found extends past our |
| 1090 | * range, we just split and search again. It'll get split |
| 1091 | * again the next time though. |
| 1092 | * |
| 1093 | * If the extent we found is inside our range, we set the |
| 1094 | * desired bit on it. |
| 1095 | */ |
| 1096 | if (state->start < start) { |
| 1097 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1098 | if (!prealloc) { |
| 1099 | err = -ENOMEM; |
| 1100 | goto out; |
| 1101 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1102 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1103 | if (err) |
| 1104 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1105 | prealloc = NULL; |
| 1106 | if (err) |
| 1107 | goto out; |
| 1108 | if (state->end <= end) { |
| 1109 | set_state_bits(tree, state, &bits); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1110 | cache_state(state, cached_state); |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1111 | state = clear_state_bit(tree, state, &clear_bits, 0); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1112 | if (last_end == (u64)-1) |
| 1113 | goto out; |
| 1114 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1115 | if (start < end && state && state->start == start && |
| 1116 | !need_resched()) |
| 1117 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1118 | } |
| 1119 | goto search_again; |
| 1120 | } |
| 1121 | /* |
| 1122 | * | ---- desired range ---- | |
| 1123 | * | state | or | state | |
| 1124 | * |
| 1125 | * There's a hole, we need to insert something in it and |
| 1126 | * ignore the extent we found. |
| 1127 | */ |
| 1128 | if (state->start > start) { |
| 1129 | u64 this_end; |
| 1130 | if (end < last_start) |
| 1131 | this_end = end; |
| 1132 | else |
| 1133 | this_end = last_start - 1; |
| 1134 | |
| 1135 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1136 | if (!prealloc) { |
| 1137 | err = -ENOMEM; |
| 1138 | goto out; |
| 1139 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1140 | |
| 1141 | /* |
| 1142 | * Avoid to free 'prealloc' if it can be merged with |
| 1143 | * the later extent. |
| 1144 | */ |
| 1145 | err = insert_state(tree, prealloc, start, this_end, |
| 1146 | &bits); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1147 | if (err) |
| 1148 | extent_io_tree_panic(tree, err); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1149 | cache_state(prealloc, cached_state); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1150 | prealloc = NULL; |
| 1151 | start = this_end + 1; |
| 1152 | goto search_again; |
| 1153 | } |
| 1154 | /* |
| 1155 | * | ---- desired range ---- | |
| 1156 | * | state | |
| 1157 | * We need to split the extent, and set the bit |
| 1158 | * on the first half |
| 1159 | */ |
| 1160 | if (state->start <= end && state->end > end) { |
| 1161 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1162 | if (!prealloc) { |
| 1163 | err = -ENOMEM; |
| 1164 | goto out; |
| 1165 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1166 | |
| 1167 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1168 | if (err) |
| 1169 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1170 | |
| 1171 | set_state_bits(tree, prealloc, &bits); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1172 | cache_state(prealloc, cached_state); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1173 | clear_state_bit(tree, prealloc, &clear_bits, 0); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1174 | prealloc = NULL; |
| 1175 | goto out; |
| 1176 | } |
| 1177 | |
| 1178 | goto search_again; |
| 1179 | |
| 1180 | out: |
| 1181 | spin_unlock(&tree->lock); |
| 1182 | if (prealloc) |
| 1183 | free_extent_state(prealloc); |
| 1184 | |
| 1185 | return err; |
| 1186 | |
| 1187 | search_again: |
| 1188 | if (start > end) |
| 1189 | goto out; |
| 1190 | spin_unlock(&tree->lock); |
| 1191 | if (mask & __GFP_WAIT) |
| 1192 | cond_resched(); |
| 1193 | goto again; |
| 1194 | } |
| 1195 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1196 | /* wrappers around set/clear extent bit */ |
| 1197 | int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, |
| 1198 | gfp_t mask) |
| 1199 | { |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1200 | return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL, |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1201 | NULL, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1202 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1203 | |
| 1204 | int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1205 | unsigned long bits, gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1206 | { |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1207 | return set_extent_bit(tree, start, end, bits, NULL, |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1208 | NULL, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1209 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1210 | |
| 1211 | int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1212 | unsigned long bits, gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1213 | { |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1214 | return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1215 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1216 | |
| 1217 | int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1218 | struct extent_state **cached_state, gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1219 | { |
| 1220 | return set_extent_bit(tree, start, end, |
Liu Bo | fee187d | 2011-09-29 15:55:28 +0800 | [diff] [blame] | 1221 | EXTENT_DELALLOC | EXTENT_UPTODATE, |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1222 | NULL, cached_state, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1223 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1224 | |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 1225 | int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end, |
| 1226 | struct extent_state **cached_state, gfp_t mask) |
| 1227 | { |
| 1228 | return set_extent_bit(tree, start, end, |
| 1229 | EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG, |
| 1230 | NULL, cached_state, mask); |
| 1231 | } |
| 1232 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1233 | int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, |
| 1234 | gfp_t mask) |
| 1235 | { |
| 1236 | return clear_extent_bit(tree, start, end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1237 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1238 | EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1239 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1240 | |
| 1241 | int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end, |
| 1242 | gfp_t mask) |
| 1243 | { |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1244 | return set_extent_bit(tree, start, end, EXTENT_NEW, NULL, |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1245 | NULL, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1246 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1247 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1248 | int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 1249 | struct extent_state **cached_state, gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1250 | { |
Liu Bo | 6b67a32 | 2013-03-28 08:30:28 +0000 | [diff] [blame] | 1251 | return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL, |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1252 | cached_state, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1253 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1254 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1255 | int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end, |
| 1256 | struct extent_state **cached_state, gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1257 | { |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1258 | return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1259 | cached_state, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1260 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1261 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1262 | /* |
| 1263 | * either insert or lock state struct between start and end use mask to tell |
| 1264 | * us if waiting is desired. |
| 1265 | */ |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1266 | int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1267 | unsigned long bits, struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1268 | { |
| 1269 | int err; |
| 1270 | u64 failed_start; |
| 1271 | while (1) { |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1272 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits, |
| 1273 | EXTENT_LOCKED, &failed_start, |
| 1274 | cached_state, GFP_NOFS); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1275 | if (err == -EEXIST) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1276 | wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED); |
| 1277 | start = failed_start; |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1278 | } else |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1279 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1280 | WARN_ON(start > end); |
| 1281 | } |
| 1282 | return err; |
| 1283 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1284 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1285 | int lock_extent(struct extent_io_tree *tree, u64 start, u64 end) |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1286 | { |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1287 | return lock_extent_bits(tree, start, end, 0, NULL); |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1288 | } |
| 1289 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1290 | 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] | 1291 | { |
| 1292 | int err; |
| 1293 | u64 failed_start; |
| 1294 | |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1295 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED, |
| 1296 | &failed_start, NULL, GFP_NOFS); |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1297 | if (err == -EEXIST) { |
| 1298 | if (failed_start > start) |
| 1299 | clear_extent_bit(tree, start, failed_start - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1300 | EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1301 | return 0; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1302 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1303 | return 1; |
| 1304 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1305 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1306 | int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end, |
| 1307 | struct extent_state **cached, gfp_t mask) |
| 1308 | { |
| 1309 | return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached, |
| 1310 | mask); |
| 1311 | } |
| 1312 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1313 | int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1314 | { |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1315 | return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1316 | GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1317 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1318 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1319 | int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end) |
| 1320 | { |
| 1321 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1322 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1323 | struct page *page; |
| 1324 | |
| 1325 | while (index <= end_index) { |
| 1326 | page = find_get_page(inode->i_mapping, index); |
| 1327 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
| 1328 | clear_page_dirty_for_io(page); |
| 1329 | page_cache_release(page); |
| 1330 | index++; |
| 1331 | } |
| 1332 | return 0; |
| 1333 | } |
| 1334 | |
| 1335 | int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end) |
| 1336 | { |
| 1337 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1338 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1339 | struct page *page; |
| 1340 | |
| 1341 | while (index <= end_index) { |
| 1342 | page = find_get_page(inode->i_mapping, index); |
| 1343 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
| 1344 | account_page_redirty(page); |
| 1345 | __set_page_dirty_nobuffers(page); |
| 1346 | page_cache_release(page); |
| 1347 | index++; |
| 1348 | } |
| 1349 | return 0; |
| 1350 | } |
| 1351 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1352 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1353 | * helper function to set both pages and extents in the tree writeback |
| 1354 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1355 | static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1356 | { |
| 1357 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1358 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1359 | struct page *page; |
| 1360 | |
| 1361 | while (index <= end_index) { |
| 1362 | page = find_get_page(tree->mapping, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1363 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1364 | set_page_writeback(page); |
| 1365 | page_cache_release(page); |
| 1366 | index++; |
| 1367 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1368 | return 0; |
| 1369 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1370 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1371 | /* find the first state struct with 'bits' set after 'start', and |
| 1372 | * return it. tree->lock must be held. NULL will returned if |
| 1373 | * nothing was found after 'start' |
| 1374 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1375 | static struct extent_state * |
| 1376 | find_first_extent_bit_state(struct extent_io_tree *tree, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1377 | u64 start, unsigned long bits) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1378 | { |
| 1379 | struct rb_node *node; |
| 1380 | struct extent_state *state; |
| 1381 | |
| 1382 | /* |
| 1383 | * this search will find all the extents that end after |
| 1384 | * our range starts. |
| 1385 | */ |
| 1386 | node = tree_search(tree, start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1387 | if (!node) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1388 | goto out; |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1389 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1390 | while (1) { |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1391 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1392 | if (state->end >= start && (state->state & bits)) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1393 | return state; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1394 | |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1395 | node = rb_next(node); |
| 1396 | if (!node) |
| 1397 | break; |
| 1398 | } |
| 1399 | out: |
| 1400 | return NULL; |
| 1401 | } |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1402 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1403 | /* |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1404 | * find the first offset in the io tree with 'bits' set. zero is |
| 1405 | * returned if we find something, and *start_ret and *end_ret are |
| 1406 | * set to reflect the state struct that was found. |
| 1407 | * |
Wang Sheng-Hui | 477d7ea | 2012-04-06 14:35:47 +0800 | [diff] [blame] | 1408 | * If nothing was found, 1 is returned. If found something, return 0. |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1409 | */ |
| 1410 | int find_first_extent_bit(struct extent_io_tree *tree, u64 start, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1411 | u64 *start_ret, u64 *end_ret, unsigned long bits, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1412 | struct extent_state **cached_state) |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1413 | { |
| 1414 | struct extent_state *state; |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1415 | struct rb_node *n; |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1416 | int ret = 1; |
| 1417 | |
| 1418 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1419 | if (cached_state && *cached_state) { |
| 1420 | state = *cached_state; |
| 1421 | if (state->end == start - 1 && state->tree) { |
| 1422 | n = rb_next(&state->rb_node); |
| 1423 | while (n) { |
| 1424 | state = rb_entry(n, struct extent_state, |
| 1425 | rb_node); |
| 1426 | if (state->state & bits) |
| 1427 | goto got_it; |
| 1428 | n = rb_next(n); |
| 1429 | } |
| 1430 | free_extent_state(*cached_state); |
| 1431 | *cached_state = NULL; |
| 1432 | goto out; |
| 1433 | } |
| 1434 | free_extent_state(*cached_state); |
| 1435 | *cached_state = NULL; |
| 1436 | } |
| 1437 | |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1438 | state = find_first_extent_bit_state(tree, start, bits); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1439 | got_it: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1440 | if (state) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1441 | cache_state(state, cached_state); |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1442 | *start_ret = state->start; |
| 1443 | *end_ret = state->end; |
| 1444 | ret = 0; |
| 1445 | } |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1446 | out: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1447 | spin_unlock(&tree->lock); |
| 1448 | return ret; |
| 1449 | } |
| 1450 | |
| 1451 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1452 | * find a contiguous range of bytes in the file marked as delalloc, not |
| 1453 | * more than 'max_bytes'. start and end are used to return the range, |
| 1454 | * |
| 1455 | * 1 is returned if we find something, 0 if nothing was in the tree |
| 1456 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1457 | static noinline u64 find_delalloc_range(struct extent_io_tree *tree, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1458 | u64 *start, u64 *end, u64 max_bytes, |
| 1459 | struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1460 | { |
| 1461 | struct rb_node *node; |
| 1462 | struct extent_state *state; |
| 1463 | u64 cur_start = *start; |
| 1464 | u64 found = 0; |
| 1465 | u64 total_bytes = 0; |
| 1466 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1467 | spin_lock(&tree->lock); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1468 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1469 | /* |
| 1470 | * this search will find all the extents that end after |
| 1471 | * our range starts. |
| 1472 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1473 | node = tree_search(tree, cur_start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1474 | if (!node) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1475 | if (!found) |
| 1476 | *end = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1477 | goto out; |
| 1478 | } |
| 1479 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1480 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1481 | state = rb_entry(node, struct extent_state, rb_node); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1482 | if (found && (state->start != cur_start || |
| 1483 | (state->state & EXTENT_BOUNDARY))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1484 | goto out; |
| 1485 | } |
| 1486 | if (!(state->state & EXTENT_DELALLOC)) { |
| 1487 | if (!found) |
| 1488 | *end = state->end; |
| 1489 | goto out; |
| 1490 | } |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1491 | if (!found) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1492 | *start = state->start; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1493 | *cached_state = state; |
| 1494 | atomic_inc(&state->refs); |
| 1495 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1496 | found++; |
| 1497 | *end = state->end; |
| 1498 | cur_start = state->end + 1; |
| 1499 | node = rb_next(node); |
| 1500 | if (!node) |
| 1501 | break; |
| 1502 | total_bytes += state->end - state->start + 1; |
| 1503 | if (total_bytes >= max_bytes) |
| 1504 | break; |
| 1505 | } |
| 1506 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1507 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1508 | return found; |
| 1509 | } |
| 1510 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1511 | static noinline void __unlock_for_delalloc(struct inode *inode, |
| 1512 | struct page *locked_page, |
| 1513 | u64 start, u64 end) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1514 | { |
| 1515 | int ret; |
| 1516 | struct page *pages[16]; |
| 1517 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1518 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1519 | unsigned long nr_pages = end_index - index + 1; |
| 1520 | int i; |
| 1521 | |
| 1522 | if (index == locked_page->index && end_index == index) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1523 | return; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1524 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1525 | while (nr_pages > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1526 | ret = find_get_pages_contig(inode->i_mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1527 | min_t(unsigned long, nr_pages, |
| 1528 | ARRAY_SIZE(pages)), pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1529 | for (i = 0; i < ret; i++) { |
| 1530 | if (pages[i] != locked_page) |
| 1531 | unlock_page(pages[i]); |
| 1532 | page_cache_release(pages[i]); |
| 1533 | } |
| 1534 | nr_pages -= ret; |
| 1535 | index += ret; |
| 1536 | cond_resched(); |
| 1537 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | static noinline int lock_delalloc_pages(struct inode *inode, |
| 1541 | struct page *locked_page, |
| 1542 | u64 delalloc_start, |
| 1543 | u64 delalloc_end) |
| 1544 | { |
| 1545 | unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT; |
| 1546 | unsigned long start_index = index; |
| 1547 | unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT; |
| 1548 | unsigned long pages_locked = 0; |
| 1549 | struct page *pages[16]; |
| 1550 | unsigned long nrpages; |
| 1551 | int ret; |
| 1552 | int i; |
| 1553 | |
| 1554 | /* the caller is responsible for locking the start index */ |
| 1555 | if (index == locked_page->index && index == end_index) |
| 1556 | return 0; |
| 1557 | |
| 1558 | /* skip the page at the start index */ |
| 1559 | nrpages = end_index - index + 1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1560 | while (nrpages > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1561 | ret = find_get_pages_contig(inode->i_mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1562 | min_t(unsigned long, |
| 1563 | nrpages, ARRAY_SIZE(pages)), pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1564 | if (ret == 0) { |
| 1565 | ret = -EAGAIN; |
| 1566 | goto done; |
| 1567 | } |
| 1568 | /* now we have an array of pages, lock them all */ |
| 1569 | for (i = 0; i < ret; i++) { |
| 1570 | /* |
| 1571 | * the caller is taking responsibility for |
| 1572 | * locked_page |
| 1573 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1574 | if (pages[i] != locked_page) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1575 | lock_page(pages[i]); |
Chris Mason | f2b1c41 | 2008-11-10 07:31:30 -0500 | [diff] [blame] | 1576 | if (!PageDirty(pages[i]) || |
| 1577 | pages[i]->mapping != inode->i_mapping) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1578 | ret = -EAGAIN; |
| 1579 | unlock_page(pages[i]); |
| 1580 | page_cache_release(pages[i]); |
| 1581 | goto done; |
| 1582 | } |
| 1583 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1584 | page_cache_release(pages[i]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1585 | pages_locked++; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1586 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1587 | nrpages -= ret; |
| 1588 | index += ret; |
| 1589 | cond_resched(); |
| 1590 | } |
| 1591 | ret = 0; |
| 1592 | done: |
| 1593 | if (ret && pages_locked) { |
| 1594 | __unlock_for_delalloc(inode, locked_page, |
| 1595 | delalloc_start, |
| 1596 | ((u64)(start_index + pages_locked - 1)) << |
| 1597 | PAGE_CACHE_SHIFT); |
| 1598 | } |
| 1599 | return ret; |
| 1600 | } |
| 1601 | |
| 1602 | /* |
| 1603 | * find a contiguous range of bytes in the file marked as delalloc, not |
| 1604 | * more than 'max_bytes'. start and end are used to return the range, |
| 1605 | * |
| 1606 | * 1 is returned if we find something, 0 if nothing was in the tree |
| 1607 | */ |
| 1608 | static noinline u64 find_lock_delalloc_range(struct inode *inode, |
| 1609 | struct extent_io_tree *tree, |
| 1610 | struct page *locked_page, |
| 1611 | u64 *start, u64 *end, |
| 1612 | u64 max_bytes) |
| 1613 | { |
| 1614 | u64 delalloc_start; |
| 1615 | u64 delalloc_end; |
| 1616 | u64 found; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1617 | struct extent_state *cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1618 | int ret; |
| 1619 | int loops = 0; |
| 1620 | |
| 1621 | again: |
| 1622 | /* step one, find a bunch of delalloc bytes starting at start */ |
| 1623 | delalloc_start = *start; |
| 1624 | delalloc_end = 0; |
| 1625 | found = find_delalloc_range(tree, &delalloc_start, &delalloc_end, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1626 | max_bytes, &cached_state); |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1627 | if (!found || delalloc_end <= *start) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1628 | *start = delalloc_start; |
| 1629 | *end = delalloc_end; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1630 | free_extent_state(cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1631 | return found; |
| 1632 | } |
| 1633 | |
| 1634 | /* |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1635 | * start comes from the offset of locked_page. We have to lock |
| 1636 | * pages in order, so we can't process delalloc bytes before |
| 1637 | * locked_page |
| 1638 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1639 | if (delalloc_start < *start) |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1640 | delalloc_start = *start; |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1641 | |
| 1642 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1643 | * make sure to limit the number of pages we try to lock down |
| 1644 | * if we're looping. |
| 1645 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1646 | if (delalloc_end + 1 - delalloc_start > max_bytes && loops) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1647 | delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1648 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1649 | /* step two, lock all the pages after the page that has start */ |
| 1650 | ret = lock_delalloc_pages(inode, locked_page, |
| 1651 | delalloc_start, delalloc_end); |
| 1652 | if (ret == -EAGAIN) { |
| 1653 | /* some of the pages are gone, lets avoid looping by |
| 1654 | * shortening the size of the delalloc range we're searching |
| 1655 | */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1656 | free_extent_state(cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1657 | if (!loops) { |
| 1658 | unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1); |
| 1659 | max_bytes = PAGE_CACHE_SIZE - offset; |
| 1660 | loops = 1; |
| 1661 | goto again; |
| 1662 | } else { |
| 1663 | found = 0; |
| 1664 | goto out_failed; |
| 1665 | } |
| 1666 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1667 | BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1668 | |
| 1669 | /* step three, lock the state bits for the whole range */ |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1670 | lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1671 | |
| 1672 | /* then test to make sure it is all still delalloc */ |
| 1673 | ret = test_range_bit(tree, delalloc_start, delalloc_end, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1674 | EXTENT_DELALLOC, 1, cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1675 | if (!ret) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1676 | unlock_extent_cached(tree, delalloc_start, delalloc_end, |
| 1677 | &cached_state, GFP_NOFS); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1678 | __unlock_for_delalloc(inode, locked_page, |
| 1679 | delalloc_start, delalloc_end); |
| 1680 | cond_resched(); |
| 1681 | goto again; |
| 1682 | } |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1683 | free_extent_state(cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1684 | *start = delalloc_start; |
| 1685 | *end = delalloc_end; |
| 1686 | out_failed: |
| 1687 | return found; |
| 1688 | } |
| 1689 | |
| 1690 | int extent_clear_unlock_delalloc(struct inode *inode, |
| 1691 | struct extent_io_tree *tree, |
| 1692 | u64 start, u64 end, struct page *locked_page, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1693 | unsigned long op) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1694 | { |
| 1695 | int ret; |
| 1696 | struct page *pages[16]; |
| 1697 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1698 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1699 | unsigned long nr_pages = end_index - index + 1; |
| 1700 | int i; |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1701 | unsigned long clear_bits = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1702 | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1703 | if (op & EXTENT_CLEAR_UNLOCK) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1704 | clear_bits |= EXTENT_LOCKED; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1705 | if (op & EXTENT_CLEAR_DIRTY) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1706 | clear_bits |= EXTENT_DIRTY; |
| 1707 | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1708 | if (op & EXTENT_CLEAR_DELALLOC) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1709 | clear_bits |= EXTENT_DELALLOC; |
| 1710 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1711 | clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS); |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1712 | if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
| 1713 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK | |
| 1714 | EXTENT_SET_PRIVATE2))) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1715 | return 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1716 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1717 | while (nr_pages > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1718 | ret = find_get_pages_contig(inode->i_mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1719 | min_t(unsigned long, |
| 1720 | nr_pages, ARRAY_SIZE(pages)), pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1721 | for (i = 0; i < ret; i++) { |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1722 | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1723 | if (op & EXTENT_SET_PRIVATE2) |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1724 | SetPagePrivate2(pages[i]); |
| 1725 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1726 | if (pages[i] == locked_page) { |
| 1727 | page_cache_release(pages[i]); |
| 1728 | continue; |
| 1729 | } |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1730 | if (op & EXTENT_CLEAR_DIRTY) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1731 | clear_page_dirty_for_io(pages[i]); |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1732 | if (op & EXTENT_SET_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1733 | set_page_writeback(pages[i]); |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1734 | if (op & EXTENT_END_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1735 | end_page_writeback(pages[i]); |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1736 | if (op & EXTENT_CLEAR_UNLOCK_PAGE) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1737 | unlock_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1738 | page_cache_release(pages[i]); |
| 1739 | } |
| 1740 | nr_pages -= ret; |
| 1741 | index += ret; |
| 1742 | cond_resched(); |
| 1743 | } |
| 1744 | return 0; |
| 1745 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1746 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1747 | /* |
| 1748 | * count the number of bytes in the tree that have a given bit(s) |
| 1749 | * set. This can be fairly slow, except for EXTENT_DIRTY which is |
| 1750 | * cached. The total number found is returned. |
| 1751 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1752 | u64 count_range_bits(struct extent_io_tree *tree, |
| 1753 | u64 *start, u64 search_end, u64 max_bytes, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1754 | unsigned long bits, int contig) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1755 | { |
| 1756 | struct rb_node *node; |
| 1757 | struct extent_state *state; |
| 1758 | u64 cur_start = *start; |
| 1759 | u64 total_bytes = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1760 | u64 last = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1761 | int found = 0; |
| 1762 | |
| 1763 | if (search_end <= cur_start) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1764 | WARN_ON(1); |
| 1765 | return 0; |
| 1766 | } |
| 1767 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1768 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1769 | if (cur_start == 0 && bits == EXTENT_DIRTY) { |
| 1770 | total_bytes = tree->dirty_bytes; |
| 1771 | goto out; |
| 1772 | } |
| 1773 | /* |
| 1774 | * this search will find all the extents that end after |
| 1775 | * our range starts. |
| 1776 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1777 | node = tree_search(tree, cur_start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1778 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1779 | goto out; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1780 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1781 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1782 | state = rb_entry(node, struct extent_state, rb_node); |
| 1783 | if (state->start > search_end) |
| 1784 | break; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1785 | if (contig && found && state->start > last + 1) |
| 1786 | break; |
| 1787 | if (state->end >= cur_start && (state->state & bits) == bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1788 | total_bytes += min(search_end, state->end) + 1 - |
| 1789 | max(cur_start, state->start); |
| 1790 | if (total_bytes >= max_bytes) |
| 1791 | break; |
| 1792 | if (!found) { |
Josef Bacik | af60bed | 2011-05-04 11:11:17 -0400 | [diff] [blame] | 1793 | *start = max(cur_start, state->start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1794 | found = 1; |
| 1795 | } |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1796 | last = state->end; |
| 1797 | } else if (contig && found) { |
| 1798 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1799 | } |
| 1800 | node = rb_next(node); |
| 1801 | if (!node) |
| 1802 | break; |
| 1803 | } |
| 1804 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1805 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1806 | return total_bytes; |
| 1807 | } |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1808 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1809 | /* |
| 1810 | * set the private field for a given byte offset in the tree. If there isn't |
| 1811 | * an extent_state there already, this does nothing. |
| 1812 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1813 | int set_state_private(struct extent_io_tree *tree, u64 start, u64 private) |
| 1814 | { |
| 1815 | struct rb_node *node; |
| 1816 | struct extent_state *state; |
| 1817 | int ret = 0; |
| 1818 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1819 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1820 | /* |
| 1821 | * this search will find all the extents that end after |
| 1822 | * our range starts. |
| 1823 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1824 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1825 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1826 | ret = -ENOENT; |
| 1827 | goto out; |
| 1828 | } |
| 1829 | state = rb_entry(node, struct extent_state, rb_node); |
| 1830 | if (state->start != start) { |
| 1831 | ret = -ENOENT; |
| 1832 | goto out; |
| 1833 | } |
| 1834 | state->private = private; |
| 1835 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1836 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1837 | return ret; |
| 1838 | } |
| 1839 | |
| 1840 | int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private) |
| 1841 | { |
| 1842 | struct rb_node *node; |
| 1843 | struct extent_state *state; |
| 1844 | int ret = 0; |
| 1845 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1846 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1847 | /* |
| 1848 | * this search will find all the extents that end after |
| 1849 | * our range starts. |
| 1850 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1851 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1852 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1853 | ret = -ENOENT; |
| 1854 | goto out; |
| 1855 | } |
| 1856 | state = rb_entry(node, struct extent_state, rb_node); |
| 1857 | if (state->start != start) { |
| 1858 | ret = -ENOENT; |
| 1859 | goto out; |
| 1860 | } |
| 1861 | *private = state->private; |
| 1862 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1863 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1864 | return ret; |
| 1865 | } |
| 1866 | |
| 1867 | /* |
| 1868 | * searches a range in the state tree for a given mask. |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1869 | * 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] | 1870 | * has the bits set. Otherwise, 1 is returned if any bit in the |
| 1871 | * range is found set. |
| 1872 | */ |
| 1873 | int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1874 | unsigned long bits, int filled, struct extent_state *cached) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1875 | { |
| 1876 | struct extent_state *state = NULL; |
| 1877 | struct rb_node *node; |
| 1878 | int bitset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1879 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1880 | spin_lock(&tree->lock); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 1881 | if (cached && cached->tree && cached->start <= start && |
| 1882 | cached->end > start) |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1883 | node = &cached->rb_node; |
| 1884 | else |
| 1885 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1886 | while (node && start <= end) { |
| 1887 | state = rb_entry(node, struct extent_state, rb_node); |
| 1888 | |
| 1889 | if (filled && state->start > start) { |
| 1890 | bitset = 0; |
| 1891 | break; |
| 1892 | } |
| 1893 | |
| 1894 | if (state->start > end) |
| 1895 | break; |
| 1896 | |
| 1897 | if (state->state & bits) { |
| 1898 | bitset = 1; |
| 1899 | if (!filled) |
| 1900 | break; |
| 1901 | } else if (filled) { |
| 1902 | bitset = 0; |
| 1903 | break; |
| 1904 | } |
Chris Mason | 46562cec | 2009-09-23 20:23:16 -0400 | [diff] [blame] | 1905 | |
| 1906 | if (state->end == (u64)-1) |
| 1907 | break; |
| 1908 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1909 | start = state->end + 1; |
| 1910 | if (start > end) |
| 1911 | break; |
| 1912 | node = rb_next(node); |
| 1913 | if (!node) { |
| 1914 | if (filled) |
| 1915 | bitset = 0; |
| 1916 | break; |
| 1917 | } |
| 1918 | } |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1919 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1920 | return bitset; |
| 1921 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1922 | |
| 1923 | /* |
| 1924 | * helper function to set a given page up to date if all the |
| 1925 | * extents in the tree for that page are up to date |
| 1926 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1927 | 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] | 1928 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 1929 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1930 | u64 end = start + PAGE_CACHE_SIZE - 1; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1931 | if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1932 | SetPageUptodate(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | /* |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1936 | * When IO fails, either with EIO or csum verification fails, we |
| 1937 | * try other mirrors that might have a good copy of the data. This |
| 1938 | * io_failure_record is used to record state as we go through all the |
| 1939 | * mirrors. If another mirror has good data, the page is set up to date |
| 1940 | * and things continue. If a good mirror can't be found, the original |
| 1941 | * bio end_io callback is called to indicate things have failed. |
| 1942 | */ |
| 1943 | struct io_failure_record { |
| 1944 | struct page *page; |
| 1945 | u64 start; |
| 1946 | u64 len; |
| 1947 | u64 logical; |
| 1948 | unsigned long bio_flags; |
| 1949 | int this_mirror; |
| 1950 | int failed_mirror; |
| 1951 | int in_validation; |
| 1952 | }; |
| 1953 | |
| 1954 | static int free_io_failure(struct inode *inode, struct io_failure_record *rec, |
| 1955 | int did_repair) |
| 1956 | { |
| 1957 | int ret; |
| 1958 | int err = 0; |
| 1959 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 1960 | |
| 1961 | set_state_private(failure_tree, rec->start, 0); |
| 1962 | ret = clear_extent_bits(failure_tree, rec->start, |
| 1963 | rec->start + rec->len - 1, |
| 1964 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 1965 | if (ret) |
| 1966 | err = ret; |
| 1967 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1968 | ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start, |
| 1969 | rec->start + rec->len - 1, |
| 1970 | EXTENT_DAMAGED, GFP_NOFS); |
| 1971 | if (ret && !err) |
| 1972 | err = ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1973 | |
| 1974 | kfree(rec); |
| 1975 | return err; |
| 1976 | } |
| 1977 | |
| 1978 | static void repair_io_failure_callback(struct bio *bio, int err) |
| 1979 | { |
| 1980 | complete(bio->bi_private); |
| 1981 | } |
| 1982 | |
| 1983 | /* |
| 1984 | * this bypasses the standard btrfs submit functions deliberately, as |
| 1985 | * the standard behavior is to write all copies in a raid setup. here we only |
| 1986 | * want to write the one bad copy. so we do the mapping for ourselves and issue |
| 1987 | * submit_bio directly. |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1988 | * to avoid any synchronization issues, wait for the data after writing, which |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1989 | * actually prevents the read that triggered the error from finishing. |
| 1990 | * currently, there can be no more than two copies of every data bit. thus, |
| 1991 | * exactly one rewrite is required. |
| 1992 | */ |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1993 | int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start, |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1994 | u64 length, u64 logical, struct page *page, |
| 1995 | int mirror_num) |
| 1996 | { |
| 1997 | struct bio *bio; |
| 1998 | struct btrfs_device *dev; |
| 1999 | DECLARE_COMPLETION_ONSTACK(compl); |
| 2000 | u64 map_length = 0; |
| 2001 | u64 sector; |
| 2002 | struct btrfs_bio *bbio = NULL; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2003 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2004 | int ret; |
| 2005 | |
| 2006 | BUG_ON(!mirror_num); |
| 2007 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2008 | /* we can't repair anything in raid56 yet */ |
| 2009 | if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num)) |
| 2010 | return 0; |
| 2011 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2012 | bio = btrfs_io_bio_alloc(GFP_NOFS, 1); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2013 | if (!bio) |
| 2014 | return -EIO; |
| 2015 | bio->bi_private = &compl; |
| 2016 | bio->bi_end_io = repair_io_failure_callback; |
| 2017 | bio->bi_size = 0; |
| 2018 | map_length = length; |
| 2019 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2020 | ret = btrfs_map_block(fs_info, WRITE, logical, |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2021 | &map_length, &bbio, mirror_num); |
| 2022 | if (ret) { |
| 2023 | bio_put(bio); |
| 2024 | return -EIO; |
| 2025 | } |
| 2026 | BUG_ON(mirror_num != bbio->mirror_num); |
| 2027 | sector = bbio->stripes[mirror_num-1].physical >> 9; |
| 2028 | bio->bi_sector = sector; |
| 2029 | dev = bbio->stripes[mirror_num-1].dev; |
| 2030 | kfree(bbio); |
| 2031 | if (!dev || !dev->bdev || !dev->writeable) { |
| 2032 | bio_put(bio); |
| 2033 | return -EIO; |
| 2034 | } |
| 2035 | bio->bi_bdev = dev->bdev; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2036 | bio_add_page(bio, page, length, start - page_offset(page)); |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 2037 | btrfsic_submit_bio(WRITE_SYNC, bio); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2038 | wait_for_completion(&compl); |
| 2039 | |
| 2040 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 2041 | /* try to remap that extent elsewhere? */ |
| 2042 | bio_put(bio); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2043 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2044 | return -EIO; |
| 2045 | } |
| 2046 | |
Anand Jain | d5b025d | 2012-07-02 22:05:21 -0600 | [diff] [blame] | 2047 | printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu " |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2048 | "(dev %s sector %llu)\n", page->mapping->host->i_ino, |
| 2049 | start, rcu_str_deref(dev->name), sector); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2050 | |
| 2051 | bio_put(bio); |
| 2052 | return 0; |
| 2053 | } |
| 2054 | |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2055 | int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb, |
| 2056 | int mirror_num) |
| 2057 | { |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2058 | u64 start = eb->start; |
| 2059 | unsigned long i, num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | d95603b | 2012-04-12 15:55:15 -0400 | [diff] [blame] | 2060 | int ret = 0; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2061 | |
| 2062 | for (i = 0; i < num_pages; i++) { |
| 2063 | struct page *p = extent_buffer_page(eb, i); |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2064 | ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE, |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2065 | start, p, mirror_num); |
| 2066 | if (ret) |
| 2067 | break; |
| 2068 | start += PAGE_CACHE_SIZE; |
| 2069 | } |
| 2070 | |
| 2071 | return ret; |
| 2072 | } |
| 2073 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2074 | /* |
| 2075 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 2076 | * to see if we need to process or clean up an io_failure_record |
| 2077 | */ |
| 2078 | static int clean_io_failure(u64 start, struct page *page) |
| 2079 | { |
| 2080 | u64 private; |
| 2081 | u64 private_failure; |
| 2082 | struct io_failure_record *failrec; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2083 | struct btrfs_fs_info *fs_info; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2084 | struct extent_state *state; |
| 2085 | int num_copies; |
| 2086 | int did_repair = 0; |
| 2087 | int ret; |
| 2088 | struct inode *inode = page->mapping->host; |
| 2089 | |
| 2090 | private = 0; |
| 2091 | ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
| 2092 | (u64)-1, 1, EXTENT_DIRTY, 0); |
| 2093 | if (!ret) |
| 2094 | return 0; |
| 2095 | |
| 2096 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start, |
| 2097 | &private_failure); |
| 2098 | if (ret) |
| 2099 | return 0; |
| 2100 | |
| 2101 | failrec = (struct io_failure_record *)(unsigned long) private_failure; |
| 2102 | BUG_ON(!failrec->this_mirror); |
| 2103 | |
| 2104 | if (failrec->in_validation) { |
| 2105 | /* there was no real error, just free the record */ |
| 2106 | pr_debug("clean_io_failure: freeing dummy error at %llu\n", |
| 2107 | failrec->start); |
| 2108 | did_repair = 1; |
| 2109 | goto out; |
| 2110 | } |
| 2111 | |
| 2112 | spin_lock(&BTRFS_I(inode)->io_tree.lock); |
| 2113 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 2114 | failrec->start, |
| 2115 | EXTENT_LOCKED); |
| 2116 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); |
| 2117 | |
| 2118 | if (state && state->start == failrec->start) { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2119 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2120 | num_copies = btrfs_num_copies(fs_info, failrec->logical, |
| 2121 | failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2122 | if (num_copies > 1) { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2123 | ret = repair_io_failure(fs_info, start, failrec->len, |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2124 | failrec->logical, page, |
| 2125 | failrec->failed_mirror); |
| 2126 | did_repair = !ret; |
| 2127 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2128 | ret = 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | out: |
| 2132 | if (!ret) |
| 2133 | ret = free_io_failure(inode, failrec, did_repair); |
| 2134 | |
| 2135 | return ret; |
| 2136 | } |
| 2137 | |
| 2138 | /* |
| 2139 | * this is a generic handler for readpage errors (default |
| 2140 | * readpage_io_failed_hook). if other copies exist, read those and write back |
| 2141 | * good data to the failed position. does not investigate in remapping the |
| 2142 | * failed extent elsewhere, hoping the device will be smart enough to do this as |
| 2143 | * needed |
| 2144 | */ |
| 2145 | |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame^] | 2146 | static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset, |
| 2147 | struct page *page, u64 start, u64 end, |
| 2148 | int failed_mirror) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2149 | { |
| 2150 | struct io_failure_record *failrec = NULL; |
| 2151 | u64 private; |
| 2152 | struct extent_map *em; |
| 2153 | struct inode *inode = page->mapping->host; |
| 2154 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 2155 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 2156 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 2157 | struct bio *bio; |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame^] | 2158 | struct btrfs_io_bio *btrfs_failed_bio; |
| 2159 | struct btrfs_io_bio *btrfs_bio; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2160 | int num_copies; |
| 2161 | int ret; |
| 2162 | int read_mode; |
| 2163 | u64 logical; |
| 2164 | |
| 2165 | BUG_ON(failed_bio->bi_rw & REQ_WRITE); |
| 2166 | |
| 2167 | ret = get_state_private(failure_tree, start, &private); |
| 2168 | if (ret) { |
| 2169 | failrec = kzalloc(sizeof(*failrec), GFP_NOFS); |
| 2170 | if (!failrec) |
| 2171 | return -ENOMEM; |
| 2172 | failrec->start = start; |
| 2173 | failrec->len = end - start + 1; |
| 2174 | failrec->this_mirror = 0; |
| 2175 | failrec->bio_flags = 0; |
| 2176 | failrec->in_validation = 0; |
| 2177 | |
| 2178 | read_lock(&em_tree->lock); |
| 2179 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 2180 | if (!em) { |
| 2181 | read_unlock(&em_tree->lock); |
| 2182 | kfree(failrec); |
| 2183 | return -EIO; |
| 2184 | } |
| 2185 | |
| 2186 | if (em->start > start || em->start + em->len < start) { |
| 2187 | free_extent_map(em); |
| 2188 | em = NULL; |
| 2189 | } |
| 2190 | read_unlock(&em_tree->lock); |
| 2191 | |
Tsutomu Itoh | 7a2d6a6 | 2012-10-01 03:07:15 -0600 | [diff] [blame] | 2192 | if (!em) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2193 | kfree(failrec); |
| 2194 | return -EIO; |
| 2195 | } |
| 2196 | logical = start - em->start; |
| 2197 | logical = em->block_start + logical; |
| 2198 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 2199 | logical = em->block_start; |
| 2200 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
| 2201 | extent_set_compress_type(&failrec->bio_flags, |
| 2202 | em->compress_type); |
| 2203 | } |
| 2204 | pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, " |
| 2205 | "len=%llu\n", logical, start, failrec->len); |
| 2206 | failrec->logical = logical; |
| 2207 | free_extent_map(em); |
| 2208 | |
| 2209 | /* set the bits in the private failure tree */ |
| 2210 | ret = set_extent_bits(failure_tree, start, end, |
| 2211 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 2212 | if (ret >= 0) |
| 2213 | ret = set_state_private(failure_tree, start, |
| 2214 | (u64)(unsigned long)failrec); |
| 2215 | /* set the bits in the inode's tree */ |
| 2216 | if (ret >= 0) |
| 2217 | ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED, |
| 2218 | GFP_NOFS); |
| 2219 | if (ret < 0) { |
| 2220 | kfree(failrec); |
| 2221 | return ret; |
| 2222 | } |
| 2223 | } else { |
| 2224 | failrec = (struct io_failure_record *)(unsigned long)private; |
| 2225 | pr_debug("bio_readpage_error: (found) logical=%llu, " |
| 2226 | "start=%llu, len=%llu, validation=%d\n", |
| 2227 | failrec->logical, failrec->start, failrec->len, |
| 2228 | failrec->in_validation); |
| 2229 | /* |
| 2230 | * when data can be on disk more than twice, add to failrec here |
| 2231 | * (e.g. with a list for failed_mirror) to make |
| 2232 | * clean_io_failure() clean all those errors at once. |
| 2233 | */ |
| 2234 | } |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 2235 | num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info, |
| 2236 | failrec->logical, failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2237 | if (num_copies == 1) { |
| 2238 | /* |
| 2239 | * we only have a single copy of the data, so don't bother with |
| 2240 | * all the retry and error correction code that follows. no |
| 2241 | * matter what the error is, it is very likely to persist. |
| 2242 | */ |
Miao Xie | 09a7f7a | 2013-07-25 19:22:32 +0800 | [diff] [blame] | 2243 | pr_debug("bio_readpage_error: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n", |
| 2244 | num_copies, failrec->this_mirror, failed_mirror); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2245 | free_io_failure(inode, failrec, 0); |
| 2246 | return -EIO; |
| 2247 | } |
| 2248 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2249 | /* |
| 2250 | * there are two premises: |
| 2251 | * a) deliver good data to the caller |
| 2252 | * b) correct the bad sectors on disk |
| 2253 | */ |
| 2254 | if (failed_bio->bi_vcnt > 1) { |
| 2255 | /* |
| 2256 | * to fulfill b), we need to know the exact failing sectors, as |
| 2257 | * we don't want to rewrite any more than the failed ones. thus, |
| 2258 | * we need separate read requests for the failed bio |
| 2259 | * |
| 2260 | * if the following BUG_ON triggers, our validation request got |
| 2261 | * merged. we need separate requests for our algorithm to work. |
| 2262 | */ |
| 2263 | BUG_ON(failrec->in_validation); |
| 2264 | failrec->in_validation = 1; |
| 2265 | failrec->this_mirror = failed_mirror; |
| 2266 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; |
| 2267 | } else { |
| 2268 | /* |
| 2269 | * we're ready to fulfill a) and b) alongside. get a good copy |
| 2270 | * of the failed sector and if we succeed, we have setup |
| 2271 | * everything for repair_io_failure to do the rest for us. |
| 2272 | */ |
| 2273 | if (failrec->in_validation) { |
| 2274 | BUG_ON(failrec->this_mirror != failed_mirror); |
| 2275 | failrec->in_validation = 0; |
| 2276 | failrec->this_mirror = 0; |
| 2277 | } |
| 2278 | failrec->failed_mirror = failed_mirror; |
| 2279 | failrec->this_mirror++; |
| 2280 | if (failrec->this_mirror == failed_mirror) |
| 2281 | failrec->this_mirror++; |
| 2282 | read_mode = READ_SYNC; |
| 2283 | } |
| 2284 | |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame^] | 2285 | if (failrec->this_mirror > num_copies) { |
| 2286 | pr_debug("bio_readpage_error: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n", |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2287 | num_copies, failrec->this_mirror, failed_mirror); |
| 2288 | free_io_failure(inode, failrec, 0); |
| 2289 | return -EIO; |
| 2290 | } |
| 2291 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2292 | bio = btrfs_io_bio_alloc(GFP_NOFS, 1); |
Tsutomu Itoh | e627ee7 | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 2293 | if (!bio) { |
| 2294 | free_io_failure(inode, failrec, 0); |
| 2295 | return -EIO; |
| 2296 | } |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2297 | bio->bi_end_io = failed_bio->bi_end_io; |
| 2298 | bio->bi_sector = failrec->logical >> 9; |
| 2299 | bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; |
| 2300 | bio->bi_size = 0; |
| 2301 | |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame^] | 2302 | btrfs_failed_bio = btrfs_io_bio(failed_bio); |
| 2303 | if (btrfs_failed_bio->csum) { |
| 2304 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2305 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
| 2306 | |
| 2307 | btrfs_bio = btrfs_io_bio(bio); |
| 2308 | btrfs_bio->csum = btrfs_bio->csum_inline; |
| 2309 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
| 2310 | phy_offset *= csum_size; |
| 2311 | memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + phy_offset, |
| 2312 | csum_size); |
| 2313 | } |
| 2314 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2315 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); |
| 2316 | |
| 2317 | pr_debug("bio_readpage_error: submitting new read[%#x] to " |
| 2318 | "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode, |
| 2319 | failrec->this_mirror, num_copies, failrec->in_validation); |
| 2320 | |
Tsutomu Itoh | 013bd4c | 2012-02-16 10:11:40 +0900 | [diff] [blame] | 2321 | ret = tree->ops->submit_bio_hook(inode, read_mode, bio, |
| 2322 | failrec->this_mirror, |
| 2323 | failrec->bio_flags, 0); |
| 2324 | return ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2325 | } |
| 2326 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2327 | /* lots and lots of room for performance fixes in the end_bio funcs */ |
| 2328 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2329 | int end_extent_writepage(struct page *page, int err, u64 start, u64 end) |
| 2330 | { |
| 2331 | int uptodate = (err == 0); |
| 2332 | struct extent_io_tree *tree; |
| 2333 | int ret; |
| 2334 | |
| 2335 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 2336 | |
| 2337 | if (tree->ops && tree->ops->writepage_end_io_hook) { |
| 2338 | ret = tree->ops->writepage_end_io_hook(page, start, |
| 2339 | end, NULL, uptodate); |
| 2340 | if (ret) |
| 2341 | uptodate = 0; |
| 2342 | } |
| 2343 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2344 | if (!uptodate) { |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2345 | ClearPageUptodate(page); |
| 2346 | SetPageError(page); |
| 2347 | } |
| 2348 | return 0; |
| 2349 | } |
| 2350 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2351 | /* |
| 2352 | * after a writepage IO is done, we need to: |
| 2353 | * clear the uptodate bits on error |
| 2354 | * clear the writeback bits in the extent tree for this IO |
| 2355 | * end_page_writeback if the page has no more pending IO |
| 2356 | * |
| 2357 | * Scheduling is not allowed, so the extent state tree is expected |
| 2358 | * to have one and only one object corresponding to this IO. |
| 2359 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2360 | static void end_bio_extent_writepage(struct bio *bio, int err) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2361 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2362 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2363 | struct extent_io_tree *tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2364 | u64 start; |
| 2365 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2366 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2367 | do { |
| 2368 | struct page *page = bvec->bv_page; |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2369 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 2370 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2371 | /* We always issue full-page reads, but if some block |
| 2372 | * in a page fails to read, blk_update_request() will |
| 2373 | * advance bv_offset and adjust bv_len to compensate. |
| 2374 | * Print a warning for nonzero offsets, and an error |
| 2375 | * if they don't add up to a full page. */ |
| 2376 | if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE) |
| 2377 | printk("%s page write in btrfs with offset %u and length %u\n", |
| 2378 | bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE |
| 2379 | ? KERN_ERR "partial" : KERN_INFO "incomplete", |
| 2380 | bvec->bv_offset, bvec->bv_len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2381 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2382 | start = page_offset(page); |
| 2383 | end = start + bvec->bv_offset + bvec->bv_len - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2384 | |
| 2385 | if (--bvec >= bio->bi_io_vec) |
| 2386 | prefetchw(&bvec->bv_page->flags); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2387 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2388 | if (end_extent_writepage(page, err, start, end)) |
| 2389 | continue; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2390 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2391 | end_page_writeback(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2392 | } while (bvec >= bio->bi_io_vec); |
Chris Mason | 2b1f55b | 2008-09-24 11:48:04 -0400 | [diff] [blame] | 2393 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2394 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2395 | } |
| 2396 | |
| 2397 | /* |
| 2398 | * after a readpage IO is done, we need to: |
| 2399 | * clear the uptodate bits on error |
| 2400 | * set the uptodate bits if things worked |
| 2401 | * set the page up to date if all extents in the tree are uptodate |
| 2402 | * clear the lock bit in the extent tree |
| 2403 | * unlock the page if there are no other extents locked for it |
| 2404 | * |
| 2405 | * Scheduling is not allowed, so the extent state tree is expected |
| 2406 | * to have one and only one object corresponding to this IO. |
| 2407 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2408 | static void end_bio_extent_readpage(struct bio *bio, int err) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2409 | { |
| 2410 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | 4125bf7 | 2010-02-03 18:18:45 +0000 | [diff] [blame] | 2411 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 2412 | struct bio_vec *bvec = bio->bi_io_vec; |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame^] | 2413 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2414 | struct extent_io_tree *tree; |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame^] | 2415 | u64 offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2416 | u64 start; |
| 2417 | u64 end; |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame^] | 2418 | u64 len; |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2419 | int mirror; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2420 | int ret; |
| 2421 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2422 | if (err) |
| 2423 | uptodate = 0; |
| 2424 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2425 | do { |
| 2426 | struct page *page = bvec->bv_page; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2427 | struct extent_state *cached = NULL; |
| 2428 | struct extent_state *state; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2429 | struct inode *inode = page->mapping->host; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2430 | |
Kent Overstreet | be3940c | 2012-09-11 14:23:05 -0600 | [diff] [blame] | 2431 | pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, " |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2432 | "mirror=%lu\n", (u64)bio->bi_sector, err, |
| 2433 | io_bio->mirror_num); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2434 | tree = &BTRFS_I(inode)->io_tree; |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2435 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2436 | /* We always issue full-page reads, but if some block |
| 2437 | * in a page fails to read, blk_update_request() will |
| 2438 | * advance bv_offset and adjust bv_len to compensate. |
| 2439 | * Print a warning for nonzero offsets, and an error |
| 2440 | * if they don't add up to a full page. */ |
| 2441 | if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE) |
| 2442 | printk("%s page read in btrfs with offset %u and length %u\n", |
| 2443 | bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE |
| 2444 | ? KERN_ERR "partial" : KERN_INFO "incomplete", |
| 2445 | bvec->bv_offset, bvec->bv_len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2446 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2447 | start = page_offset(page); |
| 2448 | end = start + bvec->bv_offset + bvec->bv_len - 1; |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame^] | 2449 | len = bvec->bv_len; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2450 | |
Chris Mason | 4125bf7 | 2010-02-03 18:18:45 +0000 | [diff] [blame] | 2451 | if (++bvec <= bvec_end) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2452 | prefetchw(&bvec->bv_page->flags); |
| 2453 | |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2454 | spin_lock(&tree->lock); |
Chris Mason | 0d39920 | 2011-04-16 06:55:39 -0400 | [diff] [blame] | 2455 | state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED); |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2456 | if (likely(state && state->start == start)) { |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2457 | /* |
| 2458 | * take a reference on the state, unlock will drop |
| 2459 | * the ref |
| 2460 | */ |
| 2461 | cache_state(state, &cached); |
| 2462 | } |
| 2463 | spin_unlock(&tree->lock); |
| 2464 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2465 | mirror = io_bio->mirror_num; |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2466 | if (likely(uptodate && tree->ops && |
| 2467 | tree->ops->readpage_end_io_hook)) { |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame^] | 2468 | ret = tree->ops->readpage_end_io_hook(io_bio, offset, |
| 2469 | page, start, end, |
| 2470 | mirror); |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2471 | if (ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2472 | uptodate = 0; |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2473 | else |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2474 | clean_io_failure(start, page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2475 | } |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2476 | |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2477 | if (likely(uptodate)) |
| 2478 | goto readpage_ok; |
| 2479 | |
| 2480 | if (tree->ops && tree->ops->readpage_io_failed_hook) { |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2481 | ret = tree->ops->readpage_io_failed_hook(page, mirror); |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2482 | if (!ret && !err && |
| 2483 | test_bit(BIO_UPTODATE, &bio->bi_flags)) |
| 2484 | uptodate = 1; |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2485 | } else { |
Jan Schmidt | f4a8e65 | 2011-12-01 09:30:36 -0500 | [diff] [blame] | 2486 | /* |
| 2487 | * The generic bio_readpage_error handles errors the |
| 2488 | * following way: If possible, new read requests are |
| 2489 | * created and submitted and will end up in |
| 2490 | * end_bio_extent_readpage as well (if we're lucky, not |
| 2491 | * in the !uptodate case). In that case it returns 0 and |
| 2492 | * we just go on with the next page in our bio. If it |
| 2493 | * can't handle the error it will return -EIO and we |
| 2494 | * remain responsible for that page. |
| 2495 | */ |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame^] | 2496 | ret = bio_readpage_error(bio, offset, page, start, end, |
| 2497 | mirror); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2498 | if (ret == 0) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2499 | uptodate = |
| 2500 | test_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2501 | if (err) |
| 2502 | uptodate = 0; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2503 | uncache_state(&cached); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2504 | continue; |
| 2505 | } |
| 2506 | } |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2507 | readpage_ok: |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 2508 | if (uptodate && tree->track_uptodate) { |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2509 | set_extent_uptodate(tree, start, end, &cached, |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2510 | GFP_ATOMIC); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2511 | } |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2512 | unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2513 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2514 | if (uptodate) { |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2515 | loff_t i_size = i_size_read(inode); |
| 2516 | pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; |
| 2517 | unsigned offset; |
| 2518 | |
| 2519 | /* Zero out the end if this page straddles i_size */ |
| 2520 | offset = i_size & (PAGE_CACHE_SIZE-1); |
| 2521 | if (page->index == end_index && offset) |
| 2522 | zero_user_segment(page, offset, PAGE_CACHE_SIZE); |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2523 | SetPageUptodate(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2524 | } else { |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2525 | ClearPageUptodate(page); |
| 2526 | SetPageError(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2527 | } |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2528 | unlock_page(page); |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame^] | 2529 | offset += len; |
Chris Mason | 4125bf7 | 2010-02-03 18:18:45 +0000 | [diff] [blame] | 2530 | } while (bvec <= bvec_end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2531 | |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame^] | 2532 | if (io_bio->end_io) |
| 2533 | io_bio->end_io(io_bio, err); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2534 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2535 | } |
| 2536 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2537 | /* |
| 2538 | * this allocates from the btrfs_bioset. We're returning a bio right now |
| 2539 | * but you can call btrfs_io_bio for the appropriate container_of magic |
| 2540 | */ |
Miao Xie | 88f794e | 2010-11-22 03:02:55 +0000 | [diff] [blame] | 2541 | struct bio * |
| 2542 | btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs, |
| 2543 | gfp_t gfp_flags) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2544 | { |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame^] | 2545 | struct btrfs_io_bio *btrfs_bio; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2546 | struct bio *bio; |
| 2547 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2548 | bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2549 | |
| 2550 | if (bio == NULL && (current->flags & PF_MEMALLOC)) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2551 | while (!bio && (nr_vecs /= 2)) { |
| 2552 | bio = bio_alloc_bioset(gfp_flags, |
| 2553 | nr_vecs, btrfs_bioset); |
| 2554 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | if (bio) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 2558 | bio->bi_size = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2559 | bio->bi_bdev = bdev; |
| 2560 | bio->bi_sector = first_sector; |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame^] | 2561 | btrfs_bio = btrfs_io_bio(bio); |
| 2562 | btrfs_bio->csum = NULL; |
| 2563 | btrfs_bio->csum_allocated = NULL; |
| 2564 | btrfs_bio->end_io = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2565 | } |
| 2566 | return bio; |
| 2567 | } |
| 2568 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2569 | struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask) |
| 2570 | { |
| 2571 | return bio_clone_bioset(bio, gfp_mask, btrfs_bioset); |
| 2572 | } |
| 2573 | |
| 2574 | |
| 2575 | /* this also allocates from the btrfs_bioset */ |
| 2576 | struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs) |
| 2577 | { |
Miao Xie | facc8a2 | 2013-07-25 19:22:34 +0800 | [diff] [blame^] | 2578 | struct btrfs_io_bio *btrfs_bio; |
| 2579 | struct bio *bio; |
| 2580 | |
| 2581 | bio = bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset); |
| 2582 | if (bio) { |
| 2583 | btrfs_bio = btrfs_io_bio(bio); |
| 2584 | btrfs_bio->csum = NULL; |
| 2585 | btrfs_bio->csum_allocated = NULL; |
| 2586 | btrfs_bio->end_io = NULL; |
| 2587 | } |
| 2588 | return bio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 2592 | static int __must_check submit_one_bio(int rw, struct bio *bio, |
| 2593 | int mirror_num, unsigned long bio_flags) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2594 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2595 | int ret = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2596 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 2597 | struct page *page = bvec->bv_page; |
| 2598 | struct extent_io_tree *tree = bio->bi_private; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2599 | u64 start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2600 | |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2601 | start = page_offset(page) + bvec->bv_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2602 | |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2603 | bio->bi_private = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2604 | |
| 2605 | bio_get(bio); |
| 2606 | |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2607 | if (tree->ops && tree->ops->submit_bio_hook) |
liubo | 6b82ce8 | 2011-01-26 06:21:39 +0000 | [diff] [blame] | 2608 | ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 2609 | mirror_num, bio_flags, start); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2610 | else |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 2611 | btrfsic_submit_bio(rw, bio); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2612 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2613 | if (bio_flagged(bio, BIO_EOPNOTSUPP)) |
| 2614 | ret = -EOPNOTSUPP; |
| 2615 | bio_put(bio); |
| 2616 | return ret; |
| 2617 | } |
| 2618 | |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 2619 | 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] | 2620 | unsigned long offset, size_t size, struct bio *bio, |
| 2621 | unsigned long bio_flags) |
| 2622 | { |
| 2623 | int ret = 0; |
| 2624 | if (tree->ops && tree->ops->merge_bio_hook) |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 2625 | ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio, |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 2626 | bio_flags); |
| 2627 | BUG_ON(ret < 0); |
| 2628 | return ret; |
| 2629 | |
| 2630 | } |
| 2631 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2632 | static int submit_extent_page(int rw, struct extent_io_tree *tree, |
| 2633 | struct page *page, sector_t sector, |
| 2634 | size_t size, unsigned long offset, |
| 2635 | struct block_device *bdev, |
| 2636 | struct bio **bio_ret, |
| 2637 | unsigned long max_pages, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2638 | bio_end_io_t end_io_func, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2639 | int mirror_num, |
| 2640 | unsigned long prev_bio_flags, |
| 2641 | unsigned long bio_flags) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2642 | { |
| 2643 | int ret = 0; |
| 2644 | struct bio *bio; |
| 2645 | int nr; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2646 | int contig = 0; |
| 2647 | int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED; |
| 2648 | int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 2649 | size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2650 | |
| 2651 | if (bio_ret && *bio_ret) { |
| 2652 | bio = *bio_ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2653 | if (old_compressed) |
| 2654 | contig = bio->bi_sector == sector; |
| 2655 | else |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 2656 | contig = bio_end_sector(bio) == sector; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2657 | |
| 2658 | if (prev_bio_flags != bio_flags || !contig || |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 2659 | merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2660 | bio_add_page(bio, page, page_size, offset) < page_size) { |
| 2661 | ret = submit_one_bio(rw, bio, mirror_num, |
| 2662 | prev_bio_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2663 | if (ret < 0) |
| 2664 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2665 | bio = NULL; |
| 2666 | } else { |
| 2667 | return 0; |
| 2668 | } |
| 2669 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2670 | if (this_compressed) |
| 2671 | nr = BIO_MAX_PAGES; |
| 2672 | else |
| 2673 | nr = bio_get_nr_vecs(bdev); |
| 2674 | |
Miao Xie | 88f794e | 2010-11-22 03:02:55 +0000 | [diff] [blame] | 2675 | bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH); |
Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 2676 | if (!bio) |
| 2677 | return -ENOMEM; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2678 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2679 | bio_add_page(bio, page, page_size, offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2680 | bio->bi_end_io = end_io_func; |
| 2681 | bio->bi_private = tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2682 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2683 | if (bio_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2684 | *bio_ret = bio; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2685 | else |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2686 | ret = submit_one_bio(rw, bio, mirror_num, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2687 | |
| 2688 | return ret; |
| 2689 | } |
| 2690 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 2691 | static void attach_extent_buffer_page(struct extent_buffer *eb, |
| 2692 | struct page *page) |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 2693 | { |
| 2694 | if (!PagePrivate(page)) { |
| 2695 | SetPagePrivate(page); |
| 2696 | page_cache_get(page); |
| 2697 | set_page_private(page, (unsigned long)eb); |
| 2698 | } else { |
| 2699 | WARN_ON(page->private != (unsigned long)eb); |
| 2700 | } |
| 2701 | } |
| 2702 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2703 | void set_page_extent_mapped(struct page *page) |
| 2704 | { |
| 2705 | if (!PagePrivate(page)) { |
| 2706 | SetPagePrivate(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2707 | page_cache_get(page); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2708 | set_page_private(page, EXTENT_PAGE_PRIVATE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2709 | } |
| 2710 | } |
| 2711 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2712 | /* |
| 2713 | * basic readpage implementation. Locked extent state structs are inserted |
| 2714 | * into the tree that are removed when the IO is done (by the end_io |
| 2715 | * handlers) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2716 | * XXX JDM: This needs looking at to ensure proper page locking |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2717 | */ |
| 2718 | static int __extent_read_full_page(struct extent_io_tree *tree, |
| 2719 | struct page *page, |
| 2720 | get_extent_t *get_extent, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2721 | struct bio **bio, int mirror_num, |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 2722 | unsigned long *bio_flags, int rw) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2723 | { |
| 2724 | struct inode *inode = page->mapping->host; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2725 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2726 | u64 page_end = start + PAGE_CACHE_SIZE - 1; |
| 2727 | u64 end; |
| 2728 | u64 cur = start; |
| 2729 | u64 extent_offset; |
| 2730 | u64 last_byte = i_size_read(inode); |
| 2731 | u64 block_start; |
| 2732 | u64 cur_end; |
| 2733 | sector_t sector; |
| 2734 | struct extent_map *em; |
| 2735 | struct block_device *bdev; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 2736 | struct btrfs_ordered_extent *ordered; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2737 | int ret; |
| 2738 | int nr = 0; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2739 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2740 | size_t iosize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2741 | size_t disk_io_size; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2742 | size_t blocksize = inode->i_sb->s_blocksize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2743 | unsigned long this_bio_flag = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2744 | |
| 2745 | set_page_extent_mapped(page); |
| 2746 | |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 2747 | if (!PageUptodate(page)) { |
| 2748 | if (cleancache_get_page(page) == 0) { |
| 2749 | BUG_ON(blocksize != PAGE_SIZE); |
| 2750 | goto out; |
| 2751 | } |
| 2752 | } |
| 2753 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2754 | end = page_end; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 2755 | while (1) { |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2756 | lock_extent(tree, start, end); |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 2757 | ordered = btrfs_lookup_ordered_extent(inode, start); |
| 2758 | if (!ordered) |
| 2759 | break; |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2760 | unlock_extent(tree, start, end); |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 2761 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 2762 | btrfs_put_ordered_extent(ordered); |
| 2763 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2764 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2765 | if (page->index == last_byte >> PAGE_CACHE_SHIFT) { |
| 2766 | char *userpage; |
| 2767 | size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1); |
| 2768 | |
| 2769 | if (zero_offset) { |
| 2770 | iosize = PAGE_CACHE_SIZE - zero_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2771 | userpage = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2772 | memset(userpage + zero_offset, 0, iosize); |
| 2773 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2774 | kunmap_atomic(userpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2775 | } |
| 2776 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2777 | while (cur <= end) { |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 2778 | unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1; |
| 2779 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2780 | if (cur >= last_byte) { |
| 2781 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2782 | struct extent_state *cached = NULL; |
| 2783 | |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2784 | iosize = PAGE_CACHE_SIZE - pg_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2785 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2786 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2787 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2788 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2789 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2790 | &cached, GFP_NOFS); |
| 2791 | unlock_extent_cached(tree, cur, cur + iosize - 1, |
| 2792 | &cached, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2793 | break; |
| 2794 | } |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2795 | em = get_extent(inode, page, pg_offset, cur, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2796 | end - cur + 1, 0); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 2797 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2798 | SetPageError(page); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2799 | unlock_extent(tree, cur, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2800 | break; |
| 2801 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2802 | extent_offset = cur - em->start; |
| 2803 | BUG_ON(extent_map_end(em) <= cur); |
| 2804 | BUG_ON(end < cur); |
| 2805 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2806 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2807 | this_bio_flag = EXTENT_BIO_COMPRESSED; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2808 | extent_set_compress_type(&this_bio_flag, |
| 2809 | em->compress_type); |
| 2810 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2811 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2812 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
| 2813 | cur_end = min(extent_map_end(em) - 1, end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 2814 | iosize = ALIGN(iosize, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2815 | if (this_bio_flag & EXTENT_BIO_COMPRESSED) { |
| 2816 | disk_io_size = em->block_len; |
| 2817 | sector = em->block_start >> 9; |
| 2818 | } else { |
| 2819 | sector = (em->block_start + extent_offset) >> 9; |
| 2820 | disk_io_size = iosize; |
| 2821 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2822 | bdev = em->bdev; |
| 2823 | block_start = em->block_start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2824 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 2825 | block_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2826 | free_extent_map(em); |
| 2827 | em = NULL; |
| 2828 | |
| 2829 | /* we've found a hole, just zero and go on */ |
| 2830 | if (block_start == EXTENT_MAP_HOLE) { |
| 2831 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2832 | struct extent_state *cached = NULL; |
| 2833 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2834 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2835 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2836 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2837 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2838 | |
| 2839 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2840 | &cached, GFP_NOFS); |
| 2841 | unlock_extent_cached(tree, cur, cur + iosize - 1, |
| 2842 | &cached, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2843 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2844 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2845 | continue; |
| 2846 | } |
| 2847 | /* the get_extent function already copied into the page */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2848 | if (test_range_bit(tree, cur, cur_end, |
| 2849 | EXTENT_UPTODATE, 1, NULL)) { |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 2850 | check_page_uptodate(tree, page); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2851 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2852 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2853 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2854 | continue; |
| 2855 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2856 | /* we have an inline extent but it didn't get marked up |
| 2857 | * to date. Error out |
| 2858 | */ |
| 2859 | if (block_start == EXTENT_MAP_INLINE) { |
| 2860 | SetPageError(page); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2861 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2862 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2863 | pg_offset += iosize; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2864 | continue; |
| 2865 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2866 | |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 2867 | pnr -= page->index; |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 2868 | ret = submit_extent_page(rw, tree, page, |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2869 | sector, disk_io_size, pg_offset, |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 2870 | bdev, bio, pnr, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2871 | end_bio_extent_readpage, mirror_num, |
| 2872 | *bio_flags, |
| 2873 | this_bio_flag); |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 2874 | if (!ret) { |
| 2875 | nr++; |
| 2876 | *bio_flags = this_bio_flag; |
| 2877 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2878 | SetPageError(page); |
Josef Bacik | edd33c9 | 2012-10-05 16:40:32 -0400 | [diff] [blame] | 2879 | unlock_extent(tree, cur, cur + iosize - 1); |
| 2880 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2881 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2882 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2883 | } |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 2884 | out: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2885 | if (!nr) { |
| 2886 | if (!PageError(page)) |
| 2887 | SetPageUptodate(page); |
| 2888 | unlock_page(page); |
| 2889 | } |
| 2890 | return 0; |
| 2891 | } |
| 2892 | |
| 2893 | 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] | 2894 | get_extent_t *get_extent, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2895 | { |
| 2896 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2897 | unsigned long bio_flags = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2898 | int ret; |
| 2899 | |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 2900 | ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num, |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 2901 | &bio_flags, READ); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2902 | if (bio) |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 2903 | ret = submit_one_bio(READ, bio, mirror_num, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2904 | return ret; |
| 2905 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2906 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 2907 | static noinline void update_nr_written(struct page *page, |
| 2908 | struct writeback_control *wbc, |
| 2909 | unsigned long nr_written) |
| 2910 | { |
| 2911 | wbc->nr_to_write -= nr_written; |
| 2912 | if (wbc->range_cyclic || (wbc->nr_to_write > 0 && |
| 2913 | wbc->range_start == 0 && wbc->range_end == LLONG_MAX)) |
| 2914 | page->mapping->writeback_index = page->index + nr_written; |
| 2915 | } |
| 2916 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2917 | /* |
| 2918 | * the writepage semantics are similar to regular writepage. extent |
| 2919 | * records are inserted to lock ranges in the tree, and as dirty areas |
| 2920 | * are found, they are marked writeback. Then the lock bits are removed |
| 2921 | * and the end_io handler clears the writeback ranges |
| 2922 | */ |
| 2923 | static int __extent_writepage(struct page *page, struct writeback_control *wbc, |
| 2924 | void *data) |
| 2925 | { |
| 2926 | struct inode *inode = page->mapping->host; |
| 2927 | struct extent_page_data *epd = data; |
| 2928 | struct extent_io_tree *tree = epd->tree; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2929 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2930 | u64 delalloc_start; |
| 2931 | u64 page_end = start + PAGE_CACHE_SIZE - 1; |
| 2932 | u64 end; |
| 2933 | u64 cur = start; |
| 2934 | u64 extent_offset; |
| 2935 | u64 last_byte = i_size_read(inode); |
| 2936 | u64 block_start; |
| 2937 | u64 iosize; |
| 2938 | sector_t sector; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 2939 | struct extent_state *cached_state = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2940 | struct extent_map *em; |
| 2941 | struct block_device *bdev; |
| 2942 | int ret; |
| 2943 | int nr = 0; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2944 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2945 | size_t blocksize; |
| 2946 | loff_t i_size = i_size_read(inode); |
| 2947 | unsigned long end_index = i_size >> PAGE_CACHE_SHIFT; |
| 2948 | u64 nr_delalloc; |
| 2949 | u64 delalloc_end; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2950 | int page_started; |
| 2951 | int compressed; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 2952 | int write_flags; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2953 | unsigned long nr_written = 0; |
Josef Bacik | 9e48710 | 2011-08-01 12:08:18 -0400 | [diff] [blame] | 2954 | bool fill_delalloc = true; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2955 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 2956 | if (wbc->sync_mode == WB_SYNC_ALL) |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 2957 | write_flags = WRITE_SYNC; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 2958 | else |
| 2959 | write_flags = WRITE; |
| 2960 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2961 | trace___extent_writepage(page, inode, wbc); |
| 2962 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2963 | WARN_ON(!PageLocked(page)); |
Chris Mason | bf0da8c | 2011-11-04 12:29:37 -0400 | [diff] [blame] | 2964 | |
| 2965 | ClearPageError(page); |
| 2966 | |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2967 | pg_offset = i_size & (PAGE_CACHE_SIZE - 1); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 2968 | if (page->index > end_index || |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2969 | (page->index == end_index && !pg_offset)) { |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 2970 | page->mapping->a_ops->invalidatepage(page, 0, PAGE_CACHE_SIZE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2971 | unlock_page(page); |
| 2972 | return 0; |
| 2973 | } |
| 2974 | |
| 2975 | if (page->index == end_index) { |
| 2976 | char *userpage; |
| 2977 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2978 | userpage = kmap_atomic(page); |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2979 | memset(userpage + pg_offset, 0, |
| 2980 | PAGE_CACHE_SIZE - pg_offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2981 | kunmap_atomic(userpage); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 2982 | flush_dcache_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2983 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2984 | pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2985 | |
| 2986 | set_page_extent_mapped(page); |
| 2987 | |
Josef Bacik | 9e48710 | 2011-08-01 12:08:18 -0400 | [diff] [blame] | 2988 | if (!tree->ops || !tree->ops->fill_delalloc) |
| 2989 | fill_delalloc = false; |
| 2990 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2991 | delalloc_start = start; |
| 2992 | delalloc_end = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2993 | page_started = 0; |
Josef Bacik | 9e48710 | 2011-08-01 12:08:18 -0400 | [diff] [blame] | 2994 | if (!epd->extent_locked && fill_delalloc) { |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 2995 | u64 delalloc_to_write = 0; |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 2996 | /* |
| 2997 | * make sure the wbc mapping index is at least updated |
| 2998 | * to this page. |
| 2999 | */ |
| 3000 | update_nr_written(page, wbc, 0); |
| 3001 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3002 | while (delalloc_end < page_end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3003 | nr_delalloc = find_lock_delalloc_range(inode, tree, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3004 | page, |
| 3005 | &delalloc_start, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3006 | &delalloc_end, |
| 3007 | 128 * 1024 * 1024); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3008 | if (nr_delalloc == 0) { |
| 3009 | delalloc_start = delalloc_end + 1; |
| 3010 | continue; |
| 3011 | } |
Tsutomu Itoh | 013bd4c | 2012-02-16 10:11:40 +0900 | [diff] [blame] | 3012 | ret = tree->ops->fill_delalloc(inode, page, |
| 3013 | delalloc_start, |
| 3014 | delalloc_end, |
| 3015 | &page_started, |
| 3016 | &nr_written); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3017 | /* File system has been set read-only */ |
| 3018 | if (ret) { |
| 3019 | SetPageError(page); |
| 3020 | goto done; |
| 3021 | } |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3022 | /* |
| 3023 | * delalloc_end is already one less than the total |
| 3024 | * length, so we don't subtract one from |
| 3025 | * PAGE_CACHE_SIZE |
| 3026 | */ |
| 3027 | delalloc_to_write += (delalloc_end - delalloc_start + |
| 3028 | PAGE_CACHE_SIZE) >> |
| 3029 | PAGE_CACHE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3030 | delalloc_start = delalloc_end + 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3031 | } |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3032 | if (wbc->nr_to_write < delalloc_to_write) { |
| 3033 | int thresh = 8192; |
| 3034 | |
| 3035 | if (delalloc_to_write < thresh * 2) |
| 3036 | thresh = delalloc_to_write; |
| 3037 | wbc->nr_to_write = min_t(u64, delalloc_to_write, |
| 3038 | thresh); |
| 3039 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3040 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3041 | /* did the fill delalloc function already unlock and start |
| 3042 | * the IO? |
| 3043 | */ |
| 3044 | if (page_started) { |
| 3045 | ret = 0; |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3046 | /* |
| 3047 | * we've unlocked the page, so we can't update |
| 3048 | * the mapping's writeback index, just update |
| 3049 | * nr_to_write. |
| 3050 | */ |
| 3051 | wbc->nr_to_write -= nr_written; |
| 3052 | goto done_unlocked; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3053 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3054 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3055 | if (tree->ops && tree->ops->writepage_start_hook) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3056 | ret = tree->ops->writepage_start_hook(page, start, |
| 3057 | page_end); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 3058 | if (ret) { |
| 3059 | /* Fixup worker will requeue */ |
| 3060 | if (ret == -EBUSY) |
| 3061 | wbc->pages_skipped++; |
| 3062 | else |
| 3063 | redirty_page_for_writepage(wbc, page); |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3064 | update_nr_written(page, wbc, nr_written); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3065 | unlock_page(page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3066 | ret = 0; |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3067 | goto done_unlocked; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3068 | } |
| 3069 | } |
| 3070 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3071 | /* |
| 3072 | * we don't want to touch the inode after unlocking the page, |
| 3073 | * so we update the mapping writeback index now |
| 3074 | */ |
| 3075 | update_nr_written(page, wbc, nr_written + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3076 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3077 | end = page_end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3078 | if (last_byte <= start) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3079 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 3080 | tree->ops->writepage_end_io_hook(page, start, |
| 3081 | page_end, NULL, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3082 | goto done; |
| 3083 | } |
| 3084 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3085 | blocksize = inode->i_sb->s_blocksize; |
| 3086 | |
| 3087 | while (cur <= end) { |
| 3088 | if (cur >= last_byte) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3089 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 3090 | tree->ops->writepage_end_io_hook(page, cur, |
| 3091 | page_end, NULL, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3092 | break; |
| 3093 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3094 | em = epd->get_extent(inode, page, pg_offset, cur, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3095 | end - cur + 1, 1); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3096 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3097 | SetPageError(page); |
| 3098 | break; |
| 3099 | } |
| 3100 | |
| 3101 | extent_offset = cur - em->start; |
| 3102 | BUG_ON(extent_map_end(em) <= cur); |
| 3103 | BUG_ON(end < cur); |
| 3104 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3105 | iosize = ALIGN(iosize, blocksize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3106 | sector = (em->block_start + extent_offset) >> 9; |
| 3107 | bdev = em->bdev; |
| 3108 | block_start = em->block_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3109 | compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3110 | free_extent_map(em); |
| 3111 | em = NULL; |
| 3112 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3113 | /* |
| 3114 | * compressed and inline extents are written through other |
| 3115 | * paths in the FS |
| 3116 | */ |
| 3117 | if (compressed || block_start == EXTENT_MAP_HOLE || |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3118 | block_start == EXTENT_MAP_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3119 | /* |
| 3120 | * end_io notification does not happen here for |
| 3121 | * compressed extents |
| 3122 | */ |
| 3123 | if (!compressed && tree->ops && |
| 3124 | tree->ops->writepage_end_io_hook) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3125 | tree->ops->writepage_end_io_hook(page, cur, |
| 3126 | cur + iosize - 1, |
| 3127 | NULL, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3128 | else if (compressed) { |
| 3129 | /* we don't want to end_page_writeback on |
| 3130 | * a compressed extent. this happens |
| 3131 | * elsewhere |
| 3132 | */ |
| 3133 | nr++; |
| 3134 | } |
| 3135 | |
| 3136 | cur += iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3137 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3138 | continue; |
| 3139 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3140 | /* leave this out until we have a page_mkwrite call */ |
| 3141 | if (0 && !test_range_bit(tree, cur, cur + iosize - 1, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3142 | EXTENT_DIRTY, 0, NULL)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3143 | cur = cur + iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3144 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3145 | continue; |
| 3146 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3147 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3148 | if (tree->ops && tree->ops->writepage_io_hook) { |
| 3149 | ret = tree->ops->writepage_io_hook(page, cur, |
| 3150 | cur + iosize - 1); |
| 3151 | } else { |
| 3152 | ret = 0; |
| 3153 | } |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 3154 | if (ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3155 | SetPageError(page); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 3156 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3157 | unsigned long max_nr = end_index + 1; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3158 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3159 | set_range_writeback(tree, cur, cur + iosize - 1); |
| 3160 | if (!PageWriteback(page)) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3161 | printk(KERN_ERR "btrfs warning page %lu not " |
| 3162 | "writeback, cur %llu end %llu\n", |
| 3163 | page->index, (unsigned long long)cur, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3164 | (unsigned long long)end); |
| 3165 | } |
| 3166 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3167 | ret = submit_extent_page(write_flags, tree, page, |
| 3168 | sector, iosize, pg_offset, |
| 3169 | bdev, &epd->bio, max_nr, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3170 | end_bio_extent_writepage, |
| 3171 | 0, 0, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3172 | if (ret) |
| 3173 | SetPageError(page); |
| 3174 | } |
| 3175 | cur = cur + iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3176 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3177 | nr++; |
| 3178 | } |
| 3179 | done: |
| 3180 | if (nr == 0) { |
| 3181 | /* make sure the mapping tag for page dirty gets cleared */ |
| 3182 | set_page_writeback(page); |
| 3183 | end_page_writeback(page); |
| 3184 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3185 | unlock_page(page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3186 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3187 | done_unlocked: |
| 3188 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 3189 | /* drop our reference on any cached states */ |
| 3190 | free_extent_state(cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3191 | return 0; |
| 3192 | } |
| 3193 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3194 | static int eb_wait(void *word) |
| 3195 | { |
| 3196 | io_schedule(); |
| 3197 | return 0; |
| 3198 | } |
| 3199 | |
Josef Bacik | fd8b2b6 | 2013-04-24 16:41:19 -0400 | [diff] [blame] | 3200 | void wait_on_extent_buffer_writeback(struct extent_buffer *eb) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3201 | { |
| 3202 | wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait, |
| 3203 | TASK_UNINTERRUPTIBLE); |
| 3204 | } |
| 3205 | |
| 3206 | static int lock_extent_buffer_for_io(struct extent_buffer *eb, |
| 3207 | struct btrfs_fs_info *fs_info, |
| 3208 | struct extent_page_data *epd) |
| 3209 | { |
| 3210 | unsigned long i, num_pages; |
| 3211 | int flush = 0; |
| 3212 | int ret = 0; |
| 3213 | |
| 3214 | if (!btrfs_try_tree_write_lock(eb)) { |
| 3215 | flush = 1; |
| 3216 | flush_write_bio(epd); |
| 3217 | btrfs_tree_lock(eb); |
| 3218 | } |
| 3219 | |
| 3220 | if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) { |
| 3221 | btrfs_tree_unlock(eb); |
| 3222 | if (!epd->sync_io) |
| 3223 | return 0; |
| 3224 | if (!flush) { |
| 3225 | flush_write_bio(epd); |
| 3226 | flush = 1; |
| 3227 | } |
Chris Mason | a098d8e | 2012-03-21 12:09:56 -0400 | [diff] [blame] | 3228 | while (1) { |
| 3229 | wait_on_extent_buffer_writeback(eb); |
| 3230 | btrfs_tree_lock(eb); |
| 3231 | if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) |
| 3232 | break; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3233 | btrfs_tree_unlock(eb); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3234 | } |
| 3235 | } |
| 3236 | |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3237 | /* |
| 3238 | * We need to do this to prevent races in people who check if the eb is |
| 3239 | * under IO since we can end up having no IO bits set for a short period |
| 3240 | * of time. |
| 3241 | */ |
| 3242 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3243 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) { |
| 3244 | set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3245 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3246 | btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); |
Miao Xie | e2d8452 | 2013-01-29 10:09:20 +0000 | [diff] [blame] | 3247 | __percpu_counter_add(&fs_info->dirty_metadata_bytes, |
| 3248 | -eb->len, |
| 3249 | fs_info->dirty_metadata_batch); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3250 | ret = 1; |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3251 | } else { |
| 3252 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | btrfs_tree_unlock(eb); |
| 3256 | |
| 3257 | if (!ret) |
| 3258 | return ret; |
| 3259 | |
| 3260 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3261 | for (i = 0; i < num_pages; i++) { |
| 3262 | struct page *p = extent_buffer_page(eb, i); |
| 3263 | |
| 3264 | if (!trylock_page(p)) { |
| 3265 | if (!flush) { |
| 3266 | flush_write_bio(epd); |
| 3267 | flush = 1; |
| 3268 | } |
| 3269 | lock_page(p); |
| 3270 | } |
| 3271 | } |
| 3272 | |
| 3273 | return ret; |
| 3274 | } |
| 3275 | |
| 3276 | static void end_extent_buffer_writeback(struct extent_buffer *eb) |
| 3277 | { |
| 3278 | clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
| 3279 | smp_mb__after_clear_bit(); |
| 3280 | wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK); |
| 3281 | } |
| 3282 | |
| 3283 | static void end_bio_extent_buffer_writepage(struct bio *bio, int err) |
| 3284 | { |
| 3285 | int uptodate = err == 0; |
| 3286 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 3287 | struct extent_buffer *eb; |
| 3288 | int done; |
| 3289 | |
| 3290 | do { |
| 3291 | struct page *page = bvec->bv_page; |
| 3292 | |
| 3293 | bvec--; |
| 3294 | eb = (struct extent_buffer *)page->private; |
| 3295 | BUG_ON(!eb); |
| 3296 | done = atomic_dec_and_test(&eb->io_pages); |
| 3297 | |
| 3298 | if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) { |
| 3299 | set_bit(EXTENT_BUFFER_IOERR, &eb->bflags); |
| 3300 | ClearPageUptodate(page); |
| 3301 | SetPageError(page); |
| 3302 | } |
| 3303 | |
| 3304 | end_page_writeback(page); |
| 3305 | |
| 3306 | if (!done) |
| 3307 | continue; |
| 3308 | |
| 3309 | end_extent_buffer_writeback(eb); |
| 3310 | } while (bvec >= bio->bi_io_vec); |
| 3311 | |
| 3312 | bio_put(bio); |
| 3313 | |
| 3314 | } |
| 3315 | |
| 3316 | static int write_one_eb(struct extent_buffer *eb, |
| 3317 | struct btrfs_fs_info *fs_info, |
| 3318 | struct writeback_control *wbc, |
| 3319 | struct extent_page_data *epd) |
| 3320 | { |
| 3321 | struct block_device *bdev = fs_info->fs_devices->latest_bdev; |
| 3322 | u64 offset = eb->start; |
| 3323 | unsigned long i, num_pages; |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3324 | unsigned long bio_flags = 0; |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 3325 | int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META; |
Josef Bacik | d7dbe9e | 2012-04-23 14:00:51 -0400 | [diff] [blame] | 3326 | int ret = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3327 | |
| 3328 | clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags); |
| 3329 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3330 | atomic_set(&eb->io_pages, num_pages); |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3331 | if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID) |
| 3332 | bio_flags = EXTENT_BIO_TREE_LOG; |
| 3333 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3334 | for (i = 0; i < num_pages; i++) { |
| 3335 | struct page *p = extent_buffer_page(eb, i); |
| 3336 | |
| 3337 | clear_page_dirty_for_io(p); |
| 3338 | set_page_writeback(p); |
| 3339 | ret = submit_extent_page(rw, eb->tree, p, offset >> 9, |
| 3340 | PAGE_CACHE_SIZE, 0, bdev, &epd->bio, |
| 3341 | -1, end_bio_extent_buffer_writepage, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3342 | 0, epd->bio_flags, bio_flags); |
| 3343 | epd->bio_flags = bio_flags; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3344 | if (ret) { |
| 3345 | set_bit(EXTENT_BUFFER_IOERR, &eb->bflags); |
| 3346 | SetPageError(p); |
| 3347 | if (atomic_sub_and_test(num_pages - i, &eb->io_pages)) |
| 3348 | end_extent_buffer_writeback(eb); |
| 3349 | ret = -EIO; |
| 3350 | break; |
| 3351 | } |
| 3352 | offset += PAGE_CACHE_SIZE; |
| 3353 | update_nr_written(p, wbc, 1); |
| 3354 | unlock_page(p); |
| 3355 | } |
| 3356 | |
| 3357 | if (unlikely(ret)) { |
| 3358 | for (; i < num_pages; i++) { |
| 3359 | struct page *p = extent_buffer_page(eb, i); |
| 3360 | unlock_page(p); |
| 3361 | } |
| 3362 | } |
| 3363 | |
| 3364 | return ret; |
| 3365 | } |
| 3366 | |
| 3367 | int btree_write_cache_pages(struct address_space *mapping, |
| 3368 | struct writeback_control *wbc) |
| 3369 | { |
| 3370 | struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree; |
| 3371 | struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info; |
| 3372 | struct extent_buffer *eb, *prev_eb = NULL; |
| 3373 | struct extent_page_data epd = { |
| 3374 | .bio = NULL, |
| 3375 | .tree = tree, |
| 3376 | .extent_locked = 0, |
| 3377 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3378 | .bio_flags = 0, |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3379 | }; |
| 3380 | int ret = 0; |
| 3381 | int done = 0; |
| 3382 | int nr_to_write_done = 0; |
| 3383 | struct pagevec pvec; |
| 3384 | int nr_pages; |
| 3385 | pgoff_t index; |
| 3386 | pgoff_t end; /* Inclusive */ |
| 3387 | int scanned = 0; |
| 3388 | int tag; |
| 3389 | |
| 3390 | pagevec_init(&pvec, 0); |
| 3391 | if (wbc->range_cyclic) { |
| 3392 | index = mapping->writeback_index; /* Start from prev offset */ |
| 3393 | end = -1; |
| 3394 | } else { |
| 3395 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 3396 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
| 3397 | scanned = 1; |
| 3398 | } |
| 3399 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3400 | tag = PAGECACHE_TAG_TOWRITE; |
| 3401 | else |
| 3402 | tag = PAGECACHE_TAG_DIRTY; |
| 3403 | retry: |
| 3404 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3405 | tag_pages_for_writeback(mapping, index, end); |
| 3406 | while (!done && !nr_to_write_done && (index <= end) && |
| 3407 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
| 3408 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { |
| 3409 | unsigned i; |
| 3410 | |
| 3411 | scanned = 1; |
| 3412 | for (i = 0; i < nr_pages; i++) { |
| 3413 | struct page *page = pvec.pages[i]; |
| 3414 | |
| 3415 | if (!PagePrivate(page)) |
| 3416 | continue; |
| 3417 | |
| 3418 | if (!wbc->range_cyclic && page->index > end) { |
| 3419 | done = 1; |
| 3420 | break; |
| 3421 | } |
| 3422 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3423 | spin_lock(&mapping->private_lock); |
| 3424 | if (!PagePrivate(page)) { |
| 3425 | spin_unlock(&mapping->private_lock); |
| 3426 | continue; |
| 3427 | } |
| 3428 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3429 | eb = (struct extent_buffer *)page->private; |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3430 | |
| 3431 | /* |
| 3432 | * Shouldn't happen and normally this would be a BUG_ON |
| 3433 | * but no sense in crashing the users box for something |
| 3434 | * we can survive anyway. |
| 3435 | */ |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3436 | if (!eb) { |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3437 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3438 | WARN_ON(1); |
| 3439 | continue; |
| 3440 | } |
| 3441 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3442 | if (eb == prev_eb) { |
| 3443 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3444 | continue; |
| 3445 | } |
| 3446 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3447 | ret = atomic_inc_not_zero(&eb->refs); |
| 3448 | spin_unlock(&mapping->private_lock); |
| 3449 | if (!ret) |
| 3450 | continue; |
| 3451 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3452 | prev_eb = eb; |
| 3453 | ret = lock_extent_buffer_for_io(eb, fs_info, &epd); |
| 3454 | if (!ret) { |
| 3455 | free_extent_buffer(eb); |
| 3456 | continue; |
| 3457 | } |
| 3458 | |
| 3459 | ret = write_one_eb(eb, fs_info, wbc, &epd); |
| 3460 | if (ret) { |
| 3461 | done = 1; |
| 3462 | free_extent_buffer(eb); |
| 3463 | break; |
| 3464 | } |
| 3465 | free_extent_buffer(eb); |
| 3466 | |
| 3467 | /* |
| 3468 | * the filesystem may choose to bump up nr_to_write. |
| 3469 | * We have to make sure to honor the new nr_to_write |
| 3470 | * at any time |
| 3471 | */ |
| 3472 | nr_to_write_done = wbc->nr_to_write <= 0; |
| 3473 | } |
| 3474 | pagevec_release(&pvec); |
| 3475 | cond_resched(); |
| 3476 | } |
| 3477 | if (!scanned && !done) { |
| 3478 | /* |
| 3479 | * We hit the last page and there is more work to be done: wrap |
| 3480 | * back to the start of the file |
| 3481 | */ |
| 3482 | scanned = 1; |
| 3483 | index = 0; |
| 3484 | goto retry; |
| 3485 | } |
| 3486 | flush_write_bio(&epd); |
| 3487 | return ret; |
| 3488 | } |
| 3489 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3490 | /** |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3491 | * write_cache_pages - walk the list of dirty pages of the given address space and write all of them. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3492 | * @mapping: address space structure to write |
| 3493 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
| 3494 | * @writepage: function called for each page |
| 3495 | * @data: data passed to writepage function |
| 3496 | * |
| 3497 | * If a page is already under I/O, write_cache_pages() skips it, even |
| 3498 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, |
| 3499 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() |
| 3500 | * and msync() need to guarantee that all the data which was dirty at the time |
| 3501 | * the call was made get new I/O started against them. If wbc->sync_mode is |
| 3502 | * WB_SYNC_ALL then we were called for data integrity and we must wait for |
| 3503 | * existing IO to complete. |
| 3504 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 3505 | static int extent_write_cache_pages(struct extent_io_tree *tree, |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3506 | struct address_space *mapping, |
| 3507 | struct writeback_control *wbc, |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3508 | writepage_t writepage, void *data, |
| 3509 | void (*flush_fn)(void *)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3510 | { |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 3511 | struct inode *inode = mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3512 | int ret = 0; |
| 3513 | int done = 0; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3514 | int nr_to_write_done = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3515 | struct pagevec pvec; |
| 3516 | int nr_pages; |
| 3517 | pgoff_t index; |
| 3518 | pgoff_t end; /* Inclusive */ |
| 3519 | int scanned = 0; |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3520 | int tag; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3521 | |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 3522 | /* |
| 3523 | * We have to hold onto the inode so that ordered extents can do their |
| 3524 | * work when the IO finishes. The alternative to this is failing to add |
| 3525 | * an ordered extent if the igrab() fails there and that is a huge pain |
| 3526 | * to deal with, so instead just hold onto the inode throughout the |
| 3527 | * writepages operation. If it fails here we are freeing up the inode |
| 3528 | * anyway and we'd rather not waste our time writing out stuff that is |
| 3529 | * going to be truncated anyway. |
| 3530 | */ |
| 3531 | if (!igrab(inode)) |
| 3532 | return 0; |
| 3533 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3534 | pagevec_init(&pvec, 0); |
| 3535 | if (wbc->range_cyclic) { |
| 3536 | index = mapping->writeback_index; /* Start from prev offset */ |
| 3537 | end = -1; |
| 3538 | } else { |
| 3539 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 3540 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3541 | scanned = 1; |
| 3542 | } |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3543 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3544 | tag = PAGECACHE_TAG_TOWRITE; |
| 3545 | else |
| 3546 | tag = PAGECACHE_TAG_DIRTY; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3547 | retry: |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3548 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3549 | tag_pages_for_writeback(mapping, index, end); |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3550 | while (!done && !nr_to_write_done && (index <= end) && |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3551 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
| 3552 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3553 | unsigned i; |
| 3554 | |
| 3555 | scanned = 1; |
| 3556 | for (i = 0; i < nr_pages; i++) { |
| 3557 | struct page *page = pvec.pages[i]; |
| 3558 | |
| 3559 | /* |
| 3560 | * At this point we hold neither mapping->tree_lock nor |
| 3561 | * lock on the page itself: the page may be truncated or |
| 3562 | * invalidated (changing page->mapping to NULL), or even |
| 3563 | * swizzled back from swapper_space to tmpfs file |
| 3564 | * mapping |
| 3565 | */ |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3566 | if (!trylock_page(page)) { |
| 3567 | flush_fn(data); |
| 3568 | lock_page(page); |
Chris Mason | 01d658f | 2011-11-01 10:08:06 -0400 | [diff] [blame] | 3569 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3570 | |
| 3571 | if (unlikely(page->mapping != mapping)) { |
| 3572 | unlock_page(page); |
| 3573 | continue; |
| 3574 | } |
| 3575 | |
| 3576 | if (!wbc->range_cyclic && page->index > end) { |
| 3577 | done = 1; |
| 3578 | unlock_page(page); |
| 3579 | continue; |
| 3580 | } |
| 3581 | |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3582 | if (wbc->sync_mode != WB_SYNC_NONE) { |
Chris Mason | 0e6bd95 | 2008-11-20 10:46:35 -0500 | [diff] [blame] | 3583 | if (PageWriteback(page)) |
| 3584 | flush_fn(data); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3585 | wait_on_page_writeback(page); |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3586 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3587 | |
| 3588 | if (PageWriteback(page) || |
| 3589 | !clear_page_dirty_for_io(page)) { |
| 3590 | unlock_page(page); |
| 3591 | continue; |
| 3592 | } |
| 3593 | |
| 3594 | ret = (*writepage)(page, wbc, data); |
| 3595 | |
| 3596 | if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) { |
| 3597 | unlock_page(page); |
| 3598 | ret = 0; |
| 3599 | } |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3600 | if (ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3601 | done = 1; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3602 | |
| 3603 | /* |
| 3604 | * the filesystem may choose to bump up nr_to_write. |
| 3605 | * We have to make sure to honor the new nr_to_write |
| 3606 | * at any time |
| 3607 | */ |
| 3608 | nr_to_write_done = wbc->nr_to_write <= 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3609 | } |
| 3610 | pagevec_release(&pvec); |
| 3611 | cond_resched(); |
| 3612 | } |
| 3613 | if (!scanned && !done) { |
| 3614 | /* |
| 3615 | * We hit the last page and there is more work to be done: wrap |
| 3616 | * back to the start of the file |
| 3617 | */ |
| 3618 | scanned = 1; |
| 3619 | index = 0; |
| 3620 | goto retry; |
| 3621 | } |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 3622 | btrfs_add_delayed_iput(inode); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3623 | return ret; |
| 3624 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3625 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3626 | static void flush_epd_write_bio(struct extent_page_data *epd) |
| 3627 | { |
| 3628 | if (epd->bio) { |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 3629 | int rw = WRITE; |
| 3630 | int ret; |
| 3631 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3632 | if (epd->sync_io) |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 3633 | rw = WRITE_SYNC; |
| 3634 | |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3635 | ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3636 | BUG_ON(ret < 0); /* -ENOMEM */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3637 | epd->bio = NULL; |
| 3638 | } |
| 3639 | } |
| 3640 | |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3641 | static noinline void flush_write_bio(void *data) |
| 3642 | { |
| 3643 | struct extent_page_data *epd = data; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3644 | flush_epd_write_bio(epd); |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3645 | } |
| 3646 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3647 | int extent_write_full_page(struct extent_io_tree *tree, struct page *page, |
| 3648 | get_extent_t *get_extent, |
| 3649 | struct writeback_control *wbc) |
| 3650 | { |
| 3651 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3652 | struct extent_page_data epd = { |
| 3653 | .bio = NULL, |
| 3654 | .tree = tree, |
| 3655 | .get_extent = get_extent, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3656 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3657 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3658 | .bio_flags = 0, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3659 | }; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3660 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3661 | ret = __extent_writepage(page, wbc, &epd); |
| 3662 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3663 | flush_epd_write_bio(&epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3664 | return ret; |
| 3665 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3666 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3667 | int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode, |
| 3668 | u64 start, u64 end, get_extent_t *get_extent, |
| 3669 | int mode) |
| 3670 | { |
| 3671 | int ret = 0; |
| 3672 | struct address_space *mapping = inode->i_mapping; |
| 3673 | struct page *page; |
| 3674 | unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >> |
| 3675 | PAGE_CACHE_SHIFT; |
| 3676 | |
| 3677 | struct extent_page_data epd = { |
| 3678 | .bio = NULL, |
| 3679 | .tree = tree, |
| 3680 | .get_extent = get_extent, |
| 3681 | .extent_locked = 1, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3682 | .sync_io = mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3683 | .bio_flags = 0, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3684 | }; |
| 3685 | struct writeback_control wbc_writepages = { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3686 | .sync_mode = mode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3687 | .nr_to_write = nr_pages * 2, |
| 3688 | .range_start = start, |
| 3689 | .range_end = end + 1, |
| 3690 | }; |
| 3691 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3692 | while (start <= end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3693 | page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT); |
| 3694 | if (clear_page_dirty_for_io(page)) |
| 3695 | ret = __extent_writepage(page, &wbc_writepages, &epd); |
| 3696 | else { |
| 3697 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 3698 | tree->ops->writepage_end_io_hook(page, start, |
| 3699 | start + PAGE_CACHE_SIZE - 1, |
| 3700 | NULL, 1); |
| 3701 | unlock_page(page); |
| 3702 | } |
| 3703 | page_cache_release(page); |
| 3704 | start += PAGE_CACHE_SIZE; |
| 3705 | } |
| 3706 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3707 | flush_epd_write_bio(&epd); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3708 | return ret; |
| 3709 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3710 | |
| 3711 | int extent_writepages(struct extent_io_tree *tree, |
| 3712 | struct address_space *mapping, |
| 3713 | get_extent_t *get_extent, |
| 3714 | struct writeback_control *wbc) |
| 3715 | { |
| 3716 | int ret = 0; |
| 3717 | struct extent_page_data epd = { |
| 3718 | .bio = NULL, |
| 3719 | .tree = tree, |
| 3720 | .get_extent = get_extent, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3721 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3722 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3723 | .bio_flags = 0, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3724 | }; |
| 3725 | |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3726 | ret = extent_write_cache_pages(tree, mapping, wbc, |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3727 | __extent_writepage, &epd, |
| 3728 | flush_write_bio); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3729 | flush_epd_write_bio(&epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3730 | return ret; |
| 3731 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3732 | |
| 3733 | int extent_readpages(struct extent_io_tree *tree, |
| 3734 | struct address_space *mapping, |
| 3735 | struct list_head *pages, unsigned nr_pages, |
| 3736 | get_extent_t get_extent) |
| 3737 | { |
| 3738 | struct bio *bio = NULL; |
| 3739 | unsigned page_idx; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3740 | unsigned long bio_flags = 0; |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 3741 | struct page *pagepool[16]; |
| 3742 | struct page *page; |
| 3743 | int i = 0; |
| 3744 | int nr = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3745 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3746 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 3747 | page = list_entry(pages->prev, struct page, lru); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3748 | |
| 3749 | prefetchw(&page->flags); |
| 3750 | list_del(&page->lru); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 3751 | if (add_to_page_cache_lru(page, mapping, |
Itaru Kitayama | 43e817a | 2011-04-25 19:43:51 -0400 | [diff] [blame] | 3752 | page->index, GFP_NOFS)) { |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 3753 | page_cache_release(page); |
| 3754 | continue; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3755 | } |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 3756 | |
| 3757 | pagepool[nr++] = page; |
| 3758 | if (nr < ARRAY_SIZE(pagepool)) |
| 3759 | continue; |
| 3760 | for (i = 0; i < nr; i++) { |
| 3761 | __extent_read_full_page(tree, pagepool[i], get_extent, |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 3762 | &bio, 0, &bio_flags, READ); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 3763 | page_cache_release(pagepool[i]); |
| 3764 | } |
| 3765 | nr = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3766 | } |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 3767 | for (i = 0; i < nr; i++) { |
| 3768 | __extent_read_full_page(tree, pagepool[i], get_extent, |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 3769 | &bio, 0, &bio_flags, READ); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 3770 | page_cache_release(pagepool[i]); |
| 3771 | } |
| 3772 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3773 | BUG_ON(!list_empty(pages)); |
| 3774 | if (bio) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3775 | return submit_one_bio(READ, bio, 0, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3776 | return 0; |
| 3777 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3778 | |
| 3779 | /* |
| 3780 | * basic invalidatepage code, this waits on any locked or writeback |
| 3781 | * ranges corresponding to the page, and then deletes any extent state |
| 3782 | * records from the tree |
| 3783 | */ |
| 3784 | int extent_invalidatepage(struct extent_io_tree *tree, |
| 3785 | struct page *page, unsigned long offset) |
| 3786 | { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3787 | struct extent_state *cached_state = NULL; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3788 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3789 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 3790 | size_t blocksize = page->mapping->host->i_sb->s_blocksize; |
| 3791 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3792 | start += ALIGN(offset, blocksize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3793 | if (start > end) |
| 3794 | return 0; |
| 3795 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 3796 | lock_extent_bits(tree, start, end, 0, &cached_state); |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 3797 | wait_on_page_writeback(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3798 | clear_extent_bit(tree, start, end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 3799 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 3800 | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3801 | 1, 1, &cached_state, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3802 | return 0; |
| 3803 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3804 | |
| 3805 | /* |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 3806 | * a helper for releasepage, this tests for areas of the page that |
| 3807 | * are locked or under IO and drops the related state bits if it is safe |
| 3808 | * to drop the page. |
| 3809 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3810 | static int try_release_extent_state(struct extent_map_tree *map, |
| 3811 | struct extent_io_tree *tree, |
| 3812 | struct page *page, gfp_t mask) |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 3813 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3814 | u64 start = page_offset(page); |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 3815 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 3816 | int ret = 1; |
| 3817 | |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3818 | if (test_range_bit(tree, start, end, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 3819 | EXTENT_IOBITS, 0, NULL)) |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 3820 | ret = 0; |
| 3821 | else { |
| 3822 | if ((mask & GFP_NOFS) == GFP_NOFS) |
| 3823 | mask = GFP_NOFS; |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 3824 | /* |
| 3825 | * at this point we can safely clear everything except the |
| 3826 | * locked bit and the nodatasum bit |
| 3827 | */ |
Chris Mason | e3f24cc | 2011-02-14 12:52:08 -0500 | [diff] [blame] | 3828 | ret = clear_extent_bit(tree, start, end, |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 3829 | ~(EXTENT_LOCKED | EXTENT_NODATASUM), |
| 3830 | 0, 0, NULL, mask); |
Chris Mason | e3f24cc | 2011-02-14 12:52:08 -0500 | [diff] [blame] | 3831 | |
| 3832 | /* if clear_extent_bit failed for enomem reasons, |
| 3833 | * we can't allow the release to continue. |
| 3834 | */ |
| 3835 | if (ret < 0) |
| 3836 | ret = 0; |
| 3837 | else |
| 3838 | ret = 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 3839 | } |
| 3840 | return ret; |
| 3841 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 3842 | |
| 3843 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3844 | * a helper for releasepage. As long as there are no locked extents |
| 3845 | * in the range corresponding to the page, both state records and extent |
| 3846 | * map records are removed |
| 3847 | */ |
| 3848 | int try_release_extent_mapping(struct extent_map_tree *map, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3849 | struct extent_io_tree *tree, struct page *page, |
| 3850 | gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3851 | { |
| 3852 | struct extent_map *em; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3853 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3854 | u64 end = start + PAGE_CACHE_SIZE - 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 3855 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3856 | if ((mask & __GFP_WAIT) && |
| 3857 | page->mapping->host->i_size > 16 * 1024 * 1024) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 3858 | u64 len; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3859 | while (start <= end) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 3860 | len = end - start + 1; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3861 | write_lock(&map->lock); |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 3862 | em = lookup_extent_mapping(map, start, len); |
Tsutomu Itoh | 285190d | 2012-02-16 16:23:58 +0900 | [diff] [blame] | 3863 | if (!em) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3864 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3865 | break; |
| 3866 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3867 | if (test_bit(EXTENT_FLAG_PINNED, &em->flags) || |
| 3868 | em->start != start) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3869 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3870 | free_extent_map(em); |
| 3871 | break; |
| 3872 | } |
| 3873 | if (!test_range_bit(tree, em->start, |
| 3874 | extent_map_end(em) - 1, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 3875 | EXTENT_LOCKED | EXTENT_WRITEBACK, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3876 | 0, NULL)) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3877 | remove_extent_mapping(map, em); |
| 3878 | /* once for the rb tree */ |
| 3879 | free_extent_map(em); |
| 3880 | } |
| 3881 | start = extent_map_end(em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3882 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3883 | |
| 3884 | /* once for us */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3885 | free_extent_map(em); |
| 3886 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3887 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 3888 | return try_release_extent_state(map, tree, page, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3889 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3890 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3891 | /* |
| 3892 | * helper function for fiemap, which doesn't want to see any holes. |
| 3893 | * This maps until we find something past 'last' |
| 3894 | */ |
| 3895 | static struct extent_map *get_extent_skip_holes(struct inode *inode, |
| 3896 | u64 offset, |
| 3897 | u64 last, |
| 3898 | get_extent_t *get_extent) |
| 3899 | { |
| 3900 | u64 sectorsize = BTRFS_I(inode)->root->sectorsize; |
| 3901 | struct extent_map *em; |
| 3902 | u64 len; |
| 3903 | |
| 3904 | if (offset >= last) |
| 3905 | return NULL; |
| 3906 | |
| 3907 | while(1) { |
| 3908 | len = last - offset; |
| 3909 | if (len == 0) |
| 3910 | break; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3911 | len = ALIGN(len, sectorsize); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3912 | em = get_extent(inode, NULL, 0, offset, len, 0); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3913 | if (IS_ERR_OR_NULL(em)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3914 | return em; |
| 3915 | |
| 3916 | /* if this isn't a hole return it */ |
| 3917 | if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) && |
| 3918 | em->block_start != EXTENT_MAP_HOLE) { |
| 3919 | return em; |
| 3920 | } |
| 3921 | |
| 3922 | /* this is a hole, advance to the next extent */ |
| 3923 | offset = extent_map_end(em); |
| 3924 | free_extent_map(em); |
| 3925 | if (offset >= last) |
| 3926 | break; |
| 3927 | } |
| 3928 | return NULL; |
| 3929 | } |
| 3930 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3931 | int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 3932 | __u64 start, __u64 len, get_extent_t *get_extent) |
| 3933 | { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3934 | int ret = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3935 | u64 off = start; |
| 3936 | u64 max = start + len; |
| 3937 | u32 flags = 0; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3938 | u32 found_type; |
| 3939 | u64 last; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3940 | u64 last_for_get_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3941 | u64 disko = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3942 | u64 isize = i_size_read(inode); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3943 | struct btrfs_key found_key; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3944 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3945 | struct extent_state *cached_state = NULL; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3946 | struct btrfs_path *path; |
| 3947 | struct btrfs_file_extent_item *item; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3948 | int end = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3949 | u64 em_start = 0; |
| 3950 | u64 em_len = 0; |
| 3951 | u64 em_end = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3952 | unsigned long emflags; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3953 | |
| 3954 | if (len == 0) |
| 3955 | return -EINVAL; |
| 3956 | |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3957 | path = btrfs_alloc_path(); |
| 3958 | if (!path) |
| 3959 | return -ENOMEM; |
| 3960 | path->leave_spinning = 1; |
| 3961 | |
Josef Bacik | 4d479cf | 2011-11-17 11:34:31 -0500 | [diff] [blame] | 3962 | start = ALIGN(start, BTRFS_I(inode)->root->sectorsize); |
| 3963 | len = ALIGN(len, BTRFS_I(inode)->root->sectorsize); |
| 3964 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3965 | /* |
| 3966 | * lookup the last file extent. We're not using i_size here |
| 3967 | * because there might be preallocation past i_size |
| 3968 | */ |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3969 | ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3970 | path, btrfs_ino(inode), -1, 0); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3971 | if (ret < 0) { |
| 3972 | btrfs_free_path(path); |
| 3973 | return ret; |
| 3974 | } |
| 3975 | WARN_ON(!ret); |
| 3976 | path->slots[0]--; |
| 3977 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3978 | struct btrfs_file_extent_item); |
| 3979 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]); |
| 3980 | found_type = btrfs_key_type(&found_key); |
| 3981 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3982 | /* No extents, but there might be delalloc bits */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3983 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3984 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3985 | /* have to trust i_size as the end */ |
| 3986 | last = (u64)-1; |
| 3987 | last_for_get_extent = isize; |
| 3988 | } else { |
| 3989 | /* |
| 3990 | * remember the start of the last extent. There are a |
| 3991 | * bunch of different factors that go into the length of the |
| 3992 | * extent, so its much less complex to remember where it started |
| 3993 | */ |
| 3994 | last = found_key.offset; |
| 3995 | last_for_get_extent = last + 1; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3996 | } |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3997 | btrfs_free_path(path); |
| 3998 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3999 | /* |
| 4000 | * we might have some extents allocated but more delalloc past those |
| 4001 | * extents. so, we trust isize unless the start of the last extent is |
| 4002 | * beyond isize |
| 4003 | */ |
| 4004 | if (last < isize) { |
| 4005 | last = (u64)-1; |
| 4006 | last_for_get_extent = isize; |
| 4007 | } |
| 4008 | |
Liu Bo | a52f4cd | 2013-05-01 16:23:41 +0000 | [diff] [blame] | 4009 | lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4010 | &cached_state); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4011 | |
Josef Bacik | 4d479cf | 2011-11-17 11:34:31 -0500 | [diff] [blame] | 4012 | em = get_extent_skip_holes(inode, start, last_for_get_extent, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4013 | get_extent); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4014 | if (!em) |
| 4015 | goto out; |
| 4016 | if (IS_ERR(em)) { |
| 4017 | ret = PTR_ERR(em); |
| 4018 | goto out; |
| 4019 | } |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4020 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4021 | while (!end) { |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4022 | u64 offset_in_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4023 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4024 | /* break if the extent we found is outside the range */ |
| 4025 | if (em->start >= max || extent_map_end(em) < off) |
| 4026 | break; |
| 4027 | |
| 4028 | /* |
| 4029 | * get_extent may return an extent that starts before our |
| 4030 | * requested range. We have to make sure the ranges |
| 4031 | * we return to fiemap always move forward and don't |
| 4032 | * overlap, so adjust the offsets here |
| 4033 | */ |
| 4034 | em_start = max(em->start, off); |
| 4035 | |
| 4036 | /* |
| 4037 | * record the offset from the start of the extent |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4038 | * for adjusting the disk offset below. Only do this if the |
| 4039 | * extent isn't compressed since our in ram offset may be past |
| 4040 | * what we have actually allocated on disk. |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4041 | */ |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4042 | if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 4043 | offset_in_extent = em_start - em->start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4044 | em_end = extent_map_end(em); |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4045 | em_len = em_end - em_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4046 | emflags = em->flags; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4047 | disko = 0; |
| 4048 | flags = 0; |
| 4049 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4050 | /* |
| 4051 | * bump off for our next call to get_extent |
| 4052 | */ |
| 4053 | off = extent_map_end(em); |
| 4054 | if (off >= max) |
| 4055 | end = 1; |
| 4056 | |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4057 | if (em->block_start == EXTENT_MAP_LAST_BYTE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4058 | end = 1; |
| 4059 | flags |= FIEMAP_EXTENT_LAST; |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4060 | } else if (em->block_start == EXTENT_MAP_INLINE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4061 | flags |= (FIEMAP_EXTENT_DATA_INLINE | |
| 4062 | FIEMAP_EXTENT_NOT_ALIGNED); |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4063 | } else if (em->block_start == EXTENT_MAP_DELALLOC) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4064 | flags |= (FIEMAP_EXTENT_DELALLOC | |
| 4065 | FIEMAP_EXTENT_UNKNOWN); |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4066 | } else { |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4067 | disko = em->block_start + offset_in_extent; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4068 | } |
| 4069 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 4070 | flags |= FIEMAP_EXTENT_ENCODED; |
| 4071 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4072 | free_extent_map(em); |
| 4073 | em = NULL; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4074 | if ((em_start >= last) || em_len == (u64)-1 || |
| 4075 | (last == (u64)-1 && isize <= em_end)) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4076 | flags |= FIEMAP_EXTENT_LAST; |
| 4077 | end = 1; |
| 4078 | } |
| 4079 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4080 | /* now scan forward to see if this is really the last extent. */ |
| 4081 | em = get_extent_skip_holes(inode, off, last_for_get_extent, |
| 4082 | get_extent); |
| 4083 | if (IS_ERR(em)) { |
| 4084 | ret = PTR_ERR(em); |
| 4085 | goto out; |
| 4086 | } |
| 4087 | if (!em) { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4088 | flags |= FIEMAP_EXTENT_LAST; |
| 4089 | end = 1; |
| 4090 | } |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4091 | ret = fiemap_fill_next_extent(fieinfo, em_start, disko, |
| 4092 | em_len, flags); |
| 4093 | if (ret) |
| 4094 | goto out_free; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4095 | } |
| 4096 | out_free: |
| 4097 | free_extent_map(em); |
| 4098 | out: |
Liu Bo | a52f4cd | 2013-05-01 16:23:41 +0000 | [diff] [blame] | 4099 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4100 | &cached_state, GFP_NOFS); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4101 | return ret; |
| 4102 | } |
| 4103 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4104 | static void __free_extent_buffer(struct extent_buffer *eb) |
| 4105 | { |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 4106 | btrfs_leak_debug_del(&eb->leak_list); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4107 | kmem_cache_free(extent_buffer_cache, eb); |
| 4108 | } |
| 4109 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4110 | static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree, |
| 4111 | u64 start, |
| 4112 | unsigned long len, |
| 4113 | gfp_t mask) |
| 4114 | { |
| 4115 | struct extent_buffer *eb = NULL; |
| 4116 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4117 | eb = kmem_cache_zalloc(extent_buffer_cache, mask); |
Tsutomu Itoh | 91ca338 | 2011-01-05 02:32:22 +0000 | [diff] [blame] | 4118 | if (eb == NULL) |
| 4119 | return NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4120 | eb->start = start; |
| 4121 | eb->len = len; |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4122 | eb->tree = tree; |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4123 | eb->bflags = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4124 | rwlock_init(&eb->lock); |
| 4125 | atomic_set(&eb->write_locks, 0); |
| 4126 | atomic_set(&eb->read_locks, 0); |
| 4127 | atomic_set(&eb->blocking_readers, 0); |
| 4128 | atomic_set(&eb->blocking_writers, 0); |
| 4129 | atomic_set(&eb->spinning_readers, 0); |
| 4130 | atomic_set(&eb->spinning_writers, 0); |
Arne Jansen | 5b25f70 | 2011-09-13 10:55:48 +0200 | [diff] [blame] | 4131 | eb->lock_nested = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4132 | init_waitqueue_head(&eb->write_lock_wq); |
| 4133 | init_waitqueue_head(&eb->read_lock_wq); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4134 | |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 4135 | btrfs_leak_debug_add(&eb->leak_list, &buffers); |
| 4136 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4137 | spin_lock_init(&eb->refs_lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4138 | atomic_set(&eb->refs, 1); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4139 | atomic_set(&eb->io_pages, 0); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4140 | |
David Sterba | b8dae31 | 2013-02-28 14:54:18 +0000 | [diff] [blame] | 4141 | /* |
| 4142 | * Sanity checks, currently the maximum is 64k covered by 16x 4k pages |
| 4143 | */ |
| 4144 | BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE |
| 4145 | > MAX_INLINE_EXTENT_BUFFER_SIZE); |
| 4146 | BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4147 | |
| 4148 | return eb; |
| 4149 | } |
| 4150 | |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4151 | struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src) |
| 4152 | { |
| 4153 | unsigned long i; |
| 4154 | struct page *p; |
| 4155 | struct extent_buffer *new; |
| 4156 | unsigned long num_pages = num_extent_pages(src->start, src->len); |
| 4157 | |
| 4158 | new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC); |
| 4159 | if (new == NULL) |
| 4160 | return NULL; |
| 4161 | |
| 4162 | for (i = 0; i < num_pages; i++) { |
| 4163 | p = alloc_page(GFP_ATOMIC); |
| 4164 | BUG_ON(!p); |
| 4165 | attach_extent_buffer_page(new, p); |
| 4166 | WARN_ON(PageDirty(p)); |
| 4167 | SetPageUptodate(p); |
| 4168 | new->pages[i] = p; |
| 4169 | } |
| 4170 | |
| 4171 | copy_extent_buffer(new, src, 0, 0, src->len); |
| 4172 | set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags); |
| 4173 | set_bit(EXTENT_BUFFER_DUMMY, &new->bflags); |
| 4174 | |
| 4175 | return new; |
| 4176 | } |
| 4177 | |
| 4178 | struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len) |
| 4179 | { |
| 4180 | struct extent_buffer *eb; |
| 4181 | unsigned long num_pages = num_extent_pages(0, len); |
| 4182 | unsigned long i; |
| 4183 | |
| 4184 | eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC); |
| 4185 | if (!eb) |
| 4186 | return NULL; |
| 4187 | |
| 4188 | for (i = 0; i < num_pages; i++) { |
| 4189 | eb->pages[i] = alloc_page(GFP_ATOMIC); |
| 4190 | if (!eb->pages[i]) |
| 4191 | goto err; |
| 4192 | } |
| 4193 | set_extent_buffer_uptodate(eb); |
| 4194 | btrfs_set_header_nritems(eb, 0); |
| 4195 | set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags); |
| 4196 | |
| 4197 | return eb; |
| 4198 | err: |
Stefan Behrens | 84167d1 | 2012-10-11 07:25:16 -0600 | [diff] [blame] | 4199 | for (; i > 0; i--) |
| 4200 | __free_page(eb->pages[i - 1]); |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4201 | __free_extent_buffer(eb); |
| 4202 | return NULL; |
| 4203 | } |
| 4204 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4205 | static int extent_buffer_under_io(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4206 | { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4207 | return (atomic_read(&eb->io_pages) || |
| 4208 | test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) || |
| 4209 | test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4210 | } |
| 4211 | |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4212 | /* |
| 4213 | * Helper for releasing extent buffer page. |
| 4214 | */ |
| 4215 | static void btrfs_release_extent_buffer_page(struct extent_buffer *eb, |
| 4216 | unsigned long start_idx) |
| 4217 | { |
| 4218 | unsigned long index; |
Wang Sheng-Hui | 39bab87 | 2012-04-06 14:35:31 +0800 | [diff] [blame] | 4219 | unsigned long num_pages; |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4220 | struct page *page; |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4221 | int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags); |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4222 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4223 | BUG_ON(extent_buffer_under_io(eb)); |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4224 | |
Wang Sheng-Hui | 39bab87 | 2012-04-06 14:35:31 +0800 | [diff] [blame] | 4225 | num_pages = num_extent_pages(eb->start, eb->len); |
| 4226 | index = start_idx + num_pages; |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4227 | if (start_idx >= index) |
| 4228 | return; |
| 4229 | |
| 4230 | do { |
| 4231 | index--; |
| 4232 | page = extent_buffer_page(eb, index); |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4233 | if (page && mapped) { |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4234 | spin_lock(&page->mapping->private_lock); |
| 4235 | /* |
| 4236 | * We do this since we'll remove the pages after we've |
| 4237 | * removed the eb from the radix tree, so we could race |
| 4238 | * and have this page now attached to the new eb. So |
| 4239 | * only clear page_private if it's still connected to |
| 4240 | * this eb. |
| 4241 | */ |
| 4242 | if (PagePrivate(page) && |
| 4243 | page->private == (unsigned long)eb) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4244 | BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4245 | BUG_ON(PageDirty(page)); |
| 4246 | BUG_ON(PageWriteback(page)); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4247 | /* |
| 4248 | * We need to make sure we haven't be attached |
| 4249 | * to a new eb. |
| 4250 | */ |
| 4251 | ClearPagePrivate(page); |
| 4252 | set_page_private(page, 0); |
| 4253 | /* One for the page private */ |
| 4254 | page_cache_release(page); |
| 4255 | } |
| 4256 | spin_unlock(&page->mapping->private_lock); |
| 4257 | |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4258 | } |
| 4259 | if (page) { |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4260 | /* One for when we alloced the page */ |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4261 | page_cache_release(page); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4262 | } |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4263 | } while (index != start_idx); |
| 4264 | } |
| 4265 | |
| 4266 | /* |
| 4267 | * Helper for releasing the extent buffer. |
| 4268 | */ |
| 4269 | static inline void btrfs_release_extent_buffer(struct extent_buffer *eb) |
| 4270 | { |
| 4271 | btrfs_release_extent_buffer_page(eb, 0); |
| 4272 | __free_extent_buffer(eb); |
| 4273 | } |
| 4274 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4275 | static void check_buffer_tree_ref(struct extent_buffer *eb) |
| 4276 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4277 | int refs; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4278 | /* the ref bit is tricky. We have to make sure it is set |
| 4279 | * if we have the buffer dirty. Otherwise the |
| 4280 | * code to free a buffer can end up dropping a dirty |
| 4281 | * page |
| 4282 | * |
| 4283 | * Once the ref bit is set, it won't go away while the |
| 4284 | * buffer is dirty or in writeback, and it also won't |
| 4285 | * go away while we have the reference count on the |
| 4286 | * eb bumped. |
| 4287 | * |
| 4288 | * We can't just set the ref bit without bumping the |
| 4289 | * ref on the eb because free_extent_buffer might |
| 4290 | * see the ref bit and try to clear it. If this happens |
| 4291 | * free_extent_buffer might end up dropping our original |
| 4292 | * ref by mistake and freeing the page before we are able |
| 4293 | * to add one more ref. |
| 4294 | * |
| 4295 | * So bump the ref count first, then set the bit. If someone |
| 4296 | * beat us to it, drop the ref we added. |
| 4297 | */ |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4298 | refs = atomic_read(&eb->refs); |
| 4299 | if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 4300 | return; |
| 4301 | |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4302 | spin_lock(&eb->refs_lock); |
| 4303 | if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4304 | atomic_inc(&eb->refs); |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4305 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4306 | } |
| 4307 | |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4308 | static void mark_extent_buffer_accessed(struct extent_buffer *eb) |
| 4309 | { |
| 4310 | unsigned long num_pages, i; |
| 4311 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4312 | check_buffer_tree_ref(eb); |
| 4313 | |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4314 | num_pages = num_extent_pages(eb->start, eb->len); |
| 4315 | for (i = 0; i < num_pages; i++) { |
| 4316 | struct page *p = extent_buffer_page(eb, i); |
| 4317 | mark_page_accessed(p); |
| 4318 | } |
| 4319 | } |
| 4320 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4321 | struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree, |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4322 | u64 start, unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4323 | { |
| 4324 | unsigned long num_pages = num_extent_pages(start, len); |
| 4325 | unsigned long i; |
| 4326 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 4327 | struct extent_buffer *eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4328 | struct extent_buffer *exists = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4329 | struct page *p; |
| 4330 | struct address_space *mapping = tree->mapping; |
| 4331 | int uptodate = 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4332 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4333 | |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4334 | rcu_read_lock(); |
| 4335 | eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT); |
| 4336 | if (eb && atomic_inc_not_zero(&eb->refs)) { |
| 4337 | rcu_read_unlock(); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4338 | mark_extent_buffer_accessed(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4339 | return eb; |
| 4340 | } |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4341 | rcu_read_unlock(); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4342 | |
David Sterba | ba14419 | 2011-04-21 01:12:06 +0200 | [diff] [blame] | 4343 | eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 4344 | if (!eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4345 | return NULL; |
| 4346 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4347 | for (i = 0; i < num_pages; i++, index++) { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4348 | p = find_or_create_page(mapping, index, GFP_NOFS); |
Josef Bacik | 4804b38 | 2012-10-05 16:43:45 -0400 | [diff] [blame] | 4349 | if (!p) |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4350 | goto free_eb; |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4351 | |
| 4352 | spin_lock(&mapping->private_lock); |
| 4353 | if (PagePrivate(p)) { |
| 4354 | /* |
| 4355 | * We could have already allocated an eb for this page |
| 4356 | * and attached one so lets see if we can get a ref on |
| 4357 | * the existing eb, and if we can we know it's good and |
| 4358 | * we can just return that one, else we know we can just |
| 4359 | * overwrite page->private. |
| 4360 | */ |
| 4361 | exists = (struct extent_buffer *)p->private; |
| 4362 | if (atomic_inc_not_zero(&exists->refs)) { |
| 4363 | spin_unlock(&mapping->private_lock); |
| 4364 | unlock_page(p); |
Josef Bacik | 17de39a | 2012-05-04 15:16:06 -0400 | [diff] [blame] | 4365 | page_cache_release(p); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4366 | mark_extent_buffer_accessed(exists); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4367 | goto free_eb; |
| 4368 | } |
| 4369 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4370 | /* |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4371 | * Do this so attach doesn't complain and we need to |
| 4372 | * drop the ref the old guy had. |
| 4373 | */ |
| 4374 | ClearPagePrivate(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4375 | WARN_ON(PageDirty(p)); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4376 | page_cache_release(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4377 | } |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4378 | attach_extent_buffer_page(eb, p); |
| 4379 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4380 | WARN_ON(PageDirty(p)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4381 | mark_page_accessed(p); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4382 | eb->pages[i] = p; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4383 | if (!PageUptodate(p)) |
| 4384 | uptodate = 0; |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 4385 | |
| 4386 | /* |
| 4387 | * see below about how we avoid a nasty race with release page |
| 4388 | * and why we unlock later |
| 4389 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4390 | } |
| 4391 | if (uptodate) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4392 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 4393 | again: |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4394 | ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); |
| 4395 | if (ret) |
| 4396 | goto free_eb; |
| 4397 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4398 | spin_lock(&tree->buffer_lock); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4399 | ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb); |
| 4400 | if (ret == -EEXIST) { |
| 4401 | exists = radix_tree_lookup(&tree->buffer, |
| 4402 | start >> PAGE_CACHE_SHIFT); |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 4403 | if (!atomic_inc_not_zero(&exists->refs)) { |
| 4404 | spin_unlock(&tree->buffer_lock); |
| 4405 | radix_tree_preload_end(); |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 4406 | exists = NULL; |
| 4407 | goto again; |
| 4408 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4409 | spin_unlock(&tree->buffer_lock); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4410 | radix_tree_preload_end(); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4411 | mark_extent_buffer_accessed(exists); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4412 | goto free_eb; |
| 4413 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4414 | /* add one reference for the tree */ |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4415 | check_buffer_tree_ref(eb); |
Yan, Zheng | f044ba7 | 2010-02-04 08:46:56 +0000 | [diff] [blame] | 4416 | spin_unlock(&tree->buffer_lock); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4417 | radix_tree_preload_end(); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 4418 | |
| 4419 | /* |
| 4420 | * there is a race where release page may have |
| 4421 | * tried to find this extent buffer in the radix |
| 4422 | * but failed. It will tell the VM it is safe to |
| 4423 | * reclaim the, and it will clear the page private bit. |
| 4424 | * We must make sure to set the page private bit properly |
| 4425 | * after the extent buffer is in the radix tree so |
| 4426 | * it doesn't get lost |
| 4427 | */ |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4428 | SetPageChecked(eb->pages[0]); |
| 4429 | for (i = 1; i < num_pages; i++) { |
| 4430 | p = extent_buffer_page(eb, i); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4431 | ClearPageChecked(p); |
| 4432 | unlock_page(p); |
| 4433 | } |
| 4434 | unlock_page(eb->pages[0]); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4435 | return eb; |
| 4436 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4437 | free_eb: |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4438 | for (i = 0; i < num_pages; i++) { |
| 4439 | if (eb->pages[i]) |
| 4440 | unlock_page(eb->pages[i]); |
| 4441 | } |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 4442 | |
Josef Bacik | 17de39a | 2012-05-04 15:16:06 -0400 | [diff] [blame] | 4443 | WARN_ON(!atomic_dec_and_test(&eb->refs)); |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4444 | btrfs_release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4445 | return exists; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4446 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4447 | |
| 4448 | struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree, |
David Sterba | f09d1f6 | 2011-04-21 01:08:01 +0200 | [diff] [blame] | 4449 | u64 start, unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4450 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4451 | struct extent_buffer *eb; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4452 | |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4453 | rcu_read_lock(); |
| 4454 | eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT); |
| 4455 | if (eb && atomic_inc_not_zero(&eb->refs)) { |
| 4456 | rcu_read_unlock(); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4457 | mark_extent_buffer_accessed(eb); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4458 | return eb; |
| 4459 | } |
| 4460 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4461 | |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4462 | return NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4463 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4464 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4465 | static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head) |
| 4466 | { |
| 4467 | struct extent_buffer *eb = |
| 4468 | container_of(head, struct extent_buffer, rcu_head); |
| 4469 | |
| 4470 | __free_extent_buffer(eb); |
| 4471 | } |
| 4472 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4473 | /* Expects to have eb->eb_lock already held */ |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 4474 | static int release_extent_buffer(struct extent_buffer *eb) |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4475 | { |
| 4476 | WARN_ON(atomic_read(&eb->refs) == 0); |
| 4477 | if (atomic_dec_and_test(&eb->refs)) { |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4478 | if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) { |
| 4479 | spin_unlock(&eb->refs_lock); |
| 4480 | } else { |
| 4481 | struct extent_io_tree *tree = eb->tree; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4482 | |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4483 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4484 | |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4485 | spin_lock(&tree->buffer_lock); |
| 4486 | radix_tree_delete(&tree->buffer, |
| 4487 | eb->start >> PAGE_CACHE_SHIFT); |
| 4488 | spin_unlock(&tree->buffer_lock); |
| 4489 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4490 | |
| 4491 | /* Should be safe to release our pages at this point */ |
| 4492 | btrfs_release_extent_buffer_page(eb, 0); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4493 | call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 4494 | return 1; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4495 | } |
| 4496 | spin_unlock(&eb->refs_lock); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 4497 | |
| 4498 | return 0; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4499 | } |
| 4500 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4501 | void free_extent_buffer(struct extent_buffer *eb) |
| 4502 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4503 | int refs; |
| 4504 | int old; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4505 | if (!eb) |
| 4506 | return; |
| 4507 | |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4508 | while (1) { |
| 4509 | refs = atomic_read(&eb->refs); |
| 4510 | if (refs <= 3) |
| 4511 | break; |
| 4512 | old = atomic_cmpxchg(&eb->refs, refs, refs - 1); |
| 4513 | if (old == refs) |
| 4514 | return; |
| 4515 | } |
| 4516 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4517 | spin_lock(&eb->refs_lock); |
| 4518 | if (atomic_read(&eb->refs) == 2 && |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4519 | test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) |
| 4520 | atomic_dec(&eb->refs); |
| 4521 | |
| 4522 | if (atomic_read(&eb->refs) == 2 && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4523 | test_bit(EXTENT_BUFFER_STALE, &eb->bflags) && |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4524 | !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4525 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 4526 | atomic_dec(&eb->refs); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4527 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4528 | /* |
| 4529 | * I know this is terrible, but it's temporary until we stop tracking |
| 4530 | * the uptodate bits and such for the extent buffers. |
| 4531 | */ |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 4532 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4533 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4534 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4535 | void free_extent_buffer_stale(struct extent_buffer *eb) |
| 4536 | { |
| 4537 | if (!eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4538 | return; |
| 4539 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4540 | spin_lock(&eb->refs_lock); |
| 4541 | set_bit(EXTENT_BUFFER_STALE, &eb->bflags); |
| 4542 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4543 | if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4544 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 4545 | atomic_dec(&eb->refs); |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 4546 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4547 | } |
| 4548 | |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 4549 | void clear_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4550 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4551 | unsigned long i; |
| 4552 | unsigned long num_pages; |
| 4553 | struct page *page; |
| 4554 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4555 | num_pages = num_extent_pages(eb->start, eb->len); |
| 4556 | |
| 4557 | for (i = 0; i < num_pages; i++) { |
| 4558 | page = extent_buffer_page(eb, i); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4559 | if (!PageDirty(page)) |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4560 | continue; |
| 4561 | |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 4562 | lock_page(page); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 4563 | WARN_ON(!PagePrivate(page)); |
| 4564 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4565 | clear_page_dirty_for_io(page); |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 4566 | spin_lock_irq(&page->mapping->tree_lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4567 | if (!PageDirty(page)) { |
| 4568 | radix_tree_tag_clear(&page->mapping->page_tree, |
| 4569 | page_index(page), |
| 4570 | PAGECACHE_TAG_DIRTY); |
| 4571 | } |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 4572 | spin_unlock_irq(&page->mapping->tree_lock); |
Chris Mason | bf0da8c | 2011-11-04 12:29:37 -0400 | [diff] [blame] | 4573 | ClearPageError(page); |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 4574 | unlock_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4575 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4576 | WARN_ON(atomic_read(&eb->refs) == 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4577 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4578 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4579 | int set_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4580 | { |
| 4581 | unsigned long i; |
| 4582 | unsigned long num_pages; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4583 | int was_dirty = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4584 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4585 | check_buffer_tree_ref(eb); |
| 4586 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4587 | was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4588 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4589 | num_pages = num_extent_pages(eb->start, eb->len); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4590 | WARN_ON(atomic_read(&eb->refs) == 0); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4591 | WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)); |
| 4592 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4593 | for (i = 0; i < num_pages; i++) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4594 | set_page_dirty(extent_buffer_page(eb, i)); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4595 | return was_dirty; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4596 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4597 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4598 | int clear_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 4599 | { |
| 4600 | unsigned long i; |
| 4601 | struct page *page; |
| 4602 | unsigned long num_pages; |
| 4603 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4604 | clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4605 | num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 4606 | for (i = 0; i < num_pages; i++) { |
| 4607 | page = extent_buffer_page(eb, i); |
Chris Mason | 33958dc | 2008-07-30 10:29:12 -0400 | [diff] [blame] | 4608 | if (page) |
| 4609 | ClearPageUptodate(page); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 4610 | } |
| 4611 | return 0; |
| 4612 | } |
| 4613 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4614 | int set_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4615 | { |
| 4616 | unsigned long i; |
| 4617 | struct page *page; |
| 4618 | unsigned long num_pages; |
| 4619 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4620 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4621 | num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4622 | for (i = 0; i < num_pages; i++) { |
| 4623 | page = extent_buffer_page(eb, i); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4624 | SetPageUptodate(page); |
| 4625 | } |
| 4626 | return 0; |
| 4627 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4628 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4629 | int extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4630 | { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4631 | return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4632 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4633 | |
| 4634 | int read_extent_buffer_pages(struct extent_io_tree *tree, |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 4635 | struct extent_buffer *eb, u64 start, int wait, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4636 | get_extent_t *get_extent, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4637 | { |
| 4638 | unsigned long i; |
| 4639 | unsigned long start_i; |
| 4640 | struct page *page; |
| 4641 | int err; |
| 4642 | int ret = 0; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4643 | int locked_pages = 0; |
| 4644 | int all_uptodate = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4645 | unsigned long num_pages; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4646 | unsigned long num_reads = 0; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 4647 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4648 | unsigned long bio_flags = 0; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 4649 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4650 | if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4651 | return 0; |
| 4652 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4653 | if (start) { |
| 4654 | WARN_ON(start < eb->start); |
| 4655 | start_i = (start >> PAGE_CACHE_SHIFT) - |
| 4656 | (eb->start >> PAGE_CACHE_SHIFT); |
| 4657 | } else { |
| 4658 | start_i = 0; |
| 4659 | } |
| 4660 | |
| 4661 | num_pages = num_extent_pages(eb->start, eb->len); |
| 4662 | for (i = start_i; i < num_pages; i++) { |
| 4663 | page = extent_buffer_page(eb, i); |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 4664 | if (wait == WAIT_NONE) { |
David Woodhouse | 2db0496 | 2008-08-07 11:19:43 -0400 | [diff] [blame] | 4665 | if (!trylock_page(page)) |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4666 | goto unlock_exit; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4667 | } else { |
| 4668 | lock_page(page); |
| 4669 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4670 | locked_pages++; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4671 | if (!PageUptodate(page)) { |
| 4672 | num_reads++; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4673 | all_uptodate = 0; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4674 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4675 | } |
| 4676 | if (all_uptodate) { |
| 4677 | if (start_i == 0) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4678 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4679 | goto unlock_exit; |
| 4680 | } |
| 4681 | |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 4682 | clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags); |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 4683 | eb->read_mirror = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4684 | atomic_set(&eb->io_pages, num_reads); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4685 | for (i = start_i; i < num_pages; i++) { |
| 4686 | page = extent_buffer_page(eb, i); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4687 | if (!PageUptodate(page)) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4688 | ClearPageError(page); |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 4689 | err = __extent_read_full_page(tree, page, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4690 | get_extent, &bio, |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 4691 | mirror_num, &bio_flags, |
| 4692 | READ | REQ_META); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4693 | if (err) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4694 | ret = err; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4695 | } else { |
| 4696 | unlock_page(page); |
| 4697 | } |
| 4698 | } |
| 4699 | |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 4700 | if (bio) { |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 4701 | err = submit_one_bio(READ | REQ_META, bio, mirror_num, |
| 4702 | bio_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4703 | if (err) |
| 4704 | return err; |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 4705 | } |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 4706 | |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 4707 | if (ret || wait != WAIT_COMPLETE) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4708 | return ret; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4709 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4710 | for (i = start_i; i < num_pages; i++) { |
| 4711 | page = extent_buffer_page(eb, i); |
| 4712 | wait_on_page_locked(page); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4713 | if (!PageUptodate(page)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4714 | ret = -EIO; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4715 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4716 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4717 | return ret; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4718 | |
| 4719 | unlock_exit: |
| 4720 | i = start_i; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4721 | while (locked_pages > 0) { |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4722 | page = extent_buffer_page(eb, i); |
| 4723 | i++; |
| 4724 | unlock_page(page); |
| 4725 | locked_pages--; |
| 4726 | } |
| 4727 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4728 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4729 | |
| 4730 | void read_extent_buffer(struct extent_buffer *eb, void *dstv, |
| 4731 | unsigned long start, |
| 4732 | unsigned long len) |
| 4733 | { |
| 4734 | size_t cur; |
| 4735 | size_t offset; |
| 4736 | struct page *page; |
| 4737 | char *kaddr; |
| 4738 | char *dst = (char *)dstv; |
| 4739 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 4740 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4741 | |
| 4742 | WARN_ON(start > eb->len); |
| 4743 | WARN_ON(start + len > eb->start + eb->len); |
| 4744 | |
| 4745 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 4746 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4747 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4748 | page = extent_buffer_page(eb, i); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4749 | |
| 4750 | cur = min(len, (PAGE_CACHE_SIZE - offset)); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4751 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4752 | memcpy(dst, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4753 | |
| 4754 | dst += cur; |
| 4755 | len -= cur; |
| 4756 | offset = 0; |
| 4757 | i++; |
| 4758 | } |
| 4759 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4760 | |
| 4761 | int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start, |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4762 | unsigned long min_len, char **map, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4763 | unsigned long *map_start, |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4764 | unsigned long *map_len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4765 | { |
| 4766 | size_t offset = start & (PAGE_CACHE_SIZE - 1); |
| 4767 | char *kaddr; |
| 4768 | struct page *p; |
| 4769 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 4770 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 4771 | unsigned long end_i = (start_offset + start + min_len - 1) >> |
| 4772 | PAGE_CACHE_SHIFT; |
| 4773 | |
| 4774 | if (i != end_i) |
| 4775 | return -EINVAL; |
| 4776 | |
| 4777 | if (i == 0) { |
| 4778 | offset = start_offset; |
| 4779 | *map_start = 0; |
| 4780 | } else { |
| 4781 | offset = 0; |
| 4782 | *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset; |
| 4783 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4784 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4785 | if (start + min_len > eb->len) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4786 | WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, " |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4787 | "wanted %lu %lu\n", (unsigned long long)eb->start, |
| 4788 | eb->len, start, min_len); |
Josef Bacik | 85026533 | 2011-03-15 14:52:12 -0400 | [diff] [blame] | 4789 | return -EINVAL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4790 | } |
| 4791 | |
| 4792 | p = extent_buffer_page(eb, i); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4793 | kaddr = page_address(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4794 | *map = kaddr + offset; |
| 4795 | *map_len = PAGE_CACHE_SIZE - offset; |
| 4796 | return 0; |
| 4797 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4798 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4799 | int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv, |
| 4800 | unsigned long start, |
| 4801 | unsigned long len) |
| 4802 | { |
| 4803 | size_t cur; |
| 4804 | size_t offset; |
| 4805 | struct page *page; |
| 4806 | char *kaddr; |
| 4807 | char *ptr = (char *)ptrv; |
| 4808 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 4809 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 4810 | int ret = 0; |
| 4811 | |
| 4812 | WARN_ON(start > eb->len); |
| 4813 | WARN_ON(start + len > eb->start + eb->len); |
| 4814 | |
| 4815 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 4816 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4817 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4818 | page = extent_buffer_page(eb, i); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4819 | |
| 4820 | cur = min(len, (PAGE_CACHE_SIZE - offset)); |
| 4821 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4822 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4823 | ret = memcmp(ptr, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4824 | if (ret) |
| 4825 | break; |
| 4826 | |
| 4827 | ptr += cur; |
| 4828 | len -= cur; |
| 4829 | offset = 0; |
| 4830 | i++; |
| 4831 | } |
| 4832 | return ret; |
| 4833 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4834 | |
| 4835 | void write_extent_buffer(struct extent_buffer *eb, const void *srcv, |
| 4836 | unsigned long start, unsigned long len) |
| 4837 | { |
| 4838 | size_t cur; |
| 4839 | size_t offset; |
| 4840 | struct page *page; |
| 4841 | char *kaddr; |
| 4842 | char *src = (char *)srcv; |
| 4843 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 4844 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 4845 | |
| 4846 | WARN_ON(start > eb->len); |
| 4847 | WARN_ON(start + len > eb->start + eb->len); |
| 4848 | |
| 4849 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 4850 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4851 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4852 | page = extent_buffer_page(eb, i); |
| 4853 | WARN_ON(!PageUptodate(page)); |
| 4854 | |
| 4855 | cur = min(len, PAGE_CACHE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4856 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4857 | memcpy(kaddr + offset, src, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4858 | |
| 4859 | src += cur; |
| 4860 | len -= cur; |
| 4861 | offset = 0; |
| 4862 | i++; |
| 4863 | } |
| 4864 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4865 | |
| 4866 | void memset_extent_buffer(struct extent_buffer *eb, char c, |
| 4867 | unsigned long start, unsigned long len) |
| 4868 | { |
| 4869 | size_t cur; |
| 4870 | size_t offset; |
| 4871 | struct page *page; |
| 4872 | char *kaddr; |
| 4873 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 4874 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 4875 | |
| 4876 | WARN_ON(start > eb->len); |
| 4877 | WARN_ON(start + len > eb->start + eb->len); |
| 4878 | |
| 4879 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 4880 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4881 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4882 | page = extent_buffer_page(eb, i); |
| 4883 | WARN_ON(!PageUptodate(page)); |
| 4884 | |
| 4885 | cur = min(len, PAGE_CACHE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4886 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4887 | memset(kaddr + offset, c, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4888 | |
| 4889 | len -= cur; |
| 4890 | offset = 0; |
| 4891 | i++; |
| 4892 | } |
| 4893 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4894 | |
| 4895 | void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src, |
| 4896 | unsigned long dst_offset, unsigned long src_offset, |
| 4897 | unsigned long len) |
| 4898 | { |
| 4899 | u64 dst_len = dst->len; |
| 4900 | size_t cur; |
| 4901 | size_t offset; |
| 4902 | struct page *page; |
| 4903 | char *kaddr; |
| 4904 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 4905 | unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT; |
| 4906 | |
| 4907 | WARN_ON(src->len != dst_len); |
| 4908 | |
| 4909 | offset = (start_offset + dst_offset) & |
| 4910 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 4911 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4912 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4913 | page = extent_buffer_page(dst, i); |
| 4914 | WARN_ON(!PageUptodate(page)); |
| 4915 | |
| 4916 | cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset)); |
| 4917 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4918 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4919 | read_extent_buffer(src, kaddr + offset, src_offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4920 | |
| 4921 | src_offset += cur; |
| 4922 | len -= cur; |
| 4923 | offset = 0; |
| 4924 | i++; |
| 4925 | } |
| 4926 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4927 | |
| 4928 | static void move_pages(struct page *dst_page, struct page *src_page, |
| 4929 | unsigned long dst_off, unsigned long src_off, |
| 4930 | unsigned long len) |
| 4931 | { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4932 | char *dst_kaddr = page_address(dst_page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4933 | if (dst_page == src_page) { |
| 4934 | memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len); |
| 4935 | } else { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4936 | char *src_kaddr = page_address(src_page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4937 | char *p = dst_kaddr + dst_off + len; |
| 4938 | char *s = src_kaddr + src_off + len; |
| 4939 | |
| 4940 | while (len--) |
| 4941 | *--p = *--s; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4942 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4943 | } |
| 4944 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 4945 | static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len) |
| 4946 | { |
| 4947 | unsigned long distance = (src > dst) ? src - dst : dst - src; |
| 4948 | return distance < len; |
| 4949 | } |
| 4950 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4951 | static void copy_pages(struct page *dst_page, struct page *src_page, |
| 4952 | unsigned long dst_off, unsigned long src_off, |
| 4953 | unsigned long len) |
| 4954 | { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4955 | char *dst_kaddr = page_address(dst_page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4956 | char *src_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4957 | int must_memmove = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4958 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 4959 | if (dst_page != src_page) { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4960 | src_kaddr = page_address(src_page); |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 4961 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4962 | src_kaddr = dst_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4963 | if (areas_overlap(src_off, dst_off, len)) |
| 4964 | must_memmove = 1; |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 4965 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4966 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4967 | if (must_memmove) |
| 4968 | memmove(dst_kaddr + dst_off, src_kaddr + src_off, len); |
| 4969 | else |
| 4970 | memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4971 | } |
| 4972 | |
| 4973 | void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 4974 | unsigned long src_offset, unsigned long len) |
| 4975 | { |
| 4976 | size_t cur; |
| 4977 | size_t dst_off_in_page; |
| 4978 | size_t src_off_in_page; |
| 4979 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 4980 | unsigned long dst_i; |
| 4981 | unsigned long src_i; |
| 4982 | |
| 4983 | if (src_offset + len > dst->len) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4984 | printk(KERN_ERR "btrfs memmove bogus src_offset %lu move " |
| 4985 | "len %lu dst len %lu\n", src_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4986 | BUG_ON(1); |
| 4987 | } |
| 4988 | if (dst_offset + len > dst->len) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4989 | printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move " |
| 4990 | "len %lu dst len %lu\n", dst_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4991 | BUG_ON(1); |
| 4992 | } |
| 4993 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4994 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4995 | dst_off_in_page = (start_offset + dst_offset) & |
| 4996 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 4997 | src_off_in_page = (start_offset + src_offset) & |
| 4998 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 4999 | |
| 5000 | dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT; |
| 5001 | src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT; |
| 5002 | |
| 5003 | cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - |
| 5004 | src_off_in_page)); |
| 5005 | cur = min_t(unsigned long, cur, |
| 5006 | (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page)); |
| 5007 | |
| 5008 | copy_pages(extent_buffer_page(dst, dst_i), |
| 5009 | extent_buffer_page(dst, src_i), |
| 5010 | dst_off_in_page, src_off_in_page, cur); |
| 5011 | |
| 5012 | src_offset += cur; |
| 5013 | dst_offset += cur; |
| 5014 | len -= cur; |
| 5015 | } |
| 5016 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5017 | |
| 5018 | void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 5019 | unsigned long src_offset, unsigned long len) |
| 5020 | { |
| 5021 | size_t cur; |
| 5022 | size_t dst_off_in_page; |
| 5023 | size_t src_off_in_page; |
| 5024 | unsigned long dst_end = dst_offset + len - 1; |
| 5025 | unsigned long src_end = src_offset + len - 1; |
| 5026 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 5027 | unsigned long dst_i; |
| 5028 | unsigned long src_i; |
| 5029 | |
| 5030 | if (src_offset + len > dst->len) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5031 | printk(KERN_ERR "btrfs memmove bogus src_offset %lu move " |
| 5032 | "len %lu len %lu\n", src_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5033 | BUG_ON(1); |
| 5034 | } |
| 5035 | if (dst_offset + len > dst->len) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5036 | printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move " |
| 5037 | "len %lu len %lu\n", dst_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5038 | BUG_ON(1); |
| 5039 | } |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5040 | if (dst_offset < src_offset) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5041 | memcpy_extent_buffer(dst, dst_offset, src_offset, len); |
| 5042 | return; |
| 5043 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5044 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5045 | dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT; |
| 5046 | src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT; |
| 5047 | |
| 5048 | dst_off_in_page = (start_offset + dst_end) & |
| 5049 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 5050 | src_off_in_page = (start_offset + src_end) & |
| 5051 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 5052 | |
| 5053 | cur = min_t(unsigned long, len, src_off_in_page + 1); |
| 5054 | cur = min(cur, dst_off_in_page + 1); |
| 5055 | move_pages(extent_buffer_page(dst, dst_i), |
| 5056 | extent_buffer_page(dst, src_i), |
| 5057 | dst_off_in_page - cur + 1, |
| 5058 | src_off_in_page - cur + 1, cur); |
| 5059 | |
| 5060 | dst_end -= cur; |
| 5061 | src_end -= cur; |
| 5062 | len -= cur; |
| 5063 | } |
| 5064 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5065 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5066 | int try_release_extent_buffer(struct page *page) |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5067 | { |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5068 | struct extent_buffer *eb; |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5069 | |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5070 | /* |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5071 | * We need to make sure noboody is attaching this page to an eb right |
| 5072 | * now. |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5073 | */ |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5074 | spin_lock(&page->mapping->private_lock); |
| 5075 | if (!PagePrivate(page)) { |
| 5076 | spin_unlock(&page->mapping->private_lock); |
| 5077 | return 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5078 | } |
| 5079 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5080 | eb = (struct extent_buffer *)page->private; |
| 5081 | BUG_ON(!eb); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5082 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5083 | /* |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5084 | * This is a little awful but should be ok, we need to make sure that |
| 5085 | * the eb doesn't disappear out from under us while we're looking at |
| 5086 | * this page. |
| 5087 | */ |
| 5088 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5089 | if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5090 | spin_unlock(&eb->refs_lock); |
| 5091 | spin_unlock(&page->mapping->private_lock); |
| 5092 | return 0; |
| 5093 | } |
| 5094 | spin_unlock(&page->mapping->private_lock); |
| 5095 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5096 | /* |
| 5097 | * If tree ref isn't set then we know the ref on this eb is a real ref, |
| 5098 | * so just return, this page will likely be freed soon anyway. |
| 5099 | */ |
| 5100 | if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { |
| 5101 | spin_unlock(&eb->refs_lock); |
| 5102 | return 0; |
| 5103 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5104 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5105 | return release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5106 | } |