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