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