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