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