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