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