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