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