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