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 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame^] | 796 | int set_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end, |
| 797 | gfp_t mask) |
| 798 | { |
| 799 | return set_extent_bit(tree, start, end, EXTENT_ORDERED, 0, NULL, mask); |
| 800 | } |
| 801 | EXPORT_SYMBOL(set_extent_ordered); |
| 802 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 803 | int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
| 804 | int bits, gfp_t mask) |
| 805 | { |
| 806 | return set_extent_bit(tree, start, end, bits, 0, NULL, |
| 807 | mask); |
| 808 | } |
| 809 | EXPORT_SYMBOL(set_extent_bits); |
| 810 | |
| 811 | int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
| 812 | int bits, gfp_t mask) |
| 813 | { |
| 814 | return clear_extent_bit(tree, start, end, bits, 0, 0, mask); |
| 815 | } |
| 816 | EXPORT_SYMBOL(clear_extent_bits); |
| 817 | |
| 818 | int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end, |
| 819 | gfp_t mask) |
| 820 | { |
| 821 | return set_extent_bit(tree, start, end, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame^] | 822 | EXTENT_DELALLOC | EXTENT_DIRTY, |
| 823 | 0, NULL, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 824 | } |
| 825 | EXPORT_SYMBOL(set_extent_delalloc); |
| 826 | |
| 827 | int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, |
| 828 | gfp_t mask) |
| 829 | { |
| 830 | return clear_extent_bit(tree, start, end, |
| 831 | EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask); |
| 832 | } |
| 833 | EXPORT_SYMBOL(clear_extent_dirty); |
| 834 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame^] | 835 | int clear_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end, |
| 836 | gfp_t mask) |
| 837 | { |
| 838 | return clear_extent_bit(tree, start, end, EXTENT_ORDERED, 1, 0, mask); |
| 839 | } |
| 840 | EXPORT_SYMBOL(clear_extent_ordered); |
| 841 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 842 | int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end, |
| 843 | gfp_t mask) |
| 844 | { |
| 845 | return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL, |
| 846 | mask); |
| 847 | } |
| 848 | EXPORT_SYMBOL(set_extent_new); |
| 849 | |
| 850 | int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end, |
| 851 | gfp_t mask) |
| 852 | { |
| 853 | return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask); |
| 854 | } |
| 855 | EXPORT_SYMBOL(clear_extent_new); |
| 856 | |
| 857 | int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end, |
| 858 | gfp_t mask) |
| 859 | { |
| 860 | return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL, |
| 861 | mask); |
| 862 | } |
| 863 | EXPORT_SYMBOL(set_extent_uptodate); |
| 864 | |
| 865 | int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end, |
| 866 | gfp_t mask) |
| 867 | { |
| 868 | return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask); |
| 869 | } |
| 870 | EXPORT_SYMBOL(clear_extent_uptodate); |
| 871 | |
| 872 | int set_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end, |
| 873 | gfp_t mask) |
| 874 | { |
| 875 | return set_extent_bit(tree, start, end, EXTENT_WRITEBACK, |
| 876 | 0, NULL, mask); |
| 877 | } |
| 878 | EXPORT_SYMBOL(set_extent_writeback); |
| 879 | |
| 880 | int clear_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end, |
| 881 | gfp_t mask) |
| 882 | { |
| 883 | return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask); |
| 884 | } |
| 885 | EXPORT_SYMBOL(clear_extent_writeback); |
| 886 | |
| 887 | int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end) |
| 888 | { |
| 889 | return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK); |
| 890 | } |
| 891 | EXPORT_SYMBOL(wait_on_extent_writeback); |
| 892 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 893 | int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask) |
| 894 | { |
| 895 | int err; |
| 896 | u64 failed_start; |
| 897 | while (1) { |
| 898 | err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1, |
| 899 | &failed_start, mask); |
| 900 | if (err == -EEXIST && (mask & __GFP_WAIT)) { |
| 901 | wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED); |
| 902 | start = failed_start; |
| 903 | } else { |
| 904 | break; |
| 905 | } |
| 906 | WARN_ON(start > end); |
| 907 | } |
| 908 | return err; |
| 909 | } |
| 910 | EXPORT_SYMBOL(lock_extent); |
| 911 | |
| 912 | int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, |
| 913 | gfp_t mask) |
| 914 | { |
| 915 | return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask); |
| 916 | } |
| 917 | EXPORT_SYMBOL(unlock_extent); |
| 918 | |
| 919 | /* |
| 920 | * helper function to set pages and extents in the tree dirty |
| 921 | */ |
| 922 | int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end) |
| 923 | { |
| 924 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 925 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 926 | struct page *page; |
| 927 | |
| 928 | while (index <= end_index) { |
| 929 | page = find_get_page(tree->mapping, index); |
| 930 | BUG_ON(!page); |
| 931 | __set_page_dirty_nobuffers(page); |
| 932 | page_cache_release(page); |
| 933 | index++; |
| 934 | } |
| 935 | set_extent_dirty(tree, start, end, GFP_NOFS); |
| 936 | return 0; |
| 937 | } |
| 938 | EXPORT_SYMBOL(set_range_dirty); |
| 939 | |
| 940 | /* |
| 941 | * helper function to set both pages and extents in the tree writeback |
| 942 | */ |
| 943 | int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end) |
| 944 | { |
| 945 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 946 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 947 | struct page *page; |
| 948 | |
| 949 | while (index <= end_index) { |
| 950 | page = find_get_page(tree->mapping, index); |
| 951 | BUG_ON(!page); |
| 952 | set_page_writeback(page); |
| 953 | page_cache_release(page); |
| 954 | index++; |
| 955 | } |
| 956 | set_extent_writeback(tree, start, end, GFP_NOFS); |
| 957 | return 0; |
| 958 | } |
| 959 | EXPORT_SYMBOL(set_range_writeback); |
| 960 | |
| 961 | int find_first_extent_bit(struct extent_io_tree *tree, u64 start, |
| 962 | u64 *start_ret, u64 *end_ret, int bits) |
| 963 | { |
| 964 | struct rb_node *node; |
| 965 | struct extent_state *state; |
| 966 | int ret = 1; |
| 967 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 968 | spin_lock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 969 | /* |
| 970 | * this search will find all the extents that end after |
| 971 | * our range starts. |
| 972 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 973 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 974 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 975 | goto out; |
| 976 | } |
| 977 | |
| 978 | while(1) { |
| 979 | state = rb_entry(node, struct extent_state, rb_node); |
| 980 | if (state->end >= start && (state->state & bits)) { |
| 981 | *start_ret = state->start; |
| 982 | *end_ret = state->end; |
| 983 | ret = 0; |
| 984 | break; |
| 985 | } |
| 986 | node = rb_next(node); |
| 987 | if (!node) |
| 988 | break; |
| 989 | } |
| 990 | out: |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 991 | spin_unlock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 992 | return ret; |
| 993 | } |
| 994 | EXPORT_SYMBOL(find_first_extent_bit); |
| 995 | |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 996 | struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree, |
| 997 | u64 start, int bits) |
| 998 | { |
| 999 | struct rb_node *node; |
| 1000 | struct extent_state *state; |
| 1001 | |
| 1002 | /* |
| 1003 | * this search will find all the extents that end after |
| 1004 | * our range starts. |
| 1005 | */ |
| 1006 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1007 | if (!node) { |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1008 | goto out; |
| 1009 | } |
| 1010 | |
| 1011 | while(1) { |
| 1012 | state = rb_entry(node, struct extent_state, rb_node); |
| 1013 | if (state->end >= start && (state->state & bits)) { |
| 1014 | return state; |
| 1015 | } |
| 1016 | node = rb_next(node); |
| 1017 | if (!node) |
| 1018 | break; |
| 1019 | } |
| 1020 | out: |
| 1021 | return NULL; |
| 1022 | } |
| 1023 | EXPORT_SYMBOL(find_first_extent_bit_state); |
| 1024 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1025 | u64 find_lock_delalloc_range(struct extent_io_tree *tree, |
| 1026 | u64 *start, u64 *end, u64 max_bytes) |
| 1027 | { |
| 1028 | struct rb_node *node; |
| 1029 | struct extent_state *state; |
| 1030 | u64 cur_start = *start; |
| 1031 | u64 found = 0; |
| 1032 | u64 total_bytes = 0; |
| 1033 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1034 | spin_lock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1035 | /* |
| 1036 | * this search will find all the extents that end after |
| 1037 | * our range starts. |
| 1038 | */ |
| 1039 | search_again: |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1040 | node = tree_search(tree, cur_start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1041 | if (!node) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1042 | if (!found) |
| 1043 | *end = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1044 | goto out; |
| 1045 | } |
| 1046 | |
| 1047 | while(1) { |
| 1048 | state = rb_entry(node, struct extent_state, rb_node); |
| 1049 | if (found && state->start != cur_start) { |
| 1050 | goto out; |
| 1051 | } |
| 1052 | if (!(state->state & EXTENT_DELALLOC)) { |
| 1053 | if (!found) |
| 1054 | *end = state->end; |
| 1055 | goto out; |
| 1056 | } |
| 1057 | if (!found) { |
| 1058 | struct extent_state *prev_state; |
| 1059 | struct rb_node *prev_node = node; |
| 1060 | while(1) { |
| 1061 | prev_node = rb_prev(prev_node); |
| 1062 | if (!prev_node) |
| 1063 | break; |
| 1064 | prev_state = rb_entry(prev_node, |
| 1065 | struct extent_state, |
| 1066 | rb_node); |
| 1067 | if (!(prev_state->state & EXTENT_DELALLOC)) |
| 1068 | break; |
| 1069 | state = prev_state; |
| 1070 | node = prev_node; |
| 1071 | } |
| 1072 | } |
| 1073 | if (state->state & EXTENT_LOCKED) { |
| 1074 | DEFINE_WAIT(wait); |
| 1075 | atomic_inc(&state->refs); |
| 1076 | prepare_to_wait(&state->wq, &wait, |
| 1077 | TASK_UNINTERRUPTIBLE); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1078 | spin_unlock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1079 | schedule(); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1080 | spin_lock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1081 | finish_wait(&state->wq, &wait); |
| 1082 | free_extent_state(state); |
| 1083 | goto search_again; |
| 1084 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1085 | set_state_cb(tree, state, EXTENT_LOCKED); |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 1086 | state->state |= EXTENT_LOCKED; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1087 | if (!found) |
| 1088 | *start = state->start; |
| 1089 | found++; |
| 1090 | *end = state->end; |
| 1091 | cur_start = state->end + 1; |
| 1092 | node = rb_next(node); |
| 1093 | if (!node) |
| 1094 | break; |
| 1095 | total_bytes += state->end - state->start + 1; |
| 1096 | if (total_bytes >= max_bytes) |
| 1097 | break; |
| 1098 | } |
| 1099 | out: |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1100 | spin_unlock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1101 | return found; |
| 1102 | } |
| 1103 | |
| 1104 | u64 count_range_bits(struct extent_io_tree *tree, |
| 1105 | u64 *start, u64 search_end, u64 max_bytes, |
| 1106 | unsigned long bits) |
| 1107 | { |
| 1108 | struct rb_node *node; |
| 1109 | struct extent_state *state; |
| 1110 | u64 cur_start = *start; |
| 1111 | u64 total_bytes = 0; |
| 1112 | int found = 0; |
| 1113 | |
| 1114 | if (search_end <= cur_start) { |
| 1115 | printk("search_end %Lu start %Lu\n", search_end, cur_start); |
| 1116 | WARN_ON(1); |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1120 | spin_lock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1121 | if (cur_start == 0 && bits == EXTENT_DIRTY) { |
| 1122 | total_bytes = tree->dirty_bytes; |
| 1123 | goto out; |
| 1124 | } |
| 1125 | /* |
| 1126 | * this search will find all the extents that end after |
| 1127 | * our range starts. |
| 1128 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1129 | node = tree_search(tree, cur_start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1130 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1131 | goto out; |
| 1132 | } |
| 1133 | |
| 1134 | while(1) { |
| 1135 | state = rb_entry(node, struct extent_state, rb_node); |
| 1136 | if (state->start > search_end) |
| 1137 | break; |
| 1138 | if (state->end >= cur_start && (state->state & bits)) { |
| 1139 | total_bytes += min(search_end, state->end) + 1 - |
| 1140 | max(cur_start, state->start); |
| 1141 | if (total_bytes >= max_bytes) |
| 1142 | break; |
| 1143 | if (!found) { |
| 1144 | *start = state->start; |
| 1145 | found = 1; |
| 1146 | } |
| 1147 | } |
| 1148 | node = rb_next(node); |
| 1149 | if (!node) |
| 1150 | break; |
| 1151 | } |
| 1152 | out: |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1153 | spin_unlock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1154 | return total_bytes; |
| 1155 | } |
| 1156 | /* |
| 1157 | * helper function to lock both pages and extents in the tree. |
| 1158 | * pages must be locked first. |
| 1159 | */ |
| 1160 | int lock_range(struct extent_io_tree *tree, u64 start, u64 end) |
| 1161 | { |
| 1162 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1163 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1164 | struct page *page; |
| 1165 | int err; |
| 1166 | |
| 1167 | while (index <= end_index) { |
| 1168 | page = grab_cache_page(tree->mapping, index); |
| 1169 | if (!page) { |
| 1170 | err = -ENOMEM; |
| 1171 | goto failed; |
| 1172 | } |
| 1173 | if (IS_ERR(page)) { |
| 1174 | err = PTR_ERR(page); |
| 1175 | goto failed; |
| 1176 | } |
| 1177 | index++; |
| 1178 | } |
| 1179 | lock_extent(tree, start, end, GFP_NOFS); |
| 1180 | return 0; |
| 1181 | |
| 1182 | failed: |
| 1183 | /* |
| 1184 | * we failed above in getting the page at 'index', so we undo here |
| 1185 | * up to but not including the page at 'index' |
| 1186 | */ |
| 1187 | end_index = index; |
| 1188 | index = start >> PAGE_CACHE_SHIFT; |
| 1189 | while (index < end_index) { |
| 1190 | page = find_get_page(tree->mapping, index); |
| 1191 | unlock_page(page); |
| 1192 | page_cache_release(page); |
| 1193 | index++; |
| 1194 | } |
| 1195 | return err; |
| 1196 | } |
| 1197 | EXPORT_SYMBOL(lock_range); |
| 1198 | |
| 1199 | /* |
| 1200 | * helper function to unlock both pages and extents in the tree. |
| 1201 | */ |
| 1202 | int unlock_range(struct extent_io_tree *tree, u64 start, u64 end) |
| 1203 | { |
| 1204 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1205 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1206 | struct page *page; |
| 1207 | |
| 1208 | while (index <= end_index) { |
| 1209 | page = find_get_page(tree->mapping, index); |
| 1210 | unlock_page(page); |
| 1211 | page_cache_release(page); |
| 1212 | index++; |
| 1213 | } |
| 1214 | unlock_extent(tree, start, end, GFP_NOFS); |
| 1215 | return 0; |
| 1216 | } |
| 1217 | EXPORT_SYMBOL(unlock_range); |
| 1218 | |
| 1219 | int set_state_private(struct extent_io_tree *tree, u64 start, u64 private) |
| 1220 | { |
| 1221 | struct rb_node *node; |
| 1222 | struct extent_state *state; |
| 1223 | int ret = 0; |
| 1224 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1225 | spin_lock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1226 | /* |
| 1227 | * this search will find all the extents that end after |
| 1228 | * our range starts. |
| 1229 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1230 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1231 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1232 | ret = -ENOENT; |
| 1233 | goto out; |
| 1234 | } |
| 1235 | state = rb_entry(node, struct extent_state, rb_node); |
| 1236 | if (state->start != start) { |
| 1237 | ret = -ENOENT; |
| 1238 | goto out; |
| 1239 | } |
| 1240 | state->private = private; |
| 1241 | out: |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1242 | spin_unlock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1243 | return ret; |
| 1244 | } |
| 1245 | |
| 1246 | int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private) |
| 1247 | { |
| 1248 | struct rb_node *node; |
| 1249 | struct extent_state *state; |
| 1250 | int ret = 0; |
| 1251 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1252 | spin_lock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1253 | /* |
| 1254 | * this search will find all the extents that end after |
| 1255 | * our range starts. |
| 1256 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1257 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1258 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1259 | ret = -ENOENT; |
| 1260 | goto out; |
| 1261 | } |
| 1262 | state = rb_entry(node, struct extent_state, rb_node); |
| 1263 | if (state->start != start) { |
| 1264 | ret = -ENOENT; |
| 1265 | goto out; |
| 1266 | } |
| 1267 | *private = state->private; |
| 1268 | out: |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1269 | spin_unlock_irq(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1270 | return ret; |
| 1271 | } |
| 1272 | |
| 1273 | /* |
| 1274 | * searches a range in the state tree for a given mask. |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1275 | * 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] | 1276 | * has the bits set. Otherwise, 1 is returned if any bit in the |
| 1277 | * range is found set. |
| 1278 | */ |
| 1279 | int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 1280 | int bits, int filled) |
| 1281 | { |
| 1282 | struct extent_state *state = NULL; |
| 1283 | struct rb_node *node; |
| 1284 | int bitset = 0; |
| 1285 | unsigned long flags; |
| 1286 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1287 | spin_lock_irqsave(&tree->lock, flags); |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1288 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1289 | while (node && start <= end) { |
| 1290 | state = rb_entry(node, struct extent_state, rb_node); |
| 1291 | |
| 1292 | if (filled && state->start > start) { |
| 1293 | bitset = 0; |
| 1294 | break; |
| 1295 | } |
| 1296 | |
| 1297 | if (state->start > end) |
| 1298 | break; |
| 1299 | |
| 1300 | if (state->state & bits) { |
| 1301 | bitset = 1; |
| 1302 | if (!filled) |
| 1303 | break; |
| 1304 | } else if (filled) { |
| 1305 | bitset = 0; |
| 1306 | break; |
| 1307 | } |
| 1308 | start = state->end + 1; |
| 1309 | if (start > end) |
| 1310 | break; |
| 1311 | node = rb_next(node); |
| 1312 | if (!node) { |
| 1313 | if (filled) |
| 1314 | bitset = 0; |
| 1315 | break; |
| 1316 | } |
| 1317 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1318 | spin_unlock_irqrestore(&tree->lock, flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1319 | return bitset; |
| 1320 | } |
| 1321 | EXPORT_SYMBOL(test_range_bit); |
| 1322 | |
| 1323 | /* |
| 1324 | * helper function to set a given page up to date if all the |
| 1325 | * extents in the tree for that page are up to date |
| 1326 | */ |
| 1327 | static int check_page_uptodate(struct extent_io_tree *tree, |
| 1328 | struct page *page) |
| 1329 | { |
| 1330 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 1331 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 1332 | if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1)) |
| 1333 | SetPageUptodate(page); |
| 1334 | return 0; |
| 1335 | } |
| 1336 | |
| 1337 | /* |
| 1338 | * helper function to unlock a page if all the extents in the tree |
| 1339 | * for that page are unlocked |
| 1340 | */ |
| 1341 | static int check_page_locked(struct extent_io_tree *tree, |
| 1342 | struct page *page) |
| 1343 | { |
| 1344 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 1345 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 1346 | if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0)) |
| 1347 | unlock_page(page); |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
| 1351 | /* |
| 1352 | * helper function to end page writeback if all the extents |
| 1353 | * in the tree for that page are done with writeback |
| 1354 | */ |
| 1355 | static int check_page_writeback(struct extent_io_tree *tree, |
| 1356 | struct page *page) |
| 1357 | { |
| 1358 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 1359 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 1360 | if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0)) |
| 1361 | end_page_writeback(page); |
| 1362 | return 0; |
| 1363 | } |
| 1364 | |
| 1365 | /* lots and lots of room for performance fixes in the end_bio funcs */ |
| 1366 | |
| 1367 | /* |
| 1368 | * after a writepage IO is done, we need to: |
| 1369 | * clear the uptodate bits on error |
| 1370 | * clear the writeback bits in the extent tree for this IO |
| 1371 | * end_page_writeback if the page has no more pending IO |
| 1372 | * |
| 1373 | * Scheduling is not allowed, so the extent state tree is expected |
| 1374 | * to have one and only one object corresponding to this IO. |
| 1375 | */ |
| 1376 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
| 1377 | static void end_bio_extent_writepage(struct bio *bio, int err) |
| 1378 | #else |
| 1379 | static int end_bio_extent_writepage(struct bio *bio, |
| 1380 | unsigned int bytes_done, int err) |
| 1381 | #endif |
| 1382 | { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1383 | int uptodate = err == 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1384 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1385 | struct extent_state *state = bio->bi_private; |
| 1386 | struct extent_io_tree *tree = state->tree; |
| 1387 | struct rb_node *node; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1388 | u64 start; |
| 1389 | u64 end; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1390 | u64 cur; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1391 | int whole_page; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1392 | int ret; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1393 | unsigned long flags; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1394 | |
| 1395 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 1396 | if (bio->bi_size) |
| 1397 | return 1; |
| 1398 | #endif |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1399 | do { |
| 1400 | struct page *page = bvec->bv_page; |
| 1401 | start = ((u64)page->index << PAGE_CACHE_SHIFT) + |
| 1402 | bvec->bv_offset; |
| 1403 | end = start + bvec->bv_len - 1; |
| 1404 | |
| 1405 | if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE) |
| 1406 | whole_page = 1; |
| 1407 | else |
| 1408 | whole_page = 0; |
| 1409 | |
| 1410 | if (--bvec >= bio->bi_io_vec) |
| 1411 | prefetchw(&bvec->bv_page->flags); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1412 | if (tree->ops && tree->ops->writepage_end_io_hook) { |
| 1413 | ret = tree->ops->writepage_end_io_hook(page, start, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame^] | 1414 | end, state, uptodate); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 1415 | if (ret) |
| 1416 | uptodate = 0; |
| 1417 | } |
| 1418 | |
| 1419 | if (!uptodate && tree->ops && |
| 1420 | tree->ops->writepage_io_failed_hook) { |
| 1421 | ret = tree->ops->writepage_io_failed_hook(bio, page, |
| 1422 | start, end, state); |
| 1423 | if (ret == 0) { |
| 1424 | state = NULL; |
| 1425 | uptodate = (err == 0); |
| 1426 | continue; |
| 1427 | } |
| 1428 | } |
| 1429 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1430 | if (!uptodate) { |
| 1431 | clear_extent_uptodate(tree, start, end, GFP_ATOMIC); |
| 1432 | ClearPageUptodate(page); |
| 1433 | SetPageError(page); |
| 1434 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1435 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1436 | /* |
| 1437 | * bios can get merged in funny ways, and so we need to |
| 1438 | * be careful with the state variable. We know the |
| 1439 | * state won't be merged with others because it has |
| 1440 | * WRITEBACK set, but we can't be sure each biovec is |
| 1441 | * sequential in the file. So, if our cached state |
| 1442 | * doesn't match the expected end, search the tree |
| 1443 | * for the correct one. |
| 1444 | */ |
| 1445 | |
| 1446 | spin_lock_irqsave(&tree->lock, flags); |
| 1447 | if (!state || state->end != end) { |
| 1448 | state = NULL; |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1449 | node = __etree_search(tree, start, NULL, NULL); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1450 | if (node) { |
| 1451 | state = rb_entry(node, struct extent_state, |
| 1452 | rb_node); |
| 1453 | if (state->end != end || |
| 1454 | !(state->state & EXTENT_WRITEBACK)) |
| 1455 | state = NULL; |
| 1456 | } |
| 1457 | if (!state) { |
| 1458 | spin_unlock_irqrestore(&tree->lock, flags); |
| 1459 | clear_extent_writeback(tree, start, |
| 1460 | end, GFP_ATOMIC); |
| 1461 | goto next_io; |
| 1462 | } |
| 1463 | } |
| 1464 | cur = end; |
| 1465 | while(1) { |
| 1466 | struct extent_state *clear = state; |
| 1467 | cur = state->start; |
| 1468 | node = rb_prev(&state->rb_node); |
| 1469 | if (node) { |
| 1470 | state = rb_entry(node, |
| 1471 | struct extent_state, |
| 1472 | rb_node); |
| 1473 | } else { |
| 1474 | state = NULL; |
| 1475 | } |
| 1476 | |
| 1477 | clear_state_bit(tree, clear, EXTENT_WRITEBACK, |
| 1478 | 1, 0); |
| 1479 | if (cur == start) |
| 1480 | break; |
| 1481 | if (cur < start) { |
| 1482 | WARN_ON(1); |
| 1483 | break; |
| 1484 | } |
| 1485 | if (!node) |
| 1486 | break; |
| 1487 | } |
| 1488 | /* before releasing the lock, make sure the next state |
| 1489 | * variable has the expected bits set and corresponds |
| 1490 | * to the correct offsets in the file |
| 1491 | */ |
| 1492 | if (state && (state->end + 1 != start || |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 1493 | !(state->state & EXTENT_WRITEBACK))) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1494 | state = NULL; |
| 1495 | } |
| 1496 | spin_unlock_irqrestore(&tree->lock, flags); |
| 1497 | next_io: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1498 | |
| 1499 | if (whole_page) |
| 1500 | end_page_writeback(page); |
| 1501 | else |
| 1502 | check_page_writeback(tree, page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1503 | } while (bvec >= bio->bi_io_vec); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1504 | bio_put(bio); |
| 1505 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 1506 | return 0; |
| 1507 | #endif |
| 1508 | } |
| 1509 | |
| 1510 | /* |
| 1511 | * after a readpage IO is done, we need to: |
| 1512 | * clear the uptodate bits on error |
| 1513 | * set the uptodate bits if things worked |
| 1514 | * set the page up to date if all extents in the tree are uptodate |
| 1515 | * clear the lock bit in the extent tree |
| 1516 | * unlock the page if there are no other extents locked for it |
| 1517 | * |
| 1518 | * Scheduling is not allowed, so the extent state tree is expected |
| 1519 | * to have one and only one object corresponding to this IO. |
| 1520 | */ |
| 1521 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
| 1522 | static void end_bio_extent_readpage(struct bio *bio, int err) |
| 1523 | #else |
| 1524 | static int end_bio_extent_readpage(struct bio *bio, |
| 1525 | unsigned int bytes_done, int err) |
| 1526 | #endif |
| 1527 | { |
| 1528 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 1529 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1530 | struct extent_state *state = bio->bi_private; |
| 1531 | struct extent_io_tree *tree = state->tree; |
| 1532 | struct rb_node *node; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1533 | u64 start; |
| 1534 | u64 end; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1535 | u64 cur; |
| 1536 | unsigned long flags; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1537 | int whole_page; |
| 1538 | int ret; |
| 1539 | |
| 1540 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 1541 | if (bio->bi_size) |
| 1542 | return 1; |
| 1543 | #endif |
| 1544 | |
| 1545 | do { |
| 1546 | struct page *page = bvec->bv_page; |
| 1547 | start = ((u64)page->index << PAGE_CACHE_SHIFT) + |
| 1548 | bvec->bv_offset; |
| 1549 | end = start + bvec->bv_len - 1; |
| 1550 | |
| 1551 | if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE) |
| 1552 | whole_page = 1; |
| 1553 | else |
| 1554 | whole_page = 0; |
| 1555 | |
| 1556 | if (--bvec >= bio->bi_io_vec) |
| 1557 | prefetchw(&bvec->bv_page->flags); |
| 1558 | |
| 1559 | if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1560 | ret = tree->ops->readpage_end_io_hook(page, start, end, |
| 1561 | state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1562 | if (ret) |
| 1563 | uptodate = 0; |
| 1564 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1565 | if (!uptodate && tree->ops && |
| 1566 | tree->ops->readpage_io_failed_hook) { |
| 1567 | ret = tree->ops->readpage_io_failed_hook(bio, page, |
| 1568 | start, end, state); |
| 1569 | if (ret == 0) { |
| 1570 | state = NULL; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1571 | uptodate = |
| 1572 | test_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1573 | continue; |
| 1574 | } |
| 1575 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1576 | |
| 1577 | spin_lock_irqsave(&tree->lock, flags); |
| 1578 | if (!state || state->end != end) { |
| 1579 | state = NULL; |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1580 | node = __etree_search(tree, start, NULL, NULL); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1581 | if (node) { |
| 1582 | state = rb_entry(node, struct extent_state, |
| 1583 | rb_node); |
| 1584 | if (state->end != end || |
| 1585 | !(state->state & EXTENT_LOCKED)) |
| 1586 | state = NULL; |
| 1587 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1588 | if (!state) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1589 | spin_unlock_irqrestore(&tree->lock, flags); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1590 | if (uptodate) |
| 1591 | set_extent_uptodate(tree, start, end, |
| 1592 | GFP_ATOMIC); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1593 | unlock_extent(tree, start, end, GFP_ATOMIC); |
| 1594 | goto next_io; |
| 1595 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1596 | } |
| 1597 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1598 | cur = end; |
| 1599 | while(1) { |
| 1600 | struct extent_state *clear = state; |
| 1601 | cur = state->start; |
| 1602 | node = rb_prev(&state->rb_node); |
| 1603 | if (node) { |
| 1604 | state = rb_entry(node, |
| 1605 | struct extent_state, |
| 1606 | rb_node); |
| 1607 | } else { |
| 1608 | state = NULL; |
| 1609 | } |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1610 | if (uptodate) { |
| 1611 | set_state_cb(tree, clear, EXTENT_UPTODATE); |
| 1612 | clear->state |= EXTENT_UPTODATE; |
| 1613 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1614 | clear_state_bit(tree, clear, EXTENT_LOCKED, |
| 1615 | 1, 0); |
| 1616 | if (cur == start) |
| 1617 | break; |
| 1618 | if (cur < start) { |
| 1619 | WARN_ON(1); |
| 1620 | break; |
| 1621 | } |
| 1622 | if (!node) |
| 1623 | break; |
| 1624 | } |
| 1625 | /* before releasing the lock, make sure the next state |
| 1626 | * variable has the expected bits set and corresponds |
| 1627 | * to the correct offsets in the file |
| 1628 | */ |
| 1629 | if (state && (state->end + 1 != start || |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 1630 | !(state->state & EXTENT_LOCKED))) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1631 | state = NULL; |
| 1632 | } |
| 1633 | spin_unlock_irqrestore(&tree->lock, flags); |
| 1634 | next_io: |
| 1635 | if (whole_page) { |
| 1636 | if (uptodate) { |
| 1637 | SetPageUptodate(page); |
| 1638 | } else { |
| 1639 | ClearPageUptodate(page); |
| 1640 | SetPageError(page); |
| 1641 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1642 | unlock_page(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1643 | } else { |
| 1644 | if (uptodate) { |
| 1645 | check_page_uptodate(tree, page); |
| 1646 | } else { |
| 1647 | ClearPageUptodate(page); |
| 1648 | SetPageError(page); |
| 1649 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1650 | check_page_locked(tree, page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1651 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1652 | } while (bvec >= bio->bi_io_vec); |
| 1653 | |
| 1654 | bio_put(bio); |
| 1655 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 1656 | return 0; |
| 1657 | #endif |
| 1658 | } |
| 1659 | |
| 1660 | /* |
| 1661 | * IO done from prepare_write is pretty simple, we just unlock |
| 1662 | * the structs in the extent tree when done, and set the uptodate bits |
| 1663 | * as appropriate. |
| 1664 | */ |
| 1665 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
| 1666 | static void end_bio_extent_preparewrite(struct bio *bio, int err) |
| 1667 | #else |
| 1668 | static int end_bio_extent_preparewrite(struct bio *bio, |
| 1669 | unsigned int bytes_done, int err) |
| 1670 | #endif |
| 1671 | { |
| 1672 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 1673 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1674 | struct extent_state *state = bio->bi_private; |
| 1675 | struct extent_io_tree *tree = state->tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1676 | u64 start; |
| 1677 | u64 end; |
| 1678 | |
| 1679 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 1680 | if (bio->bi_size) |
| 1681 | return 1; |
| 1682 | #endif |
| 1683 | |
| 1684 | do { |
| 1685 | struct page *page = bvec->bv_page; |
| 1686 | start = ((u64)page->index << PAGE_CACHE_SHIFT) + |
| 1687 | bvec->bv_offset; |
| 1688 | end = start + bvec->bv_len - 1; |
| 1689 | |
| 1690 | if (--bvec >= bio->bi_io_vec) |
| 1691 | prefetchw(&bvec->bv_page->flags); |
| 1692 | |
| 1693 | if (uptodate) { |
| 1694 | set_extent_uptodate(tree, start, end, GFP_ATOMIC); |
| 1695 | } else { |
| 1696 | ClearPageUptodate(page); |
| 1697 | SetPageError(page); |
| 1698 | } |
| 1699 | |
| 1700 | unlock_extent(tree, start, end, GFP_ATOMIC); |
| 1701 | |
| 1702 | } while (bvec >= bio->bi_io_vec); |
| 1703 | |
| 1704 | bio_put(bio); |
| 1705 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 1706 | return 0; |
| 1707 | #endif |
| 1708 | } |
| 1709 | |
| 1710 | static struct bio * |
| 1711 | extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs, |
| 1712 | gfp_t gfp_flags) |
| 1713 | { |
| 1714 | struct bio *bio; |
| 1715 | |
| 1716 | bio = bio_alloc(gfp_flags, nr_vecs); |
| 1717 | |
| 1718 | if (bio == NULL && (current->flags & PF_MEMALLOC)) { |
| 1719 | while (!bio && (nr_vecs /= 2)) |
| 1720 | bio = bio_alloc(gfp_flags, nr_vecs); |
| 1721 | } |
| 1722 | |
| 1723 | if (bio) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 1724 | bio->bi_size = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1725 | bio->bi_bdev = bdev; |
| 1726 | bio->bi_sector = first_sector; |
| 1727 | } |
| 1728 | return bio; |
| 1729 | } |
| 1730 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1731 | static int submit_one_bio(int rw, struct bio *bio, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1732 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1733 | int ret = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1734 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 1735 | struct page *page = bvec->bv_page; |
| 1736 | struct extent_io_tree *tree = bio->bi_private; |
| 1737 | struct rb_node *node; |
| 1738 | struct extent_state *state; |
| 1739 | u64 start; |
| 1740 | u64 end; |
| 1741 | |
| 1742 | start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset; |
| 1743 | end = start + bvec->bv_len - 1; |
| 1744 | |
| 1745 | spin_lock_irq(&tree->lock); |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1746 | node = __etree_search(tree, start, NULL, NULL); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1747 | BUG_ON(!node); |
| 1748 | state = rb_entry(node, struct extent_state, rb_node); |
| 1749 | while(state->end < end) { |
| 1750 | node = rb_next(node); |
| 1751 | state = rb_entry(node, struct extent_state, rb_node); |
| 1752 | } |
| 1753 | BUG_ON(state->end != end); |
| 1754 | spin_unlock_irq(&tree->lock); |
| 1755 | |
| 1756 | bio->bi_private = state; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1757 | |
| 1758 | bio_get(bio); |
| 1759 | |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1760 | if (tree->ops && tree->ops->submit_bio_hook) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1761 | tree->ops->submit_bio_hook(page->mapping->host, rw, bio, |
| 1762 | mirror_num); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1763 | else |
| 1764 | submit_bio(rw, bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1765 | if (bio_flagged(bio, BIO_EOPNOTSUPP)) |
| 1766 | ret = -EOPNOTSUPP; |
| 1767 | bio_put(bio); |
| 1768 | return ret; |
| 1769 | } |
| 1770 | |
| 1771 | static int submit_extent_page(int rw, struct extent_io_tree *tree, |
| 1772 | struct page *page, sector_t sector, |
| 1773 | size_t size, unsigned long offset, |
| 1774 | struct block_device *bdev, |
| 1775 | struct bio **bio_ret, |
| 1776 | unsigned long max_pages, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1777 | bio_end_io_t end_io_func, |
| 1778 | int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1779 | { |
| 1780 | int ret = 0; |
| 1781 | struct bio *bio; |
| 1782 | int nr; |
| 1783 | |
| 1784 | if (bio_ret && *bio_ret) { |
| 1785 | bio = *bio_ret; |
| 1786 | if (bio->bi_sector + (bio->bi_size >> 9) != sector || |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1787 | (tree->ops && tree->ops->merge_bio_hook && |
| 1788 | tree->ops->merge_bio_hook(page, offset, size, bio)) || |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1789 | bio_add_page(bio, page, size, offset) < size) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1790 | ret = submit_one_bio(rw, bio, mirror_num); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1791 | bio = NULL; |
| 1792 | } else { |
| 1793 | return 0; |
| 1794 | } |
| 1795 | } |
Chris Mason | 961d023 | 2008-02-06 11:01:42 -0500 | [diff] [blame] | 1796 | nr = bio_get_nr_vecs(bdev); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1797 | bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH); |
| 1798 | if (!bio) { |
| 1799 | printk("failed to allocate bio nr %d\n", nr); |
| 1800 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1801 | |
| 1802 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1803 | bio_add_page(bio, page, size, offset); |
| 1804 | bio->bi_end_io = end_io_func; |
| 1805 | bio->bi_private = tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1806 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1807 | if (bio_ret) { |
| 1808 | *bio_ret = bio; |
| 1809 | } else { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1810 | ret = submit_one_bio(rw, bio, mirror_num); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | return ret; |
| 1814 | } |
| 1815 | |
| 1816 | void set_page_extent_mapped(struct page *page) |
| 1817 | { |
| 1818 | if (!PagePrivate(page)) { |
| 1819 | SetPagePrivate(page); |
| 1820 | WARN_ON(!page->mapping->a_ops->invalidatepage); |
| 1821 | set_page_private(page, EXTENT_PAGE_PRIVATE); |
| 1822 | page_cache_get(page); |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | void set_page_extent_head(struct page *page, unsigned long len) |
| 1827 | { |
| 1828 | set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2); |
| 1829 | } |
| 1830 | |
| 1831 | /* |
| 1832 | * basic readpage implementation. Locked extent state structs are inserted |
| 1833 | * into the tree that are removed when the IO is done (by the end_io |
| 1834 | * handlers) |
| 1835 | */ |
| 1836 | static int __extent_read_full_page(struct extent_io_tree *tree, |
| 1837 | struct page *page, |
| 1838 | get_extent_t *get_extent, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1839 | struct bio **bio, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1840 | { |
| 1841 | struct inode *inode = page->mapping->host; |
| 1842 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 1843 | u64 page_end = start + PAGE_CACHE_SIZE - 1; |
| 1844 | u64 end; |
| 1845 | u64 cur = start; |
| 1846 | u64 extent_offset; |
| 1847 | u64 last_byte = i_size_read(inode); |
| 1848 | u64 block_start; |
| 1849 | u64 cur_end; |
| 1850 | sector_t sector; |
| 1851 | struct extent_map *em; |
| 1852 | struct block_device *bdev; |
| 1853 | int ret; |
| 1854 | int nr = 0; |
| 1855 | size_t page_offset = 0; |
| 1856 | size_t iosize; |
| 1857 | size_t blocksize = inode->i_sb->s_blocksize; |
| 1858 | |
| 1859 | set_page_extent_mapped(page); |
| 1860 | |
| 1861 | end = page_end; |
| 1862 | lock_extent(tree, start, end, GFP_NOFS); |
| 1863 | |
| 1864 | while (cur <= end) { |
| 1865 | if (cur >= last_byte) { |
| 1866 | char *userpage; |
| 1867 | iosize = PAGE_CACHE_SIZE - page_offset; |
| 1868 | userpage = kmap_atomic(page, KM_USER0); |
| 1869 | memset(userpage + page_offset, 0, iosize); |
| 1870 | flush_dcache_page(page); |
| 1871 | kunmap_atomic(userpage, KM_USER0); |
| 1872 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
| 1873 | GFP_NOFS); |
| 1874 | unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS); |
| 1875 | break; |
| 1876 | } |
| 1877 | em = get_extent(inode, page, page_offset, cur, |
| 1878 | end - cur + 1, 0); |
| 1879 | if (IS_ERR(em) || !em) { |
| 1880 | SetPageError(page); |
| 1881 | unlock_extent(tree, cur, end, GFP_NOFS); |
| 1882 | break; |
| 1883 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1884 | extent_offset = cur - em->start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame^] | 1885 | if (extent_map_end(em) <= cur) { |
| 1886 | printk("bad mapping em [%Lu %Lu] cur %Lu\n", em->start, extent_map_end(em), cur); |
| 1887 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1888 | BUG_ON(extent_map_end(em) <= cur); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame^] | 1889 | if (end < cur) { |
| 1890 | printk("2bad mapping end %Lu cur %Lu\n", end, cur); |
| 1891 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1892 | BUG_ON(end < cur); |
| 1893 | |
| 1894 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
| 1895 | cur_end = min(extent_map_end(em) - 1, end); |
| 1896 | iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1); |
| 1897 | sector = (em->block_start + extent_offset) >> 9; |
| 1898 | bdev = em->bdev; |
| 1899 | block_start = em->block_start; |
| 1900 | free_extent_map(em); |
| 1901 | em = NULL; |
| 1902 | |
| 1903 | /* we've found a hole, just zero and go on */ |
| 1904 | if (block_start == EXTENT_MAP_HOLE) { |
| 1905 | char *userpage; |
| 1906 | userpage = kmap_atomic(page, KM_USER0); |
| 1907 | memset(userpage + page_offset, 0, iosize); |
| 1908 | flush_dcache_page(page); |
| 1909 | kunmap_atomic(userpage, KM_USER0); |
| 1910 | |
| 1911 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
| 1912 | GFP_NOFS); |
| 1913 | unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS); |
| 1914 | cur = cur + iosize; |
| 1915 | page_offset += iosize; |
| 1916 | continue; |
| 1917 | } |
| 1918 | /* the get_extent function already copied into the page */ |
| 1919 | if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) { |
| 1920 | unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS); |
| 1921 | cur = cur + iosize; |
| 1922 | page_offset += iosize; |
| 1923 | continue; |
| 1924 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1925 | /* we have an inline extent but it didn't get marked up |
| 1926 | * to date. Error out |
| 1927 | */ |
| 1928 | if (block_start == EXTENT_MAP_INLINE) { |
| 1929 | SetPageError(page); |
| 1930 | unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS); |
| 1931 | cur = cur + iosize; |
| 1932 | page_offset += iosize; |
| 1933 | continue; |
| 1934 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1935 | |
| 1936 | ret = 0; |
| 1937 | if (tree->ops && tree->ops->readpage_io_hook) { |
| 1938 | ret = tree->ops->readpage_io_hook(page, cur, |
| 1939 | cur + iosize - 1); |
| 1940 | } |
| 1941 | if (!ret) { |
| 1942 | unsigned long nr = (last_byte >> PAGE_CACHE_SHIFT) + 1; |
| 1943 | nr -= page->index; |
| 1944 | ret = submit_extent_page(READ, tree, page, |
| 1945 | sector, iosize, page_offset, |
| 1946 | bdev, bio, nr, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1947 | end_bio_extent_readpage, mirror_num); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1948 | } |
| 1949 | if (ret) |
| 1950 | SetPageError(page); |
| 1951 | cur = cur + iosize; |
| 1952 | page_offset += iosize; |
| 1953 | nr++; |
| 1954 | } |
| 1955 | if (!nr) { |
| 1956 | if (!PageError(page)) |
| 1957 | SetPageUptodate(page); |
| 1958 | unlock_page(page); |
| 1959 | } |
| 1960 | return 0; |
| 1961 | } |
| 1962 | |
| 1963 | int extent_read_full_page(struct extent_io_tree *tree, struct page *page, |
| 1964 | get_extent_t *get_extent) |
| 1965 | { |
| 1966 | struct bio *bio = NULL; |
| 1967 | int ret; |
| 1968 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1969 | ret = __extent_read_full_page(tree, page, get_extent, &bio, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1970 | if (bio) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1971 | submit_one_bio(READ, bio, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1972 | return ret; |
| 1973 | } |
| 1974 | EXPORT_SYMBOL(extent_read_full_page); |
| 1975 | |
| 1976 | /* |
| 1977 | * the writepage semantics are similar to regular writepage. extent |
| 1978 | * records are inserted to lock ranges in the tree, and as dirty areas |
| 1979 | * are found, they are marked writeback. Then the lock bits are removed |
| 1980 | * and the end_io handler clears the writeback ranges |
| 1981 | */ |
| 1982 | static int __extent_writepage(struct page *page, struct writeback_control *wbc, |
| 1983 | void *data) |
| 1984 | { |
| 1985 | struct inode *inode = page->mapping->host; |
| 1986 | struct extent_page_data *epd = data; |
| 1987 | struct extent_io_tree *tree = epd->tree; |
| 1988 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 1989 | u64 delalloc_start; |
| 1990 | u64 page_end = start + PAGE_CACHE_SIZE - 1; |
| 1991 | u64 end; |
| 1992 | u64 cur = start; |
| 1993 | u64 extent_offset; |
| 1994 | u64 last_byte = i_size_read(inode); |
| 1995 | u64 block_start; |
| 1996 | u64 iosize; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame^] | 1997 | u64 unlock_start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1998 | sector_t sector; |
| 1999 | struct extent_map *em; |
| 2000 | struct block_device *bdev; |
| 2001 | int ret; |
| 2002 | int nr = 0; |
| 2003 | size_t page_offset = 0; |
| 2004 | size_t blocksize; |
| 2005 | loff_t i_size = i_size_read(inode); |
| 2006 | unsigned long end_index = i_size >> PAGE_CACHE_SHIFT; |
| 2007 | u64 nr_delalloc; |
| 2008 | u64 delalloc_end; |
| 2009 | |
| 2010 | WARN_ON(!PageLocked(page)); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 2011 | page_offset = i_size & (PAGE_CACHE_SIZE - 1); |
| 2012 | if (page->index > end_index || |
| 2013 | (page->index == end_index && !page_offset)) { |
| 2014 | page->mapping->a_ops->invalidatepage(page, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2015 | unlock_page(page); |
| 2016 | return 0; |
| 2017 | } |
| 2018 | |
| 2019 | if (page->index == end_index) { |
| 2020 | char *userpage; |
| 2021 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2022 | userpage = kmap_atomic(page, KM_USER0); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 2023 | memset(userpage + page_offset, 0, |
| 2024 | PAGE_CACHE_SIZE - page_offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2025 | kunmap_atomic(userpage, KM_USER0); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 2026 | flush_dcache_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2027 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 2028 | page_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2029 | |
| 2030 | set_page_extent_mapped(page); |
| 2031 | |
| 2032 | delalloc_start = start; |
| 2033 | delalloc_end = 0; |
| 2034 | while(delalloc_end < page_end) { |
| 2035 | nr_delalloc = find_lock_delalloc_range(tree, &delalloc_start, |
| 2036 | &delalloc_end, |
| 2037 | 128 * 1024 * 1024); |
| 2038 | if (nr_delalloc == 0) { |
| 2039 | delalloc_start = delalloc_end + 1; |
| 2040 | continue; |
| 2041 | } |
| 2042 | tree->ops->fill_delalloc(inode, delalloc_start, |
| 2043 | delalloc_end); |
| 2044 | clear_extent_bit(tree, delalloc_start, |
| 2045 | delalloc_end, |
| 2046 | EXTENT_LOCKED | EXTENT_DELALLOC, |
| 2047 | 1, 0, GFP_NOFS); |
| 2048 | delalloc_start = delalloc_end + 1; |
| 2049 | } |
| 2050 | lock_extent(tree, start, page_end, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame^] | 2051 | unlock_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2052 | |
| 2053 | end = page_end; |
| 2054 | if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) { |
| 2055 | printk("found delalloc bits after lock_extent\n"); |
| 2056 | } |
| 2057 | |
| 2058 | if (last_byte <= start) { |
| 2059 | clear_extent_dirty(tree, start, page_end, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame^] | 2060 | unlock_extent(tree, start, page_end, GFP_NOFS); |
| 2061 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 2062 | tree->ops->writepage_end_io_hook(page, start, |
| 2063 | page_end, NULL, 1); |
| 2064 | unlock_start = page_end + 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2065 | goto done; |
| 2066 | } |
| 2067 | |
| 2068 | set_extent_uptodate(tree, start, page_end, GFP_NOFS); |
| 2069 | blocksize = inode->i_sb->s_blocksize; |
| 2070 | |
| 2071 | while (cur <= end) { |
| 2072 | if (cur >= last_byte) { |
| 2073 | clear_extent_dirty(tree, cur, page_end, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame^] | 2074 | unlock_extent(tree, unlock_start, page_end, GFP_NOFS); |
| 2075 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 2076 | tree->ops->writepage_end_io_hook(page, cur, |
| 2077 | page_end, NULL, 1); |
| 2078 | unlock_start = page_end + 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2079 | break; |
| 2080 | } |
| 2081 | em = epd->get_extent(inode, page, page_offset, cur, |
| 2082 | end - cur + 1, 1); |
| 2083 | if (IS_ERR(em) || !em) { |
| 2084 | SetPageError(page); |
| 2085 | break; |
| 2086 | } |
| 2087 | |
| 2088 | extent_offset = cur - em->start; |
| 2089 | BUG_ON(extent_map_end(em) <= cur); |
| 2090 | BUG_ON(end < cur); |
| 2091 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
| 2092 | iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1); |
| 2093 | sector = (em->block_start + extent_offset) >> 9; |
| 2094 | bdev = em->bdev; |
| 2095 | block_start = em->block_start; |
| 2096 | free_extent_map(em); |
| 2097 | em = NULL; |
| 2098 | |
| 2099 | if (block_start == EXTENT_MAP_HOLE || |
| 2100 | block_start == EXTENT_MAP_INLINE) { |
| 2101 | clear_extent_dirty(tree, cur, |
| 2102 | cur + iosize - 1, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame^] | 2103 | |
| 2104 | unlock_extent(tree, unlock_start, cur + iosize -1, |
| 2105 | GFP_NOFS); |
| 2106 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 2107 | tree->ops->writepage_end_io_hook(page, cur, |
| 2108 | cur + iosize - 1, |
| 2109 | NULL, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2110 | cur = cur + iosize; |
| 2111 | page_offset += iosize; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame^] | 2112 | unlock_start = cur; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2113 | continue; |
| 2114 | } |
| 2115 | |
| 2116 | /* leave this out until we have a page_mkwrite call */ |
| 2117 | if (0 && !test_range_bit(tree, cur, cur + iosize - 1, |
| 2118 | EXTENT_DIRTY, 0)) { |
| 2119 | cur = cur + iosize; |
| 2120 | page_offset += iosize; |
| 2121 | continue; |
| 2122 | } |
| 2123 | clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS); |
| 2124 | if (tree->ops && tree->ops->writepage_io_hook) { |
| 2125 | ret = tree->ops->writepage_io_hook(page, cur, |
| 2126 | cur + iosize - 1); |
| 2127 | } else { |
| 2128 | ret = 0; |
| 2129 | } |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2130 | if (ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2131 | SetPageError(page); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2132 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2133 | unsigned long max_nr = end_index + 1; |
| 2134 | set_range_writeback(tree, cur, cur + iosize - 1); |
| 2135 | if (!PageWriteback(page)) { |
| 2136 | printk("warning page %lu not writeback, " |
| 2137 | "cur %llu end %llu\n", page->index, |
| 2138 | (unsigned long long)cur, |
| 2139 | (unsigned long long)end); |
| 2140 | } |
| 2141 | |
| 2142 | ret = submit_extent_page(WRITE, tree, page, sector, |
| 2143 | iosize, page_offset, bdev, |
| 2144 | &epd->bio, max_nr, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2145 | end_bio_extent_writepage, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2146 | if (ret) |
| 2147 | SetPageError(page); |
| 2148 | } |
| 2149 | cur = cur + iosize; |
| 2150 | page_offset += iosize; |
| 2151 | nr++; |
| 2152 | } |
| 2153 | done: |
| 2154 | if (nr == 0) { |
| 2155 | /* make sure the mapping tag for page dirty gets cleared */ |
| 2156 | set_page_writeback(page); |
| 2157 | end_page_writeback(page); |
| 2158 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame^] | 2159 | if (unlock_start <= page_end) |
| 2160 | unlock_extent(tree, unlock_start, page_end, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2161 | unlock_page(page); |
| 2162 | return 0; |
| 2163 | } |
| 2164 | |
Chris Mason | 5e478dc | 2008-04-25 09:10:45 -0400 | [diff] [blame] | 2165 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2166 | /* Taken directly from 2.6.23 for 2.6.18 back port */ |
| 2167 | typedef int (*writepage_t)(struct page *page, struct writeback_control *wbc, |
| 2168 | void *data); |
| 2169 | |
| 2170 | /** |
| 2171 | * write_cache_pages - walk the list of dirty pages of the given address space |
| 2172 | * and write all of them. |
| 2173 | * @mapping: address space structure to write |
| 2174 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
| 2175 | * @writepage: function called for each page |
| 2176 | * @data: data passed to writepage function |
| 2177 | * |
| 2178 | * If a page is already under I/O, write_cache_pages() skips it, even |
| 2179 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, |
| 2180 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() |
| 2181 | * and msync() need to guarantee that all the data which was dirty at the time |
| 2182 | * the call was made get new I/O started against them. If wbc->sync_mode is |
| 2183 | * WB_SYNC_ALL then we were called for data integrity and we must wait for |
| 2184 | * existing IO to complete. |
| 2185 | */ |
| 2186 | static int write_cache_pages(struct address_space *mapping, |
| 2187 | struct writeback_control *wbc, writepage_t writepage, |
| 2188 | void *data) |
| 2189 | { |
| 2190 | struct backing_dev_info *bdi = mapping->backing_dev_info; |
| 2191 | int ret = 0; |
| 2192 | int done = 0; |
| 2193 | struct pagevec pvec; |
| 2194 | int nr_pages; |
| 2195 | pgoff_t index; |
| 2196 | pgoff_t end; /* Inclusive */ |
| 2197 | int scanned = 0; |
| 2198 | int range_whole = 0; |
| 2199 | |
| 2200 | if (wbc->nonblocking && bdi_write_congested(bdi)) { |
| 2201 | wbc->encountered_congestion = 1; |
| 2202 | return 0; |
| 2203 | } |
| 2204 | |
| 2205 | pagevec_init(&pvec, 0); |
| 2206 | if (wbc->range_cyclic) { |
| 2207 | index = mapping->writeback_index; /* Start from prev offset */ |
| 2208 | end = -1; |
| 2209 | } else { |
| 2210 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 2211 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
| 2212 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 2213 | range_whole = 1; |
| 2214 | scanned = 1; |
| 2215 | } |
| 2216 | retry: |
| 2217 | while (!done && (index <= end) && |
| 2218 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, |
| 2219 | PAGECACHE_TAG_DIRTY, |
| 2220 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { |
| 2221 | unsigned i; |
| 2222 | |
| 2223 | scanned = 1; |
| 2224 | for (i = 0; i < nr_pages; i++) { |
| 2225 | struct page *page = pvec.pages[i]; |
| 2226 | |
| 2227 | /* |
| 2228 | * At this point we hold neither mapping->tree_lock nor |
| 2229 | * lock on the page itself: the page may be truncated or |
| 2230 | * invalidated (changing page->mapping to NULL), or even |
| 2231 | * swizzled back from swapper_space to tmpfs file |
| 2232 | * mapping |
| 2233 | */ |
| 2234 | lock_page(page); |
| 2235 | |
| 2236 | if (unlikely(page->mapping != mapping)) { |
| 2237 | unlock_page(page); |
| 2238 | continue; |
| 2239 | } |
| 2240 | |
| 2241 | if (!wbc->range_cyclic && page->index > end) { |
| 2242 | done = 1; |
| 2243 | unlock_page(page); |
| 2244 | continue; |
| 2245 | } |
| 2246 | |
| 2247 | if (wbc->sync_mode != WB_SYNC_NONE) |
| 2248 | wait_on_page_writeback(page); |
| 2249 | |
| 2250 | if (PageWriteback(page) || |
| 2251 | !clear_page_dirty_for_io(page)) { |
| 2252 | unlock_page(page); |
| 2253 | continue; |
| 2254 | } |
| 2255 | |
| 2256 | ret = (*writepage)(page, wbc, data); |
| 2257 | |
| 2258 | if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) { |
| 2259 | unlock_page(page); |
| 2260 | ret = 0; |
| 2261 | } |
| 2262 | if (ret || (--(wbc->nr_to_write) <= 0)) |
| 2263 | done = 1; |
| 2264 | if (wbc->nonblocking && bdi_write_congested(bdi)) { |
| 2265 | wbc->encountered_congestion = 1; |
| 2266 | done = 1; |
| 2267 | } |
| 2268 | } |
| 2269 | pagevec_release(&pvec); |
| 2270 | cond_resched(); |
| 2271 | } |
| 2272 | if (!scanned && !done) { |
| 2273 | /* |
| 2274 | * We hit the last page and there is more work to be done: wrap |
| 2275 | * back to the start of the file |
| 2276 | */ |
| 2277 | scanned = 1; |
| 2278 | index = 0; |
| 2279 | goto retry; |
| 2280 | } |
| 2281 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
| 2282 | mapping->writeback_index = index; |
| 2283 | return ret; |
| 2284 | } |
| 2285 | #endif |
| 2286 | |
| 2287 | int extent_write_full_page(struct extent_io_tree *tree, struct page *page, |
| 2288 | get_extent_t *get_extent, |
| 2289 | struct writeback_control *wbc) |
| 2290 | { |
| 2291 | int ret; |
| 2292 | struct address_space *mapping = page->mapping; |
| 2293 | struct extent_page_data epd = { |
| 2294 | .bio = NULL, |
| 2295 | .tree = tree, |
| 2296 | .get_extent = get_extent, |
| 2297 | }; |
| 2298 | struct writeback_control wbc_writepages = { |
| 2299 | .bdi = wbc->bdi, |
| 2300 | .sync_mode = WB_SYNC_NONE, |
| 2301 | .older_than_this = NULL, |
| 2302 | .nr_to_write = 64, |
| 2303 | .range_start = page_offset(page) + PAGE_CACHE_SIZE, |
| 2304 | .range_end = (loff_t)-1, |
| 2305 | }; |
| 2306 | |
| 2307 | |
| 2308 | ret = __extent_writepage(page, wbc, &epd); |
| 2309 | |
| 2310 | write_cache_pages(mapping, &wbc_writepages, __extent_writepage, &epd); |
| 2311 | if (epd.bio) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2312 | submit_one_bio(WRITE, epd.bio, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2313 | } |
| 2314 | return ret; |
| 2315 | } |
| 2316 | EXPORT_SYMBOL(extent_write_full_page); |
| 2317 | |
| 2318 | |
| 2319 | int extent_writepages(struct extent_io_tree *tree, |
| 2320 | struct address_space *mapping, |
| 2321 | get_extent_t *get_extent, |
| 2322 | struct writeback_control *wbc) |
| 2323 | { |
| 2324 | int ret = 0; |
| 2325 | struct extent_page_data epd = { |
| 2326 | .bio = NULL, |
| 2327 | .tree = tree, |
| 2328 | .get_extent = get_extent, |
| 2329 | }; |
| 2330 | |
| 2331 | ret = write_cache_pages(mapping, wbc, __extent_writepage, &epd); |
| 2332 | if (epd.bio) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2333 | submit_one_bio(WRITE, epd.bio, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2334 | } |
| 2335 | return ret; |
| 2336 | } |
| 2337 | EXPORT_SYMBOL(extent_writepages); |
| 2338 | |
| 2339 | int extent_readpages(struct extent_io_tree *tree, |
| 2340 | struct address_space *mapping, |
| 2341 | struct list_head *pages, unsigned nr_pages, |
| 2342 | get_extent_t get_extent) |
| 2343 | { |
| 2344 | struct bio *bio = NULL; |
| 2345 | unsigned page_idx; |
| 2346 | struct pagevec pvec; |
| 2347 | |
| 2348 | pagevec_init(&pvec, 0); |
| 2349 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { |
| 2350 | struct page *page = list_entry(pages->prev, struct page, lru); |
| 2351 | |
| 2352 | prefetchw(&page->flags); |
| 2353 | list_del(&page->lru); |
| 2354 | /* |
| 2355 | * what we want to do here is call add_to_page_cache_lru, |
| 2356 | * but that isn't exported, so we reproduce it here |
| 2357 | */ |
| 2358 | if (!add_to_page_cache(page, mapping, |
| 2359 | page->index, GFP_KERNEL)) { |
| 2360 | |
| 2361 | /* open coding of lru_cache_add, also not exported */ |
| 2362 | page_cache_get(page); |
| 2363 | if (!pagevec_add(&pvec, page)) |
| 2364 | __pagevec_lru_add(&pvec); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2365 | __extent_read_full_page(tree, page, get_extent, |
| 2366 | &bio, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2367 | } |
| 2368 | page_cache_release(page); |
| 2369 | } |
| 2370 | if (pagevec_count(&pvec)) |
| 2371 | __pagevec_lru_add(&pvec); |
| 2372 | BUG_ON(!list_empty(pages)); |
| 2373 | if (bio) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2374 | submit_one_bio(READ, bio, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2375 | return 0; |
| 2376 | } |
| 2377 | EXPORT_SYMBOL(extent_readpages); |
| 2378 | |
| 2379 | /* |
| 2380 | * basic invalidatepage code, this waits on any locked or writeback |
| 2381 | * ranges corresponding to the page, and then deletes any extent state |
| 2382 | * records from the tree |
| 2383 | */ |
| 2384 | int extent_invalidatepage(struct extent_io_tree *tree, |
| 2385 | struct page *page, unsigned long offset) |
| 2386 | { |
| 2387 | u64 start = ((u64)page->index << PAGE_CACHE_SHIFT); |
| 2388 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 2389 | size_t blocksize = page->mapping->host->i_sb->s_blocksize; |
| 2390 | |
| 2391 | start += (offset + blocksize -1) & ~(blocksize - 1); |
| 2392 | if (start > end) |
| 2393 | return 0; |
| 2394 | |
| 2395 | lock_extent(tree, start, end, GFP_NOFS); |
| 2396 | wait_on_extent_writeback(tree, start, end); |
| 2397 | clear_extent_bit(tree, start, end, |
| 2398 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC, |
| 2399 | 1, 1, GFP_NOFS); |
| 2400 | return 0; |
| 2401 | } |
| 2402 | EXPORT_SYMBOL(extent_invalidatepage); |
| 2403 | |
| 2404 | /* |
| 2405 | * simple commit_write call, set_range_dirty is used to mark both |
| 2406 | * the pages and the extent records as dirty |
| 2407 | */ |
| 2408 | int extent_commit_write(struct extent_io_tree *tree, |
| 2409 | struct inode *inode, struct page *page, |
| 2410 | unsigned from, unsigned to) |
| 2411 | { |
| 2412 | loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to; |
| 2413 | |
| 2414 | set_page_extent_mapped(page); |
| 2415 | set_page_dirty(page); |
| 2416 | |
| 2417 | if (pos > inode->i_size) { |
| 2418 | i_size_write(inode, pos); |
| 2419 | mark_inode_dirty(inode); |
| 2420 | } |
| 2421 | return 0; |
| 2422 | } |
| 2423 | EXPORT_SYMBOL(extent_commit_write); |
| 2424 | |
| 2425 | int extent_prepare_write(struct extent_io_tree *tree, |
| 2426 | struct inode *inode, struct page *page, |
| 2427 | unsigned from, unsigned to, get_extent_t *get_extent) |
| 2428 | { |
| 2429 | u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 2430 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 2431 | u64 block_start; |
| 2432 | u64 orig_block_start; |
| 2433 | u64 block_end; |
| 2434 | u64 cur_end; |
| 2435 | struct extent_map *em; |
| 2436 | unsigned blocksize = 1 << inode->i_blkbits; |
| 2437 | size_t page_offset = 0; |
| 2438 | size_t block_off_start; |
| 2439 | size_t block_off_end; |
| 2440 | int err = 0; |
| 2441 | int iocount = 0; |
| 2442 | int ret = 0; |
| 2443 | int isnew; |
| 2444 | |
| 2445 | set_page_extent_mapped(page); |
| 2446 | |
| 2447 | block_start = (page_start + from) & ~((u64)blocksize - 1); |
| 2448 | block_end = (page_start + to - 1) | (blocksize - 1); |
| 2449 | orig_block_start = block_start; |
| 2450 | |
| 2451 | lock_extent(tree, page_start, page_end, GFP_NOFS); |
| 2452 | while(block_start <= block_end) { |
| 2453 | em = get_extent(inode, page, page_offset, block_start, |
| 2454 | block_end - block_start + 1, 1); |
| 2455 | if (IS_ERR(em) || !em) { |
| 2456 | goto err; |
| 2457 | } |
| 2458 | cur_end = min(block_end, extent_map_end(em) - 1); |
| 2459 | block_off_start = block_start & (PAGE_CACHE_SIZE - 1); |
| 2460 | block_off_end = block_off_start + blocksize; |
| 2461 | isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS); |
| 2462 | |
| 2463 | if (!PageUptodate(page) && isnew && |
| 2464 | (block_off_end > to || block_off_start < from)) { |
| 2465 | void *kaddr; |
| 2466 | |
| 2467 | kaddr = kmap_atomic(page, KM_USER0); |
| 2468 | if (block_off_end > to) |
| 2469 | memset(kaddr + to, 0, block_off_end - to); |
| 2470 | if (block_off_start < from) |
| 2471 | memset(kaddr + block_off_start, 0, |
| 2472 | from - block_off_start); |
| 2473 | flush_dcache_page(page); |
| 2474 | kunmap_atomic(kaddr, KM_USER0); |
| 2475 | } |
| 2476 | if ((em->block_start != EXTENT_MAP_HOLE && |
| 2477 | em->block_start != EXTENT_MAP_INLINE) && |
| 2478 | !isnew && !PageUptodate(page) && |
| 2479 | (block_off_end > to || block_off_start < from) && |
| 2480 | !test_range_bit(tree, block_start, cur_end, |
| 2481 | EXTENT_UPTODATE, 1)) { |
| 2482 | u64 sector; |
| 2483 | u64 extent_offset = block_start - em->start; |
| 2484 | size_t iosize; |
| 2485 | sector = (em->block_start + extent_offset) >> 9; |
| 2486 | iosize = (cur_end - block_start + blocksize) & |
| 2487 | ~((u64)blocksize - 1); |
| 2488 | /* |
| 2489 | * we've already got the extent locked, but we |
| 2490 | * need to split the state such that our end_bio |
| 2491 | * handler can clear the lock. |
| 2492 | */ |
| 2493 | set_extent_bit(tree, block_start, |
| 2494 | block_start + iosize - 1, |
| 2495 | EXTENT_LOCKED, 0, NULL, GFP_NOFS); |
| 2496 | ret = submit_extent_page(READ, tree, page, |
| 2497 | sector, iosize, page_offset, em->bdev, |
| 2498 | NULL, 1, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2499 | end_bio_extent_preparewrite, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2500 | iocount++; |
| 2501 | block_start = block_start + iosize; |
| 2502 | } else { |
| 2503 | set_extent_uptodate(tree, block_start, cur_end, |
| 2504 | GFP_NOFS); |
| 2505 | unlock_extent(tree, block_start, cur_end, GFP_NOFS); |
| 2506 | block_start = cur_end + 1; |
| 2507 | } |
| 2508 | page_offset = block_start & (PAGE_CACHE_SIZE - 1); |
| 2509 | free_extent_map(em); |
| 2510 | } |
| 2511 | if (iocount) { |
| 2512 | wait_extent_bit(tree, orig_block_start, |
| 2513 | block_end, EXTENT_LOCKED); |
| 2514 | } |
| 2515 | check_page_uptodate(tree, page); |
| 2516 | err: |
| 2517 | /* FIXME, zero out newly allocated blocks on error */ |
| 2518 | return err; |
| 2519 | } |
| 2520 | EXPORT_SYMBOL(extent_prepare_write); |
| 2521 | |
| 2522 | /* |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 2523 | * a helper for releasepage, this tests for areas of the page that |
| 2524 | * are locked or under IO and drops the related state bits if it is safe |
| 2525 | * to drop the page. |
| 2526 | */ |
| 2527 | int try_release_extent_state(struct extent_map_tree *map, |
| 2528 | struct extent_io_tree *tree, struct page *page, |
| 2529 | gfp_t mask) |
| 2530 | { |
| 2531 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 2532 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 2533 | int ret = 1; |
| 2534 | |
| 2535 | if (test_range_bit(tree, start, end, EXTENT_IOBITS, 0)) |
| 2536 | ret = 0; |
| 2537 | else { |
| 2538 | if ((mask & GFP_NOFS) == GFP_NOFS) |
| 2539 | mask = GFP_NOFS; |
| 2540 | clear_extent_bit(tree, start, end, EXTENT_UPTODATE, |
| 2541 | 1, 1, mask); |
| 2542 | } |
| 2543 | return ret; |
| 2544 | } |
| 2545 | EXPORT_SYMBOL(try_release_extent_state); |
| 2546 | |
| 2547 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2548 | * a helper for releasepage. As long as there are no locked extents |
| 2549 | * in the range corresponding to the page, both state records and extent |
| 2550 | * map records are removed |
| 2551 | */ |
| 2552 | int try_release_extent_mapping(struct extent_map_tree *map, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2553 | struct extent_io_tree *tree, struct page *page, |
| 2554 | gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2555 | { |
| 2556 | struct extent_map *em; |
| 2557 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 2558 | u64 end = start + PAGE_CACHE_SIZE - 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 2559 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2560 | if ((mask & __GFP_WAIT) && |
| 2561 | page->mapping->host->i_size > 16 * 1024 * 1024) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 2562 | u64 len; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2563 | while (start <= end) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 2564 | len = end - start + 1; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2565 | spin_lock(&map->lock); |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 2566 | em = lookup_extent_mapping(map, start, len); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2567 | if (!em || IS_ERR(em)) { |
| 2568 | spin_unlock(&map->lock); |
| 2569 | break; |
| 2570 | } |
| 2571 | if (em->start != start) { |
| 2572 | spin_unlock(&map->lock); |
| 2573 | free_extent_map(em); |
| 2574 | break; |
| 2575 | } |
| 2576 | if (!test_range_bit(tree, em->start, |
| 2577 | extent_map_end(em) - 1, |
| 2578 | EXTENT_LOCKED, 0)) { |
| 2579 | remove_extent_mapping(map, em); |
| 2580 | /* once for the rb tree */ |
| 2581 | free_extent_map(em); |
| 2582 | } |
| 2583 | start = extent_map_end(em); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2584 | spin_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2585 | |
| 2586 | /* once for us */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2587 | free_extent_map(em); |
| 2588 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2589 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 2590 | return try_release_extent_state(map, tree, page, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2591 | } |
| 2592 | EXPORT_SYMBOL(try_release_extent_mapping); |
| 2593 | |
| 2594 | sector_t extent_bmap(struct address_space *mapping, sector_t iblock, |
| 2595 | get_extent_t *get_extent) |
| 2596 | { |
| 2597 | struct inode *inode = mapping->host; |
| 2598 | u64 start = iblock << inode->i_blkbits; |
| 2599 | sector_t sector = 0; |
| 2600 | struct extent_map *em; |
| 2601 | |
| 2602 | em = get_extent(inode, NULL, 0, start, (1 << inode->i_blkbits), 0); |
| 2603 | if (!em || IS_ERR(em)) |
| 2604 | return 0; |
| 2605 | |
| 2606 | if (em->block_start == EXTENT_MAP_INLINE || |
| 2607 | em->block_start == EXTENT_MAP_HOLE) |
| 2608 | goto out; |
| 2609 | |
| 2610 | sector = (em->block_start + start - em->start) >> inode->i_blkbits; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2611 | out: |
| 2612 | free_extent_map(em); |
| 2613 | return sector; |
| 2614 | } |
| 2615 | |
| 2616 | static int add_lru(struct extent_io_tree *tree, struct extent_buffer *eb) |
| 2617 | { |
| 2618 | if (list_empty(&eb->lru)) { |
| 2619 | extent_buffer_get(eb); |
| 2620 | list_add(&eb->lru, &tree->buffer_lru); |
| 2621 | tree->lru_size++; |
| 2622 | if (tree->lru_size >= BUFFER_LRU_MAX) { |
| 2623 | struct extent_buffer *rm; |
| 2624 | rm = list_entry(tree->buffer_lru.prev, |
| 2625 | struct extent_buffer, lru); |
| 2626 | tree->lru_size--; |
| 2627 | list_del_init(&rm->lru); |
| 2628 | free_extent_buffer(rm); |
| 2629 | } |
| 2630 | } else |
| 2631 | list_move(&eb->lru, &tree->buffer_lru); |
| 2632 | return 0; |
| 2633 | } |
| 2634 | static struct extent_buffer *find_lru(struct extent_io_tree *tree, |
| 2635 | u64 start, unsigned long len) |
| 2636 | { |
| 2637 | struct list_head *lru = &tree->buffer_lru; |
| 2638 | struct list_head *cur = lru->next; |
| 2639 | struct extent_buffer *eb; |
| 2640 | |
| 2641 | if (list_empty(lru)) |
| 2642 | return NULL; |
| 2643 | |
| 2644 | do { |
| 2645 | eb = list_entry(cur, struct extent_buffer, lru); |
| 2646 | if (eb->start == start && eb->len == len) { |
| 2647 | extent_buffer_get(eb); |
| 2648 | return eb; |
| 2649 | } |
| 2650 | cur = cur->next; |
| 2651 | } while (cur != lru); |
| 2652 | return NULL; |
| 2653 | } |
| 2654 | |
| 2655 | static inline unsigned long num_extent_pages(u64 start, u64 len) |
| 2656 | { |
| 2657 | return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) - |
| 2658 | (start >> PAGE_CACHE_SHIFT); |
| 2659 | } |
| 2660 | |
| 2661 | static inline struct page *extent_buffer_page(struct extent_buffer *eb, |
| 2662 | unsigned long i) |
| 2663 | { |
| 2664 | struct page *p; |
| 2665 | struct address_space *mapping; |
| 2666 | |
| 2667 | if (i == 0) |
| 2668 | return eb->first_page; |
| 2669 | i += eb->start >> PAGE_CACHE_SHIFT; |
| 2670 | mapping = eb->first_page->mapping; |
| 2671 | read_lock_irq(&mapping->tree_lock); |
| 2672 | p = radix_tree_lookup(&mapping->page_tree, i); |
| 2673 | read_unlock_irq(&mapping->tree_lock); |
| 2674 | return p; |
| 2675 | } |
| 2676 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2677 | int release_extent_buffer_tail_pages(struct extent_buffer *eb) |
| 2678 | { |
| 2679 | unsigned long num_pages = num_extent_pages(eb->start, eb->len); |
| 2680 | struct page *page; |
| 2681 | unsigned long i; |
| 2682 | |
| 2683 | if (num_pages == 1) |
| 2684 | return 0; |
| 2685 | for (i = 1; i < num_pages; i++) { |
| 2686 | page = extent_buffer_page(eb, i); |
| 2687 | page_cache_release(page); |
| 2688 | } |
| 2689 | return 0; |
| 2690 | } |
| 2691 | |
| 2692 | |
Chris Mason | 728131d | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2693 | int invalidate_extent_lru(struct extent_io_tree *tree, u64 start, |
| 2694 | unsigned long len) |
| 2695 | { |
| 2696 | struct list_head *lru = &tree->buffer_lru; |
| 2697 | struct list_head *cur = lru->next; |
| 2698 | struct extent_buffer *eb; |
| 2699 | int found = 0; |
| 2700 | |
| 2701 | spin_lock(&tree->lru_lock); |
| 2702 | if (list_empty(lru)) |
| 2703 | goto out; |
| 2704 | |
| 2705 | do { |
| 2706 | eb = list_entry(cur, struct extent_buffer, lru); |
| 2707 | if (eb->start <= start && eb->start + eb->len > start) { |
| 2708 | eb->flags &= ~EXTENT_UPTODATE; |
| 2709 | } |
Chris Mason | 728131d | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2710 | cur = cur->next; |
| 2711 | } while (cur != lru); |
| 2712 | out: |
| 2713 | spin_unlock(&tree->lru_lock); |
| 2714 | return found; |
| 2715 | } |
| 2716 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2717 | static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree, |
| 2718 | u64 start, |
| 2719 | unsigned long len, |
| 2720 | gfp_t mask) |
| 2721 | { |
| 2722 | struct extent_buffer *eb = NULL; |
Chris Mason | 2d2ae54 | 2008-03-26 16:24:23 -0400 | [diff] [blame] | 2723 | unsigned long flags; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2724 | |
| 2725 | spin_lock(&tree->lru_lock); |
| 2726 | eb = find_lru(tree, start, len); |
| 2727 | spin_unlock(&tree->lru_lock); |
| 2728 | if (eb) { |
| 2729 | return eb; |
| 2730 | } |
| 2731 | |
| 2732 | eb = kmem_cache_zalloc(extent_buffer_cache, mask); |
| 2733 | INIT_LIST_HEAD(&eb->lru); |
| 2734 | eb->start = start; |
| 2735 | eb->len = len; |
Chris Mason | 2d2ae54 | 2008-03-26 16:24:23 -0400 | [diff] [blame] | 2736 | spin_lock_irqsave(&leak_lock, flags); |
| 2737 | list_add(&eb->leak_list, &buffers); |
| 2738 | spin_unlock_irqrestore(&leak_lock, flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2739 | atomic_set(&eb->refs, 1); |
| 2740 | |
| 2741 | return eb; |
| 2742 | } |
| 2743 | |
| 2744 | static void __free_extent_buffer(struct extent_buffer *eb) |
| 2745 | { |
Chris Mason | 2d2ae54 | 2008-03-26 16:24:23 -0400 | [diff] [blame] | 2746 | unsigned long flags; |
| 2747 | spin_lock_irqsave(&leak_lock, flags); |
| 2748 | list_del(&eb->leak_list); |
| 2749 | spin_unlock_irqrestore(&leak_lock, flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2750 | kmem_cache_free(extent_buffer_cache, eb); |
| 2751 | } |
| 2752 | |
| 2753 | struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree, |
| 2754 | u64 start, unsigned long len, |
| 2755 | struct page *page0, |
| 2756 | gfp_t mask) |
| 2757 | { |
| 2758 | unsigned long num_pages = num_extent_pages(start, len); |
| 2759 | unsigned long i; |
| 2760 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 2761 | struct extent_buffer *eb; |
| 2762 | struct page *p; |
| 2763 | struct address_space *mapping = tree->mapping; |
| 2764 | int uptodate = 1; |
| 2765 | |
| 2766 | eb = __alloc_extent_buffer(tree, start, len, mask); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 2767 | if (!eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2768 | return NULL; |
| 2769 | |
| 2770 | if (eb->flags & EXTENT_BUFFER_FILLED) |
| 2771 | goto lru_add; |
| 2772 | |
| 2773 | if (page0) { |
| 2774 | eb->first_page = page0; |
| 2775 | i = 1; |
| 2776 | index++; |
| 2777 | page_cache_get(page0); |
| 2778 | mark_page_accessed(page0); |
| 2779 | set_page_extent_mapped(page0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2780 | set_page_extent_head(page0, len); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2781 | uptodate = PageUptodate(page0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2782 | } else { |
| 2783 | i = 0; |
| 2784 | } |
| 2785 | for (; i < num_pages; i++, index++) { |
| 2786 | p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM); |
| 2787 | if (!p) { |
| 2788 | WARN_ON(1); |
| 2789 | goto fail; |
| 2790 | } |
| 2791 | set_page_extent_mapped(p); |
| 2792 | mark_page_accessed(p); |
| 2793 | if (i == 0) { |
| 2794 | eb->first_page = p; |
| 2795 | set_page_extent_head(p, len); |
| 2796 | } else { |
| 2797 | set_page_private(p, EXTENT_PAGE_PRIVATE); |
| 2798 | } |
| 2799 | if (!PageUptodate(p)) |
| 2800 | uptodate = 0; |
| 2801 | unlock_page(p); |
| 2802 | } |
| 2803 | if (uptodate) |
| 2804 | eb->flags |= EXTENT_UPTODATE; |
| 2805 | eb->flags |= EXTENT_BUFFER_FILLED; |
| 2806 | |
| 2807 | lru_add: |
| 2808 | spin_lock(&tree->lru_lock); |
| 2809 | add_lru(tree, eb); |
| 2810 | spin_unlock(&tree->lru_lock); |
| 2811 | return eb; |
| 2812 | |
| 2813 | fail: |
| 2814 | spin_lock(&tree->lru_lock); |
| 2815 | list_del_init(&eb->lru); |
| 2816 | spin_unlock(&tree->lru_lock); |
| 2817 | if (!atomic_dec_and_test(&eb->refs)) |
| 2818 | return NULL; |
| 2819 | for (index = 1; index < i; index++) { |
| 2820 | page_cache_release(extent_buffer_page(eb, index)); |
| 2821 | } |
| 2822 | if (i > 0) |
| 2823 | page_cache_release(extent_buffer_page(eb, 0)); |
| 2824 | __free_extent_buffer(eb); |
| 2825 | return NULL; |
| 2826 | } |
| 2827 | EXPORT_SYMBOL(alloc_extent_buffer); |
| 2828 | |
| 2829 | struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree, |
| 2830 | u64 start, unsigned long len, |
| 2831 | gfp_t mask) |
| 2832 | { |
| 2833 | unsigned long num_pages = num_extent_pages(start, len); |
| 2834 | unsigned long i; |
| 2835 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 2836 | struct extent_buffer *eb; |
| 2837 | struct page *p; |
| 2838 | struct address_space *mapping = tree->mapping; |
| 2839 | int uptodate = 1; |
| 2840 | |
| 2841 | eb = __alloc_extent_buffer(tree, start, len, mask); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 2842 | if (!eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2843 | return NULL; |
| 2844 | |
| 2845 | if (eb->flags & EXTENT_BUFFER_FILLED) |
| 2846 | goto lru_add; |
| 2847 | |
| 2848 | for (i = 0; i < num_pages; i++, index++) { |
Chris Mason | 079899c | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2849 | p = find_get_page(mapping, index); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2850 | if (!p) { |
| 2851 | goto fail; |
| 2852 | } |
Chris Mason | 079899c | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2853 | if (TestSetPageLocked(p)) { |
| 2854 | page_cache_release(p); |
| 2855 | goto fail; |
| 2856 | } |
| 2857 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2858 | set_page_extent_mapped(p); |
| 2859 | mark_page_accessed(p); |
| 2860 | |
| 2861 | if (i == 0) { |
| 2862 | eb->first_page = p; |
| 2863 | set_page_extent_head(p, len); |
| 2864 | } else { |
| 2865 | set_page_private(p, EXTENT_PAGE_PRIVATE); |
| 2866 | } |
| 2867 | |
| 2868 | if (!PageUptodate(p)) |
| 2869 | uptodate = 0; |
| 2870 | unlock_page(p); |
| 2871 | } |
| 2872 | if (uptodate) |
| 2873 | eb->flags |= EXTENT_UPTODATE; |
| 2874 | eb->flags |= EXTENT_BUFFER_FILLED; |
| 2875 | |
| 2876 | lru_add: |
| 2877 | spin_lock(&tree->lru_lock); |
| 2878 | add_lru(tree, eb); |
| 2879 | spin_unlock(&tree->lru_lock); |
| 2880 | return eb; |
| 2881 | fail: |
| 2882 | spin_lock(&tree->lru_lock); |
| 2883 | list_del_init(&eb->lru); |
| 2884 | spin_unlock(&tree->lru_lock); |
| 2885 | if (!atomic_dec_and_test(&eb->refs)) |
| 2886 | return NULL; |
| 2887 | for (index = 1; index < i; index++) { |
| 2888 | page_cache_release(extent_buffer_page(eb, index)); |
| 2889 | } |
| 2890 | if (i > 0) |
| 2891 | page_cache_release(extent_buffer_page(eb, 0)); |
| 2892 | __free_extent_buffer(eb); |
| 2893 | return NULL; |
| 2894 | } |
| 2895 | EXPORT_SYMBOL(find_extent_buffer); |
| 2896 | |
| 2897 | void free_extent_buffer(struct extent_buffer *eb) |
| 2898 | { |
| 2899 | unsigned long i; |
| 2900 | unsigned long num_pages; |
| 2901 | |
| 2902 | if (!eb) |
| 2903 | return; |
| 2904 | |
| 2905 | if (!atomic_dec_and_test(&eb->refs)) |
| 2906 | return; |
| 2907 | |
| 2908 | WARN_ON(!list_empty(&eb->lru)); |
| 2909 | num_pages = num_extent_pages(eb->start, eb->len); |
| 2910 | |
| 2911 | for (i = 1; i < num_pages; i++) { |
| 2912 | page_cache_release(extent_buffer_page(eb, i)); |
| 2913 | } |
| 2914 | page_cache_release(extent_buffer_page(eb, 0)); |
| 2915 | __free_extent_buffer(eb); |
| 2916 | } |
| 2917 | EXPORT_SYMBOL(free_extent_buffer); |
| 2918 | |
| 2919 | int clear_extent_buffer_dirty(struct extent_io_tree *tree, |
| 2920 | struct extent_buffer *eb) |
| 2921 | { |
| 2922 | int set; |
| 2923 | unsigned long i; |
| 2924 | unsigned long num_pages; |
| 2925 | struct page *page; |
| 2926 | |
| 2927 | u64 start = eb->start; |
| 2928 | u64 end = start + eb->len - 1; |
| 2929 | |
| 2930 | set = clear_extent_dirty(tree, start, end, GFP_NOFS); |
| 2931 | num_pages = num_extent_pages(eb->start, eb->len); |
| 2932 | |
| 2933 | for (i = 0; i < num_pages; i++) { |
| 2934 | page = extent_buffer_page(eb, i); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2935 | if (i == 0) |
| 2936 | set_page_extent_head(page, eb->len); |
| 2937 | else |
| 2938 | set_page_private(page, EXTENT_PAGE_PRIVATE); |
| 2939 | |
| 2940 | /* |
| 2941 | * if we're on the last page or the first page and the |
| 2942 | * block isn't aligned on a page boundary, do extra checks |
| 2943 | * to make sure we don't clean page that is partially dirty |
| 2944 | */ |
| 2945 | if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) || |
| 2946 | ((i == num_pages - 1) && |
| 2947 | ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) { |
| 2948 | start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 2949 | end = start + PAGE_CACHE_SIZE - 1; |
| 2950 | if (test_range_bit(tree, start, end, |
| 2951 | EXTENT_DIRTY, 0)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2952 | continue; |
| 2953 | } |
| 2954 | } |
| 2955 | clear_page_dirty_for_io(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2956 | read_lock_irq(&page->mapping->tree_lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2957 | if (!PageDirty(page)) { |
| 2958 | radix_tree_tag_clear(&page->mapping->page_tree, |
| 2959 | page_index(page), |
| 2960 | PAGECACHE_TAG_DIRTY); |
| 2961 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2962 | read_unlock_irq(&page->mapping->tree_lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2963 | } |
| 2964 | return 0; |
| 2965 | } |
| 2966 | EXPORT_SYMBOL(clear_extent_buffer_dirty); |
| 2967 | |
| 2968 | int wait_on_extent_buffer_writeback(struct extent_io_tree *tree, |
| 2969 | struct extent_buffer *eb) |
| 2970 | { |
| 2971 | return wait_on_extent_writeback(tree, eb->start, |
| 2972 | eb->start + eb->len - 1); |
| 2973 | } |
| 2974 | EXPORT_SYMBOL(wait_on_extent_buffer_writeback); |
| 2975 | |
| 2976 | int set_extent_buffer_dirty(struct extent_io_tree *tree, |
| 2977 | struct extent_buffer *eb) |
| 2978 | { |
| 2979 | unsigned long i; |
| 2980 | unsigned long num_pages; |
| 2981 | |
| 2982 | num_pages = num_extent_pages(eb->start, eb->len); |
| 2983 | for (i = 0; i < num_pages; i++) { |
| 2984 | struct page *page = extent_buffer_page(eb, i); |
| 2985 | /* writepage may need to do something special for the |
| 2986 | * first page, we have to make sure page->private is |
| 2987 | * properly set. releasepage may drop page->private |
| 2988 | * on us if the page isn't already dirty. |
| 2989 | */ |
| 2990 | if (i == 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2991 | set_page_extent_head(page, eb->len); |
| 2992 | } else if (PagePrivate(page) && |
| 2993 | page->private != EXTENT_PAGE_PRIVATE) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2994 | set_page_extent_mapped(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2995 | } |
| 2996 | __set_page_dirty_nobuffers(extent_buffer_page(eb, i)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2997 | } |
| 2998 | return set_extent_dirty(tree, eb->start, |
| 2999 | eb->start + eb->len - 1, GFP_NOFS); |
| 3000 | } |
| 3001 | EXPORT_SYMBOL(set_extent_buffer_dirty); |
| 3002 | |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 3003 | int clear_extent_buffer_uptodate(struct extent_io_tree *tree, |
| 3004 | struct extent_buffer *eb) |
| 3005 | { |
| 3006 | unsigned long i; |
| 3007 | struct page *page; |
| 3008 | unsigned long num_pages; |
| 3009 | |
| 3010 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3011 | eb->flags &= ~EXTENT_UPTODATE; |
| 3012 | |
| 3013 | clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1, |
| 3014 | GFP_NOFS); |
| 3015 | for (i = 0; i < num_pages; i++) { |
| 3016 | page = extent_buffer_page(eb, i); |
| 3017 | ClearPageUptodate(page); |
| 3018 | } |
| 3019 | return 0; |
| 3020 | } |
| 3021 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3022 | int set_extent_buffer_uptodate(struct extent_io_tree *tree, |
| 3023 | struct extent_buffer *eb) |
| 3024 | { |
| 3025 | unsigned long i; |
| 3026 | struct page *page; |
| 3027 | unsigned long num_pages; |
| 3028 | |
| 3029 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3030 | |
| 3031 | set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1, |
| 3032 | GFP_NOFS); |
| 3033 | for (i = 0; i < num_pages; i++) { |
| 3034 | page = extent_buffer_page(eb, i); |
| 3035 | if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) || |
| 3036 | ((i == num_pages - 1) && |
| 3037 | ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) { |
| 3038 | check_page_uptodate(tree, page); |
| 3039 | continue; |
| 3040 | } |
| 3041 | SetPageUptodate(page); |
| 3042 | } |
| 3043 | return 0; |
| 3044 | } |
| 3045 | EXPORT_SYMBOL(set_extent_buffer_uptodate); |
| 3046 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3047 | int extent_range_uptodate(struct extent_io_tree *tree, |
| 3048 | u64 start, u64 end) |
| 3049 | { |
| 3050 | struct page *page; |
| 3051 | int ret; |
| 3052 | int pg_uptodate = 1; |
| 3053 | int uptodate; |
| 3054 | unsigned long index; |
| 3055 | |
| 3056 | ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1); |
| 3057 | if (ret) |
| 3058 | return 1; |
| 3059 | while(start <= end) { |
| 3060 | index = start >> PAGE_CACHE_SHIFT; |
| 3061 | page = find_get_page(tree->mapping, index); |
| 3062 | uptodate = PageUptodate(page); |
| 3063 | page_cache_release(page); |
| 3064 | if (!uptodate) { |
| 3065 | pg_uptodate = 0; |
| 3066 | break; |
| 3067 | } |
| 3068 | start += PAGE_CACHE_SIZE; |
| 3069 | } |
| 3070 | return pg_uptodate; |
| 3071 | } |
| 3072 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3073 | int extent_buffer_uptodate(struct extent_io_tree *tree, |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3074 | struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3075 | { |
Chris Mason | 728131d | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3076 | int ret = 0; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3077 | unsigned long num_pages; |
| 3078 | unsigned long i; |
Chris Mason | 728131d | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3079 | struct page *page; |
| 3080 | int pg_uptodate = 1; |
| 3081 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3082 | if (eb->flags & EXTENT_UPTODATE) |
Chris Mason | 4235298 | 2008-04-28 16:40:52 -0400 | [diff] [blame] | 3083 | return 1; |
Chris Mason | 728131d | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3084 | |
Chris Mason | 4235298 | 2008-04-28 16:40:52 -0400 | [diff] [blame] | 3085 | ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3086 | EXTENT_UPTODATE, 1); |
Chris Mason | 4235298 | 2008-04-28 16:40:52 -0400 | [diff] [blame] | 3087 | if (ret) |
| 3088 | return ret; |
Chris Mason | 728131d | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3089 | |
| 3090 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3091 | for (i = 0; i < num_pages; i++) { |
| 3092 | page = extent_buffer_page(eb, i); |
| 3093 | if (!PageUptodate(page)) { |
| 3094 | pg_uptodate = 0; |
| 3095 | break; |
| 3096 | } |
| 3097 | } |
Chris Mason | 4235298 | 2008-04-28 16:40:52 -0400 | [diff] [blame] | 3098 | return pg_uptodate; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3099 | } |
| 3100 | EXPORT_SYMBOL(extent_buffer_uptodate); |
| 3101 | |
| 3102 | int read_extent_buffer_pages(struct extent_io_tree *tree, |
| 3103 | struct extent_buffer *eb, |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 3104 | u64 start, int wait, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3105 | get_extent_t *get_extent, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3106 | { |
| 3107 | unsigned long i; |
| 3108 | unsigned long start_i; |
| 3109 | struct page *page; |
| 3110 | int err; |
| 3111 | int ret = 0; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3112 | int locked_pages = 0; |
| 3113 | int all_uptodate = 1; |
| 3114 | int inc_all_pages = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3115 | unsigned long num_pages; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 3116 | struct bio *bio = NULL; |
| 3117 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3118 | if (eb->flags & EXTENT_UPTODATE) |
| 3119 | return 0; |
| 3120 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3121 | if (test_range_bit(tree, eb->start, eb->start + eb->len - 1, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3122 | EXTENT_UPTODATE, 1)) { |
| 3123 | return 0; |
| 3124 | } |
| 3125 | |
| 3126 | if (start) { |
| 3127 | WARN_ON(start < eb->start); |
| 3128 | start_i = (start >> PAGE_CACHE_SHIFT) - |
| 3129 | (eb->start >> PAGE_CACHE_SHIFT); |
| 3130 | } else { |
| 3131 | start_i = 0; |
| 3132 | } |
| 3133 | |
| 3134 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3135 | for (i = start_i; i < num_pages; i++) { |
| 3136 | page = extent_buffer_page(eb, i); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3137 | if (!wait) { |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3138 | if (TestSetPageLocked(page)) |
| 3139 | goto unlock_exit; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3140 | } else { |
| 3141 | lock_page(page); |
| 3142 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3143 | locked_pages++; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3144 | if (!PageUptodate(page)) { |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3145 | all_uptodate = 0; |
| 3146 | } |
| 3147 | } |
| 3148 | if (all_uptodate) { |
| 3149 | if (start_i == 0) |
| 3150 | eb->flags |= EXTENT_UPTODATE; |
| 3151 | goto unlock_exit; |
| 3152 | } |
| 3153 | |
| 3154 | for (i = start_i; i < num_pages; i++) { |
| 3155 | page = extent_buffer_page(eb, i); |
| 3156 | if (inc_all_pages) |
| 3157 | page_cache_get(page); |
| 3158 | if (!PageUptodate(page)) { |
| 3159 | if (start_i == 0) |
| 3160 | inc_all_pages = 1; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3161 | ClearPageError(page); |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 3162 | err = __extent_read_full_page(tree, page, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3163 | get_extent, &bio, |
| 3164 | mirror_num); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3165 | if (err) { |
| 3166 | ret = err; |
| 3167 | } |
| 3168 | } else { |
| 3169 | unlock_page(page); |
| 3170 | } |
| 3171 | } |
| 3172 | |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 3173 | if (bio) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3174 | submit_one_bio(READ, bio, mirror_num); |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 3175 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3176 | if (ret || !wait) { |
| 3177 | return ret; |
| 3178 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3179 | for (i = start_i; i < num_pages; i++) { |
| 3180 | page = extent_buffer_page(eb, i); |
| 3181 | wait_on_page_locked(page); |
| 3182 | if (!PageUptodate(page)) { |
| 3183 | ret = -EIO; |
| 3184 | } |
| 3185 | } |
| 3186 | if (!ret) |
| 3187 | eb->flags |= EXTENT_UPTODATE; |
| 3188 | return ret; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3189 | |
| 3190 | unlock_exit: |
| 3191 | i = start_i; |
| 3192 | while(locked_pages > 0) { |
| 3193 | page = extent_buffer_page(eb, i); |
| 3194 | i++; |
| 3195 | unlock_page(page); |
| 3196 | locked_pages--; |
| 3197 | } |
| 3198 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3199 | } |
| 3200 | EXPORT_SYMBOL(read_extent_buffer_pages); |
| 3201 | |
| 3202 | void read_extent_buffer(struct extent_buffer *eb, void *dstv, |
| 3203 | unsigned long start, |
| 3204 | unsigned long len) |
| 3205 | { |
| 3206 | size_t cur; |
| 3207 | size_t offset; |
| 3208 | struct page *page; |
| 3209 | char *kaddr; |
| 3210 | char *dst = (char *)dstv; |
| 3211 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 3212 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3213 | |
| 3214 | WARN_ON(start > eb->len); |
| 3215 | WARN_ON(start + len > eb->start + eb->len); |
| 3216 | |
| 3217 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 3218 | |
| 3219 | while(len > 0) { |
| 3220 | page = extent_buffer_page(eb, i); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3221 | |
| 3222 | cur = min(len, (PAGE_CACHE_SIZE - offset)); |
| 3223 | kaddr = kmap_atomic(page, KM_USER1); |
| 3224 | memcpy(dst, kaddr + offset, cur); |
| 3225 | kunmap_atomic(kaddr, KM_USER1); |
| 3226 | |
| 3227 | dst += cur; |
| 3228 | len -= cur; |
| 3229 | offset = 0; |
| 3230 | i++; |
| 3231 | } |
| 3232 | } |
| 3233 | EXPORT_SYMBOL(read_extent_buffer); |
| 3234 | |
| 3235 | int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start, |
| 3236 | unsigned long min_len, char **token, char **map, |
| 3237 | unsigned long *map_start, |
| 3238 | unsigned long *map_len, int km) |
| 3239 | { |
| 3240 | size_t offset = start & (PAGE_CACHE_SIZE - 1); |
| 3241 | char *kaddr; |
| 3242 | struct page *p; |
| 3243 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 3244 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 3245 | unsigned long end_i = (start_offset + start + min_len - 1) >> |
| 3246 | PAGE_CACHE_SHIFT; |
| 3247 | |
| 3248 | if (i != end_i) |
| 3249 | return -EINVAL; |
| 3250 | |
| 3251 | if (i == 0) { |
| 3252 | offset = start_offset; |
| 3253 | *map_start = 0; |
| 3254 | } else { |
| 3255 | offset = 0; |
| 3256 | *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset; |
| 3257 | } |
| 3258 | if (start + min_len > eb->len) { |
| 3259 | printk("bad mapping eb start %Lu len %lu, wanted %lu %lu\n", eb->start, eb->len, start, min_len); |
| 3260 | WARN_ON(1); |
| 3261 | } |
| 3262 | |
| 3263 | p = extent_buffer_page(eb, i); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3264 | kaddr = kmap_atomic(p, km); |
| 3265 | *token = kaddr; |
| 3266 | *map = kaddr + offset; |
| 3267 | *map_len = PAGE_CACHE_SIZE - offset; |
| 3268 | return 0; |
| 3269 | } |
| 3270 | EXPORT_SYMBOL(map_private_extent_buffer); |
| 3271 | |
| 3272 | int map_extent_buffer(struct extent_buffer *eb, unsigned long start, |
| 3273 | unsigned long min_len, |
| 3274 | char **token, char **map, |
| 3275 | unsigned long *map_start, |
| 3276 | unsigned long *map_len, int km) |
| 3277 | { |
| 3278 | int err; |
| 3279 | int save = 0; |
| 3280 | if (eb->map_token) { |
| 3281 | unmap_extent_buffer(eb, eb->map_token, km); |
| 3282 | eb->map_token = NULL; |
| 3283 | save = 1; |
| 3284 | } |
| 3285 | err = map_private_extent_buffer(eb, start, min_len, token, map, |
| 3286 | map_start, map_len, km); |
| 3287 | if (!err && save) { |
| 3288 | eb->map_token = *token; |
| 3289 | eb->kaddr = *map; |
| 3290 | eb->map_start = *map_start; |
| 3291 | eb->map_len = *map_len; |
| 3292 | } |
| 3293 | return err; |
| 3294 | } |
| 3295 | EXPORT_SYMBOL(map_extent_buffer); |
| 3296 | |
| 3297 | void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km) |
| 3298 | { |
| 3299 | kunmap_atomic(token, km); |
| 3300 | } |
| 3301 | EXPORT_SYMBOL(unmap_extent_buffer); |
| 3302 | |
| 3303 | int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv, |
| 3304 | unsigned long start, |
| 3305 | unsigned long len) |
| 3306 | { |
| 3307 | size_t cur; |
| 3308 | size_t offset; |
| 3309 | struct page *page; |
| 3310 | char *kaddr; |
| 3311 | char *ptr = (char *)ptrv; |
| 3312 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 3313 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 3314 | int ret = 0; |
| 3315 | |
| 3316 | WARN_ON(start > eb->len); |
| 3317 | WARN_ON(start + len > eb->start + eb->len); |
| 3318 | |
| 3319 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 3320 | |
| 3321 | while(len > 0) { |
| 3322 | page = extent_buffer_page(eb, i); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3323 | |
| 3324 | cur = min(len, (PAGE_CACHE_SIZE - offset)); |
| 3325 | |
| 3326 | kaddr = kmap_atomic(page, KM_USER0); |
| 3327 | ret = memcmp(ptr, kaddr + offset, cur); |
| 3328 | kunmap_atomic(kaddr, KM_USER0); |
| 3329 | if (ret) |
| 3330 | break; |
| 3331 | |
| 3332 | ptr += cur; |
| 3333 | len -= cur; |
| 3334 | offset = 0; |
| 3335 | i++; |
| 3336 | } |
| 3337 | return ret; |
| 3338 | } |
| 3339 | EXPORT_SYMBOL(memcmp_extent_buffer); |
| 3340 | |
| 3341 | void write_extent_buffer(struct extent_buffer *eb, const void *srcv, |
| 3342 | unsigned long start, unsigned long len) |
| 3343 | { |
| 3344 | size_t cur; |
| 3345 | size_t offset; |
| 3346 | struct page *page; |
| 3347 | char *kaddr; |
| 3348 | char *src = (char *)srcv; |
| 3349 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 3350 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 3351 | |
| 3352 | WARN_ON(start > eb->len); |
| 3353 | WARN_ON(start + len > eb->start + eb->len); |
| 3354 | |
| 3355 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 3356 | |
| 3357 | while(len > 0) { |
| 3358 | page = extent_buffer_page(eb, i); |
| 3359 | WARN_ON(!PageUptodate(page)); |
| 3360 | |
| 3361 | cur = min(len, PAGE_CACHE_SIZE - offset); |
| 3362 | kaddr = kmap_atomic(page, KM_USER1); |
| 3363 | memcpy(kaddr + offset, src, cur); |
| 3364 | kunmap_atomic(kaddr, KM_USER1); |
| 3365 | |
| 3366 | src += cur; |
| 3367 | len -= cur; |
| 3368 | offset = 0; |
| 3369 | i++; |
| 3370 | } |
| 3371 | } |
| 3372 | EXPORT_SYMBOL(write_extent_buffer); |
| 3373 | |
| 3374 | void memset_extent_buffer(struct extent_buffer *eb, char c, |
| 3375 | unsigned long start, unsigned long len) |
| 3376 | { |
| 3377 | size_t cur; |
| 3378 | size_t offset; |
| 3379 | struct page *page; |
| 3380 | char *kaddr; |
| 3381 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 3382 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 3383 | |
| 3384 | WARN_ON(start > eb->len); |
| 3385 | WARN_ON(start + len > eb->start + eb->len); |
| 3386 | |
| 3387 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 3388 | |
| 3389 | while(len > 0) { |
| 3390 | page = extent_buffer_page(eb, i); |
| 3391 | WARN_ON(!PageUptodate(page)); |
| 3392 | |
| 3393 | cur = min(len, PAGE_CACHE_SIZE - offset); |
| 3394 | kaddr = kmap_atomic(page, KM_USER0); |
| 3395 | memset(kaddr + offset, c, cur); |
| 3396 | kunmap_atomic(kaddr, KM_USER0); |
| 3397 | |
| 3398 | len -= cur; |
| 3399 | offset = 0; |
| 3400 | i++; |
| 3401 | } |
| 3402 | } |
| 3403 | EXPORT_SYMBOL(memset_extent_buffer); |
| 3404 | |
| 3405 | void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src, |
| 3406 | unsigned long dst_offset, unsigned long src_offset, |
| 3407 | unsigned long len) |
| 3408 | { |
| 3409 | u64 dst_len = dst->len; |
| 3410 | size_t cur; |
| 3411 | size_t offset; |
| 3412 | struct page *page; |
| 3413 | char *kaddr; |
| 3414 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 3415 | unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT; |
| 3416 | |
| 3417 | WARN_ON(src->len != dst_len); |
| 3418 | |
| 3419 | offset = (start_offset + dst_offset) & |
| 3420 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 3421 | |
| 3422 | while(len > 0) { |
| 3423 | page = extent_buffer_page(dst, i); |
| 3424 | WARN_ON(!PageUptodate(page)); |
| 3425 | |
| 3426 | cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset)); |
| 3427 | |
| 3428 | kaddr = kmap_atomic(page, KM_USER0); |
| 3429 | read_extent_buffer(src, kaddr + offset, src_offset, cur); |
| 3430 | kunmap_atomic(kaddr, KM_USER0); |
| 3431 | |
| 3432 | src_offset += cur; |
| 3433 | len -= cur; |
| 3434 | offset = 0; |
| 3435 | i++; |
| 3436 | } |
| 3437 | } |
| 3438 | EXPORT_SYMBOL(copy_extent_buffer); |
| 3439 | |
| 3440 | static void move_pages(struct page *dst_page, struct page *src_page, |
| 3441 | unsigned long dst_off, unsigned long src_off, |
| 3442 | unsigned long len) |
| 3443 | { |
| 3444 | char *dst_kaddr = kmap_atomic(dst_page, KM_USER0); |
| 3445 | if (dst_page == src_page) { |
| 3446 | memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len); |
| 3447 | } else { |
| 3448 | char *src_kaddr = kmap_atomic(src_page, KM_USER1); |
| 3449 | char *p = dst_kaddr + dst_off + len; |
| 3450 | char *s = src_kaddr + src_off + len; |
| 3451 | |
| 3452 | while (len--) |
| 3453 | *--p = *--s; |
| 3454 | |
| 3455 | kunmap_atomic(src_kaddr, KM_USER1); |
| 3456 | } |
| 3457 | kunmap_atomic(dst_kaddr, KM_USER0); |
| 3458 | } |
| 3459 | |
| 3460 | static void copy_pages(struct page *dst_page, struct page *src_page, |
| 3461 | unsigned long dst_off, unsigned long src_off, |
| 3462 | unsigned long len) |
| 3463 | { |
| 3464 | char *dst_kaddr = kmap_atomic(dst_page, KM_USER0); |
| 3465 | char *src_kaddr; |
| 3466 | |
| 3467 | if (dst_page != src_page) |
| 3468 | src_kaddr = kmap_atomic(src_page, KM_USER1); |
| 3469 | else |
| 3470 | src_kaddr = dst_kaddr; |
| 3471 | |
| 3472 | memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len); |
| 3473 | kunmap_atomic(dst_kaddr, KM_USER0); |
| 3474 | if (dst_page != src_page) |
| 3475 | kunmap_atomic(src_kaddr, KM_USER1); |
| 3476 | } |
| 3477 | |
| 3478 | void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 3479 | unsigned long src_offset, unsigned long len) |
| 3480 | { |
| 3481 | size_t cur; |
| 3482 | size_t dst_off_in_page; |
| 3483 | size_t src_off_in_page; |
| 3484 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 3485 | unsigned long dst_i; |
| 3486 | unsigned long src_i; |
| 3487 | |
| 3488 | if (src_offset + len > dst->len) { |
| 3489 | printk("memmove bogus src_offset %lu move len %lu len %lu\n", |
| 3490 | src_offset, len, dst->len); |
| 3491 | BUG_ON(1); |
| 3492 | } |
| 3493 | if (dst_offset + len > dst->len) { |
| 3494 | printk("memmove bogus dst_offset %lu move len %lu len %lu\n", |
| 3495 | dst_offset, len, dst->len); |
| 3496 | BUG_ON(1); |
| 3497 | } |
| 3498 | |
| 3499 | while(len > 0) { |
| 3500 | dst_off_in_page = (start_offset + dst_offset) & |
| 3501 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 3502 | src_off_in_page = (start_offset + src_offset) & |
| 3503 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 3504 | |
| 3505 | dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT; |
| 3506 | src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT; |
| 3507 | |
| 3508 | cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - |
| 3509 | src_off_in_page)); |
| 3510 | cur = min_t(unsigned long, cur, |
| 3511 | (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page)); |
| 3512 | |
| 3513 | copy_pages(extent_buffer_page(dst, dst_i), |
| 3514 | extent_buffer_page(dst, src_i), |
| 3515 | dst_off_in_page, src_off_in_page, cur); |
| 3516 | |
| 3517 | src_offset += cur; |
| 3518 | dst_offset += cur; |
| 3519 | len -= cur; |
| 3520 | } |
| 3521 | } |
| 3522 | EXPORT_SYMBOL(memcpy_extent_buffer); |
| 3523 | |
| 3524 | void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 3525 | unsigned long src_offset, unsigned long len) |
| 3526 | { |
| 3527 | size_t cur; |
| 3528 | size_t dst_off_in_page; |
| 3529 | size_t src_off_in_page; |
| 3530 | unsigned long dst_end = dst_offset + len - 1; |
| 3531 | unsigned long src_end = src_offset + len - 1; |
| 3532 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 3533 | unsigned long dst_i; |
| 3534 | unsigned long src_i; |
| 3535 | |
| 3536 | if (src_offset + len > dst->len) { |
| 3537 | printk("memmove bogus src_offset %lu move len %lu len %lu\n", |
| 3538 | src_offset, len, dst->len); |
| 3539 | BUG_ON(1); |
| 3540 | } |
| 3541 | if (dst_offset + len > dst->len) { |
| 3542 | printk("memmove bogus dst_offset %lu move len %lu len %lu\n", |
| 3543 | dst_offset, len, dst->len); |
| 3544 | BUG_ON(1); |
| 3545 | } |
| 3546 | if (dst_offset < src_offset) { |
| 3547 | memcpy_extent_buffer(dst, dst_offset, src_offset, len); |
| 3548 | return; |
| 3549 | } |
| 3550 | while(len > 0) { |
| 3551 | dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT; |
| 3552 | src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT; |
| 3553 | |
| 3554 | dst_off_in_page = (start_offset + dst_end) & |
| 3555 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 3556 | src_off_in_page = (start_offset + src_end) & |
| 3557 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 3558 | |
| 3559 | cur = min_t(unsigned long, len, src_off_in_page + 1); |
| 3560 | cur = min(cur, dst_off_in_page + 1); |
| 3561 | move_pages(extent_buffer_page(dst, dst_i), |
| 3562 | extent_buffer_page(dst, src_i), |
| 3563 | dst_off_in_page - cur + 1, |
| 3564 | src_off_in_page - cur + 1, cur); |
| 3565 | |
| 3566 | dst_end -= cur; |
| 3567 | src_end -= cur; |
| 3568 | len -= cur; |
| 3569 | } |
| 3570 | } |
| 3571 | EXPORT_SYMBOL(memmove_extent_buffer); |