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