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