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