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