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