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