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