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> |
| 5 | #include <linux/gfp.h> |
| 6 | #include <linux/pagemap.h> |
| 7 | #include <linux/page-flags.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/spinlock.h> |
| 10 | #include <linux/blkdev.h> |
| 11 | #include <linux/swap.h> |
| 12 | #include <linux/version.h> |
| 13 | #include <linux/writeback.h> |
| 14 | #include <linux/pagevec.h> |
| 15 | #include "extent_io.h" |
| 16 | #include "extent_map.h" |
| 17 | |
| 18 | /* temporary define until extent_map moves out of btrfs */ |
| 19 | struct kmem_cache *btrfs_cache_create(const char *name, size_t size, |
| 20 | unsigned long extra_flags, |
| 21 | void (*ctor)(void *, struct kmem_cache *, |
| 22 | unsigned long)); |
| 23 | |
| 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 | 2d2ae54 | 2008-03-26 16:24:23 -0400 | [diff] [blame] | 29 | static spinlock_t leak_lock = SPIN_LOCK_UNLOCKED; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 30 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 31 | #define BUFFER_LRU_MAX 64 |
| 32 | |
| 33 | struct tree_entry { |
| 34 | u64 start; |
| 35 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 36 | struct rb_node rb_node; |
| 37 | }; |
| 38 | |
| 39 | struct extent_page_data { |
| 40 | struct bio *bio; |
| 41 | struct extent_io_tree *tree; |
| 42 | get_extent_t *get_extent; |
| 43 | }; |
| 44 | |
| 45 | int __init extent_io_init(void) |
| 46 | { |
| 47 | extent_state_cache = btrfs_cache_create("extent_state", |
| 48 | sizeof(struct extent_state), 0, |
| 49 | NULL); |
| 50 | if (!extent_state_cache) |
| 51 | return -ENOMEM; |
| 52 | |
| 53 | extent_buffer_cache = btrfs_cache_create("extent_buffers", |
| 54 | sizeof(struct extent_buffer), 0, |
| 55 | NULL); |
| 56 | if (!extent_buffer_cache) |
| 57 | goto free_state_cache; |
| 58 | return 0; |
| 59 | |
| 60 | free_state_cache: |
| 61 | kmem_cache_destroy(extent_state_cache); |
| 62 | return -ENOMEM; |
| 63 | } |
| 64 | |
| 65 | void extent_io_exit(void) |
| 66 | { |
| 67 | struct extent_state *state; |
Chris Mason | 2d2ae54 | 2008-03-26 16:24:23 -0400 | [diff] [blame] | 68 | struct extent_buffer *eb; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 69 | |
| 70 | while (!list_empty(&states)) { |
Chris Mason | 2d2ae54 | 2008-03-26 16:24:23 -0400 | [diff] [blame] | 71 | state = list_entry(states.next, struct extent_state, leak_list); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 72 | printk("state leak: start %Lu end %Lu state %lu in tree %p refs %d\n", state->start, state->end, state->state, state->tree, atomic_read(&state->refs)); |
Chris Mason | 2d2ae54 | 2008-03-26 16:24:23 -0400 | [diff] [blame] | 73 | list_del(&state->leak_list); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 74 | kmem_cache_free(extent_state_cache, state); |
| 75 | |
| 76 | } |
| 77 | |
Chris Mason | 2d2ae54 | 2008-03-26 16:24:23 -0400 | [diff] [blame] | 78 | while (!list_empty(&buffers)) { |
| 79 | eb = list_entry(buffers.next, struct extent_buffer, leak_list); |
| 80 | printk("buffer leak start %Lu len %lu refs %d\n", eb->start, eb->len, atomic_read(&eb->refs)); |
| 81 | list_del(&eb->leak_list); |
| 82 | kmem_cache_free(extent_buffer_cache, eb); |
| 83 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 84 | if (extent_state_cache) |
| 85 | kmem_cache_destroy(extent_state_cache); |
| 86 | if (extent_buffer_cache) |
| 87 | kmem_cache_destroy(extent_buffer_cache); |
| 88 | } |
| 89 | |
| 90 | void extent_io_tree_init(struct extent_io_tree *tree, |
| 91 | struct address_space *mapping, gfp_t mask) |
| 92 | { |
| 93 | tree->state.rb_node = NULL; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 94 | tree->buffer.rb_node = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 95 | tree->ops = NULL; |
| 96 | tree->dirty_bytes = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 97 | spin_lock_init(&tree->lock); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 98 | spin_lock_init(&tree->buffer_lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 99 | tree->mapping = mapping; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 100 | } |
| 101 | EXPORT_SYMBOL(extent_io_tree_init); |
| 102 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 103 | struct extent_state *alloc_extent_state(gfp_t mask) |
| 104 | { |
| 105 | struct extent_state *state; |
Chris Mason | 2d2ae54 | 2008-03-26 16:24:23 -0400 | [diff] [blame] | 106 | unsigned long flags; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 107 | |
| 108 | state = kmem_cache_alloc(extent_state_cache, mask); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 109 | if (!state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 110 | return state; |
| 111 | state->state = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 112 | state->private = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 113 | state->tree = NULL; |
Chris Mason | 2d2ae54 | 2008-03-26 16:24:23 -0400 | [diff] [blame] | 114 | spin_lock_irqsave(&leak_lock, flags); |
| 115 | list_add(&state->leak_list, &states); |
| 116 | spin_unlock_irqrestore(&leak_lock, flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 117 | |
| 118 | atomic_set(&state->refs, 1); |
| 119 | init_waitqueue_head(&state->wq); |
| 120 | return state; |
| 121 | } |
| 122 | EXPORT_SYMBOL(alloc_extent_state); |
| 123 | |
| 124 | void free_extent_state(struct extent_state *state) |
| 125 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 126 | if (!state) |
| 127 | return; |
| 128 | if (atomic_dec_and_test(&state->refs)) { |
Chris Mason | 2d2ae54 | 2008-03-26 16:24:23 -0400 | [diff] [blame] | 129 | unsigned long flags; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 130 | WARN_ON(state->tree); |
Chris Mason | 2d2ae54 | 2008-03-26 16:24:23 -0400 | [diff] [blame] | 131 | spin_lock_irqsave(&leak_lock, flags); |
| 132 | list_del(&state->leak_list); |
| 133 | spin_unlock_irqrestore(&leak_lock, flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 134 | kmem_cache_free(extent_state_cache, state); |
| 135 | } |
| 136 | } |
| 137 | EXPORT_SYMBOL(free_extent_state); |
| 138 | |
| 139 | static struct rb_node *tree_insert(struct rb_root *root, u64 offset, |
| 140 | struct rb_node *node) |
| 141 | { |
| 142 | struct rb_node ** p = &root->rb_node; |
| 143 | struct rb_node * parent = NULL; |
| 144 | struct tree_entry *entry; |
| 145 | |
| 146 | while(*p) { |
| 147 | parent = *p; |
| 148 | entry = rb_entry(parent, struct tree_entry, rb_node); |
| 149 | |
| 150 | if (offset < entry->start) |
| 151 | p = &(*p)->rb_left; |
| 152 | else if (offset > entry->end) |
| 153 | p = &(*p)->rb_right; |
| 154 | else |
| 155 | return parent; |
| 156 | } |
| 157 | |
| 158 | entry = rb_entry(node, struct tree_entry, rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 159 | rb_link_node(node, parent, p); |
| 160 | rb_insert_color(node, root); |
| 161 | return NULL; |
| 162 | } |
| 163 | |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 164 | 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] | 165 | struct rb_node **prev_ret, |
| 166 | struct rb_node **next_ret) |
| 167 | { |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 168 | struct rb_root *root = &tree->state; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 169 | struct rb_node * n = root->rb_node; |
| 170 | struct rb_node *prev = NULL; |
| 171 | struct rb_node *orig_prev = NULL; |
| 172 | struct tree_entry *entry; |
| 173 | struct tree_entry *prev_entry = NULL; |
| 174 | |
| 175 | while(n) { |
| 176 | entry = rb_entry(n, struct tree_entry, rb_node); |
| 177 | prev = n; |
| 178 | prev_entry = entry; |
| 179 | |
| 180 | if (offset < entry->start) |
| 181 | n = n->rb_left; |
| 182 | else if (offset > entry->end) |
| 183 | n = n->rb_right; |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 184 | else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 185 | return n; |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 186 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | if (prev_ret) { |
| 190 | orig_prev = prev; |
| 191 | while(prev && offset > prev_entry->end) { |
| 192 | prev = rb_next(prev); |
| 193 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| 194 | } |
| 195 | *prev_ret = prev; |
| 196 | prev = orig_prev; |
| 197 | } |
| 198 | |
| 199 | if (next_ret) { |
| 200 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| 201 | while(prev && offset < prev_entry->start) { |
| 202 | prev = rb_prev(prev); |
| 203 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| 204 | } |
| 205 | *next_ret = prev; |
| 206 | } |
| 207 | return NULL; |
| 208 | } |
| 209 | |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 210 | static inline struct rb_node *tree_search(struct extent_io_tree *tree, |
| 211 | u64 offset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 212 | { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 213 | struct rb_node *prev = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 214 | struct rb_node *ret; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 215 | |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 216 | ret = __etree_search(tree, offset, &prev, NULL); |
| 217 | if (!ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 218 | return prev; |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 219 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 220 | return ret; |
| 221 | } |
| 222 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 223 | static struct extent_buffer *buffer_tree_insert(struct extent_io_tree *tree, |
| 224 | u64 offset, struct rb_node *node) |
| 225 | { |
| 226 | struct rb_root *root = &tree->buffer; |
| 227 | struct rb_node ** p = &root->rb_node; |
| 228 | struct rb_node * parent = NULL; |
| 229 | struct extent_buffer *eb; |
| 230 | |
| 231 | while(*p) { |
| 232 | parent = *p; |
| 233 | eb = rb_entry(parent, struct extent_buffer, rb_node); |
| 234 | |
| 235 | if (offset < eb->start) |
| 236 | p = &(*p)->rb_left; |
| 237 | else if (offset > eb->start) |
| 238 | p = &(*p)->rb_right; |
| 239 | else |
| 240 | return eb; |
| 241 | } |
| 242 | |
| 243 | rb_link_node(node, parent, p); |
| 244 | rb_insert_color(node, root); |
| 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | static struct extent_buffer *buffer_search(struct extent_io_tree *tree, |
| 249 | u64 offset) |
| 250 | { |
| 251 | struct rb_root *root = &tree->buffer; |
| 252 | struct rb_node * n = root->rb_node; |
| 253 | struct extent_buffer *eb; |
| 254 | |
| 255 | while(n) { |
| 256 | eb = rb_entry(n, struct extent_buffer, rb_node); |
| 257 | if (offset < eb->start) |
| 258 | n = n->rb_left; |
| 259 | else if (offset > eb->start) |
| 260 | n = n->rb_right; |
| 261 | else |
| 262 | return eb; |
| 263 | } |
| 264 | return NULL; |
| 265 | } |
| 266 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 267 | /* |
| 268 | * utility function to look for merge candidates inside a given range. |
| 269 | * Any extents with matching state are merged together into a single |
| 270 | * extent in the tree. Extents with EXTENT_IO in their state field |
| 271 | * are not merged because the end_io handlers need to be able to do |
| 272 | * operations on them without sleeping (or doing allocations/splits). |
| 273 | * |
| 274 | * This should be called with the tree lock held. |
| 275 | */ |
| 276 | static int merge_state(struct extent_io_tree *tree, |
| 277 | struct extent_state *state) |
| 278 | { |
| 279 | struct extent_state *other; |
| 280 | struct rb_node *other_node; |
| 281 | |
| 282 | if (state->state & EXTENT_IOBITS) |
| 283 | return 0; |
| 284 | |
| 285 | other_node = rb_prev(&state->rb_node); |
| 286 | if (other_node) { |
| 287 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 288 | if (other->end == state->start - 1 && |
| 289 | other->state == state->state) { |
| 290 | state->start = other->start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 291 | other->tree = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 292 | rb_erase(&other->rb_node, &tree->state); |
| 293 | free_extent_state(other); |
| 294 | } |
| 295 | } |
| 296 | other_node = rb_next(&state->rb_node); |
| 297 | if (other_node) { |
| 298 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 299 | if (other->start == state->end + 1 && |
| 300 | other->state == state->state) { |
| 301 | other->start = state->start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 302 | state->tree = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 303 | rb_erase(&state->rb_node, &tree->state); |
| 304 | free_extent_state(state); |
| 305 | } |
| 306 | } |
| 307 | return 0; |
| 308 | } |
| 309 | |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 310 | static void set_state_cb(struct extent_io_tree *tree, |
| 311 | struct extent_state *state, |
| 312 | unsigned long bits) |
| 313 | { |
| 314 | if (tree->ops && tree->ops->set_bit_hook) { |
| 315 | tree->ops->set_bit_hook(tree->mapping->host, state->start, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 316 | state->end, state->state, bits); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
| 320 | static void clear_state_cb(struct extent_io_tree *tree, |
| 321 | struct extent_state *state, |
| 322 | unsigned long bits) |
| 323 | { |
| 324 | if (tree->ops && tree->ops->set_bit_hook) { |
| 325 | tree->ops->clear_bit_hook(tree->mapping->host, state->start, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 326 | state->end, state->state, bits); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 330 | /* |
| 331 | * insert an extent_state struct into the tree. 'bits' are set on the |
| 332 | * struct before it is inserted. |
| 333 | * |
| 334 | * This may return -EEXIST if the extent is already there, in which case the |
| 335 | * state struct is freed. |
| 336 | * |
| 337 | * The tree lock is not taken internally. This is a utility function and |
| 338 | * probably isn't what you want to call (see set/clear_extent_bit). |
| 339 | */ |
| 340 | static int insert_state(struct extent_io_tree *tree, |
| 341 | struct extent_state *state, u64 start, u64 end, |
| 342 | int bits) |
| 343 | { |
| 344 | struct rb_node *node; |
| 345 | |
| 346 | if (end < start) { |
| 347 | printk("end < start %Lu %Lu\n", end, start); |
| 348 | WARN_ON(1); |
| 349 | } |
| 350 | if (bits & EXTENT_DIRTY) |
| 351 | tree->dirty_bytes += end - start + 1; |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 352 | set_state_cb(tree, state, bits); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 353 | state->state |= bits; |
| 354 | state->start = start; |
| 355 | state->end = end; |
| 356 | node = tree_insert(&tree->state, end, &state->rb_node); |
| 357 | if (node) { |
| 358 | struct extent_state *found; |
| 359 | found = rb_entry(node, struct extent_state, rb_node); |
| 360 | printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, start, end); |
| 361 | free_extent_state(state); |
| 362 | return -EEXIST; |
| 363 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 364 | state->tree = tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 365 | merge_state(tree, state); |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * split a given extent state struct in two, inserting the preallocated |
| 371 | * struct 'prealloc' as the newly created second half. 'split' indicates an |
| 372 | * offset inside 'orig' where it should be split. |
| 373 | * |
| 374 | * Before calling, |
| 375 | * the tree has 'orig' at [orig->start, orig->end]. After calling, there |
| 376 | * are two extent state structs in the tree: |
| 377 | * prealloc: [orig->start, split - 1] |
| 378 | * orig: [ split, orig->end ] |
| 379 | * |
| 380 | * The tree locks are not taken by this function. They need to be held |
| 381 | * by the caller. |
| 382 | */ |
| 383 | static int split_state(struct extent_io_tree *tree, struct extent_state *orig, |
| 384 | struct extent_state *prealloc, u64 split) |
| 385 | { |
| 386 | struct rb_node *node; |
| 387 | prealloc->start = orig->start; |
| 388 | prealloc->end = split - 1; |
| 389 | prealloc->state = orig->state; |
| 390 | orig->start = split; |
| 391 | |
| 392 | node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node); |
| 393 | if (node) { |
| 394 | struct extent_state *found; |
| 395 | found = rb_entry(node, struct extent_state, rb_node); |
| 396 | printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, prealloc->start, prealloc->end); |
| 397 | free_extent_state(prealloc); |
| 398 | return -EEXIST; |
| 399 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 400 | prealloc->tree = tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * utility function to clear some bits in an extent state struct. |
| 406 | * it will optionally wake up any one waiting on this state (wake == 1), or |
| 407 | * forcibly remove the state from the tree (delete == 1). |
| 408 | * |
| 409 | * If no bits are set on the state struct after clearing things, the |
| 410 | * struct is freed and removed from the tree |
| 411 | */ |
| 412 | static int clear_state_bit(struct extent_io_tree *tree, |
| 413 | struct extent_state *state, int bits, int wake, |
| 414 | int delete) |
| 415 | { |
| 416 | int ret = state->state & bits; |
| 417 | |
| 418 | if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) { |
| 419 | u64 range = state->end - state->start + 1; |
| 420 | WARN_ON(range > tree->dirty_bytes); |
| 421 | tree->dirty_bytes -= range; |
| 422 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 423 | clear_state_cb(tree, state, bits); |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 424 | state->state &= ~bits; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 425 | if (wake) |
| 426 | wake_up(&state->wq); |
| 427 | if (delete || state->state == 0) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 428 | if (state->tree) { |
Chris Mason | ae9d128 | 2008-02-01 15:42:15 -0500 | [diff] [blame] | 429 | clear_state_cb(tree, state, state->state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 430 | rb_erase(&state->rb_node, &tree->state); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 431 | state->tree = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 432 | free_extent_state(state); |
| 433 | } else { |
| 434 | WARN_ON(1); |
| 435 | } |
| 436 | } else { |
| 437 | merge_state(tree, state); |
| 438 | } |
| 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * clear some bits on a range in the tree. This may require splitting |
| 444 | * or inserting elements in the tree, so the gfp mask is used to |
| 445 | * indicate which allocations or sleeping are allowed. |
| 446 | * |
| 447 | * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove |
| 448 | * the given range from the tree regardless of state (ie for truncate). |
| 449 | * |
| 450 | * the range [start, end] is inclusive. |
| 451 | * |
| 452 | * This takes the tree lock, and returns < 0 on error, > 0 if any of the |
| 453 | * bits were already set, or zero if none of the bits were already set. |
| 454 | */ |
| 455 | int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 456 | int bits, int wake, int delete, gfp_t mask) |
| 457 | { |
| 458 | struct extent_state *state; |
| 459 | struct extent_state *prealloc = NULL; |
| 460 | struct rb_node *node; |
| 461 | unsigned long flags; |
| 462 | int err; |
| 463 | int set = 0; |
| 464 | |
| 465 | again: |
| 466 | if (!prealloc && (mask & __GFP_WAIT)) { |
| 467 | prealloc = alloc_extent_state(mask); |
| 468 | if (!prealloc) |
| 469 | return -ENOMEM; |
| 470 | } |
| 471 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 472 | spin_lock_irqsave(&tree->lock, flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 473 | /* |
| 474 | * this search will find the extents that end after |
| 475 | * our range starts |
| 476 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 477 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 478 | if (!node) |
| 479 | goto out; |
| 480 | state = rb_entry(node, struct extent_state, rb_node); |
| 481 | if (state->start > end) |
| 482 | goto out; |
| 483 | WARN_ON(state->end < start); |
| 484 | |
| 485 | /* |
| 486 | * | ---- desired range ---- | |
| 487 | * | state | or |
| 488 | * | ------------- state -------------- | |
| 489 | * |
| 490 | * We need to split the extent we found, and may flip |
| 491 | * bits on second half. |
| 492 | * |
| 493 | * If the extent we found extends past our range, we |
| 494 | * just split and search again. It'll get split again |
| 495 | * the next time though. |
| 496 | * |
| 497 | * If the extent we found is inside our range, we clear |
| 498 | * the desired bit on it. |
| 499 | */ |
| 500 | |
| 501 | if (state->start < start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 502 | if (!prealloc) |
| 503 | prealloc = alloc_extent_state(GFP_ATOMIC); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 504 | err = split_state(tree, state, prealloc, start); |
| 505 | BUG_ON(err == -EEXIST); |
| 506 | prealloc = NULL; |
| 507 | if (err) |
| 508 | goto out; |
| 509 | if (state->end <= end) { |
| 510 | start = state->end + 1; |
| 511 | set |= clear_state_bit(tree, state, bits, |
| 512 | wake, delete); |
| 513 | } else { |
| 514 | start = state->start; |
| 515 | } |
| 516 | goto search_again; |
| 517 | } |
| 518 | /* |
| 519 | * | ---- desired range ---- | |
| 520 | * | state | |
| 521 | * We need to split the extent, and clear the bit |
| 522 | * on the first half |
| 523 | */ |
| 524 | if (state->start <= end && state->end > end) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 525 | if (!prealloc) |
| 526 | prealloc = alloc_extent_state(GFP_ATOMIC); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 527 | err = split_state(tree, state, prealloc, end + 1); |
| 528 | BUG_ON(err == -EEXIST); |
| 529 | |
| 530 | if (wake) |
| 531 | wake_up(&state->wq); |
| 532 | set |= clear_state_bit(tree, prealloc, bits, |
| 533 | wake, delete); |
| 534 | prealloc = NULL; |
| 535 | goto out; |
| 536 | } |
| 537 | |
| 538 | start = state->end + 1; |
| 539 | set |= clear_state_bit(tree, state, bits, wake, delete); |
| 540 | goto search_again; |
| 541 | |
| 542 | out: |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 543 | spin_unlock_irqrestore(&tree->lock, flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 544 | if (prealloc) |
| 545 | free_extent_state(prealloc); |
| 546 | |
| 547 | return set; |
| 548 | |
| 549 | search_again: |
| 550 | if (start > end) |
| 551 | goto out; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 552 | spin_unlock_irqrestore(&tree->lock, flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 553 | if (mask & __GFP_WAIT) |
| 554 | cond_resched(); |
| 555 | goto again; |
| 556 | } |
| 557 | EXPORT_SYMBOL(clear_extent_bit); |
| 558 | |
| 559 | static int wait_on_state(struct extent_io_tree *tree, |
| 560 | struct extent_state *state) |
| 561 | { |
| 562 | DEFINE_WAIT(wait); |
| 563 | prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 564 | spin_unlock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 565 | schedule(); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 566 | spin_lock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 567 | finish_wait(&state->wq, &wait); |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * waits for one or more bits to clear on a range in the state tree. |
| 573 | * The range [start, end] is inclusive. |
| 574 | * The tree lock is taken by this function |
| 575 | */ |
| 576 | int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits) |
| 577 | { |
| 578 | struct extent_state *state; |
| 579 | struct rb_node *node; |
| 580 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 581 | spin_lock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 582 | again: |
| 583 | while (1) { |
| 584 | /* |
| 585 | * this search will find all the extents that end after |
| 586 | * our range starts |
| 587 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 588 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 589 | if (!node) |
| 590 | break; |
| 591 | |
| 592 | state = rb_entry(node, struct extent_state, rb_node); |
| 593 | |
| 594 | if (state->start > end) |
| 595 | goto out; |
| 596 | |
| 597 | if (state->state & bits) { |
| 598 | start = state->start; |
| 599 | atomic_inc(&state->refs); |
| 600 | wait_on_state(tree, state); |
| 601 | free_extent_state(state); |
| 602 | goto again; |
| 603 | } |
| 604 | start = state->end + 1; |
| 605 | |
| 606 | if (start > end) |
| 607 | break; |
| 608 | |
| 609 | if (need_resched()) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 610 | spin_unlock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 611 | cond_resched(); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 612 | spin_lock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | out: |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 616 | spin_unlock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 617 | return 0; |
| 618 | } |
| 619 | EXPORT_SYMBOL(wait_extent_bit); |
| 620 | |
| 621 | static void set_state_bits(struct extent_io_tree *tree, |
| 622 | struct extent_state *state, |
| 623 | int bits) |
| 624 | { |
| 625 | if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) { |
| 626 | u64 range = state->end - state->start + 1; |
| 627 | tree->dirty_bytes += range; |
| 628 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 629 | set_state_cb(tree, state, bits); |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 630 | state->state |= bits; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /* |
| 634 | * set some bits on a range in the tree. This may require allocations |
| 635 | * or sleeping, so the gfp mask is used to indicate what is allowed. |
| 636 | * |
| 637 | * If 'exclusive' == 1, this will fail with -EEXIST if some part of the |
| 638 | * range already has the desired bits set. The start of the existing |
| 639 | * range is returned in failed_start in this case. |
| 640 | * |
| 641 | * [start, end] is inclusive |
| 642 | * This takes the tree lock. |
| 643 | */ |
| 644 | int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits, |
| 645 | int exclusive, u64 *failed_start, gfp_t mask) |
| 646 | { |
| 647 | struct extent_state *state; |
| 648 | struct extent_state *prealloc = NULL; |
| 649 | struct rb_node *node; |
| 650 | unsigned long flags; |
| 651 | int err = 0; |
| 652 | int set; |
| 653 | u64 last_start; |
| 654 | u64 last_end; |
| 655 | again: |
| 656 | if (!prealloc && (mask & __GFP_WAIT)) { |
| 657 | prealloc = alloc_extent_state(mask); |
| 658 | if (!prealloc) |
| 659 | return -ENOMEM; |
| 660 | } |
| 661 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 662 | spin_lock_irqsave(&tree->lock, flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 663 | /* |
| 664 | * this search will find all the extents that end after |
| 665 | * our range starts. |
| 666 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 667 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 668 | if (!node) { |
| 669 | err = insert_state(tree, prealloc, start, end, bits); |
| 670 | prealloc = NULL; |
| 671 | BUG_ON(err == -EEXIST); |
| 672 | goto out; |
| 673 | } |
| 674 | |
| 675 | state = rb_entry(node, struct extent_state, rb_node); |
| 676 | last_start = state->start; |
| 677 | last_end = state->end; |
| 678 | |
| 679 | /* |
| 680 | * | ---- desired range ---- | |
| 681 | * | state | |
| 682 | * |
| 683 | * Just lock what we found and keep going |
| 684 | */ |
| 685 | if (state->start == start && state->end <= end) { |
| 686 | set = state->state & bits; |
| 687 | if (set && exclusive) { |
| 688 | *failed_start = state->start; |
| 689 | err = -EEXIST; |
| 690 | goto out; |
| 691 | } |
| 692 | set_state_bits(tree, state, bits); |
| 693 | start = state->end + 1; |
| 694 | merge_state(tree, state); |
| 695 | goto search_again; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * | ---- desired range ---- | |
| 700 | * | state | |
| 701 | * or |
| 702 | * | ------------- state -------------- | |
| 703 | * |
| 704 | * We need to split the extent we found, and may flip bits on |
| 705 | * second half. |
| 706 | * |
| 707 | * If the extent we found extends past our |
| 708 | * range, we just split and search again. It'll get split |
| 709 | * again the next time though. |
| 710 | * |
| 711 | * If the extent we found is inside our range, we set the |
| 712 | * desired bit on it. |
| 713 | */ |
| 714 | if (state->start < start) { |
| 715 | set = state->state & bits; |
| 716 | if (exclusive && set) { |
| 717 | *failed_start = start; |
| 718 | err = -EEXIST; |
| 719 | goto out; |
| 720 | } |
| 721 | err = split_state(tree, state, prealloc, start); |
| 722 | BUG_ON(err == -EEXIST); |
| 723 | prealloc = NULL; |
| 724 | if (err) |
| 725 | goto out; |
| 726 | if (state->end <= end) { |
| 727 | set_state_bits(tree, state, bits); |
| 728 | start = state->end + 1; |
| 729 | merge_state(tree, state); |
| 730 | } else { |
| 731 | start = state->start; |
| 732 | } |
| 733 | goto search_again; |
| 734 | } |
| 735 | /* |
| 736 | * | ---- desired range ---- | |
| 737 | * | state | or | state | |
| 738 | * |
| 739 | * There's a hole, we need to insert something in it and |
| 740 | * ignore the extent we found. |
| 741 | */ |
| 742 | if (state->start > start) { |
| 743 | u64 this_end; |
| 744 | if (end < last_start) |
| 745 | this_end = end; |
| 746 | else |
| 747 | this_end = last_start -1; |
| 748 | err = insert_state(tree, prealloc, start, this_end, |
| 749 | bits); |
| 750 | prealloc = NULL; |
| 751 | BUG_ON(err == -EEXIST); |
| 752 | if (err) |
| 753 | goto out; |
| 754 | start = this_end + 1; |
| 755 | goto search_again; |
| 756 | } |
| 757 | /* |
| 758 | * | ---- desired range ---- | |
| 759 | * | state | |
| 760 | * We need to split the extent, and set the bit |
| 761 | * on the first half |
| 762 | */ |
| 763 | if (state->start <= end && state->end > end) { |
| 764 | set = state->state & bits; |
| 765 | if (exclusive && set) { |
| 766 | *failed_start = start; |
| 767 | err = -EEXIST; |
| 768 | goto out; |
| 769 | } |
| 770 | err = split_state(tree, state, prealloc, end + 1); |
| 771 | BUG_ON(err == -EEXIST); |
| 772 | |
| 773 | set_state_bits(tree, prealloc, bits); |
| 774 | merge_state(tree, prealloc); |
| 775 | prealloc = NULL; |
| 776 | goto out; |
| 777 | } |
| 778 | |
| 779 | goto search_again; |
| 780 | |
| 781 | out: |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 782 | spin_unlock_irqrestore(&tree->lock, flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 783 | if (prealloc) |
| 784 | free_extent_state(prealloc); |
| 785 | |
| 786 | return err; |
| 787 | |
| 788 | search_again: |
| 789 | if (start > end) |
| 790 | goto out; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 791 | spin_unlock_irqrestore(&tree->lock, flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 792 | if (mask & __GFP_WAIT) |
| 793 | cond_resched(); |
| 794 | goto again; |
| 795 | } |
| 796 | EXPORT_SYMBOL(set_extent_bit); |
| 797 | |
| 798 | /* wrappers around set/clear extent bit */ |
| 799 | int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, |
| 800 | gfp_t mask) |
| 801 | { |
| 802 | return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL, |
| 803 | mask); |
| 804 | } |
| 805 | EXPORT_SYMBOL(set_extent_dirty); |
| 806 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 807 | int set_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end, |
| 808 | gfp_t mask) |
| 809 | { |
| 810 | return set_extent_bit(tree, start, end, EXTENT_ORDERED, 0, NULL, mask); |
| 811 | } |
| 812 | EXPORT_SYMBOL(set_extent_ordered); |
| 813 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 814 | int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
| 815 | int bits, gfp_t mask) |
| 816 | { |
| 817 | return set_extent_bit(tree, start, end, bits, 0, NULL, |
| 818 | mask); |
| 819 | } |
| 820 | EXPORT_SYMBOL(set_extent_bits); |
| 821 | |
| 822 | int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
| 823 | int bits, gfp_t mask) |
| 824 | { |
| 825 | return clear_extent_bit(tree, start, end, bits, 0, 0, mask); |
| 826 | } |
| 827 | EXPORT_SYMBOL(clear_extent_bits); |
| 828 | |
| 829 | int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end, |
| 830 | gfp_t mask) |
| 831 | { |
| 832 | return set_extent_bit(tree, start, end, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 833 | EXTENT_DELALLOC | EXTENT_DIRTY, |
| 834 | 0, NULL, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 835 | } |
| 836 | EXPORT_SYMBOL(set_extent_delalloc); |
| 837 | |
| 838 | int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, |
| 839 | gfp_t mask) |
| 840 | { |
| 841 | return clear_extent_bit(tree, start, end, |
| 842 | EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask); |
| 843 | } |
| 844 | EXPORT_SYMBOL(clear_extent_dirty); |
| 845 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 846 | int clear_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end, |
| 847 | gfp_t mask) |
| 848 | { |
| 849 | return clear_extent_bit(tree, start, end, EXTENT_ORDERED, 1, 0, mask); |
| 850 | } |
| 851 | EXPORT_SYMBOL(clear_extent_ordered); |
| 852 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 853 | int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end, |
| 854 | gfp_t mask) |
| 855 | { |
| 856 | return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL, |
| 857 | mask); |
| 858 | } |
| 859 | EXPORT_SYMBOL(set_extent_new); |
| 860 | |
| 861 | int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end, |
| 862 | gfp_t mask) |
| 863 | { |
| 864 | return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask); |
| 865 | } |
| 866 | EXPORT_SYMBOL(clear_extent_new); |
| 867 | |
| 868 | int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end, |
| 869 | gfp_t mask) |
| 870 | { |
| 871 | return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL, |
| 872 | mask); |
| 873 | } |
| 874 | EXPORT_SYMBOL(set_extent_uptodate); |
| 875 | |
| 876 | int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end, |
| 877 | gfp_t mask) |
| 878 | { |
| 879 | return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask); |
| 880 | } |
| 881 | EXPORT_SYMBOL(clear_extent_uptodate); |
| 882 | |
| 883 | int set_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end, |
| 884 | gfp_t mask) |
| 885 | { |
| 886 | return set_extent_bit(tree, start, end, EXTENT_WRITEBACK, |
| 887 | 0, NULL, mask); |
| 888 | } |
| 889 | EXPORT_SYMBOL(set_extent_writeback); |
| 890 | |
| 891 | int clear_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end, |
| 892 | gfp_t mask) |
| 893 | { |
| 894 | return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask); |
| 895 | } |
| 896 | EXPORT_SYMBOL(clear_extent_writeback); |
| 897 | |
| 898 | int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end) |
| 899 | { |
| 900 | return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK); |
| 901 | } |
| 902 | EXPORT_SYMBOL(wait_on_extent_writeback); |
| 903 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 904 | int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask) |
| 905 | { |
| 906 | int err; |
| 907 | u64 failed_start; |
| 908 | while (1) { |
| 909 | err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1, |
| 910 | &failed_start, mask); |
| 911 | if (err == -EEXIST && (mask & __GFP_WAIT)) { |
| 912 | wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED); |
| 913 | start = failed_start; |
| 914 | } else { |
| 915 | break; |
| 916 | } |
| 917 | WARN_ON(start > end); |
| 918 | } |
| 919 | return err; |
| 920 | } |
| 921 | EXPORT_SYMBOL(lock_extent); |
| 922 | |
| 923 | int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, |
| 924 | gfp_t mask) |
| 925 | { |
| 926 | return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask); |
| 927 | } |
| 928 | EXPORT_SYMBOL(unlock_extent); |
| 929 | |
| 930 | /* |
| 931 | * helper function to set pages and extents in the tree dirty |
| 932 | */ |
| 933 | int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end) |
| 934 | { |
| 935 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 936 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 937 | struct page *page; |
| 938 | |
| 939 | while (index <= end_index) { |
| 940 | page = find_get_page(tree->mapping, index); |
| 941 | BUG_ON(!page); |
| 942 | __set_page_dirty_nobuffers(page); |
| 943 | page_cache_release(page); |
| 944 | index++; |
| 945 | } |
| 946 | set_extent_dirty(tree, start, end, GFP_NOFS); |
| 947 | return 0; |
| 948 | } |
| 949 | EXPORT_SYMBOL(set_range_dirty); |
| 950 | |
| 951 | /* |
| 952 | * helper function to set both pages and extents in the tree writeback |
| 953 | */ |
| 954 | int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end) |
| 955 | { |
| 956 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 957 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 958 | struct page *page; |
| 959 | |
| 960 | while (index <= end_index) { |
| 961 | page = find_get_page(tree->mapping, index); |
| 962 | BUG_ON(!page); |
| 963 | set_page_writeback(page); |
| 964 | page_cache_release(page); |
| 965 | index++; |
| 966 | } |
| 967 | set_extent_writeback(tree, start, end, GFP_NOFS); |
| 968 | return 0; |
| 969 | } |
| 970 | EXPORT_SYMBOL(set_range_writeback); |
| 971 | |
| 972 | int find_first_extent_bit(struct extent_io_tree *tree, u64 start, |
| 973 | u64 *start_ret, u64 *end_ret, int bits) |
| 974 | { |
| 975 | struct rb_node *node; |
| 976 | struct extent_state *state; |
| 977 | int ret = 1; |
| 978 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 979 | spin_lock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 980 | /* |
| 981 | * this search will find all the extents that end after |
| 982 | * our range starts. |
| 983 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 984 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 985 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 986 | goto out; |
| 987 | } |
| 988 | |
| 989 | while(1) { |
| 990 | state = rb_entry(node, struct extent_state, rb_node); |
| 991 | if (state->end >= start && (state->state & bits)) { |
| 992 | *start_ret = state->start; |
| 993 | *end_ret = state->end; |
| 994 | ret = 0; |
| 995 | break; |
| 996 | } |
| 997 | node = rb_next(node); |
| 998 | if (!node) |
| 999 | break; |
| 1000 | } |
| 1001 | out: |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1002 | spin_unlock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1003 | return ret; |
| 1004 | } |
| 1005 | EXPORT_SYMBOL(find_first_extent_bit); |
| 1006 | |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1007 | struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree, |
| 1008 | u64 start, int bits) |
| 1009 | { |
| 1010 | struct rb_node *node; |
| 1011 | struct extent_state *state; |
| 1012 | |
| 1013 | /* |
| 1014 | * this search will find all the extents that end after |
| 1015 | * our range starts. |
| 1016 | */ |
| 1017 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1018 | if (!node) { |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1019 | goto out; |
| 1020 | } |
| 1021 | |
| 1022 | while(1) { |
| 1023 | state = rb_entry(node, struct extent_state, rb_node); |
| 1024 | if (state->end >= start && (state->state & bits)) { |
| 1025 | return state; |
| 1026 | } |
| 1027 | node = rb_next(node); |
| 1028 | if (!node) |
| 1029 | break; |
| 1030 | } |
| 1031 | out: |
| 1032 | return NULL; |
| 1033 | } |
| 1034 | EXPORT_SYMBOL(find_first_extent_bit_state); |
| 1035 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1036 | u64 find_lock_delalloc_range(struct extent_io_tree *tree, |
| 1037 | u64 *start, u64 *end, u64 max_bytes) |
| 1038 | { |
| 1039 | struct rb_node *node; |
| 1040 | struct extent_state *state; |
| 1041 | u64 cur_start = *start; |
| 1042 | u64 found = 0; |
| 1043 | u64 total_bytes = 0; |
| 1044 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1045 | spin_lock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1046 | /* |
| 1047 | * this search will find all the extents that end after |
| 1048 | * our range starts. |
| 1049 | */ |
| 1050 | search_again: |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1051 | node = tree_search(tree, cur_start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1052 | if (!node) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1053 | if (!found) |
| 1054 | *end = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1055 | goto out; |
| 1056 | } |
| 1057 | |
| 1058 | while(1) { |
| 1059 | state = rb_entry(node, struct extent_state, rb_node); |
| 1060 | if (found && state->start != cur_start) { |
| 1061 | goto out; |
| 1062 | } |
| 1063 | if (!(state->state & EXTENT_DELALLOC)) { |
| 1064 | if (!found) |
| 1065 | *end = state->end; |
| 1066 | goto out; |
| 1067 | } |
| 1068 | if (!found) { |
| 1069 | struct extent_state *prev_state; |
| 1070 | struct rb_node *prev_node = node; |
| 1071 | while(1) { |
| 1072 | prev_node = rb_prev(prev_node); |
| 1073 | if (!prev_node) |
| 1074 | break; |
| 1075 | prev_state = rb_entry(prev_node, |
| 1076 | struct extent_state, |
| 1077 | rb_node); |
| 1078 | if (!(prev_state->state & EXTENT_DELALLOC)) |
| 1079 | break; |
| 1080 | state = prev_state; |
| 1081 | node = prev_node; |
| 1082 | } |
| 1083 | } |
| 1084 | if (state->state & EXTENT_LOCKED) { |
| 1085 | DEFINE_WAIT(wait); |
| 1086 | atomic_inc(&state->refs); |
| 1087 | prepare_to_wait(&state->wq, &wait, |
| 1088 | TASK_UNINTERRUPTIBLE); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1089 | spin_unlock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1090 | schedule(); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1091 | spin_lock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1092 | finish_wait(&state->wq, &wait); |
| 1093 | free_extent_state(state); |
| 1094 | goto search_again; |
| 1095 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1096 | set_state_cb(tree, state, EXTENT_LOCKED); |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1097 | state->state |= EXTENT_LOCKED; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1098 | if (!found) |
| 1099 | *start = state->start; |
| 1100 | found++; |
| 1101 | *end = state->end; |
| 1102 | cur_start = state->end + 1; |
| 1103 | node = rb_next(node); |
| 1104 | if (!node) |
| 1105 | break; |
| 1106 | total_bytes += state->end - state->start + 1; |
| 1107 | if (total_bytes >= max_bytes) |
| 1108 | break; |
| 1109 | } |
| 1110 | out: |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1111 | spin_unlock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1112 | return found; |
| 1113 | } |
| 1114 | |
| 1115 | u64 count_range_bits(struct extent_io_tree *tree, |
| 1116 | u64 *start, u64 search_end, u64 max_bytes, |
| 1117 | unsigned long bits) |
| 1118 | { |
| 1119 | struct rb_node *node; |
| 1120 | struct extent_state *state; |
| 1121 | u64 cur_start = *start; |
| 1122 | u64 total_bytes = 0; |
| 1123 | int found = 0; |
| 1124 | |
| 1125 | if (search_end <= cur_start) { |
| 1126 | printk("search_end %Lu start %Lu\n", search_end, cur_start); |
| 1127 | WARN_ON(1); |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1131 | spin_lock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1132 | if (cur_start == 0 && bits == EXTENT_DIRTY) { |
| 1133 | total_bytes = tree->dirty_bytes; |
| 1134 | goto out; |
| 1135 | } |
| 1136 | /* |
| 1137 | * this search will find all the extents that end after |
| 1138 | * our range starts. |
| 1139 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1140 | node = tree_search(tree, cur_start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1141 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1142 | goto out; |
| 1143 | } |
| 1144 | |
| 1145 | while(1) { |
| 1146 | state = rb_entry(node, struct extent_state, rb_node); |
| 1147 | if (state->start > search_end) |
| 1148 | break; |
| 1149 | if (state->end >= cur_start && (state->state & bits)) { |
| 1150 | total_bytes += min(search_end, state->end) + 1 - |
| 1151 | max(cur_start, state->start); |
| 1152 | if (total_bytes >= max_bytes) |
| 1153 | break; |
| 1154 | if (!found) { |
| 1155 | *start = state->start; |
| 1156 | found = 1; |
| 1157 | } |
| 1158 | } |
| 1159 | node = rb_next(node); |
| 1160 | if (!node) |
| 1161 | break; |
| 1162 | } |
| 1163 | out: |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1164 | spin_unlock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1165 | return total_bytes; |
| 1166 | } |
| 1167 | /* |
| 1168 | * helper function to lock both pages and extents in the tree. |
| 1169 | * pages must be locked first. |
| 1170 | */ |
| 1171 | int lock_range(struct extent_io_tree *tree, u64 start, u64 end) |
| 1172 | { |
| 1173 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1174 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1175 | struct page *page; |
| 1176 | int err; |
| 1177 | |
| 1178 | while (index <= end_index) { |
| 1179 | page = grab_cache_page(tree->mapping, index); |
| 1180 | if (!page) { |
| 1181 | err = -ENOMEM; |
| 1182 | goto failed; |
| 1183 | } |
| 1184 | if (IS_ERR(page)) { |
| 1185 | err = PTR_ERR(page); |
| 1186 | goto failed; |
| 1187 | } |
| 1188 | index++; |
| 1189 | } |
| 1190 | lock_extent(tree, start, end, GFP_NOFS); |
| 1191 | return 0; |
| 1192 | |
| 1193 | failed: |
| 1194 | /* |
| 1195 | * we failed above in getting the page at 'index', so we undo here |
| 1196 | * up to but not including the page at 'index' |
| 1197 | */ |
| 1198 | end_index = index; |
| 1199 | index = start >> PAGE_CACHE_SHIFT; |
| 1200 | while (index < end_index) { |
| 1201 | page = find_get_page(tree->mapping, index); |
| 1202 | unlock_page(page); |
| 1203 | page_cache_release(page); |
| 1204 | index++; |
| 1205 | } |
| 1206 | return err; |
| 1207 | } |
| 1208 | EXPORT_SYMBOL(lock_range); |
| 1209 | |
| 1210 | /* |
| 1211 | * helper function to unlock both pages and extents in the tree. |
| 1212 | */ |
| 1213 | int unlock_range(struct extent_io_tree *tree, u64 start, u64 end) |
| 1214 | { |
| 1215 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1216 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1217 | struct page *page; |
| 1218 | |
| 1219 | while (index <= end_index) { |
| 1220 | page = find_get_page(tree->mapping, index); |
| 1221 | unlock_page(page); |
| 1222 | page_cache_release(page); |
| 1223 | index++; |
| 1224 | } |
| 1225 | unlock_extent(tree, start, end, GFP_NOFS); |
| 1226 | return 0; |
| 1227 | } |
| 1228 | EXPORT_SYMBOL(unlock_range); |
| 1229 | |
| 1230 | int set_state_private(struct extent_io_tree *tree, u64 start, u64 private) |
| 1231 | { |
| 1232 | struct rb_node *node; |
| 1233 | struct extent_state *state; |
| 1234 | int ret = 0; |
| 1235 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1236 | spin_lock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1237 | /* |
| 1238 | * this search will find all the extents that end after |
| 1239 | * our range starts. |
| 1240 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1241 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1242 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1243 | ret = -ENOENT; |
| 1244 | goto out; |
| 1245 | } |
| 1246 | state = rb_entry(node, struct extent_state, rb_node); |
| 1247 | if (state->start != start) { |
| 1248 | ret = -ENOENT; |
| 1249 | goto out; |
| 1250 | } |
| 1251 | state->private = private; |
| 1252 | out: |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1253 | spin_unlock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1254 | return ret; |
| 1255 | } |
| 1256 | |
| 1257 | int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private) |
| 1258 | { |
| 1259 | struct rb_node *node; |
| 1260 | struct extent_state *state; |
| 1261 | int ret = 0; |
| 1262 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1263 | spin_lock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1264 | /* |
| 1265 | * this search will find all the extents that end after |
| 1266 | * our range starts. |
| 1267 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1268 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1269 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1270 | ret = -ENOENT; |
| 1271 | goto out; |
| 1272 | } |
| 1273 | state = rb_entry(node, struct extent_state, rb_node); |
| 1274 | if (state->start != start) { |
| 1275 | ret = -ENOENT; |
| 1276 | goto out; |
| 1277 | } |
| 1278 | *private = state->private; |
| 1279 | out: |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1280 | spin_unlock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1281 | return ret; |
| 1282 | } |
| 1283 | |
| 1284 | /* |
| 1285 | * searches a range in the state tree for a given mask. |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1286 | * 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] | 1287 | * has the bits set. Otherwise, 1 is returned if any bit in the |
| 1288 | * range is found set. |
| 1289 | */ |
| 1290 | int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 1291 | int bits, int filled) |
| 1292 | { |
| 1293 | struct extent_state *state = NULL; |
| 1294 | struct rb_node *node; |
| 1295 | int bitset = 0; |
| 1296 | unsigned long flags; |
| 1297 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1298 | spin_lock_irqsave(&tree->lock, flags); |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1299 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1300 | while (node && start <= end) { |
| 1301 | state = rb_entry(node, struct extent_state, rb_node); |
| 1302 | |
| 1303 | if (filled && state->start > start) { |
| 1304 | bitset = 0; |
| 1305 | break; |
| 1306 | } |
| 1307 | |
| 1308 | if (state->start > end) |
| 1309 | break; |
| 1310 | |
| 1311 | if (state->state & bits) { |
| 1312 | bitset = 1; |
| 1313 | if (!filled) |
| 1314 | break; |
| 1315 | } else if (filled) { |
| 1316 | bitset = 0; |
| 1317 | break; |
| 1318 | } |
| 1319 | start = state->end + 1; |
| 1320 | if (start > end) |
| 1321 | break; |
| 1322 | node = rb_next(node); |
| 1323 | if (!node) { |
| 1324 | if (filled) |
| 1325 | bitset = 0; |
| 1326 | break; |
| 1327 | } |
| 1328 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1329 | spin_unlock_irqrestore(&tree->lock, flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1330 | return bitset; |
| 1331 | } |
| 1332 | EXPORT_SYMBOL(test_range_bit); |
| 1333 | |
| 1334 | /* |
| 1335 | * helper function to set a given page up to date if all the |
| 1336 | * extents in the tree for that page are up to date |
| 1337 | */ |
| 1338 | static int check_page_uptodate(struct extent_io_tree *tree, |
| 1339 | struct page *page) |
| 1340 | { |
| 1341 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 1342 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 1343 | if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1)) |
| 1344 | SetPageUptodate(page); |
| 1345 | return 0; |
| 1346 | } |
| 1347 | |
| 1348 | /* |
| 1349 | * helper function to unlock a page if all the extents in the tree |
| 1350 | * for that page are unlocked |
| 1351 | */ |
| 1352 | static int check_page_locked(struct extent_io_tree *tree, |
| 1353 | struct page *page) |
| 1354 | { |
| 1355 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 1356 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 1357 | if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0)) |
| 1358 | unlock_page(page); |
| 1359 | return 0; |
| 1360 | } |
| 1361 | |
| 1362 | /* |
| 1363 | * helper function to end page writeback if all the extents |
| 1364 | * in the tree for that page are done with writeback |
| 1365 | */ |
| 1366 | static int check_page_writeback(struct extent_io_tree *tree, |
| 1367 | struct page *page) |
| 1368 | { |
| 1369 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 1370 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 1371 | if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0)) |
| 1372 | end_page_writeback(page); |
| 1373 | return 0; |
| 1374 | } |
| 1375 | |
| 1376 | /* lots and lots of room for performance fixes in the end_bio funcs */ |
| 1377 | |
| 1378 | /* |
| 1379 | * after a writepage IO is done, we need to: |
| 1380 | * clear the uptodate bits on error |
| 1381 | * clear the writeback bits in the extent tree for this IO |
| 1382 | * end_page_writeback if the page has no more pending IO |
| 1383 | * |
| 1384 | * Scheduling is not allowed, so the extent state tree is expected |
| 1385 | * to have one and only one object corresponding to this IO. |
| 1386 | */ |
| 1387 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
| 1388 | static void end_bio_extent_writepage(struct bio *bio, int err) |
| 1389 | #else |
| 1390 | static int end_bio_extent_writepage(struct bio *bio, |
| 1391 | unsigned int bytes_done, int err) |
| 1392 | #endif |
| 1393 | { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1394 | int uptodate = err == 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1395 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1396 | struct extent_state *state = bio->bi_private; |
| 1397 | struct extent_io_tree *tree = state->tree; |
| 1398 | struct rb_node *node; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1399 | u64 start; |
| 1400 | u64 end; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1401 | u64 cur; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1402 | int whole_page; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1403 | int ret; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1404 | unsigned long flags; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1405 | |
| 1406 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 1407 | if (bio->bi_size) |
| 1408 | return 1; |
| 1409 | #endif |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1410 | do { |
| 1411 | struct page *page = bvec->bv_page; |
| 1412 | start = ((u64)page->index << PAGE_CACHE_SHIFT) + |
| 1413 | bvec->bv_offset; |
| 1414 | end = start + bvec->bv_len - 1; |
| 1415 | |
| 1416 | if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE) |
| 1417 | whole_page = 1; |
| 1418 | else |
| 1419 | whole_page = 0; |
| 1420 | |
| 1421 | if (--bvec >= bio->bi_io_vec) |
| 1422 | prefetchw(&bvec->bv_page->flags); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1423 | if (tree->ops && tree->ops->writepage_end_io_hook) { |
| 1424 | ret = tree->ops->writepage_end_io_hook(page, start, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1425 | end, state, uptodate); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1426 | if (ret) |
| 1427 | uptodate = 0; |
| 1428 | } |
| 1429 | |
| 1430 | if (!uptodate && tree->ops && |
| 1431 | tree->ops->writepage_io_failed_hook) { |
| 1432 | ret = tree->ops->writepage_io_failed_hook(bio, page, |
| 1433 | start, end, state); |
| 1434 | if (ret == 0) { |
| 1435 | state = NULL; |
| 1436 | uptodate = (err == 0); |
| 1437 | continue; |
| 1438 | } |
| 1439 | } |
| 1440 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1441 | if (!uptodate) { |
| 1442 | clear_extent_uptodate(tree, start, end, GFP_ATOMIC); |
| 1443 | ClearPageUptodate(page); |
| 1444 | SetPageError(page); |
| 1445 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1446 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1447 | /* |
| 1448 | * bios can get merged in funny ways, and so we need to |
| 1449 | * be careful with the state variable. We know the |
| 1450 | * state won't be merged with others because it has |
| 1451 | * WRITEBACK set, but we can't be sure each biovec is |
| 1452 | * sequential in the file. So, if our cached state |
| 1453 | * doesn't match the expected end, search the tree |
| 1454 | * for the correct one. |
| 1455 | */ |
| 1456 | |
| 1457 | spin_lock_irqsave(&tree->lock, flags); |
| 1458 | if (!state || state->end != end) { |
| 1459 | state = NULL; |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1460 | node = __etree_search(tree, start, NULL, NULL); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1461 | if (node) { |
| 1462 | state = rb_entry(node, struct extent_state, |
| 1463 | rb_node); |
| 1464 | if (state->end != end || |
| 1465 | !(state->state & EXTENT_WRITEBACK)) |
| 1466 | state = NULL; |
| 1467 | } |
| 1468 | if (!state) { |
| 1469 | spin_unlock_irqrestore(&tree->lock, flags); |
| 1470 | clear_extent_writeback(tree, start, |
| 1471 | end, GFP_ATOMIC); |
| 1472 | goto next_io; |
| 1473 | } |
| 1474 | } |
| 1475 | cur = end; |
| 1476 | while(1) { |
| 1477 | struct extent_state *clear = state; |
| 1478 | cur = state->start; |
| 1479 | node = rb_prev(&state->rb_node); |
| 1480 | if (node) { |
| 1481 | state = rb_entry(node, |
| 1482 | struct extent_state, |
| 1483 | rb_node); |
| 1484 | } else { |
| 1485 | state = NULL; |
| 1486 | } |
| 1487 | |
| 1488 | clear_state_bit(tree, clear, EXTENT_WRITEBACK, |
| 1489 | 1, 0); |
| 1490 | if (cur == start) |
| 1491 | break; |
| 1492 | if (cur < start) { |
| 1493 | WARN_ON(1); |
| 1494 | break; |
| 1495 | } |
| 1496 | if (!node) |
| 1497 | break; |
| 1498 | } |
| 1499 | /* before releasing the lock, make sure the next state |
| 1500 | * variable has the expected bits set and corresponds |
| 1501 | * to the correct offsets in the file |
| 1502 | */ |
| 1503 | if (state && (state->end + 1 != start || |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 1504 | !(state->state & EXTENT_WRITEBACK))) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1505 | state = NULL; |
| 1506 | } |
| 1507 | spin_unlock_irqrestore(&tree->lock, flags); |
| 1508 | next_io: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1509 | |
| 1510 | if (whole_page) |
| 1511 | end_page_writeback(page); |
| 1512 | else |
| 1513 | check_page_writeback(tree, page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1514 | } while (bvec >= bio->bi_io_vec); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1515 | bio_put(bio); |
| 1516 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 1517 | return 0; |
| 1518 | #endif |
| 1519 | } |
| 1520 | |
| 1521 | /* |
| 1522 | * after a readpage IO is done, we need to: |
| 1523 | * clear the uptodate bits on error |
| 1524 | * set the uptodate bits if things worked |
| 1525 | * set the page up to date if all extents in the tree are uptodate |
| 1526 | * clear the lock bit in the extent tree |
| 1527 | * unlock the page if there are no other extents locked for it |
| 1528 | * |
| 1529 | * Scheduling is not allowed, so the extent state tree is expected |
| 1530 | * to have one and only one object corresponding to this IO. |
| 1531 | */ |
| 1532 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
| 1533 | static void end_bio_extent_readpage(struct bio *bio, int err) |
| 1534 | #else |
| 1535 | static int end_bio_extent_readpage(struct bio *bio, |
| 1536 | unsigned int bytes_done, int err) |
| 1537 | #endif |
| 1538 | { |
| 1539 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 1540 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1541 | struct extent_state *state = bio->bi_private; |
| 1542 | struct extent_io_tree *tree = state->tree; |
| 1543 | struct rb_node *node; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1544 | u64 start; |
| 1545 | u64 end; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1546 | u64 cur; |
| 1547 | unsigned long flags; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1548 | int whole_page; |
| 1549 | int ret; |
| 1550 | |
| 1551 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 1552 | if (bio->bi_size) |
| 1553 | return 1; |
| 1554 | #endif |
| 1555 | |
| 1556 | do { |
| 1557 | struct page *page = bvec->bv_page; |
| 1558 | start = ((u64)page->index << PAGE_CACHE_SHIFT) + |
| 1559 | bvec->bv_offset; |
| 1560 | end = start + bvec->bv_len - 1; |
| 1561 | |
| 1562 | if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE) |
| 1563 | whole_page = 1; |
| 1564 | else |
| 1565 | whole_page = 0; |
| 1566 | |
| 1567 | if (--bvec >= bio->bi_io_vec) |
| 1568 | prefetchw(&bvec->bv_page->flags); |
| 1569 | |
| 1570 | if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1571 | ret = tree->ops->readpage_end_io_hook(page, start, end, |
| 1572 | state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1573 | if (ret) |
| 1574 | uptodate = 0; |
| 1575 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1576 | if (!uptodate && tree->ops && |
| 1577 | tree->ops->readpage_io_failed_hook) { |
| 1578 | ret = tree->ops->readpage_io_failed_hook(bio, page, |
| 1579 | start, end, state); |
| 1580 | if (ret == 0) { |
| 1581 | state = NULL; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1582 | uptodate = |
| 1583 | test_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1584 | continue; |
| 1585 | } |
| 1586 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1587 | |
| 1588 | spin_lock_irqsave(&tree->lock, flags); |
| 1589 | if (!state || state->end != end) { |
| 1590 | state = NULL; |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1591 | node = __etree_search(tree, start, NULL, NULL); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1592 | if (node) { |
| 1593 | state = rb_entry(node, struct extent_state, |
| 1594 | rb_node); |
| 1595 | if (state->end != end || |
| 1596 | !(state->state & EXTENT_LOCKED)) |
| 1597 | state = NULL; |
| 1598 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1599 | if (!state) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1600 | spin_unlock_irqrestore(&tree->lock, flags); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1601 | if (uptodate) |
| 1602 | set_extent_uptodate(tree, start, end, |
| 1603 | GFP_ATOMIC); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1604 | unlock_extent(tree, start, end, GFP_ATOMIC); |
| 1605 | goto next_io; |
| 1606 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1607 | } |
| 1608 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1609 | cur = end; |
| 1610 | while(1) { |
| 1611 | struct extent_state *clear = state; |
| 1612 | cur = state->start; |
| 1613 | node = rb_prev(&state->rb_node); |
| 1614 | if (node) { |
| 1615 | state = rb_entry(node, |
| 1616 | struct extent_state, |
| 1617 | rb_node); |
| 1618 | } else { |
| 1619 | state = NULL; |
| 1620 | } |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1621 | if (uptodate) { |
| 1622 | set_state_cb(tree, clear, EXTENT_UPTODATE); |
| 1623 | clear->state |= EXTENT_UPTODATE; |
| 1624 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1625 | clear_state_bit(tree, clear, EXTENT_LOCKED, |
| 1626 | 1, 0); |
| 1627 | if (cur == start) |
| 1628 | break; |
| 1629 | if (cur < start) { |
| 1630 | WARN_ON(1); |
| 1631 | break; |
| 1632 | } |
| 1633 | if (!node) |
| 1634 | break; |
| 1635 | } |
| 1636 | /* before releasing the lock, make sure the next state |
| 1637 | * variable has the expected bits set and corresponds |
| 1638 | * to the correct offsets in the file |
| 1639 | */ |
| 1640 | if (state && (state->end + 1 != start || |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 1641 | !(state->state & EXTENT_LOCKED))) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1642 | state = NULL; |
| 1643 | } |
| 1644 | spin_unlock_irqrestore(&tree->lock, flags); |
| 1645 | next_io: |
| 1646 | if (whole_page) { |
| 1647 | if (uptodate) { |
| 1648 | SetPageUptodate(page); |
| 1649 | } else { |
| 1650 | ClearPageUptodate(page); |
| 1651 | SetPageError(page); |
| 1652 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1653 | unlock_page(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1654 | } else { |
| 1655 | if (uptodate) { |
| 1656 | check_page_uptodate(tree, page); |
| 1657 | } else { |
| 1658 | ClearPageUptodate(page); |
| 1659 | SetPageError(page); |
| 1660 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1661 | check_page_locked(tree, page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1662 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1663 | } while (bvec >= bio->bi_io_vec); |
| 1664 | |
| 1665 | bio_put(bio); |
| 1666 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 1667 | return 0; |
| 1668 | #endif |
| 1669 | } |
| 1670 | |
| 1671 | /* |
| 1672 | * IO done from prepare_write is pretty simple, we just unlock |
| 1673 | * the structs in the extent tree when done, and set the uptodate bits |
| 1674 | * as appropriate. |
| 1675 | */ |
| 1676 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
| 1677 | static void end_bio_extent_preparewrite(struct bio *bio, int err) |
| 1678 | #else |
| 1679 | static int end_bio_extent_preparewrite(struct bio *bio, |
| 1680 | unsigned int bytes_done, int err) |
| 1681 | #endif |
| 1682 | { |
| 1683 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 1684 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1685 | struct extent_state *state = bio->bi_private; |
| 1686 | struct extent_io_tree *tree = state->tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1687 | u64 start; |
| 1688 | u64 end; |
| 1689 | |
| 1690 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 1691 | if (bio->bi_size) |
| 1692 | return 1; |
| 1693 | #endif |
| 1694 | |
| 1695 | do { |
| 1696 | struct page *page = bvec->bv_page; |
| 1697 | start = ((u64)page->index << PAGE_CACHE_SHIFT) + |
| 1698 | bvec->bv_offset; |
| 1699 | end = start + bvec->bv_len - 1; |
| 1700 | |
| 1701 | if (--bvec >= bio->bi_io_vec) |
| 1702 | prefetchw(&bvec->bv_page->flags); |
| 1703 | |
| 1704 | if (uptodate) { |
| 1705 | set_extent_uptodate(tree, start, end, GFP_ATOMIC); |
| 1706 | } else { |
| 1707 | ClearPageUptodate(page); |
| 1708 | SetPageError(page); |
| 1709 | } |
| 1710 | |
| 1711 | unlock_extent(tree, start, end, GFP_ATOMIC); |
| 1712 | |
| 1713 | } while (bvec >= bio->bi_io_vec); |
| 1714 | |
| 1715 | bio_put(bio); |
| 1716 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 1717 | return 0; |
| 1718 | #endif |
| 1719 | } |
| 1720 | |
| 1721 | static struct bio * |
| 1722 | extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs, |
| 1723 | gfp_t gfp_flags) |
| 1724 | { |
| 1725 | struct bio *bio; |
| 1726 | |
| 1727 | bio = bio_alloc(gfp_flags, nr_vecs); |
| 1728 | |
| 1729 | if (bio == NULL && (current->flags & PF_MEMALLOC)) { |
| 1730 | while (!bio && (nr_vecs /= 2)) |
| 1731 | bio = bio_alloc(gfp_flags, nr_vecs); |
| 1732 | } |
| 1733 | |
| 1734 | if (bio) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 1735 | bio->bi_size = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1736 | bio->bi_bdev = bdev; |
| 1737 | bio->bi_sector = first_sector; |
| 1738 | } |
| 1739 | return bio; |
| 1740 | } |
| 1741 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1742 | static int submit_one_bio(int rw, struct bio *bio, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1743 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1744 | int ret = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1745 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 1746 | struct page *page = bvec->bv_page; |
| 1747 | struct extent_io_tree *tree = bio->bi_private; |
| 1748 | struct rb_node *node; |
| 1749 | struct extent_state *state; |
| 1750 | u64 start; |
| 1751 | u64 end; |
| 1752 | |
| 1753 | start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset; |
| 1754 | end = start + bvec->bv_len - 1; |
| 1755 | |
| 1756 | spin_lock_irq(&tree->lock); |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1757 | node = __etree_search(tree, start, NULL, NULL); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1758 | BUG_ON(!node); |
| 1759 | state = rb_entry(node, struct extent_state, rb_node); |
| 1760 | while(state->end < end) { |
| 1761 | node = rb_next(node); |
| 1762 | state = rb_entry(node, struct extent_state, rb_node); |
| 1763 | } |
| 1764 | BUG_ON(state->end != end); |
| 1765 | spin_unlock_irq(&tree->lock); |
| 1766 | |
| 1767 | bio->bi_private = state; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1768 | |
| 1769 | bio_get(bio); |
| 1770 | |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1771 | if (tree->ops && tree->ops->submit_bio_hook) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1772 | tree->ops->submit_bio_hook(page->mapping->host, rw, bio, |
| 1773 | mirror_num); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1774 | else |
| 1775 | submit_bio(rw, bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1776 | if (bio_flagged(bio, BIO_EOPNOTSUPP)) |
| 1777 | ret = -EOPNOTSUPP; |
| 1778 | bio_put(bio); |
| 1779 | return ret; |
| 1780 | } |
| 1781 | |
| 1782 | static int submit_extent_page(int rw, struct extent_io_tree *tree, |
| 1783 | struct page *page, sector_t sector, |
| 1784 | size_t size, unsigned long offset, |
| 1785 | struct block_device *bdev, |
| 1786 | struct bio **bio_ret, |
| 1787 | unsigned long max_pages, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1788 | bio_end_io_t end_io_func, |
| 1789 | int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1790 | { |
| 1791 | int ret = 0; |
| 1792 | struct bio *bio; |
| 1793 | int nr; |
| 1794 | |
| 1795 | if (bio_ret && *bio_ret) { |
| 1796 | bio = *bio_ret; |
| 1797 | if (bio->bi_sector + (bio->bi_size >> 9) != sector || |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1798 | (tree->ops && tree->ops->merge_bio_hook && |
| 1799 | tree->ops->merge_bio_hook(page, offset, size, bio)) || |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1800 | bio_add_page(bio, page, size, offset) < size) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1801 | ret = submit_one_bio(rw, bio, mirror_num); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1802 | bio = NULL; |
| 1803 | } else { |
| 1804 | return 0; |
| 1805 | } |
| 1806 | } |
Chris Mason | 961d023 | 2008-02-06 11:01:42 -0500 | [diff] [blame] | 1807 | nr = bio_get_nr_vecs(bdev); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1808 | bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH); |
| 1809 | if (!bio) { |
| 1810 | printk("failed to allocate bio nr %d\n", nr); |
| 1811 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1812 | |
| 1813 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1814 | bio_add_page(bio, page, size, offset); |
| 1815 | bio->bi_end_io = end_io_func; |
| 1816 | bio->bi_private = tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1817 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1818 | if (bio_ret) { |
| 1819 | *bio_ret = bio; |
| 1820 | } else { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1821 | ret = submit_one_bio(rw, bio, mirror_num); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | return ret; |
| 1825 | } |
| 1826 | |
| 1827 | void set_page_extent_mapped(struct page *page) |
| 1828 | { |
| 1829 | if (!PagePrivate(page)) { |
| 1830 | SetPagePrivate(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1831 | page_cache_get(page); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 1832 | set_page_private(page, EXTENT_PAGE_PRIVATE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | void set_page_extent_head(struct page *page, unsigned long len) |
| 1837 | { |
| 1838 | set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2); |
| 1839 | } |
| 1840 | |
| 1841 | /* |
| 1842 | * basic readpage implementation. Locked extent state structs are inserted |
| 1843 | * into the tree that are removed when the IO is done (by the end_io |
| 1844 | * handlers) |
| 1845 | */ |
| 1846 | static int __extent_read_full_page(struct extent_io_tree *tree, |
| 1847 | struct page *page, |
| 1848 | get_extent_t *get_extent, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1849 | struct bio **bio, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1850 | { |
| 1851 | struct inode *inode = page->mapping->host; |
| 1852 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 1853 | u64 page_end = start + PAGE_CACHE_SIZE - 1; |
| 1854 | u64 end; |
| 1855 | u64 cur = start; |
| 1856 | u64 extent_offset; |
| 1857 | u64 last_byte = i_size_read(inode); |
| 1858 | u64 block_start; |
| 1859 | u64 cur_end; |
| 1860 | sector_t sector; |
| 1861 | struct extent_map *em; |
| 1862 | struct block_device *bdev; |
| 1863 | int ret; |
| 1864 | int nr = 0; |
| 1865 | size_t page_offset = 0; |
| 1866 | size_t iosize; |
| 1867 | size_t blocksize = inode->i_sb->s_blocksize; |
| 1868 | |
| 1869 | set_page_extent_mapped(page); |
| 1870 | |
| 1871 | end = page_end; |
| 1872 | lock_extent(tree, start, end, GFP_NOFS); |
| 1873 | |
| 1874 | while (cur <= end) { |
| 1875 | if (cur >= last_byte) { |
| 1876 | char *userpage; |
| 1877 | iosize = PAGE_CACHE_SIZE - page_offset; |
| 1878 | userpage = kmap_atomic(page, KM_USER0); |
| 1879 | memset(userpage + page_offset, 0, iosize); |
| 1880 | flush_dcache_page(page); |
| 1881 | kunmap_atomic(userpage, KM_USER0); |
| 1882 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
| 1883 | GFP_NOFS); |
| 1884 | unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS); |
| 1885 | break; |
| 1886 | } |
| 1887 | em = get_extent(inode, page, page_offset, cur, |
| 1888 | end - cur + 1, 0); |
| 1889 | if (IS_ERR(em) || !em) { |
| 1890 | SetPageError(page); |
| 1891 | unlock_extent(tree, cur, end, GFP_NOFS); |
| 1892 | break; |
| 1893 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1894 | extent_offset = cur - em->start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1895 | if (extent_map_end(em) <= cur) { |
| 1896 | printk("bad mapping em [%Lu %Lu] cur %Lu\n", em->start, extent_map_end(em), cur); |
| 1897 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1898 | BUG_ON(extent_map_end(em) <= cur); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1899 | if (end < cur) { |
| 1900 | printk("2bad mapping end %Lu cur %Lu\n", end, cur); |
| 1901 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1902 | BUG_ON(end < cur); |
| 1903 | |
| 1904 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
| 1905 | cur_end = min(extent_map_end(em) - 1, end); |
| 1906 | iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1); |
| 1907 | sector = (em->block_start + extent_offset) >> 9; |
| 1908 | bdev = em->bdev; |
| 1909 | block_start = em->block_start; |
| 1910 | free_extent_map(em); |
| 1911 | em = NULL; |
| 1912 | |
| 1913 | /* we've found a hole, just zero and go on */ |
| 1914 | if (block_start == EXTENT_MAP_HOLE) { |
| 1915 | char *userpage; |
| 1916 | userpage = kmap_atomic(page, KM_USER0); |
| 1917 | memset(userpage + page_offset, 0, iosize); |
| 1918 | flush_dcache_page(page); |
| 1919 | kunmap_atomic(userpage, KM_USER0); |
| 1920 | |
| 1921 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
| 1922 | GFP_NOFS); |
| 1923 | unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS); |
| 1924 | cur = cur + iosize; |
| 1925 | page_offset += iosize; |
| 1926 | continue; |
| 1927 | } |
| 1928 | /* the get_extent function already copied into the page */ |
| 1929 | if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) { |
| 1930 | unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS); |
| 1931 | cur = cur + iosize; |
| 1932 | page_offset += iosize; |
| 1933 | continue; |
| 1934 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1935 | /* we have an inline extent but it didn't get marked up |
| 1936 | * to date. Error out |
| 1937 | */ |
| 1938 | if (block_start == EXTENT_MAP_INLINE) { |
| 1939 | SetPageError(page); |
| 1940 | unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS); |
| 1941 | cur = cur + iosize; |
| 1942 | page_offset += iosize; |
| 1943 | continue; |
| 1944 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1945 | |
| 1946 | ret = 0; |
| 1947 | if (tree->ops && tree->ops->readpage_io_hook) { |
| 1948 | ret = tree->ops->readpage_io_hook(page, cur, |
| 1949 | cur + iosize - 1); |
| 1950 | } |
| 1951 | if (!ret) { |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 1952 | unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1; |
| 1953 | pnr -= page->index; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1954 | ret = submit_extent_page(READ, tree, page, |
| 1955 | sector, iosize, page_offset, |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 1956 | bdev, bio, pnr, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1957 | end_bio_extent_readpage, mirror_num); |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 1958 | nr++; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1959 | } |
| 1960 | if (ret) |
| 1961 | SetPageError(page); |
| 1962 | cur = cur + iosize; |
| 1963 | page_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1964 | } |
| 1965 | if (!nr) { |
| 1966 | if (!PageError(page)) |
| 1967 | SetPageUptodate(page); |
| 1968 | unlock_page(page); |
| 1969 | } |
| 1970 | return 0; |
| 1971 | } |
| 1972 | |
| 1973 | int extent_read_full_page(struct extent_io_tree *tree, struct page *page, |
| 1974 | get_extent_t *get_extent) |
| 1975 | { |
| 1976 | struct bio *bio = NULL; |
| 1977 | int ret; |
| 1978 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1979 | ret = __extent_read_full_page(tree, page, get_extent, &bio, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1980 | if (bio) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1981 | submit_one_bio(READ, bio, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1982 | return ret; |
| 1983 | } |
| 1984 | EXPORT_SYMBOL(extent_read_full_page); |
| 1985 | |
| 1986 | /* |
| 1987 | * the writepage semantics are similar to regular writepage. extent |
| 1988 | * records are inserted to lock ranges in the tree, and as dirty areas |
| 1989 | * are found, they are marked writeback. Then the lock bits are removed |
| 1990 | * and the end_io handler clears the writeback ranges |
| 1991 | */ |
| 1992 | static int __extent_writepage(struct page *page, struct writeback_control *wbc, |
| 1993 | void *data) |
| 1994 | { |
| 1995 | struct inode *inode = page->mapping->host; |
| 1996 | struct extent_page_data *epd = data; |
| 1997 | struct extent_io_tree *tree = epd->tree; |
| 1998 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 1999 | u64 delalloc_start; |
| 2000 | u64 page_end = start + PAGE_CACHE_SIZE - 1; |
| 2001 | u64 end; |
| 2002 | u64 cur = start; |
| 2003 | u64 extent_offset; |
| 2004 | u64 last_byte = i_size_read(inode); |
| 2005 | u64 block_start; |
| 2006 | u64 iosize; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2007 | u64 unlock_start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2008 | sector_t sector; |
| 2009 | struct extent_map *em; |
| 2010 | struct block_device *bdev; |
| 2011 | int ret; |
| 2012 | int nr = 0; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2013 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2014 | size_t blocksize; |
| 2015 | loff_t i_size = i_size_read(inode); |
| 2016 | unsigned long end_index = i_size >> PAGE_CACHE_SHIFT; |
| 2017 | u64 nr_delalloc; |
| 2018 | u64 delalloc_end; |
| 2019 | |
| 2020 | WARN_ON(!PageLocked(page)); |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2021 | pg_offset = i_size & (PAGE_CACHE_SIZE - 1); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 2022 | if (page->index > end_index || |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2023 | (page->index == end_index && !pg_offset)) { |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 2024 | page->mapping->a_ops->invalidatepage(page, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2025 | unlock_page(page); |
| 2026 | return 0; |
| 2027 | } |
| 2028 | |
| 2029 | if (page->index == end_index) { |
| 2030 | char *userpage; |
| 2031 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2032 | userpage = kmap_atomic(page, KM_USER0); |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2033 | memset(userpage + pg_offset, 0, |
| 2034 | PAGE_CACHE_SIZE - pg_offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2035 | kunmap_atomic(userpage, KM_USER0); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 2036 | flush_dcache_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2037 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2038 | pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2039 | |
| 2040 | set_page_extent_mapped(page); |
| 2041 | |
| 2042 | delalloc_start = start; |
| 2043 | delalloc_end = 0; |
| 2044 | while(delalloc_end < page_end) { |
| 2045 | nr_delalloc = find_lock_delalloc_range(tree, &delalloc_start, |
| 2046 | &delalloc_end, |
| 2047 | 128 * 1024 * 1024); |
| 2048 | if (nr_delalloc == 0) { |
| 2049 | delalloc_start = delalloc_end + 1; |
| 2050 | continue; |
| 2051 | } |
| 2052 | tree->ops->fill_delalloc(inode, delalloc_start, |
| 2053 | delalloc_end); |
| 2054 | clear_extent_bit(tree, delalloc_start, |
| 2055 | delalloc_end, |
| 2056 | EXTENT_LOCKED | EXTENT_DELALLOC, |
| 2057 | 1, 0, GFP_NOFS); |
| 2058 | delalloc_start = delalloc_end + 1; |
| 2059 | } |
| 2060 | lock_extent(tree, start, page_end, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2061 | unlock_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2062 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2063 | if (tree->ops && tree->ops->writepage_start_hook) { |
| 2064 | ret = tree->ops->writepage_start_hook(page, start, page_end); |
| 2065 | if (ret == -EAGAIN) { |
| 2066 | unlock_extent(tree, start, page_end, GFP_NOFS); |
| 2067 | redirty_page_for_writepage(wbc, page); |
| 2068 | unlock_page(page); |
| 2069 | return 0; |
| 2070 | } |
| 2071 | } |
| 2072 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2073 | end = page_end; |
| 2074 | if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) { |
| 2075 | printk("found delalloc bits after lock_extent\n"); |
| 2076 | } |
| 2077 | |
| 2078 | if (last_byte <= start) { |
| 2079 | clear_extent_dirty(tree, start, page_end, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2080 | unlock_extent(tree, start, page_end, GFP_NOFS); |
| 2081 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 2082 | tree->ops->writepage_end_io_hook(page, start, |
| 2083 | page_end, NULL, 1); |
| 2084 | unlock_start = page_end + 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2085 | goto done; |
| 2086 | } |
| 2087 | |
| 2088 | set_extent_uptodate(tree, start, page_end, GFP_NOFS); |
| 2089 | blocksize = inode->i_sb->s_blocksize; |
| 2090 | |
| 2091 | while (cur <= end) { |
| 2092 | if (cur >= last_byte) { |
| 2093 | clear_extent_dirty(tree, cur, page_end, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2094 | unlock_extent(tree, unlock_start, page_end, GFP_NOFS); |
| 2095 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 2096 | tree->ops->writepage_end_io_hook(page, cur, |
| 2097 | page_end, NULL, 1); |
| 2098 | unlock_start = page_end + 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2099 | break; |
| 2100 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2101 | em = epd->get_extent(inode, page, pg_offset, cur, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2102 | end - cur + 1, 1); |
| 2103 | if (IS_ERR(em) || !em) { |
| 2104 | SetPageError(page); |
| 2105 | break; |
| 2106 | } |
| 2107 | |
| 2108 | extent_offset = cur - em->start; |
| 2109 | BUG_ON(extent_map_end(em) <= cur); |
| 2110 | BUG_ON(end < cur); |
| 2111 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
| 2112 | iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1); |
| 2113 | sector = (em->block_start + extent_offset) >> 9; |
| 2114 | bdev = em->bdev; |
| 2115 | block_start = em->block_start; |
| 2116 | free_extent_map(em); |
| 2117 | em = NULL; |
| 2118 | |
| 2119 | if (block_start == EXTENT_MAP_HOLE || |
| 2120 | block_start == EXTENT_MAP_INLINE) { |
| 2121 | clear_extent_dirty(tree, cur, |
| 2122 | cur + iosize - 1, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2123 | |
| 2124 | unlock_extent(tree, unlock_start, cur + iosize -1, |
| 2125 | GFP_NOFS); |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2126 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2127 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 2128 | tree->ops->writepage_end_io_hook(page, cur, |
| 2129 | cur + iosize - 1, |
| 2130 | NULL, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2131 | cur = cur + iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2132 | pg_offset += iosize; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2133 | unlock_start = cur; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2134 | continue; |
| 2135 | } |
| 2136 | |
| 2137 | /* leave this out until we have a page_mkwrite call */ |
| 2138 | if (0 && !test_range_bit(tree, cur, cur + iosize - 1, |
| 2139 | EXTENT_DIRTY, 0)) { |
| 2140 | cur = cur + iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2141 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2142 | continue; |
| 2143 | } |
| 2144 | clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS); |
| 2145 | if (tree->ops && tree->ops->writepage_io_hook) { |
| 2146 | ret = tree->ops->writepage_io_hook(page, cur, |
| 2147 | cur + iosize - 1); |
| 2148 | } else { |
| 2149 | ret = 0; |
| 2150 | } |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2151 | if (ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2152 | SetPageError(page); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2153 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2154 | unsigned long max_nr = end_index + 1; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2155 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2156 | set_range_writeback(tree, cur, cur + iosize - 1); |
| 2157 | if (!PageWriteback(page)) { |
| 2158 | printk("warning page %lu not writeback, " |
| 2159 | "cur %llu end %llu\n", page->index, |
| 2160 | (unsigned long long)cur, |
| 2161 | (unsigned long long)end); |
| 2162 | } |
| 2163 | |
| 2164 | ret = submit_extent_page(WRITE, tree, page, sector, |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2165 | iosize, pg_offset, bdev, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2166 | &epd->bio, max_nr, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2167 | end_bio_extent_writepage, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2168 | if (ret) |
| 2169 | SetPageError(page); |
| 2170 | } |
| 2171 | cur = cur + iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2172 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2173 | nr++; |
| 2174 | } |
| 2175 | done: |
| 2176 | if (nr == 0) { |
| 2177 | /* make sure the mapping tag for page dirty gets cleared */ |
| 2178 | set_page_writeback(page); |
| 2179 | end_page_writeback(page); |
| 2180 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2181 | if (unlock_start <= page_end) |
| 2182 | unlock_extent(tree, unlock_start, page_end, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2183 | unlock_page(page); |
| 2184 | return 0; |
| 2185 | } |
| 2186 | |
Chris Mason | 5e478dc | 2008-04-25 09:10:45 -0400 | [diff] [blame] | 2187 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2188 | /* Taken directly from 2.6.23 for 2.6.18 back port */ |
| 2189 | typedef int (*writepage_t)(struct page *page, struct writeback_control *wbc, |
| 2190 | void *data); |
| 2191 | |
| 2192 | /** |
| 2193 | * write_cache_pages - walk the list of dirty pages of the given address space |
| 2194 | * and write all of them. |
| 2195 | * @mapping: address space structure to write |
| 2196 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
| 2197 | * @writepage: function called for each page |
| 2198 | * @data: data passed to writepage function |
| 2199 | * |
| 2200 | * If a page is already under I/O, write_cache_pages() skips it, even |
| 2201 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, |
| 2202 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() |
| 2203 | * and msync() need to guarantee that all the data which was dirty at the time |
| 2204 | * the call was made get new I/O started against them. If wbc->sync_mode is |
| 2205 | * WB_SYNC_ALL then we were called for data integrity and we must wait for |
| 2206 | * existing IO to complete. |
| 2207 | */ |
| 2208 | static int write_cache_pages(struct address_space *mapping, |
| 2209 | struct writeback_control *wbc, writepage_t writepage, |
| 2210 | void *data) |
| 2211 | { |
| 2212 | struct backing_dev_info *bdi = mapping->backing_dev_info; |
| 2213 | int ret = 0; |
| 2214 | int done = 0; |
| 2215 | struct pagevec pvec; |
| 2216 | int nr_pages; |
| 2217 | pgoff_t index; |
| 2218 | pgoff_t end; /* Inclusive */ |
| 2219 | int scanned = 0; |
| 2220 | int range_whole = 0; |
| 2221 | |
| 2222 | if (wbc->nonblocking && bdi_write_congested(bdi)) { |
| 2223 | wbc->encountered_congestion = 1; |
| 2224 | return 0; |
| 2225 | } |
| 2226 | |
| 2227 | pagevec_init(&pvec, 0); |
| 2228 | if (wbc->range_cyclic) { |
| 2229 | index = mapping->writeback_index; /* Start from prev offset */ |
| 2230 | end = -1; |
| 2231 | } else { |
| 2232 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 2233 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
| 2234 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 2235 | range_whole = 1; |
| 2236 | scanned = 1; |
| 2237 | } |
| 2238 | retry: |
| 2239 | while (!done && (index <= end) && |
| 2240 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, |
| 2241 | PAGECACHE_TAG_DIRTY, |
| 2242 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { |
| 2243 | unsigned i; |
| 2244 | |
| 2245 | scanned = 1; |
| 2246 | for (i = 0; i < nr_pages; i++) { |
| 2247 | struct page *page = pvec.pages[i]; |
| 2248 | |
| 2249 | /* |
| 2250 | * At this point we hold neither mapping->tree_lock nor |
| 2251 | * lock on the page itself: the page may be truncated or |
| 2252 | * invalidated (changing page->mapping to NULL), or even |
| 2253 | * swizzled back from swapper_space to tmpfs file |
| 2254 | * mapping |
| 2255 | */ |
| 2256 | lock_page(page); |
| 2257 | |
| 2258 | if (unlikely(page->mapping != mapping)) { |
| 2259 | unlock_page(page); |
| 2260 | continue; |
| 2261 | } |
| 2262 | |
| 2263 | if (!wbc->range_cyclic && page->index > end) { |
| 2264 | done = 1; |
| 2265 | unlock_page(page); |
| 2266 | continue; |
| 2267 | } |
| 2268 | |
| 2269 | if (wbc->sync_mode != WB_SYNC_NONE) |
| 2270 | wait_on_page_writeback(page); |
| 2271 | |
| 2272 | if (PageWriteback(page) || |
| 2273 | !clear_page_dirty_for_io(page)) { |
| 2274 | unlock_page(page); |
| 2275 | continue; |
| 2276 | } |
| 2277 | |
| 2278 | ret = (*writepage)(page, wbc, data); |
| 2279 | |
| 2280 | if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) { |
| 2281 | unlock_page(page); |
| 2282 | ret = 0; |
| 2283 | } |
| 2284 | if (ret || (--(wbc->nr_to_write) <= 0)) |
| 2285 | done = 1; |
| 2286 | if (wbc->nonblocking && bdi_write_congested(bdi)) { |
| 2287 | wbc->encountered_congestion = 1; |
| 2288 | done = 1; |
| 2289 | } |
| 2290 | } |
| 2291 | pagevec_release(&pvec); |
| 2292 | cond_resched(); |
| 2293 | } |
| 2294 | if (!scanned && !done) { |
| 2295 | /* |
| 2296 | * We hit the last page and there is more work to be done: wrap |
| 2297 | * back to the start of the file |
| 2298 | */ |
| 2299 | scanned = 1; |
| 2300 | index = 0; |
| 2301 | goto retry; |
| 2302 | } |
| 2303 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
| 2304 | mapping->writeback_index = index; |
| 2305 | return ret; |
| 2306 | } |
| 2307 | #endif |
| 2308 | |
| 2309 | int extent_write_full_page(struct extent_io_tree *tree, struct page *page, |
| 2310 | get_extent_t *get_extent, |
| 2311 | struct writeback_control *wbc) |
| 2312 | { |
| 2313 | int ret; |
| 2314 | struct address_space *mapping = page->mapping; |
| 2315 | struct extent_page_data epd = { |
| 2316 | .bio = NULL, |
| 2317 | .tree = tree, |
| 2318 | .get_extent = get_extent, |
| 2319 | }; |
| 2320 | struct writeback_control wbc_writepages = { |
| 2321 | .bdi = wbc->bdi, |
| 2322 | .sync_mode = WB_SYNC_NONE, |
| 2323 | .older_than_this = NULL, |
| 2324 | .nr_to_write = 64, |
| 2325 | .range_start = page_offset(page) + PAGE_CACHE_SIZE, |
| 2326 | .range_end = (loff_t)-1, |
| 2327 | }; |
| 2328 | |
| 2329 | |
| 2330 | ret = __extent_writepage(page, wbc, &epd); |
| 2331 | |
| 2332 | write_cache_pages(mapping, &wbc_writepages, __extent_writepage, &epd); |
| 2333 | if (epd.bio) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2334 | submit_one_bio(WRITE, epd.bio, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2335 | } |
| 2336 | return ret; |
| 2337 | } |
| 2338 | EXPORT_SYMBOL(extent_write_full_page); |
| 2339 | |
| 2340 | |
| 2341 | int extent_writepages(struct extent_io_tree *tree, |
| 2342 | struct address_space *mapping, |
| 2343 | get_extent_t *get_extent, |
| 2344 | struct writeback_control *wbc) |
| 2345 | { |
| 2346 | int ret = 0; |
| 2347 | struct extent_page_data epd = { |
| 2348 | .bio = NULL, |
| 2349 | .tree = tree, |
| 2350 | .get_extent = get_extent, |
| 2351 | }; |
| 2352 | |
| 2353 | ret = write_cache_pages(mapping, wbc, __extent_writepage, &epd); |
| 2354 | if (epd.bio) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2355 | submit_one_bio(WRITE, epd.bio, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2356 | } |
| 2357 | return ret; |
| 2358 | } |
| 2359 | EXPORT_SYMBOL(extent_writepages); |
| 2360 | |
| 2361 | int extent_readpages(struct extent_io_tree *tree, |
| 2362 | struct address_space *mapping, |
| 2363 | struct list_head *pages, unsigned nr_pages, |
| 2364 | get_extent_t get_extent) |
| 2365 | { |
| 2366 | struct bio *bio = NULL; |
| 2367 | unsigned page_idx; |
| 2368 | struct pagevec pvec; |
| 2369 | |
| 2370 | pagevec_init(&pvec, 0); |
| 2371 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { |
| 2372 | struct page *page = list_entry(pages->prev, struct page, lru); |
| 2373 | |
| 2374 | prefetchw(&page->flags); |
| 2375 | list_del(&page->lru); |
| 2376 | /* |
| 2377 | * what we want to do here is call add_to_page_cache_lru, |
| 2378 | * but that isn't exported, so we reproduce it here |
| 2379 | */ |
| 2380 | if (!add_to_page_cache(page, mapping, |
| 2381 | page->index, GFP_KERNEL)) { |
| 2382 | |
| 2383 | /* open coding of lru_cache_add, also not exported */ |
| 2384 | page_cache_get(page); |
| 2385 | if (!pagevec_add(&pvec, page)) |
| 2386 | __pagevec_lru_add(&pvec); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2387 | __extent_read_full_page(tree, page, get_extent, |
| 2388 | &bio, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2389 | } |
| 2390 | page_cache_release(page); |
| 2391 | } |
| 2392 | if (pagevec_count(&pvec)) |
| 2393 | __pagevec_lru_add(&pvec); |
| 2394 | BUG_ON(!list_empty(pages)); |
| 2395 | if (bio) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2396 | submit_one_bio(READ, bio, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2397 | return 0; |
| 2398 | } |
| 2399 | EXPORT_SYMBOL(extent_readpages); |
| 2400 | |
| 2401 | /* |
| 2402 | * basic invalidatepage code, this waits on any locked or writeback |
| 2403 | * ranges corresponding to the page, and then deletes any extent state |
| 2404 | * records from the tree |
| 2405 | */ |
| 2406 | int extent_invalidatepage(struct extent_io_tree *tree, |
| 2407 | struct page *page, unsigned long offset) |
| 2408 | { |
| 2409 | u64 start = ((u64)page->index << PAGE_CACHE_SHIFT); |
| 2410 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 2411 | size_t blocksize = page->mapping->host->i_sb->s_blocksize; |
| 2412 | |
| 2413 | start += (offset + blocksize -1) & ~(blocksize - 1); |
| 2414 | if (start > end) |
| 2415 | return 0; |
| 2416 | |
| 2417 | lock_extent(tree, start, end, GFP_NOFS); |
| 2418 | wait_on_extent_writeback(tree, start, end); |
| 2419 | clear_extent_bit(tree, start, end, |
| 2420 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC, |
| 2421 | 1, 1, GFP_NOFS); |
| 2422 | return 0; |
| 2423 | } |
| 2424 | EXPORT_SYMBOL(extent_invalidatepage); |
| 2425 | |
| 2426 | /* |
| 2427 | * simple commit_write call, set_range_dirty is used to mark both |
| 2428 | * the pages and the extent records as dirty |
| 2429 | */ |
| 2430 | int extent_commit_write(struct extent_io_tree *tree, |
| 2431 | struct inode *inode, struct page *page, |
| 2432 | unsigned from, unsigned to) |
| 2433 | { |
| 2434 | loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to; |
| 2435 | |
| 2436 | set_page_extent_mapped(page); |
| 2437 | set_page_dirty(page); |
| 2438 | |
| 2439 | if (pos > inode->i_size) { |
| 2440 | i_size_write(inode, pos); |
| 2441 | mark_inode_dirty(inode); |
| 2442 | } |
| 2443 | return 0; |
| 2444 | } |
| 2445 | EXPORT_SYMBOL(extent_commit_write); |
| 2446 | |
| 2447 | int extent_prepare_write(struct extent_io_tree *tree, |
| 2448 | struct inode *inode, struct page *page, |
| 2449 | unsigned from, unsigned to, get_extent_t *get_extent) |
| 2450 | { |
| 2451 | u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 2452 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 2453 | u64 block_start; |
| 2454 | u64 orig_block_start; |
| 2455 | u64 block_end; |
| 2456 | u64 cur_end; |
| 2457 | struct extent_map *em; |
| 2458 | unsigned blocksize = 1 << inode->i_blkbits; |
| 2459 | size_t page_offset = 0; |
| 2460 | size_t block_off_start; |
| 2461 | size_t block_off_end; |
| 2462 | int err = 0; |
| 2463 | int iocount = 0; |
| 2464 | int ret = 0; |
| 2465 | int isnew; |
| 2466 | |
| 2467 | set_page_extent_mapped(page); |
| 2468 | |
| 2469 | block_start = (page_start + from) & ~((u64)blocksize - 1); |
| 2470 | block_end = (page_start + to - 1) | (blocksize - 1); |
| 2471 | orig_block_start = block_start; |
| 2472 | |
| 2473 | lock_extent(tree, page_start, page_end, GFP_NOFS); |
| 2474 | while(block_start <= block_end) { |
| 2475 | em = get_extent(inode, page, page_offset, block_start, |
| 2476 | block_end - block_start + 1, 1); |
| 2477 | if (IS_ERR(em) || !em) { |
| 2478 | goto err; |
| 2479 | } |
| 2480 | cur_end = min(block_end, extent_map_end(em) - 1); |
| 2481 | block_off_start = block_start & (PAGE_CACHE_SIZE - 1); |
| 2482 | block_off_end = block_off_start + blocksize; |
| 2483 | isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS); |
| 2484 | |
| 2485 | if (!PageUptodate(page) && isnew && |
| 2486 | (block_off_end > to || block_off_start < from)) { |
| 2487 | void *kaddr; |
| 2488 | |
| 2489 | kaddr = kmap_atomic(page, KM_USER0); |
| 2490 | if (block_off_end > to) |
| 2491 | memset(kaddr + to, 0, block_off_end - to); |
| 2492 | if (block_off_start < from) |
| 2493 | memset(kaddr + block_off_start, 0, |
| 2494 | from - block_off_start); |
| 2495 | flush_dcache_page(page); |
| 2496 | kunmap_atomic(kaddr, KM_USER0); |
| 2497 | } |
| 2498 | if ((em->block_start != EXTENT_MAP_HOLE && |
| 2499 | em->block_start != EXTENT_MAP_INLINE) && |
| 2500 | !isnew && !PageUptodate(page) && |
| 2501 | (block_off_end > to || block_off_start < from) && |
| 2502 | !test_range_bit(tree, block_start, cur_end, |
| 2503 | EXTENT_UPTODATE, 1)) { |
| 2504 | u64 sector; |
| 2505 | u64 extent_offset = block_start - em->start; |
| 2506 | size_t iosize; |
| 2507 | sector = (em->block_start + extent_offset) >> 9; |
| 2508 | iosize = (cur_end - block_start + blocksize) & |
| 2509 | ~((u64)blocksize - 1); |
| 2510 | /* |
| 2511 | * we've already got the extent locked, but we |
| 2512 | * need to split the state such that our end_bio |
| 2513 | * handler can clear the lock. |
| 2514 | */ |
| 2515 | set_extent_bit(tree, block_start, |
| 2516 | block_start + iosize - 1, |
| 2517 | EXTENT_LOCKED, 0, NULL, GFP_NOFS); |
| 2518 | ret = submit_extent_page(READ, tree, page, |
| 2519 | sector, iosize, page_offset, em->bdev, |
| 2520 | NULL, 1, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2521 | end_bio_extent_preparewrite, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2522 | iocount++; |
| 2523 | block_start = block_start + iosize; |
| 2524 | } else { |
| 2525 | set_extent_uptodate(tree, block_start, cur_end, |
| 2526 | GFP_NOFS); |
| 2527 | unlock_extent(tree, block_start, cur_end, GFP_NOFS); |
| 2528 | block_start = cur_end + 1; |
| 2529 | } |
| 2530 | page_offset = block_start & (PAGE_CACHE_SIZE - 1); |
| 2531 | free_extent_map(em); |
| 2532 | } |
| 2533 | if (iocount) { |
| 2534 | wait_extent_bit(tree, orig_block_start, |
| 2535 | block_end, EXTENT_LOCKED); |
| 2536 | } |
| 2537 | check_page_uptodate(tree, page); |
| 2538 | err: |
| 2539 | /* FIXME, zero out newly allocated blocks on error */ |
| 2540 | return err; |
| 2541 | } |
| 2542 | EXPORT_SYMBOL(extent_prepare_write); |
| 2543 | |
| 2544 | /* |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 2545 | * a helper for releasepage, this tests for areas of the page that |
| 2546 | * are locked or under IO and drops the related state bits if it is safe |
| 2547 | * to drop the page. |
| 2548 | */ |
| 2549 | int try_release_extent_state(struct extent_map_tree *map, |
| 2550 | struct extent_io_tree *tree, struct page *page, |
| 2551 | gfp_t mask) |
| 2552 | { |
| 2553 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 2554 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 2555 | int ret = 1; |
| 2556 | |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2557 | if (test_range_bit(tree, start, end, |
| 2558 | EXTENT_IOBITS | EXTENT_ORDERED, 0)) |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 2559 | ret = 0; |
| 2560 | else { |
| 2561 | if ((mask & GFP_NOFS) == GFP_NOFS) |
| 2562 | mask = GFP_NOFS; |
| 2563 | clear_extent_bit(tree, start, end, EXTENT_UPTODATE, |
| 2564 | 1, 1, mask); |
| 2565 | } |
| 2566 | return ret; |
| 2567 | } |
| 2568 | EXPORT_SYMBOL(try_release_extent_state); |
| 2569 | |
| 2570 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2571 | * a helper for releasepage. As long as there are no locked extents |
| 2572 | * in the range corresponding to the page, both state records and extent |
| 2573 | * map records are removed |
| 2574 | */ |
| 2575 | int try_release_extent_mapping(struct extent_map_tree *map, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2576 | struct extent_io_tree *tree, struct page *page, |
| 2577 | gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2578 | { |
| 2579 | struct extent_map *em; |
| 2580 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 2581 | u64 end = start + PAGE_CACHE_SIZE - 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 2582 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2583 | if ((mask & __GFP_WAIT) && |
| 2584 | page->mapping->host->i_size > 16 * 1024 * 1024) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 2585 | u64 len; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2586 | while (start <= end) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 2587 | len = end - start + 1; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2588 | spin_lock(&map->lock); |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 2589 | em = lookup_extent_mapping(map, start, len); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2590 | if (!em || IS_ERR(em)) { |
| 2591 | spin_unlock(&map->lock); |
| 2592 | break; |
| 2593 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2594 | if (test_bit(EXTENT_FLAG_PINNED, &em->flags) || |
| 2595 | em->start != start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2596 | spin_unlock(&map->lock); |
| 2597 | free_extent_map(em); |
| 2598 | break; |
| 2599 | } |
| 2600 | if (!test_range_bit(tree, em->start, |
| 2601 | extent_map_end(em) - 1, |
| 2602 | EXTENT_LOCKED, 0)) { |
| 2603 | remove_extent_mapping(map, em); |
| 2604 | /* once for the rb tree */ |
| 2605 | free_extent_map(em); |
| 2606 | } |
| 2607 | start = extent_map_end(em); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2608 | spin_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2609 | |
| 2610 | /* once for us */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2611 | free_extent_map(em); |
| 2612 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2613 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 2614 | return try_release_extent_state(map, tree, page, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2615 | } |
| 2616 | EXPORT_SYMBOL(try_release_extent_mapping); |
| 2617 | |
| 2618 | sector_t extent_bmap(struct address_space *mapping, sector_t iblock, |
| 2619 | get_extent_t *get_extent) |
| 2620 | { |
| 2621 | struct inode *inode = mapping->host; |
| 2622 | u64 start = iblock << inode->i_blkbits; |
| 2623 | sector_t sector = 0; |
| 2624 | struct extent_map *em; |
| 2625 | |
| 2626 | em = get_extent(inode, NULL, 0, start, (1 << inode->i_blkbits), 0); |
| 2627 | if (!em || IS_ERR(em)) |
| 2628 | return 0; |
| 2629 | |
| 2630 | if (em->block_start == EXTENT_MAP_INLINE || |
| 2631 | em->block_start == EXTENT_MAP_HOLE) |
| 2632 | goto out; |
| 2633 | |
| 2634 | sector = (em->block_start + start - em->start) >> inode->i_blkbits; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2635 | out: |
| 2636 | free_extent_map(em); |
| 2637 | return sector; |
| 2638 | } |
| 2639 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2640 | static inline struct page *extent_buffer_page(struct extent_buffer *eb, |
| 2641 | unsigned long i) |
| 2642 | { |
| 2643 | struct page *p; |
| 2644 | struct address_space *mapping; |
| 2645 | |
| 2646 | if (i == 0) |
| 2647 | return eb->first_page; |
| 2648 | i += eb->start >> PAGE_CACHE_SHIFT; |
| 2649 | mapping = eb->first_page->mapping; |
Chris Mason | 33958dc | 2008-07-30 10:29:12 -0400 | [diff] [blame] | 2650 | if (!mapping) |
| 2651 | return NULL; |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 2652 | |
| 2653 | /* |
| 2654 | * extent_buffer_page is only called after pinning the page |
| 2655 | * by increasing the reference count. So we know the page must |
| 2656 | * be in the radix tree. |
| 2657 | */ |
| 2658 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26) |
| 2659 | rcu_read_lock(); |
| 2660 | #else |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2661 | read_lock_irq(&mapping->tree_lock); |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 2662 | #endif |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2663 | p = radix_tree_lookup(&mapping->page_tree, i); |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 2664 | |
| 2665 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26) |
| 2666 | rcu_read_unlock(); |
| 2667 | #else |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2668 | read_unlock_irq(&mapping->tree_lock); |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 2669 | #endif |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2670 | return p; |
| 2671 | } |
| 2672 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2673 | static inline unsigned long num_extent_pages(u64 start, u64 len) |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2674 | { |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2675 | return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) - |
| 2676 | (start >> PAGE_CACHE_SHIFT); |
Chris Mason | 728131d | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2677 | } |
| 2678 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2679 | static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree, |
| 2680 | u64 start, |
| 2681 | unsigned long len, |
| 2682 | gfp_t mask) |
| 2683 | { |
| 2684 | struct extent_buffer *eb = NULL; |
Chris Mason | 2d2ae54 | 2008-03-26 16:24:23 -0400 | [diff] [blame] | 2685 | unsigned long flags; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2686 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2687 | eb = kmem_cache_zalloc(extent_buffer_cache, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2688 | eb->start = start; |
| 2689 | eb->len = len; |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 2690 | mutex_init(&eb->mutex); |
Chris Mason | 2d2ae54 | 2008-03-26 16:24:23 -0400 | [diff] [blame] | 2691 | spin_lock_irqsave(&leak_lock, flags); |
| 2692 | list_add(&eb->leak_list, &buffers); |
| 2693 | spin_unlock_irqrestore(&leak_lock, flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2694 | atomic_set(&eb->refs, 1); |
| 2695 | |
| 2696 | return eb; |
| 2697 | } |
| 2698 | |
| 2699 | static void __free_extent_buffer(struct extent_buffer *eb) |
| 2700 | { |
Chris Mason | 2d2ae54 | 2008-03-26 16:24:23 -0400 | [diff] [blame] | 2701 | unsigned long flags; |
| 2702 | spin_lock_irqsave(&leak_lock, flags); |
| 2703 | list_del(&eb->leak_list); |
| 2704 | spin_unlock_irqrestore(&leak_lock, flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2705 | kmem_cache_free(extent_buffer_cache, eb); |
| 2706 | } |
| 2707 | |
| 2708 | struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree, |
| 2709 | u64 start, unsigned long len, |
| 2710 | struct page *page0, |
| 2711 | gfp_t mask) |
| 2712 | { |
| 2713 | unsigned long num_pages = num_extent_pages(start, len); |
| 2714 | unsigned long i; |
| 2715 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 2716 | struct extent_buffer *eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2717 | struct extent_buffer *exists = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2718 | struct page *p; |
| 2719 | struct address_space *mapping = tree->mapping; |
| 2720 | int uptodate = 1; |
| 2721 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2722 | spin_lock(&tree->buffer_lock); |
| 2723 | eb = buffer_search(tree, start); |
| 2724 | if (eb) { |
| 2725 | atomic_inc(&eb->refs); |
| 2726 | spin_unlock(&tree->buffer_lock); |
| 2727 | return eb; |
| 2728 | } |
| 2729 | spin_unlock(&tree->buffer_lock); |
| 2730 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2731 | eb = __alloc_extent_buffer(tree, start, len, mask); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 2732 | if (!eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2733 | return NULL; |
| 2734 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2735 | if (page0) { |
| 2736 | eb->first_page = page0; |
| 2737 | i = 1; |
| 2738 | index++; |
| 2739 | page_cache_get(page0); |
| 2740 | mark_page_accessed(page0); |
| 2741 | set_page_extent_mapped(page0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2742 | set_page_extent_head(page0, len); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2743 | uptodate = PageUptodate(page0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2744 | } else { |
| 2745 | i = 0; |
| 2746 | } |
| 2747 | for (; i < num_pages; i++, index++) { |
| 2748 | p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM); |
| 2749 | if (!p) { |
| 2750 | WARN_ON(1); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2751 | goto free_eb; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2752 | } |
| 2753 | set_page_extent_mapped(p); |
| 2754 | mark_page_accessed(p); |
| 2755 | if (i == 0) { |
| 2756 | eb->first_page = p; |
| 2757 | set_page_extent_head(p, len); |
| 2758 | } else { |
| 2759 | set_page_private(p, EXTENT_PAGE_PRIVATE); |
| 2760 | } |
| 2761 | if (!PageUptodate(p)) |
| 2762 | uptodate = 0; |
| 2763 | unlock_page(p); |
| 2764 | } |
| 2765 | if (uptodate) |
| 2766 | eb->flags |= EXTENT_UPTODATE; |
| 2767 | eb->flags |= EXTENT_BUFFER_FILLED; |
| 2768 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2769 | spin_lock(&tree->buffer_lock); |
| 2770 | exists = buffer_tree_insert(tree, start, &eb->rb_node); |
| 2771 | if (exists) { |
| 2772 | /* add one reference for the caller */ |
| 2773 | atomic_inc(&exists->refs); |
| 2774 | spin_unlock(&tree->buffer_lock); |
| 2775 | goto free_eb; |
| 2776 | } |
| 2777 | spin_unlock(&tree->buffer_lock); |
| 2778 | |
| 2779 | /* add one reference for the tree */ |
| 2780 | atomic_inc(&eb->refs); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2781 | return eb; |
| 2782 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2783 | free_eb: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2784 | if (!atomic_dec_and_test(&eb->refs)) |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2785 | return exists; |
| 2786 | for (index = 1; index < i; index++) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2787 | page_cache_release(extent_buffer_page(eb, index)); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2788 | page_cache_release(extent_buffer_page(eb, 0)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2789 | __free_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2790 | return exists; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2791 | } |
| 2792 | EXPORT_SYMBOL(alloc_extent_buffer); |
| 2793 | |
| 2794 | struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree, |
| 2795 | u64 start, unsigned long len, |
| 2796 | gfp_t mask) |
| 2797 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2798 | struct extent_buffer *eb; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2799 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2800 | spin_lock(&tree->buffer_lock); |
| 2801 | eb = buffer_search(tree, start); |
| 2802 | if (eb) |
| 2803 | atomic_inc(&eb->refs); |
| 2804 | spin_unlock(&tree->buffer_lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2805 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2806 | return eb; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2807 | } |
| 2808 | EXPORT_SYMBOL(find_extent_buffer); |
| 2809 | |
| 2810 | void free_extent_buffer(struct extent_buffer *eb) |
| 2811 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2812 | if (!eb) |
| 2813 | return; |
| 2814 | |
| 2815 | if (!atomic_dec_and_test(&eb->refs)) |
| 2816 | return; |
| 2817 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2818 | WARN_ON(1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2819 | } |
| 2820 | EXPORT_SYMBOL(free_extent_buffer); |
| 2821 | |
| 2822 | int clear_extent_buffer_dirty(struct extent_io_tree *tree, |
| 2823 | struct extent_buffer *eb) |
| 2824 | { |
| 2825 | int set; |
| 2826 | unsigned long i; |
| 2827 | unsigned long num_pages; |
| 2828 | struct page *page; |
| 2829 | |
| 2830 | u64 start = eb->start; |
| 2831 | u64 end = start + eb->len - 1; |
| 2832 | |
| 2833 | set = clear_extent_dirty(tree, start, end, GFP_NOFS); |
| 2834 | num_pages = num_extent_pages(eb->start, eb->len); |
| 2835 | |
| 2836 | for (i = 0; i < num_pages; i++) { |
| 2837 | page = extent_buffer_page(eb, i); |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 2838 | lock_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2839 | if (i == 0) |
| 2840 | set_page_extent_head(page, eb->len); |
| 2841 | else |
| 2842 | set_page_private(page, EXTENT_PAGE_PRIVATE); |
| 2843 | |
| 2844 | /* |
| 2845 | * if we're on the last page or the first page and the |
| 2846 | * block isn't aligned on a page boundary, do extra checks |
| 2847 | * to make sure we don't clean page that is partially dirty |
| 2848 | */ |
| 2849 | if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) || |
| 2850 | ((i == num_pages - 1) && |
| 2851 | ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) { |
| 2852 | start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 2853 | end = start + PAGE_CACHE_SIZE - 1; |
| 2854 | if (test_range_bit(tree, start, end, |
| 2855 | EXTENT_DIRTY, 0)) { |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 2856 | unlock_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2857 | continue; |
| 2858 | } |
| 2859 | } |
| 2860 | clear_page_dirty_for_io(page); |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 2861 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26) |
| 2862 | spin_lock_irq(&page->mapping->tree_lock); |
| 2863 | #else |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2864 | read_lock_irq(&page->mapping->tree_lock); |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 2865 | #endif |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2866 | if (!PageDirty(page)) { |
| 2867 | radix_tree_tag_clear(&page->mapping->page_tree, |
| 2868 | page_index(page), |
| 2869 | PAGECACHE_TAG_DIRTY); |
| 2870 | } |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 2871 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26) |
| 2872 | spin_unlock_irq(&page->mapping->tree_lock); |
| 2873 | #else |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2874 | read_unlock_irq(&page->mapping->tree_lock); |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 2875 | #endif |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 2876 | unlock_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2877 | } |
| 2878 | return 0; |
| 2879 | } |
| 2880 | EXPORT_SYMBOL(clear_extent_buffer_dirty); |
| 2881 | |
| 2882 | int wait_on_extent_buffer_writeback(struct extent_io_tree *tree, |
| 2883 | struct extent_buffer *eb) |
| 2884 | { |
| 2885 | return wait_on_extent_writeback(tree, eb->start, |
| 2886 | eb->start + eb->len - 1); |
| 2887 | } |
| 2888 | EXPORT_SYMBOL(wait_on_extent_buffer_writeback); |
| 2889 | |
| 2890 | int set_extent_buffer_dirty(struct extent_io_tree *tree, |
| 2891 | struct extent_buffer *eb) |
| 2892 | { |
| 2893 | unsigned long i; |
| 2894 | unsigned long num_pages; |
| 2895 | |
| 2896 | num_pages = num_extent_pages(eb->start, eb->len); |
| 2897 | for (i = 0; i < num_pages; i++) { |
| 2898 | struct page *page = extent_buffer_page(eb, i); |
| 2899 | /* writepage may need to do something special for the |
| 2900 | * first page, we have to make sure page->private is |
| 2901 | * properly set. releasepage may drop page->private |
| 2902 | * on us if the page isn't already dirty. |
| 2903 | */ |
| 2904 | if (i == 0) { |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 2905 | lock_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2906 | set_page_extent_head(page, eb->len); |
| 2907 | } else if (PagePrivate(page) && |
| 2908 | page->private != EXTENT_PAGE_PRIVATE) { |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 2909 | lock_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2910 | set_page_extent_mapped(page); |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 2911 | unlock_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2912 | } |
| 2913 | __set_page_dirty_nobuffers(extent_buffer_page(eb, i)); |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 2914 | if (i == 0) |
| 2915 | unlock_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2916 | } |
| 2917 | return set_extent_dirty(tree, eb->start, |
| 2918 | eb->start + eb->len - 1, GFP_NOFS); |
| 2919 | } |
| 2920 | EXPORT_SYMBOL(set_extent_buffer_dirty); |
| 2921 | |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2922 | int clear_extent_buffer_uptodate(struct extent_io_tree *tree, |
| 2923 | struct extent_buffer *eb) |
| 2924 | { |
| 2925 | unsigned long i; |
| 2926 | struct page *page; |
| 2927 | unsigned long num_pages; |
| 2928 | |
| 2929 | num_pages = num_extent_pages(eb->start, eb->len); |
| 2930 | eb->flags &= ~EXTENT_UPTODATE; |
| 2931 | |
| 2932 | clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1, |
| 2933 | GFP_NOFS); |
| 2934 | for (i = 0; i < num_pages; i++) { |
| 2935 | page = extent_buffer_page(eb, i); |
Chris Mason | 33958dc | 2008-07-30 10:29:12 -0400 | [diff] [blame] | 2936 | if (page) |
| 2937 | ClearPageUptodate(page); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2938 | } |
| 2939 | return 0; |
| 2940 | } |
| 2941 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2942 | int set_extent_buffer_uptodate(struct extent_io_tree *tree, |
| 2943 | struct extent_buffer *eb) |
| 2944 | { |
| 2945 | unsigned long i; |
| 2946 | struct page *page; |
| 2947 | unsigned long num_pages; |
| 2948 | |
| 2949 | num_pages = num_extent_pages(eb->start, eb->len); |
| 2950 | |
| 2951 | set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1, |
| 2952 | GFP_NOFS); |
| 2953 | for (i = 0; i < num_pages; i++) { |
| 2954 | page = extent_buffer_page(eb, i); |
| 2955 | if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) || |
| 2956 | ((i == num_pages - 1) && |
| 2957 | ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) { |
| 2958 | check_page_uptodate(tree, page); |
| 2959 | continue; |
| 2960 | } |
| 2961 | SetPageUptodate(page); |
| 2962 | } |
| 2963 | return 0; |
| 2964 | } |
| 2965 | EXPORT_SYMBOL(set_extent_buffer_uptodate); |
| 2966 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2967 | int extent_range_uptodate(struct extent_io_tree *tree, |
| 2968 | u64 start, u64 end) |
| 2969 | { |
| 2970 | struct page *page; |
| 2971 | int ret; |
| 2972 | int pg_uptodate = 1; |
| 2973 | int uptodate; |
| 2974 | unsigned long index; |
| 2975 | |
| 2976 | ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1); |
| 2977 | if (ret) |
| 2978 | return 1; |
| 2979 | while(start <= end) { |
| 2980 | index = start >> PAGE_CACHE_SHIFT; |
| 2981 | page = find_get_page(tree->mapping, index); |
| 2982 | uptodate = PageUptodate(page); |
| 2983 | page_cache_release(page); |
| 2984 | if (!uptodate) { |
| 2985 | pg_uptodate = 0; |
| 2986 | break; |
| 2987 | } |
| 2988 | start += PAGE_CACHE_SIZE; |
| 2989 | } |
| 2990 | return pg_uptodate; |
| 2991 | } |
| 2992 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2993 | int extent_buffer_uptodate(struct extent_io_tree *tree, |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2994 | struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2995 | { |
Chris Mason | 728131d | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2996 | int ret = 0; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2997 | unsigned long num_pages; |
| 2998 | unsigned long i; |
Chris Mason | 728131d | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2999 | struct page *page; |
| 3000 | int pg_uptodate = 1; |
| 3001 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3002 | if (eb->flags & EXTENT_UPTODATE) |
Chris Mason | 4235298 | 2008-04-28 16:40:52 -0400 | [diff] [blame] | 3003 | return 1; |
Chris Mason | 728131d | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3004 | |
Chris Mason | 4235298 | 2008-04-28 16:40:52 -0400 | [diff] [blame] | 3005 | ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3006 | EXTENT_UPTODATE, 1); |
Chris Mason | 4235298 | 2008-04-28 16:40:52 -0400 | [diff] [blame] | 3007 | if (ret) |
| 3008 | return ret; |
Chris Mason | 728131d | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3009 | |
| 3010 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3011 | for (i = 0; i < num_pages; i++) { |
| 3012 | page = extent_buffer_page(eb, i); |
| 3013 | if (!PageUptodate(page)) { |
| 3014 | pg_uptodate = 0; |
| 3015 | break; |
| 3016 | } |
| 3017 | } |
Chris Mason | 4235298 | 2008-04-28 16:40:52 -0400 | [diff] [blame] | 3018 | return pg_uptodate; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3019 | } |
| 3020 | EXPORT_SYMBOL(extent_buffer_uptodate); |
| 3021 | |
| 3022 | int read_extent_buffer_pages(struct extent_io_tree *tree, |
| 3023 | struct extent_buffer *eb, |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 3024 | u64 start, int wait, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3025 | get_extent_t *get_extent, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3026 | { |
| 3027 | unsigned long i; |
| 3028 | unsigned long start_i; |
| 3029 | struct page *page; |
| 3030 | int err; |
| 3031 | int ret = 0; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3032 | int locked_pages = 0; |
| 3033 | int all_uptodate = 1; |
| 3034 | int inc_all_pages = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3035 | unsigned long num_pages; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 3036 | struct bio *bio = NULL; |
| 3037 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3038 | if (eb->flags & EXTENT_UPTODATE) |
| 3039 | return 0; |
| 3040 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3041 | if (test_range_bit(tree, eb->start, eb->start + eb->len - 1, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3042 | EXTENT_UPTODATE, 1)) { |
| 3043 | return 0; |
| 3044 | } |
| 3045 | |
| 3046 | if (start) { |
| 3047 | WARN_ON(start < eb->start); |
| 3048 | start_i = (start >> PAGE_CACHE_SHIFT) - |
| 3049 | (eb->start >> PAGE_CACHE_SHIFT); |
| 3050 | } else { |
| 3051 | start_i = 0; |
| 3052 | } |
| 3053 | |
| 3054 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3055 | for (i = start_i; i < num_pages; i++) { |
| 3056 | page = extent_buffer_page(eb, i); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3057 | if (!wait) { |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3058 | if (TestSetPageLocked(page)) |
| 3059 | goto unlock_exit; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3060 | } else { |
| 3061 | lock_page(page); |
| 3062 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3063 | locked_pages++; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3064 | if (!PageUptodate(page)) { |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3065 | all_uptodate = 0; |
| 3066 | } |
| 3067 | } |
| 3068 | if (all_uptodate) { |
| 3069 | if (start_i == 0) |
| 3070 | eb->flags |= EXTENT_UPTODATE; |
| 3071 | goto unlock_exit; |
| 3072 | } |
| 3073 | |
| 3074 | for (i = start_i; i < num_pages; i++) { |
| 3075 | page = extent_buffer_page(eb, i); |
| 3076 | if (inc_all_pages) |
| 3077 | page_cache_get(page); |
| 3078 | if (!PageUptodate(page)) { |
| 3079 | if (start_i == 0) |
| 3080 | inc_all_pages = 1; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3081 | ClearPageError(page); |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 3082 | err = __extent_read_full_page(tree, page, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3083 | get_extent, &bio, |
| 3084 | mirror_num); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3085 | if (err) { |
| 3086 | ret = err; |
| 3087 | } |
| 3088 | } else { |
| 3089 | unlock_page(page); |
| 3090 | } |
| 3091 | } |
| 3092 | |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 3093 | if (bio) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3094 | submit_one_bio(READ, bio, mirror_num); |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 3095 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3096 | if (ret || !wait) { |
| 3097 | return ret; |
| 3098 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3099 | for (i = start_i; i < num_pages; i++) { |
| 3100 | page = extent_buffer_page(eb, i); |
| 3101 | wait_on_page_locked(page); |
| 3102 | if (!PageUptodate(page)) { |
| 3103 | ret = -EIO; |
| 3104 | } |
| 3105 | } |
| 3106 | if (!ret) |
| 3107 | eb->flags |= EXTENT_UPTODATE; |
| 3108 | return ret; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3109 | |
| 3110 | unlock_exit: |
| 3111 | i = start_i; |
| 3112 | while(locked_pages > 0) { |
| 3113 | page = extent_buffer_page(eb, i); |
| 3114 | i++; |
| 3115 | unlock_page(page); |
| 3116 | locked_pages--; |
| 3117 | } |
| 3118 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3119 | } |
| 3120 | EXPORT_SYMBOL(read_extent_buffer_pages); |
| 3121 | |
| 3122 | void read_extent_buffer(struct extent_buffer *eb, void *dstv, |
| 3123 | unsigned long start, |
| 3124 | unsigned long len) |
| 3125 | { |
| 3126 | size_t cur; |
| 3127 | size_t offset; |
| 3128 | struct page *page; |
| 3129 | char *kaddr; |
| 3130 | char *dst = (char *)dstv; |
| 3131 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 3132 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3133 | |
| 3134 | WARN_ON(start > eb->len); |
| 3135 | WARN_ON(start + len > eb->start + eb->len); |
| 3136 | |
| 3137 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 3138 | |
| 3139 | while(len > 0) { |
| 3140 | page = extent_buffer_page(eb, i); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3141 | |
| 3142 | cur = min(len, (PAGE_CACHE_SIZE - offset)); |
| 3143 | kaddr = kmap_atomic(page, KM_USER1); |
| 3144 | memcpy(dst, kaddr + offset, cur); |
| 3145 | kunmap_atomic(kaddr, KM_USER1); |
| 3146 | |
| 3147 | dst += cur; |
| 3148 | len -= cur; |
| 3149 | offset = 0; |
| 3150 | i++; |
| 3151 | } |
| 3152 | } |
| 3153 | EXPORT_SYMBOL(read_extent_buffer); |
| 3154 | |
| 3155 | int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start, |
| 3156 | unsigned long min_len, char **token, char **map, |
| 3157 | unsigned long *map_start, |
| 3158 | unsigned long *map_len, int km) |
| 3159 | { |
| 3160 | size_t offset = start & (PAGE_CACHE_SIZE - 1); |
| 3161 | char *kaddr; |
| 3162 | struct page *p; |
| 3163 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 3164 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 3165 | unsigned long end_i = (start_offset + start + min_len - 1) >> |
| 3166 | PAGE_CACHE_SHIFT; |
| 3167 | |
| 3168 | if (i != end_i) |
| 3169 | return -EINVAL; |
| 3170 | |
| 3171 | if (i == 0) { |
| 3172 | offset = start_offset; |
| 3173 | *map_start = 0; |
| 3174 | } else { |
| 3175 | offset = 0; |
| 3176 | *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset; |
| 3177 | } |
| 3178 | if (start + min_len > eb->len) { |
| 3179 | printk("bad mapping eb start %Lu len %lu, wanted %lu %lu\n", eb->start, eb->len, start, min_len); |
| 3180 | WARN_ON(1); |
| 3181 | } |
| 3182 | |
| 3183 | p = extent_buffer_page(eb, i); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3184 | kaddr = kmap_atomic(p, km); |
| 3185 | *token = kaddr; |
| 3186 | *map = kaddr + offset; |
| 3187 | *map_len = PAGE_CACHE_SIZE - offset; |
| 3188 | return 0; |
| 3189 | } |
| 3190 | EXPORT_SYMBOL(map_private_extent_buffer); |
| 3191 | |
| 3192 | int map_extent_buffer(struct extent_buffer *eb, unsigned long start, |
| 3193 | unsigned long min_len, |
| 3194 | char **token, char **map, |
| 3195 | unsigned long *map_start, |
| 3196 | unsigned long *map_len, int km) |
| 3197 | { |
| 3198 | int err; |
| 3199 | int save = 0; |
| 3200 | if (eb->map_token) { |
| 3201 | unmap_extent_buffer(eb, eb->map_token, km); |
| 3202 | eb->map_token = NULL; |
| 3203 | save = 1; |
| 3204 | } |
| 3205 | err = map_private_extent_buffer(eb, start, min_len, token, map, |
| 3206 | map_start, map_len, km); |
| 3207 | if (!err && save) { |
| 3208 | eb->map_token = *token; |
| 3209 | eb->kaddr = *map; |
| 3210 | eb->map_start = *map_start; |
| 3211 | eb->map_len = *map_len; |
| 3212 | } |
| 3213 | return err; |
| 3214 | } |
| 3215 | EXPORT_SYMBOL(map_extent_buffer); |
| 3216 | |
| 3217 | void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km) |
| 3218 | { |
| 3219 | kunmap_atomic(token, km); |
| 3220 | } |
| 3221 | EXPORT_SYMBOL(unmap_extent_buffer); |
| 3222 | |
| 3223 | int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv, |
| 3224 | unsigned long start, |
| 3225 | unsigned long len) |
| 3226 | { |
| 3227 | size_t cur; |
| 3228 | size_t offset; |
| 3229 | struct page *page; |
| 3230 | char *kaddr; |
| 3231 | char *ptr = (char *)ptrv; |
| 3232 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 3233 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 3234 | int ret = 0; |
| 3235 | |
| 3236 | WARN_ON(start > eb->len); |
| 3237 | WARN_ON(start + len > eb->start + eb->len); |
| 3238 | |
| 3239 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 3240 | |
| 3241 | while(len > 0) { |
| 3242 | page = extent_buffer_page(eb, i); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3243 | |
| 3244 | cur = min(len, (PAGE_CACHE_SIZE - offset)); |
| 3245 | |
| 3246 | kaddr = kmap_atomic(page, KM_USER0); |
| 3247 | ret = memcmp(ptr, kaddr + offset, cur); |
| 3248 | kunmap_atomic(kaddr, KM_USER0); |
| 3249 | if (ret) |
| 3250 | break; |
| 3251 | |
| 3252 | ptr += cur; |
| 3253 | len -= cur; |
| 3254 | offset = 0; |
| 3255 | i++; |
| 3256 | } |
| 3257 | return ret; |
| 3258 | } |
| 3259 | EXPORT_SYMBOL(memcmp_extent_buffer); |
| 3260 | |
| 3261 | void write_extent_buffer(struct extent_buffer *eb, const void *srcv, |
| 3262 | unsigned long start, unsigned long len) |
| 3263 | { |
| 3264 | size_t cur; |
| 3265 | size_t offset; |
| 3266 | struct page *page; |
| 3267 | char *kaddr; |
| 3268 | char *src = (char *)srcv; |
| 3269 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 3270 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 3271 | |
| 3272 | WARN_ON(start > eb->len); |
| 3273 | WARN_ON(start + len > eb->start + eb->len); |
| 3274 | |
| 3275 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 3276 | |
| 3277 | while(len > 0) { |
| 3278 | page = extent_buffer_page(eb, i); |
| 3279 | WARN_ON(!PageUptodate(page)); |
| 3280 | |
| 3281 | cur = min(len, PAGE_CACHE_SIZE - offset); |
| 3282 | kaddr = kmap_atomic(page, KM_USER1); |
| 3283 | memcpy(kaddr + offset, src, cur); |
| 3284 | kunmap_atomic(kaddr, KM_USER1); |
| 3285 | |
| 3286 | src += cur; |
| 3287 | len -= cur; |
| 3288 | offset = 0; |
| 3289 | i++; |
| 3290 | } |
| 3291 | } |
| 3292 | EXPORT_SYMBOL(write_extent_buffer); |
| 3293 | |
| 3294 | void memset_extent_buffer(struct extent_buffer *eb, char c, |
| 3295 | unsigned long start, unsigned long len) |
| 3296 | { |
| 3297 | size_t cur; |
| 3298 | size_t offset; |
| 3299 | struct page *page; |
| 3300 | char *kaddr; |
| 3301 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 3302 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 3303 | |
| 3304 | WARN_ON(start > eb->len); |
| 3305 | WARN_ON(start + len > eb->start + eb->len); |
| 3306 | |
| 3307 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 3308 | |
| 3309 | while(len > 0) { |
| 3310 | page = extent_buffer_page(eb, i); |
| 3311 | WARN_ON(!PageUptodate(page)); |
| 3312 | |
| 3313 | cur = min(len, PAGE_CACHE_SIZE - offset); |
| 3314 | kaddr = kmap_atomic(page, KM_USER0); |
| 3315 | memset(kaddr + offset, c, cur); |
| 3316 | kunmap_atomic(kaddr, KM_USER0); |
| 3317 | |
| 3318 | len -= cur; |
| 3319 | offset = 0; |
| 3320 | i++; |
| 3321 | } |
| 3322 | } |
| 3323 | EXPORT_SYMBOL(memset_extent_buffer); |
| 3324 | |
| 3325 | void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src, |
| 3326 | unsigned long dst_offset, unsigned long src_offset, |
| 3327 | unsigned long len) |
| 3328 | { |
| 3329 | u64 dst_len = dst->len; |
| 3330 | size_t cur; |
| 3331 | size_t offset; |
| 3332 | struct page *page; |
| 3333 | char *kaddr; |
| 3334 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 3335 | unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT; |
| 3336 | |
| 3337 | WARN_ON(src->len != dst_len); |
| 3338 | |
| 3339 | offset = (start_offset + dst_offset) & |
| 3340 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 3341 | |
| 3342 | while(len > 0) { |
| 3343 | page = extent_buffer_page(dst, i); |
| 3344 | WARN_ON(!PageUptodate(page)); |
| 3345 | |
| 3346 | cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset)); |
| 3347 | |
| 3348 | kaddr = kmap_atomic(page, KM_USER0); |
| 3349 | read_extent_buffer(src, kaddr + offset, src_offset, cur); |
| 3350 | kunmap_atomic(kaddr, KM_USER0); |
| 3351 | |
| 3352 | src_offset += cur; |
| 3353 | len -= cur; |
| 3354 | offset = 0; |
| 3355 | i++; |
| 3356 | } |
| 3357 | } |
| 3358 | EXPORT_SYMBOL(copy_extent_buffer); |
| 3359 | |
| 3360 | static void move_pages(struct page *dst_page, struct page *src_page, |
| 3361 | unsigned long dst_off, unsigned long src_off, |
| 3362 | unsigned long len) |
| 3363 | { |
| 3364 | char *dst_kaddr = kmap_atomic(dst_page, KM_USER0); |
| 3365 | if (dst_page == src_page) { |
| 3366 | memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len); |
| 3367 | } else { |
| 3368 | char *src_kaddr = kmap_atomic(src_page, KM_USER1); |
| 3369 | char *p = dst_kaddr + dst_off + len; |
| 3370 | char *s = src_kaddr + src_off + len; |
| 3371 | |
| 3372 | while (len--) |
| 3373 | *--p = *--s; |
| 3374 | |
| 3375 | kunmap_atomic(src_kaddr, KM_USER1); |
| 3376 | } |
| 3377 | kunmap_atomic(dst_kaddr, KM_USER0); |
| 3378 | } |
| 3379 | |
| 3380 | static void copy_pages(struct page *dst_page, struct page *src_page, |
| 3381 | unsigned long dst_off, unsigned long src_off, |
| 3382 | unsigned long len) |
| 3383 | { |
| 3384 | char *dst_kaddr = kmap_atomic(dst_page, KM_USER0); |
| 3385 | char *src_kaddr; |
| 3386 | |
| 3387 | if (dst_page != src_page) |
| 3388 | src_kaddr = kmap_atomic(src_page, KM_USER1); |
| 3389 | else |
| 3390 | src_kaddr = dst_kaddr; |
| 3391 | |
| 3392 | memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len); |
| 3393 | kunmap_atomic(dst_kaddr, KM_USER0); |
| 3394 | if (dst_page != src_page) |
| 3395 | kunmap_atomic(src_kaddr, KM_USER1); |
| 3396 | } |
| 3397 | |
| 3398 | void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 3399 | unsigned long src_offset, unsigned long len) |
| 3400 | { |
| 3401 | size_t cur; |
| 3402 | size_t dst_off_in_page; |
| 3403 | size_t src_off_in_page; |
| 3404 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 3405 | unsigned long dst_i; |
| 3406 | unsigned long src_i; |
| 3407 | |
| 3408 | if (src_offset + len > dst->len) { |
| 3409 | printk("memmove bogus src_offset %lu move len %lu len %lu\n", |
| 3410 | src_offset, len, dst->len); |
| 3411 | BUG_ON(1); |
| 3412 | } |
| 3413 | if (dst_offset + len > dst->len) { |
| 3414 | printk("memmove bogus dst_offset %lu move len %lu len %lu\n", |
| 3415 | dst_offset, len, dst->len); |
| 3416 | BUG_ON(1); |
| 3417 | } |
| 3418 | |
| 3419 | while(len > 0) { |
| 3420 | dst_off_in_page = (start_offset + dst_offset) & |
| 3421 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 3422 | src_off_in_page = (start_offset + src_offset) & |
| 3423 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 3424 | |
| 3425 | dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT; |
| 3426 | src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT; |
| 3427 | |
| 3428 | cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - |
| 3429 | src_off_in_page)); |
| 3430 | cur = min_t(unsigned long, cur, |
| 3431 | (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page)); |
| 3432 | |
| 3433 | copy_pages(extent_buffer_page(dst, dst_i), |
| 3434 | extent_buffer_page(dst, src_i), |
| 3435 | dst_off_in_page, src_off_in_page, cur); |
| 3436 | |
| 3437 | src_offset += cur; |
| 3438 | dst_offset += cur; |
| 3439 | len -= cur; |
| 3440 | } |
| 3441 | } |
| 3442 | EXPORT_SYMBOL(memcpy_extent_buffer); |
| 3443 | |
| 3444 | void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 3445 | unsigned long src_offset, unsigned long len) |
| 3446 | { |
| 3447 | size_t cur; |
| 3448 | size_t dst_off_in_page; |
| 3449 | size_t src_off_in_page; |
| 3450 | unsigned long dst_end = dst_offset + len - 1; |
| 3451 | unsigned long src_end = src_offset + len - 1; |
| 3452 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 3453 | unsigned long dst_i; |
| 3454 | unsigned long src_i; |
| 3455 | |
| 3456 | if (src_offset + len > dst->len) { |
| 3457 | printk("memmove bogus src_offset %lu move len %lu len %lu\n", |
| 3458 | src_offset, len, dst->len); |
| 3459 | BUG_ON(1); |
| 3460 | } |
| 3461 | if (dst_offset + len > dst->len) { |
| 3462 | printk("memmove bogus dst_offset %lu move len %lu len %lu\n", |
| 3463 | dst_offset, len, dst->len); |
| 3464 | BUG_ON(1); |
| 3465 | } |
| 3466 | if (dst_offset < src_offset) { |
| 3467 | memcpy_extent_buffer(dst, dst_offset, src_offset, len); |
| 3468 | return; |
| 3469 | } |
| 3470 | while(len > 0) { |
| 3471 | dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT; |
| 3472 | src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT; |
| 3473 | |
| 3474 | dst_off_in_page = (start_offset + dst_end) & |
| 3475 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 3476 | src_off_in_page = (start_offset + src_end) & |
| 3477 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 3478 | |
| 3479 | cur = min_t(unsigned long, len, src_off_in_page + 1); |
| 3480 | cur = min(cur, dst_off_in_page + 1); |
| 3481 | move_pages(extent_buffer_page(dst, dst_i), |
| 3482 | extent_buffer_page(dst, src_i), |
| 3483 | dst_off_in_page - cur + 1, |
| 3484 | src_off_in_page - cur + 1, cur); |
| 3485 | |
| 3486 | dst_end -= cur; |
| 3487 | src_end -= cur; |
| 3488 | len -= cur; |
| 3489 | } |
| 3490 | } |
| 3491 | EXPORT_SYMBOL(memmove_extent_buffer); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 3492 | |
| 3493 | int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page) |
| 3494 | { |
| 3495 | u64 start = page_offset(page); |
| 3496 | struct extent_buffer *eb; |
| 3497 | int ret = 1; |
| 3498 | unsigned long i; |
| 3499 | unsigned long num_pages; |
| 3500 | |
| 3501 | spin_lock(&tree->buffer_lock); |
| 3502 | eb = buffer_search(tree, start); |
| 3503 | if (!eb) |
| 3504 | goto out; |
| 3505 | |
| 3506 | if (atomic_read(&eb->refs) > 1) { |
| 3507 | ret = 0; |
| 3508 | goto out; |
| 3509 | } |
| 3510 | /* at this point we can safely release the extent buffer */ |
| 3511 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3512 | for (i = 0; i < num_pages; i++) { |
| 3513 | struct page *page = extent_buffer_page(eb, i); |
| 3514 | page_cache_release(page); |
| 3515 | } |
| 3516 | rb_erase(&eb->rb_node, &tree->buffer); |
| 3517 | __free_extent_buffer(eb); |
| 3518 | out: |
| 3519 | spin_unlock(&tree->buffer_lock); |
| 3520 | return ret; |
| 3521 | } |
| 3522 | EXPORT_SYMBOL(try_release_extent_buffer); |