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 | { |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 1192 | return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, |
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 | |
| 1260 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1261 | * helper function to set both pages and extents in the tree writeback |
| 1262 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1263 | 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] | 1264 | { |
| 1265 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1266 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1267 | struct page *page; |
| 1268 | |
| 1269 | while (index <= end_index) { |
| 1270 | page = find_get_page(tree->mapping, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1271 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1272 | set_page_writeback(page); |
| 1273 | page_cache_release(page); |
| 1274 | index++; |
| 1275 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1276 | return 0; |
| 1277 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1278 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1279 | /* find the first state struct with 'bits' set after 'start', and |
| 1280 | * return it. tree->lock must be held. NULL will returned if |
| 1281 | * nothing was found after 'start' |
| 1282 | */ |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1283 | struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree, |
| 1284 | u64 start, int bits) |
| 1285 | { |
| 1286 | struct rb_node *node; |
| 1287 | struct extent_state *state; |
| 1288 | |
| 1289 | /* |
| 1290 | * this search will find all the extents that end after |
| 1291 | * our range starts. |
| 1292 | */ |
| 1293 | node = tree_search(tree, start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1294 | if (!node) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1295 | goto out; |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1296 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1297 | while (1) { |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1298 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1299 | if (state->end >= start && (state->state & bits)) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1300 | return state; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1301 | |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1302 | node = rb_next(node); |
| 1303 | if (!node) |
| 1304 | break; |
| 1305 | } |
| 1306 | out: |
| 1307 | return NULL; |
| 1308 | } |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1309 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1310 | /* |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1311 | * find the first offset in the io tree with 'bits' set. zero is |
| 1312 | * returned if we find something, and *start_ret and *end_ret are |
| 1313 | * set to reflect the state struct that was found. |
| 1314 | * |
Wang Sheng-Hui | 477d7ea | 2012-04-06 14:35:47 +0800 | [diff] [blame] | 1315 | * If nothing was found, 1 is returned. If found something, return 0. |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1316 | */ |
| 1317 | int find_first_extent_bit(struct extent_io_tree *tree, u64 start, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1318 | u64 *start_ret, u64 *end_ret, int bits, |
| 1319 | struct extent_state **cached_state) |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1320 | { |
| 1321 | struct extent_state *state; |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1322 | struct rb_node *n; |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1323 | int ret = 1; |
| 1324 | |
| 1325 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1326 | if (cached_state && *cached_state) { |
| 1327 | state = *cached_state; |
| 1328 | if (state->end == start - 1 && state->tree) { |
| 1329 | n = rb_next(&state->rb_node); |
| 1330 | while (n) { |
| 1331 | state = rb_entry(n, struct extent_state, |
| 1332 | rb_node); |
| 1333 | if (state->state & bits) |
| 1334 | goto got_it; |
| 1335 | n = rb_next(n); |
| 1336 | } |
| 1337 | free_extent_state(*cached_state); |
| 1338 | *cached_state = NULL; |
| 1339 | goto out; |
| 1340 | } |
| 1341 | free_extent_state(*cached_state); |
| 1342 | *cached_state = NULL; |
| 1343 | } |
| 1344 | |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1345 | state = find_first_extent_bit_state(tree, start, bits); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1346 | got_it: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1347 | if (state) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1348 | cache_state(state, cached_state); |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1349 | *start_ret = state->start; |
| 1350 | *end_ret = state->end; |
| 1351 | ret = 0; |
| 1352 | } |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1353 | out: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1354 | spin_unlock(&tree->lock); |
| 1355 | return ret; |
| 1356 | } |
| 1357 | |
| 1358 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1359 | * find a contiguous range of bytes in the file marked as delalloc, not |
| 1360 | * more than 'max_bytes'. start and end are used to return the range, |
| 1361 | * |
| 1362 | * 1 is returned if we find something, 0 if nothing was in the tree |
| 1363 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1364 | static noinline u64 find_delalloc_range(struct extent_io_tree *tree, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1365 | u64 *start, u64 *end, u64 max_bytes, |
| 1366 | struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1367 | { |
| 1368 | struct rb_node *node; |
| 1369 | struct extent_state *state; |
| 1370 | u64 cur_start = *start; |
| 1371 | u64 found = 0; |
| 1372 | u64 total_bytes = 0; |
| 1373 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1374 | spin_lock(&tree->lock); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1375 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1376 | /* |
| 1377 | * this search will find all the extents that end after |
| 1378 | * our range starts. |
| 1379 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1380 | node = tree_search(tree, cur_start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1381 | if (!node) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1382 | if (!found) |
| 1383 | *end = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1384 | goto out; |
| 1385 | } |
| 1386 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1387 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1388 | state = rb_entry(node, struct extent_state, rb_node); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1389 | if (found && (state->start != cur_start || |
| 1390 | (state->state & EXTENT_BOUNDARY))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1391 | goto out; |
| 1392 | } |
| 1393 | if (!(state->state & EXTENT_DELALLOC)) { |
| 1394 | if (!found) |
| 1395 | *end = state->end; |
| 1396 | goto out; |
| 1397 | } |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1398 | if (!found) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1399 | *start = state->start; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1400 | *cached_state = state; |
| 1401 | atomic_inc(&state->refs); |
| 1402 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1403 | found++; |
| 1404 | *end = state->end; |
| 1405 | cur_start = state->end + 1; |
| 1406 | node = rb_next(node); |
| 1407 | if (!node) |
| 1408 | break; |
| 1409 | total_bytes += state->end - state->start + 1; |
| 1410 | if (total_bytes >= max_bytes) |
| 1411 | break; |
| 1412 | } |
| 1413 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1414 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1415 | return found; |
| 1416 | } |
| 1417 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1418 | static noinline void __unlock_for_delalloc(struct inode *inode, |
| 1419 | struct page *locked_page, |
| 1420 | u64 start, u64 end) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1421 | { |
| 1422 | int ret; |
| 1423 | struct page *pages[16]; |
| 1424 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1425 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1426 | unsigned long nr_pages = end_index - index + 1; |
| 1427 | int i; |
| 1428 | |
| 1429 | if (index == locked_page->index && end_index == index) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1430 | return; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1431 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1432 | while (nr_pages > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1433 | ret = find_get_pages_contig(inode->i_mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1434 | min_t(unsigned long, nr_pages, |
| 1435 | ARRAY_SIZE(pages)), pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1436 | for (i = 0; i < ret; i++) { |
| 1437 | if (pages[i] != locked_page) |
| 1438 | unlock_page(pages[i]); |
| 1439 | page_cache_release(pages[i]); |
| 1440 | } |
| 1441 | nr_pages -= ret; |
| 1442 | index += ret; |
| 1443 | cond_resched(); |
| 1444 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | static noinline int lock_delalloc_pages(struct inode *inode, |
| 1448 | struct page *locked_page, |
| 1449 | u64 delalloc_start, |
| 1450 | u64 delalloc_end) |
| 1451 | { |
| 1452 | unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT; |
| 1453 | unsigned long start_index = index; |
| 1454 | unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT; |
| 1455 | unsigned long pages_locked = 0; |
| 1456 | struct page *pages[16]; |
| 1457 | unsigned long nrpages; |
| 1458 | int ret; |
| 1459 | int i; |
| 1460 | |
| 1461 | /* the caller is responsible for locking the start index */ |
| 1462 | if (index == locked_page->index && index == end_index) |
| 1463 | return 0; |
| 1464 | |
| 1465 | /* skip the page at the start index */ |
| 1466 | nrpages = end_index - index + 1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1467 | while (nrpages > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1468 | ret = find_get_pages_contig(inode->i_mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1469 | min_t(unsigned long, |
| 1470 | nrpages, ARRAY_SIZE(pages)), pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1471 | if (ret == 0) { |
| 1472 | ret = -EAGAIN; |
| 1473 | goto done; |
| 1474 | } |
| 1475 | /* now we have an array of pages, lock them all */ |
| 1476 | for (i = 0; i < ret; i++) { |
| 1477 | /* |
| 1478 | * the caller is taking responsibility for |
| 1479 | * locked_page |
| 1480 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1481 | if (pages[i] != locked_page) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1482 | lock_page(pages[i]); |
Chris Mason | f2b1c41 | 2008-11-10 07:31:30 -0500 | [diff] [blame] | 1483 | if (!PageDirty(pages[i]) || |
| 1484 | pages[i]->mapping != inode->i_mapping) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1485 | ret = -EAGAIN; |
| 1486 | unlock_page(pages[i]); |
| 1487 | page_cache_release(pages[i]); |
| 1488 | goto done; |
| 1489 | } |
| 1490 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1491 | page_cache_release(pages[i]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1492 | pages_locked++; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1493 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1494 | nrpages -= ret; |
| 1495 | index += ret; |
| 1496 | cond_resched(); |
| 1497 | } |
| 1498 | ret = 0; |
| 1499 | done: |
| 1500 | if (ret && pages_locked) { |
| 1501 | __unlock_for_delalloc(inode, locked_page, |
| 1502 | delalloc_start, |
| 1503 | ((u64)(start_index + pages_locked - 1)) << |
| 1504 | PAGE_CACHE_SHIFT); |
| 1505 | } |
| 1506 | return ret; |
| 1507 | } |
| 1508 | |
| 1509 | /* |
| 1510 | * find a contiguous range of bytes in the file marked as delalloc, not |
| 1511 | * more than 'max_bytes'. start and end are used to return the range, |
| 1512 | * |
| 1513 | * 1 is returned if we find something, 0 if nothing was in the tree |
| 1514 | */ |
| 1515 | static noinline u64 find_lock_delalloc_range(struct inode *inode, |
| 1516 | struct extent_io_tree *tree, |
| 1517 | struct page *locked_page, |
| 1518 | u64 *start, u64 *end, |
| 1519 | u64 max_bytes) |
| 1520 | { |
| 1521 | u64 delalloc_start; |
| 1522 | u64 delalloc_end; |
| 1523 | u64 found; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1524 | struct extent_state *cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1525 | int ret; |
| 1526 | int loops = 0; |
| 1527 | |
| 1528 | again: |
| 1529 | /* step one, find a bunch of delalloc bytes starting at start */ |
| 1530 | delalloc_start = *start; |
| 1531 | delalloc_end = 0; |
| 1532 | found = find_delalloc_range(tree, &delalloc_start, &delalloc_end, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1533 | max_bytes, &cached_state); |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1534 | if (!found || delalloc_end <= *start) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1535 | *start = delalloc_start; |
| 1536 | *end = delalloc_end; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1537 | free_extent_state(cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1538 | return found; |
| 1539 | } |
| 1540 | |
| 1541 | /* |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1542 | * start comes from the offset of locked_page. We have to lock |
| 1543 | * pages in order, so we can't process delalloc bytes before |
| 1544 | * locked_page |
| 1545 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1546 | if (delalloc_start < *start) |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1547 | delalloc_start = *start; |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1548 | |
| 1549 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1550 | * make sure to limit the number of pages we try to lock down |
| 1551 | * if we're looping. |
| 1552 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1553 | if (delalloc_end + 1 - delalloc_start > max_bytes && loops) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1554 | delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1555 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1556 | /* step two, lock all the pages after the page that has start */ |
| 1557 | ret = lock_delalloc_pages(inode, locked_page, |
| 1558 | delalloc_start, delalloc_end); |
| 1559 | if (ret == -EAGAIN) { |
| 1560 | /* some of the pages are gone, lets avoid looping by |
| 1561 | * shortening the size of the delalloc range we're searching |
| 1562 | */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1563 | free_extent_state(cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1564 | if (!loops) { |
| 1565 | unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1); |
| 1566 | max_bytes = PAGE_CACHE_SIZE - offset; |
| 1567 | loops = 1; |
| 1568 | goto again; |
| 1569 | } else { |
| 1570 | found = 0; |
| 1571 | goto out_failed; |
| 1572 | } |
| 1573 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1574 | BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1575 | |
| 1576 | /* step three, lock the state bits for the whole range */ |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1577 | lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1578 | |
| 1579 | /* then test to make sure it is all still delalloc */ |
| 1580 | ret = test_range_bit(tree, delalloc_start, delalloc_end, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1581 | EXTENT_DELALLOC, 1, cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1582 | if (!ret) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1583 | unlock_extent_cached(tree, delalloc_start, delalloc_end, |
| 1584 | &cached_state, GFP_NOFS); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1585 | __unlock_for_delalloc(inode, locked_page, |
| 1586 | delalloc_start, delalloc_end); |
| 1587 | cond_resched(); |
| 1588 | goto again; |
| 1589 | } |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1590 | free_extent_state(cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1591 | *start = delalloc_start; |
| 1592 | *end = delalloc_end; |
| 1593 | out_failed: |
| 1594 | return found; |
| 1595 | } |
| 1596 | |
| 1597 | int extent_clear_unlock_delalloc(struct inode *inode, |
| 1598 | struct extent_io_tree *tree, |
| 1599 | u64 start, u64 end, struct page *locked_page, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1600 | unsigned long op) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1601 | { |
| 1602 | int ret; |
| 1603 | struct page *pages[16]; |
| 1604 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1605 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1606 | unsigned long nr_pages = end_index - index + 1; |
| 1607 | int i; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1608 | int clear_bits = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1609 | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1610 | if (op & EXTENT_CLEAR_UNLOCK) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1611 | clear_bits |= EXTENT_LOCKED; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1612 | if (op & EXTENT_CLEAR_DIRTY) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1613 | clear_bits |= EXTENT_DIRTY; |
| 1614 | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1615 | if (op & EXTENT_CLEAR_DELALLOC) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1616 | clear_bits |= EXTENT_DELALLOC; |
| 1617 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1618 | 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] | 1619 | if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
| 1620 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK | |
| 1621 | EXTENT_SET_PRIVATE2))) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1622 | return 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1623 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1624 | while (nr_pages > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1625 | ret = find_get_pages_contig(inode->i_mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1626 | min_t(unsigned long, |
| 1627 | nr_pages, ARRAY_SIZE(pages)), pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1628 | for (i = 0; i < ret; i++) { |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1629 | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1630 | if (op & EXTENT_SET_PRIVATE2) |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1631 | SetPagePrivate2(pages[i]); |
| 1632 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1633 | if (pages[i] == locked_page) { |
| 1634 | page_cache_release(pages[i]); |
| 1635 | continue; |
| 1636 | } |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1637 | if (op & EXTENT_CLEAR_DIRTY) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1638 | clear_page_dirty_for_io(pages[i]); |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1639 | if (op & EXTENT_SET_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1640 | set_page_writeback(pages[i]); |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1641 | if (op & EXTENT_END_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1642 | end_page_writeback(pages[i]); |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1643 | if (op & EXTENT_CLEAR_UNLOCK_PAGE) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1644 | unlock_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1645 | page_cache_release(pages[i]); |
| 1646 | } |
| 1647 | nr_pages -= ret; |
| 1648 | index += ret; |
| 1649 | cond_resched(); |
| 1650 | } |
| 1651 | return 0; |
| 1652 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1653 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1654 | /* |
| 1655 | * count the number of bytes in the tree that have a given bit(s) |
| 1656 | * set. This can be fairly slow, except for EXTENT_DIRTY which is |
| 1657 | * cached. The total number found is returned. |
| 1658 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1659 | u64 count_range_bits(struct extent_io_tree *tree, |
| 1660 | u64 *start, u64 search_end, u64 max_bytes, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1661 | unsigned long bits, int contig) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1662 | { |
| 1663 | struct rb_node *node; |
| 1664 | struct extent_state *state; |
| 1665 | u64 cur_start = *start; |
| 1666 | u64 total_bytes = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1667 | u64 last = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1668 | int found = 0; |
| 1669 | |
| 1670 | if (search_end <= cur_start) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1671 | WARN_ON(1); |
| 1672 | return 0; |
| 1673 | } |
| 1674 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1675 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1676 | if (cur_start == 0 && bits == EXTENT_DIRTY) { |
| 1677 | total_bytes = tree->dirty_bytes; |
| 1678 | goto out; |
| 1679 | } |
| 1680 | /* |
| 1681 | * this search will find all the extents that end after |
| 1682 | * our range starts. |
| 1683 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1684 | node = tree_search(tree, cur_start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1685 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1686 | goto out; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1687 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1688 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1689 | state = rb_entry(node, struct extent_state, rb_node); |
| 1690 | if (state->start > search_end) |
| 1691 | break; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1692 | if (contig && found && state->start > last + 1) |
| 1693 | break; |
| 1694 | if (state->end >= cur_start && (state->state & bits) == bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1695 | total_bytes += min(search_end, state->end) + 1 - |
| 1696 | max(cur_start, state->start); |
| 1697 | if (total_bytes >= max_bytes) |
| 1698 | break; |
| 1699 | if (!found) { |
Josef Bacik | af60bed | 2011-05-04 11:11:17 -0400 | [diff] [blame] | 1700 | *start = max(cur_start, state->start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1701 | found = 1; |
| 1702 | } |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1703 | last = state->end; |
| 1704 | } else if (contig && found) { |
| 1705 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1706 | } |
| 1707 | node = rb_next(node); |
| 1708 | if (!node) |
| 1709 | break; |
| 1710 | } |
| 1711 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1712 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1713 | return total_bytes; |
| 1714 | } |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1715 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1716 | /* |
| 1717 | * set the private field for a given byte offset in the tree. If there isn't |
| 1718 | * an extent_state there already, this does nothing. |
| 1719 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1720 | int set_state_private(struct extent_io_tree *tree, u64 start, u64 private) |
| 1721 | { |
| 1722 | struct rb_node *node; |
| 1723 | struct extent_state *state; |
| 1724 | int ret = 0; |
| 1725 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1726 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1727 | /* |
| 1728 | * this search will find all the extents that end after |
| 1729 | * our range starts. |
| 1730 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1731 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1732 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1733 | ret = -ENOENT; |
| 1734 | goto out; |
| 1735 | } |
| 1736 | state = rb_entry(node, struct extent_state, rb_node); |
| 1737 | if (state->start != start) { |
| 1738 | ret = -ENOENT; |
| 1739 | goto out; |
| 1740 | } |
| 1741 | state->private = private; |
| 1742 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1743 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1744 | return ret; |
| 1745 | } |
| 1746 | |
| 1747 | int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private) |
| 1748 | { |
| 1749 | struct rb_node *node; |
| 1750 | struct extent_state *state; |
| 1751 | int ret = 0; |
| 1752 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1753 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1754 | /* |
| 1755 | * this search will find all the extents that end after |
| 1756 | * our range starts. |
| 1757 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1758 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1759 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1760 | ret = -ENOENT; |
| 1761 | goto out; |
| 1762 | } |
| 1763 | state = rb_entry(node, struct extent_state, rb_node); |
| 1764 | if (state->start != start) { |
| 1765 | ret = -ENOENT; |
| 1766 | goto out; |
| 1767 | } |
| 1768 | *private = state->private; |
| 1769 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1770 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1771 | return ret; |
| 1772 | } |
| 1773 | |
| 1774 | /* |
| 1775 | * searches a range in the state tree for a given mask. |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1776 | * 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] | 1777 | * has the bits set. Otherwise, 1 is returned if any bit in the |
| 1778 | * range is found set. |
| 1779 | */ |
| 1780 | 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] | 1781 | int bits, int filled, struct extent_state *cached) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1782 | { |
| 1783 | struct extent_state *state = NULL; |
| 1784 | struct rb_node *node; |
| 1785 | int bitset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1786 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1787 | spin_lock(&tree->lock); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 1788 | if (cached && cached->tree && cached->start <= start && |
| 1789 | cached->end > start) |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1790 | node = &cached->rb_node; |
| 1791 | else |
| 1792 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1793 | while (node && start <= end) { |
| 1794 | state = rb_entry(node, struct extent_state, rb_node); |
| 1795 | |
| 1796 | if (filled && state->start > start) { |
| 1797 | bitset = 0; |
| 1798 | break; |
| 1799 | } |
| 1800 | |
| 1801 | if (state->start > end) |
| 1802 | break; |
| 1803 | |
| 1804 | if (state->state & bits) { |
| 1805 | bitset = 1; |
| 1806 | if (!filled) |
| 1807 | break; |
| 1808 | } else if (filled) { |
| 1809 | bitset = 0; |
| 1810 | break; |
| 1811 | } |
Chris Mason | 46562ce | 2009-09-23 20:23:16 -0400 | [diff] [blame] | 1812 | |
| 1813 | if (state->end == (u64)-1) |
| 1814 | break; |
| 1815 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1816 | start = state->end + 1; |
| 1817 | if (start > end) |
| 1818 | break; |
| 1819 | node = rb_next(node); |
| 1820 | if (!node) { |
| 1821 | if (filled) |
| 1822 | bitset = 0; |
| 1823 | break; |
| 1824 | } |
| 1825 | } |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1826 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1827 | return bitset; |
| 1828 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1829 | |
| 1830 | /* |
| 1831 | * helper function to set a given page up to date if all the |
| 1832 | * extents in the tree for that page are up to date |
| 1833 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1834 | 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] | 1835 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 1836 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1837 | u64 end = start + PAGE_CACHE_SIZE - 1; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1838 | if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1839 | SetPageUptodate(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | /* |
| 1843 | * helper function to unlock a page if all the extents in the tree |
| 1844 | * for that page are unlocked |
| 1845 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1846 | 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] | 1847 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 1848 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1849 | u64 end = start + PAGE_CACHE_SIZE - 1; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1850 | if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1851 | unlock_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | /* |
| 1855 | * helper function to end page writeback if all the extents |
| 1856 | * in the tree for that page are done with writeback |
| 1857 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1858 | static void check_page_writeback(struct extent_io_tree *tree, |
| 1859 | struct page *page) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1860 | { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1861 | end_page_writeback(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1862 | } |
| 1863 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1864 | /* |
| 1865 | * When IO fails, either with EIO or csum verification fails, we |
| 1866 | * try other mirrors that might have a good copy of the data. This |
| 1867 | * io_failure_record is used to record state as we go through all the |
| 1868 | * mirrors. If another mirror has good data, the page is set up to date |
| 1869 | * and things continue. If a good mirror can't be found, the original |
| 1870 | * bio end_io callback is called to indicate things have failed. |
| 1871 | */ |
| 1872 | struct io_failure_record { |
| 1873 | struct page *page; |
| 1874 | u64 start; |
| 1875 | u64 len; |
| 1876 | u64 logical; |
| 1877 | unsigned long bio_flags; |
| 1878 | int this_mirror; |
| 1879 | int failed_mirror; |
| 1880 | int in_validation; |
| 1881 | }; |
| 1882 | |
| 1883 | static int free_io_failure(struct inode *inode, struct io_failure_record *rec, |
| 1884 | int did_repair) |
| 1885 | { |
| 1886 | int ret; |
| 1887 | int err = 0; |
| 1888 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 1889 | |
| 1890 | set_state_private(failure_tree, rec->start, 0); |
| 1891 | ret = clear_extent_bits(failure_tree, rec->start, |
| 1892 | rec->start + rec->len - 1, |
| 1893 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 1894 | if (ret) |
| 1895 | err = ret; |
| 1896 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1897 | ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start, |
| 1898 | rec->start + rec->len - 1, |
| 1899 | EXTENT_DAMAGED, GFP_NOFS); |
| 1900 | if (ret && !err) |
| 1901 | err = ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1902 | |
| 1903 | kfree(rec); |
| 1904 | return err; |
| 1905 | } |
| 1906 | |
| 1907 | static void repair_io_failure_callback(struct bio *bio, int err) |
| 1908 | { |
| 1909 | complete(bio->bi_private); |
| 1910 | } |
| 1911 | |
| 1912 | /* |
| 1913 | * this bypasses the standard btrfs submit functions deliberately, as |
| 1914 | * the standard behavior is to write all copies in a raid setup. here we only |
| 1915 | * want to write the one bad copy. so we do the mapping for ourselves and issue |
| 1916 | * submit_bio directly. |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1917 | * to avoid any synchronization issues, wait for the data after writing, which |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1918 | * actually prevents the read that triggered the error from finishing. |
| 1919 | * currently, there can be no more than two copies of every data bit. thus, |
| 1920 | * exactly one rewrite is required. |
| 1921 | */ |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1922 | int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start, |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1923 | u64 length, u64 logical, struct page *page, |
| 1924 | int mirror_num) |
| 1925 | { |
| 1926 | struct bio *bio; |
| 1927 | struct btrfs_device *dev; |
| 1928 | DECLARE_COMPLETION_ONSTACK(compl); |
| 1929 | u64 map_length = 0; |
| 1930 | u64 sector; |
| 1931 | struct btrfs_bio *bbio = NULL; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1932 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1933 | int ret; |
| 1934 | |
| 1935 | BUG_ON(!mirror_num); |
| 1936 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1937 | /* we can't repair anything in raid56 yet */ |
| 1938 | if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num)) |
| 1939 | return 0; |
| 1940 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1941 | bio = bio_alloc(GFP_NOFS, 1); |
| 1942 | if (!bio) |
| 1943 | return -EIO; |
| 1944 | bio->bi_private = &compl; |
| 1945 | bio->bi_end_io = repair_io_failure_callback; |
| 1946 | bio->bi_size = 0; |
| 1947 | map_length = length; |
| 1948 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1949 | ret = btrfs_map_block(fs_info, WRITE, logical, |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1950 | &map_length, &bbio, mirror_num); |
| 1951 | if (ret) { |
| 1952 | bio_put(bio); |
| 1953 | return -EIO; |
| 1954 | } |
| 1955 | BUG_ON(mirror_num != bbio->mirror_num); |
| 1956 | sector = bbio->stripes[mirror_num-1].physical >> 9; |
| 1957 | bio->bi_sector = sector; |
| 1958 | dev = bbio->stripes[mirror_num-1].dev; |
| 1959 | kfree(bbio); |
| 1960 | if (!dev || !dev->bdev || !dev->writeable) { |
| 1961 | bio_put(bio); |
| 1962 | return -EIO; |
| 1963 | } |
| 1964 | bio->bi_bdev = dev->bdev; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 1965 | bio_add_page(bio, page, length, start - page_offset(page)); |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 1966 | btrfsic_submit_bio(WRITE_SYNC, bio); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1967 | wait_for_completion(&compl); |
| 1968 | |
| 1969 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 1970 | /* try to remap that extent elsewhere? */ |
| 1971 | bio_put(bio); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 1972 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1973 | return -EIO; |
| 1974 | } |
| 1975 | |
Anand Jain | d5b025d | 2012-07-02 22:05:21 -0600 | [diff] [blame] | 1976 | printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu " |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1977 | "(dev %s sector %llu)\n", page->mapping->host->i_ino, |
| 1978 | start, rcu_str_deref(dev->name), sector); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1979 | |
| 1980 | bio_put(bio); |
| 1981 | return 0; |
| 1982 | } |
| 1983 | |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 1984 | int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb, |
| 1985 | int mirror_num) |
| 1986 | { |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 1987 | u64 start = eb->start; |
| 1988 | unsigned long i, num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | d95603b | 2012-04-12 15:55:15 -0400 | [diff] [blame] | 1989 | int ret = 0; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 1990 | |
| 1991 | for (i = 0; i < num_pages; i++) { |
| 1992 | struct page *p = extent_buffer_page(eb, i); |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1993 | ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE, |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 1994 | start, p, mirror_num); |
| 1995 | if (ret) |
| 1996 | break; |
| 1997 | start += PAGE_CACHE_SIZE; |
| 1998 | } |
| 1999 | |
| 2000 | return ret; |
| 2001 | } |
| 2002 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2003 | /* |
| 2004 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 2005 | * to see if we need to process or clean up an io_failure_record |
| 2006 | */ |
| 2007 | static int clean_io_failure(u64 start, struct page *page) |
| 2008 | { |
| 2009 | u64 private; |
| 2010 | u64 private_failure; |
| 2011 | struct io_failure_record *failrec; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2012 | struct btrfs_fs_info *fs_info; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2013 | struct extent_state *state; |
| 2014 | int num_copies; |
| 2015 | int did_repair = 0; |
| 2016 | int ret; |
| 2017 | struct inode *inode = page->mapping->host; |
| 2018 | |
| 2019 | private = 0; |
| 2020 | ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
| 2021 | (u64)-1, 1, EXTENT_DIRTY, 0); |
| 2022 | if (!ret) |
| 2023 | return 0; |
| 2024 | |
| 2025 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start, |
| 2026 | &private_failure); |
| 2027 | if (ret) |
| 2028 | return 0; |
| 2029 | |
| 2030 | failrec = (struct io_failure_record *)(unsigned long) private_failure; |
| 2031 | BUG_ON(!failrec->this_mirror); |
| 2032 | |
| 2033 | if (failrec->in_validation) { |
| 2034 | /* there was no real error, just free the record */ |
| 2035 | pr_debug("clean_io_failure: freeing dummy error at %llu\n", |
| 2036 | failrec->start); |
| 2037 | did_repair = 1; |
| 2038 | goto out; |
| 2039 | } |
| 2040 | |
| 2041 | spin_lock(&BTRFS_I(inode)->io_tree.lock); |
| 2042 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 2043 | failrec->start, |
| 2044 | EXTENT_LOCKED); |
| 2045 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); |
| 2046 | |
| 2047 | if (state && state->start == failrec->start) { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2048 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2049 | num_copies = btrfs_num_copies(fs_info, failrec->logical, |
| 2050 | failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2051 | if (num_copies > 1) { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2052 | ret = repair_io_failure(fs_info, start, failrec->len, |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2053 | failrec->logical, page, |
| 2054 | failrec->failed_mirror); |
| 2055 | did_repair = !ret; |
| 2056 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2057 | ret = 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | out: |
| 2061 | if (!ret) |
| 2062 | ret = free_io_failure(inode, failrec, did_repair); |
| 2063 | |
| 2064 | return ret; |
| 2065 | } |
| 2066 | |
| 2067 | /* |
| 2068 | * this is a generic handler for readpage errors (default |
| 2069 | * readpage_io_failed_hook). if other copies exist, read those and write back |
| 2070 | * good data to the failed position. does not investigate in remapping the |
| 2071 | * failed extent elsewhere, hoping the device will be smart enough to do this as |
| 2072 | * needed |
| 2073 | */ |
| 2074 | |
| 2075 | static int bio_readpage_error(struct bio *failed_bio, struct page *page, |
| 2076 | u64 start, u64 end, int failed_mirror, |
| 2077 | struct extent_state *state) |
| 2078 | { |
| 2079 | struct io_failure_record *failrec = NULL; |
| 2080 | u64 private; |
| 2081 | struct extent_map *em; |
| 2082 | struct inode *inode = page->mapping->host; |
| 2083 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 2084 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 2085 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 2086 | struct bio *bio; |
| 2087 | int num_copies; |
| 2088 | int ret; |
| 2089 | int read_mode; |
| 2090 | u64 logical; |
| 2091 | |
| 2092 | BUG_ON(failed_bio->bi_rw & REQ_WRITE); |
| 2093 | |
| 2094 | ret = get_state_private(failure_tree, start, &private); |
| 2095 | if (ret) { |
| 2096 | failrec = kzalloc(sizeof(*failrec), GFP_NOFS); |
| 2097 | if (!failrec) |
| 2098 | return -ENOMEM; |
| 2099 | failrec->start = start; |
| 2100 | failrec->len = end - start + 1; |
| 2101 | failrec->this_mirror = 0; |
| 2102 | failrec->bio_flags = 0; |
| 2103 | failrec->in_validation = 0; |
| 2104 | |
| 2105 | read_lock(&em_tree->lock); |
| 2106 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 2107 | if (!em) { |
| 2108 | read_unlock(&em_tree->lock); |
| 2109 | kfree(failrec); |
| 2110 | return -EIO; |
| 2111 | } |
| 2112 | |
| 2113 | if (em->start > start || em->start + em->len < start) { |
| 2114 | free_extent_map(em); |
| 2115 | em = NULL; |
| 2116 | } |
| 2117 | read_unlock(&em_tree->lock); |
| 2118 | |
Tsutomu Itoh | 7a2d6a6 | 2012-10-01 03:07:15 -0600 | [diff] [blame] | 2119 | if (!em) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2120 | kfree(failrec); |
| 2121 | return -EIO; |
| 2122 | } |
| 2123 | logical = start - em->start; |
| 2124 | logical = em->block_start + logical; |
| 2125 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 2126 | logical = em->block_start; |
| 2127 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
| 2128 | extent_set_compress_type(&failrec->bio_flags, |
| 2129 | em->compress_type); |
| 2130 | } |
| 2131 | pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, " |
| 2132 | "len=%llu\n", logical, start, failrec->len); |
| 2133 | failrec->logical = logical; |
| 2134 | free_extent_map(em); |
| 2135 | |
| 2136 | /* set the bits in the private failure tree */ |
| 2137 | ret = set_extent_bits(failure_tree, start, end, |
| 2138 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 2139 | if (ret >= 0) |
| 2140 | ret = set_state_private(failure_tree, start, |
| 2141 | (u64)(unsigned long)failrec); |
| 2142 | /* set the bits in the inode's tree */ |
| 2143 | if (ret >= 0) |
| 2144 | ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED, |
| 2145 | GFP_NOFS); |
| 2146 | if (ret < 0) { |
| 2147 | kfree(failrec); |
| 2148 | return ret; |
| 2149 | } |
| 2150 | } else { |
| 2151 | failrec = (struct io_failure_record *)(unsigned long)private; |
| 2152 | pr_debug("bio_readpage_error: (found) logical=%llu, " |
| 2153 | "start=%llu, len=%llu, validation=%d\n", |
| 2154 | failrec->logical, failrec->start, failrec->len, |
| 2155 | failrec->in_validation); |
| 2156 | /* |
| 2157 | * when data can be on disk more than twice, add to failrec here |
| 2158 | * (e.g. with a list for failed_mirror) to make |
| 2159 | * clean_io_failure() clean all those errors at once. |
| 2160 | */ |
| 2161 | } |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 2162 | num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info, |
| 2163 | failrec->logical, failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2164 | if (num_copies == 1) { |
| 2165 | /* |
| 2166 | * we only have a single copy of the data, so don't bother with |
| 2167 | * all the retry and error correction code that follows. no |
| 2168 | * matter what the error is, it is very likely to persist. |
| 2169 | */ |
| 2170 | pr_debug("bio_readpage_error: cannot repair, num_copies == 1. " |
| 2171 | "state=%p, num_copies=%d, next_mirror %d, " |
| 2172 | "failed_mirror %d\n", state, num_copies, |
| 2173 | failrec->this_mirror, failed_mirror); |
| 2174 | free_io_failure(inode, failrec, 0); |
| 2175 | return -EIO; |
| 2176 | } |
| 2177 | |
| 2178 | if (!state) { |
| 2179 | spin_lock(&tree->lock); |
| 2180 | state = find_first_extent_bit_state(tree, failrec->start, |
| 2181 | EXTENT_LOCKED); |
| 2182 | if (state && state->start != failrec->start) |
| 2183 | state = NULL; |
| 2184 | spin_unlock(&tree->lock); |
| 2185 | } |
| 2186 | |
| 2187 | /* |
| 2188 | * there are two premises: |
| 2189 | * a) deliver good data to the caller |
| 2190 | * b) correct the bad sectors on disk |
| 2191 | */ |
| 2192 | if (failed_bio->bi_vcnt > 1) { |
| 2193 | /* |
| 2194 | * to fulfill b), we need to know the exact failing sectors, as |
| 2195 | * we don't want to rewrite any more than the failed ones. thus, |
| 2196 | * we need separate read requests for the failed bio |
| 2197 | * |
| 2198 | * if the following BUG_ON triggers, our validation request got |
| 2199 | * merged. we need separate requests for our algorithm to work. |
| 2200 | */ |
| 2201 | BUG_ON(failrec->in_validation); |
| 2202 | failrec->in_validation = 1; |
| 2203 | failrec->this_mirror = failed_mirror; |
| 2204 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; |
| 2205 | } else { |
| 2206 | /* |
| 2207 | * we're ready to fulfill a) and b) alongside. get a good copy |
| 2208 | * of the failed sector and if we succeed, we have setup |
| 2209 | * everything for repair_io_failure to do the rest for us. |
| 2210 | */ |
| 2211 | if (failrec->in_validation) { |
| 2212 | BUG_ON(failrec->this_mirror != failed_mirror); |
| 2213 | failrec->in_validation = 0; |
| 2214 | failrec->this_mirror = 0; |
| 2215 | } |
| 2216 | failrec->failed_mirror = failed_mirror; |
| 2217 | failrec->this_mirror++; |
| 2218 | if (failrec->this_mirror == failed_mirror) |
| 2219 | failrec->this_mirror++; |
| 2220 | read_mode = READ_SYNC; |
| 2221 | } |
| 2222 | |
| 2223 | if (!state || failrec->this_mirror > num_copies) { |
| 2224 | pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, " |
| 2225 | "next_mirror %d, failed_mirror %d\n", state, |
| 2226 | num_copies, failrec->this_mirror, failed_mirror); |
| 2227 | free_io_failure(inode, failrec, 0); |
| 2228 | return -EIO; |
| 2229 | } |
| 2230 | |
| 2231 | bio = bio_alloc(GFP_NOFS, 1); |
Tsutomu Itoh | e627ee7 | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 2232 | if (!bio) { |
| 2233 | free_io_failure(inode, failrec, 0); |
| 2234 | return -EIO; |
| 2235 | } |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2236 | bio->bi_private = state; |
| 2237 | bio->bi_end_io = failed_bio->bi_end_io; |
| 2238 | bio->bi_sector = failrec->logical >> 9; |
| 2239 | bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; |
| 2240 | bio->bi_size = 0; |
| 2241 | |
| 2242 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); |
| 2243 | |
| 2244 | pr_debug("bio_readpage_error: submitting new read[%#x] to " |
| 2245 | "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode, |
| 2246 | failrec->this_mirror, num_copies, failrec->in_validation); |
| 2247 | |
Tsutomu Itoh | 013bd4c | 2012-02-16 10:11:40 +0900 | [diff] [blame] | 2248 | ret = tree->ops->submit_bio_hook(inode, read_mode, bio, |
| 2249 | failrec->this_mirror, |
| 2250 | failrec->bio_flags, 0); |
| 2251 | return ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2252 | } |
| 2253 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2254 | /* lots and lots of room for performance fixes in the end_bio funcs */ |
| 2255 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2256 | int end_extent_writepage(struct page *page, int err, u64 start, u64 end) |
| 2257 | { |
| 2258 | int uptodate = (err == 0); |
| 2259 | struct extent_io_tree *tree; |
| 2260 | int ret; |
| 2261 | |
| 2262 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 2263 | |
| 2264 | if (tree->ops && tree->ops->writepage_end_io_hook) { |
| 2265 | ret = tree->ops->writepage_end_io_hook(page, start, |
| 2266 | end, NULL, uptodate); |
| 2267 | if (ret) |
| 2268 | uptodate = 0; |
| 2269 | } |
| 2270 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2271 | if (!uptodate) { |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2272 | ClearPageUptodate(page); |
| 2273 | SetPageError(page); |
| 2274 | } |
| 2275 | return 0; |
| 2276 | } |
| 2277 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2278 | /* |
| 2279 | * after a writepage IO is done, we need to: |
| 2280 | * clear the uptodate bits on error |
| 2281 | * clear the writeback bits in the extent tree for this IO |
| 2282 | * end_page_writeback if the page has no more pending IO |
| 2283 | * |
| 2284 | * Scheduling is not allowed, so the extent state tree is expected |
| 2285 | * to have one and only one object corresponding to this IO. |
| 2286 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2287 | static void end_bio_extent_writepage(struct bio *bio, int err) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2288 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2289 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2290 | struct extent_io_tree *tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2291 | u64 start; |
| 2292 | u64 end; |
| 2293 | int whole_page; |
| 2294 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2295 | do { |
| 2296 | struct page *page = bvec->bv_page; |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2297 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 2298 | |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2299 | start = page_offset(page) + bvec->bv_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2300 | end = start + bvec->bv_len - 1; |
| 2301 | |
| 2302 | if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE) |
| 2303 | whole_page = 1; |
| 2304 | else |
| 2305 | whole_page = 0; |
| 2306 | |
| 2307 | if (--bvec >= bio->bi_io_vec) |
| 2308 | prefetchw(&bvec->bv_page->flags); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2309 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2310 | if (end_extent_writepage(page, err, start, end)) |
| 2311 | continue; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2312 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2313 | if (whole_page) |
| 2314 | end_page_writeback(page); |
| 2315 | else |
| 2316 | check_page_writeback(tree, page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2317 | } while (bvec >= bio->bi_io_vec); |
Chris Mason | 2b1f55b | 2008-09-24 11:48:04 -0400 | [diff] [blame] | 2318 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2319 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2320 | } |
| 2321 | |
| 2322 | /* |
| 2323 | * after a readpage IO is done, we need to: |
| 2324 | * clear the uptodate bits on error |
| 2325 | * set the uptodate bits if things worked |
| 2326 | * set the page up to date if all extents in the tree are uptodate |
| 2327 | * clear the lock bit in the extent tree |
| 2328 | * unlock the page if there are no other extents locked for it |
| 2329 | * |
| 2330 | * Scheduling is not allowed, so the extent state tree is expected |
| 2331 | * to have one and only one object corresponding to this IO. |
| 2332 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2333 | static void end_bio_extent_readpage(struct bio *bio, int err) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2334 | { |
| 2335 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | 4125bf7 | 2010-02-03 18:18:45 +0000 | [diff] [blame] | 2336 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 2337 | struct bio_vec *bvec = bio->bi_io_vec; |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2338 | struct extent_io_tree *tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2339 | u64 start; |
| 2340 | u64 end; |
| 2341 | int whole_page; |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2342 | int mirror; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2343 | int ret; |
| 2344 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2345 | if (err) |
| 2346 | uptodate = 0; |
| 2347 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2348 | do { |
| 2349 | struct page *page = bvec->bv_page; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2350 | struct extent_state *cached = NULL; |
| 2351 | struct extent_state *state; |
| 2352 | |
Kent Overstreet | be3940c | 2012-09-11 14:23:05 -0600 | [diff] [blame] | 2353 | pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, " |
| 2354 | "mirror=%ld\n", (u64)bio->bi_sector, err, |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2355 | (long int)bio->bi_bdev); |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2356 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 2357 | |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2358 | start = page_offset(page) + bvec->bv_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2359 | end = start + bvec->bv_len - 1; |
| 2360 | |
| 2361 | if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE) |
| 2362 | whole_page = 1; |
| 2363 | else |
| 2364 | whole_page = 0; |
| 2365 | |
Chris Mason | 4125bf7 | 2010-02-03 18:18:45 +0000 | [diff] [blame] | 2366 | if (++bvec <= bvec_end) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2367 | prefetchw(&bvec->bv_page->flags); |
| 2368 | |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2369 | spin_lock(&tree->lock); |
Chris Mason | 0d39920 | 2011-04-16 06:55:39 -0400 | [diff] [blame] | 2370 | state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED); |
Chris Mason | 109b36a | 2011-04-12 13:57:39 -0400 | [diff] [blame] | 2371 | if (state && state->start == start) { |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2372 | /* |
| 2373 | * take a reference on the state, unlock will drop |
| 2374 | * the ref |
| 2375 | */ |
| 2376 | cache_state(state, &cached); |
| 2377 | } |
| 2378 | spin_unlock(&tree->lock); |
| 2379 | |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2380 | mirror = (int)(unsigned long)bio->bi_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2381 | if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2382 | ret = tree->ops->readpage_end_io_hook(page, start, end, |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2383 | state, mirror); |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2384 | if (ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2385 | uptodate = 0; |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2386 | else |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2387 | clean_io_failure(start, page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2388 | } |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2389 | |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2390 | if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) { |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2391 | ret = tree->ops->readpage_io_failed_hook(page, mirror); |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2392 | if (!ret && !err && |
| 2393 | test_bit(BIO_UPTODATE, &bio->bi_flags)) |
| 2394 | uptodate = 1; |
| 2395 | } else if (!uptodate) { |
Jan Schmidt | f4a8e65 | 2011-12-01 09:30:36 -0500 | [diff] [blame] | 2396 | /* |
| 2397 | * The generic bio_readpage_error handles errors the |
| 2398 | * following way: If possible, new read requests are |
| 2399 | * created and submitted and will end up in |
| 2400 | * end_bio_extent_readpage as well (if we're lucky, not |
| 2401 | * in the !uptodate case). In that case it returns 0 and |
| 2402 | * we just go on with the next page in our bio. If it |
| 2403 | * can't handle the error it will return -EIO and we |
| 2404 | * remain responsible for that page. |
| 2405 | */ |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2406 | ret = bio_readpage_error(bio, page, start, end, mirror, NULL); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2407 | if (ret == 0) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2408 | uptodate = |
| 2409 | test_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2410 | if (err) |
| 2411 | uptodate = 0; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2412 | uncache_state(&cached); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2413 | continue; |
| 2414 | } |
| 2415 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2416 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 2417 | if (uptodate && tree->track_uptodate) { |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2418 | set_extent_uptodate(tree, start, end, &cached, |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2419 | GFP_ATOMIC); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2420 | } |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2421 | unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2422 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2423 | if (whole_page) { |
| 2424 | if (uptodate) { |
| 2425 | SetPageUptodate(page); |
| 2426 | } else { |
| 2427 | ClearPageUptodate(page); |
| 2428 | SetPageError(page); |
| 2429 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2430 | unlock_page(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2431 | } else { |
| 2432 | if (uptodate) { |
| 2433 | check_page_uptodate(tree, page); |
| 2434 | } else { |
| 2435 | ClearPageUptodate(page); |
| 2436 | SetPageError(page); |
| 2437 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2438 | check_page_locked(tree, page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2439 | } |
Chris Mason | 4125bf7 | 2010-02-03 18:18:45 +0000 | [diff] [blame] | 2440 | } while (bvec <= bvec_end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2441 | |
| 2442 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2443 | } |
| 2444 | |
Miao Xie | 88f794e | 2010-11-22 03:02:55 +0000 | [diff] [blame] | 2445 | struct bio * |
| 2446 | btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs, |
| 2447 | gfp_t gfp_flags) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2448 | { |
| 2449 | struct bio *bio; |
| 2450 | |
| 2451 | bio = bio_alloc(gfp_flags, nr_vecs); |
| 2452 | |
| 2453 | if (bio == NULL && (current->flags & PF_MEMALLOC)) { |
| 2454 | while (!bio && (nr_vecs /= 2)) |
| 2455 | bio = bio_alloc(gfp_flags, nr_vecs); |
| 2456 | } |
| 2457 | |
| 2458 | if (bio) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 2459 | bio->bi_size = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2460 | bio->bi_bdev = bdev; |
| 2461 | bio->bi_sector = first_sector; |
| 2462 | } |
| 2463 | return bio; |
| 2464 | } |
| 2465 | |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 2466 | static int __must_check submit_one_bio(int rw, struct bio *bio, |
| 2467 | int mirror_num, unsigned long bio_flags) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2468 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2469 | int ret = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2470 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 2471 | struct page *page = bvec->bv_page; |
| 2472 | struct extent_io_tree *tree = bio->bi_private; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2473 | u64 start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2474 | |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2475 | start = page_offset(page) + bvec->bv_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2476 | |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2477 | bio->bi_private = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2478 | |
| 2479 | bio_get(bio); |
| 2480 | |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2481 | if (tree->ops && tree->ops->submit_bio_hook) |
liubo | 6b82ce8 | 2011-01-26 06:21:39 +0000 | [diff] [blame] | 2482 | ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 2483 | mirror_num, bio_flags, start); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2484 | else |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 2485 | btrfsic_submit_bio(rw, bio); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2486 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2487 | if (bio_flagged(bio, BIO_EOPNOTSUPP)) |
| 2488 | ret = -EOPNOTSUPP; |
| 2489 | bio_put(bio); |
| 2490 | return ret; |
| 2491 | } |
| 2492 | |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 2493 | 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] | 2494 | unsigned long offset, size_t size, struct bio *bio, |
| 2495 | unsigned long bio_flags) |
| 2496 | { |
| 2497 | int ret = 0; |
| 2498 | if (tree->ops && tree->ops->merge_bio_hook) |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 2499 | ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio, |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 2500 | bio_flags); |
| 2501 | BUG_ON(ret < 0); |
| 2502 | return ret; |
| 2503 | |
| 2504 | } |
| 2505 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2506 | static int submit_extent_page(int rw, struct extent_io_tree *tree, |
| 2507 | struct page *page, sector_t sector, |
| 2508 | size_t size, unsigned long offset, |
| 2509 | struct block_device *bdev, |
| 2510 | struct bio **bio_ret, |
| 2511 | unsigned long max_pages, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2512 | bio_end_io_t end_io_func, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2513 | int mirror_num, |
| 2514 | unsigned long prev_bio_flags, |
| 2515 | unsigned long bio_flags) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2516 | { |
| 2517 | int ret = 0; |
| 2518 | struct bio *bio; |
| 2519 | int nr; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2520 | int contig = 0; |
| 2521 | int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED; |
| 2522 | int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 2523 | size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2524 | |
| 2525 | if (bio_ret && *bio_ret) { |
| 2526 | bio = *bio_ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2527 | if (old_compressed) |
| 2528 | contig = bio->bi_sector == sector; |
| 2529 | else |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame^] | 2530 | contig = bio_end_sector(bio) == sector; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2531 | |
| 2532 | if (prev_bio_flags != bio_flags || !contig || |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 2533 | merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2534 | bio_add_page(bio, page, page_size, offset) < page_size) { |
| 2535 | ret = submit_one_bio(rw, bio, mirror_num, |
| 2536 | prev_bio_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2537 | if (ret < 0) |
| 2538 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2539 | bio = NULL; |
| 2540 | } else { |
| 2541 | return 0; |
| 2542 | } |
| 2543 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2544 | if (this_compressed) |
| 2545 | nr = BIO_MAX_PAGES; |
| 2546 | else |
| 2547 | nr = bio_get_nr_vecs(bdev); |
| 2548 | |
Miao Xie | 88f794e | 2010-11-22 03:02:55 +0000 | [diff] [blame] | 2549 | bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH); |
Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 2550 | if (!bio) |
| 2551 | return -ENOMEM; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2552 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2553 | bio_add_page(bio, page, page_size, offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2554 | bio->bi_end_io = end_io_func; |
| 2555 | bio->bi_private = tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2556 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2557 | if (bio_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2558 | *bio_ret = bio; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2559 | else |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2560 | ret = submit_one_bio(rw, bio, mirror_num, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2561 | |
| 2562 | return ret; |
| 2563 | } |
| 2564 | |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 2565 | void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page) |
| 2566 | { |
| 2567 | if (!PagePrivate(page)) { |
| 2568 | SetPagePrivate(page); |
| 2569 | page_cache_get(page); |
| 2570 | set_page_private(page, (unsigned long)eb); |
| 2571 | } else { |
| 2572 | WARN_ON(page->private != (unsigned long)eb); |
| 2573 | } |
| 2574 | } |
| 2575 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2576 | void set_page_extent_mapped(struct page *page) |
| 2577 | { |
| 2578 | if (!PagePrivate(page)) { |
| 2579 | SetPagePrivate(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2580 | page_cache_get(page); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2581 | set_page_private(page, EXTENT_PAGE_PRIVATE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2582 | } |
| 2583 | } |
| 2584 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2585 | /* |
| 2586 | * basic readpage implementation. Locked extent state structs are inserted |
| 2587 | * into the tree that are removed when the IO is done (by the end_io |
| 2588 | * handlers) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2589 | * XXX JDM: This needs looking at to ensure proper page locking |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2590 | */ |
| 2591 | static int __extent_read_full_page(struct extent_io_tree *tree, |
| 2592 | struct page *page, |
| 2593 | get_extent_t *get_extent, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2594 | struct bio **bio, int mirror_num, |
| 2595 | unsigned long *bio_flags) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2596 | { |
| 2597 | struct inode *inode = page->mapping->host; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2598 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2599 | u64 page_end = start + PAGE_CACHE_SIZE - 1; |
| 2600 | u64 end; |
| 2601 | u64 cur = start; |
| 2602 | u64 extent_offset; |
| 2603 | u64 last_byte = i_size_read(inode); |
| 2604 | u64 block_start; |
| 2605 | u64 cur_end; |
| 2606 | sector_t sector; |
| 2607 | struct extent_map *em; |
| 2608 | struct block_device *bdev; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 2609 | struct btrfs_ordered_extent *ordered; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2610 | int ret; |
| 2611 | int nr = 0; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2612 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2613 | size_t iosize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2614 | size_t disk_io_size; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2615 | size_t blocksize = inode->i_sb->s_blocksize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2616 | unsigned long this_bio_flag = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2617 | |
| 2618 | set_page_extent_mapped(page); |
| 2619 | |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 2620 | if (!PageUptodate(page)) { |
| 2621 | if (cleancache_get_page(page) == 0) { |
| 2622 | BUG_ON(blocksize != PAGE_SIZE); |
| 2623 | goto out; |
| 2624 | } |
| 2625 | } |
| 2626 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2627 | end = page_end; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 2628 | while (1) { |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2629 | lock_extent(tree, start, end); |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 2630 | ordered = btrfs_lookup_ordered_extent(inode, start); |
| 2631 | if (!ordered) |
| 2632 | break; |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2633 | unlock_extent(tree, start, end); |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 2634 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 2635 | btrfs_put_ordered_extent(ordered); |
| 2636 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2637 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2638 | if (page->index == last_byte >> PAGE_CACHE_SHIFT) { |
| 2639 | char *userpage; |
| 2640 | size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1); |
| 2641 | |
| 2642 | if (zero_offset) { |
| 2643 | iosize = PAGE_CACHE_SIZE - zero_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2644 | userpage = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2645 | memset(userpage + zero_offset, 0, iosize); |
| 2646 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2647 | kunmap_atomic(userpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2648 | } |
| 2649 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2650 | while (cur <= end) { |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 2651 | unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1; |
| 2652 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2653 | if (cur >= last_byte) { |
| 2654 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2655 | struct extent_state *cached = NULL; |
| 2656 | |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2657 | iosize = PAGE_CACHE_SIZE - pg_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2658 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2659 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2660 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2661 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2662 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2663 | &cached, GFP_NOFS); |
| 2664 | unlock_extent_cached(tree, cur, cur + iosize - 1, |
| 2665 | &cached, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2666 | break; |
| 2667 | } |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2668 | em = get_extent(inode, page, pg_offset, cur, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2669 | end - cur + 1, 0); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 2670 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2671 | SetPageError(page); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2672 | unlock_extent(tree, cur, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2673 | break; |
| 2674 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2675 | extent_offset = cur - em->start; |
| 2676 | BUG_ON(extent_map_end(em) <= cur); |
| 2677 | BUG_ON(end < cur); |
| 2678 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2679 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2680 | this_bio_flag = EXTENT_BIO_COMPRESSED; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2681 | extent_set_compress_type(&this_bio_flag, |
| 2682 | em->compress_type); |
| 2683 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2684 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2685 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
| 2686 | cur_end = min(extent_map_end(em) - 1, end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 2687 | iosize = ALIGN(iosize, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2688 | if (this_bio_flag & EXTENT_BIO_COMPRESSED) { |
| 2689 | disk_io_size = em->block_len; |
| 2690 | sector = em->block_start >> 9; |
| 2691 | } else { |
| 2692 | sector = (em->block_start + extent_offset) >> 9; |
| 2693 | disk_io_size = iosize; |
| 2694 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2695 | bdev = em->bdev; |
| 2696 | block_start = em->block_start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2697 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 2698 | block_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2699 | free_extent_map(em); |
| 2700 | em = NULL; |
| 2701 | |
| 2702 | /* we've found a hole, just zero and go on */ |
| 2703 | if (block_start == EXTENT_MAP_HOLE) { |
| 2704 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2705 | struct extent_state *cached = NULL; |
| 2706 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2707 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2708 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2709 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2710 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2711 | |
| 2712 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2713 | &cached, GFP_NOFS); |
| 2714 | unlock_extent_cached(tree, cur, cur + iosize - 1, |
| 2715 | &cached, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2716 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2717 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2718 | continue; |
| 2719 | } |
| 2720 | /* the get_extent function already copied into the page */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2721 | if (test_range_bit(tree, cur, cur_end, |
| 2722 | EXTENT_UPTODATE, 1, NULL)) { |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 2723 | check_page_uptodate(tree, page); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2724 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2725 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2726 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2727 | continue; |
| 2728 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2729 | /* we have an inline extent but it didn't get marked up |
| 2730 | * to date. Error out |
| 2731 | */ |
| 2732 | if (block_start == EXTENT_MAP_INLINE) { |
| 2733 | SetPageError(page); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2734 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2735 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2736 | pg_offset += iosize; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2737 | continue; |
| 2738 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2739 | |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 2740 | pnr -= page->index; |
| 2741 | ret = submit_extent_page(READ, tree, page, |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2742 | sector, disk_io_size, pg_offset, |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 2743 | bdev, bio, pnr, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2744 | end_bio_extent_readpage, mirror_num, |
| 2745 | *bio_flags, |
| 2746 | this_bio_flag); |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 2747 | if (!ret) { |
| 2748 | nr++; |
| 2749 | *bio_flags = this_bio_flag; |
| 2750 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2751 | SetPageError(page); |
Josef Bacik | edd33c9 | 2012-10-05 16:40:32 -0400 | [diff] [blame] | 2752 | unlock_extent(tree, cur, cur + iosize - 1); |
| 2753 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2754 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2755 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2756 | } |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 2757 | out: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2758 | if (!nr) { |
| 2759 | if (!PageError(page)) |
| 2760 | SetPageUptodate(page); |
| 2761 | unlock_page(page); |
| 2762 | } |
| 2763 | return 0; |
| 2764 | } |
| 2765 | |
| 2766 | 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] | 2767 | get_extent_t *get_extent, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2768 | { |
| 2769 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2770 | unsigned long bio_flags = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2771 | int ret; |
| 2772 | |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 2773 | ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2774 | &bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2775 | if (bio) |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 2776 | ret = submit_one_bio(READ, bio, mirror_num, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2777 | return ret; |
| 2778 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2779 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 2780 | static noinline void update_nr_written(struct page *page, |
| 2781 | struct writeback_control *wbc, |
| 2782 | unsigned long nr_written) |
| 2783 | { |
| 2784 | wbc->nr_to_write -= nr_written; |
| 2785 | if (wbc->range_cyclic || (wbc->nr_to_write > 0 && |
| 2786 | wbc->range_start == 0 && wbc->range_end == LLONG_MAX)) |
| 2787 | page->mapping->writeback_index = page->index + nr_written; |
| 2788 | } |
| 2789 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2790 | /* |
| 2791 | * the writepage semantics are similar to regular writepage. extent |
| 2792 | * records are inserted to lock ranges in the tree, and as dirty areas |
| 2793 | * are found, they are marked writeback. Then the lock bits are removed |
| 2794 | * and the end_io handler clears the writeback ranges |
| 2795 | */ |
| 2796 | static int __extent_writepage(struct page *page, struct writeback_control *wbc, |
| 2797 | void *data) |
| 2798 | { |
| 2799 | struct inode *inode = page->mapping->host; |
| 2800 | struct extent_page_data *epd = data; |
| 2801 | struct extent_io_tree *tree = epd->tree; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2802 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2803 | u64 delalloc_start; |
| 2804 | u64 page_end = start + PAGE_CACHE_SIZE - 1; |
| 2805 | u64 end; |
| 2806 | u64 cur = start; |
| 2807 | u64 extent_offset; |
| 2808 | u64 last_byte = i_size_read(inode); |
| 2809 | u64 block_start; |
| 2810 | u64 iosize; |
| 2811 | sector_t sector; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 2812 | struct extent_state *cached_state = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2813 | struct extent_map *em; |
| 2814 | struct block_device *bdev; |
| 2815 | int ret; |
| 2816 | int nr = 0; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2817 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2818 | size_t blocksize; |
| 2819 | loff_t i_size = i_size_read(inode); |
| 2820 | unsigned long end_index = i_size >> PAGE_CACHE_SHIFT; |
| 2821 | u64 nr_delalloc; |
| 2822 | u64 delalloc_end; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2823 | int page_started; |
| 2824 | int compressed; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 2825 | int write_flags; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2826 | unsigned long nr_written = 0; |
Josef Bacik | 9e48710 | 2011-08-01 12:08:18 -0400 | [diff] [blame] | 2827 | bool fill_delalloc = true; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2828 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 2829 | if (wbc->sync_mode == WB_SYNC_ALL) |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 2830 | write_flags = WRITE_SYNC; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 2831 | else |
| 2832 | write_flags = WRITE; |
| 2833 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2834 | trace___extent_writepage(page, inode, wbc); |
| 2835 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2836 | WARN_ON(!PageLocked(page)); |
Chris Mason | bf0da8c | 2011-11-04 12:29:37 -0400 | [diff] [blame] | 2837 | |
| 2838 | ClearPageError(page); |
| 2839 | |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2840 | pg_offset = i_size & (PAGE_CACHE_SIZE - 1); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 2841 | if (page->index > end_index || |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2842 | (page->index == end_index && !pg_offset)) { |
Chris Mason | 39be25c | 2008-11-10 11:50:50 -0500 | [diff] [blame] | 2843 | page->mapping->a_ops->invalidatepage(page, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2844 | unlock_page(page); |
| 2845 | return 0; |
| 2846 | } |
| 2847 | |
| 2848 | if (page->index == end_index) { |
| 2849 | char *userpage; |
| 2850 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2851 | userpage = kmap_atomic(page); |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2852 | memset(userpage + pg_offset, 0, |
| 2853 | PAGE_CACHE_SIZE - pg_offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2854 | kunmap_atomic(userpage); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 2855 | flush_dcache_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2856 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2857 | pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2858 | |
| 2859 | set_page_extent_mapped(page); |
| 2860 | |
Josef Bacik | 9e48710 | 2011-08-01 12:08:18 -0400 | [diff] [blame] | 2861 | if (!tree->ops || !tree->ops->fill_delalloc) |
| 2862 | fill_delalloc = false; |
| 2863 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2864 | delalloc_start = start; |
| 2865 | delalloc_end = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2866 | page_started = 0; |
Josef Bacik | 9e48710 | 2011-08-01 12:08:18 -0400 | [diff] [blame] | 2867 | if (!epd->extent_locked && fill_delalloc) { |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 2868 | u64 delalloc_to_write = 0; |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 2869 | /* |
| 2870 | * make sure the wbc mapping index is at least updated |
| 2871 | * to this page. |
| 2872 | */ |
| 2873 | update_nr_written(page, wbc, 0); |
| 2874 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2875 | while (delalloc_end < page_end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2876 | nr_delalloc = find_lock_delalloc_range(inode, tree, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2877 | page, |
| 2878 | &delalloc_start, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2879 | &delalloc_end, |
| 2880 | 128 * 1024 * 1024); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2881 | if (nr_delalloc == 0) { |
| 2882 | delalloc_start = delalloc_end + 1; |
| 2883 | continue; |
| 2884 | } |
Tsutomu Itoh | 013bd4c | 2012-02-16 10:11:40 +0900 | [diff] [blame] | 2885 | ret = tree->ops->fill_delalloc(inode, page, |
| 2886 | delalloc_start, |
| 2887 | delalloc_end, |
| 2888 | &page_started, |
| 2889 | &nr_written); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2890 | /* File system has been set read-only */ |
| 2891 | if (ret) { |
| 2892 | SetPageError(page); |
| 2893 | goto done; |
| 2894 | } |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 2895 | /* |
| 2896 | * delalloc_end is already one less than the total |
| 2897 | * length, so we don't subtract one from |
| 2898 | * PAGE_CACHE_SIZE |
| 2899 | */ |
| 2900 | delalloc_to_write += (delalloc_end - delalloc_start + |
| 2901 | PAGE_CACHE_SIZE) >> |
| 2902 | PAGE_CACHE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2903 | delalloc_start = delalloc_end + 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2904 | } |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 2905 | if (wbc->nr_to_write < delalloc_to_write) { |
| 2906 | int thresh = 8192; |
| 2907 | |
| 2908 | if (delalloc_to_write < thresh * 2) |
| 2909 | thresh = delalloc_to_write; |
| 2910 | wbc->nr_to_write = min_t(u64, delalloc_to_write, |
| 2911 | thresh); |
| 2912 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2913 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2914 | /* did the fill delalloc function already unlock and start |
| 2915 | * the IO? |
| 2916 | */ |
| 2917 | if (page_started) { |
| 2918 | ret = 0; |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 2919 | /* |
| 2920 | * we've unlocked the page, so we can't update |
| 2921 | * the mapping's writeback index, just update |
| 2922 | * nr_to_write. |
| 2923 | */ |
| 2924 | wbc->nr_to_write -= nr_written; |
| 2925 | goto done_unlocked; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2926 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2927 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2928 | if (tree->ops && tree->ops->writepage_start_hook) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2929 | ret = tree->ops->writepage_start_hook(page, start, |
| 2930 | page_end); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2931 | if (ret) { |
| 2932 | /* Fixup worker will requeue */ |
| 2933 | if (ret == -EBUSY) |
| 2934 | wbc->pages_skipped++; |
| 2935 | else |
| 2936 | redirty_page_for_writepage(wbc, page); |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 2937 | update_nr_written(page, wbc, nr_written); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2938 | unlock_page(page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2939 | ret = 0; |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 2940 | goto done_unlocked; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2941 | } |
| 2942 | } |
| 2943 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 2944 | /* |
| 2945 | * we don't want to touch the inode after unlocking the page, |
| 2946 | * so we update the mapping writeback index now |
| 2947 | */ |
| 2948 | update_nr_written(page, wbc, nr_written + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2949 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2950 | end = page_end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2951 | if (last_byte <= start) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2952 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 2953 | tree->ops->writepage_end_io_hook(page, start, |
| 2954 | page_end, NULL, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2955 | goto done; |
| 2956 | } |
| 2957 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2958 | blocksize = inode->i_sb->s_blocksize; |
| 2959 | |
| 2960 | while (cur <= end) { |
| 2961 | if (cur >= last_byte) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2962 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 2963 | tree->ops->writepage_end_io_hook(page, cur, |
| 2964 | page_end, NULL, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2965 | break; |
| 2966 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 2967 | em = epd->get_extent(inode, page, pg_offset, cur, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2968 | end - cur + 1, 1); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 2969 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2970 | SetPageError(page); |
| 2971 | break; |
| 2972 | } |
| 2973 | |
| 2974 | extent_offset = cur - em->start; |
| 2975 | BUG_ON(extent_map_end(em) <= cur); |
| 2976 | BUG_ON(end < cur); |
| 2977 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 2978 | iosize = ALIGN(iosize, blocksize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2979 | sector = (em->block_start + extent_offset) >> 9; |
| 2980 | bdev = em->bdev; |
| 2981 | block_start = em->block_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2982 | compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2983 | free_extent_map(em); |
| 2984 | em = NULL; |
| 2985 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2986 | /* |
| 2987 | * compressed and inline extents are written through other |
| 2988 | * paths in the FS |
| 2989 | */ |
| 2990 | if (compressed || block_start == EXTENT_MAP_HOLE || |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2991 | block_start == EXTENT_MAP_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2992 | /* |
| 2993 | * end_io notification does not happen here for |
| 2994 | * compressed extents |
| 2995 | */ |
| 2996 | if (!compressed && tree->ops && |
| 2997 | tree->ops->writepage_end_io_hook) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2998 | tree->ops->writepage_end_io_hook(page, cur, |
| 2999 | cur + iosize - 1, |
| 3000 | NULL, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3001 | else if (compressed) { |
| 3002 | /* we don't want to end_page_writeback on |
| 3003 | * a compressed extent. this happens |
| 3004 | * elsewhere |
| 3005 | */ |
| 3006 | nr++; |
| 3007 | } |
| 3008 | |
| 3009 | cur += iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3010 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3011 | continue; |
| 3012 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3013 | /* leave this out until we have a page_mkwrite call */ |
| 3014 | if (0 && !test_range_bit(tree, cur, cur + iosize - 1, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3015 | EXTENT_DIRTY, 0, NULL)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3016 | cur = cur + iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3017 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3018 | continue; |
| 3019 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3020 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3021 | if (tree->ops && tree->ops->writepage_io_hook) { |
| 3022 | ret = tree->ops->writepage_io_hook(page, cur, |
| 3023 | cur + iosize - 1); |
| 3024 | } else { |
| 3025 | ret = 0; |
| 3026 | } |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 3027 | if (ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3028 | SetPageError(page); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 3029 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3030 | unsigned long max_nr = end_index + 1; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3031 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3032 | set_range_writeback(tree, cur, cur + iosize - 1); |
| 3033 | if (!PageWriteback(page)) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3034 | printk(KERN_ERR "btrfs warning page %lu not " |
| 3035 | "writeback, cur %llu end %llu\n", |
| 3036 | page->index, (unsigned long long)cur, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3037 | (unsigned long long)end); |
| 3038 | } |
| 3039 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3040 | ret = submit_extent_page(write_flags, tree, page, |
| 3041 | sector, iosize, pg_offset, |
| 3042 | bdev, &epd->bio, max_nr, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3043 | end_bio_extent_writepage, |
| 3044 | 0, 0, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3045 | if (ret) |
| 3046 | SetPageError(page); |
| 3047 | } |
| 3048 | cur = cur + iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3049 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3050 | nr++; |
| 3051 | } |
| 3052 | done: |
| 3053 | if (nr == 0) { |
| 3054 | /* make sure the mapping tag for page dirty gets cleared */ |
| 3055 | set_page_writeback(page); |
| 3056 | end_page_writeback(page); |
| 3057 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3058 | unlock_page(page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3059 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3060 | done_unlocked: |
| 3061 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 3062 | /* drop our reference on any cached states */ |
| 3063 | free_extent_state(cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3064 | return 0; |
| 3065 | } |
| 3066 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3067 | static int eb_wait(void *word) |
| 3068 | { |
| 3069 | io_schedule(); |
| 3070 | return 0; |
| 3071 | } |
| 3072 | |
| 3073 | static void wait_on_extent_buffer_writeback(struct extent_buffer *eb) |
| 3074 | { |
| 3075 | wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait, |
| 3076 | TASK_UNINTERRUPTIBLE); |
| 3077 | } |
| 3078 | |
| 3079 | static int lock_extent_buffer_for_io(struct extent_buffer *eb, |
| 3080 | struct btrfs_fs_info *fs_info, |
| 3081 | struct extent_page_data *epd) |
| 3082 | { |
| 3083 | unsigned long i, num_pages; |
| 3084 | int flush = 0; |
| 3085 | int ret = 0; |
| 3086 | |
| 3087 | if (!btrfs_try_tree_write_lock(eb)) { |
| 3088 | flush = 1; |
| 3089 | flush_write_bio(epd); |
| 3090 | btrfs_tree_lock(eb); |
| 3091 | } |
| 3092 | |
| 3093 | if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) { |
| 3094 | btrfs_tree_unlock(eb); |
| 3095 | if (!epd->sync_io) |
| 3096 | return 0; |
| 3097 | if (!flush) { |
| 3098 | flush_write_bio(epd); |
| 3099 | flush = 1; |
| 3100 | } |
Chris Mason | a098d8e | 2012-03-21 12:09:56 -0400 | [diff] [blame] | 3101 | while (1) { |
| 3102 | wait_on_extent_buffer_writeback(eb); |
| 3103 | btrfs_tree_lock(eb); |
| 3104 | if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) |
| 3105 | break; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3106 | btrfs_tree_unlock(eb); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3107 | } |
| 3108 | } |
| 3109 | |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3110 | /* |
| 3111 | * We need to do this to prevent races in people who check if the eb is |
| 3112 | * under IO since we can end up having no IO bits set for a short period |
| 3113 | * of time. |
| 3114 | */ |
| 3115 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3116 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) { |
| 3117 | set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3118 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3119 | btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); |
Miao Xie | e2d8452 | 2013-01-29 10:09:20 +0000 | [diff] [blame] | 3120 | __percpu_counter_add(&fs_info->dirty_metadata_bytes, |
| 3121 | -eb->len, |
| 3122 | fs_info->dirty_metadata_batch); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3123 | ret = 1; |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3124 | } else { |
| 3125 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | btrfs_tree_unlock(eb); |
| 3129 | |
| 3130 | if (!ret) |
| 3131 | return ret; |
| 3132 | |
| 3133 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3134 | for (i = 0; i < num_pages; i++) { |
| 3135 | struct page *p = extent_buffer_page(eb, i); |
| 3136 | |
| 3137 | if (!trylock_page(p)) { |
| 3138 | if (!flush) { |
| 3139 | flush_write_bio(epd); |
| 3140 | flush = 1; |
| 3141 | } |
| 3142 | lock_page(p); |
| 3143 | } |
| 3144 | } |
| 3145 | |
| 3146 | return ret; |
| 3147 | } |
| 3148 | |
| 3149 | static void end_extent_buffer_writeback(struct extent_buffer *eb) |
| 3150 | { |
| 3151 | clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
| 3152 | smp_mb__after_clear_bit(); |
| 3153 | wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK); |
| 3154 | } |
| 3155 | |
| 3156 | static void end_bio_extent_buffer_writepage(struct bio *bio, int err) |
| 3157 | { |
| 3158 | int uptodate = err == 0; |
| 3159 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 3160 | struct extent_buffer *eb; |
| 3161 | int done; |
| 3162 | |
| 3163 | do { |
| 3164 | struct page *page = bvec->bv_page; |
| 3165 | |
| 3166 | bvec--; |
| 3167 | eb = (struct extent_buffer *)page->private; |
| 3168 | BUG_ON(!eb); |
| 3169 | done = atomic_dec_and_test(&eb->io_pages); |
| 3170 | |
| 3171 | if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) { |
| 3172 | set_bit(EXTENT_BUFFER_IOERR, &eb->bflags); |
| 3173 | ClearPageUptodate(page); |
| 3174 | SetPageError(page); |
| 3175 | } |
| 3176 | |
| 3177 | end_page_writeback(page); |
| 3178 | |
| 3179 | if (!done) |
| 3180 | continue; |
| 3181 | |
| 3182 | end_extent_buffer_writeback(eb); |
| 3183 | } while (bvec >= bio->bi_io_vec); |
| 3184 | |
| 3185 | bio_put(bio); |
| 3186 | |
| 3187 | } |
| 3188 | |
| 3189 | static int write_one_eb(struct extent_buffer *eb, |
| 3190 | struct btrfs_fs_info *fs_info, |
| 3191 | struct writeback_control *wbc, |
| 3192 | struct extent_page_data *epd) |
| 3193 | { |
| 3194 | struct block_device *bdev = fs_info->fs_devices->latest_bdev; |
| 3195 | u64 offset = eb->start; |
| 3196 | unsigned long i, num_pages; |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3197 | unsigned long bio_flags = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3198 | int rw = (epd->sync_io ? WRITE_SYNC : WRITE); |
Josef Bacik | d7dbe9e | 2012-04-23 14:00:51 -0400 | [diff] [blame] | 3199 | int ret = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3200 | |
| 3201 | clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags); |
| 3202 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3203 | atomic_set(&eb->io_pages, num_pages); |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3204 | if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID) |
| 3205 | bio_flags = EXTENT_BIO_TREE_LOG; |
| 3206 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3207 | for (i = 0; i < num_pages; i++) { |
| 3208 | struct page *p = extent_buffer_page(eb, i); |
| 3209 | |
| 3210 | clear_page_dirty_for_io(p); |
| 3211 | set_page_writeback(p); |
| 3212 | ret = submit_extent_page(rw, eb->tree, p, offset >> 9, |
| 3213 | PAGE_CACHE_SIZE, 0, bdev, &epd->bio, |
| 3214 | -1, end_bio_extent_buffer_writepage, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3215 | 0, epd->bio_flags, bio_flags); |
| 3216 | epd->bio_flags = bio_flags; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3217 | if (ret) { |
| 3218 | set_bit(EXTENT_BUFFER_IOERR, &eb->bflags); |
| 3219 | SetPageError(p); |
| 3220 | if (atomic_sub_and_test(num_pages - i, &eb->io_pages)) |
| 3221 | end_extent_buffer_writeback(eb); |
| 3222 | ret = -EIO; |
| 3223 | break; |
| 3224 | } |
| 3225 | offset += PAGE_CACHE_SIZE; |
| 3226 | update_nr_written(p, wbc, 1); |
| 3227 | unlock_page(p); |
| 3228 | } |
| 3229 | |
| 3230 | if (unlikely(ret)) { |
| 3231 | for (; i < num_pages; i++) { |
| 3232 | struct page *p = extent_buffer_page(eb, i); |
| 3233 | unlock_page(p); |
| 3234 | } |
| 3235 | } |
| 3236 | |
| 3237 | return ret; |
| 3238 | } |
| 3239 | |
| 3240 | int btree_write_cache_pages(struct address_space *mapping, |
| 3241 | struct writeback_control *wbc) |
| 3242 | { |
| 3243 | struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree; |
| 3244 | struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info; |
| 3245 | struct extent_buffer *eb, *prev_eb = NULL; |
| 3246 | struct extent_page_data epd = { |
| 3247 | .bio = NULL, |
| 3248 | .tree = tree, |
| 3249 | .extent_locked = 0, |
| 3250 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3251 | .bio_flags = 0, |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3252 | }; |
| 3253 | int ret = 0; |
| 3254 | int done = 0; |
| 3255 | int nr_to_write_done = 0; |
| 3256 | struct pagevec pvec; |
| 3257 | int nr_pages; |
| 3258 | pgoff_t index; |
| 3259 | pgoff_t end; /* Inclusive */ |
| 3260 | int scanned = 0; |
| 3261 | int tag; |
| 3262 | |
| 3263 | pagevec_init(&pvec, 0); |
| 3264 | if (wbc->range_cyclic) { |
| 3265 | index = mapping->writeback_index; /* Start from prev offset */ |
| 3266 | end = -1; |
| 3267 | } else { |
| 3268 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 3269 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
| 3270 | scanned = 1; |
| 3271 | } |
| 3272 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3273 | tag = PAGECACHE_TAG_TOWRITE; |
| 3274 | else |
| 3275 | tag = PAGECACHE_TAG_DIRTY; |
| 3276 | retry: |
| 3277 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3278 | tag_pages_for_writeback(mapping, index, end); |
| 3279 | while (!done && !nr_to_write_done && (index <= end) && |
| 3280 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
| 3281 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { |
| 3282 | unsigned i; |
| 3283 | |
| 3284 | scanned = 1; |
| 3285 | for (i = 0; i < nr_pages; i++) { |
| 3286 | struct page *page = pvec.pages[i]; |
| 3287 | |
| 3288 | if (!PagePrivate(page)) |
| 3289 | continue; |
| 3290 | |
| 3291 | if (!wbc->range_cyclic && page->index > end) { |
| 3292 | done = 1; |
| 3293 | break; |
| 3294 | } |
| 3295 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3296 | spin_lock(&mapping->private_lock); |
| 3297 | if (!PagePrivate(page)) { |
| 3298 | spin_unlock(&mapping->private_lock); |
| 3299 | continue; |
| 3300 | } |
| 3301 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3302 | eb = (struct extent_buffer *)page->private; |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3303 | |
| 3304 | /* |
| 3305 | * Shouldn't happen and normally this would be a BUG_ON |
| 3306 | * but no sense in crashing the users box for something |
| 3307 | * we can survive anyway. |
| 3308 | */ |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3309 | if (!eb) { |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3310 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3311 | WARN_ON(1); |
| 3312 | continue; |
| 3313 | } |
| 3314 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3315 | if (eb == prev_eb) { |
| 3316 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3317 | continue; |
| 3318 | } |
| 3319 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3320 | ret = atomic_inc_not_zero(&eb->refs); |
| 3321 | spin_unlock(&mapping->private_lock); |
| 3322 | if (!ret) |
| 3323 | continue; |
| 3324 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3325 | prev_eb = eb; |
| 3326 | ret = lock_extent_buffer_for_io(eb, fs_info, &epd); |
| 3327 | if (!ret) { |
| 3328 | free_extent_buffer(eb); |
| 3329 | continue; |
| 3330 | } |
| 3331 | |
| 3332 | ret = write_one_eb(eb, fs_info, wbc, &epd); |
| 3333 | if (ret) { |
| 3334 | done = 1; |
| 3335 | free_extent_buffer(eb); |
| 3336 | break; |
| 3337 | } |
| 3338 | free_extent_buffer(eb); |
| 3339 | |
| 3340 | /* |
| 3341 | * the filesystem may choose to bump up nr_to_write. |
| 3342 | * We have to make sure to honor the new nr_to_write |
| 3343 | * at any time |
| 3344 | */ |
| 3345 | nr_to_write_done = wbc->nr_to_write <= 0; |
| 3346 | } |
| 3347 | pagevec_release(&pvec); |
| 3348 | cond_resched(); |
| 3349 | } |
| 3350 | if (!scanned && !done) { |
| 3351 | /* |
| 3352 | * We hit the last page and there is more work to be done: wrap |
| 3353 | * back to the start of the file |
| 3354 | */ |
| 3355 | scanned = 1; |
| 3356 | index = 0; |
| 3357 | goto retry; |
| 3358 | } |
| 3359 | flush_write_bio(&epd); |
| 3360 | return ret; |
| 3361 | } |
| 3362 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3363 | /** |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3364 | * 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] | 3365 | * @mapping: address space structure to write |
| 3366 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
| 3367 | * @writepage: function called for each page |
| 3368 | * @data: data passed to writepage function |
| 3369 | * |
| 3370 | * If a page is already under I/O, write_cache_pages() skips it, even |
| 3371 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, |
| 3372 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() |
| 3373 | * and msync() need to guarantee that all the data which was dirty at the time |
| 3374 | * the call was made get new I/O started against them. If wbc->sync_mode is |
| 3375 | * WB_SYNC_ALL then we were called for data integrity and we must wait for |
| 3376 | * existing IO to complete. |
| 3377 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 3378 | static int extent_write_cache_pages(struct extent_io_tree *tree, |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3379 | struct address_space *mapping, |
| 3380 | struct writeback_control *wbc, |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3381 | writepage_t writepage, void *data, |
| 3382 | void (*flush_fn)(void *)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3383 | { |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 3384 | struct inode *inode = mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3385 | int ret = 0; |
| 3386 | int done = 0; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3387 | int nr_to_write_done = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3388 | struct pagevec pvec; |
| 3389 | int nr_pages; |
| 3390 | pgoff_t index; |
| 3391 | pgoff_t end; /* Inclusive */ |
| 3392 | int scanned = 0; |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3393 | int tag; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3394 | |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 3395 | /* |
| 3396 | * We have to hold onto the inode so that ordered extents can do their |
| 3397 | * work when the IO finishes. The alternative to this is failing to add |
| 3398 | * an ordered extent if the igrab() fails there and that is a huge pain |
| 3399 | * to deal with, so instead just hold onto the inode throughout the |
| 3400 | * writepages operation. If it fails here we are freeing up the inode |
| 3401 | * anyway and we'd rather not waste our time writing out stuff that is |
| 3402 | * going to be truncated anyway. |
| 3403 | */ |
| 3404 | if (!igrab(inode)) |
| 3405 | return 0; |
| 3406 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3407 | pagevec_init(&pvec, 0); |
| 3408 | if (wbc->range_cyclic) { |
| 3409 | index = mapping->writeback_index; /* Start from prev offset */ |
| 3410 | end = -1; |
| 3411 | } else { |
| 3412 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 3413 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3414 | scanned = 1; |
| 3415 | } |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3416 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3417 | tag = PAGECACHE_TAG_TOWRITE; |
| 3418 | else |
| 3419 | tag = PAGECACHE_TAG_DIRTY; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3420 | retry: |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3421 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3422 | tag_pages_for_writeback(mapping, index, end); |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3423 | while (!done && !nr_to_write_done && (index <= end) && |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3424 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
| 3425 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3426 | unsigned i; |
| 3427 | |
| 3428 | scanned = 1; |
| 3429 | for (i = 0; i < nr_pages; i++) { |
| 3430 | struct page *page = pvec.pages[i]; |
| 3431 | |
| 3432 | /* |
| 3433 | * At this point we hold neither mapping->tree_lock nor |
| 3434 | * lock on the page itself: the page may be truncated or |
| 3435 | * invalidated (changing page->mapping to NULL), or even |
| 3436 | * swizzled back from swapper_space to tmpfs file |
| 3437 | * mapping |
| 3438 | */ |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3439 | if (!trylock_page(page)) { |
| 3440 | flush_fn(data); |
| 3441 | lock_page(page); |
Chris Mason | 01d658f | 2011-11-01 10:08:06 -0400 | [diff] [blame] | 3442 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3443 | |
| 3444 | if (unlikely(page->mapping != mapping)) { |
| 3445 | unlock_page(page); |
| 3446 | continue; |
| 3447 | } |
| 3448 | |
| 3449 | if (!wbc->range_cyclic && page->index > end) { |
| 3450 | done = 1; |
| 3451 | unlock_page(page); |
| 3452 | continue; |
| 3453 | } |
| 3454 | |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3455 | if (wbc->sync_mode != WB_SYNC_NONE) { |
Chris Mason | 0e6bd95 | 2008-11-20 10:46:35 -0500 | [diff] [blame] | 3456 | if (PageWriteback(page)) |
| 3457 | flush_fn(data); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3458 | wait_on_page_writeback(page); |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3459 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3460 | |
| 3461 | if (PageWriteback(page) || |
| 3462 | !clear_page_dirty_for_io(page)) { |
| 3463 | unlock_page(page); |
| 3464 | continue; |
| 3465 | } |
| 3466 | |
| 3467 | ret = (*writepage)(page, wbc, data); |
| 3468 | |
| 3469 | if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) { |
| 3470 | unlock_page(page); |
| 3471 | ret = 0; |
| 3472 | } |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3473 | if (ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3474 | done = 1; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3475 | |
| 3476 | /* |
| 3477 | * the filesystem may choose to bump up nr_to_write. |
| 3478 | * We have to make sure to honor the new nr_to_write |
| 3479 | * at any time |
| 3480 | */ |
| 3481 | nr_to_write_done = wbc->nr_to_write <= 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3482 | } |
| 3483 | pagevec_release(&pvec); |
| 3484 | cond_resched(); |
| 3485 | } |
| 3486 | if (!scanned && !done) { |
| 3487 | /* |
| 3488 | * We hit the last page and there is more work to be done: wrap |
| 3489 | * back to the start of the file |
| 3490 | */ |
| 3491 | scanned = 1; |
| 3492 | index = 0; |
| 3493 | goto retry; |
| 3494 | } |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 3495 | btrfs_add_delayed_iput(inode); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3496 | return ret; |
| 3497 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3498 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3499 | static void flush_epd_write_bio(struct extent_page_data *epd) |
| 3500 | { |
| 3501 | if (epd->bio) { |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 3502 | int rw = WRITE; |
| 3503 | int ret; |
| 3504 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3505 | if (epd->sync_io) |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 3506 | rw = WRITE_SYNC; |
| 3507 | |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3508 | ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3509 | BUG_ON(ret < 0); /* -ENOMEM */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3510 | epd->bio = NULL; |
| 3511 | } |
| 3512 | } |
| 3513 | |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3514 | static noinline void flush_write_bio(void *data) |
| 3515 | { |
| 3516 | struct extent_page_data *epd = data; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3517 | flush_epd_write_bio(epd); |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3518 | } |
| 3519 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3520 | int extent_write_full_page(struct extent_io_tree *tree, struct page *page, |
| 3521 | get_extent_t *get_extent, |
| 3522 | struct writeback_control *wbc) |
| 3523 | { |
| 3524 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3525 | struct extent_page_data epd = { |
| 3526 | .bio = NULL, |
| 3527 | .tree = tree, |
| 3528 | .get_extent = get_extent, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3529 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3530 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3531 | .bio_flags = 0, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3532 | }; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3533 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3534 | ret = __extent_writepage(page, wbc, &epd); |
| 3535 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3536 | flush_epd_write_bio(&epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3537 | return ret; |
| 3538 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3539 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3540 | int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode, |
| 3541 | u64 start, u64 end, get_extent_t *get_extent, |
| 3542 | int mode) |
| 3543 | { |
| 3544 | int ret = 0; |
| 3545 | struct address_space *mapping = inode->i_mapping; |
| 3546 | struct page *page; |
| 3547 | unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >> |
| 3548 | PAGE_CACHE_SHIFT; |
| 3549 | |
| 3550 | struct extent_page_data epd = { |
| 3551 | .bio = NULL, |
| 3552 | .tree = tree, |
| 3553 | .get_extent = get_extent, |
| 3554 | .extent_locked = 1, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3555 | .sync_io = mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3556 | .bio_flags = 0, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3557 | }; |
| 3558 | struct writeback_control wbc_writepages = { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3559 | .sync_mode = mode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3560 | .nr_to_write = nr_pages * 2, |
| 3561 | .range_start = start, |
| 3562 | .range_end = end + 1, |
| 3563 | }; |
| 3564 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3565 | while (start <= end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3566 | page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT); |
| 3567 | if (clear_page_dirty_for_io(page)) |
| 3568 | ret = __extent_writepage(page, &wbc_writepages, &epd); |
| 3569 | else { |
| 3570 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 3571 | tree->ops->writepage_end_io_hook(page, start, |
| 3572 | start + PAGE_CACHE_SIZE - 1, |
| 3573 | NULL, 1); |
| 3574 | unlock_page(page); |
| 3575 | } |
| 3576 | page_cache_release(page); |
| 3577 | start += PAGE_CACHE_SIZE; |
| 3578 | } |
| 3579 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3580 | flush_epd_write_bio(&epd); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3581 | return ret; |
| 3582 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3583 | |
| 3584 | int extent_writepages(struct extent_io_tree *tree, |
| 3585 | struct address_space *mapping, |
| 3586 | get_extent_t *get_extent, |
| 3587 | struct writeback_control *wbc) |
| 3588 | { |
| 3589 | int ret = 0; |
| 3590 | struct extent_page_data epd = { |
| 3591 | .bio = NULL, |
| 3592 | .tree = tree, |
| 3593 | .get_extent = get_extent, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3594 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3595 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3596 | .bio_flags = 0, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3597 | }; |
| 3598 | |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3599 | ret = extent_write_cache_pages(tree, mapping, wbc, |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3600 | __extent_writepage, &epd, |
| 3601 | flush_write_bio); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3602 | flush_epd_write_bio(&epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3603 | return ret; |
| 3604 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3605 | |
| 3606 | int extent_readpages(struct extent_io_tree *tree, |
| 3607 | struct address_space *mapping, |
| 3608 | struct list_head *pages, unsigned nr_pages, |
| 3609 | get_extent_t get_extent) |
| 3610 | { |
| 3611 | struct bio *bio = NULL; |
| 3612 | unsigned page_idx; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3613 | unsigned long bio_flags = 0; |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 3614 | struct page *pagepool[16]; |
| 3615 | struct page *page; |
| 3616 | int i = 0; |
| 3617 | int nr = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3618 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3619 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 3620 | page = list_entry(pages->prev, struct page, lru); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3621 | |
| 3622 | prefetchw(&page->flags); |
| 3623 | list_del(&page->lru); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 3624 | if (add_to_page_cache_lru(page, mapping, |
Itaru Kitayama | 43e817a | 2011-04-25 19:43:51 -0400 | [diff] [blame] | 3625 | page->index, GFP_NOFS)) { |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 3626 | page_cache_release(page); |
| 3627 | continue; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3628 | } |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 3629 | |
| 3630 | pagepool[nr++] = page; |
| 3631 | if (nr < ARRAY_SIZE(pagepool)) |
| 3632 | continue; |
| 3633 | for (i = 0; i < nr; i++) { |
| 3634 | __extent_read_full_page(tree, pagepool[i], get_extent, |
| 3635 | &bio, 0, &bio_flags); |
| 3636 | page_cache_release(pagepool[i]); |
| 3637 | } |
| 3638 | nr = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3639 | } |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 3640 | for (i = 0; i < nr; i++) { |
| 3641 | __extent_read_full_page(tree, pagepool[i], get_extent, |
| 3642 | &bio, 0, &bio_flags); |
| 3643 | page_cache_release(pagepool[i]); |
| 3644 | } |
| 3645 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3646 | BUG_ON(!list_empty(pages)); |
| 3647 | if (bio) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3648 | return submit_one_bio(READ, bio, 0, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3649 | return 0; |
| 3650 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3651 | |
| 3652 | /* |
| 3653 | * basic invalidatepage code, this waits on any locked or writeback |
| 3654 | * ranges corresponding to the page, and then deletes any extent state |
| 3655 | * records from the tree |
| 3656 | */ |
| 3657 | int extent_invalidatepage(struct extent_io_tree *tree, |
| 3658 | struct page *page, unsigned long offset) |
| 3659 | { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3660 | struct extent_state *cached_state = NULL; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3661 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3662 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 3663 | size_t blocksize = page->mapping->host->i_sb->s_blocksize; |
| 3664 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3665 | start += ALIGN(offset, blocksize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3666 | if (start > end) |
| 3667 | return 0; |
| 3668 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 3669 | lock_extent_bits(tree, start, end, 0, &cached_state); |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 3670 | wait_on_page_writeback(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3671 | clear_extent_bit(tree, start, end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 3672 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 3673 | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3674 | 1, 1, &cached_state, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3675 | return 0; |
| 3676 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3677 | |
| 3678 | /* |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 3679 | * a helper for releasepage, this tests for areas of the page that |
| 3680 | * are locked or under IO and drops the related state bits if it is safe |
| 3681 | * to drop the page. |
| 3682 | */ |
| 3683 | int try_release_extent_state(struct extent_map_tree *map, |
| 3684 | struct extent_io_tree *tree, struct page *page, |
| 3685 | gfp_t mask) |
| 3686 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3687 | u64 start = page_offset(page); |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 3688 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 3689 | int ret = 1; |
| 3690 | |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3691 | if (test_range_bit(tree, start, end, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 3692 | EXTENT_IOBITS, 0, NULL)) |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 3693 | ret = 0; |
| 3694 | else { |
| 3695 | if ((mask & GFP_NOFS) == GFP_NOFS) |
| 3696 | mask = GFP_NOFS; |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 3697 | /* |
| 3698 | * at this point we can safely clear everything except the |
| 3699 | * locked bit and the nodatasum bit |
| 3700 | */ |
Chris Mason | e3f24cc | 2011-02-14 12:52:08 -0500 | [diff] [blame] | 3701 | ret = clear_extent_bit(tree, start, end, |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 3702 | ~(EXTENT_LOCKED | EXTENT_NODATASUM), |
| 3703 | 0, 0, NULL, mask); |
Chris Mason | e3f24cc | 2011-02-14 12:52:08 -0500 | [diff] [blame] | 3704 | |
| 3705 | /* if clear_extent_bit failed for enomem reasons, |
| 3706 | * we can't allow the release to continue. |
| 3707 | */ |
| 3708 | if (ret < 0) |
| 3709 | ret = 0; |
| 3710 | else |
| 3711 | ret = 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 3712 | } |
| 3713 | return ret; |
| 3714 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 3715 | |
| 3716 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3717 | * a helper for releasepage. As long as there are no locked extents |
| 3718 | * in the range corresponding to the page, both state records and extent |
| 3719 | * map records are removed |
| 3720 | */ |
| 3721 | int try_release_extent_mapping(struct extent_map_tree *map, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3722 | struct extent_io_tree *tree, struct page *page, |
| 3723 | gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3724 | { |
| 3725 | struct extent_map *em; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3726 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3727 | u64 end = start + PAGE_CACHE_SIZE - 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 3728 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3729 | if ((mask & __GFP_WAIT) && |
| 3730 | page->mapping->host->i_size > 16 * 1024 * 1024) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 3731 | u64 len; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3732 | while (start <= end) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 3733 | len = end - start + 1; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3734 | write_lock(&map->lock); |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 3735 | em = lookup_extent_mapping(map, start, len); |
Tsutomu Itoh | 285190d | 2012-02-16 16:23:58 +0900 | [diff] [blame] | 3736 | if (!em) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3737 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3738 | break; |
| 3739 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3740 | if (test_bit(EXTENT_FLAG_PINNED, &em->flags) || |
| 3741 | em->start != start) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3742 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3743 | free_extent_map(em); |
| 3744 | break; |
| 3745 | } |
| 3746 | if (!test_range_bit(tree, em->start, |
| 3747 | extent_map_end(em) - 1, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 3748 | EXTENT_LOCKED | EXTENT_WRITEBACK, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3749 | 0, NULL)) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3750 | remove_extent_mapping(map, em); |
| 3751 | /* once for the rb tree */ |
| 3752 | free_extent_map(em); |
| 3753 | } |
| 3754 | start = extent_map_end(em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3755 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3756 | |
| 3757 | /* once for us */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3758 | free_extent_map(em); |
| 3759 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3760 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 3761 | return try_release_extent_state(map, tree, page, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3762 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3763 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3764 | /* |
| 3765 | * helper function for fiemap, which doesn't want to see any holes. |
| 3766 | * This maps until we find something past 'last' |
| 3767 | */ |
| 3768 | static struct extent_map *get_extent_skip_holes(struct inode *inode, |
| 3769 | u64 offset, |
| 3770 | u64 last, |
| 3771 | get_extent_t *get_extent) |
| 3772 | { |
| 3773 | u64 sectorsize = BTRFS_I(inode)->root->sectorsize; |
| 3774 | struct extent_map *em; |
| 3775 | u64 len; |
| 3776 | |
| 3777 | if (offset >= last) |
| 3778 | return NULL; |
| 3779 | |
| 3780 | while(1) { |
| 3781 | len = last - offset; |
| 3782 | if (len == 0) |
| 3783 | break; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3784 | len = ALIGN(len, sectorsize); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3785 | em = get_extent(inode, NULL, 0, offset, len, 0); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3786 | if (IS_ERR_OR_NULL(em)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3787 | return em; |
| 3788 | |
| 3789 | /* if this isn't a hole return it */ |
| 3790 | if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) && |
| 3791 | em->block_start != EXTENT_MAP_HOLE) { |
| 3792 | return em; |
| 3793 | } |
| 3794 | |
| 3795 | /* this is a hole, advance to the next extent */ |
| 3796 | offset = extent_map_end(em); |
| 3797 | free_extent_map(em); |
| 3798 | if (offset >= last) |
| 3799 | break; |
| 3800 | } |
| 3801 | return NULL; |
| 3802 | } |
| 3803 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3804 | int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 3805 | __u64 start, __u64 len, get_extent_t *get_extent) |
| 3806 | { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3807 | int ret = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3808 | u64 off = start; |
| 3809 | u64 max = start + len; |
| 3810 | u32 flags = 0; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3811 | u32 found_type; |
| 3812 | u64 last; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3813 | u64 last_for_get_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3814 | u64 disko = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3815 | u64 isize = i_size_read(inode); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3816 | struct btrfs_key found_key; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3817 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3818 | struct extent_state *cached_state = NULL; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3819 | struct btrfs_path *path; |
| 3820 | struct btrfs_file_extent_item *item; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3821 | int end = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3822 | u64 em_start = 0; |
| 3823 | u64 em_len = 0; |
| 3824 | u64 em_end = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3825 | unsigned long emflags; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3826 | |
| 3827 | if (len == 0) |
| 3828 | return -EINVAL; |
| 3829 | |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3830 | path = btrfs_alloc_path(); |
| 3831 | if (!path) |
| 3832 | return -ENOMEM; |
| 3833 | path->leave_spinning = 1; |
| 3834 | |
Josef Bacik | 4d479cf | 2011-11-17 11:34:31 -0500 | [diff] [blame] | 3835 | start = ALIGN(start, BTRFS_I(inode)->root->sectorsize); |
| 3836 | len = ALIGN(len, BTRFS_I(inode)->root->sectorsize); |
| 3837 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3838 | /* |
| 3839 | * lookup the last file extent. We're not using i_size here |
| 3840 | * because there might be preallocation past i_size |
| 3841 | */ |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3842 | ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3843 | path, btrfs_ino(inode), -1, 0); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3844 | if (ret < 0) { |
| 3845 | btrfs_free_path(path); |
| 3846 | return ret; |
| 3847 | } |
| 3848 | WARN_ON(!ret); |
| 3849 | path->slots[0]--; |
| 3850 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3851 | struct btrfs_file_extent_item); |
| 3852 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]); |
| 3853 | found_type = btrfs_key_type(&found_key); |
| 3854 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3855 | /* No extents, but there might be delalloc bits */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3856 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3857 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3858 | /* have to trust i_size as the end */ |
| 3859 | last = (u64)-1; |
| 3860 | last_for_get_extent = isize; |
| 3861 | } else { |
| 3862 | /* |
| 3863 | * remember the start of the last extent. There are a |
| 3864 | * bunch of different factors that go into the length of the |
| 3865 | * extent, so its much less complex to remember where it started |
| 3866 | */ |
| 3867 | last = found_key.offset; |
| 3868 | last_for_get_extent = last + 1; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3869 | } |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3870 | btrfs_free_path(path); |
| 3871 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3872 | /* |
| 3873 | * we might have some extents allocated but more delalloc past those |
| 3874 | * extents. so, we trust isize unless the start of the last extent is |
| 3875 | * beyond isize |
| 3876 | */ |
| 3877 | if (last < isize) { |
| 3878 | last = (u64)-1; |
| 3879 | last_for_get_extent = isize; |
| 3880 | } |
| 3881 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3882 | lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 3883 | &cached_state); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3884 | |
Josef Bacik | 4d479cf | 2011-11-17 11:34:31 -0500 | [diff] [blame] | 3885 | em = get_extent_skip_holes(inode, start, last_for_get_extent, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3886 | get_extent); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3887 | if (!em) |
| 3888 | goto out; |
| 3889 | if (IS_ERR(em)) { |
| 3890 | ret = PTR_ERR(em); |
| 3891 | goto out; |
| 3892 | } |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3893 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3894 | while (!end) { |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 3895 | u64 offset_in_extent; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3896 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 3897 | /* break if the extent we found is outside the range */ |
| 3898 | if (em->start >= max || extent_map_end(em) < off) |
| 3899 | break; |
| 3900 | |
| 3901 | /* |
| 3902 | * get_extent may return an extent that starts before our |
| 3903 | * requested range. We have to make sure the ranges |
| 3904 | * we return to fiemap always move forward and don't |
| 3905 | * overlap, so adjust the offsets here |
| 3906 | */ |
| 3907 | em_start = max(em->start, off); |
| 3908 | |
| 3909 | /* |
| 3910 | * record the offset from the start of the extent |
| 3911 | * for adjusting the disk offset below |
| 3912 | */ |
| 3913 | offset_in_extent = em_start - em->start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3914 | em_end = extent_map_end(em); |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 3915 | em_len = em_end - em_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3916 | emflags = em->flags; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3917 | disko = 0; |
| 3918 | flags = 0; |
| 3919 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 3920 | /* |
| 3921 | * bump off for our next call to get_extent |
| 3922 | */ |
| 3923 | off = extent_map_end(em); |
| 3924 | if (off >= max) |
| 3925 | end = 1; |
| 3926 | |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 3927 | if (em->block_start == EXTENT_MAP_LAST_BYTE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3928 | end = 1; |
| 3929 | flags |= FIEMAP_EXTENT_LAST; |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 3930 | } else if (em->block_start == EXTENT_MAP_INLINE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3931 | flags |= (FIEMAP_EXTENT_DATA_INLINE | |
| 3932 | FIEMAP_EXTENT_NOT_ALIGNED); |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 3933 | } else if (em->block_start == EXTENT_MAP_DELALLOC) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3934 | flags |= (FIEMAP_EXTENT_DELALLOC | |
| 3935 | FIEMAP_EXTENT_UNKNOWN); |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 3936 | } else { |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 3937 | disko = em->block_start + offset_in_extent; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3938 | } |
| 3939 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 3940 | flags |= FIEMAP_EXTENT_ENCODED; |
| 3941 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3942 | free_extent_map(em); |
| 3943 | em = NULL; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3944 | if ((em_start >= last) || em_len == (u64)-1 || |
| 3945 | (last == (u64)-1 && isize <= em_end)) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3946 | flags |= FIEMAP_EXTENT_LAST; |
| 3947 | end = 1; |
| 3948 | } |
| 3949 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3950 | /* now scan forward to see if this is really the last extent. */ |
| 3951 | em = get_extent_skip_holes(inode, off, last_for_get_extent, |
| 3952 | get_extent); |
| 3953 | if (IS_ERR(em)) { |
| 3954 | ret = PTR_ERR(em); |
| 3955 | goto out; |
| 3956 | } |
| 3957 | if (!em) { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 3958 | flags |= FIEMAP_EXTENT_LAST; |
| 3959 | end = 1; |
| 3960 | } |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 3961 | ret = fiemap_fill_next_extent(fieinfo, em_start, disko, |
| 3962 | em_len, flags); |
| 3963 | if (ret) |
| 3964 | goto out_free; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3965 | } |
| 3966 | out_free: |
| 3967 | free_extent_map(em); |
| 3968 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3969 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len, |
| 3970 | &cached_state, GFP_NOFS); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 3971 | return ret; |
| 3972 | } |
| 3973 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 3974 | static void __free_extent_buffer(struct extent_buffer *eb) |
| 3975 | { |
| 3976 | #if LEAK_DEBUG |
| 3977 | unsigned long flags; |
| 3978 | spin_lock_irqsave(&leak_lock, flags); |
| 3979 | list_del(&eb->leak_list); |
| 3980 | spin_unlock_irqrestore(&leak_lock, flags); |
| 3981 | #endif |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 3982 | kmem_cache_free(extent_buffer_cache, eb); |
| 3983 | } |
| 3984 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3985 | static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree, |
| 3986 | u64 start, |
| 3987 | unsigned long len, |
| 3988 | gfp_t mask) |
| 3989 | { |
| 3990 | struct extent_buffer *eb = NULL; |
Chris Mason | 3935127 | 2009-02-04 09:24:05 -0500 | [diff] [blame] | 3991 | #if LEAK_DEBUG |
Chris Mason | 2d2ae54 | 2008-03-26 16:24:23 -0400 | [diff] [blame] | 3992 | unsigned long flags; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3993 | #endif |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3994 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3995 | eb = kmem_cache_zalloc(extent_buffer_cache, mask); |
Tsutomu Itoh | 91ca338 | 2011-01-05 02:32:22 +0000 | [diff] [blame] | 3996 | if (eb == NULL) |
| 3997 | return NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3998 | eb->start = start; |
| 3999 | eb->len = len; |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4000 | eb->tree = tree; |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4001 | eb->bflags = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4002 | rwlock_init(&eb->lock); |
| 4003 | atomic_set(&eb->write_locks, 0); |
| 4004 | atomic_set(&eb->read_locks, 0); |
| 4005 | atomic_set(&eb->blocking_readers, 0); |
| 4006 | atomic_set(&eb->blocking_writers, 0); |
| 4007 | atomic_set(&eb->spinning_readers, 0); |
| 4008 | atomic_set(&eb->spinning_writers, 0); |
Arne Jansen | 5b25f70 | 2011-09-13 10:55:48 +0200 | [diff] [blame] | 4009 | eb->lock_nested = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4010 | init_waitqueue_head(&eb->write_lock_wq); |
| 4011 | init_waitqueue_head(&eb->read_lock_wq); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4012 | |
Chris Mason | 3935127 | 2009-02-04 09:24:05 -0500 | [diff] [blame] | 4013 | #if LEAK_DEBUG |
Chris Mason | 2d2ae54 | 2008-03-26 16:24:23 -0400 | [diff] [blame] | 4014 | spin_lock_irqsave(&leak_lock, flags); |
| 4015 | list_add(&eb->leak_list, &buffers); |
| 4016 | spin_unlock_irqrestore(&leak_lock, flags); |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 4017 | #endif |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4018 | spin_lock_init(&eb->refs_lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4019 | atomic_set(&eb->refs, 1); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4020 | atomic_set(&eb->io_pages, 0); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4021 | |
David Sterba | b8dae31 | 2013-02-28 14:54:18 +0000 | [diff] [blame] | 4022 | /* |
| 4023 | * Sanity checks, currently the maximum is 64k covered by 16x 4k pages |
| 4024 | */ |
| 4025 | BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE |
| 4026 | > MAX_INLINE_EXTENT_BUFFER_SIZE); |
| 4027 | BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4028 | |
| 4029 | return eb; |
| 4030 | } |
| 4031 | |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4032 | struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src) |
| 4033 | { |
| 4034 | unsigned long i; |
| 4035 | struct page *p; |
| 4036 | struct extent_buffer *new; |
| 4037 | unsigned long num_pages = num_extent_pages(src->start, src->len); |
| 4038 | |
| 4039 | new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC); |
| 4040 | if (new == NULL) |
| 4041 | return NULL; |
| 4042 | |
| 4043 | for (i = 0; i < num_pages; i++) { |
| 4044 | p = alloc_page(GFP_ATOMIC); |
| 4045 | BUG_ON(!p); |
| 4046 | attach_extent_buffer_page(new, p); |
| 4047 | WARN_ON(PageDirty(p)); |
| 4048 | SetPageUptodate(p); |
| 4049 | new->pages[i] = p; |
| 4050 | } |
| 4051 | |
| 4052 | copy_extent_buffer(new, src, 0, 0, src->len); |
| 4053 | set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags); |
| 4054 | set_bit(EXTENT_BUFFER_DUMMY, &new->bflags); |
| 4055 | |
| 4056 | return new; |
| 4057 | } |
| 4058 | |
| 4059 | struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len) |
| 4060 | { |
| 4061 | struct extent_buffer *eb; |
| 4062 | unsigned long num_pages = num_extent_pages(0, len); |
| 4063 | unsigned long i; |
| 4064 | |
| 4065 | eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC); |
| 4066 | if (!eb) |
| 4067 | return NULL; |
| 4068 | |
| 4069 | for (i = 0; i < num_pages; i++) { |
| 4070 | eb->pages[i] = alloc_page(GFP_ATOMIC); |
| 4071 | if (!eb->pages[i]) |
| 4072 | goto err; |
| 4073 | } |
| 4074 | set_extent_buffer_uptodate(eb); |
| 4075 | btrfs_set_header_nritems(eb, 0); |
| 4076 | set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags); |
| 4077 | |
| 4078 | return eb; |
| 4079 | err: |
Stefan Behrens | 84167d1 | 2012-10-11 07:25:16 -0600 | [diff] [blame] | 4080 | for (; i > 0; i--) |
| 4081 | __free_page(eb->pages[i - 1]); |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4082 | __free_extent_buffer(eb); |
| 4083 | return NULL; |
| 4084 | } |
| 4085 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4086 | static int extent_buffer_under_io(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4087 | { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4088 | return (atomic_read(&eb->io_pages) || |
| 4089 | test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) || |
| 4090 | test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4091 | } |
| 4092 | |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4093 | /* |
| 4094 | * Helper for releasing extent buffer page. |
| 4095 | */ |
| 4096 | static void btrfs_release_extent_buffer_page(struct extent_buffer *eb, |
| 4097 | unsigned long start_idx) |
| 4098 | { |
| 4099 | unsigned long index; |
Wang Sheng-Hui | 39bab87 | 2012-04-06 14:35:31 +0800 | [diff] [blame] | 4100 | unsigned long num_pages; |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4101 | struct page *page; |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4102 | int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4103 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4104 | BUG_ON(extent_buffer_under_io(eb)); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4105 | |
Wang Sheng-Hui | 39bab87 | 2012-04-06 14:35:31 +0800 | [diff] [blame] | 4106 | num_pages = num_extent_pages(eb->start, eb->len); |
| 4107 | index = start_idx + num_pages; |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4108 | if (start_idx >= index) |
| 4109 | return; |
| 4110 | |
| 4111 | do { |
| 4112 | index--; |
| 4113 | page = extent_buffer_page(eb, index); |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4114 | if (page && mapped) { |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4115 | spin_lock(&page->mapping->private_lock); |
| 4116 | /* |
| 4117 | * We do this since we'll remove the pages after we've |
| 4118 | * removed the eb from the radix tree, so we could race |
| 4119 | * and have this page now attached to the new eb. So |
| 4120 | * only clear page_private if it's still connected to |
| 4121 | * this eb. |
| 4122 | */ |
| 4123 | if (PagePrivate(page) && |
| 4124 | page->private == (unsigned long)eb) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4125 | BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4126 | BUG_ON(PageDirty(page)); |
| 4127 | BUG_ON(PageWriteback(page)); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4128 | /* |
| 4129 | * We need to make sure we haven't be attached |
| 4130 | * to a new eb. |
| 4131 | */ |
| 4132 | ClearPagePrivate(page); |
| 4133 | set_page_private(page, 0); |
| 4134 | /* One for the page private */ |
| 4135 | page_cache_release(page); |
| 4136 | } |
| 4137 | spin_unlock(&page->mapping->private_lock); |
| 4138 | |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4139 | } |
| 4140 | if (page) { |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4141 | /* One for when we alloced the page */ |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4142 | page_cache_release(page); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4143 | } |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4144 | } while (index != start_idx); |
| 4145 | } |
| 4146 | |
| 4147 | /* |
| 4148 | * Helper for releasing the extent buffer. |
| 4149 | */ |
| 4150 | static inline void btrfs_release_extent_buffer(struct extent_buffer *eb) |
| 4151 | { |
| 4152 | btrfs_release_extent_buffer_page(eb, 0); |
| 4153 | __free_extent_buffer(eb); |
| 4154 | } |
| 4155 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4156 | static void check_buffer_tree_ref(struct extent_buffer *eb) |
| 4157 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4158 | int refs; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4159 | /* the ref bit is tricky. We have to make sure it is set |
| 4160 | * if we have the buffer dirty. Otherwise the |
| 4161 | * code to free a buffer can end up dropping a dirty |
| 4162 | * page |
| 4163 | * |
| 4164 | * Once the ref bit is set, it won't go away while the |
| 4165 | * buffer is dirty or in writeback, and it also won't |
| 4166 | * go away while we have the reference count on the |
| 4167 | * eb bumped. |
| 4168 | * |
| 4169 | * We can't just set the ref bit without bumping the |
| 4170 | * ref on the eb because free_extent_buffer might |
| 4171 | * see the ref bit and try to clear it. If this happens |
| 4172 | * free_extent_buffer might end up dropping our original |
| 4173 | * ref by mistake and freeing the page before we are able |
| 4174 | * to add one more ref. |
| 4175 | * |
| 4176 | * So bump the ref count first, then set the bit. If someone |
| 4177 | * beat us to it, drop the ref we added. |
| 4178 | */ |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4179 | refs = atomic_read(&eb->refs); |
| 4180 | if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 4181 | return; |
| 4182 | |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4183 | spin_lock(&eb->refs_lock); |
| 4184 | if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4185 | atomic_inc(&eb->refs); |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4186 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4187 | } |
| 4188 | |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4189 | static void mark_extent_buffer_accessed(struct extent_buffer *eb) |
| 4190 | { |
| 4191 | unsigned long num_pages, i; |
| 4192 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4193 | check_buffer_tree_ref(eb); |
| 4194 | |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4195 | num_pages = num_extent_pages(eb->start, eb->len); |
| 4196 | for (i = 0; i < num_pages; i++) { |
| 4197 | struct page *p = extent_buffer_page(eb, i); |
| 4198 | mark_page_accessed(p); |
| 4199 | } |
| 4200 | } |
| 4201 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4202 | struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree, |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4203 | u64 start, unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4204 | { |
| 4205 | unsigned long num_pages = num_extent_pages(start, len); |
| 4206 | unsigned long i; |
| 4207 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 4208 | struct extent_buffer *eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4209 | struct extent_buffer *exists = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4210 | struct page *p; |
| 4211 | struct address_space *mapping = tree->mapping; |
| 4212 | int uptodate = 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4213 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4214 | |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4215 | rcu_read_lock(); |
| 4216 | eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT); |
| 4217 | if (eb && atomic_inc_not_zero(&eb->refs)) { |
| 4218 | rcu_read_unlock(); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4219 | mark_extent_buffer_accessed(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4220 | return eb; |
| 4221 | } |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4222 | rcu_read_unlock(); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4223 | |
David Sterba | ba14419 | 2011-04-21 01:12:06 +0200 | [diff] [blame] | 4224 | eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 4225 | if (!eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4226 | return NULL; |
| 4227 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4228 | for (i = 0; i < num_pages; i++, index++) { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4229 | p = find_or_create_page(mapping, index, GFP_NOFS); |
Josef Bacik | 4804b38 | 2012-10-05 16:43:45 -0400 | [diff] [blame] | 4230 | if (!p) |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4231 | goto free_eb; |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4232 | |
| 4233 | spin_lock(&mapping->private_lock); |
| 4234 | if (PagePrivate(p)) { |
| 4235 | /* |
| 4236 | * We could have already allocated an eb for this page |
| 4237 | * and attached one so lets see if we can get a ref on |
| 4238 | * the existing eb, and if we can we know it's good and |
| 4239 | * we can just return that one, else we know we can just |
| 4240 | * overwrite page->private. |
| 4241 | */ |
| 4242 | exists = (struct extent_buffer *)p->private; |
| 4243 | if (atomic_inc_not_zero(&exists->refs)) { |
| 4244 | spin_unlock(&mapping->private_lock); |
| 4245 | unlock_page(p); |
Josef Bacik | 17de39a | 2012-05-04 15:16:06 -0400 | [diff] [blame] | 4246 | page_cache_release(p); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4247 | mark_extent_buffer_accessed(exists); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4248 | goto free_eb; |
| 4249 | } |
| 4250 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4251 | /* |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4252 | * Do this so attach doesn't complain and we need to |
| 4253 | * drop the ref the old guy had. |
| 4254 | */ |
| 4255 | ClearPagePrivate(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4256 | WARN_ON(PageDirty(p)); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4257 | page_cache_release(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4258 | } |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4259 | attach_extent_buffer_page(eb, p); |
| 4260 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4261 | WARN_ON(PageDirty(p)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4262 | mark_page_accessed(p); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4263 | eb->pages[i] = p; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4264 | if (!PageUptodate(p)) |
| 4265 | uptodate = 0; |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 4266 | |
| 4267 | /* |
| 4268 | * see below about how we avoid a nasty race with release page |
| 4269 | * and why we unlock later |
| 4270 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4271 | } |
| 4272 | if (uptodate) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4273 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 4274 | again: |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4275 | ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); |
| 4276 | if (ret) |
| 4277 | goto free_eb; |
| 4278 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4279 | spin_lock(&tree->buffer_lock); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4280 | ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb); |
| 4281 | if (ret == -EEXIST) { |
| 4282 | exists = radix_tree_lookup(&tree->buffer, |
| 4283 | start >> PAGE_CACHE_SHIFT); |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 4284 | if (!atomic_inc_not_zero(&exists->refs)) { |
| 4285 | spin_unlock(&tree->buffer_lock); |
| 4286 | radix_tree_preload_end(); |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 4287 | exists = NULL; |
| 4288 | goto again; |
| 4289 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4290 | spin_unlock(&tree->buffer_lock); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4291 | radix_tree_preload_end(); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4292 | mark_extent_buffer_accessed(exists); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4293 | goto free_eb; |
| 4294 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4295 | /* add one reference for the tree */ |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4296 | check_buffer_tree_ref(eb); |
Yan, Zheng | f044ba7 | 2010-02-04 08:46:56 +0000 | [diff] [blame] | 4297 | spin_unlock(&tree->buffer_lock); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4298 | radix_tree_preload_end(); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 4299 | |
| 4300 | /* |
| 4301 | * there is a race where release page may have |
| 4302 | * tried to find this extent buffer in the radix |
| 4303 | * but failed. It will tell the VM it is safe to |
| 4304 | * reclaim the, and it will clear the page private bit. |
| 4305 | * We must make sure to set the page private bit properly |
| 4306 | * after the extent buffer is in the radix tree so |
| 4307 | * it doesn't get lost |
| 4308 | */ |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4309 | SetPageChecked(eb->pages[0]); |
| 4310 | for (i = 1; i < num_pages; i++) { |
| 4311 | p = extent_buffer_page(eb, i); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4312 | ClearPageChecked(p); |
| 4313 | unlock_page(p); |
| 4314 | } |
| 4315 | unlock_page(eb->pages[0]); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4316 | return eb; |
| 4317 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4318 | free_eb: |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4319 | for (i = 0; i < num_pages; i++) { |
| 4320 | if (eb->pages[i]) |
| 4321 | unlock_page(eb->pages[i]); |
| 4322 | } |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 4323 | |
Josef Bacik | 17de39a | 2012-05-04 15:16:06 -0400 | [diff] [blame] | 4324 | WARN_ON(!atomic_dec_and_test(&eb->refs)); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4325 | btrfs_release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4326 | return exists; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4327 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4328 | |
| 4329 | struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree, |
David Sterba | f09d1f6 | 2011-04-21 01:08:01 +0200 | [diff] [blame] | 4330 | u64 start, unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4331 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4332 | struct extent_buffer *eb; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4333 | |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4334 | rcu_read_lock(); |
| 4335 | eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT); |
| 4336 | if (eb && atomic_inc_not_zero(&eb->refs)) { |
| 4337 | rcu_read_unlock(); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4338 | mark_extent_buffer_accessed(eb); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4339 | return eb; |
| 4340 | } |
| 4341 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4342 | |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4343 | return NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4344 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4345 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4346 | static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head) |
| 4347 | { |
| 4348 | struct extent_buffer *eb = |
| 4349 | container_of(head, struct extent_buffer, rcu_head); |
| 4350 | |
| 4351 | __free_extent_buffer(eb); |
| 4352 | } |
| 4353 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4354 | /* Expects to have eb->eb_lock already held */ |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 4355 | static int release_extent_buffer(struct extent_buffer *eb, gfp_t mask) |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4356 | { |
| 4357 | WARN_ON(atomic_read(&eb->refs) == 0); |
| 4358 | if (atomic_dec_and_test(&eb->refs)) { |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4359 | if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) { |
| 4360 | spin_unlock(&eb->refs_lock); |
| 4361 | } else { |
| 4362 | struct extent_io_tree *tree = eb->tree; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4363 | |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4364 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4365 | |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4366 | spin_lock(&tree->buffer_lock); |
| 4367 | radix_tree_delete(&tree->buffer, |
| 4368 | eb->start >> PAGE_CACHE_SHIFT); |
| 4369 | spin_unlock(&tree->buffer_lock); |
| 4370 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4371 | |
| 4372 | /* Should be safe to release our pages at this point */ |
| 4373 | btrfs_release_extent_buffer_page(eb, 0); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4374 | call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 4375 | return 1; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4376 | } |
| 4377 | spin_unlock(&eb->refs_lock); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 4378 | |
| 4379 | return 0; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4380 | } |
| 4381 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4382 | void free_extent_buffer(struct extent_buffer *eb) |
| 4383 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4384 | int refs; |
| 4385 | int old; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4386 | if (!eb) |
| 4387 | return; |
| 4388 | |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4389 | while (1) { |
| 4390 | refs = atomic_read(&eb->refs); |
| 4391 | if (refs <= 3) |
| 4392 | break; |
| 4393 | old = atomic_cmpxchg(&eb->refs, refs, refs - 1); |
| 4394 | if (old == refs) |
| 4395 | return; |
| 4396 | } |
| 4397 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4398 | spin_lock(&eb->refs_lock); |
| 4399 | if (atomic_read(&eb->refs) == 2 && |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4400 | test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) |
| 4401 | atomic_dec(&eb->refs); |
| 4402 | |
| 4403 | if (atomic_read(&eb->refs) == 2 && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4404 | test_bit(EXTENT_BUFFER_STALE, &eb->bflags) && |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4405 | !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4406 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 4407 | atomic_dec(&eb->refs); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4408 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4409 | /* |
| 4410 | * I know this is terrible, but it's temporary until we stop tracking |
| 4411 | * the uptodate bits and such for the extent buffers. |
| 4412 | */ |
| 4413 | release_extent_buffer(eb, GFP_ATOMIC); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4414 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4415 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4416 | void free_extent_buffer_stale(struct extent_buffer *eb) |
| 4417 | { |
| 4418 | if (!eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4419 | return; |
| 4420 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4421 | spin_lock(&eb->refs_lock); |
| 4422 | set_bit(EXTENT_BUFFER_STALE, &eb->bflags); |
| 4423 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4424 | if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4425 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 4426 | atomic_dec(&eb->refs); |
| 4427 | release_extent_buffer(eb, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4428 | } |
| 4429 | |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 4430 | void clear_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4431 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4432 | unsigned long i; |
| 4433 | unsigned long num_pages; |
| 4434 | struct page *page; |
| 4435 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4436 | num_pages = num_extent_pages(eb->start, eb->len); |
| 4437 | |
| 4438 | for (i = 0; i < num_pages; i++) { |
| 4439 | page = extent_buffer_page(eb, i); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4440 | if (!PageDirty(page)) |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4441 | continue; |
| 4442 | |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 4443 | lock_page(page); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 4444 | WARN_ON(!PagePrivate(page)); |
| 4445 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4446 | clear_page_dirty_for_io(page); |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 4447 | spin_lock_irq(&page->mapping->tree_lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4448 | if (!PageDirty(page)) { |
| 4449 | radix_tree_tag_clear(&page->mapping->page_tree, |
| 4450 | page_index(page), |
| 4451 | PAGECACHE_TAG_DIRTY); |
| 4452 | } |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 4453 | spin_unlock_irq(&page->mapping->tree_lock); |
Chris Mason | bf0da8c | 2011-11-04 12:29:37 -0400 | [diff] [blame] | 4454 | ClearPageError(page); |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 4455 | unlock_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4456 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4457 | WARN_ON(atomic_read(&eb->refs) == 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4458 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4459 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4460 | int set_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4461 | { |
| 4462 | unsigned long i; |
| 4463 | unsigned long num_pages; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4464 | int was_dirty = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4465 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4466 | check_buffer_tree_ref(eb); |
| 4467 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4468 | was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4469 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4470 | num_pages = num_extent_pages(eb->start, eb->len); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4471 | WARN_ON(atomic_read(&eb->refs) == 0); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4472 | WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)); |
| 4473 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4474 | for (i = 0; i < num_pages; i++) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4475 | set_page_dirty(extent_buffer_page(eb, i)); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4476 | return was_dirty; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4477 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4478 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4479 | static int range_straddles_pages(u64 start, u64 len) |
Chris Mason | 19b6caf | 2011-07-25 06:50:50 -0400 | [diff] [blame] | 4480 | { |
| 4481 | if (len < PAGE_CACHE_SIZE) |
| 4482 | return 1; |
| 4483 | if (start & (PAGE_CACHE_SIZE - 1)) |
| 4484 | return 1; |
| 4485 | if ((start + len) & (PAGE_CACHE_SIZE - 1)) |
| 4486 | return 1; |
| 4487 | return 0; |
| 4488 | } |
| 4489 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4490 | int clear_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 4491 | { |
| 4492 | unsigned long i; |
| 4493 | struct page *page; |
| 4494 | unsigned long num_pages; |
| 4495 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4496 | clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4497 | num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 4498 | for (i = 0; i < num_pages; i++) { |
| 4499 | page = extent_buffer_page(eb, i); |
Chris Mason | 33958dc | 2008-07-30 10:29:12 -0400 | [diff] [blame] | 4500 | if (page) |
| 4501 | ClearPageUptodate(page); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 4502 | } |
| 4503 | return 0; |
| 4504 | } |
| 4505 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4506 | int set_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4507 | { |
| 4508 | unsigned long i; |
| 4509 | struct page *page; |
| 4510 | unsigned long num_pages; |
| 4511 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4512 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4513 | num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4514 | for (i = 0; i < num_pages; i++) { |
| 4515 | page = extent_buffer_page(eb, i); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4516 | SetPageUptodate(page); |
| 4517 | } |
| 4518 | return 0; |
| 4519 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4520 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4521 | int extent_range_uptodate(struct extent_io_tree *tree, |
| 4522 | u64 start, u64 end) |
| 4523 | { |
| 4524 | struct page *page; |
| 4525 | int ret; |
| 4526 | int pg_uptodate = 1; |
| 4527 | int uptodate; |
| 4528 | unsigned long index; |
| 4529 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4530 | if (range_straddles_pages(start, end - start + 1)) { |
Chris Mason | 19b6caf | 2011-07-25 06:50:50 -0400 | [diff] [blame] | 4531 | ret = test_range_bit(tree, start, end, |
| 4532 | EXTENT_UPTODATE, 1, NULL); |
| 4533 | if (ret) |
| 4534 | return 1; |
| 4535 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4536 | while (start <= end) { |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4537 | index = start >> PAGE_CACHE_SHIFT; |
| 4538 | page = find_get_page(tree->mapping, index); |
Mitch Harder | 8bedd51 | 2012-01-26 15:01:11 -0500 | [diff] [blame] | 4539 | if (!page) |
| 4540 | return 1; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4541 | uptodate = PageUptodate(page); |
| 4542 | page_cache_release(page); |
| 4543 | if (!uptodate) { |
| 4544 | pg_uptodate = 0; |
| 4545 | break; |
| 4546 | } |
| 4547 | start += PAGE_CACHE_SIZE; |
| 4548 | } |
| 4549 | return pg_uptodate; |
| 4550 | } |
| 4551 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4552 | int extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4553 | { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4554 | return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4555 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4556 | |
| 4557 | int read_extent_buffer_pages(struct extent_io_tree *tree, |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 4558 | struct extent_buffer *eb, u64 start, int wait, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4559 | get_extent_t *get_extent, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4560 | { |
| 4561 | unsigned long i; |
| 4562 | unsigned long start_i; |
| 4563 | struct page *page; |
| 4564 | int err; |
| 4565 | int ret = 0; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4566 | int locked_pages = 0; |
| 4567 | int all_uptodate = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4568 | unsigned long num_pages; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4569 | unsigned long num_reads = 0; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 4570 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4571 | unsigned long bio_flags = 0; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 4572 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4573 | if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4574 | return 0; |
| 4575 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4576 | if (start) { |
| 4577 | WARN_ON(start < eb->start); |
| 4578 | start_i = (start >> PAGE_CACHE_SHIFT) - |
| 4579 | (eb->start >> PAGE_CACHE_SHIFT); |
| 4580 | } else { |
| 4581 | start_i = 0; |
| 4582 | } |
| 4583 | |
| 4584 | num_pages = num_extent_pages(eb->start, eb->len); |
| 4585 | for (i = start_i; i < num_pages; i++) { |
| 4586 | page = extent_buffer_page(eb, i); |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 4587 | if (wait == WAIT_NONE) { |
David Woodhouse | 2db0496 | 2008-08-07 11:19:43 -0400 | [diff] [blame] | 4588 | if (!trylock_page(page)) |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4589 | goto unlock_exit; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4590 | } else { |
| 4591 | lock_page(page); |
| 4592 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4593 | locked_pages++; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4594 | if (!PageUptodate(page)) { |
| 4595 | num_reads++; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4596 | all_uptodate = 0; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4597 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4598 | } |
| 4599 | if (all_uptodate) { |
| 4600 | if (start_i == 0) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4601 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4602 | goto unlock_exit; |
| 4603 | } |
| 4604 | |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 4605 | clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags); |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 4606 | eb->read_mirror = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4607 | atomic_set(&eb->io_pages, num_reads); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4608 | for (i = start_i; i < num_pages; i++) { |
| 4609 | page = extent_buffer_page(eb, i); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4610 | if (!PageUptodate(page)) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4611 | ClearPageError(page); |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 4612 | err = __extent_read_full_page(tree, page, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4613 | get_extent, &bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4614 | mirror_num, &bio_flags); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4615 | if (err) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4616 | ret = err; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4617 | } else { |
| 4618 | unlock_page(page); |
| 4619 | } |
| 4620 | } |
| 4621 | |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 4622 | if (bio) { |
| 4623 | err = submit_one_bio(READ, bio, mirror_num, bio_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4624 | if (err) |
| 4625 | return err; |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 4626 | } |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 4627 | |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 4628 | if (ret || wait != WAIT_COMPLETE) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4629 | return ret; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4630 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4631 | for (i = start_i; i < num_pages; i++) { |
| 4632 | page = extent_buffer_page(eb, i); |
| 4633 | wait_on_page_locked(page); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4634 | if (!PageUptodate(page)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4635 | ret = -EIO; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4636 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4637 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4638 | return ret; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4639 | |
| 4640 | unlock_exit: |
| 4641 | i = start_i; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4642 | while (locked_pages > 0) { |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4643 | page = extent_buffer_page(eb, i); |
| 4644 | i++; |
| 4645 | unlock_page(page); |
| 4646 | locked_pages--; |
| 4647 | } |
| 4648 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4649 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4650 | |
| 4651 | void read_extent_buffer(struct extent_buffer *eb, void *dstv, |
| 4652 | unsigned long start, |
| 4653 | unsigned long len) |
| 4654 | { |
| 4655 | size_t cur; |
| 4656 | size_t offset; |
| 4657 | struct page *page; |
| 4658 | char *kaddr; |
| 4659 | char *dst = (char *)dstv; |
| 4660 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 4661 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4662 | |
| 4663 | WARN_ON(start > eb->len); |
| 4664 | WARN_ON(start + len > eb->start + eb->len); |
| 4665 | |
| 4666 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 4667 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4668 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4669 | page = extent_buffer_page(eb, i); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4670 | |
| 4671 | cur = min(len, (PAGE_CACHE_SIZE - offset)); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4672 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4673 | memcpy(dst, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4674 | |
| 4675 | dst += cur; |
| 4676 | len -= cur; |
| 4677 | offset = 0; |
| 4678 | i++; |
| 4679 | } |
| 4680 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4681 | |
| 4682 | int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start, |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4683 | unsigned long min_len, char **map, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4684 | unsigned long *map_start, |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4685 | unsigned long *map_len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4686 | { |
| 4687 | size_t offset = start & (PAGE_CACHE_SIZE - 1); |
| 4688 | char *kaddr; |
| 4689 | struct page *p; |
| 4690 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 4691 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 4692 | unsigned long end_i = (start_offset + start + min_len - 1) >> |
| 4693 | PAGE_CACHE_SHIFT; |
| 4694 | |
| 4695 | if (i != end_i) |
| 4696 | return -EINVAL; |
| 4697 | |
| 4698 | if (i == 0) { |
| 4699 | offset = start_offset; |
| 4700 | *map_start = 0; |
| 4701 | } else { |
| 4702 | offset = 0; |
| 4703 | *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset; |
| 4704 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4705 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4706 | if (start + min_len > eb->len) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4707 | WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, " |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4708 | "wanted %lu %lu\n", (unsigned long long)eb->start, |
| 4709 | eb->len, start, min_len); |
Josef Bacik | 85026533 | 2011-03-15 14:52:12 -0400 | [diff] [blame] | 4710 | return -EINVAL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4711 | } |
| 4712 | |
| 4713 | p = extent_buffer_page(eb, i); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4714 | kaddr = page_address(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4715 | *map = kaddr + offset; |
| 4716 | *map_len = PAGE_CACHE_SIZE - offset; |
| 4717 | return 0; |
| 4718 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4719 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4720 | int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv, |
| 4721 | unsigned long start, |
| 4722 | unsigned long len) |
| 4723 | { |
| 4724 | size_t cur; |
| 4725 | size_t offset; |
| 4726 | struct page *page; |
| 4727 | char *kaddr; |
| 4728 | char *ptr = (char *)ptrv; |
| 4729 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 4730 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 4731 | int ret = 0; |
| 4732 | |
| 4733 | WARN_ON(start > eb->len); |
| 4734 | WARN_ON(start + len > eb->start + eb->len); |
| 4735 | |
| 4736 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 4737 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4738 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4739 | page = extent_buffer_page(eb, i); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4740 | |
| 4741 | cur = min(len, (PAGE_CACHE_SIZE - offset)); |
| 4742 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4743 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4744 | ret = memcmp(ptr, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4745 | if (ret) |
| 4746 | break; |
| 4747 | |
| 4748 | ptr += cur; |
| 4749 | len -= cur; |
| 4750 | offset = 0; |
| 4751 | i++; |
| 4752 | } |
| 4753 | return ret; |
| 4754 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4755 | |
| 4756 | void write_extent_buffer(struct extent_buffer *eb, const void *srcv, |
| 4757 | unsigned long start, unsigned long len) |
| 4758 | { |
| 4759 | size_t cur; |
| 4760 | size_t offset; |
| 4761 | struct page *page; |
| 4762 | char *kaddr; |
| 4763 | char *src = (char *)srcv; |
| 4764 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 4765 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 4766 | |
| 4767 | WARN_ON(start > eb->len); |
| 4768 | WARN_ON(start + len > eb->start + eb->len); |
| 4769 | |
| 4770 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 4771 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4772 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4773 | page = extent_buffer_page(eb, i); |
| 4774 | WARN_ON(!PageUptodate(page)); |
| 4775 | |
| 4776 | cur = min(len, PAGE_CACHE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4777 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4778 | memcpy(kaddr + offset, src, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4779 | |
| 4780 | src += cur; |
| 4781 | len -= cur; |
| 4782 | offset = 0; |
| 4783 | i++; |
| 4784 | } |
| 4785 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4786 | |
| 4787 | void memset_extent_buffer(struct extent_buffer *eb, char c, |
| 4788 | unsigned long start, unsigned long len) |
| 4789 | { |
| 4790 | size_t cur; |
| 4791 | size_t offset; |
| 4792 | struct page *page; |
| 4793 | char *kaddr; |
| 4794 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 4795 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 4796 | |
| 4797 | WARN_ON(start > eb->len); |
| 4798 | WARN_ON(start + len > eb->start + eb->len); |
| 4799 | |
| 4800 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 4801 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4802 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4803 | page = extent_buffer_page(eb, i); |
| 4804 | WARN_ON(!PageUptodate(page)); |
| 4805 | |
| 4806 | cur = min(len, PAGE_CACHE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4807 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4808 | memset(kaddr + offset, c, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4809 | |
| 4810 | len -= cur; |
| 4811 | offset = 0; |
| 4812 | i++; |
| 4813 | } |
| 4814 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4815 | |
| 4816 | void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src, |
| 4817 | unsigned long dst_offset, unsigned long src_offset, |
| 4818 | unsigned long len) |
| 4819 | { |
| 4820 | u64 dst_len = dst->len; |
| 4821 | size_t cur; |
| 4822 | size_t offset; |
| 4823 | struct page *page; |
| 4824 | char *kaddr; |
| 4825 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 4826 | unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT; |
| 4827 | |
| 4828 | WARN_ON(src->len != dst_len); |
| 4829 | |
| 4830 | offset = (start_offset + dst_offset) & |
| 4831 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 4832 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4833 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4834 | page = extent_buffer_page(dst, i); |
| 4835 | WARN_ON(!PageUptodate(page)); |
| 4836 | |
| 4837 | cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset)); |
| 4838 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4839 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4840 | read_extent_buffer(src, kaddr + offset, src_offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4841 | |
| 4842 | src_offset += cur; |
| 4843 | len -= cur; |
| 4844 | offset = 0; |
| 4845 | i++; |
| 4846 | } |
| 4847 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4848 | |
| 4849 | static void move_pages(struct page *dst_page, struct page *src_page, |
| 4850 | unsigned long dst_off, unsigned long src_off, |
| 4851 | unsigned long len) |
| 4852 | { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4853 | char *dst_kaddr = page_address(dst_page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4854 | if (dst_page == src_page) { |
| 4855 | memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len); |
| 4856 | } else { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4857 | char *src_kaddr = page_address(src_page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4858 | char *p = dst_kaddr + dst_off + len; |
| 4859 | char *s = src_kaddr + src_off + len; |
| 4860 | |
| 4861 | while (len--) |
| 4862 | *--p = *--s; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4863 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4864 | } |
| 4865 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 4866 | static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len) |
| 4867 | { |
| 4868 | unsigned long distance = (src > dst) ? src - dst : dst - src; |
| 4869 | return distance < len; |
| 4870 | } |
| 4871 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4872 | static void copy_pages(struct page *dst_page, struct page *src_page, |
| 4873 | unsigned long dst_off, unsigned long src_off, |
| 4874 | unsigned long len) |
| 4875 | { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4876 | char *dst_kaddr = page_address(dst_page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4877 | char *src_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4878 | int must_memmove = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4879 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 4880 | if (dst_page != src_page) { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4881 | src_kaddr = page_address(src_page); |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 4882 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4883 | src_kaddr = dst_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4884 | if (areas_overlap(src_off, dst_off, len)) |
| 4885 | must_memmove = 1; |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 4886 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4887 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4888 | if (must_memmove) |
| 4889 | memmove(dst_kaddr + dst_off, src_kaddr + src_off, len); |
| 4890 | else |
| 4891 | memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4892 | } |
| 4893 | |
| 4894 | void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 4895 | unsigned long src_offset, unsigned long len) |
| 4896 | { |
| 4897 | size_t cur; |
| 4898 | size_t dst_off_in_page; |
| 4899 | size_t src_off_in_page; |
| 4900 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 4901 | unsigned long dst_i; |
| 4902 | unsigned long src_i; |
| 4903 | |
| 4904 | if (src_offset + len > dst->len) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4905 | printk(KERN_ERR "btrfs memmove bogus src_offset %lu move " |
| 4906 | "len %lu dst len %lu\n", src_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4907 | BUG_ON(1); |
| 4908 | } |
| 4909 | if (dst_offset + len > dst->len) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4910 | printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move " |
| 4911 | "len %lu dst len %lu\n", dst_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4912 | BUG_ON(1); |
| 4913 | } |
| 4914 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4915 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4916 | dst_off_in_page = (start_offset + dst_offset) & |
| 4917 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 4918 | src_off_in_page = (start_offset + src_offset) & |
| 4919 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 4920 | |
| 4921 | dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT; |
| 4922 | src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT; |
| 4923 | |
| 4924 | cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - |
| 4925 | src_off_in_page)); |
| 4926 | cur = min_t(unsigned long, cur, |
| 4927 | (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page)); |
| 4928 | |
| 4929 | copy_pages(extent_buffer_page(dst, dst_i), |
| 4930 | extent_buffer_page(dst, src_i), |
| 4931 | dst_off_in_page, src_off_in_page, cur); |
| 4932 | |
| 4933 | src_offset += cur; |
| 4934 | dst_offset += cur; |
| 4935 | len -= cur; |
| 4936 | } |
| 4937 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4938 | |
| 4939 | void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 4940 | unsigned long src_offset, unsigned long len) |
| 4941 | { |
| 4942 | size_t cur; |
| 4943 | size_t dst_off_in_page; |
| 4944 | size_t src_off_in_page; |
| 4945 | unsigned long dst_end = dst_offset + len - 1; |
| 4946 | unsigned long src_end = src_offset + len - 1; |
| 4947 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 4948 | unsigned long dst_i; |
| 4949 | unsigned long src_i; |
| 4950 | |
| 4951 | if (src_offset + len > dst->len) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4952 | printk(KERN_ERR "btrfs memmove bogus src_offset %lu move " |
| 4953 | "len %lu len %lu\n", src_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4954 | BUG_ON(1); |
| 4955 | } |
| 4956 | if (dst_offset + len > dst->len) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4957 | printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move " |
| 4958 | "len %lu len %lu\n", dst_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4959 | BUG_ON(1); |
| 4960 | } |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4961 | if (dst_offset < src_offset) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4962 | memcpy_extent_buffer(dst, dst_offset, src_offset, len); |
| 4963 | return; |
| 4964 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4965 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4966 | dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT; |
| 4967 | src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT; |
| 4968 | |
| 4969 | dst_off_in_page = (start_offset + dst_end) & |
| 4970 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 4971 | src_off_in_page = (start_offset + src_end) & |
| 4972 | ((unsigned long)PAGE_CACHE_SIZE - 1); |
| 4973 | |
| 4974 | cur = min_t(unsigned long, len, src_off_in_page + 1); |
| 4975 | cur = min(cur, dst_off_in_page + 1); |
| 4976 | move_pages(extent_buffer_page(dst, dst_i), |
| 4977 | extent_buffer_page(dst, src_i), |
| 4978 | dst_off_in_page - cur + 1, |
| 4979 | src_off_in_page - cur + 1, cur); |
| 4980 | |
| 4981 | dst_end -= cur; |
| 4982 | src_end -= cur; |
| 4983 | len -= cur; |
| 4984 | } |
| 4985 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4986 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4987 | int try_release_extent_buffer(struct page *page, gfp_t mask) |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4988 | { |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4989 | struct extent_buffer *eb; |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4990 | |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4991 | /* |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4992 | * We need to make sure noboody is attaching this page to an eb right |
| 4993 | * now. |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4994 | */ |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4995 | spin_lock(&page->mapping->private_lock); |
| 4996 | if (!PagePrivate(page)) { |
| 4997 | spin_unlock(&page->mapping->private_lock); |
| 4998 | return 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4999 | } |
| 5000 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5001 | eb = (struct extent_buffer *)page->private; |
| 5002 | BUG_ON(!eb); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5003 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5004 | /* |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5005 | * This is a little awful but should be ok, we need to make sure that |
| 5006 | * the eb doesn't disappear out from under us while we're looking at |
| 5007 | * this page. |
| 5008 | */ |
| 5009 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5010 | if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5011 | spin_unlock(&eb->refs_lock); |
| 5012 | spin_unlock(&page->mapping->private_lock); |
| 5013 | return 0; |
| 5014 | } |
| 5015 | spin_unlock(&page->mapping->private_lock); |
| 5016 | |
| 5017 | if ((mask & GFP_NOFS) == GFP_NOFS) |
| 5018 | mask = GFP_NOFS; |
| 5019 | |
| 5020 | /* |
| 5021 | * If tree ref isn't set then we know the ref on this eb is a real ref, |
| 5022 | * so just return, this page will likely be freed soon anyway. |
| 5023 | */ |
| 5024 | if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { |
| 5025 | spin_unlock(&eb->refs_lock); |
| 5026 | return 0; |
| 5027 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5028 | |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 5029 | return release_extent_buffer(eb, mask); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5030 | } |