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