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