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