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