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