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