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