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 | |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 28 | static inline bool extent_state_in_tree(const struct extent_state *state) |
| 29 | { |
| 30 | return !RB_EMPTY_NODE(&state->rb_node); |
| 31 | } |
| 32 | |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 33 | #ifdef CONFIG_BTRFS_DEBUG |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 34 | static LIST_HEAD(buffers); |
| 35 | static LIST_HEAD(states); |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 36 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 37 | static DEFINE_SPINLOCK(leak_lock); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 38 | |
| 39 | static inline |
| 40 | void btrfs_leak_debug_add(struct list_head *new, struct list_head *head) |
| 41 | { |
| 42 | unsigned long flags; |
| 43 | |
| 44 | spin_lock_irqsave(&leak_lock, flags); |
| 45 | list_add(new, head); |
| 46 | spin_unlock_irqrestore(&leak_lock, flags); |
| 47 | } |
| 48 | |
| 49 | static inline |
| 50 | void btrfs_leak_debug_del(struct list_head *entry) |
| 51 | { |
| 52 | unsigned long flags; |
| 53 | |
| 54 | spin_lock_irqsave(&leak_lock, flags); |
| 55 | list_del(entry); |
| 56 | spin_unlock_irqrestore(&leak_lock, flags); |
| 57 | } |
| 58 | |
| 59 | static inline |
| 60 | void btrfs_leak_debug_check(void) |
| 61 | { |
| 62 | struct extent_state *state; |
| 63 | struct extent_buffer *eb; |
| 64 | |
| 65 | while (!list_empty(&states)) { |
| 66 | state = list_entry(states.next, struct extent_state, leak_list); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 67 | pr_err("BTRFS: state leak: start %llu end %llu state %lu in tree %d refs %d\n", |
| 68 | state->start, state->end, state->state, |
| 69 | extent_state_in_tree(state), |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 70 | atomic_read(&state->refs)); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 71 | list_del(&state->leak_list); |
| 72 | kmem_cache_free(extent_state_cache, state); |
| 73 | } |
| 74 | |
| 75 | while (!list_empty(&buffers)) { |
| 76 | eb = list_entry(buffers.next, struct extent_buffer, leak_list); |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 77 | printk(KERN_ERR "BTRFS: buffer leak start %llu len %lu " |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 78 | "refs %d\n", |
| 79 | eb->start, eb->len, atomic_read(&eb->refs)); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 80 | list_del(&eb->leak_list); |
| 81 | kmem_cache_free(extent_buffer_cache, eb); |
| 82 | } |
| 83 | } |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 84 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 85 | #define btrfs_debug_check_extent_io_range(tree, start, end) \ |
| 86 | __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end)) |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 87 | static inline void __btrfs_debug_check_extent_io_range(const char *caller, |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 88 | struct extent_io_tree *tree, u64 start, u64 end) |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 89 | { |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 90 | struct inode *inode; |
| 91 | u64 isize; |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 92 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 93 | if (!tree->mapping) |
| 94 | return; |
| 95 | |
| 96 | inode = tree->mapping->host; |
| 97 | isize = i_size_read(inode); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 98 | if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) { |
| 99 | printk_ratelimited(KERN_DEBUG |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 100 | "BTRFS: %s: ino %llu isize %llu odd range [%llu,%llu]\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 101 | caller, btrfs_ino(inode), isize, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 104 | #else |
| 105 | #define btrfs_leak_debug_add(new, head) do {} while (0) |
| 106 | #define btrfs_leak_debug_del(entry) do {} while (0) |
| 107 | #define btrfs_leak_debug_check() do {} while (0) |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 108 | #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] | 109 | #endif |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 110 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 111 | #define BUFFER_LRU_MAX 64 |
| 112 | |
| 113 | struct tree_entry { |
| 114 | u64 start; |
| 115 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 116 | struct rb_node rb_node; |
| 117 | }; |
| 118 | |
| 119 | struct extent_page_data { |
| 120 | struct bio *bio; |
| 121 | struct extent_io_tree *tree; |
| 122 | get_extent_t *get_extent; |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 123 | unsigned long bio_flags; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 124 | |
| 125 | /* tells writepage not to lock the state bits for this range |
| 126 | * it still does the unlocking |
| 127 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 128 | unsigned int extent_locked:1; |
| 129 | |
| 130 | /* tells the submit_bio code to use a WRITE_SYNC */ |
| 131 | unsigned int sync_io:1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 132 | }; |
| 133 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 134 | static noinline void flush_write_bio(void *data); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 135 | static inline struct btrfs_fs_info * |
| 136 | tree_fs_info(struct extent_io_tree *tree) |
| 137 | { |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 138 | if (!tree->mapping) |
| 139 | return NULL; |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 140 | return btrfs_sb(tree->mapping->host->i_sb); |
| 141 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 142 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 143 | int __init extent_io_init(void) |
| 144 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 145 | extent_state_cache = kmem_cache_create("btrfs_extent_state", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 146 | sizeof(struct extent_state), 0, |
| 147 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 148 | if (!extent_state_cache) |
| 149 | return -ENOMEM; |
| 150 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 151 | extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 152 | sizeof(struct extent_buffer), 0, |
| 153 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 154 | if (!extent_buffer_cache) |
| 155 | goto free_state_cache; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 156 | |
| 157 | btrfs_bioset = bioset_create(BIO_POOL_SIZE, |
| 158 | offsetof(struct btrfs_io_bio, bio)); |
| 159 | if (!btrfs_bioset) |
| 160 | goto free_buffer_cache; |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 161 | |
| 162 | if (bioset_integrity_create(btrfs_bioset, BIO_POOL_SIZE)) |
| 163 | goto free_bioset; |
| 164 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 165 | return 0; |
| 166 | |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 167 | free_bioset: |
| 168 | bioset_free(btrfs_bioset); |
| 169 | btrfs_bioset = NULL; |
| 170 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 171 | free_buffer_cache: |
| 172 | kmem_cache_destroy(extent_buffer_cache); |
| 173 | extent_buffer_cache = NULL; |
| 174 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 175 | free_state_cache: |
| 176 | kmem_cache_destroy(extent_state_cache); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 177 | extent_state_cache = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 178 | return -ENOMEM; |
| 179 | } |
| 180 | |
| 181 | void extent_io_exit(void) |
| 182 | { |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 183 | btrfs_leak_debug_check(); |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 184 | |
| 185 | /* |
| 186 | * Make sure all delayed rcu free are flushed before we |
| 187 | * destroy caches. |
| 188 | */ |
| 189 | rcu_barrier(); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 190 | if (extent_state_cache) |
| 191 | kmem_cache_destroy(extent_state_cache); |
| 192 | if (extent_buffer_cache) |
| 193 | kmem_cache_destroy(extent_buffer_cache); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 194 | if (btrfs_bioset) |
| 195 | bioset_free(btrfs_bioset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void extent_io_tree_init(struct extent_io_tree *tree, |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 199 | struct address_space *mapping) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 200 | { |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 201 | tree->state = RB_ROOT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 202 | tree->ops = NULL; |
| 203 | tree->dirty_bytes = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 204 | spin_lock_init(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 205 | tree->mapping = mapping; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 206 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 207 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 208 | static struct extent_state *alloc_extent_state(gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 209 | { |
| 210 | struct extent_state *state; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 211 | |
| 212 | state = kmem_cache_alloc(extent_state_cache, mask); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 213 | if (!state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 214 | return state; |
| 215 | state->state = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 216 | state->private = 0; |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 217 | RB_CLEAR_NODE(&state->rb_node); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 218 | btrfs_leak_debug_add(&state->leak_list, &states); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 219 | atomic_set(&state->refs, 1); |
| 220 | init_waitqueue_head(&state->wq); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 221 | trace_alloc_extent_state(state, mask, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 222 | return state; |
| 223 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 224 | |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 225 | void free_extent_state(struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 226 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 227 | if (!state) |
| 228 | return; |
| 229 | if (atomic_dec_and_test(&state->refs)) { |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 230 | WARN_ON(extent_state_in_tree(state)); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 231 | btrfs_leak_debug_del(&state->leak_list); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 232 | trace_free_extent_state(state, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 233 | kmem_cache_free(extent_state_cache, state); |
| 234 | } |
| 235 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 236 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 237 | static struct rb_node *tree_insert(struct rb_root *root, |
| 238 | struct rb_node *search_start, |
| 239 | u64 offset, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 240 | struct rb_node *node, |
| 241 | struct rb_node ***p_in, |
| 242 | struct rb_node **parent_in) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 243 | { |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 244 | struct rb_node **p; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 245 | struct rb_node *parent = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 246 | struct tree_entry *entry; |
| 247 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 248 | if (p_in && parent_in) { |
| 249 | p = *p_in; |
| 250 | parent = *parent_in; |
| 251 | goto do_insert; |
| 252 | } |
| 253 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 254 | p = search_start ? &search_start : &root->rb_node; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 255 | while (*p) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 256 | parent = *p; |
| 257 | entry = rb_entry(parent, struct tree_entry, rb_node); |
| 258 | |
| 259 | if (offset < entry->start) |
| 260 | p = &(*p)->rb_left; |
| 261 | else if (offset > entry->end) |
| 262 | p = &(*p)->rb_right; |
| 263 | else |
| 264 | return parent; |
| 265 | } |
| 266 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 267 | do_insert: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 268 | rb_link_node(node, parent, p); |
| 269 | rb_insert_color(node, root); |
| 270 | return NULL; |
| 271 | } |
| 272 | |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 273 | 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] | 274 | struct rb_node **prev_ret, |
| 275 | struct rb_node **next_ret, |
| 276 | struct rb_node ***p_ret, |
| 277 | struct rb_node **parent_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 278 | { |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 279 | struct rb_root *root = &tree->state; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 280 | struct rb_node **n = &root->rb_node; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 281 | struct rb_node *prev = NULL; |
| 282 | struct rb_node *orig_prev = NULL; |
| 283 | struct tree_entry *entry; |
| 284 | struct tree_entry *prev_entry = NULL; |
| 285 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 286 | while (*n) { |
| 287 | prev = *n; |
| 288 | entry = rb_entry(prev, struct tree_entry, rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 289 | prev_entry = entry; |
| 290 | |
| 291 | if (offset < entry->start) |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 292 | n = &(*n)->rb_left; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 293 | else if (offset > entry->end) |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 294 | n = &(*n)->rb_right; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 295 | else |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 296 | return *n; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 297 | } |
| 298 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 299 | if (p_ret) |
| 300 | *p_ret = n; |
| 301 | if (parent_ret) |
| 302 | *parent_ret = prev; |
| 303 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 304 | if (prev_ret) { |
| 305 | orig_prev = prev; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 306 | while (prev && offset > prev_entry->end) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 307 | prev = rb_next(prev); |
| 308 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| 309 | } |
| 310 | *prev_ret = prev; |
| 311 | prev = orig_prev; |
| 312 | } |
| 313 | |
| 314 | if (next_ret) { |
| 315 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 316 | while (prev && offset < prev_entry->start) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 317 | prev = rb_prev(prev); |
| 318 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| 319 | } |
| 320 | *next_ret = prev; |
| 321 | } |
| 322 | return NULL; |
| 323 | } |
| 324 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 325 | static inline struct rb_node * |
| 326 | tree_search_for_insert(struct extent_io_tree *tree, |
| 327 | u64 offset, |
| 328 | struct rb_node ***p_ret, |
| 329 | struct rb_node **parent_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 330 | { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 331 | struct rb_node *prev = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 332 | struct rb_node *ret; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 333 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 334 | ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 335 | if (!ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 336 | return prev; |
| 337 | return ret; |
| 338 | } |
| 339 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 340 | static inline struct rb_node *tree_search(struct extent_io_tree *tree, |
| 341 | u64 offset) |
| 342 | { |
| 343 | return tree_search_for_insert(tree, offset, NULL, NULL); |
| 344 | } |
| 345 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 346 | static void merge_cb(struct extent_io_tree *tree, struct extent_state *new, |
| 347 | struct extent_state *other) |
| 348 | { |
| 349 | if (tree->ops && tree->ops->merge_extent_hook) |
| 350 | tree->ops->merge_extent_hook(tree->mapping->host, new, |
| 351 | other); |
| 352 | } |
| 353 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 354 | /* |
| 355 | * utility function to look for merge candidates inside a given range. |
| 356 | * Any extents with matching state are merged together into a single |
| 357 | * extent in the tree. Extents with EXTENT_IO in their state field |
| 358 | * are not merged because the end_io handlers need to be able to do |
| 359 | * operations on them without sleeping (or doing allocations/splits). |
| 360 | * |
| 361 | * This should be called with the tree lock held. |
| 362 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 363 | static void merge_state(struct extent_io_tree *tree, |
| 364 | struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 365 | { |
| 366 | struct extent_state *other; |
| 367 | struct rb_node *other_node; |
| 368 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 369 | if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 370 | return; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 371 | |
| 372 | other_node = rb_prev(&state->rb_node); |
| 373 | if (other_node) { |
| 374 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 375 | if (other->end == state->start - 1 && |
| 376 | other->state == state->state) { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 377 | merge_cb(tree, state, other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 378 | state->start = other->start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 379 | rb_erase(&other->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 380 | RB_CLEAR_NODE(&other->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 381 | free_extent_state(other); |
| 382 | } |
| 383 | } |
| 384 | other_node = rb_next(&state->rb_node); |
| 385 | if (other_node) { |
| 386 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 387 | if (other->start == state->end + 1 && |
| 388 | other->state == state->state) { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 389 | merge_cb(tree, state, other); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 390 | state->end = other->end; |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 391 | rb_erase(&other->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 392 | RB_CLEAR_NODE(&other->rb_node); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 393 | free_extent_state(other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 394 | } |
| 395 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 396 | } |
| 397 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 398 | static void set_state_cb(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) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 400 | { |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 401 | if (tree->ops && tree->ops->set_bit_hook) |
| 402 | tree->ops->set_bit_hook(tree->mapping->host, state, bits); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | static void clear_state_cb(struct extent_io_tree *tree, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 406 | struct extent_state *state, unsigned long *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 407 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 408 | if (tree->ops && tree->ops->clear_bit_hook) |
| 409 | tree->ops->clear_bit_hook(tree->mapping->host, state, bits); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 410 | } |
| 411 | |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 412 | static void set_state_bits(struct extent_io_tree *tree, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 413 | struct extent_state *state, unsigned long *bits); |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 414 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 415 | /* |
| 416 | * insert an extent_state struct into the tree. 'bits' are set on the |
| 417 | * struct before it is inserted. |
| 418 | * |
| 419 | * This may return -EEXIST if the extent is already there, in which case the |
| 420 | * state struct is freed. |
| 421 | * |
| 422 | * The tree lock is not taken internally. This is a utility function and |
| 423 | * probably isn't what you want to call (see set/clear_extent_bit). |
| 424 | */ |
| 425 | static int insert_state(struct extent_io_tree *tree, |
| 426 | struct extent_state *state, u64 start, u64 end, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 427 | struct rb_node ***p, |
| 428 | struct rb_node **parent, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 429 | unsigned long *bits) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 430 | { |
| 431 | struct rb_node *node; |
| 432 | |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 433 | if (end < start) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 434 | WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 435 | end, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 436 | state->start = start; |
| 437 | state->end = end; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 438 | |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 439 | set_state_bits(tree, state, bits); |
| 440 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 441 | node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 442 | if (node) { |
| 443 | struct extent_state *found; |
| 444 | found = rb_entry(node, struct extent_state, rb_node); |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 445 | printk(KERN_ERR "BTRFS: found node %llu %llu on insert of " |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 446 | "%llu %llu\n", |
| 447 | found->start, found->end, start, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 448 | return -EEXIST; |
| 449 | } |
| 450 | merge_state(tree, state); |
| 451 | return 0; |
| 452 | } |
| 453 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 454 | 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] | 455 | u64 split) |
| 456 | { |
| 457 | if (tree->ops && tree->ops->split_extent_hook) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 458 | tree->ops->split_extent_hook(tree->mapping->host, orig, split); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 459 | } |
| 460 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 461 | /* |
| 462 | * split a given extent state struct in two, inserting the preallocated |
| 463 | * struct 'prealloc' as the newly created second half. 'split' indicates an |
| 464 | * offset inside 'orig' where it should be split. |
| 465 | * |
| 466 | * Before calling, |
| 467 | * the tree has 'orig' at [orig->start, orig->end]. After calling, there |
| 468 | * are two extent state structs in the tree: |
| 469 | * prealloc: [orig->start, split - 1] |
| 470 | * orig: [ split, orig->end ] |
| 471 | * |
| 472 | * The tree locks are not taken by this function. They need to be held |
| 473 | * by the caller. |
| 474 | */ |
| 475 | static int split_state(struct extent_io_tree *tree, struct extent_state *orig, |
| 476 | struct extent_state *prealloc, u64 split) |
| 477 | { |
| 478 | struct rb_node *node; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 479 | |
| 480 | split_cb(tree, orig, split); |
| 481 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 482 | prealloc->start = orig->start; |
| 483 | prealloc->end = split - 1; |
| 484 | prealloc->state = orig->state; |
| 485 | orig->start = split; |
| 486 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 487 | node = tree_insert(&tree->state, &orig->rb_node, prealloc->end, |
| 488 | &prealloc->rb_node, NULL, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 489 | if (node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 490 | free_extent_state(prealloc); |
| 491 | return -EEXIST; |
| 492 | } |
| 493 | return 0; |
| 494 | } |
| 495 | |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 496 | static struct extent_state *next_state(struct extent_state *state) |
| 497 | { |
| 498 | struct rb_node *next = rb_next(&state->rb_node); |
| 499 | if (next) |
| 500 | return rb_entry(next, struct extent_state, rb_node); |
| 501 | else |
| 502 | return NULL; |
| 503 | } |
| 504 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 505 | /* |
| 506 | * utility function to clear some bits in an extent state struct. |
Wang Sheng-Hui | 1b303fc | 2012-04-06 14:35:18 +0800 | [diff] [blame] | 507 | * 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] | 508 | * |
| 509 | * If no bits are set on the state struct after clearing things, the |
| 510 | * struct is freed and removed from the tree |
| 511 | */ |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 512 | static struct extent_state *clear_state_bit(struct extent_io_tree *tree, |
| 513 | struct extent_state *state, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 514 | unsigned long *bits, int wake) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 515 | { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 516 | struct extent_state *next; |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 517 | unsigned long bits_to_clear = *bits & ~EXTENT_CTLBITS; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 518 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 519 | if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 520 | u64 range = state->end - state->start + 1; |
| 521 | WARN_ON(range > tree->dirty_bytes); |
| 522 | tree->dirty_bytes -= range; |
| 523 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 524 | clear_state_cb(tree, state, bits); |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 525 | state->state &= ~bits_to_clear; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 526 | if (wake) |
| 527 | wake_up(&state->wq); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 528 | if (state->state == 0) { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 529 | next = next_state(state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 530 | if (extent_state_in_tree(state)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 531 | rb_erase(&state->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 532 | RB_CLEAR_NODE(&state->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 533 | free_extent_state(state); |
| 534 | } else { |
| 535 | WARN_ON(1); |
| 536 | } |
| 537 | } else { |
| 538 | merge_state(tree, state); |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 539 | next = next_state(state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 540 | } |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 541 | return next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 542 | } |
| 543 | |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 544 | static struct extent_state * |
| 545 | alloc_extent_state_atomic(struct extent_state *prealloc) |
| 546 | { |
| 547 | if (!prealloc) |
| 548 | prealloc = alloc_extent_state(GFP_ATOMIC); |
| 549 | |
| 550 | return prealloc; |
| 551 | } |
| 552 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 553 | 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] | 554 | { |
| 555 | btrfs_panic(tree_fs_info(tree), err, "Locking error: " |
| 556 | "Extent tree was modified by another " |
| 557 | "thread while locked."); |
| 558 | } |
| 559 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 560 | /* |
| 561 | * clear some bits on a range in the tree. This may require splitting |
| 562 | * or inserting elements in the tree, so the gfp mask is used to |
| 563 | * indicate which allocations or sleeping are allowed. |
| 564 | * |
| 565 | * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove |
| 566 | * the given range from the tree regardless of state (ie for truncate). |
| 567 | * |
| 568 | * the range [start, end] is inclusive. |
| 569 | * |
Jeff Mahoney | 6763af8 | 2012-03-01 14:56:29 +0100 | [diff] [blame] | 570 | * 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] | 571 | */ |
| 572 | 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] | 573 | unsigned long bits, int wake, int delete, |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 574 | struct extent_state **cached_state, |
| 575 | gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 576 | { |
| 577 | struct extent_state *state; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 578 | struct extent_state *cached; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 579 | struct extent_state *prealloc = NULL; |
| 580 | struct rb_node *node; |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 581 | u64 last_end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 582 | int err; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 583 | int clear = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 584 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 585 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 586 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 587 | if (bits & EXTENT_DELALLOC) |
| 588 | bits |= EXTENT_NORESERVE; |
| 589 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 590 | if (delete) |
| 591 | bits |= ~EXTENT_CTLBITS; |
| 592 | bits |= EXTENT_FIRST_DELALLOC; |
| 593 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 594 | if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY)) |
| 595 | clear = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 596 | again: |
| 597 | if (!prealloc && (mask & __GFP_WAIT)) { |
| 598 | prealloc = alloc_extent_state(mask); |
| 599 | if (!prealloc) |
| 600 | return -ENOMEM; |
| 601 | } |
| 602 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 603 | spin_lock(&tree->lock); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 604 | if (cached_state) { |
| 605 | cached = *cached_state; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 606 | |
| 607 | if (clear) { |
| 608 | *cached_state = NULL; |
| 609 | cached_state = NULL; |
| 610 | } |
| 611 | |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 612 | if (cached && extent_state_in_tree(cached) && |
| 613 | cached->start <= start && cached->end > start) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 614 | if (clear) |
| 615 | atomic_dec(&cached->refs); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 616 | state = cached; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 617 | goto hit_next; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 618 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 619 | if (clear) |
| 620 | free_extent_state(cached); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 621 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 622 | /* |
| 623 | * this search will find the extents that end after |
| 624 | * our range starts |
| 625 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 626 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 627 | if (!node) |
| 628 | goto out; |
| 629 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 630 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 631 | if (state->start > end) |
| 632 | goto out; |
| 633 | WARN_ON(state->end < start); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 634 | last_end = state->end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 635 | |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 636 | /* the state doesn't have the wanted bits, go ahead */ |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 637 | if (!(state->state & bits)) { |
| 638 | state = next_state(state); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 639 | goto next; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 640 | } |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 641 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 642 | /* |
| 643 | * | ---- desired range ---- | |
| 644 | * | state | or |
| 645 | * | ------------- state -------------- | |
| 646 | * |
| 647 | * We need to split the extent we found, and may flip |
| 648 | * bits on second half. |
| 649 | * |
| 650 | * If the extent we found extends past our range, we |
| 651 | * just split and search again. It'll get split again |
| 652 | * the next time though. |
| 653 | * |
| 654 | * If the extent we found is inside our range, we clear |
| 655 | * the desired bit on it. |
| 656 | */ |
| 657 | |
| 658 | if (state->start < start) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 659 | prealloc = alloc_extent_state_atomic(prealloc); |
| 660 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 661 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 662 | if (err) |
| 663 | extent_io_tree_panic(tree, err); |
| 664 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 665 | prealloc = NULL; |
| 666 | if (err) |
| 667 | goto out; |
| 668 | if (state->end <= end) { |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 669 | state = clear_state_bit(tree, state, &bits, wake); |
| 670 | goto next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 671 | } |
| 672 | goto search_again; |
| 673 | } |
| 674 | /* |
| 675 | * | ---- desired range ---- | |
| 676 | * | state | |
| 677 | * We need to split the extent, and clear the bit |
| 678 | * on the first half |
| 679 | */ |
| 680 | if (state->start <= end && state->end > end) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 681 | prealloc = alloc_extent_state_atomic(prealloc); |
| 682 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 683 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 684 | if (err) |
| 685 | extent_io_tree_panic(tree, err); |
| 686 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 687 | if (wake) |
| 688 | wake_up(&state->wq); |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 689 | |
Jeff Mahoney | 6763af8 | 2012-03-01 14:56:29 +0100 | [diff] [blame] | 690 | clear_state_bit(tree, prealloc, &bits, wake); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 691 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 692 | prealloc = NULL; |
| 693 | goto out; |
| 694 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 695 | |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 696 | state = clear_state_bit(tree, state, &bits, wake); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 697 | next: |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 698 | if (last_end == (u64)-1) |
| 699 | goto out; |
| 700 | start = last_end + 1; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 701 | if (start <= end && state && !need_resched()) |
Liu Bo | 692e575 | 2012-02-16 18:34:36 +0800 | [diff] [blame] | 702 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 703 | goto search_again; |
| 704 | |
| 705 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 706 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 707 | if (prealloc) |
| 708 | free_extent_state(prealloc); |
| 709 | |
Jeff Mahoney | 6763af8 | 2012-03-01 14:56:29 +0100 | [diff] [blame] | 710 | return 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 711 | |
| 712 | search_again: |
| 713 | if (start > end) |
| 714 | goto out; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 715 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 716 | if (mask & __GFP_WAIT) |
| 717 | cond_resched(); |
| 718 | goto again; |
| 719 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 720 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 721 | static void wait_on_state(struct extent_io_tree *tree, |
| 722 | struct extent_state *state) |
Christoph Hellwig | 641f521 | 2008-12-02 06:36:10 -0500 | [diff] [blame] | 723 | __releases(tree->lock) |
| 724 | __acquires(tree->lock) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 725 | { |
| 726 | DEFINE_WAIT(wait); |
| 727 | prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 728 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 729 | schedule(); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 730 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 731 | finish_wait(&state->wq, &wait); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /* |
| 735 | * waits for one or more bits to clear on a range in the state tree. |
| 736 | * The range [start, end] is inclusive. |
| 737 | * The tree lock is taken by this function |
| 738 | */ |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 739 | static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 740 | unsigned long bits) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 741 | { |
| 742 | struct extent_state *state; |
| 743 | struct rb_node *node; |
| 744 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 745 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 746 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 747 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 748 | again: |
| 749 | while (1) { |
| 750 | /* |
| 751 | * this search will find all the extents that end after |
| 752 | * our range starts |
| 753 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 754 | node = tree_search(tree, start); |
Filipe Manana | c50d3e7 | 2014-03-31 14:53:25 +0100 | [diff] [blame] | 755 | process_node: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 756 | if (!node) |
| 757 | break; |
| 758 | |
| 759 | state = rb_entry(node, struct extent_state, rb_node); |
| 760 | |
| 761 | if (state->start > end) |
| 762 | goto out; |
| 763 | |
| 764 | if (state->state & bits) { |
| 765 | start = state->start; |
| 766 | atomic_inc(&state->refs); |
| 767 | wait_on_state(tree, state); |
| 768 | free_extent_state(state); |
| 769 | goto again; |
| 770 | } |
| 771 | start = state->end + 1; |
| 772 | |
| 773 | if (start > end) |
| 774 | break; |
| 775 | |
Filipe Manana | c50d3e7 | 2014-03-31 14:53:25 +0100 | [diff] [blame] | 776 | if (!cond_resched_lock(&tree->lock)) { |
| 777 | node = rb_next(node); |
| 778 | goto process_node; |
| 779 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 780 | } |
| 781 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 782 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 783 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 784 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 785 | static void set_state_bits(struct extent_io_tree *tree, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 786 | struct extent_state *state, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 787 | unsigned long *bits) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 788 | { |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 789 | unsigned long bits_to_set = *bits & ~EXTENT_CTLBITS; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 790 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 791 | set_state_cb(tree, state, bits); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 792 | if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 793 | u64 range = state->end - state->start + 1; |
| 794 | tree->dirty_bytes += range; |
| 795 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 796 | state->state |= bits_to_set; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 797 | } |
| 798 | |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 799 | static void cache_state_if_flags(struct extent_state *state, |
| 800 | struct extent_state **cached_ptr, |
| 801 | const u64 flags) |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 802 | { |
| 803 | if (cached_ptr && !(*cached_ptr)) { |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 804 | if (!flags || (state->state & flags)) { |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 805 | *cached_ptr = state; |
| 806 | atomic_inc(&state->refs); |
| 807 | } |
| 808 | } |
| 809 | } |
| 810 | |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 811 | static void cache_state(struct extent_state *state, |
| 812 | struct extent_state **cached_ptr) |
| 813 | { |
| 814 | return cache_state_if_flags(state, cached_ptr, |
| 815 | EXTENT_IOBITS | EXTENT_BOUNDARY); |
| 816 | } |
| 817 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 818 | /* |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 819 | * set some bits on a range in the tree. This may require allocations or |
| 820 | * sleeping, so the gfp mask is used to indicate what is allowed. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 821 | * |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 822 | * If any of the exclusive bits are set, this will fail with -EEXIST if some |
| 823 | * part of the range already has the desired bits set. The start of the |
| 824 | * existing range is returned in failed_start in this case. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 825 | * |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 826 | * [start, end] is inclusive This takes the tree lock. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 827 | */ |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 828 | |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 829 | static int __must_check |
| 830 | __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 831 | unsigned long bits, unsigned long exclusive_bits, |
| 832 | u64 *failed_start, struct extent_state **cached_state, |
| 833 | gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 834 | { |
| 835 | struct extent_state *state; |
| 836 | struct extent_state *prealloc = NULL; |
| 837 | struct rb_node *node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 838 | struct rb_node **p; |
| 839 | struct rb_node *parent; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 840 | int err = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 841 | u64 last_start; |
| 842 | u64 last_end; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 843 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 844 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 845 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 846 | bits |= EXTENT_FIRST_DELALLOC; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 847 | again: |
| 848 | if (!prealloc && (mask & __GFP_WAIT)) { |
| 849 | prealloc = alloc_extent_state(mask); |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 850 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 851 | } |
| 852 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 853 | spin_lock(&tree->lock); |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 854 | if (cached_state && *cached_state) { |
| 855 | state = *cached_state; |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 856 | if (state->start <= start && state->end > start && |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 857 | extent_state_in_tree(state)) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 858 | node = &state->rb_node; |
| 859 | goto hit_next; |
| 860 | } |
| 861 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 862 | /* |
| 863 | * this search will find all the extents that end after |
| 864 | * our range starts. |
| 865 | */ |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 866 | node = tree_search_for_insert(tree, start, &p, &parent); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 867 | if (!node) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 868 | prealloc = alloc_extent_state_atomic(prealloc); |
| 869 | BUG_ON(!prealloc); |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 870 | err = insert_state(tree, prealloc, start, end, |
| 871 | &p, &parent, &bits); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 872 | if (err) |
| 873 | extent_io_tree_panic(tree, err); |
| 874 | |
Filipe David Borba Manana | c42ac0b | 2013-11-26 15:01:34 +0000 | [diff] [blame] | 875 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 876 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 877 | goto out; |
| 878 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 879 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 40431d6 | 2009-08-05 12:57:59 -0400 | [diff] [blame] | 880 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 881 | last_start = state->start; |
| 882 | last_end = state->end; |
| 883 | |
| 884 | /* |
| 885 | * | ---- desired range ---- | |
| 886 | * | state | |
| 887 | * |
| 888 | * Just lock what we found and keep going |
| 889 | */ |
| 890 | if (state->start == start && state->end <= end) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 891 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 892 | *failed_start = state->start; |
| 893 | err = -EEXIST; |
| 894 | goto out; |
| 895 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 896 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 897 | set_state_bits(tree, state, &bits); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 898 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 899 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 900 | if (last_end == (u64)-1) |
| 901 | goto out; |
| 902 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 903 | state = next_state(state); |
| 904 | if (start < end && state && state->start == start && |
| 905 | !need_resched()) |
| 906 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 907 | goto search_again; |
| 908 | } |
| 909 | |
| 910 | /* |
| 911 | * | ---- desired range ---- | |
| 912 | * | state | |
| 913 | * or |
| 914 | * | ------------- state -------------- | |
| 915 | * |
| 916 | * We need to split the extent we found, and may flip bits on |
| 917 | * second half. |
| 918 | * |
| 919 | * If the extent we found extends past our |
| 920 | * range, we just split and search again. It'll get split |
| 921 | * again the next time though. |
| 922 | * |
| 923 | * If the extent we found is inside our range, we set the |
| 924 | * desired bit on it. |
| 925 | */ |
| 926 | if (state->start < start) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 927 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 928 | *failed_start = start; |
| 929 | err = -EEXIST; |
| 930 | goto out; |
| 931 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 932 | |
| 933 | prealloc = alloc_extent_state_atomic(prealloc); |
| 934 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 935 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 936 | if (err) |
| 937 | extent_io_tree_panic(tree, err); |
| 938 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 939 | prealloc = NULL; |
| 940 | if (err) |
| 941 | goto out; |
| 942 | if (state->end <= end) { |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 943 | set_state_bits(tree, state, &bits); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 944 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 945 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 946 | if (last_end == (u64)-1) |
| 947 | goto out; |
| 948 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 949 | state = next_state(state); |
| 950 | if (start < end && state && state->start == start && |
| 951 | !need_resched()) |
| 952 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 953 | } |
| 954 | goto search_again; |
| 955 | } |
| 956 | /* |
| 957 | * | ---- desired range ---- | |
| 958 | * | state | or | state | |
| 959 | * |
| 960 | * There's a hole, we need to insert something in it and |
| 961 | * ignore the extent we found. |
| 962 | */ |
| 963 | if (state->start > start) { |
| 964 | u64 this_end; |
| 965 | if (end < last_start) |
| 966 | this_end = end; |
| 967 | else |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 968 | this_end = last_start - 1; |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 969 | |
| 970 | prealloc = alloc_extent_state_atomic(prealloc); |
| 971 | BUG_ON(!prealloc); |
Xiao Guangrong | c7f895a | 2011-04-20 06:45:49 +0000 | [diff] [blame] | 972 | |
| 973 | /* |
| 974 | * Avoid to free 'prealloc' if it can be merged with |
| 975 | * the later extent. |
| 976 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 977 | err = insert_state(tree, prealloc, start, this_end, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 978 | NULL, NULL, &bits); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 979 | if (err) |
| 980 | extent_io_tree_panic(tree, err); |
| 981 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 982 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 983 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 984 | start = this_end + 1; |
| 985 | goto search_again; |
| 986 | } |
| 987 | /* |
| 988 | * | ---- desired range ---- | |
| 989 | * | state | |
| 990 | * We need to split the extent, and set the bit |
| 991 | * on the first half |
| 992 | */ |
| 993 | if (state->start <= end && state->end > end) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 994 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 995 | *failed_start = start; |
| 996 | err = -EEXIST; |
| 997 | goto out; |
| 998 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 999 | |
| 1000 | prealloc = alloc_extent_state_atomic(prealloc); |
| 1001 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1002 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1003 | if (err) |
| 1004 | extent_io_tree_panic(tree, err); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1005 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1006 | set_state_bits(tree, prealloc, &bits); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1007 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1008 | merge_state(tree, prealloc); |
| 1009 | prealloc = NULL; |
| 1010 | goto out; |
| 1011 | } |
| 1012 | |
| 1013 | goto search_again; |
| 1014 | |
| 1015 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1016 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1017 | if (prealloc) |
| 1018 | free_extent_state(prealloc); |
| 1019 | |
| 1020 | return err; |
| 1021 | |
| 1022 | search_again: |
| 1023 | if (start > end) |
| 1024 | goto out; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1025 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1026 | if (mask & __GFP_WAIT) |
| 1027 | cond_resched(); |
| 1028 | goto again; |
| 1029 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1030 | |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1031 | int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 1032 | unsigned long bits, u64 * failed_start, |
| 1033 | struct extent_state **cached_state, gfp_t mask) |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1034 | { |
| 1035 | return __set_extent_bit(tree, start, end, bits, 0, failed_start, |
| 1036 | cached_state, mask); |
| 1037 | } |
| 1038 | |
| 1039 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1040 | /** |
Liu Bo | 10983f2 | 2012-07-11 15:26:19 +0800 | [diff] [blame] | 1041 | * convert_extent_bit - convert all bits in a given range from one bit to |
| 1042 | * another |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1043 | * @tree: the io tree to search |
| 1044 | * @start: the start offset in bytes |
| 1045 | * @end: the end offset in bytes (inclusive) |
| 1046 | * @bits: the bits to set in this range |
| 1047 | * @clear_bits: the bits to clear in this range |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1048 | * @cached_state: state that we're going to cache |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1049 | * @mask: the allocation mask |
| 1050 | * |
| 1051 | * This will go through and set bits for the given range. If any states exist |
| 1052 | * already in this range they are set with the given bit and cleared of the |
| 1053 | * clear_bits. This is only meant to be used by things that are mergeable, ie |
| 1054 | * converting from say DELALLOC to DIRTY. This is not meant to be used with |
| 1055 | * boundary bits like LOCK. |
| 1056 | */ |
| 1057 | 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] | 1058 | unsigned long bits, unsigned long clear_bits, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1059 | struct extent_state **cached_state, gfp_t mask) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1060 | { |
| 1061 | struct extent_state *state; |
| 1062 | struct extent_state *prealloc = NULL; |
| 1063 | struct rb_node *node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1064 | struct rb_node **p; |
| 1065 | struct rb_node *parent; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1066 | int err = 0; |
| 1067 | u64 last_start; |
| 1068 | u64 last_end; |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame^] | 1069 | bool first_iteration = true; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1070 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 1071 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 1072 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1073 | again: |
| 1074 | if (!prealloc && (mask & __GFP_WAIT)) { |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame^] | 1075 | /* |
| 1076 | * Best effort, don't worry if extent state allocation fails |
| 1077 | * here for the first iteration. We might have a cached state |
| 1078 | * that matches exactly the target range, in which case no |
| 1079 | * extent state allocations are needed. We'll only know this |
| 1080 | * after locking the tree. |
| 1081 | */ |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1082 | prealloc = alloc_extent_state(mask); |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame^] | 1083 | if (!prealloc && !first_iteration) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1084 | return -ENOMEM; |
| 1085 | } |
| 1086 | |
| 1087 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1088 | if (cached_state && *cached_state) { |
| 1089 | state = *cached_state; |
| 1090 | if (state->start <= start && state->end > start && |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1091 | extent_state_in_tree(state)) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1092 | node = &state->rb_node; |
| 1093 | goto hit_next; |
| 1094 | } |
| 1095 | } |
| 1096 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1097 | /* |
| 1098 | * this search will find all the extents that end after |
| 1099 | * our range starts. |
| 1100 | */ |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1101 | node = tree_search_for_insert(tree, start, &p, &parent); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1102 | if (!node) { |
| 1103 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1104 | if (!prealloc) { |
| 1105 | err = -ENOMEM; |
| 1106 | goto out; |
| 1107 | } |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1108 | err = insert_state(tree, prealloc, start, end, |
| 1109 | &p, &parent, &bits); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1110 | if (err) |
| 1111 | extent_io_tree_panic(tree, err); |
Filipe David Borba Manana | c42ac0b | 2013-11-26 15:01:34 +0000 | [diff] [blame] | 1112 | cache_state(prealloc, cached_state); |
| 1113 | prealloc = NULL; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1114 | goto out; |
| 1115 | } |
| 1116 | state = rb_entry(node, struct extent_state, rb_node); |
| 1117 | hit_next: |
| 1118 | last_start = state->start; |
| 1119 | last_end = state->end; |
| 1120 | |
| 1121 | /* |
| 1122 | * | ---- desired range ---- | |
| 1123 | * | state | |
| 1124 | * |
| 1125 | * Just lock what we found and keep going |
| 1126 | */ |
| 1127 | if (state->start == start && state->end <= end) { |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1128 | set_state_bits(tree, state, &bits); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1129 | cache_state(state, cached_state); |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1130 | state = clear_state_bit(tree, state, &clear_bits, 0); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1131 | if (last_end == (u64)-1) |
| 1132 | goto out; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1133 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1134 | if (start < end && state && state->start == start && |
| 1135 | !need_resched()) |
| 1136 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1137 | goto search_again; |
| 1138 | } |
| 1139 | |
| 1140 | /* |
| 1141 | * | ---- desired range ---- | |
| 1142 | * | state | |
| 1143 | * or |
| 1144 | * | ------------- state -------------- | |
| 1145 | * |
| 1146 | * We need to split the extent we found, and may flip bits on |
| 1147 | * second half. |
| 1148 | * |
| 1149 | * If the extent we found extends past our |
| 1150 | * range, we just split and search again. It'll get split |
| 1151 | * again the next time though. |
| 1152 | * |
| 1153 | * If the extent we found is inside our range, we set the |
| 1154 | * desired bit on it. |
| 1155 | */ |
| 1156 | if (state->start < start) { |
| 1157 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1158 | if (!prealloc) { |
| 1159 | err = -ENOMEM; |
| 1160 | goto out; |
| 1161 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1162 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1163 | if (err) |
| 1164 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1165 | prealloc = NULL; |
| 1166 | if (err) |
| 1167 | goto out; |
| 1168 | if (state->end <= end) { |
| 1169 | set_state_bits(tree, state, &bits); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1170 | cache_state(state, cached_state); |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1171 | state = clear_state_bit(tree, state, &clear_bits, 0); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1172 | if (last_end == (u64)-1) |
| 1173 | goto out; |
| 1174 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1175 | if (start < end && state && state->start == start && |
| 1176 | !need_resched()) |
| 1177 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1178 | } |
| 1179 | goto search_again; |
| 1180 | } |
| 1181 | /* |
| 1182 | * | ---- desired range ---- | |
| 1183 | * | state | or | state | |
| 1184 | * |
| 1185 | * There's a hole, we need to insert something in it and |
| 1186 | * ignore the extent we found. |
| 1187 | */ |
| 1188 | if (state->start > start) { |
| 1189 | u64 this_end; |
| 1190 | if (end < last_start) |
| 1191 | this_end = end; |
| 1192 | else |
| 1193 | this_end = last_start - 1; |
| 1194 | |
| 1195 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1196 | if (!prealloc) { |
| 1197 | err = -ENOMEM; |
| 1198 | goto out; |
| 1199 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1200 | |
| 1201 | /* |
| 1202 | * Avoid to free 'prealloc' if it can be merged with |
| 1203 | * the later extent. |
| 1204 | */ |
| 1205 | err = insert_state(tree, prealloc, start, this_end, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1206 | NULL, NULL, &bits); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1207 | if (err) |
| 1208 | extent_io_tree_panic(tree, err); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1209 | cache_state(prealloc, cached_state); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1210 | prealloc = NULL; |
| 1211 | start = this_end + 1; |
| 1212 | goto search_again; |
| 1213 | } |
| 1214 | /* |
| 1215 | * | ---- desired range ---- | |
| 1216 | * | state | |
| 1217 | * We need to split the extent, and set the bit |
| 1218 | * on the first half |
| 1219 | */ |
| 1220 | if (state->start <= end && state->end > end) { |
| 1221 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1222 | if (!prealloc) { |
| 1223 | err = -ENOMEM; |
| 1224 | goto out; |
| 1225 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1226 | |
| 1227 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1228 | if (err) |
| 1229 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1230 | |
| 1231 | set_state_bits(tree, prealloc, &bits); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1232 | cache_state(prealloc, cached_state); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1233 | clear_state_bit(tree, prealloc, &clear_bits, 0); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1234 | prealloc = NULL; |
| 1235 | goto out; |
| 1236 | } |
| 1237 | |
| 1238 | goto search_again; |
| 1239 | |
| 1240 | out: |
| 1241 | spin_unlock(&tree->lock); |
| 1242 | if (prealloc) |
| 1243 | free_extent_state(prealloc); |
| 1244 | |
| 1245 | return err; |
| 1246 | |
| 1247 | search_again: |
| 1248 | if (start > end) |
| 1249 | goto out; |
| 1250 | spin_unlock(&tree->lock); |
| 1251 | if (mask & __GFP_WAIT) |
| 1252 | cond_resched(); |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame^] | 1253 | first_iteration = false; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1254 | goto again; |
| 1255 | } |
| 1256 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1257 | /* wrappers around set/clear extent bit */ |
| 1258 | int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, |
| 1259 | gfp_t mask) |
| 1260 | { |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1261 | return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL, |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1262 | NULL, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1263 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1264 | |
| 1265 | 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] | 1266 | unsigned long bits, gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1267 | { |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1268 | return set_extent_bit(tree, start, end, bits, NULL, |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1269 | NULL, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1270 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1271 | |
| 1272 | 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] | 1273 | unsigned long bits, gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1274 | { |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1275 | return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1276 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1277 | |
| 1278 | 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] | 1279 | struct extent_state **cached_state, gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1280 | { |
| 1281 | return set_extent_bit(tree, start, end, |
Liu Bo | fee187d | 2011-09-29 15:55:28 +0800 | [diff] [blame] | 1282 | EXTENT_DELALLOC | EXTENT_UPTODATE, |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1283 | NULL, cached_state, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1284 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1285 | |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 1286 | int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end, |
| 1287 | struct extent_state **cached_state, gfp_t mask) |
| 1288 | { |
| 1289 | return set_extent_bit(tree, start, end, |
| 1290 | EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG, |
| 1291 | NULL, cached_state, mask); |
| 1292 | } |
| 1293 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1294 | int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, |
| 1295 | gfp_t mask) |
| 1296 | { |
| 1297 | return clear_extent_bit(tree, start, end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1298 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1299 | EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1300 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1301 | |
| 1302 | int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end, |
| 1303 | gfp_t mask) |
| 1304 | { |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1305 | return set_extent_bit(tree, start, end, EXTENT_NEW, NULL, |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1306 | NULL, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1307 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1308 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1309 | 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] | 1310 | struct extent_state **cached_state, gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1311 | { |
Liu Bo | 6b67a32 | 2013-03-28 08:30:28 +0000 | [diff] [blame] | 1312 | return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL, |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1313 | cached_state, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1314 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1315 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1316 | int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end, |
| 1317 | struct extent_state **cached_state, gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1318 | { |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1319 | return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1320 | cached_state, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1321 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1322 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1323 | /* |
| 1324 | * either insert or lock state struct between start and end use mask to tell |
| 1325 | * us if waiting is desired. |
| 1326 | */ |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1327 | 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] | 1328 | unsigned long bits, struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1329 | { |
| 1330 | int err; |
| 1331 | u64 failed_start; |
| 1332 | while (1) { |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1333 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits, |
| 1334 | EXTENT_LOCKED, &failed_start, |
| 1335 | cached_state, GFP_NOFS); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1336 | if (err == -EEXIST) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1337 | wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED); |
| 1338 | start = failed_start; |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1339 | } else |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1340 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1341 | WARN_ON(start > end); |
| 1342 | } |
| 1343 | return err; |
| 1344 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1345 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1346 | int lock_extent(struct extent_io_tree *tree, u64 start, u64 end) |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1347 | { |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1348 | return lock_extent_bits(tree, start, end, 0, NULL); |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1349 | } |
| 1350 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1351 | 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] | 1352 | { |
| 1353 | int err; |
| 1354 | u64 failed_start; |
| 1355 | |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1356 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED, |
| 1357 | &failed_start, NULL, GFP_NOFS); |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1358 | if (err == -EEXIST) { |
| 1359 | if (failed_start > start) |
| 1360 | clear_extent_bit(tree, start, failed_start - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1361 | EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1362 | return 0; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1363 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1364 | return 1; |
| 1365 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1366 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1367 | int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end, |
| 1368 | struct extent_state **cached, gfp_t mask) |
| 1369 | { |
| 1370 | return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached, |
| 1371 | mask); |
| 1372 | } |
| 1373 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1374 | int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1375 | { |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1376 | return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1377 | GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1378 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1379 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1380 | int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end) |
| 1381 | { |
| 1382 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1383 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1384 | struct page *page; |
| 1385 | |
| 1386 | while (index <= end_index) { |
| 1387 | page = find_get_page(inode->i_mapping, index); |
| 1388 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
| 1389 | clear_page_dirty_for_io(page); |
| 1390 | page_cache_release(page); |
| 1391 | index++; |
| 1392 | } |
| 1393 | return 0; |
| 1394 | } |
| 1395 | |
| 1396 | int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end) |
| 1397 | { |
| 1398 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1399 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1400 | struct page *page; |
| 1401 | |
| 1402 | while (index <= end_index) { |
| 1403 | page = find_get_page(inode->i_mapping, index); |
| 1404 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
| 1405 | account_page_redirty(page); |
| 1406 | __set_page_dirty_nobuffers(page); |
| 1407 | page_cache_release(page); |
| 1408 | index++; |
| 1409 | } |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1413 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1414 | * helper function to set both pages and extents in the tree writeback |
| 1415 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1416 | 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] | 1417 | { |
| 1418 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1419 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1420 | struct page *page; |
| 1421 | |
| 1422 | while (index <= end_index) { |
| 1423 | page = find_get_page(tree->mapping, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1424 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1425 | set_page_writeback(page); |
| 1426 | page_cache_release(page); |
| 1427 | index++; |
| 1428 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1429 | return 0; |
| 1430 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1431 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1432 | /* find the first state struct with 'bits' set after 'start', and |
| 1433 | * return it. tree->lock must be held. NULL will returned if |
| 1434 | * nothing was found after 'start' |
| 1435 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1436 | static struct extent_state * |
| 1437 | find_first_extent_bit_state(struct extent_io_tree *tree, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1438 | u64 start, unsigned long bits) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1439 | { |
| 1440 | struct rb_node *node; |
| 1441 | struct extent_state *state; |
| 1442 | |
| 1443 | /* |
| 1444 | * this search will find all the extents that end after |
| 1445 | * our range starts. |
| 1446 | */ |
| 1447 | node = tree_search(tree, start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1448 | if (!node) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1449 | goto out; |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1450 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1451 | while (1) { |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1452 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1453 | if (state->end >= start && (state->state & bits)) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1454 | return state; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1455 | |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1456 | node = rb_next(node); |
| 1457 | if (!node) |
| 1458 | break; |
| 1459 | } |
| 1460 | out: |
| 1461 | return NULL; |
| 1462 | } |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1463 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1464 | /* |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1465 | * find the first offset in the io tree with 'bits' set. zero is |
| 1466 | * returned if we find something, and *start_ret and *end_ret are |
| 1467 | * set to reflect the state struct that was found. |
| 1468 | * |
Wang Sheng-Hui | 477d7ea | 2012-04-06 14:35:47 +0800 | [diff] [blame] | 1469 | * If nothing was found, 1 is returned. If found something, return 0. |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1470 | */ |
| 1471 | int find_first_extent_bit(struct extent_io_tree *tree, u64 start, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1472 | u64 *start_ret, u64 *end_ret, unsigned long bits, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1473 | struct extent_state **cached_state) |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1474 | { |
| 1475 | struct extent_state *state; |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1476 | struct rb_node *n; |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1477 | int ret = 1; |
| 1478 | |
| 1479 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1480 | if (cached_state && *cached_state) { |
| 1481 | state = *cached_state; |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1482 | if (state->end == start - 1 && extent_state_in_tree(state)) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1483 | n = rb_next(&state->rb_node); |
| 1484 | while (n) { |
| 1485 | state = rb_entry(n, struct extent_state, |
| 1486 | rb_node); |
| 1487 | if (state->state & bits) |
| 1488 | goto got_it; |
| 1489 | n = rb_next(n); |
| 1490 | } |
| 1491 | free_extent_state(*cached_state); |
| 1492 | *cached_state = NULL; |
| 1493 | goto out; |
| 1494 | } |
| 1495 | free_extent_state(*cached_state); |
| 1496 | *cached_state = NULL; |
| 1497 | } |
| 1498 | |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1499 | state = find_first_extent_bit_state(tree, start, bits); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1500 | got_it: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1501 | if (state) { |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 1502 | cache_state_if_flags(state, cached_state, 0); |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1503 | *start_ret = state->start; |
| 1504 | *end_ret = state->end; |
| 1505 | ret = 0; |
| 1506 | } |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1507 | out: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1508 | spin_unlock(&tree->lock); |
| 1509 | return ret; |
| 1510 | } |
| 1511 | |
| 1512 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1513 | * find a contiguous range of bytes in the file marked as delalloc, not |
| 1514 | * more than 'max_bytes'. start and end are used to return the range, |
| 1515 | * |
| 1516 | * 1 is returned if we find something, 0 if nothing was in the tree |
| 1517 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1518 | static noinline u64 find_delalloc_range(struct extent_io_tree *tree, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1519 | u64 *start, u64 *end, u64 max_bytes, |
| 1520 | struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1521 | { |
| 1522 | struct rb_node *node; |
| 1523 | struct extent_state *state; |
| 1524 | u64 cur_start = *start; |
| 1525 | u64 found = 0; |
| 1526 | u64 total_bytes = 0; |
| 1527 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1528 | spin_lock(&tree->lock); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1529 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1530 | /* |
| 1531 | * this search will find all the extents that end after |
| 1532 | * our range starts. |
| 1533 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1534 | node = tree_search(tree, cur_start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1535 | if (!node) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1536 | if (!found) |
| 1537 | *end = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1538 | goto out; |
| 1539 | } |
| 1540 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1541 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1542 | state = rb_entry(node, struct extent_state, rb_node); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1543 | if (found && (state->start != cur_start || |
| 1544 | (state->state & EXTENT_BOUNDARY))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1545 | goto out; |
| 1546 | } |
| 1547 | if (!(state->state & EXTENT_DELALLOC)) { |
| 1548 | if (!found) |
| 1549 | *end = state->end; |
| 1550 | goto out; |
| 1551 | } |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1552 | if (!found) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1553 | *start = state->start; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1554 | *cached_state = state; |
| 1555 | atomic_inc(&state->refs); |
| 1556 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1557 | found++; |
| 1558 | *end = state->end; |
| 1559 | cur_start = state->end + 1; |
| 1560 | node = rb_next(node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1561 | total_bytes += state->end - state->start + 1; |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 1562 | if (total_bytes >= max_bytes) |
Josef Bacik | 573aeca | 2013-08-30 14:38:49 -0400 | [diff] [blame] | 1563 | break; |
Josef Bacik | 573aeca | 2013-08-30 14:38:49 -0400 | [diff] [blame] | 1564 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1565 | break; |
| 1566 | } |
| 1567 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1568 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1569 | return found; |
| 1570 | } |
| 1571 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1572 | static noinline void __unlock_for_delalloc(struct inode *inode, |
| 1573 | struct page *locked_page, |
| 1574 | u64 start, u64 end) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1575 | { |
| 1576 | int ret; |
| 1577 | struct page *pages[16]; |
| 1578 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1579 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1580 | unsigned long nr_pages = end_index - index + 1; |
| 1581 | int i; |
| 1582 | |
| 1583 | if (index == locked_page->index && end_index == index) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1584 | return; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1585 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1586 | while (nr_pages > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1587 | ret = find_get_pages_contig(inode->i_mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1588 | min_t(unsigned long, nr_pages, |
| 1589 | ARRAY_SIZE(pages)), pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1590 | for (i = 0; i < ret; i++) { |
| 1591 | if (pages[i] != locked_page) |
| 1592 | unlock_page(pages[i]); |
| 1593 | page_cache_release(pages[i]); |
| 1594 | } |
| 1595 | nr_pages -= ret; |
| 1596 | index += ret; |
| 1597 | cond_resched(); |
| 1598 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | static noinline int lock_delalloc_pages(struct inode *inode, |
| 1602 | struct page *locked_page, |
| 1603 | u64 delalloc_start, |
| 1604 | u64 delalloc_end) |
| 1605 | { |
| 1606 | unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT; |
| 1607 | unsigned long start_index = index; |
| 1608 | unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT; |
| 1609 | unsigned long pages_locked = 0; |
| 1610 | struct page *pages[16]; |
| 1611 | unsigned long nrpages; |
| 1612 | int ret; |
| 1613 | int i; |
| 1614 | |
| 1615 | /* the caller is responsible for locking the start index */ |
| 1616 | if (index == locked_page->index && index == end_index) |
| 1617 | return 0; |
| 1618 | |
| 1619 | /* skip the page at the start index */ |
| 1620 | nrpages = end_index - index + 1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1621 | while (nrpages > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1622 | ret = find_get_pages_contig(inode->i_mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1623 | min_t(unsigned long, |
| 1624 | nrpages, ARRAY_SIZE(pages)), pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1625 | if (ret == 0) { |
| 1626 | ret = -EAGAIN; |
| 1627 | goto done; |
| 1628 | } |
| 1629 | /* now we have an array of pages, lock them all */ |
| 1630 | for (i = 0; i < ret; i++) { |
| 1631 | /* |
| 1632 | * the caller is taking responsibility for |
| 1633 | * locked_page |
| 1634 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1635 | if (pages[i] != locked_page) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1636 | lock_page(pages[i]); |
Chris Mason | f2b1c41 | 2008-11-10 07:31:30 -0500 | [diff] [blame] | 1637 | if (!PageDirty(pages[i]) || |
| 1638 | pages[i]->mapping != inode->i_mapping) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1639 | ret = -EAGAIN; |
| 1640 | unlock_page(pages[i]); |
| 1641 | page_cache_release(pages[i]); |
| 1642 | goto done; |
| 1643 | } |
| 1644 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1645 | page_cache_release(pages[i]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1646 | pages_locked++; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1647 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1648 | nrpages -= ret; |
| 1649 | index += ret; |
| 1650 | cond_resched(); |
| 1651 | } |
| 1652 | ret = 0; |
| 1653 | done: |
| 1654 | if (ret && pages_locked) { |
| 1655 | __unlock_for_delalloc(inode, locked_page, |
| 1656 | delalloc_start, |
| 1657 | ((u64)(start_index + pages_locked - 1)) << |
| 1658 | PAGE_CACHE_SHIFT); |
| 1659 | } |
| 1660 | return ret; |
| 1661 | } |
| 1662 | |
| 1663 | /* |
| 1664 | * find a contiguous range of bytes in the file marked as delalloc, not |
| 1665 | * more than 'max_bytes'. start and end are used to return the range, |
| 1666 | * |
| 1667 | * 1 is returned if we find something, 0 if nothing was in the tree |
| 1668 | */ |
Josef Bacik | 294e30f | 2013-10-09 12:00:56 -0400 | [diff] [blame] | 1669 | STATIC u64 find_lock_delalloc_range(struct inode *inode, |
| 1670 | struct extent_io_tree *tree, |
| 1671 | struct page *locked_page, u64 *start, |
| 1672 | u64 *end, u64 max_bytes) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1673 | { |
| 1674 | u64 delalloc_start; |
| 1675 | u64 delalloc_end; |
| 1676 | u64 found; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1677 | struct extent_state *cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1678 | int ret; |
| 1679 | int loops = 0; |
| 1680 | |
| 1681 | again: |
| 1682 | /* step one, find a bunch of delalloc bytes starting at start */ |
| 1683 | delalloc_start = *start; |
| 1684 | delalloc_end = 0; |
| 1685 | found = find_delalloc_range(tree, &delalloc_start, &delalloc_end, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1686 | max_bytes, &cached_state); |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1687 | if (!found || delalloc_end <= *start) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1688 | *start = delalloc_start; |
| 1689 | *end = delalloc_end; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1690 | free_extent_state(cached_state); |
Liu Bo | 385fe0b | 2013-10-01 23:49:49 +0800 | [diff] [blame] | 1691 | return 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | /* |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1695 | * start comes from the offset of locked_page. We have to lock |
| 1696 | * pages in order, so we can't process delalloc bytes before |
| 1697 | * locked_page |
| 1698 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1699 | if (delalloc_start < *start) |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1700 | delalloc_start = *start; |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1701 | |
| 1702 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1703 | * 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] | 1704 | */ |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 1705 | if (delalloc_end + 1 - delalloc_start > max_bytes) |
| 1706 | delalloc_end = delalloc_start + max_bytes - 1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1707 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1708 | /* step two, lock all the pages after the page that has start */ |
| 1709 | ret = lock_delalloc_pages(inode, locked_page, |
| 1710 | delalloc_start, delalloc_end); |
| 1711 | if (ret == -EAGAIN) { |
| 1712 | /* some of the pages are gone, lets avoid looping by |
| 1713 | * shortening the size of the delalloc range we're searching |
| 1714 | */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1715 | free_extent_state(cached_state); |
Chris Mason | 7d78874 | 2014-05-21 05:49:54 -0700 | [diff] [blame] | 1716 | cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1717 | if (!loops) { |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 1718 | max_bytes = PAGE_CACHE_SIZE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1719 | loops = 1; |
| 1720 | goto again; |
| 1721 | } else { |
| 1722 | found = 0; |
| 1723 | goto out_failed; |
| 1724 | } |
| 1725 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1726 | BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1727 | |
| 1728 | /* step three, lock the state bits for the whole range */ |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1729 | lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1730 | |
| 1731 | /* then test to make sure it is all still delalloc */ |
| 1732 | ret = test_range_bit(tree, delalloc_start, delalloc_end, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1733 | EXTENT_DELALLOC, 1, cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1734 | if (!ret) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1735 | unlock_extent_cached(tree, delalloc_start, delalloc_end, |
| 1736 | &cached_state, GFP_NOFS); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1737 | __unlock_for_delalloc(inode, locked_page, |
| 1738 | delalloc_start, delalloc_end); |
| 1739 | cond_resched(); |
| 1740 | goto again; |
| 1741 | } |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1742 | free_extent_state(cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1743 | *start = delalloc_start; |
| 1744 | *end = delalloc_end; |
| 1745 | out_failed: |
| 1746 | return found; |
| 1747 | } |
| 1748 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1749 | int extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end, |
| 1750 | struct page *locked_page, |
| 1751 | unsigned long clear_bits, |
| 1752 | unsigned long page_ops) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1753 | { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1754 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1755 | int ret; |
| 1756 | struct page *pages[16]; |
| 1757 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1758 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1759 | unsigned long nr_pages = end_index - index + 1; |
| 1760 | int i; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1761 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1762 | 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] | 1763 | if (page_ops == 0) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1764 | return 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1765 | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1766 | if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0) |
| 1767 | mapping_set_error(inode->i_mapping, -EIO); |
| 1768 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1769 | while (nr_pages > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1770 | ret = find_get_pages_contig(inode->i_mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1771 | min_t(unsigned long, |
| 1772 | nr_pages, ARRAY_SIZE(pages)), pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1773 | for (i = 0; i < ret; i++) { |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1774 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1775 | if (page_ops & PAGE_SET_PRIVATE2) |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1776 | SetPagePrivate2(pages[i]); |
| 1777 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1778 | if (pages[i] == locked_page) { |
| 1779 | page_cache_release(pages[i]); |
| 1780 | continue; |
| 1781 | } |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1782 | if (page_ops & PAGE_CLEAR_DIRTY) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1783 | clear_page_dirty_for_io(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1784 | if (page_ops & PAGE_SET_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1785 | set_page_writeback(pages[i]); |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1786 | if (page_ops & PAGE_SET_ERROR) |
| 1787 | SetPageError(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1788 | if (page_ops & PAGE_END_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1789 | end_page_writeback(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1790 | if (page_ops & PAGE_UNLOCK) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1791 | unlock_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1792 | page_cache_release(pages[i]); |
| 1793 | } |
| 1794 | nr_pages -= ret; |
| 1795 | index += ret; |
| 1796 | cond_resched(); |
| 1797 | } |
| 1798 | return 0; |
| 1799 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1800 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1801 | /* |
| 1802 | * count the number of bytes in the tree that have a given bit(s) |
| 1803 | * set. This can be fairly slow, except for EXTENT_DIRTY which is |
| 1804 | * cached. The total number found is returned. |
| 1805 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1806 | u64 count_range_bits(struct extent_io_tree *tree, |
| 1807 | u64 *start, u64 search_end, u64 max_bytes, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1808 | unsigned long bits, int contig) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1809 | { |
| 1810 | struct rb_node *node; |
| 1811 | struct extent_state *state; |
| 1812 | u64 cur_start = *start; |
| 1813 | u64 total_bytes = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1814 | u64 last = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1815 | int found = 0; |
| 1816 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 1817 | if (WARN_ON(search_end <= cur_start)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1818 | return 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1819 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1820 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1821 | if (cur_start == 0 && bits == EXTENT_DIRTY) { |
| 1822 | total_bytes = tree->dirty_bytes; |
| 1823 | goto out; |
| 1824 | } |
| 1825 | /* |
| 1826 | * this search will find all the extents that end after |
| 1827 | * our range starts. |
| 1828 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1829 | node = tree_search(tree, cur_start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1830 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1831 | goto out; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1832 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1833 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1834 | state = rb_entry(node, struct extent_state, rb_node); |
| 1835 | if (state->start > search_end) |
| 1836 | break; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1837 | if (contig && found && state->start > last + 1) |
| 1838 | break; |
| 1839 | if (state->end >= cur_start && (state->state & bits) == bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1840 | total_bytes += min(search_end, state->end) + 1 - |
| 1841 | max(cur_start, state->start); |
| 1842 | if (total_bytes >= max_bytes) |
| 1843 | break; |
| 1844 | if (!found) { |
Josef Bacik | af60bed | 2011-05-04 11:11:17 -0400 | [diff] [blame] | 1845 | *start = max(cur_start, state->start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1846 | found = 1; |
| 1847 | } |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1848 | last = state->end; |
| 1849 | } else if (contig && found) { |
| 1850 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1851 | } |
| 1852 | node = rb_next(node); |
| 1853 | if (!node) |
| 1854 | break; |
| 1855 | } |
| 1856 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1857 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1858 | return total_bytes; |
| 1859 | } |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1860 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1861 | /* |
| 1862 | * set the private field for a given byte offset in the tree. If there isn't |
| 1863 | * an extent_state there already, this does nothing. |
| 1864 | */ |
Sergei Trofimovich | 171170c | 2013-08-14 23:27:46 +0300 | [diff] [blame] | 1865 | 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] | 1866 | { |
| 1867 | struct rb_node *node; |
| 1868 | struct extent_state *state; |
| 1869 | int ret = 0; |
| 1870 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1871 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1872 | /* |
| 1873 | * this search will find all the extents that end after |
| 1874 | * our range starts. |
| 1875 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1876 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1877 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1878 | ret = -ENOENT; |
| 1879 | goto out; |
| 1880 | } |
| 1881 | state = rb_entry(node, struct extent_state, rb_node); |
| 1882 | if (state->start != start) { |
| 1883 | ret = -ENOENT; |
| 1884 | goto out; |
| 1885 | } |
| 1886 | state->private = private; |
| 1887 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1888 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1889 | return ret; |
| 1890 | } |
| 1891 | |
| 1892 | int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private) |
| 1893 | { |
| 1894 | struct rb_node *node; |
| 1895 | struct extent_state *state; |
| 1896 | int ret = 0; |
| 1897 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1898 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1899 | /* |
| 1900 | * this search will find all the extents that end after |
| 1901 | * our range starts. |
| 1902 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1903 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1904 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1905 | ret = -ENOENT; |
| 1906 | goto out; |
| 1907 | } |
| 1908 | state = rb_entry(node, struct extent_state, rb_node); |
| 1909 | if (state->start != start) { |
| 1910 | ret = -ENOENT; |
| 1911 | goto out; |
| 1912 | } |
| 1913 | *private = state->private; |
| 1914 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1915 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1916 | return ret; |
| 1917 | } |
| 1918 | |
| 1919 | /* |
| 1920 | * searches a range in the state tree for a given mask. |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1921 | * 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] | 1922 | * has the bits set. Otherwise, 1 is returned if any bit in the |
| 1923 | * range is found set. |
| 1924 | */ |
| 1925 | 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] | 1926 | unsigned long bits, int filled, struct extent_state *cached) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1927 | { |
| 1928 | struct extent_state *state = NULL; |
| 1929 | struct rb_node *node; |
| 1930 | int bitset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1931 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1932 | spin_lock(&tree->lock); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1933 | if (cached && extent_state_in_tree(cached) && cached->start <= start && |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 1934 | cached->end > start) |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1935 | node = &cached->rb_node; |
| 1936 | else |
| 1937 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1938 | while (node && start <= end) { |
| 1939 | state = rb_entry(node, struct extent_state, rb_node); |
| 1940 | |
| 1941 | if (filled && state->start > start) { |
| 1942 | bitset = 0; |
| 1943 | break; |
| 1944 | } |
| 1945 | |
| 1946 | if (state->start > end) |
| 1947 | break; |
| 1948 | |
| 1949 | if (state->state & bits) { |
| 1950 | bitset = 1; |
| 1951 | if (!filled) |
| 1952 | break; |
| 1953 | } else if (filled) { |
| 1954 | bitset = 0; |
| 1955 | break; |
| 1956 | } |
Chris Mason | 46562cec | 2009-09-23 20:23:16 -0400 | [diff] [blame] | 1957 | |
| 1958 | if (state->end == (u64)-1) |
| 1959 | break; |
| 1960 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1961 | start = state->end + 1; |
| 1962 | if (start > end) |
| 1963 | break; |
| 1964 | node = rb_next(node); |
| 1965 | if (!node) { |
| 1966 | if (filled) |
| 1967 | bitset = 0; |
| 1968 | break; |
| 1969 | } |
| 1970 | } |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1971 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1972 | return bitset; |
| 1973 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1974 | |
| 1975 | /* |
| 1976 | * helper function to set a given page up to date if all the |
| 1977 | * extents in the tree for that page are up to date |
| 1978 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1979 | 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] | 1980 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 1981 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1982 | u64 end = start + PAGE_CACHE_SIZE - 1; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1983 | if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1984 | SetPageUptodate(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1985 | } |
| 1986 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 1987 | int free_io_failure(struct inode *inode, struct io_failure_record *rec) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1988 | { |
| 1989 | int ret; |
| 1990 | int err = 0; |
| 1991 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 1992 | |
| 1993 | set_state_private(failure_tree, rec->start, 0); |
| 1994 | ret = clear_extent_bits(failure_tree, rec->start, |
| 1995 | rec->start + rec->len - 1, |
| 1996 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 1997 | if (ret) |
| 1998 | err = ret; |
| 1999 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2000 | ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start, |
| 2001 | rec->start + rec->len - 1, |
| 2002 | EXTENT_DAMAGED, GFP_NOFS); |
| 2003 | if (ret && !err) |
| 2004 | err = ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2005 | |
| 2006 | kfree(rec); |
| 2007 | return err; |
| 2008 | } |
| 2009 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2010 | /* |
| 2011 | * this bypasses the standard btrfs submit functions deliberately, as |
| 2012 | * the standard behavior is to write all copies in a raid setup. here we only |
| 2013 | * want to write the one bad copy. so we do the mapping for ourselves and issue |
| 2014 | * submit_bio directly. |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2015 | * to avoid any synchronization issues, wait for the data after writing, which |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2016 | * actually prevents the read that triggered the error from finishing. |
| 2017 | * currently, there can be no more than two copies of every data bit. thus, |
| 2018 | * exactly one rewrite is required. |
| 2019 | */ |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2020 | int repair_io_failure(struct inode *inode, u64 start, u64 length, u64 logical, |
| 2021 | struct page *page, unsigned int pg_offset, int mirror_num) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2022 | { |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2023 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2024 | struct bio *bio; |
| 2025 | struct btrfs_device *dev; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2026 | u64 map_length = 0; |
| 2027 | u64 sector; |
| 2028 | struct btrfs_bio *bbio = NULL; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2029 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2030 | int ret; |
| 2031 | |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2032 | ASSERT(!(fs_info->sb->s_flags & MS_RDONLY)); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2033 | BUG_ON(!mirror_num); |
| 2034 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2035 | /* we can't repair anything in raid56 yet */ |
| 2036 | if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num)) |
| 2037 | return 0; |
| 2038 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2039 | bio = btrfs_io_bio_alloc(GFP_NOFS, 1); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2040 | if (!bio) |
| 2041 | return -EIO; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2042 | bio->bi_iter.bi_size = 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2043 | map_length = length; |
| 2044 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2045 | ret = btrfs_map_block(fs_info, WRITE, logical, |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2046 | &map_length, &bbio, mirror_num); |
| 2047 | if (ret) { |
| 2048 | bio_put(bio); |
| 2049 | return -EIO; |
| 2050 | } |
| 2051 | BUG_ON(mirror_num != bbio->mirror_num); |
| 2052 | sector = bbio->stripes[mirror_num-1].physical >> 9; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2053 | bio->bi_iter.bi_sector = sector; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2054 | dev = bbio->stripes[mirror_num-1].dev; |
| 2055 | kfree(bbio); |
| 2056 | if (!dev || !dev->bdev || !dev->writeable) { |
| 2057 | bio_put(bio); |
| 2058 | return -EIO; |
| 2059 | } |
| 2060 | bio->bi_bdev = dev->bdev; |
Miao Xie | ffdd201 | 2014-09-12 18:44:00 +0800 | [diff] [blame] | 2061 | bio_add_page(bio, page, length, pg_offset); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2062 | |
Kent Overstreet | 33879d4 | 2013-11-23 22:33:32 -0800 | [diff] [blame] | 2063 | if (btrfsic_submit_bio_wait(WRITE_SYNC, bio)) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2064 | /* try to remap that extent elsewhere? */ |
| 2065 | bio_put(bio); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2066 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2067 | return -EIO; |
| 2068 | } |
| 2069 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2070 | printk_ratelimited_in_rcu(KERN_INFO |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2071 | "BTRFS: read error corrected: ino %llu off %llu (dev %s sector %llu)\n", |
| 2072 | btrfs_ino(inode), start, |
| 2073 | rcu_str_deref(dev->name), sector); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2074 | bio_put(bio); |
| 2075 | return 0; |
| 2076 | } |
| 2077 | |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2078 | int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb, |
| 2079 | int mirror_num) |
| 2080 | { |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2081 | u64 start = eb->start; |
| 2082 | unsigned long i, num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | d95603b | 2012-04-12 15:55:15 -0400 | [diff] [blame] | 2083 | int ret = 0; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2084 | |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2085 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 2086 | return -EROFS; |
| 2087 | |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2088 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 2089 | struct page *p = eb->pages[i]; |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2090 | |
| 2091 | ret = repair_io_failure(root->fs_info->btree_inode, start, |
| 2092 | PAGE_CACHE_SIZE, start, p, |
| 2093 | start - page_offset(p), mirror_num); |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2094 | if (ret) |
| 2095 | break; |
| 2096 | start += PAGE_CACHE_SIZE; |
| 2097 | } |
| 2098 | |
| 2099 | return ret; |
| 2100 | } |
| 2101 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2102 | /* |
| 2103 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 2104 | * to see if we need to process or clean up an io_failure_record |
| 2105 | */ |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2106 | int clean_io_failure(struct inode *inode, u64 start, struct page *page, |
| 2107 | unsigned int pg_offset) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2108 | { |
| 2109 | u64 private; |
| 2110 | u64 private_failure; |
| 2111 | struct io_failure_record *failrec; |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2112 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2113 | struct extent_state *state; |
| 2114 | int num_copies; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2115 | int ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2116 | |
| 2117 | private = 0; |
| 2118 | ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
| 2119 | (u64)-1, 1, EXTENT_DIRTY, 0); |
| 2120 | if (!ret) |
| 2121 | return 0; |
| 2122 | |
| 2123 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start, |
| 2124 | &private_failure); |
| 2125 | if (ret) |
| 2126 | return 0; |
| 2127 | |
| 2128 | failrec = (struct io_failure_record *)(unsigned long) private_failure; |
| 2129 | BUG_ON(!failrec->this_mirror); |
| 2130 | |
| 2131 | if (failrec->in_validation) { |
| 2132 | /* there was no real error, just free the record */ |
| 2133 | pr_debug("clean_io_failure: freeing dummy error at %llu\n", |
| 2134 | failrec->start); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2135 | goto out; |
| 2136 | } |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2137 | if (fs_info->sb->s_flags & MS_RDONLY) |
| 2138 | goto out; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2139 | |
| 2140 | spin_lock(&BTRFS_I(inode)->io_tree.lock); |
| 2141 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 2142 | failrec->start, |
| 2143 | EXTENT_LOCKED); |
| 2144 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); |
| 2145 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2146 | if (state && state->start <= failrec->start && |
| 2147 | state->end >= failrec->start + failrec->len - 1) { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2148 | num_copies = btrfs_num_copies(fs_info, failrec->logical, |
| 2149 | failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2150 | if (num_copies > 1) { |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2151 | repair_io_failure(inode, start, failrec->len, |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2152 | failrec->logical, page, |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2153 | pg_offset, failrec->failed_mirror); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2154 | } |
| 2155 | } |
| 2156 | |
| 2157 | out: |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2158 | free_io_failure(inode, failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2159 | |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2160 | return 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2161 | } |
| 2162 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2163 | /* |
| 2164 | * Can be called when |
| 2165 | * - hold extent lock |
| 2166 | * - under ordered extent |
| 2167 | * - the inode is freeing |
| 2168 | */ |
| 2169 | void btrfs_free_io_failure_record(struct inode *inode, u64 start, u64 end) |
| 2170 | { |
| 2171 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 2172 | struct io_failure_record *failrec; |
| 2173 | struct extent_state *state, *next; |
| 2174 | |
| 2175 | if (RB_EMPTY_ROOT(&failure_tree->state)) |
| 2176 | return; |
| 2177 | |
| 2178 | spin_lock(&failure_tree->lock); |
| 2179 | state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY); |
| 2180 | while (state) { |
| 2181 | if (state->start > end) |
| 2182 | break; |
| 2183 | |
| 2184 | ASSERT(state->end <= end); |
| 2185 | |
| 2186 | next = next_state(state); |
| 2187 | |
| 2188 | failrec = (struct io_failure_record *)state->private; |
| 2189 | free_extent_state(state); |
| 2190 | kfree(failrec); |
| 2191 | |
| 2192 | state = next; |
| 2193 | } |
| 2194 | spin_unlock(&failure_tree->lock); |
| 2195 | } |
| 2196 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2197 | int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end, |
| 2198 | struct io_failure_record **failrec_ret) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2199 | { |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2200 | struct io_failure_record *failrec; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2201 | u64 private; |
| 2202 | struct extent_map *em; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2203 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 2204 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 2205 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2206 | int ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2207 | u64 logical; |
| 2208 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2209 | ret = get_state_private(failure_tree, start, &private); |
| 2210 | if (ret) { |
| 2211 | failrec = kzalloc(sizeof(*failrec), GFP_NOFS); |
| 2212 | if (!failrec) |
| 2213 | return -ENOMEM; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2214 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2215 | failrec->start = start; |
| 2216 | failrec->len = end - start + 1; |
| 2217 | failrec->this_mirror = 0; |
| 2218 | failrec->bio_flags = 0; |
| 2219 | failrec->in_validation = 0; |
| 2220 | |
| 2221 | read_lock(&em_tree->lock); |
| 2222 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 2223 | if (!em) { |
| 2224 | read_unlock(&em_tree->lock); |
| 2225 | kfree(failrec); |
| 2226 | return -EIO; |
| 2227 | } |
| 2228 | |
Filipe David Borba Manana | 68ba990 | 2013-11-25 03:22:07 +0000 | [diff] [blame] | 2229 | if (em->start > start || em->start + em->len <= start) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2230 | free_extent_map(em); |
| 2231 | em = NULL; |
| 2232 | } |
| 2233 | read_unlock(&em_tree->lock); |
Tsutomu Itoh | 7a2d6a6 | 2012-10-01 03:07:15 -0600 | [diff] [blame] | 2234 | if (!em) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2235 | kfree(failrec); |
| 2236 | return -EIO; |
| 2237 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2238 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2239 | logical = start - em->start; |
| 2240 | logical = em->block_start + logical; |
| 2241 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 2242 | logical = em->block_start; |
| 2243 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
| 2244 | extent_set_compress_type(&failrec->bio_flags, |
| 2245 | em->compress_type); |
| 2246 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2247 | |
| 2248 | pr_debug("Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu\n", |
| 2249 | logical, start, failrec->len); |
| 2250 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2251 | failrec->logical = logical; |
| 2252 | free_extent_map(em); |
| 2253 | |
| 2254 | /* set the bits in the private failure tree */ |
| 2255 | ret = set_extent_bits(failure_tree, start, end, |
| 2256 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 2257 | if (ret >= 0) |
| 2258 | ret = set_state_private(failure_tree, start, |
| 2259 | (u64)(unsigned long)failrec); |
| 2260 | /* set the bits in the inode's tree */ |
| 2261 | if (ret >= 0) |
| 2262 | ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED, |
| 2263 | GFP_NOFS); |
| 2264 | if (ret < 0) { |
| 2265 | kfree(failrec); |
| 2266 | return ret; |
| 2267 | } |
| 2268 | } else { |
| 2269 | failrec = (struct io_failure_record *)(unsigned long)private; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2270 | pr_debug("Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d\n", |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2271 | failrec->logical, failrec->start, failrec->len, |
| 2272 | failrec->in_validation); |
| 2273 | /* |
| 2274 | * when data can be on disk more than twice, add to failrec here |
| 2275 | * (e.g. with a list for failed_mirror) to make |
| 2276 | * clean_io_failure() clean all those errors at once. |
| 2277 | */ |
| 2278 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2279 | |
| 2280 | *failrec_ret = failrec; |
| 2281 | |
| 2282 | return 0; |
| 2283 | } |
| 2284 | |
| 2285 | int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio, |
| 2286 | struct io_failure_record *failrec, int failed_mirror) |
| 2287 | { |
| 2288 | int num_copies; |
| 2289 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 2290 | num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info, |
| 2291 | failrec->logical, failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2292 | if (num_copies == 1) { |
| 2293 | /* |
| 2294 | * we only have a single copy of the data, so don't bother with |
| 2295 | * all the retry and error correction code that follows. no |
| 2296 | * matter what the error is, it is very likely to persist. |
| 2297 | */ |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2298 | pr_debug("Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n", |
Miao Xie | 09a7f7a | 2013-07-25 19:22:32 +0800 | [diff] [blame] | 2299 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2300 | return 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2301 | } |
| 2302 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2303 | /* |
| 2304 | * there are two premises: |
| 2305 | * a) deliver good data to the caller |
| 2306 | * b) correct the bad sectors on disk |
| 2307 | */ |
| 2308 | if (failed_bio->bi_vcnt > 1) { |
| 2309 | /* |
| 2310 | * to fulfill b), we need to know the exact failing sectors, as |
| 2311 | * we don't want to rewrite any more than the failed ones. thus, |
| 2312 | * we need separate read requests for the failed bio |
| 2313 | * |
| 2314 | * if the following BUG_ON triggers, our validation request got |
| 2315 | * merged. we need separate requests for our algorithm to work. |
| 2316 | */ |
| 2317 | BUG_ON(failrec->in_validation); |
| 2318 | failrec->in_validation = 1; |
| 2319 | failrec->this_mirror = failed_mirror; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2320 | } else { |
| 2321 | /* |
| 2322 | * we're ready to fulfill a) and b) alongside. get a good copy |
| 2323 | * of the failed sector and if we succeed, we have setup |
| 2324 | * everything for repair_io_failure to do the rest for us. |
| 2325 | */ |
| 2326 | if (failrec->in_validation) { |
| 2327 | BUG_ON(failrec->this_mirror != failed_mirror); |
| 2328 | failrec->in_validation = 0; |
| 2329 | failrec->this_mirror = 0; |
| 2330 | } |
| 2331 | failrec->failed_mirror = failed_mirror; |
| 2332 | failrec->this_mirror++; |
| 2333 | if (failrec->this_mirror == failed_mirror) |
| 2334 | failrec->this_mirror++; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2335 | } |
| 2336 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2337 | if (failrec->this_mirror > num_copies) { |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2338 | pr_debug("Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n", |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2339 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2340 | return 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2341 | } |
| 2342 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2343 | return 1; |
| 2344 | } |
| 2345 | |
| 2346 | |
| 2347 | struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio, |
| 2348 | struct io_failure_record *failrec, |
| 2349 | struct page *page, int pg_offset, int icsum, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2350 | bio_end_io_t *endio_func, void *data) |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2351 | { |
| 2352 | struct bio *bio; |
| 2353 | struct btrfs_io_bio *btrfs_failed_bio; |
| 2354 | struct btrfs_io_bio *btrfs_bio; |
| 2355 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2356 | bio = btrfs_io_bio_alloc(GFP_NOFS, 1); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2357 | if (!bio) |
| 2358 | return NULL; |
| 2359 | |
| 2360 | bio->bi_end_io = endio_func; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2361 | bio->bi_iter.bi_sector = failrec->logical >> 9; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2362 | bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2363 | bio->bi_iter.bi_size = 0; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2364 | bio->bi_private = data; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2365 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2366 | btrfs_failed_bio = btrfs_io_bio(failed_bio); |
| 2367 | if (btrfs_failed_bio->csum) { |
| 2368 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2369 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
| 2370 | |
| 2371 | btrfs_bio = btrfs_io_bio(bio); |
| 2372 | btrfs_bio->csum = btrfs_bio->csum_inline; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2373 | icsum *= csum_size; |
| 2374 | memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum, |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2375 | csum_size); |
| 2376 | } |
| 2377 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2378 | bio_add_page(bio, page, failrec->len, pg_offset); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2379 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2380 | return bio; |
| 2381 | } |
| 2382 | |
| 2383 | /* |
| 2384 | * this is a generic handler for readpage errors (default |
| 2385 | * readpage_io_failed_hook). if other copies exist, read those and write back |
| 2386 | * good data to the failed position. does not investigate in remapping the |
| 2387 | * failed extent elsewhere, hoping the device will be smart enough to do this as |
| 2388 | * needed |
| 2389 | */ |
| 2390 | |
| 2391 | static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset, |
| 2392 | struct page *page, u64 start, u64 end, |
| 2393 | int failed_mirror) |
| 2394 | { |
| 2395 | struct io_failure_record *failrec; |
| 2396 | struct inode *inode = page->mapping->host; |
| 2397 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 2398 | struct bio *bio; |
| 2399 | int read_mode; |
| 2400 | int ret; |
| 2401 | |
| 2402 | BUG_ON(failed_bio->bi_rw & REQ_WRITE); |
| 2403 | |
| 2404 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 2405 | if (ret) |
| 2406 | return ret; |
| 2407 | |
| 2408 | ret = btrfs_check_repairable(inode, failed_bio, failrec, failed_mirror); |
| 2409 | if (!ret) { |
| 2410 | free_io_failure(inode, failrec); |
| 2411 | return -EIO; |
| 2412 | } |
| 2413 | |
| 2414 | if (failed_bio->bi_vcnt > 1) |
| 2415 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; |
| 2416 | else |
| 2417 | read_mode = READ_SYNC; |
| 2418 | |
| 2419 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
| 2420 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
| 2421 | start - page_offset(page), |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2422 | (int)phy_offset, failed_bio->bi_end_io, |
| 2423 | NULL); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2424 | if (!bio) { |
| 2425 | free_io_failure(inode, failrec); |
| 2426 | return -EIO; |
| 2427 | } |
| 2428 | |
| 2429 | pr_debug("Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d\n", |
| 2430 | read_mode, failrec->this_mirror, failrec->in_validation); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2431 | |
Tsutomu Itoh | 013bd4c | 2012-02-16 10:11:40 +0900 | [diff] [blame] | 2432 | ret = tree->ops->submit_bio_hook(inode, read_mode, bio, |
| 2433 | failrec->this_mirror, |
| 2434 | failrec->bio_flags, 0); |
Miao Xie | 6c387ab | 2014-09-12 18:43:57 +0800 | [diff] [blame] | 2435 | if (ret) { |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2436 | free_io_failure(inode, failrec); |
Miao Xie | 6c387ab | 2014-09-12 18:43:57 +0800 | [diff] [blame] | 2437 | bio_put(bio); |
| 2438 | } |
| 2439 | |
Tsutomu Itoh | 013bd4c | 2012-02-16 10:11:40 +0900 | [diff] [blame] | 2440 | return ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2441 | } |
| 2442 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2443 | /* lots and lots of room for performance fixes in the end_bio funcs */ |
| 2444 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2445 | int end_extent_writepage(struct page *page, int err, u64 start, u64 end) |
| 2446 | { |
| 2447 | int uptodate = (err == 0); |
| 2448 | struct extent_io_tree *tree; |
Eric Sandeen | 3e2426b | 2014-06-12 00:39:58 -0500 | [diff] [blame] | 2449 | int ret = 0; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2450 | |
| 2451 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 2452 | |
| 2453 | if (tree->ops && tree->ops->writepage_end_io_hook) { |
| 2454 | ret = tree->ops->writepage_end_io_hook(page, start, |
| 2455 | end, NULL, uptodate); |
| 2456 | if (ret) |
| 2457 | uptodate = 0; |
| 2458 | } |
| 2459 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2460 | if (!uptodate) { |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2461 | ClearPageUptodate(page); |
| 2462 | SetPageError(page); |
Liu Bo | 5dca6ee | 2014-05-12 12:47:36 +0800 | [diff] [blame] | 2463 | ret = ret < 0 ? ret : -EIO; |
| 2464 | mapping_set_error(page->mapping, ret); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2465 | } |
| 2466 | return 0; |
| 2467 | } |
| 2468 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2469 | /* |
| 2470 | * after a writepage IO is done, we need to: |
| 2471 | * clear the uptodate bits on error |
| 2472 | * clear the writeback bits in the extent tree for this IO |
| 2473 | * end_page_writeback if the page has no more pending IO |
| 2474 | * |
| 2475 | * Scheduling is not allowed, so the extent state tree is expected |
| 2476 | * to have one and only one object corresponding to this IO. |
| 2477 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2478 | static void end_bio_extent_writepage(struct bio *bio, int err) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2479 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2480 | struct bio_vec *bvec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2481 | u64 start; |
| 2482 | u64 end; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2483 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2484 | |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2485 | bio_for_each_segment_all(bvec, bio, i) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2486 | struct page *page = bvec->bv_page; |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2487 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2488 | /* We always issue full-page reads, but if some block |
| 2489 | * in a page fails to read, blk_update_request() will |
| 2490 | * advance bv_offset and adjust bv_len to compensate. |
| 2491 | * Print a warning for nonzero offsets, and an error |
| 2492 | * if they don't add up to a full page. */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2493 | if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE) { |
| 2494 | if (bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE) |
| 2495 | btrfs_err(BTRFS_I(page->mapping->host)->root->fs_info, |
| 2496 | "partial page write in btrfs with offset %u and length %u", |
| 2497 | bvec->bv_offset, bvec->bv_len); |
| 2498 | else |
| 2499 | btrfs_info(BTRFS_I(page->mapping->host)->root->fs_info, |
| 2500 | "incomplete page write in btrfs with offset %u and " |
| 2501 | "length %u", |
| 2502 | bvec->bv_offset, bvec->bv_len); |
| 2503 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2504 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2505 | start = page_offset(page); |
| 2506 | end = start + bvec->bv_offset + bvec->bv_len - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2507 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2508 | if (end_extent_writepage(page, err, start, end)) |
| 2509 | continue; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2510 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2511 | end_page_writeback(page); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2512 | } |
Chris Mason | 2b1f55b | 2008-09-24 11:48:04 -0400 | [diff] [blame] | 2513 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2514 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2515 | } |
| 2516 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2517 | static void |
| 2518 | endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len, |
| 2519 | int uptodate) |
| 2520 | { |
| 2521 | struct extent_state *cached = NULL; |
| 2522 | u64 end = start + len - 1; |
| 2523 | |
| 2524 | if (uptodate && tree->track_uptodate) |
| 2525 | set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC); |
| 2526 | unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC); |
| 2527 | } |
| 2528 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2529 | /* |
| 2530 | * after a readpage IO is done, we need to: |
| 2531 | * clear the uptodate bits on error |
| 2532 | * set the uptodate bits if things worked |
| 2533 | * set the page up to date if all extents in the tree are uptodate |
| 2534 | * clear the lock bit in the extent tree |
| 2535 | * unlock the page if there are no other extents locked for it |
| 2536 | * |
| 2537 | * Scheduling is not allowed, so the extent state tree is expected |
| 2538 | * to have one and only one object corresponding to this IO. |
| 2539 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2540 | static void end_bio_extent_readpage(struct bio *bio, int err) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2541 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2542 | struct bio_vec *bvec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2543 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2544 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2545 | struct extent_io_tree *tree; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2546 | u64 offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2547 | u64 start; |
| 2548 | u64 end; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2549 | u64 len; |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2550 | u64 extent_start = 0; |
| 2551 | u64 extent_len = 0; |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2552 | int mirror; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2553 | int ret; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2554 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2555 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2556 | if (err) |
| 2557 | uptodate = 0; |
| 2558 | |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2559 | bio_for_each_segment_all(bvec, bio, i) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2560 | struct page *page = bvec->bv_page; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2561 | struct inode *inode = page->mapping->host; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2562 | |
Kent Overstreet | be3940c | 2012-09-11 14:23:05 -0600 | [diff] [blame] | 2563 | pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, " |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 2564 | "mirror=%u\n", (u64)bio->bi_iter.bi_sector, err, |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2565 | io_bio->mirror_num); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2566 | tree = &BTRFS_I(inode)->io_tree; |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2567 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2568 | /* We always issue full-page reads, but if some block |
| 2569 | * in a page fails to read, blk_update_request() will |
| 2570 | * advance bv_offset and adjust bv_len to compensate. |
| 2571 | * Print a warning for nonzero offsets, and an error |
| 2572 | * if they don't add up to a full page. */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2573 | if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE) { |
| 2574 | if (bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE) |
| 2575 | btrfs_err(BTRFS_I(page->mapping->host)->root->fs_info, |
| 2576 | "partial page read in btrfs with offset %u and length %u", |
| 2577 | bvec->bv_offset, bvec->bv_len); |
| 2578 | else |
| 2579 | btrfs_info(BTRFS_I(page->mapping->host)->root->fs_info, |
| 2580 | "incomplete page read in btrfs with offset %u and " |
| 2581 | "length %u", |
| 2582 | bvec->bv_offset, bvec->bv_len); |
| 2583 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2584 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2585 | start = page_offset(page); |
| 2586 | end = start + bvec->bv_offset + bvec->bv_len - 1; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2587 | len = bvec->bv_len; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2588 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2589 | mirror = io_bio->mirror_num; |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2590 | if (likely(uptodate && tree->ops && |
| 2591 | tree->ops->readpage_end_io_hook)) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2592 | ret = tree->ops->readpage_end_io_hook(io_bio, offset, |
| 2593 | page, start, end, |
| 2594 | mirror); |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2595 | if (ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2596 | uptodate = 0; |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2597 | else |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2598 | clean_io_failure(inode, start, page, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2599 | } |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2600 | |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2601 | if (likely(uptodate)) |
| 2602 | goto readpage_ok; |
| 2603 | |
| 2604 | if (tree->ops && tree->ops->readpage_io_failed_hook) { |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2605 | ret = tree->ops->readpage_io_failed_hook(page, mirror); |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2606 | if (!ret && !err && |
| 2607 | test_bit(BIO_UPTODATE, &bio->bi_flags)) |
| 2608 | uptodate = 1; |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2609 | } else { |
Jan Schmidt | f4a8e65 | 2011-12-01 09:30:36 -0500 | [diff] [blame] | 2610 | /* |
| 2611 | * The generic bio_readpage_error handles errors the |
| 2612 | * following way: If possible, new read requests are |
| 2613 | * created and submitted and will end up in |
| 2614 | * end_bio_extent_readpage as well (if we're lucky, not |
| 2615 | * in the !uptodate case). In that case it returns 0 and |
| 2616 | * we just go on with the next page in our bio. If it |
| 2617 | * can't handle the error it will return -EIO and we |
| 2618 | * remain responsible for that page. |
| 2619 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2620 | ret = bio_readpage_error(bio, offset, page, start, end, |
| 2621 | mirror); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2622 | if (ret == 0) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2623 | uptodate = |
| 2624 | test_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2625 | if (err) |
| 2626 | uptodate = 0; |
Liu Bo | 38c1c2e | 2014-08-19 23:33:13 +0800 | [diff] [blame] | 2627 | offset += len; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2628 | continue; |
| 2629 | } |
| 2630 | } |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2631 | readpage_ok: |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2632 | if (likely(uptodate)) { |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2633 | loff_t i_size = i_size_read(inode); |
| 2634 | pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; |
Liu Bo | a583c02 | 2014-08-19 23:32:22 +0800 | [diff] [blame] | 2635 | unsigned off; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2636 | |
| 2637 | /* Zero out the end if this page straddles i_size */ |
Liu Bo | a583c02 | 2014-08-19 23:32:22 +0800 | [diff] [blame] | 2638 | off = i_size & (PAGE_CACHE_SIZE-1); |
| 2639 | if (page->index == end_index && off) |
| 2640 | zero_user_segment(page, off, PAGE_CACHE_SIZE); |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2641 | SetPageUptodate(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2642 | } else { |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2643 | ClearPageUptodate(page); |
| 2644 | SetPageError(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2645 | } |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2646 | unlock_page(page); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2647 | offset += len; |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2648 | |
| 2649 | if (unlikely(!uptodate)) { |
| 2650 | if (extent_len) { |
| 2651 | endio_readpage_release_extent(tree, |
| 2652 | extent_start, |
| 2653 | extent_len, 1); |
| 2654 | extent_start = 0; |
| 2655 | extent_len = 0; |
| 2656 | } |
| 2657 | endio_readpage_release_extent(tree, start, |
| 2658 | end - start + 1, 0); |
| 2659 | } else if (!extent_len) { |
| 2660 | extent_start = start; |
| 2661 | extent_len = end + 1 - start; |
| 2662 | } else if (extent_start + extent_len == start) { |
| 2663 | extent_len += end + 1 - start; |
| 2664 | } else { |
| 2665 | endio_readpage_release_extent(tree, extent_start, |
| 2666 | extent_len, uptodate); |
| 2667 | extent_start = start; |
| 2668 | extent_len = end + 1 - start; |
| 2669 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2670 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2671 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2672 | if (extent_len) |
| 2673 | endio_readpage_release_extent(tree, extent_start, extent_len, |
| 2674 | uptodate); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2675 | if (io_bio->end_io) |
| 2676 | io_bio->end_io(io_bio, err); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2677 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2678 | } |
| 2679 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2680 | /* |
| 2681 | * this allocates from the btrfs_bioset. We're returning a bio right now |
| 2682 | * but you can call btrfs_io_bio for the appropriate container_of magic |
| 2683 | */ |
Miao Xie | 88f794e | 2010-11-22 03:02:55 +0000 | [diff] [blame] | 2684 | struct bio * |
| 2685 | btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs, |
| 2686 | gfp_t gfp_flags) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2687 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2688 | struct btrfs_io_bio *btrfs_bio; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2689 | struct bio *bio; |
| 2690 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2691 | bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2692 | |
| 2693 | if (bio == NULL && (current->flags & PF_MEMALLOC)) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2694 | while (!bio && (nr_vecs /= 2)) { |
| 2695 | bio = bio_alloc_bioset(gfp_flags, |
| 2696 | nr_vecs, btrfs_bioset); |
| 2697 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2698 | } |
| 2699 | |
| 2700 | if (bio) { |
| 2701 | bio->bi_bdev = bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2702 | bio->bi_iter.bi_sector = first_sector; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2703 | btrfs_bio = btrfs_io_bio(bio); |
| 2704 | btrfs_bio->csum = NULL; |
| 2705 | btrfs_bio->csum_allocated = NULL; |
| 2706 | btrfs_bio->end_io = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2707 | } |
| 2708 | return bio; |
| 2709 | } |
| 2710 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2711 | struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask) |
| 2712 | { |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 2713 | struct btrfs_io_bio *btrfs_bio; |
| 2714 | struct bio *new; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2715 | |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 2716 | new = bio_clone_bioset(bio, gfp_mask, btrfs_bioset); |
| 2717 | if (new) { |
| 2718 | btrfs_bio = btrfs_io_bio(new); |
| 2719 | btrfs_bio->csum = NULL; |
| 2720 | btrfs_bio->csum_allocated = NULL; |
| 2721 | btrfs_bio->end_io = NULL; |
| 2722 | } |
| 2723 | return new; |
| 2724 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2725 | |
| 2726 | /* this also allocates from the btrfs_bioset */ |
| 2727 | struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs) |
| 2728 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2729 | struct btrfs_io_bio *btrfs_bio; |
| 2730 | struct bio *bio; |
| 2731 | |
| 2732 | bio = bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset); |
| 2733 | if (bio) { |
| 2734 | btrfs_bio = btrfs_io_bio(bio); |
| 2735 | btrfs_bio->csum = NULL; |
| 2736 | btrfs_bio->csum_allocated = NULL; |
| 2737 | btrfs_bio->end_io = NULL; |
| 2738 | } |
| 2739 | return bio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 2743 | static int __must_check submit_one_bio(int rw, struct bio *bio, |
| 2744 | int mirror_num, unsigned long bio_flags) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2745 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2746 | int ret = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2747 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 2748 | struct page *page = bvec->bv_page; |
| 2749 | struct extent_io_tree *tree = bio->bi_private; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2750 | u64 start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2751 | |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2752 | start = page_offset(page) + bvec->bv_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2753 | |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2754 | bio->bi_private = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2755 | |
| 2756 | bio_get(bio); |
| 2757 | |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2758 | if (tree->ops && tree->ops->submit_bio_hook) |
liubo | 6b82ce8 | 2011-01-26 06:21:39 +0000 | [diff] [blame] | 2759 | ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 2760 | mirror_num, bio_flags, start); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2761 | else |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 2762 | btrfsic_submit_bio(rw, bio); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2763 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2764 | if (bio_flagged(bio, BIO_EOPNOTSUPP)) |
| 2765 | ret = -EOPNOTSUPP; |
| 2766 | bio_put(bio); |
| 2767 | return ret; |
| 2768 | } |
| 2769 | |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 2770 | 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] | 2771 | unsigned long offset, size_t size, struct bio *bio, |
| 2772 | unsigned long bio_flags) |
| 2773 | { |
| 2774 | int ret = 0; |
| 2775 | if (tree->ops && tree->ops->merge_bio_hook) |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 2776 | ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio, |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 2777 | bio_flags); |
| 2778 | BUG_ON(ret < 0); |
| 2779 | return ret; |
| 2780 | |
| 2781 | } |
| 2782 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2783 | static int submit_extent_page(int rw, struct extent_io_tree *tree, |
| 2784 | struct page *page, sector_t sector, |
| 2785 | size_t size, unsigned long offset, |
| 2786 | struct block_device *bdev, |
| 2787 | struct bio **bio_ret, |
| 2788 | unsigned long max_pages, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2789 | bio_end_io_t end_io_func, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2790 | int mirror_num, |
| 2791 | unsigned long prev_bio_flags, |
| 2792 | unsigned long bio_flags) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2793 | { |
| 2794 | int ret = 0; |
| 2795 | struct bio *bio; |
| 2796 | int nr; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2797 | int contig = 0; |
| 2798 | int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED; |
| 2799 | int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 2800 | size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2801 | |
| 2802 | if (bio_ret && *bio_ret) { |
| 2803 | bio = *bio_ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2804 | if (old_compressed) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2805 | contig = bio->bi_iter.bi_sector == sector; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2806 | else |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 2807 | contig = bio_end_sector(bio) == sector; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2808 | |
| 2809 | if (prev_bio_flags != bio_flags || !contig || |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 2810 | merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2811 | bio_add_page(bio, page, page_size, offset) < page_size) { |
| 2812 | ret = submit_one_bio(rw, bio, mirror_num, |
| 2813 | prev_bio_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2814 | if (ret < 0) |
| 2815 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2816 | bio = NULL; |
| 2817 | } else { |
| 2818 | return 0; |
| 2819 | } |
| 2820 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2821 | if (this_compressed) |
| 2822 | nr = BIO_MAX_PAGES; |
| 2823 | else |
| 2824 | nr = bio_get_nr_vecs(bdev); |
| 2825 | |
Miao Xie | 88f794e | 2010-11-22 03:02:55 +0000 | [diff] [blame] | 2826 | bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH); |
Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 2827 | if (!bio) |
| 2828 | return -ENOMEM; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2829 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2830 | bio_add_page(bio, page, page_size, offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2831 | bio->bi_end_io = end_io_func; |
| 2832 | bio->bi_private = tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2833 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2834 | if (bio_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2835 | *bio_ret = bio; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2836 | else |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2837 | ret = submit_one_bio(rw, bio, mirror_num, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2838 | |
| 2839 | return ret; |
| 2840 | } |
| 2841 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 2842 | static void attach_extent_buffer_page(struct extent_buffer *eb, |
| 2843 | struct page *page) |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 2844 | { |
| 2845 | if (!PagePrivate(page)) { |
| 2846 | SetPagePrivate(page); |
| 2847 | page_cache_get(page); |
| 2848 | set_page_private(page, (unsigned long)eb); |
| 2849 | } else { |
| 2850 | WARN_ON(page->private != (unsigned long)eb); |
| 2851 | } |
| 2852 | } |
| 2853 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2854 | void set_page_extent_mapped(struct page *page) |
| 2855 | { |
| 2856 | if (!PagePrivate(page)) { |
| 2857 | SetPagePrivate(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2858 | page_cache_get(page); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2859 | set_page_private(page, EXTENT_PAGE_PRIVATE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2860 | } |
| 2861 | } |
| 2862 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2863 | static struct extent_map * |
| 2864 | __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset, |
| 2865 | u64 start, u64 len, get_extent_t *get_extent, |
| 2866 | struct extent_map **em_cached) |
| 2867 | { |
| 2868 | struct extent_map *em; |
| 2869 | |
| 2870 | if (em_cached && *em_cached) { |
| 2871 | em = *em_cached; |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 2872 | if (extent_map_in_tree(em) && start >= em->start && |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2873 | start < extent_map_end(em)) { |
| 2874 | atomic_inc(&em->refs); |
| 2875 | return em; |
| 2876 | } |
| 2877 | |
| 2878 | free_extent_map(em); |
| 2879 | *em_cached = NULL; |
| 2880 | } |
| 2881 | |
| 2882 | em = get_extent(inode, page, pg_offset, start, len, 0); |
| 2883 | if (em_cached && !IS_ERR_OR_NULL(em)) { |
| 2884 | BUG_ON(*em_cached); |
| 2885 | atomic_inc(&em->refs); |
| 2886 | *em_cached = em; |
| 2887 | } |
| 2888 | return em; |
| 2889 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2890 | /* |
| 2891 | * basic readpage implementation. Locked extent state structs are inserted |
| 2892 | * into the tree that are removed when the IO is done (by the end_io |
| 2893 | * handlers) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2894 | * XXX JDM: This needs looking at to ensure proper page locking |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2895 | */ |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2896 | static int __do_readpage(struct extent_io_tree *tree, |
| 2897 | struct page *page, |
| 2898 | get_extent_t *get_extent, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2899 | struct extent_map **em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2900 | struct bio **bio, int mirror_num, |
| 2901 | unsigned long *bio_flags, int rw) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2902 | { |
| 2903 | struct inode *inode = page->mapping->host; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2904 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2905 | u64 page_end = start + PAGE_CACHE_SIZE - 1; |
| 2906 | u64 end; |
| 2907 | u64 cur = start; |
| 2908 | u64 extent_offset; |
| 2909 | u64 last_byte = i_size_read(inode); |
| 2910 | u64 block_start; |
| 2911 | u64 cur_end; |
| 2912 | sector_t sector; |
| 2913 | struct extent_map *em; |
| 2914 | struct block_device *bdev; |
| 2915 | int ret; |
| 2916 | int nr = 0; |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 2917 | int parent_locked = *bio_flags & EXTENT_BIO_PARENT_LOCKED; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2918 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2919 | size_t iosize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2920 | size_t disk_io_size; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2921 | size_t blocksize = inode->i_sb->s_blocksize; |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 2922 | unsigned long this_bio_flag = *bio_flags & EXTENT_BIO_PARENT_LOCKED; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2923 | |
| 2924 | set_page_extent_mapped(page); |
| 2925 | |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2926 | end = page_end; |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 2927 | if (!PageUptodate(page)) { |
| 2928 | if (cleancache_get_page(page) == 0) { |
| 2929 | BUG_ON(blocksize != PAGE_SIZE); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2930 | unlock_extent(tree, start, end); |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 2931 | goto out; |
| 2932 | } |
| 2933 | } |
| 2934 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2935 | if (page->index == last_byte >> PAGE_CACHE_SHIFT) { |
| 2936 | char *userpage; |
| 2937 | size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1); |
| 2938 | |
| 2939 | if (zero_offset) { |
| 2940 | iosize = PAGE_CACHE_SIZE - zero_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2941 | userpage = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2942 | memset(userpage + zero_offset, 0, iosize); |
| 2943 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2944 | kunmap_atomic(userpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2945 | } |
| 2946 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2947 | while (cur <= end) { |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 2948 | unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1; |
| 2949 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2950 | if (cur >= last_byte) { |
| 2951 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2952 | struct extent_state *cached = NULL; |
| 2953 | |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2954 | iosize = PAGE_CACHE_SIZE - pg_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2955 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2956 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2957 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2958 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2959 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2960 | &cached, GFP_NOFS); |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 2961 | if (!parent_locked) |
| 2962 | unlock_extent_cached(tree, cur, |
| 2963 | cur + iosize - 1, |
| 2964 | &cached, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2965 | break; |
| 2966 | } |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2967 | em = __get_extent_map(inode, page, pg_offset, cur, |
| 2968 | end - cur + 1, get_extent, em_cached); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 2969 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2970 | SetPageError(page); |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 2971 | if (!parent_locked) |
| 2972 | unlock_extent(tree, cur, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2973 | break; |
| 2974 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2975 | extent_offset = cur - em->start; |
| 2976 | BUG_ON(extent_map_end(em) <= cur); |
| 2977 | BUG_ON(end < cur); |
| 2978 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2979 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 2980 | this_bio_flag |= EXTENT_BIO_COMPRESSED; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2981 | extent_set_compress_type(&this_bio_flag, |
| 2982 | em->compress_type); |
| 2983 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2984 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2985 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
| 2986 | cur_end = min(extent_map_end(em) - 1, end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 2987 | iosize = ALIGN(iosize, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2988 | if (this_bio_flag & EXTENT_BIO_COMPRESSED) { |
| 2989 | disk_io_size = em->block_len; |
| 2990 | sector = em->block_start >> 9; |
| 2991 | } else { |
| 2992 | sector = (em->block_start + extent_offset) >> 9; |
| 2993 | disk_io_size = iosize; |
| 2994 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2995 | bdev = em->bdev; |
| 2996 | block_start = em->block_start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2997 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 2998 | block_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2999 | free_extent_map(em); |
| 3000 | em = NULL; |
| 3001 | |
| 3002 | /* we've found a hole, just zero and go on */ |
| 3003 | if (block_start == EXTENT_MAP_HOLE) { |
| 3004 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3005 | struct extent_state *cached = NULL; |
| 3006 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3007 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3008 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3009 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3010 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3011 | |
| 3012 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3013 | &cached, GFP_NOFS); |
| 3014 | unlock_extent_cached(tree, cur, cur + iosize - 1, |
| 3015 | &cached, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3016 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3017 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3018 | continue; |
| 3019 | } |
| 3020 | /* the get_extent function already copied into the page */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3021 | if (test_range_bit(tree, cur, cur_end, |
| 3022 | EXTENT_UPTODATE, 1, NULL)) { |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 3023 | check_page_uptodate(tree, page); |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 3024 | if (!parent_locked) |
| 3025 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3026 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3027 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3028 | continue; |
| 3029 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3030 | /* we have an inline extent but it didn't get marked up |
| 3031 | * to date. Error out |
| 3032 | */ |
| 3033 | if (block_start == EXTENT_MAP_INLINE) { |
| 3034 | SetPageError(page); |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 3035 | if (!parent_locked) |
| 3036 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3037 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3038 | pg_offset += iosize; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3039 | continue; |
| 3040 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3041 | |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3042 | pnr -= page->index; |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 3043 | ret = submit_extent_page(rw, tree, page, |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3044 | sector, disk_io_size, pg_offset, |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3045 | bdev, bio, pnr, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3046 | end_bio_extent_readpage, mirror_num, |
| 3047 | *bio_flags, |
| 3048 | this_bio_flag); |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3049 | if (!ret) { |
| 3050 | nr++; |
| 3051 | *bio_flags = this_bio_flag; |
| 3052 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3053 | SetPageError(page); |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 3054 | if (!parent_locked) |
| 3055 | unlock_extent(tree, cur, cur + iosize - 1); |
Josef Bacik | edd33c9 | 2012-10-05 16:40:32 -0400 | [diff] [blame] | 3056 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3057 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3058 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3059 | } |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 3060 | out: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3061 | if (!nr) { |
| 3062 | if (!PageError(page)) |
| 3063 | SetPageUptodate(page); |
| 3064 | unlock_page(page); |
| 3065 | } |
| 3066 | return 0; |
| 3067 | } |
| 3068 | |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3069 | static inline void __do_contiguous_readpages(struct extent_io_tree *tree, |
| 3070 | struct page *pages[], int nr_pages, |
| 3071 | u64 start, u64 end, |
| 3072 | get_extent_t *get_extent, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3073 | struct extent_map **em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3074 | struct bio **bio, int mirror_num, |
| 3075 | unsigned long *bio_flags, int rw) |
| 3076 | { |
| 3077 | struct inode *inode; |
| 3078 | struct btrfs_ordered_extent *ordered; |
| 3079 | int index; |
| 3080 | |
| 3081 | inode = pages[0]->mapping->host; |
| 3082 | while (1) { |
| 3083 | lock_extent(tree, start, end); |
| 3084 | ordered = btrfs_lookup_ordered_range(inode, start, |
| 3085 | end - start + 1); |
| 3086 | if (!ordered) |
| 3087 | break; |
| 3088 | unlock_extent(tree, start, end); |
| 3089 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3090 | btrfs_put_ordered_extent(ordered); |
| 3091 | } |
| 3092 | |
| 3093 | for (index = 0; index < nr_pages; index++) { |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3094 | __do_readpage(tree, pages[index], get_extent, em_cached, bio, |
| 3095 | mirror_num, bio_flags, rw); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3096 | page_cache_release(pages[index]); |
| 3097 | } |
| 3098 | } |
| 3099 | |
| 3100 | static void __extent_readpages(struct extent_io_tree *tree, |
| 3101 | struct page *pages[], |
| 3102 | int nr_pages, get_extent_t *get_extent, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3103 | struct extent_map **em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3104 | struct bio **bio, int mirror_num, |
| 3105 | unsigned long *bio_flags, int rw) |
| 3106 | { |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 3107 | u64 start = 0; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3108 | u64 end = 0; |
| 3109 | u64 page_start; |
| 3110 | int index; |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 3111 | int first_index = 0; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3112 | |
| 3113 | for (index = 0; index < nr_pages; index++) { |
| 3114 | page_start = page_offset(pages[index]); |
| 3115 | if (!end) { |
| 3116 | start = page_start; |
| 3117 | end = start + PAGE_CACHE_SIZE - 1; |
| 3118 | first_index = index; |
| 3119 | } else if (end + 1 == page_start) { |
| 3120 | end += PAGE_CACHE_SIZE; |
| 3121 | } else { |
| 3122 | __do_contiguous_readpages(tree, &pages[first_index], |
| 3123 | index - first_index, start, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3124 | end, get_extent, em_cached, |
| 3125 | bio, mirror_num, bio_flags, |
| 3126 | rw); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3127 | start = page_start; |
| 3128 | end = start + PAGE_CACHE_SIZE - 1; |
| 3129 | first_index = index; |
| 3130 | } |
| 3131 | } |
| 3132 | |
| 3133 | if (end) |
| 3134 | __do_contiguous_readpages(tree, &pages[first_index], |
| 3135 | index - first_index, start, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3136 | end, get_extent, em_cached, bio, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3137 | mirror_num, bio_flags, rw); |
| 3138 | } |
| 3139 | |
| 3140 | static int __extent_read_full_page(struct extent_io_tree *tree, |
| 3141 | struct page *page, |
| 3142 | get_extent_t *get_extent, |
| 3143 | struct bio **bio, int mirror_num, |
| 3144 | unsigned long *bio_flags, int rw) |
| 3145 | { |
| 3146 | struct inode *inode = page->mapping->host; |
| 3147 | struct btrfs_ordered_extent *ordered; |
| 3148 | u64 start = page_offset(page); |
| 3149 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 3150 | int ret; |
| 3151 | |
| 3152 | while (1) { |
| 3153 | lock_extent(tree, start, end); |
| 3154 | ordered = btrfs_lookup_ordered_extent(inode, start); |
| 3155 | if (!ordered) |
| 3156 | break; |
| 3157 | unlock_extent(tree, start, end); |
| 3158 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3159 | btrfs_put_ordered_extent(ordered); |
| 3160 | } |
| 3161 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3162 | ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num, |
| 3163 | bio_flags, rw); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3164 | return ret; |
| 3165 | } |
| 3166 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3167 | 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] | 3168 | get_extent_t *get_extent, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3169 | { |
| 3170 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3171 | unsigned long bio_flags = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3172 | int ret; |
| 3173 | |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 3174 | ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num, |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 3175 | &bio_flags, READ); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3176 | if (bio) |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 3177 | ret = submit_one_bio(READ, bio, mirror_num, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3178 | return ret; |
| 3179 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3180 | |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 3181 | int extent_read_full_page_nolock(struct extent_io_tree *tree, struct page *page, |
| 3182 | get_extent_t *get_extent, int mirror_num) |
| 3183 | { |
| 3184 | struct bio *bio = NULL; |
| 3185 | unsigned long bio_flags = EXTENT_BIO_PARENT_LOCKED; |
| 3186 | int ret; |
| 3187 | |
| 3188 | ret = __do_readpage(tree, page, get_extent, NULL, &bio, mirror_num, |
| 3189 | &bio_flags, READ); |
| 3190 | if (bio) |
| 3191 | ret = submit_one_bio(READ, bio, mirror_num, bio_flags); |
| 3192 | return ret; |
| 3193 | } |
| 3194 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3195 | static noinline void update_nr_written(struct page *page, |
| 3196 | struct writeback_control *wbc, |
| 3197 | unsigned long nr_written) |
| 3198 | { |
| 3199 | wbc->nr_to_write -= nr_written; |
| 3200 | if (wbc->range_cyclic || (wbc->nr_to_write > 0 && |
| 3201 | wbc->range_start == 0 && wbc->range_end == LLONG_MAX)) |
| 3202 | page->mapping->writeback_index = page->index + nr_written; |
| 3203 | } |
| 3204 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3205 | /* |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3206 | * helper for __extent_writepage, doing all of the delayed allocation setup. |
| 3207 | * |
| 3208 | * This returns 1 if our fill_delalloc function did all the work required |
| 3209 | * to write the page (copy into inline extent). In this case the IO has |
| 3210 | * been started and the page is already unlocked. |
| 3211 | * |
| 3212 | * This returns 0 if all went well (page still locked) |
| 3213 | * This returns < 0 if there were errors (page still locked) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3214 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3215 | static noinline_for_stack int writepage_delalloc(struct inode *inode, |
| 3216 | struct page *page, struct writeback_control *wbc, |
| 3217 | struct extent_page_data *epd, |
| 3218 | u64 delalloc_start, |
| 3219 | unsigned long *nr_written) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3220 | { |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3221 | struct extent_io_tree *tree = epd->tree; |
| 3222 | u64 page_end = delalloc_start + PAGE_CACHE_SIZE - 1; |
| 3223 | u64 nr_delalloc; |
| 3224 | u64 delalloc_to_write = 0; |
| 3225 | u64 delalloc_end = 0; |
| 3226 | int ret; |
| 3227 | int page_started = 0; |
| 3228 | |
| 3229 | if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc) |
| 3230 | return 0; |
| 3231 | |
| 3232 | while (delalloc_end < page_end) { |
| 3233 | nr_delalloc = find_lock_delalloc_range(inode, tree, |
| 3234 | page, |
| 3235 | &delalloc_start, |
| 3236 | &delalloc_end, |
| 3237 | 128 * 1024 * 1024); |
| 3238 | if (nr_delalloc == 0) { |
| 3239 | delalloc_start = delalloc_end + 1; |
| 3240 | continue; |
| 3241 | } |
| 3242 | ret = tree->ops->fill_delalloc(inode, page, |
| 3243 | delalloc_start, |
| 3244 | delalloc_end, |
| 3245 | &page_started, |
| 3246 | nr_written); |
| 3247 | /* File system has been set read-only */ |
| 3248 | if (ret) { |
| 3249 | SetPageError(page); |
| 3250 | /* fill_delalloc should be return < 0 for error |
| 3251 | * but just in case, we use > 0 here meaning the |
| 3252 | * IO is started, so we don't want to return > 0 |
| 3253 | * unless things are going well. |
| 3254 | */ |
| 3255 | ret = ret < 0 ? ret : -EIO; |
| 3256 | goto done; |
| 3257 | } |
| 3258 | /* |
| 3259 | * delalloc_end is already one less than the total |
| 3260 | * length, so we don't subtract one from |
| 3261 | * PAGE_CACHE_SIZE |
| 3262 | */ |
| 3263 | delalloc_to_write += (delalloc_end - delalloc_start + |
| 3264 | PAGE_CACHE_SIZE) >> |
| 3265 | PAGE_CACHE_SHIFT; |
| 3266 | delalloc_start = delalloc_end + 1; |
| 3267 | } |
| 3268 | if (wbc->nr_to_write < delalloc_to_write) { |
| 3269 | int thresh = 8192; |
| 3270 | |
| 3271 | if (delalloc_to_write < thresh * 2) |
| 3272 | thresh = delalloc_to_write; |
| 3273 | wbc->nr_to_write = min_t(u64, delalloc_to_write, |
| 3274 | thresh); |
| 3275 | } |
| 3276 | |
| 3277 | /* did the fill delalloc function already unlock and start |
| 3278 | * the IO? |
| 3279 | */ |
| 3280 | if (page_started) { |
| 3281 | /* |
| 3282 | * we've unlocked the page, so we can't update |
| 3283 | * the mapping's writeback index, just update |
| 3284 | * nr_to_write. |
| 3285 | */ |
| 3286 | wbc->nr_to_write -= *nr_written; |
| 3287 | return 1; |
| 3288 | } |
| 3289 | |
| 3290 | ret = 0; |
| 3291 | |
| 3292 | done: |
| 3293 | return ret; |
| 3294 | } |
| 3295 | |
| 3296 | /* |
| 3297 | * helper for __extent_writepage. This calls the writepage start hooks, |
| 3298 | * and does the loop to map the page into extents and bios. |
| 3299 | * |
| 3300 | * We return 1 if the IO is started and the page is unlocked, |
| 3301 | * 0 if all went well (page still locked) |
| 3302 | * < 0 if there were errors (page still locked) |
| 3303 | */ |
| 3304 | static noinline_for_stack int __extent_writepage_io(struct inode *inode, |
| 3305 | struct page *page, |
| 3306 | struct writeback_control *wbc, |
| 3307 | struct extent_page_data *epd, |
| 3308 | loff_t i_size, |
| 3309 | unsigned long nr_written, |
| 3310 | int write_flags, int *nr_ret) |
| 3311 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3312 | struct extent_io_tree *tree = epd->tree; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3313 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3314 | u64 page_end = start + PAGE_CACHE_SIZE - 1; |
| 3315 | u64 end; |
| 3316 | u64 cur = start; |
| 3317 | u64 extent_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3318 | u64 block_start; |
| 3319 | u64 iosize; |
| 3320 | sector_t sector; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 3321 | struct extent_state *cached_state = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3322 | struct extent_map *em; |
| 3323 | struct block_device *bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3324 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3325 | size_t blocksize; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3326 | int ret = 0; |
| 3327 | int nr = 0; |
| 3328 | bool compressed; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3329 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3330 | if (tree->ops && tree->ops->writepage_start_hook) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3331 | ret = tree->ops->writepage_start_hook(page, start, |
| 3332 | page_end); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 3333 | if (ret) { |
| 3334 | /* Fixup worker will requeue */ |
| 3335 | if (ret == -EBUSY) |
| 3336 | wbc->pages_skipped++; |
| 3337 | else |
| 3338 | redirty_page_for_writepage(wbc, page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3339 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3340 | update_nr_written(page, wbc, nr_written); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3341 | unlock_page(page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3342 | ret = 1; |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3343 | goto done_unlocked; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3344 | } |
| 3345 | } |
| 3346 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3347 | /* |
| 3348 | * we don't want to touch the inode after unlocking the page, |
| 3349 | * so we update the mapping writeback index now |
| 3350 | */ |
| 3351 | update_nr_written(page, wbc, nr_written + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3352 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3353 | end = page_end; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3354 | if (i_size <= start) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3355 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 3356 | tree->ops->writepage_end_io_hook(page, start, |
| 3357 | page_end, NULL, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3358 | goto done; |
| 3359 | } |
| 3360 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3361 | blocksize = inode->i_sb->s_blocksize; |
| 3362 | |
| 3363 | while (cur <= end) { |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3364 | u64 em_end; |
| 3365 | if (cur >= i_size) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3366 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 3367 | tree->ops->writepage_end_io_hook(page, cur, |
| 3368 | page_end, NULL, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3369 | break; |
| 3370 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3371 | em = epd->get_extent(inode, page, pg_offset, cur, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3372 | end - cur + 1, 1); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3373 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3374 | SetPageError(page); |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 3375 | ret = PTR_ERR_OR_ZERO(em); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3376 | break; |
| 3377 | } |
| 3378 | |
| 3379 | extent_offset = cur - em->start; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3380 | em_end = extent_map_end(em); |
| 3381 | BUG_ON(em_end <= cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3382 | BUG_ON(end < cur); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3383 | iosize = min(em_end - cur, end - cur + 1); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3384 | iosize = ALIGN(iosize, blocksize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3385 | sector = (em->block_start + extent_offset) >> 9; |
| 3386 | bdev = em->bdev; |
| 3387 | block_start = em->block_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3388 | compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3389 | free_extent_map(em); |
| 3390 | em = NULL; |
| 3391 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3392 | /* |
| 3393 | * compressed and inline extents are written through other |
| 3394 | * paths in the FS |
| 3395 | */ |
| 3396 | if (compressed || block_start == EXTENT_MAP_HOLE || |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3397 | block_start == EXTENT_MAP_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3398 | /* |
| 3399 | * end_io notification does not happen here for |
| 3400 | * compressed extents |
| 3401 | */ |
| 3402 | if (!compressed && tree->ops && |
| 3403 | tree->ops->writepage_end_io_hook) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3404 | tree->ops->writepage_end_io_hook(page, cur, |
| 3405 | cur + iosize - 1, |
| 3406 | NULL, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3407 | else if (compressed) { |
| 3408 | /* we don't want to end_page_writeback on |
| 3409 | * a compressed extent. this happens |
| 3410 | * elsewhere |
| 3411 | */ |
| 3412 | nr++; |
| 3413 | } |
| 3414 | |
| 3415 | cur += iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3416 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3417 | continue; |
| 3418 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3419 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3420 | if (tree->ops && tree->ops->writepage_io_hook) { |
| 3421 | ret = tree->ops->writepage_io_hook(page, cur, |
| 3422 | cur + iosize - 1); |
| 3423 | } else { |
| 3424 | ret = 0; |
| 3425 | } |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 3426 | if (ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3427 | SetPageError(page); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 3428 | } else { |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3429 | unsigned long max_nr = (i_size >> PAGE_CACHE_SHIFT) + 1; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3430 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3431 | set_range_writeback(tree, cur, cur + iosize - 1); |
| 3432 | if (!PageWriteback(page)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3433 | btrfs_err(BTRFS_I(inode)->root->fs_info, |
| 3434 | "page %lu not writeback, cur %llu end %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3435 | page->index, cur, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3436 | } |
| 3437 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3438 | ret = submit_extent_page(write_flags, tree, page, |
| 3439 | sector, iosize, pg_offset, |
| 3440 | bdev, &epd->bio, max_nr, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3441 | end_bio_extent_writepage, |
| 3442 | 0, 0, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3443 | if (ret) |
| 3444 | SetPageError(page); |
| 3445 | } |
| 3446 | cur = cur + iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3447 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3448 | nr++; |
| 3449 | } |
| 3450 | done: |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3451 | *nr_ret = nr; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3452 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3453 | done_unlocked: |
| 3454 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 3455 | /* drop our reference on any cached states */ |
| 3456 | free_extent_state(cached_state); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3457 | return ret; |
| 3458 | } |
| 3459 | |
| 3460 | /* |
| 3461 | * the writepage semantics are similar to regular writepage. extent |
| 3462 | * records are inserted to lock ranges in the tree, and as dirty areas |
| 3463 | * are found, they are marked writeback. Then the lock bits are removed |
| 3464 | * and the end_io handler clears the writeback ranges |
| 3465 | */ |
| 3466 | static int __extent_writepage(struct page *page, struct writeback_control *wbc, |
| 3467 | void *data) |
| 3468 | { |
| 3469 | struct inode *inode = page->mapping->host; |
| 3470 | struct extent_page_data *epd = data; |
| 3471 | u64 start = page_offset(page); |
| 3472 | u64 page_end = start + PAGE_CACHE_SIZE - 1; |
| 3473 | int ret; |
| 3474 | int nr = 0; |
| 3475 | size_t pg_offset = 0; |
| 3476 | loff_t i_size = i_size_read(inode); |
| 3477 | unsigned long end_index = i_size >> PAGE_CACHE_SHIFT; |
| 3478 | int write_flags; |
| 3479 | unsigned long nr_written = 0; |
| 3480 | |
| 3481 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3482 | write_flags = WRITE_SYNC; |
| 3483 | else |
| 3484 | write_flags = WRITE; |
| 3485 | |
| 3486 | trace___extent_writepage(page, inode, wbc); |
| 3487 | |
| 3488 | WARN_ON(!PageLocked(page)); |
| 3489 | |
| 3490 | ClearPageError(page); |
| 3491 | |
| 3492 | pg_offset = i_size & (PAGE_CACHE_SIZE - 1); |
| 3493 | if (page->index > end_index || |
| 3494 | (page->index == end_index && !pg_offset)) { |
| 3495 | page->mapping->a_ops->invalidatepage(page, 0, PAGE_CACHE_SIZE); |
| 3496 | unlock_page(page); |
| 3497 | return 0; |
| 3498 | } |
| 3499 | |
| 3500 | if (page->index == end_index) { |
| 3501 | char *userpage; |
| 3502 | |
| 3503 | userpage = kmap_atomic(page); |
| 3504 | memset(userpage + pg_offset, 0, |
| 3505 | PAGE_CACHE_SIZE - pg_offset); |
| 3506 | kunmap_atomic(userpage); |
| 3507 | flush_dcache_page(page); |
| 3508 | } |
| 3509 | |
| 3510 | pg_offset = 0; |
| 3511 | |
| 3512 | set_page_extent_mapped(page); |
| 3513 | |
| 3514 | ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written); |
| 3515 | if (ret == 1) |
| 3516 | goto done_unlocked; |
| 3517 | if (ret) |
| 3518 | goto done; |
| 3519 | |
| 3520 | ret = __extent_writepage_io(inode, page, wbc, epd, |
| 3521 | i_size, nr_written, write_flags, &nr); |
| 3522 | if (ret == 1) |
| 3523 | goto done_unlocked; |
| 3524 | |
| 3525 | done: |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3526 | if (nr == 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3527 | /* make sure the mapping tag for page dirty gets cleared */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3528 | set_page_writeback(page); |
| 3529 | end_page_writeback(page); |
| 3530 | } |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 3531 | if (PageError(page)) { |
| 3532 | ret = ret < 0 ? ret : -EIO; |
| 3533 | end_extent_writepage(page, ret, start, page_end); |
| 3534 | } |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 3535 | unlock_page(page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3536 | return ret; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3537 | |
| 3538 | done_unlocked: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3539 | return 0; |
| 3540 | } |
| 3541 | |
Josef Bacik | fd8b2b6 | 2013-04-24 16:41:19 -0400 | [diff] [blame] | 3542 | void wait_on_extent_buffer_writeback(struct extent_buffer *eb) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3543 | { |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 3544 | wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK, |
| 3545 | TASK_UNINTERRUPTIBLE); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3546 | } |
| 3547 | |
Chris Mason | 0e378df | 2014-05-19 20:55:27 -0700 | [diff] [blame] | 3548 | static noinline_for_stack int |
| 3549 | lock_extent_buffer_for_io(struct extent_buffer *eb, |
| 3550 | struct btrfs_fs_info *fs_info, |
| 3551 | struct extent_page_data *epd) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3552 | { |
| 3553 | unsigned long i, num_pages; |
| 3554 | int flush = 0; |
| 3555 | int ret = 0; |
| 3556 | |
| 3557 | if (!btrfs_try_tree_write_lock(eb)) { |
| 3558 | flush = 1; |
| 3559 | flush_write_bio(epd); |
| 3560 | btrfs_tree_lock(eb); |
| 3561 | } |
| 3562 | |
| 3563 | if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) { |
| 3564 | btrfs_tree_unlock(eb); |
| 3565 | if (!epd->sync_io) |
| 3566 | return 0; |
| 3567 | if (!flush) { |
| 3568 | flush_write_bio(epd); |
| 3569 | flush = 1; |
| 3570 | } |
Chris Mason | a098d8e | 2012-03-21 12:09:56 -0400 | [diff] [blame] | 3571 | while (1) { |
| 3572 | wait_on_extent_buffer_writeback(eb); |
| 3573 | btrfs_tree_lock(eb); |
| 3574 | if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) |
| 3575 | break; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3576 | btrfs_tree_unlock(eb); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3577 | } |
| 3578 | } |
| 3579 | |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3580 | /* |
| 3581 | * We need to do this to prevent races in people who check if the eb is |
| 3582 | * under IO since we can end up having no IO bits set for a short period |
| 3583 | * of time. |
| 3584 | */ |
| 3585 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3586 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) { |
| 3587 | set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3588 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3589 | btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); |
Miao Xie | e2d8452 | 2013-01-29 10:09:20 +0000 | [diff] [blame] | 3590 | __percpu_counter_add(&fs_info->dirty_metadata_bytes, |
| 3591 | -eb->len, |
| 3592 | fs_info->dirty_metadata_batch); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3593 | ret = 1; |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3594 | } else { |
| 3595 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3596 | } |
| 3597 | |
| 3598 | btrfs_tree_unlock(eb); |
| 3599 | |
| 3600 | if (!ret) |
| 3601 | return ret; |
| 3602 | |
| 3603 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3604 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 3605 | struct page *p = eb->pages[i]; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3606 | |
| 3607 | if (!trylock_page(p)) { |
| 3608 | if (!flush) { |
| 3609 | flush_write_bio(epd); |
| 3610 | flush = 1; |
| 3611 | } |
| 3612 | lock_page(p); |
| 3613 | } |
| 3614 | } |
| 3615 | |
| 3616 | return ret; |
| 3617 | } |
| 3618 | |
| 3619 | static void end_extent_buffer_writeback(struct extent_buffer *eb) |
| 3620 | { |
| 3621 | clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 3622 | smp_mb__after_atomic(); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3623 | wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK); |
| 3624 | } |
| 3625 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3626 | static void set_btree_ioerr(struct page *page) |
| 3627 | { |
| 3628 | struct extent_buffer *eb = (struct extent_buffer *)page->private; |
| 3629 | struct btrfs_inode *btree_ino = BTRFS_I(eb->fs_info->btree_inode); |
| 3630 | |
| 3631 | SetPageError(page); |
| 3632 | if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) |
| 3633 | return; |
| 3634 | |
| 3635 | /* |
| 3636 | * If writeback for a btree extent that doesn't belong to a log tree |
| 3637 | * failed, increment the counter transaction->eb_write_errors. |
| 3638 | * We do this because while the transaction is running and before it's |
| 3639 | * committing (when we call filemap_fdata[write|wait]_range against |
| 3640 | * the btree inode), we might have |
| 3641 | * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it |
| 3642 | * returns an error or an error happens during writeback, when we're |
| 3643 | * committing the transaction we wouldn't know about it, since the pages |
| 3644 | * can be no longer dirty nor marked anymore for writeback (if a |
| 3645 | * subsequent modification to the extent buffer didn't happen before the |
| 3646 | * transaction commit), which makes filemap_fdata[write|wait]_range not |
| 3647 | * able to find the pages tagged with SetPageError at transaction |
| 3648 | * commit time. So if this happens we must abort the transaction, |
| 3649 | * otherwise we commit a super block with btree roots that point to |
| 3650 | * btree nodes/leafs whose content on disk is invalid - either garbage |
| 3651 | * or the content of some node/leaf from a past generation that got |
| 3652 | * cowed or deleted and is no longer valid. |
| 3653 | * |
| 3654 | * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would |
| 3655 | * not be enough - we need to distinguish between log tree extents vs |
| 3656 | * non-log tree extents, and the next filemap_fdatawait_range() call |
| 3657 | * will catch and clear such errors in the mapping - and that call might |
| 3658 | * be from a log sync and not from a transaction commit. Also, checking |
| 3659 | * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is |
| 3660 | * not done and would not be reliable - the eb might have been released |
| 3661 | * from memory and reading it back again means that flag would not be |
| 3662 | * set (since it's a runtime flag, not persisted on disk). |
| 3663 | * |
| 3664 | * Using the flags below in the btree inode also makes us achieve the |
| 3665 | * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started |
| 3666 | * writeback for all dirty pages and before filemap_fdatawait_range() |
| 3667 | * is called, the writeback for all dirty pages had already finished |
| 3668 | * with errors - because we were not using AS_EIO/AS_ENOSPC, |
| 3669 | * filemap_fdatawait_range() would return success, as it could not know |
| 3670 | * that writeback errors happened (the pages were no longer tagged for |
| 3671 | * writeback). |
| 3672 | */ |
| 3673 | switch (eb->log_index) { |
| 3674 | case -1: |
| 3675 | set_bit(BTRFS_INODE_BTREE_ERR, &btree_ino->runtime_flags); |
| 3676 | break; |
| 3677 | case 0: |
| 3678 | set_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags); |
| 3679 | break; |
| 3680 | case 1: |
| 3681 | set_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags); |
| 3682 | break; |
| 3683 | default: |
| 3684 | BUG(); /* unexpected, logic error */ |
| 3685 | } |
| 3686 | } |
| 3687 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3688 | static void end_bio_extent_buffer_writepage(struct bio *bio, int err) |
| 3689 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3690 | struct bio_vec *bvec; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3691 | struct extent_buffer *eb; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3692 | int i, done; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3693 | |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3694 | bio_for_each_segment_all(bvec, bio, i) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3695 | struct page *page = bvec->bv_page; |
| 3696 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3697 | eb = (struct extent_buffer *)page->private; |
| 3698 | BUG_ON(!eb); |
| 3699 | done = atomic_dec_and_test(&eb->io_pages); |
| 3700 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3701 | if (err || test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3702 | ClearPageUptodate(page); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3703 | set_btree_ioerr(page); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3704 | } |
| 3705 | |
| 3706 | end_page_writeback(page); |
| 3707 | |
| 3708 | if (!done) |
| 3709 | continue; |
| 3710 | |
| 3711 | end_extent_buffer_writeback(eb); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3712 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3713 | |
| 3714 | bio_put(bio); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3715 | } |
| 3716 | |
Chris Mason | 0e378df | 2014-05-19 20:55:27 -0700 | [diff] [blame] | 3717 | static noinline_for_stack int write_one_eb(struct extent_buffer *eb, |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3718 | struct btrfs_fs_info *fs_info, |
| 3719 | struct writeback_control *wbc, |
| 3720 | struct extent_page_data *epd) |
| 3721 | { |
| 3722 | struct block_device *bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 3723 | struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3724 | u64 offset = eb->start; |
| 3725 | unsigned long i, num_pages; |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3726 | unsigned long bio_flags = 0; |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 3727 | int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META; |
Josef Bacik | d7dbe9e | 2012-04-23 14:00:51 -0400 | [diff] [blame] | 3728 | int ret = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3729 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3730 | clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3731 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3732 | atomic_set(&eb->io_pages, num_pages); |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3733 | if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID) |
| 3734 | bio_flags = EXTENT_BIO_TREE_LOG; |
| 3735 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3736 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 3737 | struct page *p = eb->pages[i]; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3738 | |
| 3739 | clear_page_dirty_for_io(p); |
| 3740 | set_page_writeback(p); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 3741 | ret = submit_extent_page(rw, tree, p, offset >> 9, |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3742 | PAGE_CACHE_SIZE, 0, bdev, &epd->bio, |
| 3743 | -1, end_bio_extent_buffer_writepage, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3744 | 0, epd->bio_flags, bio_flags); |
| 3745 | epd->bio_flags = bio_flags; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3746 | if (ret) { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3747 | set_btree_ioerr(p); |
Filipe Manana | 55e3bd2 | 2014-09-22 17:41:04 +0100 | [diff] [blame] | 3748 | end_page_writeback(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3749 | if (atomic_sub_and_test(num_pages - i, &eb->io_pages)) |
| 3750 | end_extent_buffer_writeback(eb); |
| 3751 | ret = -EIO; |
| 3752 | break; |
| 3753 | } |
| 3754 | offset += PAGE_CACHE_SIZE; |
| 3755 | update_nr_written(p, wbc, 1); |
| 3756 | unlock_page(p); |
| 3757 | } |
| 3758 | |
| 3759 | if (unlikely(ret)) { |
| 3760 | for (; i < num_pages; i++) { |
Chris Mason | bbf65cf | 2014-10-04 09:56:45 -0700 | [diff] [blame] | 3761 | struct page *p = eb->pages[i]; |
Liu Bo | 8146502 | 2014-09-23 22:22:33 +0800 | [diff] [blame] | 3762 | clear_page_dirty_for_io(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3763 | unlock_page(p); |
| 3764 | } |
| 3765 | } |
| 3766 | |
| 3767 | return ret; |
| 3768 | } |
| 3769 | |
| 3770 | int btree_write_cache_pages(struct address_space *mapping, |
| 3771 | struct writeback_control *wbc) |
| 3772 | { |
| 3773 | struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree; |
| 3774 | struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info; |
| 3775 | struct extent_buffer *eb, *prev_eb = NULL; |
| 3776 | struct extent_page_data epd = { |
| 3777 | .bio = NULL, |
| 3778 | .tree = tree, |
| 3779 | .extent_locked = 0, |
| 3780 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3781 | .bio_flags = 0, |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3782 | }; |
| 3783 | int ret = 0; |
| 3784 | int done = 0; |
| 3785 | int nr_to_write_done = 0; |
| 3786 | struct pagevec pvec; |
| 3787 | int nr_pages; |
| 3788 | pgoff_t index; |
| 3789 | pgoff_t end; /* Inclusive */ |
| 3790 | int scanned = 0; |
| 3791 | int tag; |
| 3792 | |
| 3793 | pagevec_init(&pvec, 0); |
| 3794 | if (wbc->range_cyclic) { |
| 3795 | index = mapping->writeback_index; /* Start from prev offset */ |
| 3796 | end = -1; |
| 3797 | } else { |
| 3798 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 3799 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
| 3800 | scanned = 1; |
| 3801 | } |
| 3802 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3803 | tag = PAGECACHE_TAG_TOWRITE; |
| 3804 | else |
| 3805 | tag = PAGECACHE_TAG_DIRTY; |
| 3806 | retry: |
| 3807 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3808 | tag_pages_for_writeback(mapping, index, end); |
| 3809 | while (!done && !nr_to_write_done && (index <= end) && |
| 3810 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
| 3811 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { |
| 3812 | unsigned i; |
| 3813 | |
| 3814 | scanned = 1; |
| 3815 | for (i = 0; i < nr_pages; i++) { |
| 3816 | struct page *page = pvec.pages[i]; |
| 3817 | |
| 3818 | if (!PagePrivate(page)) |
| 3819 | continue; |
| 3820 | |
| 3821 | if (!wbc->range_cyclic && page->index > end) { |
| 3822 | done = 1; |
| 3823 | break; |
| 3824 | } |
| 3825 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3826 | spin_lock(&mapping->private_lock); |
| 3827 | if (!PagePrivate(page)) { |
| 3828 | spin_unlock(&mapping->private_lock); |
| 3829 | continue; |
| 3830 | } |
| 3831 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3832 | eb = (struct extent_buffer *)page->private; |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3833 | |
| 3834 | /* |
| 3835 | * Shouldn't happen and normally this would be a BUG_ON |
| 3836 | * but no sense in crashing the users box for something |
| 3837 | * we can survive anyway. |
| 3838 | */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3839 | if (WARN_ON(!eb)) { |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3840 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3841 | continue; |
| 3842 | } |
| 3843 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3844 | if (eb == prev_eb) { |
| 3845 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3846 | continue; |
| 3847 | } |
| 3848 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3849 | ret = atomic_inc_not_zero(&eb->refs); |
| 3850 | spin_unlock(&mapping->private_lock); |
| 3851 | if (!ret) |
| 3852 | continue; |
| 3853 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3854 | prev_eb = eb; |
| 3855 | ret = lock_extent_buffer_for_io(eb, fs_info, &epd); |
| 3856 | if (!ret) { |
| 3857 | free_extent_buffer(eb); |
| 3858 | continue; |
| 3859 | } |
| 3860 | |
| 3861 | ret = write_one_eb(eb, fs_info, wbc, &epd); |
| 3862 | if (ret) { |
| 3863 | done = 1; |
| 3864 | free_extent_buffer(eb); |
| 3865 | break; |
| 3866 | } |
| 3867 | free_extent_buffer(eb); |
| 3868 | |
| 3869 | /* |
| 3870 | * the filesystem may choose to bump up nr_to_write. |
| 3871 | * We have to make sure to honor the new nr_to_write |
| 3872 | * at any time |
| 3873 | */ |
| 3874 | nr_to_write_done = wbc->nr_to_write <= 0; |
| 3875 | } |
| 3876 | pagevec_release(&pvec); |
| 3877 | cond_resched(); |
| 3878 | } |
| 3879 | if (!scanned && !done) { |
| 3880 | /* |
| 3881 | * We hit the last page and there is more work to be done: wrap |
| 3882 | * back to the start of the file |
| 3883 | */ |
| 3884 | scanned = 1; |
| 3885 | index = 0; |
| 3886 | goto retry; |
| 3887 | } |
| 3888 | flush_write_bio(&epd); |
| 3889 | return ret; |
| 3890 | } |
| 3891 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3892 | /** |
| 3893 | * write_cache_pages - walk the list of dirty pages of the given address space and write all of them. |
| 3894 | * @mapping: address space structure to write |
| 3895 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
| 3896 | * @writepage: function called for each page |
| 3897 | * @data: data passed to writepage function |
| 3898 | * |
| 3899 | * If a page is already under I/O, write_cache_pages() skips it, even |
| 3900 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, |
| 3901 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() |
| 3902 | * and msync() need to guarantee that all the data which was dirty at the time |
| 3903 | * the call was made get new I/O started against them. If wbc->sync_mode is |
| 3904 | * WB_SYNC_ALL then we were called for data integrity and we must wait for |
| 3905 | * existing IO to complete. |
| 3906 | */ |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3907 | static int extent_write_cache_pages(struct extent_io_tree *tree, |
| 3908 | struct address_space *mapping, |
| 3909 | struct writeback_control *wbc, |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3910 | writepage_t writepage, void *data, |
| 3911 | void (*flush_fn)(void *)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3912 | { |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 3913 | struct inode *inode = mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3914 | int ret = 0; |
| 3915 | int done = 0; |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 3916 | int err = 0; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3917 | int nr_to_write_done = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3918 | struct pagevec pvec; |
| 3919 | int nr_pages; |
| 3920 | pgoff_t index; |
| 3921 | pgoff_t end; /* Inclusive */ |
| 3922 | int scanned = 0; |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3923 | int tag; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3924 | |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 3925 | /* |
| 3926 | * We have to hold onto the inode so that ordered extents can do their |
| 3927 | * work when the IO finishes. The alternative to this is failing to add |
| 3928 | * an ordered extent if the igrab() fails there and that is a huge pain |
| 3929 | * to deal with, so instead just hold onto the inode throughout the |
| 3930 | * writepages operation. If it fails here we are freeing up the inode |
| 3931 | * anyway and we'd rather not waste our time writing out stuff that is |
| 3932 | * going to be truncated anyway. |
| 3933 | */ |
| 3934 | if (!igrab(inode)) |
| 3935 | return 0; |
| 3936 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3937 | pagevec_init(&pvec, 0); |
| 3938 | if (wbc->range_cyclic) { |
| 3939 | index = mapping->writeback_index; /* Start from prev offset */ |
| 3940 | end = -1; |
| 3941 | } else { |
| 3942 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 3943 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3944 | scanned = 1; |
| 3945 | } |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3946 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3947 | tag = PAGECACHE_TAG_TOWRITE; |
| 3948 | else |
| 3949 | tag = PAGECACHE_TAG_DIRTY; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3950 | retry: |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3951 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3952 | tag_pages_for_writeback(mapping, index, end); |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3953 | while (!done && !nr_to_write_done && (index <= end) && |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3954 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
| 3955 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3956 | unsigned i; |
| 3957 | |
| 3958 | scanned = 1; |
| 3959 | for (i = 0; i < nr_pages; i++) { |
| 3960 | struct page *page = pvec.pages[i]; |
| 3961 | |
| 3962 | /* |
| 3963 | * At this point we hold neither mapping->tree_lock nor |
| 3964 | * lock on the page itself: the page may be truncated or |
| 3965 | * invalidated (changing page->mapping to NULL), or even |
| 3966 | * swizzled back from swapper_space to tmpfs file |
| 3967 | * mapping |
| 3968 | */ |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3969 | if (!trylock_page(page)) { |
| 3970 | flush_fn(data); |
| 3971 | lock_page(page); |
Chris Mason | 01d658f | 2011-11-01 10:08:06 -0400 | [diff] [blame] | 3972 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3973 | |
| 3974 | if (unlikely(page->mapping != mapping)) { |
| 3975 | unlock_page(page); |
| 3976 | continue; |
| 3977 | } |
| 3978 | |
| 3979 | if (!wbc->range_cyclic && page->index > end) { |
| 3980 | done = 1; |
| 3981 | unlock_page(page); |
| 3982 | continue; |
| 3983 | } |
| 3984 | |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3985 | if (wbc->sync_mode != WB_SYNC_NONE) { |
Chris Mason | 0e6bd95 | 2008-11-20 10:46:35 -0500 | [diff] [blame] | 3986 | if (PageWriteback(page)) |
| 3987 | flush_fn(data); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3988 | wait_on_page_writeback(page); |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3989 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3990 | |
| 3991 | if (PageWriteback(page) || |
| 3992 | !clear_page_dirty_for_io(page)) { |
| 3993 | unlock_page(page); |
| 3994 | continue; |
| 3995 | } |
| 3996 | |
| 3997 | ret = (*writepage)(page, wbc, data); |
| 3998 | |
| 3999 | if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) { |
| 4000 | unlock_page(page); |
| 4001 | ret = 0; |
| 4002 | } |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 4003 | if (!err && ret < 0) |
| 4004 | err = ret; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 4005 | |
| 4006 | /* |
| 4007 | * the filesystem may choose to bump up nr_to_write. |
| 4008 | * We have to make sure to honor the new nr_to_write |
| 4009 | * at any time |
| 4010 | */ |
| 4011 | nr_to_write_done = wbc->nr_to_write <= 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4012 | } |
| 4013 | pagevec_release(&pvec); |
| 4014 | cond_resched(); |
| 4015 | } |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 4016 | if (!scanned && !done && !err) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4017 | /* |
| 4018 | * We hit the last page and there is more work to be done: wrap |
| 4019 | * back to the start of the file |
| 4020 | */ |
| 4021 | scanned = 1; |
| 4022 | index = 0; |
| 4023 | goto retry; |
| 4024 | } |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 4025 | btrfs_add_delayed_iput(inode); |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 4026 | return err; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4027 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4028 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4029 | static void flush_epd_write_bio(struct extent_page_data *epd) |
| 4030 | { |
| 4031 | if (epd->bio) { |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 4032 | int rw = WRITE; |
| 4033 | int ret; |
| 4034 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4035 | if (epd->sync_io) |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 4036 | rw = WRITE_SYNC; |
| 4037 | |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 4038 | ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4039 | BUG_ON(ret < 0); /* -ENOMEM */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4040 | epd->bio = NULL; |
| 4041 | } |
| 4042 | } |
| 4043 | |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4044 | static noinline void flush_write_bio(void *data) |
| 4045 | { |
| 4046 | struct extent_page_data *epd = data; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4047 | flush_epd_write_bio(epd); |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4048 | } |
| 4049 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4050 | int extent_write_full_page(struct extent_io_tree *tree, struct page *page, |
| 4051 | get_extent_t *get_extent, |
| 4052 | struct writeback_control *wbc) |
| 4053 | { |
| 4054 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4055 | struct extent_page_data epd = { |
| 4056 | .bio = NULL, |
| 4057 | .tree = tree, |
| 4058 | .get_extent = get_extent, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4059 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4060 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 4061 | .bio_flags = 0, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4062 | }; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4063 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4064 | ret = __extent_writepage(page, wbc, &epd); |
| 4065 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4066 | flush_epd_write_bio(&epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4067 | return ret; |
| 4068 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4069 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4070 | int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode, |
| 4071 | u64 start, u64 end, get_extent_t *get_extent, |
| 4072 | int mode) |
| 4073 | { |
| 4074 | int ret = 0; |
| 4075 | struct address_space *mapping = inode->i_mapping; |
| 4076 | struct page *page; |
| 4077 | unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >> |
| 4078 | PAGE_CACHE_SHIFT; |
| 4079 | |
| 4080 | struct extent_page_data epd = { |
| 4081 | .bio = NULL, |
| 4082 | .tree = tree, |
| 4083 | .get_extent = get_extent, |
| 4084 | .extent_locked = 1, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4085 | .sync_io = mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 4086 | .bio_flags = 0, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4087 | }; |
| 4088 | struct writeback_control wbc_writepages = { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4089 | .sync_mode = mode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4090 | .nr_to_write = nr_pages * 2, |
| 4091 | .range_start = start, |
| 4092 | .range_end = end + 1, |
| 4093 | }; |
| 4094 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4095 | while (start <= end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4096 | page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT); |
| 4097 | if (clear_page_dirty_for_io(page)) |
| 4098 | ret = __extent_writepage(page, &wbc_writepages, &epd); |
| 4099 | else { |
| 4100 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 4101 | tree->ops->writepage_end_io_hook(page, start, |
| 4102 | start + PAGE_CACHE_SIZE - 1, |
| 4103 | NULL, 1); |
| 4104 | unlock_page(page); |
| 4105 | } |
| 4106 | page_cache_release(page); |
| 4107 | start += PAGE_CACHE_SIZE; |
| 4108 | } |
| 4109 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4110 | flush_epd_write_bio(&epd); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4111 | return ret; |
| 4112 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4113 | |
| 4114 | int extent_writepages(struct extent_io_tree *tree, |
| 4115 | struct address_space *mapping, |
| 4116 | get_extent_t *get_extent, |
| 4117 | struct writeback_control *wbc) |
| 4118 | { |
| 4119 | int ret = 0; |
| 4120 | struct extent_page_data epd = { |
| 4121 | .bio = NULL, |
| 4122 | .tree = tree, |
| 4123 | .get_extent = get_extent, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4124 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4125 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 4126 | .bio_flags = 0, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4127 | }; |
| 4128 | |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 4129 | ret = extent_write_cache_pages(tree, mapping, wbc, |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4130 | __extent_writepage, &epd, |
| 4131 | flush_write_bio); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4132 | flush_epd_write_bio(&epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4133 | return ret; |
| 4134 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4135 | |
| 4136 | int extent_readpages(struct extent_io_tree *tree, |
| 4137 | struct address_space *mapping, |
| 4138 | struct list_head *pages, unsigned nr_pages, |
| 4139 | get_extent_t get_extent) |
| 4140 | { |
| 4141 | struct bio *bio = NULL; |
| 4142 | unsigned page_idx; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4143 | unsigned long bio_flags = 0; |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4144 | struct page *pagepool[16]; |
| 4145 | struct page *page; |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4146 | struct extent_map *em_cached = NULL; |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4147 | int nr = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4148 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4149 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4150 | page = list_entry(pages->prev, struct page, lru); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4151 | |
| 4152 | prefetchw(&page->flags); |
| 4153 | list_del(&page->lru); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4154 | if (add_to_page_cache_lru(page, mapping, |
Itaru Kitayama | 43e817a | 2011-04-25 19:43:51 -0400 | [diff] [blame] | 4155 | page->index, GFP_NOFS)) { |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4156 | page_cache_release(page); |
| 4157 | continue; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4158 | } |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4159 | |
| 4160 | pagepool[nr++] = page; |
| 4161 | if (nr < ARRAY_SIZE(pagepool)) |
| 4162 | continue; |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4163 | __extent_readpages(tree, pagepool, nr, get_extent, &em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 4164 | &bio, 0, &bio_flags, READ); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4165 | nr = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4166 | } |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 4167 | if (nr) |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4168 | __extent_readpages(tree, pagepool, nr, get_extent, &em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 4169 | &bio, 0, &bio_flags, READ); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4170 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4171 | if (em_cached) |
| 4172 | free_extent_map(em_cached); |
| 4173 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4174 | BUG_ON(!list_empty(pages)); |
| 4175 | if (bio) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4176 | return submit_one_bio(READ, bio, 0, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4177 | return 0; |
| 4178 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4179 | |
| 4180 | /* |
| 4181 | * basic invalidatepage code, this waits on any locked or writeback |
| 4182 | * ranges corresponding to the page, and then deletes any extent state |
| 4183 | * records from the tree |
| 4184 | */ |
| 4185 | int extent_invalidatepage(struct extent_io_tree *tree, |
| 4186 | struct page *page, unsigned long offset) |
| 4187 | { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4188 | struct extent_state *cached_state = NULL; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4189 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4190 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 4191 | size_t blocksize = page->mapping->host->i_sb->s_blocksize; |
| 4192 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4193 | start += ALIGN(offset, blocksize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4194 | if (start > end) |
| 4195 | return 0; |
| 4196 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4197 | lock_extent_bits(tree, start, end, 0, &cached_state); |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 4198 | wait_on_page_writeback(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4199 | clear_extent_bit(tree, start, end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 4200 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4201 | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4202 | 1, 1, &cached_state, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4203 | return 0; |
| 4204 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4205 | |
| 4206 | /* |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4207 | * a helper for releasepage, this tests for areas of the page that |
| 4208 | * are locked or under IO and drops the related state bits if it is safe |
| 4209 | * to drop the page. |
| 4210 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 4211 | static int try_release_extent_state(struct extent_map_tree *map, |
| 4212 | struct extent_io_tree *tree, |
| 4213 | struct page *page, gfp_t mask) |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4214 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4215 | u64 start = page_offset(page); |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4216 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 4217 | int ret = 1; |
| 4218 | |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 4219 | if (test_range_bit(tree, start, end, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4220 | EXTENT_IOBITS, 0, NULL)) |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4221 | ret = 0; |
| 4222 | else { |
| 4223 | if ((mask & GFP_NOFS) == GFP_NOFS) |
| 4224 | mask = GFP_NOFS; |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 4225 | /* |
| 4226 | * at this point we can safely clear everything except the |
| 4227 | * locked bit and the nodatasum bit |
| 4228 | */ |
Chris Mason | e3f24cc | 2011-02-14 12:52:08 -0500 | [diff] [blame] | 4229 | ret = clear_extent_bit(tree, start, end, |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 4230 | ~(EXTENT_LOCKED | EXTENT_NODATASUM), |
| 4231 | 0, 0, NULL, mask); |
Chris Mason | e3f24cc | 2011-02-14 12:52:08 -0500 | [diff] [blame] | 4232 | |
| 4233 | /* if clear_extent_bit failed for enomem reasons, |
| 4234 | * we can't allow the release to continue. |
| 4235 | */ |
| 4236 | if (ret < 0) |
| 4237 | ret = 0; |
| 4238 | else |
| 4239 | ret = 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4240 | } |
| 4241 | return ret; |
| 4242 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4243 | |
| 4244 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4245 | * a helper for releasepage. As long as there are no locked extents |
| 4246 | * in the range corresponding to the page, both state records and extent |
| 4247 | * map records are removed |
| 4248 | */ |
| 4249 | int try_release_extent_mapping(struct extent_map_tree *map, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4250 | struct extent_io_tree *tree, struct page *page, |
| 4251 | gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4252 | { |
| 4253 | struct extent_map *em; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4254 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4255 | u64 end = start + PAGE_CACHE_SIZE - 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4256 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4257 | if ((mask & __GFP_WAIT) && |
| 4258 | page->mapping->host->i_size > 16 * 1024 * 1024) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4259 | u64 len; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4260 | while (start <= end) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4261 | len = end - start + 1; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4262 | write_lock(&map->lock); |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4263 | em = lookup_extent_mapping(map, start, len); |
Tsutomu Itoh | 285190d | 2012-02-16 16:23:58 +0900 | [diff] [blame] | 4264 | if (!em) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4265 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4266 | break; |
| 4267 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 4268 | if (test_bit(EXTENT_FLAG_PINNED, &em->flags) || |
| 4269 | em->start != start) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4270 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4271 | free_extent_map(em); |
| 4272 | break; |
| 4273 | } |
| 4274 | if (!test_range_bit(tree, em->start, |
| 4275 | extent_map_end(em) - 1, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4276 | EXTENT_LOCKED | EXTENT_WRITEBACK, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 4277 | 0, NULL)) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4278 | remove_extent_mapping(map, em); |
| 4279 | /* once for the rb tree */ |
| 4280 | free_extent_map(em); |
| 4281 | } |
| 4282 | start = extent_map_end(em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4283 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4284 | |
| 4285 | /* once for us */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4286 | free_extent_map(em); |
| 4287 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4288 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4289 | return try_release_extent_state(map, tree, page, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4290 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4291 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4292 | /* |
| 4293 | * helper function for fiemap, which doesn't want to see any holes. |
| 4294 | * This maps until we find something past 'last' |
| 4295 | */ |
| 4296 | static struct extent_map *get_extent_skip_holes(struct inode *inode, |
| 4297 | u64 offset, |
| 4298 | u64 last, |
| 4299 | get_extent_t *get_extent) |
| 4300 | { |
| 4301 | u64 sectorsize = BTRFS_I(inode)->root->sectorsize; |
| 4302 | struct extent_map *em; |
| 4303 | u64 len; |
| 4304 | |
| 4305 | if (offset >= last) |
| 4306 | return NULL; |
| 4307 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 4308 | while (1) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4309 | len = last - offset; |
| 4310 | if (len == 0) |
| 4311 | break; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4312 | len = ALIGN(len, sectorsize); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4313 | em = get_extent(inode, NULL, 0, offset, len, 0); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 4314 | if (IS_ERR_OR_NULL(em)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4315 | return em; |
| 4316 | |
| 4317 | /* if this isn't a hole return it */ |
| 4318 | if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) && |
| 4319 | em->block_start != EXTENT_MAP_HOLE) { |
| 4320 | return em; |
| 4321 | } |
| 4322 | |
| 4323 | /* this is a hole, advance to the next extent */ |
| 4324 | offset = extent_map_end(em); |
| 4325 | free_extent_map(em); |
| 4326 | if (offset >= last) |
| 4327 | break; |
| 4328 | } |
| 4329 | return NULL; |
| 4330 | } |
| 4331 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4332 | int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4333 | __u64 start, __u64 len, get_extent_t *get_extent) |
| 4334 | { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4335 | int ret = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4336 | u64 off = start; |
| 4337 | u64 max = start + len; |
| 4338 | u32 flags = 0; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4339 | u32 found_type; |
| 4340 | u64 last; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4341 | u64 last_for_get_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4342 | u64 disko = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4343 | u64 isize = i_size_read(inode); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4344 | struct btrfs_key found_key; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4345 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4346 | struct extent_state *cached_state = NULL; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4347 | struct btrfs_path *path; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4348 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4349 | int end = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4350 | u64 em_start = 0; |
| 4351 | u64 em_len = 0; |
| 4352 | u64 em_end = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4353 | |
| 4354 | if (len == 0) |
| 4355 | return -EINVAL; |
| 4356 | |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4357 | path = btrfs_alloc_path(); |
| 4358 | if (!path) |
| 4359 | return -ENOMEM; |
| 4360 | path->leave_spinning = 1; |
| 4361 | |
Qu Wenruo | 2c91943 | 2014-07-18 09:55:43 +0800 | [diff] [blame] | 4362 | start = round_down(start, BTRFS_I(inode)->root->sectorsize); |
| 4363 | len = round_up(max, BTRFS_I(inode)->root->sectorsize) - start; |
Josef Bacik | 4d479cf | 2011-11-17 11:34:31 -0500 | [diff] [blame] | 4364 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4365 | /* |
| 4366 | * lookup the last file extent. We're not using i_size here |
| 4367 | * because there might be preallocation past i_size |
| 4368 | */ |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4369 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), -1, |
| 4370 | 0); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4371 | if (ret < 0) { |
| 4372 | btrfs_free_path(path); |
| 4373 | return ret; |
| 4374 | } |
| 4375 | WARN_ON(!ret); |
| 4376 | path->slots[0]--; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4377 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4378 | found_type = found_key.type; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4379 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4380 | /* No extents, but there might be delalloc bits */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4381 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4382 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4383 | /* have to trust i_size as the end */ |
| 4384 | last = (u64)-1; |
| 4385 | last_for_get_extent = isize; |
| 4386 | } else { |
| 4387 | /* |
| 4388 | * remember the start of the last extent. There are a |
| 4389 | * bunch of different factors that go into the length of the |
| 4390 | * extent, so its much less complex to remember where it started |
| 4391 | */ |
| 4392 | last = found_key.offset; |
| 4393 | last_for_get_extent = last + 1; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4394 | } |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4395 | btrfs_release_path(path); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4396 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4397 | /* |
| 4398 | * we might have some extents allocated but more delalloc past those |
| 4399 | * extents. so, we trust isize unless the start of the last extent is |
| 4400 | * beyond isize |
| 4401 | */ |
| 4402 | if (last < isize) { |
| 4403 | last = (u64)-1; |
| 4404 | last_for_get_extent = isize; |
| 4405 | } |
| 4406 | |
Liu Bo | a52f4cd | 2013-05-01 16:23:41 +0000 | [diff] [blame] | 4407 | 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] | 4408 | &cached_state); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4409 | |
Josef Bacik | 4d479cf | 2011-11-17 11:34:31 -0500 | [diff] [blame] | 4410 | em = get_extent_skip_holes(inode, start, last_for_get_extent, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4411 | get_extent); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4412 | if (!em) |
| 4413 | goto out; |
| 4414 | if (IS_ERR(em)) { |
| 4415 | ret = PTR_ERR(em); |
| 4416 | goto out; |
| 4417 | } |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4418 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4419 | while (!end) { |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4420 | u64 offset_in_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4421 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4422 | /* break if the extent we found is outside the range */ |
| 4423 | if (em->start >= max || extent_map_end(em) < off) |
| 4424 | break; |
| 4425 | |
| 4426 | /* |
| 4427 | * get_extent may return an extent that starts before our |
| 4428 | * requested range. We have to make sure the ranges |
| 4429 | * we return to fiemap always move forward and don't |
| 4430 | * overlap, so adjust the offsets here |
| 4431 | */ |
| 4432 | em_start = max(em->start, off); |
| 4433 | |
| 4434 | /* |
| 4435 | * record the offset from the start of the extent |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4436 | * for adjusting the disk offset below. Only do this if the |
| 4437 | * extent isn't compressed since our in ram offset may be past |
| 4438 | * what we have actually allocated on disk. |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4439 | */ |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4440 | if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 4441 | offset_in_extent = em_start - em->start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4442 | em_end = extent_map_end(em); |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4443 | em_len = em_end - em_start; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4444 | disko = 0; |
| 4445 | flags = 0; |
| 4446 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4447 | /* |
| 4448 | * bump off for our next call to get_extent |
| 4449 | */ |
| 4450 | off = extent_map_end(em); |
| 4451 | if (off >= max) |
| 4452 | end = 1; |
| 4453 | |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4454 | if (em->block_start == EXTENT_MAP_LAST_BYTE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4455 | end = 1; |
| 4456 | flags |= FIEMAP_EXTENT_LAST; |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4457 | } else if (em->block_start == EXTENT_MAP_INLINE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4458 | flags |= (FIEMAP_EXTENT_DATA_INLINE | |
| 4459 | FIEMAP_EXTENT_NOT_ALIGNED); |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4460 | } else if (em->block_start == EXTENT_MAP_DELALLOC) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4461 | flags |= (FIEMAP_EXTENT_DELALLOC | |
| 4462 | FIEMAP_EXTENT_UNKNOWN); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4463 | } else if (fieinfo->fi_extents_max) { |
| 4464 | u64 bytenr = em->block_start - |
| 4465 | (em->start - em->orig_start); |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4466 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4467 | disko = em->block_start + offset_in_extent; |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4468 | |
| 4469 | /* |
| 4470 | * As btrfs supports shared space, this information |
| 4471 | * can be exported to userspace tools via |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4472 | * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0 |
| 4473 | * then we're just getting a count and we can skip the |
| 4474 | * lookup stuff. |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4475 | */ |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4476 | ret = btrfs_check_shared(NULL, root->fs_info, |
| 4477 | root->objectid, |
| 4478 | btrfs_ino(inode), bytenr); |
| 4479 | if (ret < 0) |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4480 | goto out_free; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4481 | if (ret) |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4482 | flags |= FIEMAP_EXTENT_SHARED; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4483 | ret = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4484 | } |
| 4485 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 4486 | flags |= FIEMAP_EXTENT_ENCODED; |
| 4487 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4488 | free_extent_map(em); |
| 4489 | em = NULL; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4490 | if ((em_start >= last) || em_len == (u64)-1 || |
| 4491 | (last == (u64)-1 && isize <= em_end)) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4492 | flags |= FIEMAP_EXTENT_LAST; |
| 4493 | end = 1; |
| 4494 | } |
| 4495 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4496 | /* now scan forward to see if this is really the last extent. */ |
| 4497 | em = get_extent_skip_holes(inode, off, last_for_get_extent, |
| 4498 | get_extent); |
| 4499 | if (IS_ERR(em)) { |
| 4500 | ret = PTR_ERR(em); |
| 4501 | goto out; |
| 4502 | } |
| 4503 | if (!em) { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4504 | flags |= FIEMAP_EXTENT_LAST; |
| 4505 | end = 1; |
| 4506 | } |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4507 | ret = fiemap_fill_next_extent(fieinfo, em_start, disko, |
| 4508 | em_len, flags); |
| 4509 | if (ret) |
| 4510 | goto out_free; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4511 | } |
| 4512 | out_free: |
| 4513 | free_extent_map(em); |
| 4514 | out: |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4515 | btrfs_free_path(path); |
Liu Bo | a52f4cd | 2013-05-01 16:23:41 +0000 | [diff] [blame] | 4516 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4517 | &cached_state, GFP_NOFS); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4518 | return ret; |
| 4519 | } |
| 4520 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4521 | static void __free_extent_buffer(struct extent_buffer *eb) |
| 4522 | { |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 4523 | btrfs_leak_debug_del(&eb->leak_list); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4524 | kmem_cache_free(extent_buffer_cache, eb); |
| 4525 | } |
| 4526 | |
Josef Bacik | a26e8c9 | 2014-03-28 17:07:27 -0400 | [diff] [blame] | 4527 | int extent_buffer_under_io(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4528 | { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4529 | return (atomic_read(&eb->io_pages) || |
| 4530 | test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) || |
| 4531 | test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4532 | } |
| 4533 | |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4534 | /* |
| 4535 | * Helper for releasing extent buffer page. |
| 4536 | */ |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4537 | static void btrfs_release_extent_buffer_page(struct extent_buffer *eb) |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4538 | { |
| 4539 | unsigned long index; |
| 4540 | struct page *page; |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4541 | int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags); |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4542 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4543 | BUG_ON(extent_buffer_under_io(eb)); |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4544 | |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4545 | index = num_extent_pages(eb->start, eb->len); |
| 4546 | if (index == 0) |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4547 | return; |
| 4548 | |
| 4549 | do { |
| 4550 | index--; |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 4551 | page = eb->pages[index]; |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4552 | if (page && mapped) { |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4553 | spin_lock(&page->mapping->private_lock); |
| 4554 | /* |
| 4555 | * We do this since we'll remove the pages after we've |
| 4556 | * removed the eb from the radix tree, so we could race |
| 4557 | * and have this page now attached to the new eb. So |
| 4558 | * only clear page_private if it's still connected to |
| 4559 | * this eb. |
| 4560 | */ |
| 4561 | if (PagePrivate(page) && |
| 4562 | page->private == (unsigned long)eb) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4563 | BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4564 | BUG_ON(PageDirty(page)); |
| 4565 | BUG_ON(PageWriteback(page)); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4566 | /* |
| 4567 | * We need to make sure we haven't be attached |
| 4568 | * to a new eb. |
| 4569 | */ |
| 4570 | ClearPagePrivate(page); |
| 4571 | set_page_private(page, 0); |
| 4572 | /* One for the page private */ |
| 4573 | page_cache_release(page); |
| 4574 | } |
| 4575 | spin_unlock(&page->mapping->private_lock); |
| 4576 | |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4577 | } |
| 4578 | if (page) { |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4579 | /* One for when we alloced the page */ |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4580 | page_cache_release(page); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4581 | } |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4582 | } while (index != 0); |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4583 | } |
| 4584 | |
| 4585 | /* |
| 4586 | * Helper for releasing the extent buffer. |
| 4587 | */ |
| 4588 | static inline void btrfs_release_extent_buffer(struct extent_buffer *eb) |
| 4589 | { |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4590 | btrfs_release_extent_buffer_page(eb); |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4591 | __free_extent_buffer(eb); |
| 4592 | } |
| 4593 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4594 | static struct extent_buffer * |
| 4595 | __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, |
| 4596 | unsigned long len, gfp_t mask) |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4597 | { |
| 4598 | struct extent_buffer *eb = NULL; |
| 4599 | |
| 4600 | eb = kmem_cache_zalloc(extent_buffer_cache, mask); |
| 4601 | if (eb == NULL) |
| 4602 | return NULL; |
| 4603 | eb->start = start; |
| 4604 | eb->len = len; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4605 | eb->fs_info = fs_info; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4606 | eb->bflags = 0; |
| 4607 | rwlock_init(&eb->lock); |
| 4608 | atomic_set(&eb->write_locks, 0); |
| 4609 | atomic_set(&eb->read_locks, 0); |
| 4610 | atomic_set(&eb->blocking_readers, 0); |
| 4611 | atomic_set(&eb->blocking_writers, 0); |
| 4612 | atomic_set(&eb->spinning_readers, 0); |
| 4613 | atomic_set(&eb->spinning_writers, 0); |
| 4614 | eb->lock_nested = 0; |
| 4615 | init_waitqueue_head(&eb->write_lock_wq); |
| 4616 | init_waitqueue_head(&eb->read_lock_wq); |
| 4617 | |
| 4618 | btrfs_leak_debug_add(&eb->leak_list, &buffers); |
| 4619 | |
| 4620 | spin_lock_init(&eb->refs_lock); |
| 4621 | atomic_set(&eb->refs, 1); |
| 4622 | atomic_set(&eb->io_pages, 0); |
| 4623 | |
| 4624 | /* |
| 4625 | * Sanity checks, currently the maximum is 64k covered by 16x 4k pages |
| 4626 | */ |
| 4627 | BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE |
| 4628 | > MAX_INLINE_EXTENT_BUFFER_SIZE); |
| 4629 | BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE); |
| 4630 | |
| 4631 | return eb; |
| 4632 | } |
| 4633 | |
| 4634 | struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src) |
| 4635 | { |
| 4636 | unsigned long i; |
| 4637 | struct page *p; |
| 4638 | struct extent_buffer *new; |
| 4639 | unsigned long num_pages = num_extent_pages(src->start, src->len); |
| 4640 | |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 4641 | new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_NOFS); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4642 | if (new == NULL) |
| 4643 | return NULL; |
| 4644 | |
| 4645 | for (i = 0; i < num_pages; i++) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 4646 | p = alloc_page(GFP_NOFS); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4647 | if (!p) { |
| 4648 | btrfs_release_extent_buffer(new); |
| 4649 | return NULL; |
| 4650 | } |
| 4651 | attach_extent_buffer_page(new, p); |
| 4652 | WARN_ON(PageDirty(p)); |
| 4653 | SetPageUptodate(p); |
| 4654 | new->pages[i] = p; |
| 4655 | } |
| 4656 | |
| 4657 | copy_extent_buffer(new, src, 0, 0, src->len); |
| 4658 | set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags); |
| 4659 | set_bit(EXTENT_BUFFER_DUMMY, &new->bflags); |
| 4660 | |
| 4661 | return new; |
| 4662 | } |
| 4663 | |
| 4664 | struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len) |
| 4665 | { |
| 4666 | struct extent_buffer *eb; |
| 4667 | unsigned long num_pages = num_extent_pages(0, len); |
| 4668 | unsigned long i; |
| 4669 | |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 4670 | eb = __alloc_extent_buffer(NULL, start, len, GFP_NOFS); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4671 | if (!eb) |
| 4672 | return NULL; |
| 4673 | |
| 4674 | for (i = 0; i < num_pages; i++) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 4675 | eb->pages[i] = alloc_page(GFP_NOFS); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4676 | if (!eb->pages[i]) |
| 4677 | goto err; |
| 4678 | } |
| 4679 | set_extent_buffer_uptodate(eb); |
| 4680 | btrfs_set_header_nritems(eb, 0); |
| 4681 | set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags); |
| 4682 | |
| 4683 | return eb; |
| 4684 | err: |
| 4685 | for (; i > 0; i--) |
| 4686 | __free_page(eb->pages[i - 1]); |
| 4687 | __free_extent_buffer(eb); |
| 4688 | return NULL; |
| 4689 | } |
| 4690 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4691 | static void check_buffer_tree_ref(struct extent_buffer *eb) |
| 4692 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4693 | int refs; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4694 | /* the ref bit is tricky. We have to make sure it is set |
| 4695 | * if we have the buffer dirty. Otherwise the |
| 4696 | * code to free a buffer can end up dropping a dirty |
| 4697 | * page |
| 4698 | * |
| 4699 | * Once the ref bit is set, it won't go away while the |
| 4700 | * buffer is dirty or in writeback, and it also won't |
| 4701 | * go away while we have the reference count on the |
| 4702 | * eb bumped. |
| 4703 | * |
| 4704 | * We can't just set the ref bit without bumping the |
| 4705 | * ref on the eb because free_extent_buffer might |
| 4706 | * see the ref bit and try to clear it. If this happens |
| 4707 | * free_extent_buffer might end up dropping our original |
| 4708 | * ref by mistake and freeing the page before we are able |
| 4709 | * to add one more ref. |
| 4710 | * |
| 4711 | * So bump the ref count first, then set the bit. If someone |
| 4712 | * beat us to it, drop the ref we added. |
| 4713 | */ |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4714 | refs = atomic_read(&eb->refs); |
| 4715 | if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 4716 | return; |
| 4717 | |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4718 | spin_lock(&eb->refs_lock); |
| 4719 | if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4720 | atomic_inc(&eb->refs); |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4721 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4722 | } |
| 4723 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4724 | static void mark_extent_buffer_accessed(struct extent_buffer *eb, |
| 4725 | struct page *accessed) |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4726 | { |
| 4727 | unsigned long num_pages, i; |
| 4728 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4729 | check_buffer_tree_ref(eb); |
| 4730 | |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4731 | num_pages = num_extent_pages(eb->start, eb->len); |
| 4732 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 4733 | struct page *p = eb->pages[i]; |
| 4734 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4735 | if (p != accessed) |
| 4736 | mark_page_accessed(p); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4737 | } |
| 4738 | } |
| 4739 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4740 | struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info, |
| 4741 | u64 start) |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4742 | { |
| 4743 | struct extent_buffer *eb; |
| 4744 | |
| 4745 | rcu_read_lock(); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4746 | eb = radix_tree_lookup(&fs_info->buffer_radix, |
| 4747 | start >> PAGE_CACHE_SHIFT); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4748 | if (eb && atomic_inc_not_zero(&eb->refs)) { |
| 4749 | rcu_read_unlock(); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4750 | mark_extent_buffer_accessed(eb, NULL); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4751 | return eb; |
| 4752 | } |
| 4753 | rcu_read_unlock(); |
| 4754 | |
| 4755 | return NULL; |
| 4756 | } |
| 4757 | |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4758 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 4759 | struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info, |
| 4760 | u64 start, unsigned long len) |
| 4761 | { |
| 4762 | struct extent_buffer *eb, *exists = NULL; |
| 4763 | int ret; |
| 4764 | |
| 4765 | eb = find_extent_buffer(fs_info, start); |
| 4766 | if (eb) |
| 4767 | return eb; |
| 4768 | eb = alloc_dummy_extent_buffer(start, len); |
| 4769 | if (!eb) |
| 4770 | return NULL; |
| 4771 | eb->fs_info = fs_info; |
| 4772 | again: |
| 4773 | ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); |
| 4774 | if (ret) |
| 4775 | goto free_eb; |
| 4776 | spin_lock(&fs_info->buffer_lock); |
| 4777 | ret = radix_tree_insert(&fs_info->buffer_radix, |
| 4778 | start >> PAGE_CACHE_SHIFT, eb); |
| 4779 | spin_unlock(&fs_info->buffer_lock); |
| 4780 | radix_tree_preload_end(); |
| 4781 | if (ret == -EEXIST) { |
| 4782 | exists = find_extent_buffer(fs_info, start); |
| 4783 | if (exists) |
| 4784 | goto free_eb; |
| 4785 | else |
| 4786 | goto again; |
| 4787 | } |
| 4788 | check_buffer_tree_ref(eb); |
| 4789 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
| 4790 | |
| 4791 | /* |
| 4792 | * We will free dummy extent buffer's if they come into |
| 4793 | * free_extent_buffer with a ref count of 2, but if we are using this we |
| 4794 | * want the buffers to stay in memory until we're done with them, so |
| 4795 | * bump the ref count again. |
| 4796 | */ |
| 4797 | atomic_inc(&eb->refs); |
| 4798 | return eb; |
| 4799 | free_eb: |
| 4800 | btrfs_release_extent_buffer(eb); |
| 4801 | return exists; |
| 4802 | } |
| 4803 | #endif |
| 4804 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4805 | struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4806 | u64 start, unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4807 | { |
| 4808 | unsigned long num_pages = num_extent_pages(start, len); |
| 4809 | unsigned long i; |
| 4810 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 4811 | struct extent_buffer *eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4812 | struct extent_buffer *exists = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4813 | struct page *p; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4814 | struct address_space *mapping = fs_info->btree_inode->i_mapping; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4815 | int uptodate = 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4816 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4817 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4818 | eb = find_extent_buffer(fs_info, start); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4819 | if (eb) |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4820 | return eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4821 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4822 | eb = __alloc_extent_buffer(fs_info, start, len, GFP_NOFS); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 4823 | if (!eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4824 | return NULL; |
| 4825 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4826 | for (i = 0; i < num_pages; i++, index++) { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 4827 | p = find_or_create_page(mapping, index, GFP_NOFS); |
Josef Bacik | 4804b38 | 2012-10-05 16:43:45 -0400 | [diff] [blame] | 4828 | if (!p) |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4829 | goto free_eb; |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4830 | |
| 4831 | spin_lock(&mapping->private_lock); |
| 4832 | if (PagePrivate(p)) { |
| 4833 | /* |
| 4834 | * We could have already allocated an eb for this page |
| 4835 | * and attached one so lets see if we can get a ref on |
| 4836 | * the existing eb, and if we can we know it's good and |
| 4837 | * we can just return that one, else we know we can just |
| 4838 | * overwrite page->private. |
| 4839 | */ |
| 4840 | exists = (struct extent_buffer *)p->private; |
| 4841 | if (atomic_inc_not_zero(&exists->refs)) { |
| 4842 | spin_unlock(&mapping->private_lock); |
| 4843 | unlock_page(p); |
Josef Bacik | 17de39a | 2012-05-04 15:16:06 -0400 | [diff] [blame] | 4844 | page_cache_release(p); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4845 | mark_extent_buffer_accessed(exists, p); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4846 | goto free_eb; |
| 4847 | } |
| 4848 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4849 | /* |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4850 | * Do this so attach doesn't complain and we need to |
| 4851 | * drop the ref the old guy had. |
| 4852 | */ |
| 4853 | ClearPagePrivate(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4854 | WARN_ON(PageDirty(p)); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4855 | page_cache_release(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4856 | } |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4857 | attach_extent_buffer_page(eb, p); |
| 4858 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4859 | WARN_ON(PageDirty(p)); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4860 | eb->pages[i] = p; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4861 | if (!PageUptodate(p)) |
| 4862 | uptodate = 0; |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 4863 | |
| 4864 | /* |
| 4865 | * see below about how we avoid a nasty race with release page |
| 4866 | * and why we unlock later |
| 4867 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4868 | } |
| 4869 | if (uptodate) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4870 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 4871 | again: |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4872 | ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); |
| 4873 | if (ret) |
| 4874 | goto free_eb; |
| 4875 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4876 | spin_lock(&fs_info->buffer_lock); |
| 4877 | ret = radix_tree_insert(&fs_info->buffer_radix, |
| 4878 | start >> PAGE_CACHE_SHIFT, eb); |
| 4879 | spin_unlock(&fs_info->buffer_lock); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4880 | radix_tree_preload_end(); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4881 | if (ret == -EEXIST) { |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4882 | exists = find_extent_buffer(fs_info, start); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4883 | if (exists) |
| 4884 | goto free_eb; |
| 4885 | else |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 4886 | goto again; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4887 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4888 | /* add one reference for the tree */ |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4889 | check_buffer_tree_ref(eb); |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 4890 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 4891 | |
| 4892 | /* |
| 4893 | * there is a race where release page may have |
| 4894 | * tried to find this extent buffer in the radix |
| 4895 | * but failed. It will tell the VM it is safe to |
| 4896 | * reclaim the, and it will clear the page private bit. |
| 4897 | * We must make sure to set the page private bit properly |
| 4898 | * after the extent buffer is in the radix tree so |
| 4899 | * it doesn't get lost |
| 4900 | */ |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4901 | SetPageChecked(eb->pages[0]); |
| 4902 | for (i = 1; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 4903 | p = eb->pages[i]; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4904 | ClearPageChecked(p); |
| 4905 | unlock_page(p); |
| 4906 | } |
| 4907 | unlock_page(eb->pages[0]); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4908 | return eb; |
| 4909 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4910 | free_eb: |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4911 | for (i = 0; i < num_pages; i++) { |
| 4912 | if (eb->pages[i]) |
| 4913 | unlock_page(eb->pages[i]); |
| 4914 | } |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 4915 | |
Josef Bacik | 17de39a | 2012-05-04 15:16:06 -0400 | [diff] [blame] | 4916 | WARN_ON(!atomic_dec_and_test(&eb->refs)); |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4917 | btrfs_release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4918 | return exists; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4919 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4920 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4921 | static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head) |
| 4922 | { |
| 4923 | struct extent_buffer *eb = |
| 4924 | container_of(head, struct extent_buffer, rcu_head); |
| 4925 | |
| 4926 | __free_extent_buffer(eb); |
| 4927 | } |
| 4928 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4929 | /* Expects to have eb->eb_lock already held */ |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 4930 | static int release_extent_buffer(struct extent_buffer *eb) |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4931 | { |
| 4932 | WARN_ON(atomic_read(&eb->refs) == 0); |
| 4933 | if (atomic_dec_and_test(&eb->refs)) { |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 4934 | if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) { |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4935 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4936 | |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4937 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4938 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4939 | spin_lock(&fs_info->buffer_lock); |
| 4940 | radix_tree_delete(&fs_info->buffer_radix, |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4941 | eb->start >> PAGE_CACHE_SHIFT); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4942 | spin_unlock(&fs_info->buffer_lock); |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 4943 | } else { |
| 4944 | spin_unlock(&eb->refs_lock); |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4945 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4946 | |
| 4947 | /* Should be safe to release our pages at this point */ |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4948 | btrfs_release_extent_buffer_page(eb); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4949 | call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 4950 | return 1; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4951 | } |
| 4952 | spin_unlock(&eb->refs_lock); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 4953 | |
| 4954 | return 0; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4955 | } |
| 4956 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4957 | void free_extent_buffer(struct extent_buffer *eb) |
| 4958 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4959 | int refs; |
| 4960 | int old; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4961 | if (!eb) |
| 4962 | return; |
| 4963 | |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4964 | while (1) { |
| 4965 | refs = atomic_read(&eb->refs); |
| 4966 | if (refs <= 3) |
| 4967 | break; |
| 4968 | old = atomic_cmpxchg(&eb->refs, refs, refs - 1); |
| 4969 | if (old == refs) |
| 4970 | return; |
| 4971 | } |
| 4972 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4973 | spin_lock(&eb->refs_lock); |
| 4974 | if (atomic_read(&eb->refs) == 2 && |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4975 | test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) |
| 4976 | atomic_dec(&eb->refs); |
| 4977 | |
| 4978 | if (atomic_read(&eb->refs) == 2 && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4979 | test_bit(EXTENT_BUFFER_STALE, &eb->bflags) && |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4980 | !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4981 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 4982 | atomic_dec(&eb->refs); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4983 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4984 | /* |
| 4985 | * I know this is terrible, but it's temporary until we stop tracking |
| 4986 | * the uptodate bits and such for the extent buffers. |
| 4987 | */ |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 4988 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4989 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4990 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4991 | void free_extent_buffer_stale(struct extent_buffer *eb) |
| 4992 | { |
| 4993 | if (!eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4994 | return; |
| 4995 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4996 | spin_lock(&eb->refs_lock); |
| 4997 | set_bit(EXTENT_BUFFER_STALE, &eb->bflags); |
| 4998 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4999 | if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5000 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 5001 | atomic_dec(&eb->refs); |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5002 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5003 | } |
| 5004 | |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 5005 | void clear_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5006 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5007 | unsigned long i; |
| 5008 | unsigned long num_pages; |
| 5009 | struct page *page; |
| 5010 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5011 | num_pages = num_extent_pages(eb->start, eb->len); |
| 5012 | |
| 5013 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5014 | page = eb->pages[i]; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5015 | if (!PageDirty(page)) |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 5016 | continue; |
| 5017 | |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 5018 | lock_page(page); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5019 | WARN_ON(!PagePrivate(page)); |
| 5020 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5021 | clear_page_dirty_for_io(page); |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 5022 | spin_lock_irq(&page->mapping->tree_lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5023 | if (!PageDirty(page)) { |
| 5024 | radix_tree_tag_clear(&page->mapping->page_tree, |
| 5025 | page_index(page), |
| 5026 | PAGECACHE_TAG_DIRTY); |
| 5027 | } |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 5028 | spin_unlock_irq(&page->mapping->tree_lock); |
Chris Mason | bf0da8c | 2011-11-04 12:29:37 -0400 | [diff] [blame] | 5029 | ClearPageError(page); |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 5030 | unlock_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5031 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5032 | WARN_ON(atomic_read(&eb->refs) == 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5033 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5034 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5035 | int set_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5036 | { |
| 5037 | unsigned long i; |
| 5038 | unsigned long num_pages; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5039 | int was_dirty = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5040 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5041 | check_buffer_tree_ref(eb); |
| 5042 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5043 | was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5044 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5045 | num_pages = num_extent_pages(eb->start, eb->len); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5046 | WARN_ON(atomic_read(&eb->refs) == 0); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5047 | WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)); |
| 5048 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5049 | for (i = 0; i < num_pages; i++) |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5050 | set_page_dirty(eb->pages[i]); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5051 | return was_dirty; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5052 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5053 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5054 | int clear_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5055 | { |
| 5056 | unsigned long i; |
| 5057 | struct page *page; |
| 5058 | unsigned long num_pages; |
| 5059 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5060 | clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5061 | num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5062 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5063 | page = eb->pages[i]; |
Chris Mason | 33958dc | 2008-07-30 10:29:12 -0400 | [diff] [blame] | 5064 | if (page) |
| 5065 | ClearPageUptodate(page); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5066 | } |
| 5067 | return 0; |
| 5068 | } |
| 5069 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5070 | int set_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5071 | { |
| 5072 | unsigned long i; |
| 5073 | struct page *page; |
| 5074 | unsigned long num_pages; |
| 5075 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5076 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5077 | num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5078 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5079 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5080 | SetPageUptodate(page); |
| 5081 | } |
| 5082 | return 0; |
| 5083 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5084 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5085 | int extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5086 | { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5087 | return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5088 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5089 | |
| 5090 | int read_extent_buffer_pages(struct extent_io_tree *tree, |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 5091 | struct extent_buffer *eb, u64 start, int wait, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5092 | get_extent_t *get_extent, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5093 | { |
| 5094 | unsigned long i; |
| 5095 | unsigned long start_i; |
| 5096 | struct page *page; |
| 5097 | int err; |
| 5098 | int ret = 0; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5099 | int locked_pages = 0; |
| 5100 | int all_uptodate = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5101 | unsigned long num_pages; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5102 | unsigned long num_reads = 0; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5103 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5104 | unsigned long bio_flags = 0; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5105 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5106 | if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5107 | return 0; |
| 5108 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5109 | if (start) { |
| 5110 | WARN_ON(start < eb->start); |
| 5111 | start_i = (start >> PAGE_CACHE_SHIFT) - |
| 5112 | (eb->start >> PAGE_CACHE_SHIFT); |
| 5113 | } else { |
| 5114 | start_i = 0; |
| 5115 | } |
| 5116 | |
| 5117 | num_pages = num_extent_pages(eb->start, eb->len); |
| 5118 | for (i = start_i; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5119 | page = eb->pages[i]; |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 5120 | if (wait == WAIT_NONE) { |
David Woodhouse | 2db0496 | 2008-08-07 11:19:43 -0400 | [diff] [blame] | 5121 | if (!trylock_page(page)) |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5122 | goto unlock_exit; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5123 | } else { |
| 5124 | lock_page(page); |
| 5125 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5126 | locked_pages++; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5127 | if (!PageUptodate(page)) { |
| 5128 | num_reads++; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5129 | all_uptodate = 0; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5130 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5131 | } |
| 5132 | if (all_uptodate) { |
| 5133 | if (start_i == 0) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5134 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5135 | goto unlock_exit; |
| 5136 | } |
| 5137 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 5138 | clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 5139 | eb->read_mirror = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5140 | atomic_set(&eb->io_pages, num_reads); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5141 | for (i = start_i; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5142 | page = eb->pages[i]; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5143 | if (!PageUptodate(page)) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5144 | ClearPageError(page); |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5145 | err = __extent_read_full_page(tree, page, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5146 | get_extent, &bio, |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 5147 | mirror_num, &bio_flags, |
| 5148 | READ | REQ_META); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5149 | if (err) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5150 | ret = err; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5151 | } else { |
| 5152 | unlock_page(page); |
| 5153 | } |
| 5154 | } |
| 5155 | |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 5156 | if (bio) { |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 5157 | err = submit_one_bio(READ | REQ_META, bio, mirror_num, |
| 5158 | bio_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5159 | if (err) |
| 5160 | return err; |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 5161 | } |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5162 | |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 5163 | if (ret || wait != WAIT_COMPLETE) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5164 | return ret; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5165 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5166 | for (i = start_i; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5167 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5168 | wait_on_page_locked(page); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5169 | if (!PageUptodate(page)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5170 | ret = -EIO; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5171 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5172 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5173 | return ret; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5174 | |
| 5175 | unlock_exit: |
| 5176 | i = start_i; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5177 | while (locked_pages > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5178 | page = eb->pages[i]; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5179 | i++; |
| 5180 | unlock_page(page); |
| 5181 | locked_pages--; |
| 5182 | } |
| 5183 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5184 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5185 | |
| 5186 | void read_extent_buffer(struct extent_buffer *eb, void *dstv, |
| 5187 | unsigned long start, |
| 5188 | unsigned long len) |
| 5189 | { |
| 5190 | size_t cur; |
| 5191 | size_t offset; |
| 5192 | struct page *page; |
| 5193 | char *kaddr; |
| 5194 | char *dst = (char *)dstv; |
| 5195 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 5196 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5197 | |
| 5198 | WARN_ON(start > eb->len); |
| 5199 | WARN_ON(start + len > eb->start + eb->len); |
| 5200 | |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 5201 | offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5202 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5203 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5204 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5205 | |
| 5206 | cur = min(len, (PAGE_CACHE_SIZE - offset)); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5207 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5208 | memcpy(dst, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5209 | |
| 5210 | dst += cur; |
| 5211 | len -= cur; |
| 5212 | offset = 0; |
| 5213 | i++; |
| 5214 | } |
| 5215 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5216 | |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5217 | int read_extent_buffer_to_user(struct extent_buffer *eb, void __user *dstv, |
| 5218 | unsigned long start, |
| 5219 | unsigned long len) |
| 5220 | { |
| 5221 | size_t cur; |
| 5222 | size_t offset; |
| 5223 | struct page *page; |
| 5224 | char *kaddr; |
| 5225 | char __user *dst = (char __user *)dstv; |
| 5226 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 5227 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 5228 | int ret = 0; |
| 5229 | |
| 5230 | WARN_ON(start > eb->len); |
| 5231 | WARN_ON(start + len > eb->start + eb->len); |
| 5232 | |
| 5233 | offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1); |
| 5234 | |
| 5235 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5236 | page = eb->pages[i]; |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5237 | |
| 5238 | cur = min(len, (PAGE_CACHE_SIZE - offset)); |
| 5239 | kaddr = page_address(page); |
| 5240 | if (copy_to_user(dst, kaddr + offset, cur)) { |
| 5241 | ret = -EFAULT; |
| 5242 | break; |
| 5243 | } |
| 5244 | |
| 5245 | dst += cur; |
| 5246 | len -= cur; |
| 5247 | offset = 0; |
| 5248 | i++; |
| 5249 | } |
| 5250 | |
| 5251 | return ret; |
| 5252 | } |
| 5253 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5254 | int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start, |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5255 | unsigned long min_len, char **map, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5256 | unsigned long *map_start, |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5257 | unsigned long *map_len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5258 | { |
| 5259 | size_t offset = start & (PAGE_CACHE_SIZE - 1); |
| 5260 | char *kaddr; |
| 5261 | struct page *p; |
| 5262 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 5263 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 5264 | unsigned long end_i = (start_offset + start + min_len - 1) >> |
| 5265 | PAGE_CACHE_SHIFT; |
| 5266 | |
| 5267 | if (i != end_i) |
| 5268 | return -EINVAL; |
| 5269 | |
| 5270 | if (i == 0) { |
| 5271 | offset = start_offset; |
| 5272 | *map_start = 0; |
| 5273 | } else { |
| 5274 | offset = 0; |
| 5275 | *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset; |
| 5276 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5277 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5278 | if (start + min_len > eb->len) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 5279 | WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, " |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 5280 | "wanted %lu %lu\n", |
| 5281 | eb->start, eb->len, start, min_len); |
Josef Bacik | 85026533 | 2011-03-15 14:52:12 -0400 | [diff] [blame] | 5282 | return -EINVAL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5283 | } |
| 5284 | |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5285 | p = eb->pages[i]; |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5286 | kaddr = page_address(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5287 | *map = kaddr + offset; |
| 5288 | *map_len = PAGE_CACHE_SIZE - offset; |
| 5289 | return 0; |
| 5290 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5291 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5292 | int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv, |
| 5293 | unsigned long start, |
| 5294 | unsigned long len) |
| 5295 | { |
| 5296 | size_t cur; |
| 5297 | size_t offset; |
| 5298 | struct page *page; |
| 5299 | char *kaddr; |
| 5300 | char *ptr = (char *)ptrv; |
| 5301 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 5302 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 5303 | int ret = 0; |
| 5304 | |
| 5305 | WARN_ON(start > eb->len); |
| 5306 | WARN_ON(start + len > eb->start + eb->len); |
| 5307 | |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 5308 | offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5309 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5310 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5311 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5312 | |
| 5313 | cur = min(len, (PAGE_CACHE_SIZE - offset)); |
| 5314 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5315 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5316 | ret = memcmp(ptr, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5317 | if (ret) |
| 5318 | break; |
| 5319 | |
| 5320 | ptr += cur; |
| 5321 | len -= cur; |
| 5322 | offset = 0; |
| 5323 | i++; |
| 5324 | } |
| 5325 | return ret; |
| 5326 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5327 | |
| 5328 | void write_extent_buffer(struct extent_buffer *eb, const void *srcv, |
| 5329 | unsigned long start, unsigned long len) |
| 5330 | { |
| 5331 | size_t cur; |
| 5332 | size_t offset; |
| 5333 | struct page *page; |
| 5334 | char *kaddr; |
| 5335 | char *src = (char *)srcv; |
| 5336 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 5337 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 5338 | |
| 5339 | WARN_ON(start > eb->len); |
| 5340 | WARN_ON(start + len > eb->start + eb->len); |
| 5341 | |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 5342 | offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5343 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5344 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5345 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5346 | WARN_ON(!PageUptodate(page)); |
| 5347 | |
| 5348 | cur = min(len, PAGE_CACHE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5349 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5350 | memcpy(kaddr + offset, src, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5351 | |
| 5352 | src += cur; |
| 5353 | len -= cur; |
| 5354 | offset = 0; |
| 5355 | i++; |
| 5356 | } |
| 5357 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5358 | |
| 5359 | void memset_extent_buffer(struct extent_buffer *eb, char c, |
| 5360 | unsigned long start, unsigned long len) |
| 5361 | { |
| 5362 | size_t cur; |
| 5363 | size_t offset; |
| 5364 | struct page *page; |
| 5365 | char *kaddr; |
| 5366 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 5367 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 5368 | |
| 5369 | WARN_ON(start > eb->len); |
| 5370 | WARN_ON(start + len > eb->start + eb->len); |
| 5371 | |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 5372 | offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5373 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5374 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5375 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5376 | WARN_ON(!PageUptodate(page)); |
| 5377 | |
| 5378 | cur = min(len, PAGE_CACHE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5379 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5380 | memset(kaddr + offset, c, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5381 | |
| 5382 | len -= cur; |
| 5383 | offset = 0; |
| 5384 | i++; |
| 5385 | } |
| 5386 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5387 | |
| 5388 | void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src, |
| 5389 | unsigned long dst_offset, unsigned long src_offset, |
| 5390 | unsigned long len) |
| 5391 | { |
| 5392 | u64 dst_len = dst->len; |
| 5393 | size_t cur; |
| 5394 | size_t offset; |
| 5395 | struct page *page; |
| 5396 | char *kaddr; |
| 5397 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 5398 | unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT; |
| 5399 | |
| 5400 | WARN_ON(src->len != dst_len); |
| 5401 | |
| 5402 | offset = (start_offset + dst_offset) & |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 5403 | (PAGE_CACHE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5404 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5405 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5406 | page = dst->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5407 | WARN_ON(!PageUptodate(page)); |
| 5408 | |
| 5409 | cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset)); |
| 5410 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5411 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5412 | read_extent_buffer(src, kaddr + offset, src_offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5413 | |
| 5414 | src_offset += cur; |
| 5415 | len -= cur; |
| 5416 | offset = 0; |
| 5417 | i++; |
| 5418 | } |
| 5419 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5420 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5421 | static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len) |
| 5422 | { |
| 5423 | unsigned long distance = (src > dst) ? src - dst : dst - src; |
| 5424 | return distance < len; |
| 5425 | } |
| 5426 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5427 | static void copy_pages(struct page *dst_page, struct page *src_page, |
| 5428 | unsigned long dst_off, unsigned long src_off, |
| 5429 | unsigned long len) |
| 5430 | { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5431 | char *dst_kaddr = page_address(dst_page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5432 | char *src_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5433 | int must_memmove = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5434 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5435 | if (dst_page != src_page) { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5436 | src_kaddr = page_address(src_page); |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5437 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5438 | src_kaddr = dst_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5439 | if (areas_overlap(src_off, dst_off, len)) |
| 5440 | must_memmove = 1; |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5441 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5442 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5443 | if (must_memmove) |
| 5444 | memmove(dst_kaddr + dst_off, src_kaddr + src_off, len); |
| 5445 | else |
| 5446 | memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5447 | } |
| 5448 | |
| 5449 | void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 5450 | unsigned long src_offset, unsigned long len) |
| 5451 | { |
| 5452 | size_t cur; |
| 5453 | size_t dst_off_in_page; |
| 5454 | size_t src_off_in_page; |
| 5455 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 5456 | unsigned long dst_i; |
| 5457 | unsigned long src_i; |
| 5458 | |
| 5459 | if (src_offset + len > dst->len) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5460 | printk(KERN_ERR "BTRFS: memmove bogus src_offset %lu move " |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5461 | "len %lu dst len %lu\n", src_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5462 | BUG_ON(1); |
| 5463 | } |
| 5464 | if (dst_offset + len > dst->len) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5465 | printk(KERN_ERR "BTRFS: memmove bogus dst_offset %lu move " |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5466 | "len %lu dst len %lu\n", dst_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5467 | BUG_ON(1); |
| 5468 | } |
| 5469 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5470 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5471 | dst_off_in_page = (start_offset + dst_offset) & |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 5472 | (PAGE_CACHE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5473 | src_off_in_page = (start_offset + src_offset) & |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 5474 | (PAGE_CACHE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5475 | |
| 5476 | dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT; |
| 5477 | src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT; |
| 5478 | |
| 5479 | cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - |
| 5480 | src_off_in_page)); |
| 5481 | cur = min_t(unsigned long, cur, |
| 5482 | (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page)); |
| 5483 | |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5484 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5485 | dst_off_in_page, src_off_in_page, cur); |
| 5486 | |
| 5487 | src_offset += cur; |
| 5488 | dst_offset += cur; |
| 5489 | len -= cur; |
| 5490 | } |
| 5491 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5492 | |
| 5493 | void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 5494 | unsigned long src_offset, unsigned long len) |
| 5495 | { |
| 5496 | size_t cur; |
| 5497 | size_t dst_off_in_page; |
| 5498 | size_t src_off_in_page; |
| 5499 | unsigned long dst_end = dst_offset + len - 1; |
| 5500 | unsigned long src_end = src_offset + len - 1; |
| 5501 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 5502 | unsigned long dst_i; |
| 5503 | unsigned long src_i; |
| 5504 | |
| 5505 | if (src_offset + len > dst->len) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5506 | printk(KERN_ERR "BTRFS: memmove bogus src_offset %lu move " |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5507 | "len %lu len %lu\n", src_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5508 | BUG_ON(1); |
| 5509 | } |
| 5510 | if (dst_offset + len > dst->len) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5511 | printk(KERN_ERR "BTRFS: memmove bogus dst_offset %lu move " |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5512 | "len %lu len %lu\n", dst_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5513 | BUG_ON(1); |
| 5514 | } |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5515 | if (dst_offset < src_offset) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5516 | memcpy_extent_buffer(dst, dst_offset, src_offset, len); |
| 5517 | return; |
| 5518 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5519 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5520 | dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT; |
| 5521 | src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT; |
| 5522 | |
| 5523 | dst_off_in_page = (start_offset + dst_end) & |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 5524 | (PAGE_CACHE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5525 | src_off_in_page = (start_offset + src_end) & |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 5526 | (PAGE_CACHE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5527 | |
| 5528 | cur = min_t(unsigned long, len, src_off_in_page + 1); |
| 5529 | cur = min(cur, dst_off_in_page + 1); |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5530 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5531 | dst_off_in_page - cur + 1, |
| 5532 | src_off_in_page - cur + 1, cur); |
| 5533 | |
| 5534 | dst_end -= cur; |
| 5535 | src_end -= cur; |
| 5536 | len -= cur; |
| 5537 | } |
| 5538 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5539 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5540 | int try_release_extent_buffer(struct page *page) |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5541 | { |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5542 | struct extent_buffer *eb; |
Miao Xie | 897ca6e | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5543 | |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5544 | /* |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5545 | * We need to make sure noboody is attaching this page to an eb right |
| 5546 | * now. |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5547 | */ |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5548 | spin_lock(&page->mapping->private_lock); |
| 5549 | if (!PagePrivate(page)) { |
| 5550 | spin_unlock(&page->mapping->private_lock); |
| 5551 | return 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5552 | } |
| 5553 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5554 | eb = (struct extent_buffer *)page->private; |
| 5555 | BUG_ON(!eb); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5556 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5557 | /* |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5558 | * This is a little awful but should be ok, we need to make sure that |
| 5559 | * the eb doesn't disappear out from under us while we're looking at |
| 5560 | * this page. |
| 5561 | */ |
| 5562 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5563 | if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5564 | spin_unlock(&eb->refs_lock); |
| 5565 | spin_unlock(&page->mapping->private_lock); |
| 5566 | return 0; |
| 5567 | } |
| 5568 | spin_unlock(&page->mapping->private_lock); |
| 5569 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5570 | /* |
| 5571 | * If tree ref isn't set then we know the ref on this eb is a real ref, |
| 5572 | * so just return, this page will likely be freed soon anyway. |
| 5573 | */ |
| 5574 | if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { |
| 5575 | spin_unlock(&eb->refs_lock); |
| 5576 | return 0; |
| 5577 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5578 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5579 | return release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5580 | } |