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