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