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