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