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