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