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