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