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