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); |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 67 | pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n", |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 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) { |
David Sterba | 9464732 | 2015-10-08 11:01:36 +0200 | [diff] [blame] | 99 | btrfs_debug_rl(BTRFS_I(inode)->root->fs_info, |
| 100 | "%s: ino %llu isize %llu odd range [%llu,%llu]", |
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 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 134 | static void add_extent_changeset(struct extent_state *state, unsigned bits, |
| 135 | struct extent_changeset *changeset, |
| 136 | int set) |
| 137 | { |
| 138 | int ret; |
| 139 | |
| 140 | if (!changeset) |
| 141 | return; |
| 142 | if (set && (state->state & bits) == bits) |
| 143 | return; |
| 144 | changeset->bytes_changed += state->end - state->start + 1; |
| 145 | ret = ulist_add(changeset->range_changed, state->start, state->end, |
| 146 | GFP_ATOMIC); |
| 147 | /* ENOMEM */ |
| 148 | BUG_ON(ret < 0); |
| 149 | } |
| 150 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 151 | static noinline void flush_write_bio(void *data); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 152 | static inline struct btrfs_fs_info * |
| 153 | tree_fs_info(struct extent_io_tree *tree) |
| 154 | { |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 155 | if (!tree->mapping) |
| 156 | return NULL; |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 157 | return btrfs_sb(tree->mapping->host->i_sb); |
| 158 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 159 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 160 | int __init extent_io_init(void) |
| 161 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 162 | extent_state_cache = kmem_cache_create("btrfs_extent_state", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 163 | sizeof(struct extent_state), 0, |
| 164 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 165 | if (!extent_state_cache) |
| 166 | return -ENOMEM; |
| 167 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 168 | extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 169 | sizeof(struct extent_buffer), 0, |
| 170 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 171 | if (!extent_buffer_cache) |
| 172 | goto free_state_cache; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 173 | |
| 174 | btrfs_bioset = bioset_create(BIO_POOL_SIZE, |
| 175 | offsetof(struct btrfs_io_bio, bio)); |
| 176 | if (!btrfs_bioset) |
| 177 | goto free_buffer_cache; |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 178 | |
| 179 | if (bioset_integrity_create(btrfs_bioset, BIO_POOL_SIZE)) |
| 180 | goto free_bioset; |
| 181 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 182 | return 0; |
| 183 | |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 184 | free_bioset: |
| 185 | bioset_free(btrfs_bioset); |
| 186 | btrfs_bioset = NULL; |
| 187 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 188 | free_buffer_cache: |
| 189 | kmem_cache_destroy(extent_buffer_cache); |
| 190 | extent_buffer_cache = NULL; |
| 191 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 192 | free_state_cache: |
| 193 | kmem_cache_destroy(extent_state_cache); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 194 | extent_state_cache = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 195 | return -ENOMEM; |
| 196 | } |
| 197 | |
| 198 | void extent_io_exit(void) |
| 199 | { |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 200 | btrfs_leak_debug_check(); |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 201 | |
| 202 | /* |
| 203 | * Make sure all delayed rcu free are flushed before we |
| 204 | * destroy caches. |
| 205 | */ |
| 206 | rcu_barrier(); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 207 | if (extent_state_cache) |
| 208 | kmem_cache_destroy(extent_state_cache); |
| 209 | if (extent_buffer_cache) |
| 210 | kmem_cache_destroy(extent_buffer_cache); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 211 | if (btrfs_bioset) |
| 212 | bioset_free(btrfs_bioset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void extent_io_tree_init(struct extent_io_tree *tree, |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 216 | struct address_space *mapping) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 217 | { |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 218 | tree->state = RB_ROOT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 219 | tree->ops = NULL; |
| 220 | tree->dirty_bytes = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 221 | spin_lock_init(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 222 | tree->mapping = mapping; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 223 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 224 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 225 | static struct extent_state *alloc_extent_state(gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 226 | { |
| 227 | struct extent_state *state; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 228 | |
| 229 | state = kmem_cache_alloc(extent_state_cache, mask); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 230 | if (!state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 231 | return state; |
| 232 | state->state = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 233 | state->private = 0; |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 234 | RB_CLEAR_NODE(&state->rb_node); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 235 | btrfs_leak_debug_add(&state->leak_list, &states); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 236 | atomic_set(&state->refs, 1); |
| 237 | init_waitqueue_head(&state->wq); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 238 | trace_alloc_extent_state(state, mask, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 239 | return state; |
| 240 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 241 | |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 242 | void free_extent_state(struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 243 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 244 | if (!state) |
| 245 | return; |
| 246 | if (atomic_dec_and_test(&state->refs)) { |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 247 | WARN_ON(extent_state_in_tree(state)); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 248 | btrfs_leak_debug_del(&state->leak_list); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 249 | trace_free_extent_state(state, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 250 | kmem_cache_free(extent_state_cache, state); |
| 251 | } |
| 252 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 253 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 254 | static struct rb_node *tree_insert(struct rb_root *root, |
| 255 | struct rb_node *search_start, |
| 256 | u64 offset, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 257 | struct rb_node *node, |
| 258 | struct rb_node ***p_in, |
| 259 | struct rb_node **parent_in) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 260 | { |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 261 | struct rb_node **p; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 262 | struct rb_node *parent = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 263 | struct tree_entry *entry; |
| 264 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 265 | if (p_in && parent_in) { |
| 266 | p = *p_in; |
| 267 | parent = *parent_in; |
| 268 | goto do_insert; |
| 269 | } |
| 270 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 271 | p = search_start ? &search_start : &root->rb_node; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 272 | while (*p) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 273 | parent = *p; |
| 274 | entry = rb_entry(parent, struct tree_entry, rb_node); |
| 275 | |
| 276 | if (offset < entry->start) |
| 277 | p = &(*p)->rb_left; |
| 278 | else if (offset > entry->end) |
| 279 | p = &(*p)->rb_right; |
| 280 | else |
| 281 | return parent; |
| 282 | } |
| 283 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 284 | do_insert: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 285 | rb_link_node(node, parent, p); |
| 286 | rb_insert_color(node, root); |
| 287 | return NULL; |
| 288 | } |
| 289 | |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 290 | 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] | 291 | struct rb_node **prev_ret, |
| 292 | struct rb_node **next_ret, |
| 293 | struct rb_node ***p_ret, |
| 294 | struct rb_node **parent_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 295 | { |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 296 | struct rb_root *root = &tree->state; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 297 | struct rb_node **n = &root->rb_node; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 298 | struct rb_node *prev = NULL; |
| 299 | struct rb_node *orig_prev = NULL; |
| 300 | struct tree_entry *entry; |
| 301 | struct tree_entry *prev_entry = NULL; |
| 302 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 303 | while (*n) { |
| 304 | prev = *n; |
| 305 | entry = rb_entry(prev, struct tree_entry, rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 306 | prev_entry = entry; |
| 307 | |
| 308 | if (offset < entry->start) |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 309 | n = &(*n)->rb_left; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 310 | else if (offset > entry->end) |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 311 | n = &(*n)->rb_right; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 312 | else |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 313 | return *n; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 314 | } |
| 315 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 316 | if (p_ret) |
| 317 | *p_ret = n; |
| 318 | if (parent_ret) |
| 319 | *parent_ret = prev; |
| 320 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 321 | if (prev_ret) { |
| 322 | orig_prev = prev; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 323 | while (prev && offset > prev_entry->end) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 324 | prev = rb_next(prev); |
| 325 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| 326 | } |
| 327 | *prev_ret = prev; |
| 328 | prev = orig_prev; |
| 329 | } |
| 330 | |
| 331 | if (next_ret) { |
| 332 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 333 | while (prev && offset < prev_entry->start) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 334 | prev = rb_prev(prev); |
| 335 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| 336 | } |
| 337 | *next_ret = prev; |
| 338 | } |
| 339 | return NULL; |
| 340 | } |
| 341 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 342 | static inline struct rb_node * |
| 343 | tree_search_for_insert(struct extent_io_tree *tree, |
| 344 | u64 offset, |
| 345 | struct rb_node ***p_ret, |
| 346 | struct rb_node **parent_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 347 | { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 348 | struct rb_node *prev = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 349 | struct rb_node *ret; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 350 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 351 | ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 352 | if (!ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 353 | return prev; |
| 354 | return ret; |
| 355 | } |
| 356 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 357 | static inline struct rb_node *tree_search(struct extent_io_tree *tree, |
| 358 | u64 offset) |
| 359 | { |
| 360 | return tree_search_for_insert(tree, offset, NULL, NULL); |
| 361 | } |
| 362 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 363 | static void merge_cb(struct extent_io_tree *tree, struct extent_state *new, |
| 364 | struct extent_state *other) |
| 365 | { |
| 366 | if (tree->ops && tree->ops->merge_extent_hook) |
| 367 | tree->ops->merge_extent_hook(tree->mapping->host, new, |
| 368 | other); |
| 369 | } |
| 370 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 371 | /* |
| 372 | * utility function to look for merge candidates inside a given range. |
| 373 | * Any extents with matching state are merged together into a single |
| 374 | * extent in the tree. Extents with EXTENT_IO in their state field |
| 375 | * are not merged because the end_io handlers need to be able to do |
| 376 | * operations on them without sleeping (or doing allocations/splits). |
| 377 | * |
| 378 | * This should be called with the tree lock held. |
| 379 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 380 | static void merge_state(struct extent_io_tree *tree, |
| 381 | struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 382 | { |
| 383 | struct extent_state *other; |
| 384 | struct rb_node *other_node; |
| 385 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 386 | if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 387 | return; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 388 | |
| 389 | other_node = rb_prev(&state->rb_node); |
| 390 | if (other_node) { |
| 391 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 392 | if (other->end == state->start - 1 && |
| 393 | other->state == state->state) { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 394 | merge_cb(tree, state, other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 395 | state->start = other->start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 396 | rb_erase(&other->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 397 | RB_CLEAR_NODE(&other->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 398 | free_extent_state(other); |
| 399 | } |
| 400 | } |
| 401 | other_node = rb_next(&state->rb_node); |
| 402 | if (other_node) { |
| 403 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 404 | if (other->start == state->end + 1 && |
| 405 | other->state == state->state) { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 406 | merge_cb(tree, state, other); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 407 | state->end = other->end; |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 408 | rb_erase(&other->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 409 | RB_CLEAR_NODE(&other->rb_node); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 410 | free_extent_state(other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 411 | } |
| 412 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 413 | } |
| 414 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 415 | static void set_state_cb(struct extent_io_tree *tree, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 416 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 417 | { |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 418 | if (tree->ops && tree->ops->set_bit_hook) |
| 419 | tree->ops->set_bit_hook(tree->mapping->host, state, bits); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static void clear_state_cb(struct extent_io_tree *tree, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 423 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 424 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 425 | if (tree->ops && tree->ops->clear_bit_hook) |
| 426 | tree->ops->clear_bit_hook(tree->mapping->host, state, bits); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 427 | } |
| 428 | |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 429 | static void set_state_bits(struct extent_io_tree *tree, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 430 | struct extent_state *state, unsigned *bits, |
| 431 | struct extent_changeset *changeset); |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 432 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 433 | /* |
| 434 | * insert an extent_state struct into the tree. 'bits' are set on the |
| 435 | * struct before it is inserted. |
| 436 | * |
| 437 | * This may return -EEXIST if the extent is already there, in which case the |
| 438 | * state struct is freed. |
| 439 | * |
| 440 | * The tree lock is not taken internally. This is a utility function and |
| 441 | * probably isn't what you want to call (see set/clear_extent_bit). |
| 442 | */ |
| 443 | static int insert_state(struct extent_io_tree *tree, |
| 444 | struct extent_state *state, u64 start, u64 end, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 445 | struct rb_node ***p, |
| 446 | struct rb_node **parent, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 447 | unsigned *bits, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 448 | { |
| 449 | struct rb_node *node; |
| 450 | |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 451 | if (end < start) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 452 | WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 453 | end, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 454 | state->start = start; |
| 455 | state->end = end; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 456 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 457 | set_state_bits(tree, state, bits, changeset); |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 458 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 459 | node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 460 | if (node) { |
| 461 | struct extent_state *found; |
| 462 | found = rb_entry(node, struct extent_state, rb_node); |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 463 | printk(KERN_ERR "BTRFS: found node %llu %llu on insert of " |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 464 | "%llu %llu\n", |
| 465 | found->start, found->end, start, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 466 | return -EEXIST; |
| 467 | } |
| 468 | merge_state(tree, state); |
| 469 | return 0; |
| 470 | } |
| 471 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 472 | 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] | 473 | u64 split) |
| 474 | { |
| 475 | if (tree->ops && tree->ops->split_extent_hook) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 476 | tree->ops->split_extent_hook(tree->mapping->host, orig, split); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 477 | } |
| 478 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 479 | /* |
| 480 | * split a given extent state struct in two, inserting the preallocated |
| 481 | * struct 'prealloc' as the newly created second half. 'split' indicates an |
| 482 | * offset inside 'orig' where it should be split. |
| 483 | * |
| 484 | * Before calling, |
| 485 | * the tree has 'orig' at [orig->start, orig->end]. After calling, there |
| 486 | * are two extent state structs in the tree: |
| 487 | * prealloc: [orig->start, split - 1] |
| 488 | * orig: [ split, orig->end ] |
| 489 | * |
| 490 | * The tree locks are not taken by this function. They need to be held |
| 491 | * by the caller. |
| 492 | */ |
| 493 | static int split_state(struct extent_io_tree *tree, struct extent_state *orig, |
| 494 | struct extent_state *prealloc, u64 split) |
| 495 | { |
| 496 | struct rb_node *node; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 497 | |
| 498 | split_cb(tree, orig, split); |
| 499 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 500 | prealloc->start = orig->start; |
| 501 | prealloc->end = split - 1; |
| 502 | prealloc->state = orig->state; |
| 503 | orig->start = split; |
| 504 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 505 | node = tree_insert(&tree->state, &orig->rb_node, prealloc->end, |
| 506 | &prealloc->rb_node, NULL, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 507 | if (node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 508 | free_extent_state(prealloc); |
| 509 | return -EEXIST; |
| 510 | } |
| 511 | return 0; |
| 512 | } |
| 513 | |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 514 | static struct extent_state *next_state(struct extent_state *state) |
| 515 | { |
| 516 | struct rb_node *next = rb_next(&state->rb_node); |
| 517 | if (next) |
| 518 | return rb_entry(next, struct extent_state, rb_node); |
| 519 | else |
| 520 | return NULL; |
| 521 | } |
| 522 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 523 | /* |
| 524 | * utility function to clear some bits in an extent state struct. |
Wang Sheng-Hui | 1b303fc | 2012-04-06 14:35:18 +0800 | [diff] [blame] | 525 | * 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] | 526 | * |
| 527 | * If no bits are set on the state struct after clearing things, the |
| 528 | * struct is freed and removed from the tree |
| 529 | */ |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 530 | static struct extent_state *clear_state_bit(struct extent_io_tree *tree, |
| 531 | struct extent_state *state, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 532 | unsigned *bits, int wake) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 533 | { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 534 | struct extent_state *next; |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 535 | unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 536 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 537 | if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 538 | u64 range = state->end - state->start + 1; |
| 539 | WARN_ON(range > tree->dirty_bytes); |
| 540 | tree->dirty_bytes -= range; |
| 541 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 542 | clear_state_cb(tree, state, bits); |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 543 | state->state &= ~bits_to_clear; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 544 | if (wake) |
| 545 | wake_up(&state->wq); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 546 | if (state->state == 0) { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 547 | next = next_state(state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 548 | if (extent_state_in_tree(state)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 549 | rb_erase(&state->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 550 | RB_CLEAR_NODE(&state->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 551 | free_extent_state(state); |
| 552 | } else { |
| 553 | WARN_ON(1); |
| 554 | } |
| 555 | } else { |
| 556 | merge_state(tree, state); |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 557 | next = next_state(state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 558 | } |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 559 | return next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 560 | } |
| 561 | |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 562 | static struct extent_state * |
| 563 | alloc_extent_state_atomic(struct extent_state *prealloc) |
| 564 | { |
| 565 | if (!prealloc) |
| 566 | prealloc = alloc_extent_state(GFP_ATOMIC); |
| 567 | |
| 568 | return prealloc; |
| 569 | } |
| 570 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 571 | 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] | 572 | { |
| 573 | btrfs_panic(tree_fs_info(tree), err, "Locking error: " |
| 574 | "Extent tree was modified by another " |
| 575 | "thread while locked."); |
| 576 | } |
| 577 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 578 | /* |
| 579 | * clear some bits on a range in the tree. This may require splitting |
| 580 | * or inserting elements in the tree, so the gfp mask is used to |
| 581 | * indicate which allocations or sleeping are allowed. |
| 582 | * |
| 583 | * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove |
| 584 | * the given range from the tree regardless of state (ie for truncate). |
| 585 | * |
| 586 | * the range [start, end] is inclusive. |
| 587 | * |
Jeff Mahoney | 6763af8 | 2012-03-01 14:56:29 +0100 | [diff] [blame] | 588 | * 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] | 589 | */ |
| 590 | int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 591 | unsigned bits, int wake, int delete, |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 592 | struct extent_state **cached_state, |
| 593 | gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 594 | { |
| 595 | struct extent_state *state; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 596 | struct extent_state *cached; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 597 | struct extent_state *prealloc = NULL; |
| 598 | struct rb_node *node; |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 599 | u64 last_end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 600 | int err; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 601 | int clear = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 602 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 603 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 604 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 605 | if (bits & EXTENT_DELALLOC) |
| 606 | bits |= EXTENT_NORESERVE; |
| 607 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 608 | if (delete) |
| 609 | bits |= ~EXTENT_CTLBITS; |
| 610 | bits |= EXTENT_FIRST_DELALLOC; |
| 611 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 612 | if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY)) |
| 613 | clear = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 614 | again: |
| 615 | if (!prealloc && (mask & __GFP_WAIT)) { |
Filipe Manana | c7bc631 | 2014-11-03 14:12:57 +0000 | [diff] [blame] | 616 | /* |
| 617 | * Don't care for allocation failure here because we might end |
| 618 | * up not needing the pre-allocated extent state at all, which |
| 619 | * is the case if we only have in the tree extent states that |
| 620 | * cover our input range and don't cover too any other range. |
| 621 | * If we end up needing a new extent state we allocate it later. |
| 622 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 623 | prealloc = alloc_extent_state(mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 624 | } |
| 625 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 626 | spin_lock(&tree->lock); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 627 | if (cached_state) { |
| 628 | cached = *cached_state; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 629 | |
| 630 | if (clear) { |
| 631 | *cached_state = NULL; |
| 632 | cached_state = NULL; |
| 633 | } |
| 634 | |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 635 | if (cached && extent_state_in_tree(cached) && |
| 636 | cached->start <= start && cached->end > start) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 637 | if (clear) |
| 638 | atomic_dec(&cached->refs); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 639 | state = cached; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 640 | goto hit_next; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 641 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 642 | if (clear) |
| 643 | free_extent_state(cached); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 644 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 645 | /* |
| 646 | * this search will find the extents that end after |
| 647 | * our range starts |
| 648 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 649 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 650 | if (!node) |
| 651 | goto out; |
| 652 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 653 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 654 | if (state->start > end) |
| 655 | goto out; |
| 656 | WARN_ON(state->end < start); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 657 | last_end = state->end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 658 | |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 659 | /* the state doesn't have the wanted bits, go ahead */ |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 660 | if (!(state->state & bits)) { |
| 661 | state = next_state(state); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 662 | goto next; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 663 | } |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 664 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 665 | /* |
| 666 | * | ---- desired range ---- | |
| 667 | * | state | or |
| 668 | * | ------------- state -------------- | |
| 669 | * |
| 670 | * We need to split the extent we found, and may flip |
| 671 | * bits on second half. |
| 672 | * |
| 673 | * If the extent we found extends past our range, we |
| 674 | * just split and search again. It'll get split again |
| 675 | * the next time though. |
| 676 | * |
| 677 | * If the extent we found is inside our range, we clear |
| 678 | * the desired bit on it. |
| 679 | */ |
| 680 | |
| 681 | if (state->start < start) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 682 | prealloc = alloc_extent_state_atomic(prealloc); |
| 683 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 684 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 685 | if (err) |
| 686 | extent_io_tree_panic(tree, err); |
| 687 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 688 | prealloc = NULL; |
| 689 | if (err) |
| 690 | goto out; |
| 691 | if (state->end <= end) { |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 692 | state = clear_state_bit(tree, state, &bits, wake); |
| 693 | goto next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 694 | } |
| 695 | goto search_again; |
| 696 | } |
| 697 | /* |
| 698 | * | ---- desired range ---- | |
| 699 | * | state | |
| 700 | * We need to split the extent, and clear the bit |
| 701 | * on the first half |
| 702 | */ |
| 703 | if (state->start <= end && state->end > end) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 704 | prealloc = alloc_extent_state_atomic(prealloc); |
| 705 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 706 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 707 | if (err) |
| 708 | extent_io_tree_panic(tree, err); |
| 709 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 710 | if (wake) |
| 711 | wake_up(&state->wq); |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 712 | |
Jeff Mahoney | 6763af8 | 2012-03-01 14:56:29 +0100 | [diff] [blame] | 713 | clear_state_bit(tree, prealloc, &bits, wake); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 714 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 715 | prealloc = NULL; |
| 716 | goto out; |
| 717 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 718 | |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 719 | state = clear_state_bit(tree, state, &bits, wake); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 720 | next: |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 721 | if (last_end == (u64)-1) |
| 722 | goto out; |
| 723 | start = last_end + 1; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 724 | if (start <= end && state && !need_resched()) |
Liu Bo | 692e575 | 2012-02-16 18:34:36 +0800 | [diff] [blame] | 725 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 726 | goto search_again; |
| 727 | |
| 728 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 729 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 730 | if (prealloc) |
| 731 | free_extent_state(prealloc); |
| 732 | |
Jeff Mahoney | 6763af8 | 2012-03-01 14:56:29 +0100 | [diff] [blame] | 733 | return 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 734 | |
| 735 | search_again: |
| 736 | if (start > end) |
| 737 | goto out; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 738 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 739 | if (mask & __GFP_WAIT) |
| 740 | cond_resched(); |
| 741 | goto again; |
| 742 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 743 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 744 | static void wait_on_state(struct extent_io_tree *tree, |
| 745 | struct extent_state *state) |
Christoph Hellwig | 641f521 | 2008-12-02 06:36:10 -0500 | [diff] [blame] | 746 | __releases(tree->lock) |
| 747 | __acquires(tree->lock) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 748 | { |
| 749 | DEFINE_WAIT(wait); |
| 750 | prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 751 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 752 | schedule(); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 753 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 754 | finish_wait(&state->wq, &wait); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | /* |
| 758 | * waits for one or more bits to clear on a range in the state tree. |
| 759 | * The range [start, end] is inclusive. |
| 760 | * The tree lock is taken by this function |
| 761 | */ |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 762 | static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 763 | unsigned long bits) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 764 | { |
| 765 | struct extent_state *state; |
| 766 | struct rb_node *node; |
| 767 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 768 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 769 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 770 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 771 | again: |
| 772 | while (1) { |
| 773 | /* |
| 774 | * this search will find all the extents that end after |
| 775 | * our range starts |
| 776 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 777 | node = tree_search(tree, start); |
Filipe Manana | c50d3e7 | 2014-03-31 14:53:25 +0100 | [diff] [blame] | 778 | process_node: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 779 | if (!node) |
| 780 | break; |
| 781 | |
| 782 | state = rb_entry(node, struct extent_state, rb_node); |
| 783 | |
| 784 | if (state->start > end) |
| 785 | goto out; |
| 786 | |
| 787 | if (state->state & bits) { |
| 788 | start = state->start; |
| 789 | atomic_inc(&state->refs); |
| 790 | wait_on_state(tree, state); |
| 791 | free_extent_state(state); |
| 792 | goto again; |
| 793 | } |
| 794 | start = state->end + 1; |
| 795 | |
| 796 | if (start > end) |
| 797 | break; |
| 798 | |
Filipe Manana | c50d3e7 | 2014-03-31 14:53:25 +0100 | [diff] [blame] | 799 | if (!cond_resched_lock(&tree->lock)) { |
| 800 | node = rb_next(node); |
| 801 | goto process_node; |
| 802 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 803 | } |
| 804 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 805 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 806 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 807 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 808 | static void set_state_bits(struct extent_io_tree *tree, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 809 | struct extent_state *state, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 810 | unsigned *bits, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 811 | { |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 812 | unsigned bits_to_set = *bits & ~EXTENT_CTLBITS; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 813 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 814 | set_state_cb(tree, state, bits); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 815 | if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 816 | u64 range = state->end - state->start + 1; |
| 817 | tree->dirty_bytes += range; |
| 818 | } |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 819 | add_extent_changeset(state, bits_to_set, changeset, 1); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 820 | state->state |= bits_to_set; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 821 | } |
| 822 | |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 823 | static void cache_state_if_flags(struct extent_state *state, |
| 824 | struct extent_state **cached_ptr, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 825 | unsigned flags) |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 826 | { |
| 827 | if (cached_ptr && !(*cached_ptr)) { |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 828 | if (!flags || (state->state & flags)) { |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 829 | *cached_ptr = state; |
| 830 | atomic_inc(&state->refs); |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 835 | static void cache_state(struct extent_state *state, |
| 836 | struct extent_state **cached_ptr) |
| 837 | { |
| 838 | return cache_state_if_flags(state, cached_ptr, |
| 839 | EXTENT_IOBITS | EXTENT_BOUNDARY); |
| 840 | } |
| 841 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 842 | /* |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 843 | * set some bits on a range in the tree. This may require allocations or |
| 844 | * sleeping, so the gfp mask is used to indicate what is allowed. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 845 | * |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 846 | * If any of the exclusive bits are set, this will fail with -EEXIST if some |
| 847 | * part of the range already has the desired bits set. The start of the |
| 848 | * existing range is returned in failed_start in this case. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 849 | * |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 850 | * [start, end] is inclusive This takes the tree lock. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 851 | */ |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 852 | |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 853 | static int __must_check |
| 854 | __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 855 | unsigned bits, unsigned exclusive_bits, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 856 | u64 *failed_start, struct extent_state **cached_state, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 857 | gfp_t mask, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 858 | { |
| 859 | struct extent_state *state; |
| 860 | struct extent_state *prealloc = NULL; |
| 861 | struct rb_node *node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 862 | struct rb_node **p; |
| 863 | struct rb_node *parent; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 864 | int err = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 865 | u64 last_start; |
| 866 | u64 last_end; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 867 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 868 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 869 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 870 | bits |= EXTENT_FIRST_DELALLOC; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 871 | again: |
| 872 | if (!prealloc && (mask & __GFP_WAIT)) { |
| 873 | prealloc = alloc_extent_state(mask); |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 874 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 875 | } |
| 876 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 877 | spin_lock(&tree->lock); |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 878 | if (cached_state && *cached_state) { |
| 879 | state = *cached_state; |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 880 | if (state->start <= start && state->end > start && |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 881 | extent_state_in_tree(state)) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 882 | node = &state->rb_node; |
| 883 | goto hit_next; |
| 884 | } |
| 885 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 886 | /* |
| 887 | * this search will find all the extents that end after |
| 888 | * our range starts. |
| 889 | */ |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 890 | node = tree_search_for_insert(tree, start, &p, &parent); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 891 | if (!node) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 892 | prealloc = alloc_extent_state_atomic(prealloc); |
| 893 | BUG_ON(!prealloc); |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 894 | err = insert_state(tree, prealloc, start, end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 895 | &p, &parent, &bits, changeset); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 896 | if (err) |
| 897 | extent_io_tree_panic(tree, err); |
| 898 | |
Filipe David Borba Manana | c42ac0b | 2013-11-26 15:01:34 +0000 | [diff] [blame] | 899 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 900 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 901 | goto out; |
| 902 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 903 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 40431d6 | 2009-08-05 12:57:59 -0400 | [diff] [blame] | 904 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 905 | last_start = state->start; |
| 906 | last_end = state->end; |
| 907 | |
| 908 | /* |
| 909 | * | ---- desired range ---- | |
| 910 | * | state | |
| 911 | * |
| 912 | * Just lock what we found and keep going |
| 913 | */ |
| 914 | if (state->start == start && state->end <= end) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 915 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 916 | *failed_start = state->start; |
| 917 | err = -EEXIST; |
| 918 | goto out; |
| 919 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 920 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 921 | set_state_bits(tree, state, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 922 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 923 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 924 | if (last_end == (u64)-1) |
| 925 | goto out; |
| 926 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 927 | state = next_state(state); |
| 928 | if (start < end && state && state->start == start && |
| 929 | !need_resched()) |
| 930 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 931 | goto search_again; |
| 932 | } |
| 933 | |
| 934 | /* |
| 935 | * | ---- desired range ---- | |
| 936 | * | state | |
| 937 | * or |
| 938 | * | ------------- state -------------- | |
| 939 | * |
| 940 | * We need to split the extent we found, and may flip bits on |
| 941 | * second half. |
| 942 | * |
| 943 | * If the extent we found extends past our |
| 944 | * range, we just split and search again. It'll get split |
| 945 | * again the next time though. |
| 946 | * |
| 947 | * If the extent we found is inside our range, we set the |
| 948 | * desired bit on it. |
| 949 | */ |
| 950 | if (state->start < start) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 951 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 952 | *failed_start = start; |
| 953 | err = -EEXIST; |
| 954 | goto out; |
| 955 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 956 | |
| 957 | prealloc = alloc_extent_state_atomic(prealloc); |
| 958 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 959 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 960 | if (err) |
| 961 | extent_io_tree_panic(tree, err); |
| 962 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 963 | prealloc = NULL; |
| 964 | if (err) |
| 965 | goto out; |
| 966 | if (state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 967 | set_state_bits(tree, state, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 968 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 969 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 970 | if (last_end == (u64)-1) |
| 971 | goto out; |
| 972 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 973 | state = next_state(state); |
| 974 | if (start < end && state && state->start == start && |
| 975 | !need_resched()) |
| 976 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 977 | } |
| 978 | goto search_again; |
| 979 | } |
| 980 | /* |
| 981 | * | ---- desired range ---- | |
| 982 | * | state | or | state | |
| 983 | * |
| 984 | * There's a hole, we need to insert something in it and |
| 985 | * ignore the extent we found. |
| 986 | */ |
| 987 | if (state->start > start) { |
| 988 | u64 this_end; |
| 989 | if (end < last_start) |
| 990 | this_end = end; |
| 991 | else |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 992 | this_end = last_start - 1; |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 993 | |
| 994 | prealloc = alloc_extent_state_atomic(prealloc); |
| 995 | BUG_ON(!prealloc); |
Xiao Guangrong | c7f895a | 2011-04-20 06:45:49 +0000 | [diff] [blame] | 996 | |
| 997 | /* |
| 998 | * Avoid to free 'prealloc' if it can be merged with |
| 999 | * the later extent. |
| 1000 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1001 | err = insert_state(tree, prealloc, start, this_end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 1002 | NULL, NULL, &bits, changeset); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1003 | if (err) |
| 1004 | extent_io_tree_panic(tree, err); |
| 1005 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1006 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1007 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1008 | start = this_end + 1; |
| 1009 | goto search_again; |
| 1010 | } |
| 1011 | /* |
| 1012 | * | ---- desired range ---- | |
| 1013 | * | state | |
| 1014 | * We need to split the extent, and set the bit |
| 1015 | * on the first half |
| 1016 | */ |
| 1017 | if (state->start <= end && state->end > end) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1018 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1019 | *failed_start = start; |
| 1020 | err = -EEXIST; |
| 1021 | goto out; |
| 1022 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 1023 | |
| 1024 | prealloc = alloc_extent_state_atomic(prealloc); |
| 1025 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1026 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1027 | if (err) |
| 1028 | extent_io_tree_panic(tree, err); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1029 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 1030 | set_state_bits(tree, prealloc, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1031 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1032 | merge_state(tree, prealloc); |
| 1033 | prealloc = NULL; |
| 1034 | goto out; |
| 1035 | } |
| 1036 | |
| 1037 | goto search_again; |
| 1038 | |
| 1039 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1040 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1041 | if (prealloc) |
| 1042 | free_extent_state(prealloc); |
| 1043 | |
| 1044 | return err; |
| 1045 | |
| 1046 | search_again: |
| 1047 | if (start > end) |
| 1048 | goto out; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1049 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1050 | if (mask & __GFP_WAIT) |
| 1051 | cond_resched(); |
| 1052 | goto again; |
| 1053 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1054 | |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1055 | int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1056 | unsigned bits, u64 * failed_start, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1057 | struct extent_state **cached_state, gfp_t mask) |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1058 | { |
| 1059 | return __set_extent_bit(tree, start, end, bits, 0, failed_start, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 1060 | cached_state, mask, NULL); |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1064 | /** |
Liu Bo | 10983f2 | 2012-07-11 15:26:19 +0800 | [diff] [blame] | 1065 | * convert_extent_bit - convert all bits in a given range from one bit to |
| 1066 | * another |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1067 | * @tree: the io tree to search |
| 1068 | * @start: the start offset in bytes |
| 1069 | * @end: the end offset in bytes (inclusive) |
| 1070 | * @bits: the bits to set in this range |
| 1071 | * @clear_bits: the bits to clear in this range |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1072 | * @cached_state: state that we're going to cache |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1073 | * @mask: the allocation mask |
| 1074 | * |
| 1075 | * This will go through and set bits for the given range. If any states exist |
| 1076 | * already in this range they are set with the given bit and cleared of the |
| 1077 | * clear_bits. This is only meant to be used by things that are mergeable, ie |
| 1078 | * converting from say DELALLOC to DIRTY. This is not meant to be used with |
| 1079 | * boundary bits like LOCK. |
| 1080 | */ |
| 1081 | int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1082 | unsigned bits, unsigned clear_bits, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1083 | struct extent_state **cached_state, gfp_t mask) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1084 | { |
| 1085 | struct extent_state *state; |
| 1086 | struct extent_state *prealloc = NULL; |
| 1087 | struct rb_node *node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1088 | struct rb_node **p; |
| 1089 | struct rb_node *parent; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1090 | int err = 0; |
| 1091 | u64 last_start; |
| 1092 | u64 last_end; |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1093 | bool first_iteration = true; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1094 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 1095 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 1096 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1097 | again: |
| 1098 | if (!prealloc && (mask & __GFP_WAIT)) { |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1099 | /* |
| 1100 | * Best effort, don't worry if extent state allocation fails |
| 1101 | * here for the first iteration. We might have a cached state |
| 1102 | * that matches exactly the target range, in which case no |
| 1103 | * extent state allocations are needed. We'll only know this |
| 1104 | * after locking the tree. |
| 1105 | */ |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1106 | prealloc = alloc_extent_state(mask); |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1107 | if (!prealloc && !first_iteration) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1108 | return -ENOMEM; |
| 1109 | } |
| 1110 | |
| 1111 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1112 | if (cached_state && *cached_state) { |
| 1113 | state = *cached_state; |
| 1114 | if (state->start <= start && state->end > start && |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1115 | extent_state_in_tree(state)) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1116 | node = &state->rb_node; |
| 1117 | goto hit_next; |
| 1118 | } |
| 1119 | } |
| 1120 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1121 | /* |
| 1122 | * this search will find all the extents that end after |
| 1123 | * our range starts. |
| 1124 | */ |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1125 | node = tree_search_for_insert(tree, start, &p, &parent); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1126 | if (!node) { |
| 1127 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1128 | if (!prealloc) { |
| 1129 | err = -ENOMEM; |
| 1130 | goto out; |
| 1131 | } |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1132 | err = insert_state(tree, prealloc, start, end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 1133 | &p, &parent, &bits, NULL); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1134 | if (err) |
| 1135 | extent_io_tree_panic(tree, err); |
Filipe David Borba Manana | c42ac0b | 2013-11-26 15:01:34 +0000 | [diff] [blame] | 1136 | cache_state(prealloc, cached_state); |
| 1137 | prealloc = NULL; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1138 | goto out; |
| 1139 | } |
| 1140 | state = rb_entry(node, struct extent_state, rb_node); |
| 1141 | hit_next: |
| 1142 | last_start = state->start; |
| 1143 | last_end = state->end; |
| 1144 | |
| 1145 | /* |
| 1146 | * | ---- desired range ---- | |
| 1147 | * | state | |
| 1148 | * |
| 1149 | * Just lock what we found and keep going |
| 1150 | */ |
| 1151 | if (state->start == start && state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 1152 | set_state_bits(tree, state, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1153 | cache_state(state, cached_state); |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1154 | state = clear_state_bit(tree, state, &clear_bits, 0); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1155 | if (last_end == (u64)-1) |
| 1156 | goto out; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1157 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1158 | if (start < end && state && state->start == start && |
| 1159 | !need_resched()) |
| 1160 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1161 | goto search_again; |
| 1162 | } |
| 1163 | |
| 1164 | /* |
| 1165 | * | ---- desired range ---- | |
| 1166 | * | state | |
| 1167 | * or |
| 1168 | * | ------------- state -------------- | |
| 1169 | * |
| 1170 | * We need to split the extent we found, and may flip bits on |
| 1171 | * second half. |
| 1172 | * |
| 1173 | * If the extent we found extends past our |
| 1174 | * range, we just split and search again. It'll get split |
| 1175 | * again the next time though. |
| 1176 | * |
| 1177 | * If the extent we found is inside our range, we set the |
| 1178 | * desired bit on it. |
| 1179 | */ |
| 1180 | if (state->start < start) { |
| 1181 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1182 | if (!prealloc) { |
| 1183 | err = -ENOMEM; |
| 1184 | goto out; |
| 1185 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1186 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1187 | if (err) |
| 1188 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1189 | prealloc = NULL; |
| 1190 | if (err) |
| 1191 | goto out; |
| 1192 | if (state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 1193 | set_state_bits(tree, state, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1194 | cache_state(state, cached_state); |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1195 | state = clear_state_bit(tree, state, &clear_bits, 0); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1196 | if (last_end == (u64)-1) |
| 1197 | goto out; |
| 1198 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1199 | if (start < end && state && state->start == start && |
| 1200 | !need_resched()) |
| 1201 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1202 | } |
| 1203 | goto search_again; |
| 1204 | } |
| 1205 | /* |
| 1206 | * | ---- desired range ---- | |
| 1207 | * | state | or | state | |
| 1208 | * |
| 1209 | * There's a hole, we need to insert something in it and |
| 1210 | * ignore the extent we found. |
| 1211 | */ |
| 1212 | if (state->start > start) { |
| 1213 | u64 this_end; |
| 1214 | if (end < last_start) |
| 1215 | this_end = end; |
| 1216 | else |
| 1217 | this_end = last_start - 1; |
| 1218 | |
| 1219 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1220 | if (!prealloc) { |
| 1221 | err = -ENOMEM; |
| 1222 | goto out; |
| 1223 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1224 | |
| 1225 | /* |
| 1226 | * Avoid to free 'prealloc' if it can be merged with |
| 1227 | * the later extent. |
| 1228 | */ |
| 1229 | err = insert_state(tree, prealloc, start, this_end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 1230 | NULL, NULL, &bits, NULL); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1231 | if (err) |
| 1232 | extent_io_tree_panic(tree, err); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1233 | cache_state(prealloc, cached_state); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1234 | prealloc = NULL; |
| 1235 | start = this_end + 1; |
| 1236 | goto search_again; |
| 1237 | } |
| 1238 | /* |
| 1239 | * | ---- desired range ---- | |
| 1240 | * | state | |
| 1241 | * We need to split the extent, and set the bit |
| 1242 | * on the first half |
| 1243 | */ |
| 1244 | if (state->start <= end && state->end > end) { |
| 1245 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1246 | if (!prealloc) { |
| 1247 | err = -ENOMEM; |
| 1248 | goto out; |
| 1249 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1250 | |
| 1251 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1252 | if (err) |
| 1253 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1254 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 1255 | set_state_bits(tree, prealloc, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1256 | cache_state(prealloc, cached_state); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1257 | clear_state_bit(tree, prealloc, &clear_bits, 0); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1258 | prealloc = NULL; |
| 1259 | goto out; |
| 1260 | } |
| 1261 | |
| 1262 | goto search_again; |
| 1263 | |
| 1264 | out: |
| 1265 | spin_unlock(&tree->lock); |
| 1266 | if (prealloc) |
| 1267 | free_extent_state(prealloc); |
| 1268 | |
| 1269 | return err; |
| 1270 | |
| 1271 | search_again: |
| 1272 | if (start > end) |
| 1273 | goto out; |
| 1274 | spin_unlock(&tree->lock); |
| 1275 | if (mask & __GFP_WAIT) |
| 1276 | cond_resched(); |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1277 | first_iteration = false; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1278 | goto again; |
| 1279 | } |
| 1280 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1281 | /* wrappers around set/clear extent bit */ |
| 1282 | int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, |
| 1283 | gfp_t mask) |
| 1284 | { |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1285 | return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL, |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1286 | NULL, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1287 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1288 | |
| 1289 | int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1290 | unsigned bits, gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1291 | { |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1292 | return set_extent_bit(tree, start, end, bits, NULL, |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1293 | NULL, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1294 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1295 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 1296 | int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
| 1297 | unsigned bits, gfp_t mask, |
| 1298 | struct extent_changeset *changeset) |
| 1299 | { |
| 1300 | /* |
| 1301 | * We don't support EXTENT_LOCKED yet, as current changeset will |
| 1302 | * record any bits changed, so for EXTENT_LOCKED case, it will |
| 1303 | * either fail with -EEXIST or changeset will record the whole |
| 1304 | * range. |
| 1305 | */ |
| 1306 | BUG_ON(bits & EXTENT_LOCKED); |
| 1307 | |
| 1308 | return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, mask, |
| 1309 | changeset); |
| 1310 | } |
| 1311 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1312 | int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1313 | unsigned bits, gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1314 | { |
Filipe Manana | 0f31871 | 2015-05-14 20:41:07 +0100 | [diff] [blame] | 1315 | int wake = 0; |
| 1316 | |
| 1317 | if (bits & EXTENT_LOCKED) |
| 1318 | wake = 1; |
| 1319 | |
| 1320 | return clear_extent_bit(tree, start, end, bits, wake, 0, NULL, 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 | |
| 1323 | 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] | 1324 | struct extent_state **cached_state, gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1325 | { |
| 1326 | return set_extent_bit(tree, start, end, |
Liu Bo | fee187d | 2011-09-29 15:55:28 +0800 | [diff] [blame] | 1327 | EXTENT_DELALLOC | EXTENT_UPTODATE, |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1328 | NULL, cached_state, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1329 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1330 | |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 1331 | int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end, |
| 1332 | struct extent_state **cached_state, gfp_t mask) |
| 1333 | { |
| 1334 | return set_extent_bit(tree, start, end, |
| 1335 | EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG, |
| 1336 | NULL, cached_state, mask); |
| 1337 | } |
| 1338 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1339 | int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, |
| 1340 | gfp_t mask) |
| 1341 | { |
| 1342 | return clear_extent_bit(tree, start, end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1343 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1344 | EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1345 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1346 | |
| 1347 | int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end, |
| 1348 | gfp_t mask) |
| 1349 | { |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1350 | return set_extent_bit(tree, start, end, EXTENT_NEW, NULL, |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1351 | NULL, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1352 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1353 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1354 | 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] | 1355 | struct extent_state **cached_state, gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1356 | { |
Liu Bo | 6b67a32 | 2013-03-28 08:30:28 +0000 | [diff] [blame] | 1357 | return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL, |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1358 | cached_state, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1359 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1360 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1361 | int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end, |
| 1362 | struct extent_state **cached_state, gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1363 | { |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1364 | return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1365 | cached_state, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1366 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1367 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1368 | /* |
| 1369 | * either insert or lock state struct between start and end use mask to tell |
| 1370 | * us if waiting is desired. |
| 1371 | */ |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1372 | int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1373 | unsigned bits, struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1374 | { |
| 1375 | int err; |
| 1376 | u64 failed_start; |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1377 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1378 | while (1) { |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1379 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits, |
| 1380 | EXTENT_LOCKED, &failed_start, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 1381 | cached_state, GFP_NOFS, NULL); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1382 | if (err == -EEXIST) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1383 | wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED); |
| 1384 | start = failed_start; |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1385 | } else |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1386 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1387 | WARN_ON(start > end); |
| 1388 | } |
| 1389 | return err; |
| 1390 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1391 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1392 | int lock_extent(struct extent_io_tree *tree, u64 start, u64 end) |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1393 | { |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1394 | return lock_extent_bits(tree, start, end, 0, NULL); |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1395 | } |
| 1396 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1397 | 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] | 1398 | { |
| 1399 | int err; |
| 1400 | u64 failed_start; |
| 1401 | |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1402 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame^] | 1403 | &failed_start, NULL, GFP_NOFS, NULL); |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1404 | if (err == -EEXIST) { |
| 1405 | if (failed_start > start) |
| 1406 | clear_extent_bit(tree, start, failed_start - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1407 | EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1408 | return 0; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1409 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1410 | return 1; |
| 1411 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1412 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1413 | int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end, |
| 1414 | struct extent_state **cached, gfp_t mask) |
| 1415 | { |
| 1416 | return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached, |
| 1417 | mask); |
| 1418 | } |
| 1419 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1420 | int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1421 | { |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1422 | return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1423 | GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1424 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1425 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1426 | int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end) |
| 1427 | { |
| 1428 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1429 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1430 | struct page *page; |
| 1431 | |
| 1432 | while (index <= end_index) { |
| 1433 | page = find_get_page(inode->i_mapping, index); |
| 1434 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
| 1435 | clear_page_dirty_for_io(page); |
| 1436 | page_cache_release(page); |
| 1437 | index++; |
| 1438 | } |
| 1439 | return 0; |
| 1440 | } |
| 1441 | |
| 1442 | int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end) |
| 1443 | { |
| 1444 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1445 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1446 | struct page *page; |
| 1447 | |
| 1448 | while (index <= end_index) { |
| 1449 | page = find_get_page(inode->i_mapping, index); |
| 1450 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1451 | __set_page_dirty_nobuffers(page); |
Konstantin Khebnikov | 8d38633 | 2015-02-11 15:26:55 -0800 | [diff] [blame] | 1452 | account_page_redirty(page); |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1453 | page_cache_release(page); |
| 1454 | index++; |
| 1455 | } |
| 1456 | return 0; |
| 1457 | } |
| 1458 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1459 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1460 | * helper function to set both pages and extents in the tree writeback |
| 1461 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1462 | 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] | 1463 | { |
| 1464 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1465 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1466 | struct page *page; |
| 1467 | |
| 1468 | while (index <= end_index) { |
| 1469 | page = find_get_page(tree->mapping, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1470 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1471 | set_page_writeback(page); |
| 1472 | page_cache_release(page); |
| 1473 | index++; |
| 1474 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1475 | return 0; |
| 1476 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1477 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1478 | /* find the first state struct with 'bits' set after 'start', and |
| 1479 | * return it. tree->lock must be held. NULL will returned if |
| 1480 | * nothing was found after 'start' |
| 1481 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1482 | static struct extent_state * |
| 1483 | find_first_extent_bit_state(struct extent_io_tree *tree, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1484 | u64 start, unsigned bits) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1485 | { |
| 1486 | struct rb_node *node; |
| 1487 | struct extent_state *state; |
| 1488 | |
| 1489 | /* |
| 1490 | * this search will find all the extents that end after |
| 1491 | * our range starts. |
| 1492 | */ |
| 1493 | node = tree_search(tree, start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1494 | if (!node) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1495 | goto out; |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1496 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1497 | while (1) { |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1498 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1499 | if (state->end >= start && (state->state & bits)) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1500 | return state; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1501 | |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1502 | node = rb_next(node); |
| 1503 | if (!node) |
| 1504 | break; |
| 1505 | } |
| 1506 | out: |
| 1507 | return NULL; |
| 1508 | } |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1509 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1510 | /* |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1511 | * find the first offset in the io tree with 'bits' set. zero is |
| 1512 | * returned if we find something, and *start_ret and *end_ret are |
| 1513 | * set to reflect the state struct that was found. |
| 1514 | * |
Wang Sheng-Hui | 477d7ea | 2012-04-06 14:35:47 +0800 | [diff] [blame] | 1515 | * If nothing was found, 1 is returned. If found something, return 0. |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1516 | */ |
| 1517 | int find_first_extent_bit(struct extent_io_tree *tree, u64 start, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1518 | u64 *start_ret, u64 *end_ret, unsigned bits, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1519 | struct extent_state **cached_state) |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1520 | { |
| 1521 | struct extent_state *state; |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1522 | struct rb_node *n; |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1523 | int ret = 1; |
| 1524 | |
| 1525 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1526 | if (cached_state && *cached_state) { |
| 1527 | state = *cached_state; |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1528 | if (state->end == start - 1 && extent_state_in_tree(state)) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1529 | n = rb_next(&state->rb_node); |
| 1530 | while (n) { |
| 1531 | state = rb_entry(n, struct extent_state, |
| 1532 | rb_node); |
| 1533 | if (state->state & bits) |
| 1534 | goto got_it; |
| 1535 | n = rb_next(n); |
| 1536 | } |
| 1537 | free_extent_state(*cached_state); |
| 1538 | *cached_state = NULL; |
| 1539 | goto out; |
| 1540 | } |
| 1541 | free_extent_state(*cached_state); |
| 1542 | *cached_state = NULL; |
| 1543 | } |
| 1544 | |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1545 | state = find_first_extent_bit_state(tree, start, bits); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1546 | got_it: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1547 | if (state) { |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 1548 | cache_state_if_flags(state, cached_state, 0); |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1549 | *start_ret = state->start; |
| 1550 | *end_ret = state->end; |
| 1551 | ret = 0; |
| 1552 | } |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1553 | out: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1554 | spin_unlock(&tree->lock); |
| 1555 | return ret; |
| 1556 | } |
| 1557 | |
| 1558 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1559 | * find a contiguous range of bytes in the file marked as delalloc, not |
| 1560 | * more than 'max_bytes'. start and end are used to return the range, |
| 1561 | * |
| 1562 | * 1 is returned if we find something, 0 if nothing was in the tree |
| 1563 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1564 | static noinline u64 find_delalloc_range(struct extent_io_tree *tree, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1565 | u64 *start, u64 *end, u64 max_bytes, |
| 1566 | struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1567 | { |
| 1568 | struct rb_node *node; |
| 1569 | struct extent_state *state; |
| 1570 | u64 cur_start = *start; |
| 1571 | u64 found = 0; |
| 1572 | u64 total_bytes = 0; |
| 1573 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1574 | spin_lock(&tree->lock); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1575 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1576 | /* |
| 1577 | * this search will find all the extents that end after |
| 1578 | * our range starts. |
| 1579 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1580 | node = tree_search(tree, cur_start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1581 | if (!node) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1582 | if (!found) |
| 1583 | *end = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1584 | goto out; |
| 1585 | } |
| 1586 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1587 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1588 | state = rb_entry(node, struct extent_state, rb_node); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1589 | if (found && (state->start != cur_start || |
| 1590 | (state->state & EXTENT_BOUNDARY))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1591 | goto out; |
| 1592 | } |
| 1593 | if (!(state->state & EXTENT_DELALLOC)) { |
| 1594 | if (!found) |
| 1595 | *end = state->end; |
| 1596 | goto out; |
| 1597 | } |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1598 | if (!found) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1599 | *start = state->start; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1600 | *cached_state = state; |
| 1601 | atomic_inc(&state->refs); |
| 1602 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1603 | found++; |
| 1604 | *end = state->end; |
| 1605 | cur_start = state->end + 1; |
| 1606 | node = rb_next(node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1607 | total_bytes += state->end - state->start + 1; |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 1608 | if (total_bytes >= max_bytes) |
Josef Bacik | 573aeca | 2013-08-30 14:38:49 -0400 | [diff] [blame] | 1609 | break; |
Josef Bacik | 573aeca | 2013-08-30 14:38:49 -0400 | [diff] [blame] | 1610 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1611 | break; |
| 1612 | } |
| 1613 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1614 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1615 | return found; |
| 1616 | } |
| 1617 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1618 | static noinline void __unlock_for_delalloc(struct inode *inode, |
| 1619 | struct page *locked_page, |
| 1620 | u64 start, u64 end) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1621 | { |
| 1622 | int ret; |
| 1623 | struct page *pages[16]; |
| 1624 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1625 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1626 | unsigned long nr_pages = end_index - index + 1; |
| 1627 | int i; |
| 1628 | |
| 1629 | if (index == locked_page->index && end_index == index) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1630 | return; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1631 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1632 | while (nr_pages > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1633 | ret = find_get_pages_contig(inode->i_mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1634 | min_t(unsigned long, nr_pages, |
| 1635 | ARRAY_SIZE(pages)), pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1636 | for (i = 0; i < ret; i++) { |
| 1637 | if (pages[i] != locked_page) |
| 1638 | unlock_page(pages[i]); |
| 1639 | page_cache_release(pages[i]); |
| 1640 | } |
| 1641 | nr_pages -= ret; |
| 1642 | index += ret; |
| 1643 | cond_resched(); |
| 1644 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | static noinline int lock_delalloc_pages(struct inode *inode, |
| 1648 | struct page *locked_page, |
| 1649 | u64 delalloc_start, |
| 1650 | u64 delalloc_end) |
| 1651 | { |
| 1652 | unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT; |
| 1653 | unsigned long start_index = index; |
| 1654 | unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT; |
| 1655 | unsigned long pages_locked = 0; |
| 1656 | struct page *pages[16]; |
| 1657 | unsigned long nrpages; |
| 1658 | int ret; |
| 1659 | int i; |
| 1660 | |
| 1661 | /* the caller is responsible for locking the start index */ |
| 1662 | if (index == locked_page->index && index == end_index) |
| 1663 | return 0; |
| 1664 | |
| 1665 | /* skip the page at the start index */ |
| 1666 | nrpages = end_index - index + 1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1667 | while (nrpages > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1668 | ret = find_get_pages_contig(inode->i_mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1669 | min_t(unsigned long, |
| 1670 | nrpages, ARRAY_SIZE(pages)), pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1671 | if (ret == 0) { |
| 1672 | ret = -EAGAIN; |
| 1673 | goto done; |
| 1674 | } |
| 1675 | /* now we have an array of pages, lock them all */ |
| 1676 | for (i = 0; i < ret; i++) { |
| 1677 | /* |
| 1678 | * the caller is taking responsibility for |
| 1679 | * locked_page |
| 1680 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1681 | if (pages[i] != locked_page) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1682 | lock_page(pages[i]); |
Chris Mason | f2b1c41 | 2008-11-10 07:31:30 -0500 | [diff] [blame] | 1683 | if (!PageDirty(pages[i]) || |
| 1684 | pages[i]->mapping != inode->i_mapping) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1685 | ret = -EAGAIN; |
| 1686 | unlock_page(pages[i]); |
| 1687 | page_cache_release(pages[i]); |
| 1688 | goto done; |
| 1689 | } |
| 1690 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1691 | page_cache_release(pages[i]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1692 | pages_locked++; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1693 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1694 | nrpages -= ret; |
| 1695 | index += ret; |
| 1696 | cond_resched(); |
| 1697 | } |
| 1698 | ret = 0; |
| 1699 | done: |
| 1700 | if (ret && pages_locked) { |
| 1701 | __unlock_for_delalloc(inode, locked_page, |
| 1702 | delalloc_start, |
| 1703 | ((u64)(start_index + pages_locked - 1)) << |
| 1704 | PAGE_CACHE_SHIFT); |
| 1705 | } |
| 1706 | return ret; |
| 1707 | } |
| 1708 | |
| 1709 | /* |
| 1710 | * find a contiguous range of bytes in the file marked as delalloc, not |
| 1711 | * more than 'max_bytes'. start and end are used to return the range, |
| 1712 | * |
| 1713 | * 1 is returned if we find something, 0 if nothing was in the tree |
| 1714 | */ |
Josef Bacik | 294e30f | 2013-10-09 12:00:56 -0400 | [diff] [blame] | 1715 | STATIC u64 find_lock_delalloc_range(struct inode *inode, |
| 1716 | struct extent_io_tree *tree, |
| 1717 | struct page *locked_page, u64 *start, |
| 1718 | u64 *end, u64 max_bytes) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1719 | { |
| 1720 | u64 delalloc_start; |
| 1721 | u64 delalloc_end; |
| 1722 | u64 found; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1723 | struct extent_state *cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1724 | int ret; |
| 1725 | int loops = 0; |
| 1726 | |
| 1727 | again: |
| 1728 | /* step one, find a bunch of delalloc bytes starting at start */ |
| 1729 | delalloc_start = *start; |
| 1730 | delalloc_end = 0; |
| 1731 | found = find_delalloc_range(tree, &delalloc_start, &delalloc_end, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1732 | max_bytes, &cached_state); |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1733 | if (!found || delalloc_end <= *start) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1734 | *start = delalloc_start; |
| 1735 | *end = delalloc_end; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1736 | free_extent_state(cached_state); |
Liu Bo | 385fe0b | 2013-10-01 23:49:49 +0800 | [diff] [blame] | 1737 | return 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | /* |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1741 | * start comes from the offset of locked_page. We have to lock |
| 1742 | * pages in order, so we can't process delalloc bytes before |
| 1743 | * locked_page |
| 1744 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1745 | if (delalloc_start < *start) |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1746 | delalloc_start = *start; |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1747 | |
| 1748 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1749 | * 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] | 1750 | */ |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 1751 | if (delalloc_end + 1 - delalloc_start > max_bytes) |
| 1752 | delalloc_end = delalloc_start + max_bytes - 1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1753 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1754 | /* step two, lock all the pages after the page that has start */ |
| 1755 | ret = lock_delalloc_pages(inode, locked_page, |
| 1756 | delalloc_start, delalloc_end); |
| 1757 | if (ret == -EAGAIN) { |
| 1758 | /* some of the pages are gone, lets avoid looping by |
| 1759 | * shortening the size of the delalloc range we're searching |
| 1760 | */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1761 | free_extent_state(cached_state); |
Chris Mason | 7d78874 | 2014-05-21 05:49:54 -0700 | [diff] [blame] | 1762 | cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1763 | if (!loops) { |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 1764 | max_bytes = PAGE_CACHE_SIZE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1765 | loops = 1; |
| 1766 | goto again; |
| 1767 | } else { |
| 1768 | found = 0; |
| 1769 | goto out_failed; |
| 1770 | } |
| 1771 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1772 | BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1773 | |
| 1774 | /* step three, lock the state bits for the whole range */ |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1775 | lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1776 | |
| 1777 | /* then test to make sure it is all still delalloc */ |
| 1778 | ret = test_range_bit(tree, delalloc_start, delalloc_end, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1779 | EXTENT_DELALLOC, 1, cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1780 | if (!ret) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1781 | unlock_extent_cached(tree, delalloc_start, delalloc_end, |
| 1782 | &cached_state, GFP_NOFS); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1783 | __unlock_for_delalloc(inode, locked_page, |
| 1784 | delalloc_start, delalloc_end); |
| 1785 | cond_resched(); |
| 1786 | goto again; |
| 1787 | } |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1788 | free_extent_state(cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1789 | *start = delalloc_start; |
| 1790 | *end = delalloc_end; |
| 1791 | out_failed: |
| 1792 | return found; |
| 1793 | } |
| 1794 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1795 | int extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end, |
| 1796 | struct page *locked_page, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1797 | unsigned clear_bits, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1798 | unsigned long page_ops) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1799 | { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1800 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1801 | int ret; |
| 1802 | struct page *pages[16]; |
| 1803 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 1804 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; |
| 1805 | unsigned long nr_pages = end_index - index + 1; |
| 1806 | int i; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1807 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1808 | 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] | 1809 | if (page_ops == 0) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1810 | return 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1811 | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1812 | if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0) |
| 1813 | mapping_set_error(inode->i_mapping, -EIO); |
| 1814 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1815 | while (nr_pages > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1816 | ret = find_get_pages_contig(inode->i_mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1817 | min_t(unsigned long, |
| 1818 | nr_pages, ARRAY_SIZE(pages)), pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1819 | for (i = 0; i < ret; i++) { |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1820 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1821 | if (page_ops & PAGE_SET_PRIVATE2) |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1822 | SetPagePrivate2(pages[i]); |
| 1823 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1824 | if (pages[i] == locked_page) { |
| 1825 | page_cache_release(pages[i]); |
| 1826 | continue; |
| 1827 | } |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1828 | if (page_ops & PAGE_CLEAR_DIRTY) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1829 | clear_page_dirty_for_io(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1830 | if (page_ops & PAGE_SET_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1831 | set_page_writeback(pages[i]); |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1832 | if (page_ops & PAGE_SET_ERROR) |
| 1833 | SetPageError(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1834 | if (page_ops & PAGE_END_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1835 | end_page_writeback(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1836 | if (page_ops & PAGE_UNLOCK) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1837 | unlock_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1838 | page_cache_release(pages[i]); |
| 1839 | } |
| 1840 | nr_pages -= ret; |
| 1841 | index += ret; |
| 1842 | cond_resched(); |
| 1843 | } |
| 1844 | return 0; |
| 1845 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1846 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1847 | /* |
| 1848 | * count the number of bytes in the tree that have a given bit(s) |
| 1849 | * set. This can be fairly slow, except for EXTENT_DIRTY which is |
| 1850 | * cached. The total number found is returned. |
| 1851 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1852 | u64 count_range_bits(struct extent_io_tree *tree, |
| 1853 | u64 *start, u64 search_end, u64 max_bytes, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1854 | unsigned bits, int contig) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1855 | { |
| 1856 | struct rb_node *node; |
| 1857 | struct extent_state *state; |
| 1858 | u64 cur_start = *start; |
| 1859 | u64 total_bytes = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1860 | u64 last = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1861 | int found = 0; |
| 1862 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 1863 | if (WARN_ON(search_end <= cur_start)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1864 | return 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1865 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1866 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1867 | if (cur_start == 0 && bits == EXTENT_DIRTY) { |
| 1868 | total_bytes = tree->dirty_bytes; |
| 1869 | goto out; |
| 1870 | } |
| 1871 | /* |
| 1872 | * this search will find all the extents that end after |
| 1873 | * our range starts. |
| 1874 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1875 | node = tree_search(tree, cur_start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1876 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1877 | goto out; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1878 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1879 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1880 | state = rb_entry(node, struct extent_state, rb_node); |
| 1881 | if (state->start > search_end) |
| 1882 | break; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1883 | if (contig && found && state->start > last + 1) |
| 1884 | break; |
| 1885 | if (state->end >= cur_start && (state->state & bits) == bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1886 | total_bytes += min(search_end, state->end) + 1 - |
| 1887 | max(cur_start, state->start); |
| 1888 | if (total_bytes >= max_bytes) |
| 1889 | break; |
| 1890 | if (!found) { |
Josef Bacik | af60bed | 2011-05-04 11:11:17 -0400 | [diff] [blame] | 1891 | *start = max(cur_start, state->start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1892 | found = 1; |
| 1893 | } |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1894 | last = state->end; |
| 1895 | } else if (contig && found) { |
| 1896 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1897 | } |
| 1898 | node = rb_next(node); |
| 1899 | if (!node) |
| 1900 | break; |
| 1901 | } |
| 1902 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1903 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1904 | return total_bytes; |
| 1905 | } |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1906 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1907 | /* |
| 1908 | * set the private field for a given byte offset in the tree. If there isn't |
| 1909 | * an extent_state there already, this does nothing. |
| 1910 | */ |
Sergei Trofimovich | 171170c | 2013-08-14 23:27:46 +0300 | [diff] [blame] | 1911 | 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] | 1912 | { |
| 1913 | struct rb_node *node; |
| 1914 | struct extent_state *state; |
| 1915 | int ret = 0; |
| 1916 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1917 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1918 | /* |
| 1919 | * this search will find all the extents that end after |
| 1920 | * our range starts. |
| 1921 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1922 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1923 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1924 | ret = -ENOENT; |
| 1925 | goto out; |
| 1926 | } |
| 1927 | state = rb_entry(node, struct extent_state, rb_node); |
| 1928 | if (state->start != start) { |
| 1929 | ret = -ENOENT; |
| 1930 | goto out; |
| 1931 | } |
| 1932 | state->private = private; |
| 1933 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1934 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1935 | return ret; |
| 1936 | } |
| 1937 | |
| 1938 | int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private) |
| 1939 | { |
| 1940 | struct rb_node *node; |
| 1941 | struct extent_state *state; |
| 1942 | int ret = 0; |
| 1943 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1944 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1945 | /* |
| 1946 | * this search will find all the extents that end after |
| 1947 | * our range starts. |
| 1948 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1949 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1950 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1951 | ret = -ENOENT; |
| 1952 | goto out; |
| 1953 | } |
| 1954 | state = rb_entry(node, struct extent_state, rb_node); |
| 1955 | if (state->start != start) { |
| 1956 | ret = -ENOENT; |
| 1957 | goto out; |
| 1958 | } |
| 1959 | *private = state->private; |
| 1960 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1961 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1962 | return ret; |
| 1963 | } |
| 1964 | |
| 1965 | /* |
| 1966 | * searches a range in the state tree for a given mask. |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1967 | * 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] | 1968 | * has the bits set. Otherwise, 1 is returned if any bit in the |
| 1969 | * range is found set. |
| 1970 | */ |
| 1971 | int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1972 | unsigned bits, int filled, struct extent_state *cached) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1973 | { |
| 1974 | struct extent_state *state = NULL; |
| 1975 | struct rb_node *node; |
| 1976 | int bitset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1977 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1978 | spin_lock(&tree->lock); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1979 | if (cached && extent_state_in_tree(cached) && cached->start <= start && |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 1980 | cached->end > start) |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1981 | node = &cached->rb_node; |
| 1982 | else |
| 1983 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1984 | while (node && start <= end) { |
| 1985 | state = rb_entry(node, struct extent_state, rb_node); |
| 1986 | |
| 1987 | if (filled && state->start > start) { |
| 1988 | bitset = 0; |
| 1989 | break; |
| 1990 | } |
| 1991 | |
| 1992 | if (state->start > end) |
| 1993 | break; |
| 1994 | |
| 1995 | if (state->state & bits) { |
| 1996 | bitset = 1; |
| 1997 | if (!filled) |
| 1998 | break; |
| 1999 | } else if (filled) { |
| 2000 | bitset = 0; |
| 2001 | break; |
| 2002 | } |
Chris Mason | 46562ce | 2009-09-23 20:23:16 -0400 | [diff] [blame] | 2003 | |
| 2004 | if (state->end == (u64)-1) |
| 2005 | break; |
| 2006 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2007 | start = state->end + 1; |
| 2008 | if (start > end) |
| 2009 | break; |
| 2010 | node = rb_next(node); |
| 2011 | if (!node) { |
| 2012 | if (filled) |
| 2013 | bitset = 0; |
| 2014 | break; |
| 2015 | } |
| 2016 | } |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2017 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2018 | return bitset; |
| 2019 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2020 | |
| 2021 | /* |
| 2022 | * helper function to set a given page up to date if all the |
| 2023 | * extents in the tree for that page are up to date |
| 2024 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2025 | 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] | 2026 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2027 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2028 | u64 end = start + PAGE_CACHE_SIZE - 1; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2029 | if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2030 | SetPageUptodate(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2031 | } |
| 2032 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2033 | int free_io_failure(struct inode *inode, struct io_failure_record *rec) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2034 | { |
| 2035 | int ret; |
| 2036 | int err = 0; |
| 2037 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 2038 | |
| 2039 | set_state_private(failure_tree, rec->start, 0); |
| 2040 | ret = clear_extent_bits(failure_tree, rec->start, |
| 2041 | rec->start + rec->len - 1, |
| 2042 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 2043 | if (ret) |
| 2044 | err = ret; |
| 2045 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2046 | ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start, |
| 2047 | rec->start + rec->len - 1, |
| 2048 | EXTENT_DAMAGED, GFP_NOFS); |
| 2049 | if (ret && !err) |
| 2050 | err = ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2051 | |
| 2052 | kfree(rec); |
| 2053 | return err; |
| 2054 | } |
| 2055 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2056 | /* |
| 2057 | * this bypasses the standard btrfs submit functions deliberately, as |
| 2058 | * the standard behavior is to write all copies in a raid setup. here we only |
| 2059 | * want to write the one bad copy. so we do the mapping for ourselves and issue |
| 2060 | * submit_bio directly. |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2061 | * to avoid any synchronization issues, wait for the data after writing, which |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2062 | * actually prevents the read that triggered the error from finishing. |
| 2063 | * currently, there can be no more than two copies of every data bit. thus, |
| 2064 | * exactly one rewrite is required. |
| 2065 | */ |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2066 | int repair_io_failure(struct inode *inode, u64 start, u64 length, u64 logical, |
| 2067 | struct page *page, unsigned int pg_offset, int mirror_num) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2068 | { |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2069 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2070 | struct bio *bio; |
| 2071 | struct btrfs_device *dev; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2072 | u64 map_length = 0; |
| 2073 | u64 sector; |
| 2074 | struct btrfs_bio *bbio = NULL; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2075 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2076 | int ret; |
| 2077 | |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2078 | ASSERT(!(fs_info->sb->s_flags & MS_RDONLY)); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2079 | BUG_ON(!mirror_num); |
| 2080 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2081 | /* we can't repair anything in raid56 yet */ |
| 2082 | if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num)) |
| 2083 | return 0; |
| 2084 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2085 | bio = btrfs_io_bio_alloc(GFP_NOFS, 1); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2086 | if (!bio) |
| 2087 | return -EIO; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2088 | bio->bi_iter.bi_size = 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2089 | map_length = length; |
| 2090 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2091 | ret = btrfs_map_block(fs_info, WRITE, logical, |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2092 | &map_length, &bbio, mirror_num); |
| 2093 | if (ret) { |
| 2094 | bio_put(bio); |
| 2095 | return -EIO; |
| 2096 | } |
| 2097 | BUG_ON(mirror_num != bbio->mirror_num); |
| 2098 | sector = bbio->stripes[mirror_num-1].physical >> 9; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2099 | bio->bi_iter.bi_sector = sector; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2100 | dev = bbio->stripes[mirror_num-1].dev; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 2101 | btrfs_put_bbio(bbio); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2102 | if (!dev || !dev->bdev || !dev->writeable) { |
| 2103 | bio_put(bio); |
| 2104 | return -EIO; |
| 2105 | } |
| 2106 | bio->bi_bdev = dev->bdev; |
Miao Xie | ffdd201 | 2014-09-12 18:44:00 +0800 | [diff] [blame] | 2107 | bio_add_page(bio, page, length, pg_offset); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2108 | |
Kent Overstreet | 33879d4 | 2013-11-23 22:33:32 -0800 | [diff] [blame] | 2109 | if (btrfsic_submit_bio_wait(WRITE_SYNC, bio)) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2110 | /* try to remap that extent elsewhere? */ |
| 2111 | bio_put(bio); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2112 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2113 | return -EIO; |
| 2114 | } |
| 2115 | |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 2116 | btrfs_info_rl_in_rcu(fs_info, |
| 2117 | "read error corrected: ino %llu off %llu (dev %s sector %llu)", |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2118 | btrfs_ino(inode), start, |
| 2119 | rcu_str_deref(dev->name), sector); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2120 | bio_put(bio); |
| 2121 | return 0; |
| 2122 | } |
| 2123 | |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2124 | int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb, |
| 2125 | int mirror_num) |
| 2126 | { |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2127 | u64 start = eb->start; |
| 2128 | unsigned long i, num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | d95603b | 2012-04-12 15:55:15 -0400 | [diff] [blame] | 2129 | int ret = 0; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2130 | |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2131 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 2132 | return -EROFS; |
| 2133 | |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2134 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 2135 | struct page *p = eb->pages[i]; |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2136 | |
| 2137 | ret = repair_io_failure(root->fs_info->btree_inode, start, |
| 2138 | PAGE_CACHE_SIZE, start, p, |
| 2139 | start - page_offset(p), mirror_num); |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2140 | if (ret) |
| 2141 | break; |
| 2142 | start += PAGE_CACHE_SIZE; |
| 2143 | } |
| 2144 | |
| 2145 | return ret; |
| 2146 | } |
| 2147 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2148 | /* |
| 2149 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 2150 | * to see if we need to process or clean up an io_failure_record |
| 2151 | */ |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2152 | int clean_io_failure(struct inode *inode, u64 start, struct page *page, |
| 2153 | unsigned int pg_offset) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2154 | { |
| 2155 | u64 private; |
| 2156 | u64 private_failure; |
| 2157 | struct io_failure_record *failrec; |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2158 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2159 | struct extent_state *state; |
| 2160 | int num_copies; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2161 | int ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2162 | |
| 2163 | private = 0; |
| 2164 | ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
| 2165 | (u64)-1, 1, EXTENT_DIRTY, 0); |
| 2166 | if (!ret) |
| 2167 | return 0; |
| 2168 | |
| 2169 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start, |
| 2170 | &private_failure); |
| 2171 | if (ret) |
| 2172 | return 0; |
| 2173 | |
| 2174 | failrec = (struct io_failure_record *)(unsigned long) private_failure; |
| 2175 | BUG_ON(!failrec->this_mirror); |
| 2176 | |
| 2177 | if (failrec->in_validation) { |
| 2178 | /* there was no real error, just free the record */ |
| 2179 | pr_debug("clean_io_failure: freeing dummy error at %llu\n", |
| 2180 | failrec->start); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2181 | goto out; |
| 2182 | } |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2183 | if (fs_info->sb->s_flags & MS_RDONLY) |
| 2184 | goto out; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2185 | |
| 2186 | spin_lock(&BTRFS_I(inode)->io_tree.lock); |
| 2187 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 2188 | failrec->start, |
| 2189 | EXTENT_LOCKED); |
| 2190 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); |
| 2191 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2192 | if (state && state->start <= failrec->start && |
| 2193 | state->end >= failrec->start + failrec->len - 1) { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2194 | num_copies = btrfs_num_copies(fs_info, failrec->logical, |
| 2195 | failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2196 | if (num_copies > 1) { |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2197 | repair_io_failure(inode, start, failrec->len, |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2198 | failrec->logical, page, |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2199 | pg_offset, failrec->failed_mirror); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2200 | } |
| 2201 | } |
| 2202 | |
| 2203 | out: |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2204 | free_io_failure(inode, failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2205 | |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2206 | return 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2207 | } |
| 2208 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2209 | /* |
| 2210 | * Can be called when |
| 2211 | * - hold extent lock |
| 2212 | * - under ordered extent |
| 2213 | * - the inode is freeing |
| 2214 | */ |
| 2215 | void btrfs_free_io_failure_record(struct inode *inode, u64 start, u64 end) |
| 2216 | { |
| 2217 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 2218 | struct io_failure_record *failrec; |
| 2219 | struct extent_state *state, *next; |
| 2220 | |
| 2221 | if (RB_EMPTY_ROOT(&failure_tree->state)) |
| 2222 | return; |
| 2223 | |
| 2224 | spin_lock(&failure_tree->lock); |
| 2225 | state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY); |
| 2226 | while (state) { |
| 2227 | if (state->start > end) |
| 2228 | break; |
| 2229 | |
| 2230 | ASSERT(state->end <= end); |
| 2231 | |
| 2232 | next = next_state(state); |
| 2233 | |
Satoru Takeuchi | 6e1103a | 2014-12-25 18:21:41 +0900 | [diff] [blame] | 2234 | failrec = (struct io_failure_record *)(unsigned long)state->private; |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2235 | free_extent_state(state); |
| 2236 | kfree(failrec); |
| 2237 | |
| 2238 | state = next; |
| 2239 | } |
| 2240 | spin_unlock(&failure_tree->lock); |
| 2241 | } |
| 2242 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2243 | int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end, |
| 2244 | struct io_failure_record **failrec_ret) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2245 | { |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2246 | struct io_failure_record *failrec; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2247 | u64 private; |
| 2248 | struct extent_map *em; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2249 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 2250 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 2251 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2252 | int ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2253 | u64 logical; |
| 2254 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2255 | ret = get_state_private(failure_tree, start, &private); |
| 2256 | if (ret) { |
| 2257 | failrec = kzalloc(sizeof(*failrec), GFP_NOFS); |
| 2258 | if (!failrec) |
| 2259 | return -ENOMEM; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2260 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2261 | failrec->start = start; |
| 2262 | failrec->len = end - start + 1; |
| 2263 | failrec->this_mirror = 0; |
| 2264 | failrec->bio_flags = 0; |
| 2265 | failrec->in_validation = 0; |
| 2266 | |
| 2267 | read_lock(&em_tree->lock); |
| 2268 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 2269 | if (!em) { |
| 2270 | read_unlock(&em_tree->lock); |
| 2271 | kfree(failrec); |
| 2272 | return -EIO; |
| 2273 | } |
| 2274 | |
Filipe David Borba Manana | 68ba990 | 2013-11-25 03:22:07 +0000 | [diff] [blame] | 2275 | if (em->start > start || em->start + em->len <= start) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2276 | free_extent_map(em); |
| 2277 | em = NULL; |
| 2278 | } |
| 2279 | read_unlock(&em_tree->lock); |
Tsutomu Itoh | 7a2d6a6 | 2012-10-01 03:07:15 -0600 | [diff] [blame] | 2280 | if (!em) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2281 | kfree(failrec); |
| 2282 | return -EIO; |
| 2283 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2284 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2285 | logical = start - em->start; |
| 2286 | logical = em->block_start + logical; |
| 2287 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 2288 | logical = em->block_start; |
| 2289 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
| 2290 | extent_set_compress_type(&failrec->bio_flags, |
| 2291 | em->compress_type); |
| 2292 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2293 | |
| 2294 | pr_debug("Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu\n", |
| 2295 | logical, start, failrec->len); |
| 2296 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2297 | failrec->logical = logical; |
| 2298 | free_extent_map(em); |
| 2299 | |
| 2300 | /* set the bits in the private failure tree */ |
| 2301 | ret = set_extent_bits(failure_tree, start, end, |
| 2302 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 2303 | if (ret >= 0) |
| 2304 | ret = set_state_private(failure_tree, start, |
| 2305 | (u64)(unsigned long)failrec); |
| 2306 | /* set the bits in the inode's tree */ |
| 2307 | if (ret >= 0) |
| 2308 | ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED, |
| 2309 | GFP_NOFS); |
| 2310 | if (ret < 0) { |
| 2311 | kfree(failrec); |
| 2312 | return ret; |
| 2313 | } |
| 2314 | } else { |
| 2315 | failrec = (struct io_failure_record *)(unsigned long)private; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2316 | 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] | 2317 | failrec->logical, failrec->start, failrec->len, |
| 2318 | failrec->in_validation); |
| 2319 | /* |
| 2320 | * when data can be on disk more than twice, add to failrec here |
| 2321 | * (e.g. with a list for failed_mirror) to make |
| 2322 | * clean_io_failure() clean all those errors at once. |
| 2323 | */ |
| 2324 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2325 | |
| 2326 | *failrec_ret = failrec; |
| 2327 | |
| 2328 | return 0; |
| 2329 | } |
| 2330 | |
| 2331 | int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio, |
| 2332 | struct io_failure_record *failrec, int failed_mirror) |
| 2333 | { |
| 2334 | int num_copies; |
| 2335 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 2336 | num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info, |
| 2337 | failrec->logical, failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2338 | if (num_copies == 1) { |
| 2339 | /* |
| 2340 | * we only have a single copy of the data, so don't bother with |
| 2341 | * all the retry and error correction code that follows. no |
| 2342 | * matter what the error is, it is very likely to persist. |
| 2343 | */ |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2344 | 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] | 2345 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2346 | return 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2347 | } |
| 2348 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2349 | /* |
| 2350 | * there are two premises: |
| 2351 | * a) deliver good data to the caller |
| 2352 | * b) correct the bad sectors on disk |
| 2353 | */ |
| 2354 | if (failed_bio->bi_vcnt > 1) { |
| 2355 | /* |
| 2356 | * to fulfill b), we need to know the exact failing sectors, as |
| 2357 | * we don't want to rewrite any more than the failed ones. thus, |
| 2358 | * we need separate read requests for the failed bio |
| 2359 | * |
| 2360 | * if the following BUG_ON triggers, our validation request got |
| 2361 | * merged. we need separate requests for our algorithm to work. |
| 2362 | */ |
| 2363 | BUG_ON(failrec->in_validation); |
| 2364 | failrec->in_validation = 1; |
| 2365 | failrec->this_mirror = failed_mirror; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2366 | } else { |
| 2367 | /* |
| 2368 | * we're ready to fulfill a) and b) alongside. get a good copy |
| 2369 | * of the failed sector and if we succeed, we have setup |
| 2370 | * everything for repair_io_failure to do the rest for us. |
| 2371 | */ |
| 2372 | if (failrec->in_validation) { |
| 2373 | BUG_ON(failrec->this_mirror != failed_mirror); |
| 2374 | failrec->in_validation = 0; |
| 2375 | failrec->this_mirror = 0; |
| 2376 | } |
| 2377 | failrec->failed_mirror = failed_mirror; |
| 2378 | failrec->this_mirror++; |
| 2379 | if (failrec->this_mirror == failed_mirror) |
| 2380 | failrec->this_mirror++; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2381 | } |
| 2382 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2383 | if (failrec->this_mirror > num_copies) { |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2384 | 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] | 2385 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2386 | return 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2387 | } |
| 2388 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2389 | return 1; |
| 2390 | } |
| 2391 | |
| 2392 | |
| 2393 | struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio, |
| 2394 | struct io_failure_record *failrec, |
| 2395 | struct page *page, int pg_offset, int icsum, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2396 | bio_end_io_t *endio_func, void *data) |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2397 | { |
| 2398 | struct bio *bio; |
| 2399 | struct btrfs_io_bio *btrfs_failed_bio; |
| 2400 | struct btrfs_io_bio *btrfs_bio; |
| 2401 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2402 | bio = btrfs_io_bio_alloc(GFP_NOFS, 1); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2403 | if (!bio) |
| 2404 | return NULL; |
| 2405 | |
| 2406 | bio->bi_end_io = endio_func; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2407 | bio->bi_iter.bi_sector = failrec->logical >> 9; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2408 | 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] | 2409 | bio->bi_iter.bi_size = 0; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2410 | bio->bi_private = data; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2411 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2412 | btrfs_failed_bio = btrfs_io_bio(failed_bio); |
| 2413 | if (btrfs_failed_bio->csum) { |
| 2414 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2415 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
| 2416 | |
| 2417 | btrfs_bio = btrfs_io_bio(bio); |
| 2418 | btrfs_bio->csum = btrfs_bio->csum_inline; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2419 | icsum *= csum_size; |
| 2420 | memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum, |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2421 | csum_size); |
| 2422 | } |
| 2423 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2424 | bio_add_page(bio, page, failrec->len, pg_offset); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2425 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2426 | return bio; |
| 2427 | } |
| 2428 | |
| 2429 | /* |
| 2430 | * this is a generic handler for readpage errors (default |
| 2431 | * readpage_io_failed_hook). if other copies exist, read those and write back |
| 2432 | * good data to the failed position. does not investigate in remapping the |
| 2433 | * failed extent elsewhere, hoping the device will be smart enough to do this as |
| 2434 | * needed |
| 2435 | */ |
| 2436 | |
| 2437 | static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset, |
| 2438 | struct page *page, u64 start, u64 end, |
| 2439 | int failed_mirror) |
| 2440 | { |
| 2441 | struct io_failure_record *failrec; |
| 2442 | struct inode *inode = page->mapping->host; |
| 2443 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 2444 | struct bio *bio; |
| 2445 | int read_mode; |
| 2446 | int ret; |
| 2447 | |
| 2448 | BUG_ON(failed_bio->bi_rw & REQ_WRITE); |
| 2449 | |
| 2450 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 2451 | if (ret) |
| 2452 | return ret; |
| 2453 | |
| 2454 | ret = btrfs_check_repairable(inode, failed_bio, failrec, failed_mirror); |
| 2455 | if (!ret) { |
| 2456 | free_io_failure(inode, failrec); |
| 2457 | return -EIO; |
| 2458 | } |
| 2459 | |
| 2460 | if (failed_bio->bi_vcnt > 1) |
| 2461 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; |
| 2462 | else |
| 2463 | read_mode = READ_SYNC; |
| 2464 | |
| 2465 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
| 2466 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
| 2467 | start - page_offset(page), |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2468 | (int)phy_offset, failed_bio->bi_end_io, |
| 2469 | NULL); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2470 | if (!bio) { |
| 2471 | free_io_failure(inode, failrec); |
| 2472 | return -EIO; |
| 2473 | } |
| 2474 | |
| 2475 | pr_debug("Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d\n", |
| 2476 | read_mode, failrec->this_mirror, failrec->in_validation); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2477 | |
Tsutomu Itoh | 013bd4c | 2012-02-16 10:11:40 +0900 | [diff] [blame] | 2478 | ret = tree->ops->submit_bio_hook(inode, read_mode, bio, |
| 2479 | failrec->this_mirror, |
| 2480 | failrec->bio_flags, 0); |
Miao Xie | 6c387ab | 2014-09-12 18:43:57 +0800 | [diff] [blame] | 2481 | if (ret) { |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2482 | free_io_failure(inode, failrec); |
Miao Xie | 6c387ab | 2014-09-12 18:43:57 +0800 | [diff] [blame] | 2483 | bio_put(bio); |
| 2484 | } |
| 2485 | |
Tsutomu Itoh | 013bd4c | 2012-02-16 10:11:40 +0900 | [diff] [blame] | 2486 | return ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2487 | } |
| 2488 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2489 | /* lots and lots of room for performance fixes in the end_bio funcs */ |
| 2490 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2491 | int end_extent_writepage(struct page *page, int err, u64 start, u64 end) |
| 2492 | { |
| 2493 | int uptodate = (err == 0); |
| 2494 | struct extent_io_tree *tree; |
Eric Sandeen | 3e2426b | 2014-06-12 00:39:58 -0500 | [diff] [blame] | 2495 | int ret = 0; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2496 | |
| 2497 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 2498 | |
| 2499 | if (tree->ops && tree->ops->writepage_end_io_hook) { |
| 2500 | ret = tree->ops->writepage_end_io_hook(page, start, |
| 2501 | end, NULL, uptodate); |
| 2502 | if (ret) |
| 2503 | uptodate = 0; |
| 2504 | } |
| 2505 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2506 | if (!uptodate) { |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2507 | ClearPageUptodate(page); |
| 2508 | SetPageError(page); |
Liu Bo | 5dca6ee | 2014-05-12 12:47:36 +0800 | [diff] [blame] | 2509 | ret = ret < 0 ? ret : -EIO; |
| 2510 | mapping_set_error(page->mapping, ret); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2511 | } |
| 2512 | return 0; |
| 2513 | } |
| 2514 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2515 | /* |
| 2516 | * after a writepage IO is done, we need to: |
| 2517 | * clear the uptodate bits on error |
| 2518 | * clear the writeback bits in the extent tree for this IO |
| 2519 | * end_page_writeback if the page has no more pending IO |
| 2520 | * |
| 2521 | * Scheduling is not allowed, so the extent state tree is expected |
| 2522 | * to have one and only one object corresponding to this IO. |
| 2523 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2524 | static void end_bio_extent_writepage(struct bio *bio) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2525 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2526 | struct bio_vec *bvec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2527 | u64 start; |
| 2528 | u64 end; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2529 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2530 | |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2531 | bio_for_each_segment_all(bvec, bio, i) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2532 | struct page *page = bvec->bv_page; |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2533 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2534 | /* We always issue full-page reads, but if some block |
| 2535 | * in a page fails to read, blk_update_request() will |
| 2536 | * advance bv_offset and adjust bv_len to compensate. |
| 2537 | * Print a warning for nonzero offsets, and an error |
| 2538 | * if they don't add up to a full page. */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2539 | if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE) { |
| 2540 | if (bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE) |
| 2541 | btrfs_err(BTRFS_I(page->mapping->host)->root->fs_info, |
| 2542 | "partial page write in btrfs with offset %u and length %u", |
| 2543 | bvec->bv_offset, bvec->bv_len); |
| 2544 | else |
| 2545 | btrfs_info(BTRFS_I(page->mapping->host)->root->fs_info, |
| 2546 | "incomplete page write in btrfs with offset %u and " |
| 2547 | "length %u", |
| 2548 | bvec->bv_offset, bvec->bv_len); |
| 2549 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2550 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2551 | start = page_offset(page); |
| 2552 | end = start + bvec->bv_offset + bvec->bv_len - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2553 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2554 | if (end_extent_writepage(page, bio->bi_error, start, end)) |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2555 | continue; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2556 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2557 | end_page_writeback(page); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2558 | } |
Chris Mason | 2b1f55b | 2008-09-24 11:48:04 -0400 | [diff] [blame] | 2559 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2560 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2561 | } |
| 2562 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2563 | static void |
| 2564 | endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len, |
| 2565 | int uptodate) |
| 2566 | { |
| 2567 | struct extent_state *cached = NULL; |
| 2568 | u64 end = start + len - 1; |
| 2569 | |
| 2570 | if (uptodate && tree->track_uptodate) |
| 2571 | set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC); |
| 2572 | unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC); |
| 2573 | } |
| 2574 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2575 | /* |
| 2576 | * after a readpage IO is done, we need to: |
| 2577 | * clear the uptodate bits on error |
| 2578 | * set the uptodate bits if things worked |
| 2579 | * set the page up to date if all extents in the tree are uptodate |
| 2580 | * clear the lock bit in the extent tree |
| 2581 | * unlock the page if there are no other extents locked for it |
| 2582 | * |
| 2583 | * Scheduling is not allowed, so the extent state tree is expected |
| 2584 | * to have one and only one object corresponding to this IO. |
| 2585 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2586 | static void end_bio_extent_readpage(struct bio *bio) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2587 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2588 | struct bio_vec *bvec; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2589 | int uptodate = !bio->bi_error; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2590 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2591 | struct extent_io_tree *tree; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2592 | u64 offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2593 | u64 start; |
| 2594 | u64 end; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2595 | u64 len; |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2596 | u64 extent_start = 0; |
| 2597 | u64 extent_len = 0; |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2598 | int mirror; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2599 | int ret; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2600 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2601 | |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2602 | bio_for_each_segment_all(bvec, bio, i) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2603 | struct page *page = bvec->bv_page; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2604 | struct inode *inode = page->mapping->host; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2605 | |
Kent Overstreet | be3940c | 2012-09-11 14:23:05 -0600 | [diff] [blame] | 2606 | pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, " |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2607 | "mirror=%u\n", (u64)bio->bi_iter.bi_sector, |
| 2608 | bio->bi_error, io_bio->mirror_num); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2609 | tree = &BTRFS_I(inode)->io_tree; |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2610 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2611 | /* We always issue full-page reads, but if some block |
| 2612 | * in a page fails to read, blk_update_request() will |
| 2613 | * advance bv_offset and adjust bv_len to compensate. |
| 2614 | * Print a warning for nonzero offsets, and an error |
| 2615 | * if they don't add up to a full page. */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2616 | if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE) { |
| 2617 | if (bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE) |
| 2618 | btrfs_err(BTRFS_I(page->mapping->host)->root->fs_info, |
| 2619 | "partial page read in btrfs with offset %u and length %u", |
| 2620 | bvec->bv_offset, bvec->bv_len); |
| 2621 | else |
| 2622 | btrfs_info(BTRFS_I(page->mapping->host)->root->fs_info, |
| 2623 | "incomplete page read in btrfs with offset %u and " |
| 2624 | "length %u", |
| 2625 | bvec->bv_offset, bvec->bv_len); |
| 2626 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2627 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2628 | start = page_offset(page); |
| 2629 | end = start + bvec->bv_offset + bvec->bv_len - 1; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2630 | len = bvec->bv_len; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2631 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2632 | mirror = io_bio->mirror_num; |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2633 | if (likely(uptodate && tree->ops && |
| 2634 | tree->ops->readpage_end_io_hook)) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2635 | ret = tree->ops->readpage_end_io_hook(io_bio, offset, |
| 2636 | page, start, end, |
| 2637 | mirror); |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2638 | if (ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2639 | uptodate = 0; |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2640 | else |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2641 | clean_io_failure(inode, start, page, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2642 | } |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2643 | |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2644 | if (likely(uptodate)) |
| 2645 | goto readpage_ok; |
| 2646 | |
| 2647 | if (tree->ops && tree->ops->readpage_io_failed_hook) { |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2648 | ret = tree->ops->readpage_io_failed_hook(page, mirror); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2649 | if (!ret && !bio->bi_error) |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2650 | uptodate = 1; |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2651 | } else { |
Jan Schmidt | f4a8e65 | 2011-12-01 09:30:36 -0500 | [diff] [blame] | 2652 | /* |
| 2653 | * The generic bio_readpage_error handles errors the |
| 2654 | * following way: If possible, new read requests are |
| 2655 | * created and submitted and will end up in |
| 2656 | * end_bio_extent_readpage as well (if we're lucky, not |
| 2657 | * in the !uptodate case). In that case it returns 0 and |
| 2658 | * we just go on with the next page in our bio. If it |
| 2659 | * can't handle the error it will return -EIO and we |
| 2660 | * remain responsible for that page. |
| 2661 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2662 | ret = bio_readpage_error(bio, offset, page, start, end, |
| 2663 | mirror); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2664 | if (ret == 0) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2665 | uptodate = !bio->bi_error; |
Liu Bo | 38c1c2e | 2014-08-19 23:33:13 +0800 | [diff] [blame] | 2666 | offset += len; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2667 | continue; |
| 2668 | } |
| 2669 | } |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2670 | readpage_ok: |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2671 | if (likely(uptodate)) { |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2672 | loff_t i_size = i_size_read(inode); |
| 2673 | pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; |
Liu Bo | a583c02 | 2014-08-19 23:32:22 +0800 | [diff] [blame] | 2674 | unsigned off; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2675 | |
| 2676 | /* Zero out the end if this page straddles i_size */ |
Liu Bo | a583c02 | 2014-08-19 23:32:22 +0800 | [diff] [blame] | 2677 | off = i_size & (PAGE_CACHE_SIZE-1); |
| 2678 | if (page->index == end_index && off) |
| 2679 | zero_user_segment(page, off, PAGE_CACHE_SIZE); |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2680 | SetPageUptodate(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2681 | } else { |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2682 | ClearPageUptodate(page); |
| 2683 | SetPageError(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2684 | } |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2685 | unlock_page(page); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2686 | offset += len; |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2687 | |
| 2688 | if (unlikely(!uptodate)) { |
| 2689 | if (extent_len) { |
| 2690 | endio_readpage_release_extent(tree, |
| 2691 | extent_start, |
| 2692 | extent_len, 1); |
| 2693 | extent_start = 0; |
| 2694 | extent_len = 0; |
| 2695 | } |
| 2696 | endio_readpage_release_extent(tree, start, |
| 2697 | end - start + 1, 0); |
| 2698 | } else if (!extent_len) { |
| 2699 | extent_start = start; |
| 2700 | extent_len = end + 1 - start; |
| 2701 | } else if (extent_start + extent_len == start) { |
| 2702 | extent_len += end + 1 - start; |
| 2703 | } else { |
| 2704 | endio_readpage_release_extent(tree, extent_start, |
| 2705 | extent_len, uptodate); |
| 2706 | extent_start = start; |
| 2707 | extent_len = end + 1 - start; |
| 2708 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2709 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2710 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2711 | if (extent_len) |
| 2712 | endio_readpage_release_extent(tree, extent_start, extent_len, |
| 2713 | uptodate); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2714 | if (io_bio->end_io) |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2715 | io_bio->end_io(io_bio, bio->bi_error); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2716 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2717 | } |
| 2718 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2719 | /* |
| 2720 | * this allocates from the btrfs_bioset. We're returning a bio right now |
| 2721 | * but you can call btrfs_io_bio for the appropriate container_of magic |
| 2722 | */ |
Miao Xie | 88f794e | 2010-11-22 03:02:55 +0000 | [diff] [blame] | 2723 | struct bio * |
| 2724 | btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs, |
| 2725 | gfp_t gfp_flags) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2726 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2727 | struct btrfs_io_bio *btrfs_bio; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2728 | struct bio *bio; |
| 2729 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2730 | bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2731 | |
| 2732 | if (bio == NULL && (current->flags & PF_MEMALLOC)) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2733 | while (!bio && (nr_vecs /= 2)) { |
| 2734 | bio = bio_alloc_bioset(gfp_flags, |
| 2735 | nr_vecs, btrfs_bioset); |
| 2736 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2737 | } |
| 2738 | |
| 2739 | if (bio) { |
| 2740 | bio->bi_bdev = bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2741 | bio->bi_iter.bi_sector = first_sector; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2742 | btrfs_bio = btrfs_io_bio(bio); |
| 2743 | btrfs_bio->csum = NULL; |
| 2744 | btrfs_bio->csum_allocated = NULL; |
| 2745 | btrfs_bio->end_io = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2746 | } |
| 2747 | return bio; |
| 2748 | } |
| 2749 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2750 | struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask) |
| 2751 | { |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 2752 | struct btrfs_io_bio *btrfs_bio; |
| 2753 | struct bio *new; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2754 | |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 2755 | new = bio_clone_bioset(bio, gfp_mask, btrfs_bioset); |
| 2756 | if (new) { |
| 2757 | btrfs_bio = btrfs_io_bio(new); |
| 2758 | btrfs_bio->csum = NULL; |
| 2759 | btrfs_bio->csum_allocated = NULL; |
| 2760 | btrfs_bio->end_io = NULL; |
Chris Mason | 3a9508b | 2015-08-21 10:05:39 -0700 | [diff] [blame] | 2761 | |
| 2762 | #ifdef CONFIG_BLK_CGROUP |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2763 | /* FIXME, put this into bio_clone_bioset */ |
| 2764 | if (bio->bi_css) |
| 2765 | bio_associate_blkcg(new, bio->bi_css); |
Chris Mason | 3a9508b | 2015-08-21 10:05:39 -0700 | [diff] [blame] | 2766 | #endif |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 2767 | } |
| 2768 | return new; |
| 2769 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2770 | |
| 2771 | /* this also allocates from the btrfs_bioset */ |
| 2772 | struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs) |
| 2773 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2774 | struct btrfs_io_bio *btrfs_bio; |
| 2775 | struct bio *bio; |
| 2776 | |
| 2777 | bio = bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset); |
| 2778 | if (bio) { |
| 2779 | btrfs_bio = btrfs_io_bio(bio); |
| 2780 | btrfs_bio->csum = NULL; |
| 2781 | btrfs_bio->csum_allocated = NULL; |
| 2782 | btrfs_bio->end_io = NULL; |
| 2783 | } |
| 2784 | return bio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2785 | } |
| 2786 | |
| 2787 | |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 2788 | static int __must_check submit_one_bio(int rw, struct bio *bio, |
| 2789 | int mirror_num, unsigned long bio_flags) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2790 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2791 | int ret = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2792 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 2793 | struct page *page = bvec->bv_page; |
| 2794 | struct extent_io_tree *tree = bio->bi_private; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2795 | u64 start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2796 | |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2797 | start = page_offset(page) + bvec->bv_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2798 | |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2799 | bio->bi_private = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2800 | |
| 2801 | bio_get(bio); |
| 2802 | |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2803 | if (tree->ops && tree->ops->submit_bio_hook) |
liubo | 6b82ce8 | 2011-01-26 06:21:39 +0000 | [diff] [blame] | 2804 | ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 2805 | mirror_num, bio_flags, start); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2806 | else |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 2807 | btrfsic_submit_bio(rw, bio); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2808 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2809 | bio_put(bio); |
| 2810 | return ret; |
| 2811 | } |
| 2812 | |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 2813 | 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] | 2814 | unsigned long offset, size_t size, struct bio *bio, |
| 2815 | unsigned long bio_flags) |
| 2816 | { |
| 2817 | int ret = 0; |
| 2818 | if (tree->ops && tree->ops->merge_bio_hook) |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 2819 | ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio, |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 2820 | bio_flags); |
| 2821 | BUG_ON(ret < 0); |
| 2822 | return ret; |
| 2823 | |
| 2824 | } |
| 2825 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2826 | static int submit_extent_page(int rw, struct extent_io_tree *tree, |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2827 | struct writeback_control *wbc, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2828 | struct page *page, sector_t sector, |
| 2829 | size_t size, unsigned long offset, |
| 2830 | struct block_device *bdev, |
| 2831 | struct bio **bio_ret, |
| 2832 | unsigned long max_pages, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2833 | bio_end_io_t end_io_func, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2834 | int mirror_num, |
| 2835 | unsigned long prev_bio_flags, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2836 | unsigned long bio_flags, |
| 2837 | bool force_bio_submit) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2838 | { |
| 2839 | int ret = 0; |
| 2840 | struct bio *bio; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2841 | int contig = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2842 | int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 2843 | size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2844 | |
| 2845 | if (bio_ret && *bio_ret) { |
| 2846 | bio = *bio_ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2847 | if (old_compressed) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2848 | contig = bio->bi_iter.bi_sector == sector; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2849 | else |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 2850 | contig = bio_end_sector(bio) == sector; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2851 | |
| 2852 | if (prev_bio_flags != bio_flags || !contig || |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2853 | force_bio_submit || |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 2854 | merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2855 | bio_add_page(bio, page, page_size, offset) < page_size) { |
| 2856 | ret = submit_one_bio(rw, bio, mirror_num, |
| 2857 | prev_bio_flags); |
Naohiro Aota | 289454a | 2015-01-06 01:01:03 +0900 | [diff] [blame] | 2858 | if (ret < 0) { |
| 2859 | *bio_ret = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2860 | return ret; |
Naohiro Aota | 289454a | 2015-01-06 01:01:03 +0900 | [diff] [blame] | 2861 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2862 | bio = NULL; |
| 2863 | } else { |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2864 | if (wbc) |
| 2865 | wbc_account_io(wbc, page, page_size); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2866 | return 0; |
| 2867 | } |
| 2868 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2869 | |
Kent Overstreet | b54ffb7 | 2015-05-19 14:31:01 +0200 | [diff] [blame] | 2870 | bio = btrfs_bio_alloc(bdev, sector, BIO_MAX_PAGES, |
| 2871 | GFP_NOFS | __GFP_HIGH); |
Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 2872 | if (!bio) |
| 2873 | return -ENOMEM; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2874 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2875 | bio_add_page(bio, page, page_size, offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2876 | bio->bi_end_io = end_io_func; |
| 2877 | bio->bi_private = tree; |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2878 | if (wbc) { |
| 2879 | wbc_init_bio(wbc, bio); |
| 2880 | wbc_account_io(wbc, page, page_size); |
| 2881 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2882 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2883 | if (bio_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2884 | *bio_ret = bio; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2885 | else |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2886 | ret = submit_one_bio(rw, bio, mirror_num, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2887 | |
| 2888 | return ret; |
| 2889 | } |
| 2890 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 2891 | static void attach_extent_buffer_page(struct extent_buffer *eb, |
| 2892 | struct page *page) |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 2893 | { |
| 2894 | if (!PagePrivate(page)) { |
| 2895 | SetPagePrivate(page); |
| 2896 | page_cache_get(page); |
| 2897 | set_page_private(page, (unsigned long)eb); |
| 2898 | } else { |
| 2899 | WARN_ON(page->private != (unsigned long)eb); |
| 2900 | } |
| 2901 | } |
| 2902 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2903 | void set_page_extent_mapped(struct page *page) |
| 2904 | { |
| 2905 | if (!PagePrivate(page)) { |
| 2906 | SetPagePrivate(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2907 | page_cache_get(page); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2908 | set_page_private(page, EXTENT_PAGE_PRIVATE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2909 | } |
| 2910 | } |
| 2911 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2912 | static struct extent_map * |
| 2913 | __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset, |
| 2914 | u64 start, u64 len, get_extent_t *get_extent, |
| 2915 | struct extent_map **em_cached) |
| 2916 | { |
| 2917 | struct extent_map *em; |
| 2918 | |
| 2919 | if (em_cached && *em_cached) { |
| 2920 | em = *em_cached; |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 2921 | if (extent_map_in_tree(em) && start >= em->start && |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2922 | start < extent_map_end(em)) { |
| 2923 | atomic_inc(&em->refs); |
| 2924 | return em; |
| 2925 | } |
| 2926 | |
| 2927 | free_extent_map(em); |
| 2928 | *em_cached = NULL; |
| 2929 | } |
| 2930 | |
| 2931 | em = get_extent(inode, page, pg_offset, start, len, 0); |
| 2932 | if (em_cached && !IS_ERR_OR_NULL(em)) { |
| 2933 | BUG_ON(*em_cached); |
| 2934 | atomic_inc(&em->refs); |
| 2935 | *em_cached = em; |
| 2936 | } |
| 2937 | return em; |
| 2938 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2939 | /* |
| 2940 | * basic readpage implementation. Locked extent state structs are inserted |
| 2941 | * into the tree that are removed when the IO is done (by the end_io |
| 2942 | * handlers) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2943 | * XXX JDM: This needs looking at to ensure proper page locking |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2944 | */ |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2945 | static int __do_readpage(struct extent_io_tree *tree, |
| 2946 | struct page *page, |
| 2947 | get_extent_t *get_extent, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2948 | struct extent_map **em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2949 | struct bio **bio, int mirror_num, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2950 | unsigned long *bio_flags, int rw, |
| 2951 | u64 *prev_em_start) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2952 | { |
| 2953 | struct inode *inode = page->mapping->host; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2954 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2955 | u64 page_end = start + PAGE_CACHE_SIZE - 1; |
| 2956 | u64 end; |
| 2957 | u64 cur = start; |
| 2958 | u64 extent_offset; |
| 2959 | u64 last_byte = i_size_read(inode); |
| 2960 | u64 block_start; |
| 2961 | u64 cur_end; |
| 2962 | sector_t sector; |
| 2963 | struct extent_map *em; |
| 2964 | struct block_device *bdev; |
| 2965 | int ret; |
| 2966 | int nr = 0; |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 2967 | int parent_locked = *bio_flags & EXTENT_BIO_PARENT_LOCKED; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2968 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2969 | size_t iosize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2970 | size_t disk_io_size; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2971 | size_t blocksize = inode->i_sb->s_blocksize; |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 2972 | unsigned long this_bio_flag = *bio_flags & EXTENT_BIO_PARENT_LOCKED; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2973 | |
| 2974 | set_page_extent_mapped(page); |
| 2975 | |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2976 | end = page_end; |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 2977 | if (!PageUptodate(page)) { |
| 2978 | if (cleancache_get_page(page) == 0) { |
| 2979 | BUG_ON(blocksize != PAGE_SIZE); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2980 | unlock_extent(tree, start, end); |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 2981 | goto out; |
| 2982 | } |
| 2983 | } |
| 2984 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2985 | if (page->index == last_byte >> PAGE_CACHE_SHIFT) { |
| 2986 | char *userpage; |
| 2987 | size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1); |
| 2988 | |
| 2989 | if (zero_offset) { |
| 2990 | iosize = PAGE_CACHE_SIZE - zero_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2991 | userpage = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2992 | memset(userpage + zero_offset, 0, iosize); |
| 2993 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2994 | kunmap_atomic(userpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2995 | } |
| 2996 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2997 | while (cur <= end) { |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 2998 | unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1; |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2999 | bool force_bio_submit = false; |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3000 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3001 | if (cur >= last_byte) { |
| 3002 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3003 | struct extent_state *cached = NULL; |
| 3004 | |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3005 | iosize = PAGE_CACHE_SIZE - pg_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3006 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3007 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3008 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3009 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3010 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3011 | &cached, GFP_NOFS); |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 3012 | if (!parent_locked) |
| 3013 | unlock_extent_cached(tree, cur, |
| 3014 | cur + iosize - 1, |
| 3015 | &cached, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3016 | break; |
| 3017 | } |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3018 | em = __get_extent_map(inode, page, pg_offset, cur, |
| 3019 | end - cur + 1, get_extent, em_cached); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3020 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3021 | SetPageError(page); |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 3022 | if (!parent_locked) |
| 3023 | unlock_extent(tree, cur, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3024 | break; |
| 3025 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3026 | extent_offset = cur - em->start; |
| 3027 | BUG_ON(extent_map_end(em) <= cur); |
| 3028 | BUG_ON(end < cur); |
| 3029 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3030 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 3031 | this_bio_flag |= EXTENT_BIO_COMPRESSED; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3032 | extent_set_compress_type(&this_bio_flag, |
| 3033 | em->compress_type); |
| 3034 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3035 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3036 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
| 3037 | cur_end = min(extent_map_end(em) - 1, end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3038 | iosize = ALIGN(iosize, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3039 | if (this_bio_flag & EXTENT_BIO_COMPRESSED) { |
| 3040 | disk_io_size = em->block_len; |
| 3041 | sector = em->block_start >> 9; |
| 3042 | } else { |
| 3043 | sector = (em->block_start + extent_offset) >> 9; |
| 3044 | disk_io_size = iosize; |
| 3045 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3046 | bdev = em->bdev; |
| 3047 | block_start = em->block_start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3048 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 3049 | block_start = EXTENT_MAP_HOLE; |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3050 | |
| 3051 | /* |
| 3052 | * If we have a file range that points to a compressed extent |
| 3053 | * and it's followed by a consecutive file range that points to |
| 3054 | * to the same compressed extent (possibly with a different |
| 3055 | * offset and/or length, so it either points to the whole extent |
| 3056 | * or only part of it), we must make sure we do not submit a |
| 3057 | * single bio to populate the pages for the 2 ranges because |
| 3058 | * this makes the compressed extent read zero out the pages |
| 3059 | * belonging to the 2nd range. Imagine the following scenario: |
| 3060 | * |
| 3061 | * File layout |
| 3062 | * [0 - 8K] [8K - 24K] |
| 3063 | * | | |
| 3064 | * | | |
| 3065 | * points to extent X, points to extent X, |
| 3066 | * offset 4K, length of 8K offset 0, length 16K |
| 3067 | * |
| 3068 | * [extent X, compressed length = 4K uncompressed length = 16K] |
| 3069 | * |
| 3070 | * If the bio to read the compressed extent covers both ranges, |
| 3071 | * it will decompress extent X into the pages belonging to the |
| 3072 | * first range and then it will stop, zeroing out the remaining |
| 3073 | * pages that belong to the other range that points to extent X. |
| 3074 | * So here we make sure we submit 2 bios, one for the first |
| 3075 | * range and another one for the third range. Both will target |
| 3076 | * the same physical extent from disk, but we can't currently |
| 3077 | * make the compressed bio endio callback populate the pages |
| 3078 | * for both ranges because each compressed bio is tightly |
| 3079 | * coupled with a single extent map, and each range can have |
| 3080 | * an extent map with a different offset value relative to the |
| 3081 | * uncompressed data of our extent and different lengths. This |
| 3082 | * is a corner case so we prioritize correctness over |
| 3083 | * non-optimal behavior (submitting 2 bios for the same extent). |
| 3084 | */ |
| 3085 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) && |
| 3086 | prev_em_start && *prev_em_start != (u64)-1 && |
| 3087 | *prev_em_start != em->orig_start) |
| 3088 | force_bio_submit = true; |
| 3089 | |
| 3090 | if (prev_em_start) |
| 3091 | *prev_em_start = em->orig_start; |
| 3092 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3093 | free_extent_map(em); |
| 3094 | em = NULL; |
| 3095 | |
| 3096 | /* we've found a hole, just zero and go on */ |
| 3097 | if (block_start == EXTENT_MAP_HOLE) { |
| 3098 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3099 | struct extent_state *cached = NULL; |
| 3100 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3101 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3102 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3103 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3104 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3105 | |
| 3106 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3107 | &cached, GFP_NOFS); |
Filipe Manana | 5e6ecb3 | 2015-10-13 16:36:09 +0100 | [diff] [blame] | 3108 | if (parent_locked) |
| 3109 | free_extent_state(cached); |
| 3110 | else |
| 3111 | unlock_extent_cached(tree, cur, |
| 3112 | cur + iosize - 1, |
| 3113 | &cached, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3114 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3115 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3116 | continue; |
| 3117 | } |
| 3118 | /* the get_extent function already copied into the page */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3119 | if (test_range_bit(tree, cur, cur_end, |
| 3120 | EXTENT_UPTODATE, 1, NULL)) { |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 3121 | check_page_uptodate(tree, page); |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 3122 | if (!parent_locked) |
| 3123 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3124 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3125 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3126 | continue; |
| 3127 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3128 | /* we have an inline extent but it didn't get marked up |
| 3129 | * to date. Error out |
| 3130 | */ |
| 3131 | if (block_start == EXTENT_MAP_INLINE) { |
| 3132 | SetPageError(page); |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 3133 | if (!parent_locked) |
| 3134 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3135 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3136 | pg_offset += iosize; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3137 | continue; |
| 3138 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3139 | |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3140 | pnr -= page->index; |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 3141 | ret = submit_extent_page(rw, tree, NULL, page, |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3142 | sector, disk_io_size, pg_offset, |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3143 | bdev, bio, pnr, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3144 | end_bio_extent_readpage, mirror_num, |
| 3145 | *bio_flags, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3146 | this_bio_flag, |
| 3147 | force_bio_submit); |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3148 | if (!ret) { |
| 3149 | nr++; |
| 3150 | *bio_flags = this_bio_flag; |
| 3151 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3152 | SetPageError(page); |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 3153 | if (!parent_locked) |
| 3154 | unlock_extent(tree, cur, cur + iosize - 1); |
Josef Bacik | edd33c9 | 2012-10-05 16:40:32 -0400 | [diff] [blame] | 3155 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3156 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3157 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3158 | } |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 3159 | out: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3160 | if (!nr) { |
| 3161 | if (!PageError(page)) |
| 3162 | SetPageUptodate(page); |
| 3163 | unlock_page(page); |
| 3164 | } |
| 3165 | return 0; |
| 3166 | } |
| 3167 | |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3168 | static inline void __do_contiguous_readpages(struct extent_io_tree *tree, |
| 3169 | struct page *pages[], int nr_pages, |
| 3170 | u64 start, u64 end, |
| 3171 | get_extent_t *get_extent, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3172 | struct extent_map **em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3173 | struct bio **bio, int mirror_num, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3174 | unsigned long *bio_flags, int rw, |
| 3175 | u64 *prev_em_start) |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3176 | { |
| 3177 | struct inode *inode; |
| 3178 | struct btrfs_ordered_extent *ordered; |
| 3179 | int index; |
| 3180 | |
| 3181 | inode = pages[0]->mapping->host; |
| 3182 | while (1) { |
| 3183 | lock_extent(tree, start, end); |
| 3184 | ordered = btrfs_lookup_ordered_range(inode, start, |
| 3185 | end - start + 1); |
| 3186 | if (!ordered) |
| 3187 | break; |
| 3188 | unlock_extent(tree, start, end); |
| 3189 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3190 | btrfs_put_ordered_extent(ordered); |
| 3191 | } |
| 3192 | |
| 3193 | for (index = 0; index < nr_pages; index++) { |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3194 | __do_readpage(tree, pages[index], get_extent, em_cached, bio, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3195 | mirror_num, bio_flags, rw, prev_em_start); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3196 | page_cache_release(pages[index]); |
| 3197 | } |
| 3198 | } |
| 3199 | |
| 3200 | static void __extent_readpages(struct extent_io_tree *tree, |
| 3201 | struct page *pages[], |
| 3202 | int nr_pages, get_extent_t *get_extent, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3203 | struct extent_map **em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3204 | struct bio **bio, int mirror_num, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3205 | unsigned long *bio_flags, int rw, |
| 3206 | u64 *prev_em_start) |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3207 | { |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 3208 | u64 start = 0; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3209 | u64 end = 0; |
| 3210 | u64 page_start; |
| 3211 | int index; |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 3212 | int first_index = 0; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3213 | |
| 3214 | for (index = 0; index < nr_pages; index++) { |
| 3215 | page_start = page_offset(pages[index]); |
| 3216 | if (!end) { |
| 3217 | start = page_start; |
| 3218 | end = start + PAGE_CACHE_SIZE - 1; |
| 3219 | first_index = index; |
| 3220 | } else if (end + 1 == page_start) { |
| 3221 | end += PAGE_CACHE_SIZE; |
| 3222 | } else { |
| 3223 | __do_contiguous_readpages(tree, &pages[first_index], |
| 3224 | index - first_index, start, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3225 | end, get_extent, em_cached, |
| 3226 | bio, mirror_num, bio_flags, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3227 | rw, prev_em_start); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3228 | start = page_start; |
| 3229 | end = start + PAGE_CACHE_SIZE - 1; |
| 3230 | first_index = index; |
| 3231 | } |
| 3232 | } |
| 3233 | |
| 3234 | if (end) |
| 3235 | __do_contiguous_readpages(tree, &pages[first_index], |
| 3236 | index - first_index, start, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3237 | end, get_extent, em_cached, bio, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3238 | mirror_num, bio_flags, rw, |
| 3239 | prev_em_start); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3240 | } |
| 3241 | |
| 3242 | static int __extent_read_full_page(struct extent_io_tree *tree, |
| 3243 | struct page *page, |
| 3244 | get_extent_t *get_extent, |
| 3245 | struct bio **bio, int mirror_num, |
| 3246 | unsigned long *bio_flags, int rw) |
| 3247 | { |
| 3248 | struct inode *inode = page->mapping->host; |
| 3249 | struct btrfs_ordered_extent *ordered; |
| 3250 | u64 start = page_offset(page); |
| 3251 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 3252 | int ret; |
| 3253 | |
| 3254 | while (1) { |
| 3255 | lock_extent(tree, start, end); |
| 3256 | ordered = btrfs_lookup_ordered_extent(inode, start); |
| 3257 | if (!ordered) |
| 3258 | break; |
| 3259 | unlock_extent(tree, start, end); |
| 3260 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3261 | btrfs_put_ordered_extent(ordered); |
| 3262 | } |
| 3263 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3264 | ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3265 | bio_flags, rw, NULL); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3266 | return ret; |
| 3267 | } |
| 3268 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3269 | 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] | 3270 | get_extent_t *get_extent, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3271 | { |
| 3272 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3273 | unsigned long bio_flags = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3274 | int ret; |
| 3275 | |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 3276 | ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num, |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 3277 | &bio_flags, READ); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3278 | if (bio) |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 3279 | ret = submit_one_bio(READ, bio, mirror_num, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3280 | return ret; |
| 3281 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3282 | |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 3283 | int extent_read_full_page_nolock(struct extent_io_tree *tree, struct page *page, |
| 3284 | get_extent_t *get_extent, int mirror_num) |
| 3285 | { |
| 3286 | struct bio *bio = NULL; |
| 3287 | unsigned long bio_flags = EXTENT_BIO_PARENT_LOCKED; |
| 3288 | int ret; |
| 3289 | |
| 3290 | ret = __do_readpage(tree, page, get_extent, NULL, &bio, mirror_num, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3291 | &bio_flags, READ, NULL); |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 3292 | if (bio) |
| 3293 | ret = submit_one_bio(READ, bio, mirror_num, bio_flags); |
| 3294 | return ret; |
| 3295 | } |
| 3296 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3297 | static noinline void update_nr_written(struct page *page, |
| 3298 | struct writeback_control *wbc, |
| 3299 | unsigned long nr_written) |
| 3300 | { |
| 3301 | wbc->nr_to_write -= nr_written; |
| 3302 | if (wbc->range_cyclic || (wbc->nr_to_write > 0 && |
| 3303 | wbc->range_start == 0 && wbc->range_end == LLONG_MAX)) |
| 3304 | page->mapping->writeback_index = page->index + nr_written; |
| 3305 | } |
| 3306 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3307 | /* |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3308 | * helper for __extent_writepage, doing all of the delayed allocation setup. |
| 3309 | * |
| 3310 | * This returns 1 if our fill_delalloc function did all the work required |
| 3311 | * to write the page (copy into inline extent). In this case the IO has |
| 3312 | * been started and the page is already unlocked. |
| 3313 | * |
| 3314 | * This returns 0 if all went well (page still locked) |
| 3315 | * This returns < 0 if there were errors (page still locked) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3316 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3317 | static noinline_for_stack int writepage_delalloc(struct inode *inode, |
| 3318 | struct page *page, struct writeback_control *wbc, |
| 3319 | struct extent_page_data *epd, |
| 3320 | u64 delalloc_start, |
| 3321 | unsigned long *nr_written) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3322 | { |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3323 | struct extent_io_tree *tree = epd->tree; |
| 3324 | u64 page_end = delalloc_start + PAGE_CACHE_SIZE - 1; |
| 3325 | u64 nr_delalloc; |
| 3326 | u64 delalloc_to_write = 0; |
| 3327 | u64 delalloc_end = 0; |
| 3328 | int ret; |
| 3329 | int page_started = 0; |
| 3330 | |
| 3331 | if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc) |
| 3332 | return 0; |
| 3333 | |
| 3334 | while (delalloc_end < page_end) { |
| 3335 | nr_delalloc = find_lock_delalloc_range(inode, tree, |
| 3336 | page, |
| 3337 | &delalloc_start, |
| 3338 | &delalloc_end, |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 3339 | BTRFS_MAX_EXTENT_SIZE); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3340 | if (nr_delalloc == 0) { |
| 3341 | delalloc_start = delalloc_end + 1; |
| 3342 | continue; |
| 3343 | } |
| 3344 | ret = tree->ops->fill_delalloc(inode, page, |
| 3345 | delalloc_start, |
| 3346 | delalloc_end, |
| 3347 | &page_started, |
| 3348 | nr_written); |
| 3349 | /* File system has been set read-only */ |
| 3350 | if (ret) { |
| 3351 | SetPageError(page); |
| 3352 | /* fill_delalloc should be return < 0 for error |
| 3353 | * but just in case, we use > 0 here meaning the |
| 3354 | * IO is started, so we don't want to return > 0 |
| 3355 | * unless things are going well. |
| 3356 | */ |
| 3357 | ret = ret < 0 ? ret : -EIO; |
| 3358 | goto done; |
| 3359 | } |
| 3360 | /* |
| 3361 | * delalloc_end is already one less than the total |
| 3362 | * length, so we don't subtract one from |
| 3363 | * PAGE_CACHE_SIZE |
| 3364 | */ |
| 3365 | delalloc_to_write += (delalloc_end - delalloc_start + |
| 3366 | PAGE_CACHE_SIZE) >> |
| 3367 | PAGE_CACHE_SHIFT; |
| 3368 | delalloc_start = delalloc_end + 1; |
| 3369 | } |
| 3370 | if (wbc->nr_to_write < delalloc_to_write) { |
| 3371 | int thresh = 8192; |
| 3372 | |
| 3373 | if (delalloc_to_write < thresh * 2) |
| 3374 | thresh = delalloc_to_write; |
| 3375 | wbc->nr_to_write = min_t(u64, delalloc_to_write, |
| 3376 | thresh); |
| 3377 | } |
| 3378 | |
| 3379 | /* did the fill delalloc function already unlock and start |
| 3380 | * the IO? |
| 3381 | */ |
| 3382 | if (page_started) { |
| 3383 | /* |
| 3384 | * we've unlocked the page, so we can't update |
| 3385 | * the mapping's writeback index, just update |
| 3386 | * nr_to_write. |
| 3387 | */ |
| 3388 | wbc->nr_to_write -= *nr_written; |
| 3389 | return 1; |
| 3390 | } |
| 3391 | |
| 3392 | ret = 0; |
| 3393 | |
| 3394 | done: |
| 3395 | return ret; |
| 3396 | } |
| 3397 | |
| 3398 | /* |
| 3399 | * helper for __extent_writepage. This calls the writepage start hooks, |
| 3400 | * and does the loop to map the page into extents and bios. |
| 3401 | * |
| 3402 | * We return 1 if the IO is started and the page is unlocked, |
| 3403 | * 0 if all went well (page still locked) |
| 3404 | * < 0 if there were errors (page still locked) |
| 3405 | */ |
| 3406 | static noinline_for_stack int __extent_writepage_io(struct inode *inode, |
| 3407 | struct page *page, |
| 3408 | struct writeback_control *wbc, |
| 3409 | struct extent_page_data *epd, |
| 3410 | loff_t i_size, |
| 3411 | unsigned long nr_written, |
| 3412 | int write_flags, int *nr_ret) |
| 3413 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3414 | struct extent_io_tree *tree = epd->tree; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3415 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3416 | u64 page_end = start + PAGE_CACHE_SIZE - 1; |
| 3417 | u64 end; |
| 3418 | u64 cur = start; |
| 3419 | u64 extent_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3420 | u64 block_start; |
| 3421 | u64 iosize; |
| 3422 | sector_t sector; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 3423 | struct extent_state *cached_state = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3424 | struct extent_map *em; |
| 3425 | struct block_device *bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3426 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3427 | size_t blocksize; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3428 | int ret = 0; |
| 3429 | int nr = 0; |
| 3430 | bool compressed; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3431 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3432 | if (tree->ops && tree->ops->writepage_start_hook) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3433 | ret = tree->ops->writepage_start_hook(page, start, |
| 3434 | page_end); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 3435 | if (ret) { |
| 3436 | /* Fixup worker will requeue */ |
| 3437 | if (ret == -EBUSY) |
| 3438 | wbc->pages_skipped++; |
| 3439 | else |
| 3440 | redirty_page_for_writepage(wbc, page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3441 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3442 | update_nr_written(page, wbc, nr_written); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3443 | unlock_page(page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3444 | ret = 1; |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3445 | goto done_unlocked; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3446 | } |
| 3447 | } |
| 3448 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3449 | /* |
| 3450 | * we don't want to touch the inode after unlocking the page, |
| 3451 | * so we update the mapping writeback index now |
| 3452 | */ |
| 3453 | update_nr_written(page, wbc, nr_written + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3454 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3455 | end = page_end; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3456 | if (i_size <= start) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3457 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 3458 | tree->ops->writepage_end_io_hook(page, start, |
| 3459 | page_end, NULL, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3460 | goto done; |
| 3461 | } |
| 3462 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3463 | blocksize = inode->i_sb->s_blocksize; |
| 3464 | |
| 3465 | while (cur <= end) { |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3466 | u64 em_end; |
| 3467 | if (cur >= i_size) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3468 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 3469 | tree->ops->writepage_end_io_hook(page, cur, |
| 3470 | page_end, NULL, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3471 | break; |
| 3472 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3473 | em = epd->get_extent(inode, page, pg_offset, cur, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3474 | end - cur + 1, 1); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3475 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3476 | SetPageError(page); |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 3477 | ret = PTR_ERR_OR_ZERO(em); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3478 | break; |
| 3479 | } |
| 3480 | |
| 3481 | extent_offset = cur - em->start; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3482 | em_end = extent_map_end(em); |
| 3483 | BUG_ON(em_end <= cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3484 | BUG_ON(end < cur); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3485 | iosize = min(em_end - cur, end - cur + 1); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3486 | iosize = ALIGN(iosize, blocksize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3487 | sector = (em->block_start + extent_offset) >> 9; |
| 3488 | bdev = em->bdev; |
| 3489 | block_start = em->block_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3490 | compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3491 | free_extent_map(em); |
| 3492 | em = NULL; |
| 3493 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3494 | /* |
| 3495 | * compressed and inline extents are written through other |
| 3496 | * paths in the FS |
| 3497 | */ |
| 3498 | if (compressed || block_start == EXTENT_MAP_HOLE || |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3499 | block_start == EXTENT_MAP_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3500 | /* |
| 3501 | * end_io notification does not happen here for |
| 3502 | * compressed extents |
| 3503 | */ |
| 3504 | if (!compressed && tree->ops && |
| 3505 | tree->ops->writepage_end_io_hook) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3506 | tree->ops->writepage_end_io_hook(page, cur, |
| 3507 | cur + iosize - 1, |
| 3508 | NULL, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3509 | else if (compressed) { |
| 3510 | /* we don't want to end_page_writeback on |
| 3511 | * a compressed extent. this happens |
| 3512 | * elsewhere |
| 3513 | */ |
| 3514 | nr++; |
| 3515 | } |
| 3516 | |
| 3517 | cur += iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3518 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3519 | continue; |
| 3520 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3521 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3522 | if (tree->ops && tree->ops->writepage_io_hook) { |
| 3523 | ret = tree->ops->writepage_io_hook(page, cur, |
| 3524 | cur + iosize - 1); |
| 3525 | } else { |
| 3526 | ret = 0; |
| 3527 | } |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 3528 | if (ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3529 | SetPageError(page); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 3530 | } else { |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3531 | unsigned long max_nr = (i_size >> PAGE_CACHE_SHIFT) + 1; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3532 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3533 | set_range_writeback(tree, cur, cur + iosize - 1); |
| 3534 | if (!PageWriteback(page)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3535 | btrfs_err(BTRFS_I(inode)->root->fs_info, |
| 3536 | "page %lu not writeback, cur %llu end %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3537 | page->index, cur, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3538 | } |
| 3539 | |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 3540 | ret = submit_extent_page(write_flags, tree, wbc, page, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3541 | sector, iosize, pg_offset, |
| 3542 | bdev, &epd->bio, max_nr, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3543 | end_bio_extent_writepage, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3544 | 0, 0, 0, false); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3545 | if (ret) |
| 3546 | SetPageError(page); |
| 3547 | } |
| 3548 | cur = cur + iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3549 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3550 | nr++; |
| 3551 | } |
| 3552 | done: |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3553 | *nr_ret = nr; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3554 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3555 | done_unlocked: |
| 3556 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 3557 | /* drop our reference on any cached states */ |
| 3558 | free_extent_state(cached_state); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3559 | return ret; |
| 3560 | } |
| 3561 | |
| 3562 | /* |
| 3563 | * the writepage semantics are similar to regular writepage. extent |
| 3564 | * records are inserted to lock ranges in the tree, and as dirty areas |
| 3565 | * are found, they are marked writeback. Then the lock bits are removed |
| 3566 | * and the end_io handler clears the writeback ranges |
| 3567 | */ |
| 3568 | static int __extent_writepage(struct page *page, struct writeback_control *wbc, |
| 3569 | void *data) |
| 3570 | { |
| 3571 | struct inode *inode = page->mapping->host; |
| 3572 | struct extent_page_data *epd = data; |
| 3573 | u64 start = page_offset(page); |
| 3574 | u64 page_end = start + PAGE_CACHE_SIZE - 1; |
| 3575 | int ret; |
| 3576 | int nr = 0; |
| 3577 | size_t pg_offset = 0; |
| 3578 | loff_t i_size = i_size_read(inode); |
| 3579 | unsigned long end_index = i_size >> PAGE_CACHE_SHIFT; |
| 3580 | int write_flags; |
| 3581 | unsigned long nr_written = 0; |
| 3582 | |
| 3583 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3584 | write_flags = WRITE_SYNC; |
| 3585 | else |
| 3586 | write_flags = WRITE; |
| 3587 | |
| 3588 | trace___extent_writepage(page, inode, wbc); |
| 3589 | |
| 3590 | WARN_ON(!PageLocked(page)); |
| 3591 | |
| 3592 | ClearPageError(page); |
| 3593 | |
| 3594 | pg_offset = i_size & (PAGE_CACHE_SIZE - 1); |
| 3595 | if (page->index > end_index || |
| 3596 | (page->index == end_index && !pg_offset)) { |
| 3597 | page->mapping->a_ops->invalidatepage(page, 0, PAGE_CACHE_SIZE); |
| 3598 | unlock_page(page); |
| 3599 | return 0; |
| 3600 | } |
| 3601 | |
| 3602 | if (page->index == end_index) { |
| 3603 | char *userpage; |
| 3604 | |
| 3605 | userpage = kmap_atomic(page); |
| 3606 | memset(userpage + pg_offset, 0, |
| 3607 | PAGE_CACHE_SIZE - pg_offset); |
| 3608 | kunmap_atomic(userpage); |
| 3609 | flush_dcache_page(page); |
| 3610 | } |
| 3611 | |
| 3612 | pg_offset = 0; |
| 3613 | |
| 3614 | set_page_extent_mapped(page); |
| 3615 | |
| 3616 | ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written); |
| 3617 | if (ret == 1) |
| 3618 | goto done_unlocked; |
| 3619 | if (ret) |
| 3620 | goto done; |
| 3621 | |
| 3622 | ret = __extent_writepage_io(inode, page, wbc, epd, |
| 3623 | i_size, nr_written, write_flags, &nr); |
| 3624 | if (ret == 1) |
| 3625 | goto done_unlocked; |
| 3626 | |
| 3627 | done: |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3628 | if (nr == 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3629 | /* make sure the mapping tag for page dirty gets cleared */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3630 | set_page_writeback(page); |
| 3631 | end_page_writeback(page); |
| 3632 | } |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 3633 | if (PageError(page)) { |
| 3634 | ret = ret < 0 ? ret : -EIO; |
| 3635 | end_extent_writepage(page, ret, start, page_end); |
| 3636 | } |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 3637 | unlock_page(page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3638 | return ret; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3639 | |
| 3640 | done_unlocked: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3641 | return 0; |
| 3642 | } |
| 3643 | |
Josef Bacik | fd8b2b6 | 2013-04-24 16:41:19 -0400 | [diff] [blame] | 3644 | void wait_on_extent_buffer_writeback(struct extent_buffer *eb) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3645 | { |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 3646 | wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK, |
| 3647 | TASK_UNINTERRUPTIBLE); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3648 | } |
| 3649 | |
Chris Mason | 0e378df | 2014-05-19 20:55:27 -0700 | [diff] [blame] | 3650 | static noinline_for_stack int |
| 3651 | lock_extent_buffer_for_io(struct extent_buffer *eb, |
| 3652 | struct btrfs_fs_info *fs_info, |
| 3653 | struct extent_page_data *epd) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3654 | { |
| 3655 | unsigned long i, num_pages; |
| 3656 | int flush = 0; |
| 3657 | int ret = 0; |
| 3658 | |
| 3659 | if (!btrfs_try_tree_write_lock(eb)) { |
| 3660 | flush = 1; |
| 3661 | flush_write_bio(epd); |
| 3662 | btrfs_tree_lock(eb); |
| 3663 | } |
| 3664 | |
| 3665 | if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) { |
| 3666 | btrfs_tree_unlock(eb); |
| 3667 | if (!epd->sync_io) |
| 3668 | return 0; |
| 3669 | if (!flush) { |
| 3670 | flush_write_bio(epd); |
| 3671 | flush = 1; |
| 3672 | } |
Chris Mason | a098d8e | 2012-03-21 12:09:56 -0400 | [diff] [blame] | 3673 | while (1) { |
| 3674 | wait_on_extent_buffer_writeback(eb); |
| 3675 | btrfs_tree_lock(eb); |
| 3676 | if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) |
| 3677 | break; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3678 | btrfs_tree_unlock(eb); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3679 | } |
| 3680 | } |
| 3681 | |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3682 | /* |
| 3683 | * We need to do this to prevent races in people who check if the eb is |
| 3684 | * under IO since we can end up having no IO bits set for a short period |
| 3685 | * of time. |
| 3686 | */ |
| 3687 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3688 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) { |
| 3689 | set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3690 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3691 | btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); |
Miao Xie | e2d8452 | 2013-01-29 10:09:20 +0000 | [diff] [blame] | 3692 | __percpu_counter_add(&fs_info->dirty_metadata_bytes, |
| 3693 | -eb->len, |
| 3694 | fs_info->dirty_metadata_batch); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3695 | ret = 1; |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3696 | } else { |
| 3697 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3698 | } |
| 3699 | |
| 3700 | btrfs_tree_unlock(eb); |
| 3701 | |
| 3702 | if (!ret) |
| 3703 | return ret; |
| 3704 | |
| 3705 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3706 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 3707 | struct page *p = eb->pages[i]; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3708 | |
| 3709 | if (!trylock_page(p)) { |
| 3710 | if (!flush) { |
| 3711 | flush_write_bio(epd); |
| 3712 | flush = 1; |
| 3713 | } |
| 3714 | lock_page(p); |
| 3715 | } |
| 3716 | } |
| 3717 | |
| 3718 | return ret; |
| 3719 | } |
| 3720 | |
| 3721 | static void end_extent_buffer_writeback(struct extent_buffer *eb) |
| 3722 | { |
| 3723 | clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 3724 | smp_mb__after_atomic(); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3725 | wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK); |
| 3726 | } |
| 3727 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3728 | static void set_btree_ioerr(struct page *page) |
| 3729 | { |
| 3730 | struct extent_buffer *eb = (struct extent_buffer *)page->private; |
| 3731 | struct btrfs_inode *btree_ino = BTRFS_I(eb->fs_info->btree_inode); |
| 3732 | |
| 3733 | SetPageError(page); |
| 3734 | if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) |
| 3735 | return; |
| 3736 | |
| 3737 | /* |
| 3738 | * If writeback for a btree extent that doesn't belong to a log tree |
| 3739 | * failed, increment the counter transaction->eb_write_errors. |
| 3740 | * We do this because while the transaction is running and before it's |
| 3741 | * committing (when we call filemap_fdata[write|wait]_range against |
| 3742 | * the btree inode), we might have |
| 3743 | * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it |
| 3744 | * returns an error or an error happens during writeback, when we're |
| 3745 | * committing the transaction we wouldn't know about it, since the pages |
| 3746 | * can be no longer dirty nor marked anymore for writeback (if a |
| 3747 | * subsequent modification to the extent buffer didn't happen before the |
| 3748 | * transaction commit), which makes filemap_fdata[write|wait]_range not |
| 3749 | * able to find the pages tagged with SetPageError at transaction |
| 3750 | * commit time. So if this happens we must abort the transaction, |
| 3751 | * otherwise we commit a super block with btree roots that point to |
| 3752 | * btree nodes/leafs whose content on disk is invalid - either garbage |
| 3753 | * or the content of some node/leaf from a past generation that got |
| 3754 | * cowed or deleted and is no longer valid. |
| 3755 | * |
| 3756 | * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would |
| 3757 | * not be enough - we need to distinguish between log tree extents vs |
| 3758 | * non-log tree extents, and the next filemap_fdatawait_range() call |
| 3759 | * will catch and clear such errors in the mapping - and that call might |
| 3760 | * be from a log sync and not from a transaction commit. Also, checking |
| 3761 | * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is |
| 3762 | * not done and would not be reliable - the eb might have been released |
| 3763 | * from memory and reading it back again means that flag would not be |
| 3764 | * set (since it's a runtime flag, not persisted on disk). |
| 3765 | * |
| 3766 | * Using the flags below in the btree inode also makes us achieve the |
| 3767 | * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started |
| 3768 | * writeback for all dirty pages and before filemap_fdatawait_range() |
| 3769 | * is called, the writeback for all dirty pages had already finished |
| 3770 | * with errors - because we were not using AS_EIO/AS_ENOSPC, |
| 3771 | * filemap_fdatawait_range() would return success, as it could not know |
| 3772 | * that writeback errors happened (the pages were no longer tagged for |
| 3773 | * writeback). |
| 3774 | */ |
| 3775 | switch (eb->log_index) { |
| 3776 | case -1: |
| 3777 | set_bit(BTRFS_INODE_BTREE_ERR, &btree_ino->runtime_flags); |
| 3778 | break; |
| 3779 | case 0: |
| 3780 | set_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags); |
| 3781 | break; |
| 3782 | case 1: |
| 3783 | set_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags); |
| 3784 | break; |
| 3785 | default: |
| 3786 | BUG(); /* unexpected, logic error */ |
| 3787 | } |
| 3788 | } |
| 3789 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3790 | static void end_bio_extent_buffer_writepage(struct bio *bio) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3791 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3792 | struct bio_vec *bvec; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3793 | struct extent_buffer *eb; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3794 | int i, done; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3795 | |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3796 | bio_for_each_segment_all(bvec, bio, i) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3797 | struct page *page = bvec->bv_page; |
| 3798 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3799 | eb = (struct extent_buffer *)page->private; |
| 3800 | BUG_ON(!eb); |
| 3801 | done = atomic_dec_and_test(&eb->io_pages); |
| 3802 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3803 | if (bio->bi_error || |
| 3804 | test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3805 | ClearPageUptodate(page); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3806 | set_btree_ioerr(page); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3807 | } |
| 3808 | |
| 3809 | end_page_writeback(page); |
| 3810 | |
| 3811 | if (!done) |
| 3812 | continue; |
| 3813 | |
| 3814 | end_extent_buffer_writeback(eb); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3815 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3816 | |
| 3817 | bio_put(bio); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3818 | } |
| 3819 | |
Chris Mason | 0e378df | 2014-05-19 20:55:27 -0700 | [diff] [blame] | 3820 | static noinline_for_stack int write_one_eb(struct extent_buffer *eb, |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3821 | struct btrfs_fs_info *fs_info, |
| 3822 | struct writeback_control *wbc, |
| 3823 | struct extent_page_data *epd) |
| 3824 | { |
| 3825 | struct block_device *bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 3826 | 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] | 3827 | u64 offset = eb->start; |
| 3828 | unsigned long i, num_pages; |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3829 | unsigned long bio_flags = 0; |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 3830 | int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META; |
Josef Bacik | d7dbe9e | 2012-04-23 14:00:51 -0400 | [diff] [blame] | 3831 | int ret = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3832 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3833 | clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3834 | num_pages = num_extent_pages(eb->start, eb->len); |
| 3835 | atomic_set(&eb->io_pages, num_pages); |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3836 | if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID) |
| 3837 | bio_flags = EXTENT_BIO_TREE_LOG; |
| 3838 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3839 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 3840 | struct page *p = eb->pages[i]; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3841 | |
| 3842 | clear_page_dirty_for_io(p); |
| 3843 | set_page_writeback(p); |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 3844 | ret = submit_extent_page(rw, tree, wbc, p, offset >> 9, |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3845 | PAGE_CACHE_SIZE, 0, bdev, &epd->bio, |
| 3846 | -1, end_bio_extent_buffer_writepage, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3847 | 0, epd->bio_flags, bio_flags, false); |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3848 | epd->bio_flags = bio_flags; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3849 | if (ret) { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3850 | set_btree_ioerr(p); |
Filipe Manana | 55e3bd2 | 2014-09-22 17:41:04 +0100 | [diff] [blame] | 3851 | end_page_writeback(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3852 | if (atomic_sub_and_test(num_pages - i, &eb->io_pages)) |
| 3853 | end_extent_buffer_writeback(eb); |
| 3854 | ret = -EIO; |
| 3855 | break; |
| 3856 | } |
| 3857 | offset += PAGE_CACHE_SIZE; |
| 3858 | update_nr_written(p, wbc, 1); |
| 3859 | unlock_page(p); |
| 3860 | } |
| 3861 | |
| 3862 | if (unlikely(ret)) { |
| 3863 | for (; i < num_pages; i++) { |
Chris Mason | bbf65cf | 2014-10-04 09:56:45 -0700 | [diff] [blame] | 3864 | struct page *p = eb->pages[i]; |
Liu Bo | 8146502 | 2014-09-23 22:22:33 +0800 | [diff] [blame] | 3865 | clear_page_dirty_for_io(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3866 | unlock_page(p); |
| 3867 | } |
| 3868 | } |
| 3869 | |
| 3870 | return ret; |
| 3871 | } |
| 3872 | |
| 3873 | int btree_write_cache_pages(struct address_space *mapping, |
| 3874 | struct writeback_control *wbc) |
| 3875 | { |
| 3876 | struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree; |
| 3877 | struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info; |
| 3878 | struct extent_buffer *eb, *prev_eb = NULL; |
| 3879 | struct extent_page_data epd = { |
| 3880 | .bio = NULL, |
| 3881 | .tree = tree, |
| 3882 | .extent_locked = 0, |
| 3883 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3884 | .bio_flags = 0, |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3885 | }; |
| 3886 | int ret = 0; |
| 3887 | int done = 0; |
| 3888 | int nr_to_write_done = 0; |
| 3889 | struct pagevec pvec; |
| 3890 | int nr_pages; |
| 3891 | pgoff_t index; |
| 3892 | pgoff_t end; /* Inclusive */ |
| 3893 | int scanned = 0; |
| 3894 | int tag; |
| 3895 | |
| 3896 | pagevec_init(&pvec, 0); |
| 3897 | if (wbc->range_cyclic) { |
| 3898 | index = mapping->writeback_index; /* Start from prev offset */ |
| 3899 | end = -1; |
| 3900 | } else { |
| 3901 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 3902 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
| 3903 | scanned = 1; |
| 3904 | } |
| 3905 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3906 | tag = PAGECACHE_TAG_TOWRITE; |
| 3907 | else |
| 3908 | tag = PAGECACHE_TAG_DIRTY; |
| 3909 | retry: |
| 3910 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3911 | tag_pages_for_writeback(mapping, index, end); |
| 3912 | while (!done && !nr_to_write_done && (index <= end) && |
| 3913 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
| 3914 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { |
| 3915 | unsigned i; |
| 3916 | |
| 3917 | scanned = 1; |
| 3918 | for (i = 0; i < nr_pages; i++) { |
| 3919 | struct page *page = pvec.pages[i]; |
| 3920 | |
| 3921 | if (!PagePrivate(page)) |
| 3922 | continue; |
| 3923 | |
| 3924 | if (!wbc->range_cyclic && page->index > end) { |
| 3925 | done = 1; |
| 3926 | break; |
| 3927 | } |
| 3928 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3929 | spin_lock(&mapping->private_lock); |
| 3930 | if (!PagePrivate(page)) { |
| 3931 | spin_unlock(&mapping->private_lock); |
| 3932 | continue; |
| 3933 | } |
| 3934 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3935 | eb = (struct extent_buffer *)page->private; |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3936 | |
| 3937 | /* |
| 3938 | * Shouldn't happen and normally this would be a BUG_ON |
| 3939 | * but no sense in crashing the users box for something |
| 3940 | * we can survive anyway. |
| 3941 | */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3942 | if (WARN_ON(!eb)) { |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3943 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3944 | continue; |
| 3945 | } |
| 3946 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3947 | if (eb == prev_eb) { |
| 3948 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3949 | continue; |
| 3950 | } |
| 3951 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3952 | ret = atomic_inc_not_zero(&eb->refs); |
| 3953 | spin_unlock(&mapping->private_lock); |
| 3954 | if (!ret) |
| 3955 | continue; |
| 3956 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3957 | prev_eb = eb; |
| 3958 | ret = lock_extent_buffer_for_io(eb, fs_info, &epd); |
| 3959 | if (!ret) { |
| 3960 | free_extent_buffer(eb); |
| 3961 | continue; |
| 3962 | } |
| 3963 | |
| 3964 | ret = write_one_eb(eb, fs_info, wbc, &epd); |
| 3965 | if (ret) { |
| 3966 | done = 1; |
| 3967 | free_extent_buffer(eb); |
| 3968 | break; |
| 3969 | } |
| 3970 | free_extent_buffer(eb); |
| 3971 | |
| 3972 | /* |
| 3973 | * the filesystem may choose to bump up nr_to_write. |
| 3974 | * We have to make sure to honor the new nr_to_write |
| 3975 | * at any time |
| 3976 | */ |
| 3977 | nr_to_write_done = wbc->nr_to_write <= 0; |
| 3978 | } |
| 3979 | pagevec_release(&pvec); |
| 3980 | cond_resched(); |
| 3981 | } |
| 3982 | if (!scanned && !done) { |
| 3983 | /* |
| 3984 | * We hit the last page and there is more work to be done: wrap |
| 3985 | * back to the start of the file |
| 3986 | */ |
| 3987 | scanned = 1; |
| 3988 | index = 0; |
| 3989 | goto retry; |
| 3990 | } |
| 3991 | flush_write_bio(&epd); |
| 3992 | return ret; |
| 3993 | } |
| 3994 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3995 | /** |
| 3996 | * write_cache_pages - walk the list of dirty pages of the given address space and write all of them. |
| 3997 | * @mapping: address space structure to write |
| 3998 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
| 3999 | * @writepage: function called for each page |
| 4000 | * @data: data passed to writepage function |
| 4001 | * |
| 4002 | * If a page is already under I/O, write_cache_pages() skips it, even |
| 4003 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, |
| 4004 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() |
| 4005 | * and msync() need to guarantee that all the data which was dirty at the time |
| 4006 | * the call was made get new I/O started against them. If wbc->sync_mode is |
| 4007 | * WB_SYNC_ALL then we were called for data integrity and we must wait for |
| 4008 | * existing IO to complete. |
| 4009 | */ |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 4010 | static int extent_write_cache_pages(struct extent_io_tree *tree, |
| 4011 | struct address_space *mapping, |
| 4012 | struct writeback_control *wbc, |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4013 | writepage_t writepage, void *data, |
| 4014 | void (*flush_fn)(void *)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4015 | { |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 4016 | struct inode *inode = mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4017 | int ret = 0; |
| 4018 | int done = 0; |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 4019 | int err = 0; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 4020 | int nr_to_write_done = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4021 | struct pagevec pvec; |
| 4022 | int nr_pages; |
| 4023 | pgoff_t index; |
| 4024 | pgoff_t end; /* Inclusive */ |
| 4025 | int scanned = 0; |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 4026 | int tag; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4027 | |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 4028 | /* |
| 4029 | * We have to hold onto the inode so that ordered extents can do their |
| 4030 | * work when the IO finishes. The alternative to this is failing to add |
| 4031 | * an ordered extent if the igrab() fails there and that is a huge pain |
| 4032 | * to deal with, so instead just hold onto the inode throughout the |
| 4033 | * writepages operation. If it fails here we are freeing up the inode |
| 4034 | * anyway and we'd rather not waste our time writing out stuff that is |
| 4035 | * going to be truncated anyway. |
| 4036 | */ |
| 4037 | if (!igrab(inode)) |
| 4038 | return 0; |
| 4039 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4040 | pagevec_init(&pvec, 0); |
| 4041 | if (wbc->range_cyclic) { |
| 4042 | index = mapping->writeback_index; /* Start from prev offset */ |
| 4043 | end = -1; |
| 4044 | } else { |
| 4045 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 4046 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4047 | scanned = 1; |
| 4048 | } |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 4049 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 4050 | tag = PAGECACHE_TAG_TOWRITE; |
| 4051 | else |
| 4052 | tag = PAGECACHE_TAG_DIRTY; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4053 | retry: |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 4054 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 4055 | tag_pages_for_writeback(mapping, index, end); |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 4056 | while (!done && !nr_to_write_done && (index <= end) && |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 4057 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
| 4058 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4059 | unsigned i; |
| 4060 | |
| 4061 | scanned = 1; |
| 4062 | for (i = 0; i < nr_pages; i++) { |
| 4063 | struct page *page = pvec.pages[i]; |
| 4064 | |
| 4065 | /* |
| 4066 | * At this point we hold neither mapping->tree_lock nor |
| 4067 | * lock on the page itself: the page may be truncated or |
| 4068 | * invalidated (changing page->mapping to NULL), or even |
| 4069 | * swizzled back from swapper_space to tmpfs file |
| 4070 | * mapping |
| 4071 | */ |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 4072 | if (!trylock_page(page)) { |
| 4073 | flush_fn(data); |
| 4074 | lock_page(page); |
Chris Mason | 01d658f | 2011-11-01 10:08:06 -0400 | [diff] [blame] | 4075 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4076 | |
| 4077 | if (unlikely(page->mapping != mapping)) { |
| 4078 | unlock_page(page); |
| 4079 | continue; |
| 4080 | } |
| 4081 | |
| 4082 | if (!wbc->range_cyclic && page->index > end) { |
| 4083 | done = 1; |
| 4084 | unlock_page(page); |
| 4085 | continue; |
| 4086 | } |
| 4087 | |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4088 | if (wbc->sync_mode != WB_SYNC_NONE) { |
Chris Mason | 0e6bd95 | 2008-11-20 10:46:35 -0500 | [diff] [blame] | 4089 | if (PageWriteback(page)) |
| 4090 | flush_fn(data); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4091 | wait_on_page_writeback(page); |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4092 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4093 | |
| 4094 | if (PageWriteback(page) || |
| 4095 | !clear_page_dirty_for_io(page)) { |
| 4096 | unlock_page(page); |
| 4097 | continue; |
| 4098 | } |
| 4099 | |
| 4100 | ret = (*writepage)(page, wbc, data); |
| 4101 | |
| 4102 | if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) { |
| 4103 | unlock_page(page); |
| 4104 | ret = 0; |
| 4105 | } |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 4106 | if (!err && ret < 0) |
| 4107 | err = ret; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 4108 | |
| 4109 | /* |
| 4110 | * the filesystem may choose to bump up nr_to_write. |
| 4111 | * We have to make sure to honor the new nr_to_write |
| 4112 | * at any time |
| 4113 | */ |
| 4114 | nr_to_write_done = wbc->nr_to_write <= 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4115 | } |
| 4116 | pagevec_release(&pvec); |
| 4117 | cond_resched(); |
| 4118 | } |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 4119 | if (!scanned && !done && !err) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4120 | /* |
| 4121 | * We hit the last page and there is more work to be done: wrap |
| 4122 | * back to the start of the file |
| 4123 | */ |
| 4124 | scanned = 1; |
| 4125 | index = 0; |
| 4126 | goto retry; |
| 4127 | } |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 4128 | btrfs_add_delayed_iput(inode); |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 4129 | return err; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4130 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4131 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4132 | static void flush_epd_write_bio(struct extent_page_data *epd) |
| 4133 | { |
| 4134 | if (epd->bio) { |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 4135 | int rw = WRITE; |
| 4136 | int ret; |
| 4137 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4138 | if (epd->sync_io) |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 4139 | rw = WRITE_SYNC; |
| 4140 | |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 4141 | ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4142 | BUG_ON(ret < 0); /* -ENOMEM */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4143 | epd->bio = NULL; |
| 4144 | } |
| 4145 | } |
| 4146 | |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4147 | static noinline void flush_write_bio(void *data) |
| 4148 | { |
| 4149 | struct extent_page_data *epd = data; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4150 | flush_epd_write_bio(epd); |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4151 | } |
| 4152 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4153 | int extent_write_full_page(struct extent_io_tree *tree, struct page *page, |
| 4154 | get_extent_t *get_extent, |
| 4155 | struct writeback_control *wbc) |
| 4156 | { |
| 4157 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4158 | struct extent_page_data epd = { |
| 4159 | .bio = NULL, |
| 4160 | .tree = tree, |
| 4161 | .get_extent = get_extent, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4162 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4163 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 4164 | .bio_flags = 0, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4165 | }; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4166 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4167 | ret = __extent_writepage(page, wbc, &epd); |
| 4168 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4169 | flush_epd_write_bio(&epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4170 | return ret; |
| 4171 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4172 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4173 | int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode, |
| 4174 | u64 start, u64 end, get_extent_t *get_extent, |
| 4175 | int mode) |
| 4176 | { |
| 4177 | int ret = 0; |
| 4178 | struct address_space *mapping = inode->i_mapping; |
| 4179 | struct page *page; |
| 4180 | unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >> |
| 4181 | PAGE_CACHE_SHIFT; |
| 4182 | |
| 4183 | struct extent_page_data epd = { |
| 4184 | .bio = NULL, |
| 4185 | .tree = tree, |
| 4186 | .get_extent = get_extent, |
| 4187 | .extent_locked = 1, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4188 | .sync_io = mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 4189 | .bio_flags = 0, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4190 | }; |
| 4191 | struct writeback_control wbc_writepages = { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4192 | .sync_mode = mode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4193 | .nr_to_write = nr_pages * 2, |
| 4194 | .range_start = start, |
| 4195 | .range_end = end + 1, |
| 4196 | }; |
| 4197 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4198 | while (start <= end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4199 | page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT); |
| 4200 | if (clear_page_dirty_for_io(page)) |
| 4201 | ret = __extent_writepage(page, &wbc_writepages, &epd); |
| 4202 | else { |
| 4203 | if (tree->ops && tree->ops->writepage_end_io_hook) |
| 4204 | tree->ops->writepage_end_io_hook(page, start, |
| 4205 | start + PAGE_CACHE_SIZE - 1, |
| 4206 | NULL, 1); |
| 4207 | unlock_page(page); |
| 4208 | } |
| 4209 | page_cache_release(page); |
| 4210 | start += PAGE_CACHE_SIZE; |
| 4211 | } |
| 4212 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4213 | flush_epd_write_bio(&epd); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4214 | return ret; |
| 4215 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4216 | |
| 4217 | int extent_writepages(struct extent_io_tree *tree, |
| 4218 | struct address_space *mapping, |
| 4219 | get_extent_t *get_extent, |
| 4220 | struct writeback_control *wbc) |
| 4221 | { |
| 4222 | int ret = 0; |
| 4223 | struct extent_page_data epd = { |
| 4224 | .bio = NULL, |
| 4225 | .tree = tree, |
| 4226 | .get_extent = get_extent, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4227 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4228 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 4229 | .bio_flags = 0, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4230 | }; |
| 4231 | |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 4232 | ret = extent_write_cache_pages(tree, mapping, wbc, |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4233 | __extent_writepage, &epd, |
| 4234 | flush_write_bio); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4235 | flush_epd_write_bio(&epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4236 | return ret; |
| 4237 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4238 | |
| 4239 | int extent_readpages(struct extent_io_tree *tree, |
| 4240 | struct address_space *mapping, |
| 4241 | struct list_head *pages, unsigned nr_pages, |
| 4242 | get_extent_t get_extent) |
| 4243 | { |
| 4244 | struct bio *bio = NULL; |
| 4245 | unsigned page_idx; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4246 | unsigned long bio_flags = 0; |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4247 | struct page *pagepool[16]; |
| 4248 | struct page *page; |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4249 | struct extent_map *em_cached = NULL; |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4250 | int nr = 0; |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 4251 | u64 prev_em_start = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4252 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4253 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4254 | page = list_entry(pages->prev, struct page, lru); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4255 | |
| 4256 | prefetchw(&page->flags); |
| 4257 | list_del(&page->lru); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4258 | if (add_to_page_cache_lru(page, mapping, |
Itaru Kitayama | 43e817a | 2011-04-25 19:43:51 -0400 | [diff] [blame] | 4259 | page->index, GFP_NOFS)) { |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4260 | page_cache_release(page); |
| 4261 | continue; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4262 | } |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4263 | |
| 4264 | pagepool[nr++] = page; |
| 4265 | if (nr < ARRAY_SIZE(pagepool)) |
| 4266 | continue; |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4267 | __extent_readpages(tree, pagepool, nr, get_extent, &em_cached, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 4268 | &bio, 0, &bio_flags, READ, &prev_em_start); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4269 | nr = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4270 | } |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 4271 | if (nr) |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4272 | __extent_readpages(tree, pagepool, nr, get_extent, &em_cached, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 4273 | &bio, 0, &bio_flags, READ, &prev_em_start); |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4274 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4275 | if (em_cached) |
| 4276 | free_extent_map(em_cached); |
| 4277 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4278 | BUG_ON(!list_empty(pages)); |
| 4279 | if (bio) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4280 | return submit_one_bio(READ, bio, 0, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4281 | return 0; |
| 4282 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4283 | |
| 4284 | /* |
| 4285 | * basic invalidatepage code, this waits on any locked or writeback |
| 4286 | * ranges corresponding to the page, and then deletes any extent state |
| 4287 | * records from the tree |
| 4288 | */ |
| 4289 | int extent_invalidatepage(struct extent_io_tree *tree, |
| 4290 | struct page *page, unsigned long offset) |
| 4291 | { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4292 | struct extent_state *cached_state = NULL; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4293 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4294 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 4295 | size_t blocksize = page->mapping->host->i_sb->s_blocksize; |
| 4296 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4297 | start += ALIGN(offset, blocksize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4298 | if (start > end) |
| 4299 | return 0; |
| 4300 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4301 | lock_extent_bits(tree, start, end, 0, &cached_state); |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 4302 | wait_on_page_writeback(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4303 | clear_extent_bit(tree, start, end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 4304 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4305 | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4306 | 1, 1, &cached_state, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4307 | return 0; |
| 4308 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4309 | |
| 4310 | /* |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4311 | * a helper for releasepage, this tests for areas of the page that |
| 4312 | * are locked or under IO and drops the related state bits if it is safe |
| 4313 | * to drop the page. |
| 4314 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 4315 | static int try_release_extent_state(struct extent_map_tree *map, |
| 4316 | struct extent_io_tree *tree, |
| 4317 | struct page *page, gfp_t mask) |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4318 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4319 | u64 start = page_offset(page); |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4320 | u64 end = start + PAGE_CACHE_SIZE - 1; |
| 4321 | int ret = 1; |
| 4322 | |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 4323 | if (test_range_bit(tree, start, end, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4324 | EXTENT_IOBITS, 0, NULL)) |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4325 | ret = 0; |
| 4326 | else { |
| 4327 | if ((mask & GFP_NOFS) == GFP_NOFS) |
| 4328 | mask = GFP_NOFS; |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 4329 | /* |
| 4330 | * at this point we can safely clear everything except the |
| 4331 | * locked bit and the nodatasum bit |
| 4332 | */ |
Chris Mason | e3f24cc | 2011-02-14 12:52:08 -0500 | [diff] [blame] | 4333 | ret = clear_extent_bit(tree, start, end, |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 4334 | ~(EXTENT_LOCKED | EXTENT_NODATASUM), |
| 4335 | 0, 0, NULL, mask); |
Chris Mason | e3f24cc | 2011-02-14 12:52:08 -0500 | [diff] [blame] | 4336 | |
| 4337 | /* if clear_extent_bit failed for enomem reasons, |
| 4338 | * we can't allow the release to continue. |
| 4339 | */ |
| 4340 | if (ret < 0) |
| 4341 | ret = 0; |
| 4342 | else |
| 4343 | ret = 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4344 | } |
| 4345 | return ret; |
| 4346 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4347 | |
| 4348 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4349 | * a helper for releasepage. As long as there are no locked extents |
| 4350 | * in the range corresponding to the page, both state records and extent |
| 4351 | * map records are removed |
| 4352 | */ |
| 4353 | int try_release_extent_mapping(struct extent_map_tree *map, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4354 | struct extent_io_tree *tree, struct page *page, |
| 4355 | gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4356 | { |
| 4357 | struct extent_map *em; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4358 | u64 start = page_offset(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4359 | u64 end = start + PAGE_CACHE_SIZE - 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4360 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4361 | if ((mask & __GFP_WAIT) && |
| 4362 | page->mapping->host->i_size > 16 * 1024 * 1024) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4363 | u64 len; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4364 | while (start <= end) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4365 | len = end - start + 1; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4366 | write_lock(&map->lock); |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4367 | em = lookup_extent_mapping(map, start, len); |
Tsutomu Itoh | 285190d | 2012-02-16 16:23:58 +0900 | [diff] [blame] | 4368 | if (!em) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4369 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4370 | break; |
| 4371 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 4372 | if (test_bit(EXTENT_FLAG_PINNED, &em->flags) || |
| 4373 | em->start != start) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4374 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4375 | free_extent_map(em); |
| 4376 | break; |
| 4377 | } |
| 4378 | if (!test_range_bit(tree, em->start, |
| 4379 | extent_map_end(em) - 1, |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 4380 | EXTENT_LOCKED | EXTENT_WRITEBACK, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 4381 | 0, NULL)) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4382 | remove_extent_mapping(map, em); |
| 4383 | /* once for the rb tree */ |
| 4384 | free_extent_map(em); |
| 4385 | } |
| 4386 | start = extent_map_end(em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4387 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4388 | |
| 4389 | /* once for us */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4390 | free_extent_map(em); |
| 4391 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4392 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4393 | return try_release_extent_state(map, tree, page, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4394 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4395 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4396 | /* |
| 4397 | * helper function for fiemap, which doesn't want to see any holes. |
| 4398 | * This maps until we find something past 'last' |
| 4399 | */ |
| 4400 | static struct extent_map *get_extent_skip_holes(struct inode *inode, |
| 4401 | u64 offset, |
| 4402 | u64 last, |
| 4403 | get_extent_t *get_extent) |
| 4404 | { |
| 4405 | u64 sectorsize = BTRFS_I(inode)->root->sectorsize; |
| 4406 | struct extent_map *em; |
| 4407 | u64 len; |
| 4408 | |
| 4409 | if (offset >= last) |
| 4410 | return NULL; |
| 4411 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 4412 | while (1) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4413 | len = last - offset; |
| 4414 | if (len == 0) |
| 4415 | break; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4416 | len = ALIGN(len, sectorsize); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4417 | em = get_extent(inode, NULL, 0, offset, len, 0); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 4418 | if (IS_ERR_OR_NULL(em)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4419 | return em; |
| 4420 | |
| 4421 | /* if this isn't a hole return it */ |
| 4422 | if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) && |
| 4423 | em->block_start != EXTENT_MAP_HOLE) { |
| 4424 | return em; |
| 4425 | } |
| 4426 | |
| 4427 | /* this is a hole, advance to the next extent */ |
| 4428 | offset = extent_map_end(em); |
| 4429 | free_extent_map(em); |
| 4430 | if (offset >= last) |
| 4431 | break; |
| 4432 | } |
| 4433 | return NULL; |
| 4434 | } |
| 4435 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4436 | int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4437 | __u64 start, __u64 len, get_extent_t *get_extent) |
| 4438 | { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4439 | int ret = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4440 | u64 off = start; |
| 4441 | u64 max = start + len; |
| 4442 | u32 flags = 0; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4443 | u32 found_type; |
| 4444 | u64 last; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4445 | u64 last_for_get_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4446 | u64 disko = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4447 | u64 isize = i_size_read(inode); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4448 | struct btrfs_key found_key; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4449 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4450 | struct extent_state *cached_state = NULL; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4451 | struct btrfs_path *path; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4452 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4453 | int end = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4454 | u64 em_start = 0; |
| 4455 | u64 em_len = 0; |
| 4456 | u64 em_end = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4457 | |
| 4458 | if (len == 0) |
| 4459 | return -EINVAL; |
| 4460 | |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4461 | path = btrfs_alloc_path(); |
| 4462 | if (!path) |
| 4463 | return -ENOMEM; |
| 4464 | path->leave_spinning = 1; |
| 4465 | |
Qu Wenruo | 2c91943 | 2014-07-18 09:55:43 +0800 | [diff] [blame] | 4466 | start = round_down(start, BTRFS_I(inode)->root->sectorsize); |
| 4467 | len = round_up(max, BTRFS_I(inode)->root->sectorsize) - start; |
Josef Bacik | 4d479cf | 2011-11-17 11:34:31 -0500 | [diff] [blame] | 4468 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4469 | /* |
| 4470 | * lookup the last file extent. We're not using i_size here |
| 4471 | * because there might be preallocation past i_size |
| 4472 | */ |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4473 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), -1, |
| 4474 | 0); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4475 | if (ret < 0) { |
| 4476 | btrfs_free_path(path); |
| 4477 | return ret; |
| 4478 | } |
| 4479 | WARN_ON(!ret); |
| 4480 | path->slots[0]--; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4481 | 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] | 4482 | found_type = found_key.type; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4483 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4484 | /* No extents, but there might be delalloc bits */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4485 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4486 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4487 | /* have to trust i_size as the end */ |
| 4488 | last = (u64)-1; |
| 4489 | last_for_get_extent = isize; |
| 4490 | } else { |
| 4491 | /* |
| 4492 | * remember the start of the last extent. There are a |
| 4493 | * bunch of different factors that go into the length of the |
| 4494 | * extent, so its much less complex to remember where it started |
| 4495 | */ |
| 4496 | last = found_key.offset; |
| 4497 | last_for_get_extent = last + 1; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4498 | } |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4499 | btrfs_release_path(path); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4500 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4501 | /* |
| 4502 | * we might have some extents allocated but more delalloc past those |
| 4503 | * extents. so, we trust isize unless the start of the last extent is |
| 4504 | * beyond isize |
| 4505 | */ |
| 4506 | if (last < isize) { |
| 4507 | last = (u64)-1; |
| 4508 | last_for_get_extent = isize; |
| 4509 | } |
| 4510 | |
Liu Bo | a52f4cd | 2013-05-01 16:23:41 +0000 | [diff] [blame] | 4511 | 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] | 4512 | &cached_state); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4513 | |
Josef Bacik | 4d479cf | 2011-11-17 11:34:31 -0500 | [diff] [blame] | 4514 | em = get_extent_skip_holes(inode, start, last_for_get_extent, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4515 | get_extent); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4516 | if (!em) |
| 4517 | goto out; |
| 4518 | if (IS_ERR(em)) { |
| 4519 | ret = PTR_ERR(em); |
| 4520 | goto out; |
| 4521 | } |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4522 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4523 | while (!end) { |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4524 | u64 offset_in_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4525 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4526 | /* break if the extent we found is outside the range */ |
| 4527 | if (em->start >= max || extent_map_end(em) < off) |
| 4528 | break; |
| 4529 | |
| 4530 | /* |
| 4531 | * get_extent may return an extent that starts before our |
| 4532 | * requested range. We have to make sure the ranges |
| 4533 | * we return to fiemap always move forward and don't |
| 4534 | * overlap, so adjust the offsets here |
| 4535 | */ |
| 4536 | em_start = max(em->start, off); |
| 4537 | |
| 4538 | /* |
| 4539 | * record the offset from the start of the extent |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4540 | * for adjusting the disk offset below. Only do this if the |
| 4541 | * extent isn't compressed since our in ram offset may be past |
| 4542 | * what we have actually allocated on disk. |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4543 | */ |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4544 | if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 4545 | offset_in_extent = em_start - em->start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4546 | em_end = extent_map_end(em); |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4547 | em_len = em_end - em_start; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4548 | disko = 0; |
| 4549 | flags = 0; |
| 4550 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4551 | /* |
| 4552 | * bump off for our next call to get_extent |
| 4553 | */ |
| 4554 | off = extent_map_end(em); |
| 4555 | if (off >= max) |
| 4556 | end = 1; |
| 4557 | |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4558 | if (em->block_start == EXTENT_MAP_LAST_BYTE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4559 | end = 1; |
| 4560 | flags |= FIEMAP_EXTENT_LAST; |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4561 | } else if (em->block_start == EXTENT_MAP_INLINE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4562 | flags |= (FIEMAP_EXTENT_DATA_INLINE | |
| 4563 | FIEMAP_EXTENT_NOT_ALIGNED); |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4564 | } else if (em->block_start == EXTENT_MAP_DELALLOC) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4565 | flags |= (FIEMAP_EXTENT_DELALLOC | |
| 4566 | FIEMAP_EXTENT_UNKNOWN); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4567 | } else if (fieinfo->fi_extents_max) { |
| 4568 | u64 bytenr = em->block_start - |
| 4569 | (em->start - em->orig_start); |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4570 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4571 | disko = em->block_start + offset_in_extent; |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4572 | |
| 4573 | /* |
| 4574 | * As btrfs supports shared space, this information |
| 4575 | * can be exported to userspace tools via |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4576 | * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0 |
| 4577 | * then we're just getting a count and we can skip the |
| 4578 | * lookup stuff. |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4579 | */ |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4580 | ret = btrfs_check_shared(NULL, root->fs_info, |
| 4581 | root->objectid, |
| 4582 | btrfs_ino(inode), bytenr); |
| 4583 | if (ret < 0) |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4584 | goto out_free; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4585 | if (ret) |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4586 | flags |= FIEMAP_EXTENT_SHARED; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4587 | ret = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4588 | } |
| 4589 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 4590 | flags |= FIEMAP_EXTENT_ENCODED; |
Josef Bacik | 0d2b237 | 2015-05-19 10:44:04 -0400 | [diff] [blame] | 4591 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 4592 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4593 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4594 | free_extent_map(em); |
| 4595 | em = NULL; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4596 | if ((em_start >= last) || em_len == (u64)-1 || |
| 4597 | (last == (u64)-1 && isize <= em_end)) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4598 | flags |= FIEMAP_EXTENT_LAST; |
| 4599 | end = 1; |
| 4600 | } |
| 4601 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4602 | /* now scan forward to see if this is really the last extent. */ |
| 4603 | em = get_extent_skip_holes(inode, off, last_for_get_extent, |
| 4604 | get_extent); |
| 4605 | if (IS_ERR(em)) { |
| 4606 | ret = PTR_ERR(em); |
| 4607 | goto out; |
| 4608 | } |
| 4609 | if (!em) { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4610 | flags |= FIEMAP_EXTENT_LAST; |
| 4611 | end = 1; |
| 4612 | } |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4613 | ret = fiemap_fill_next_extent(fieinfo, em_start, disko, |
| 4614 | em_len, flags); |
Chengyu Song | 26e726a | 2015-03-24 18:12:56 -0400 | [diff] [blame] | 4615 | if (ret) { |
| 4616 | if (ret == 1) |
| 4617 | ret = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4618 | goto out_free; |
Chengyu Song | 26e726a | 2015-03-24 18:12:56 -0400 | [diff] [blame] | 4619 | } |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4620 | } |
| 4621 | out_free: |
| 4622 | free_extent_map(em); |
| 4623 | out: |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4624 | btrfs_free_path(path); |
Liu Bo | a52f4cd | 2013-05-01 16:23:41 +0000 | [diff] [blame] | 4625 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4626 | &cached_state, GFP_NOFS); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4627 | return ret; |
| 4628 | } |
| 4629 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4630 | static void __free_extent_buffer(struct extent_buffer *eb) |
| 4631 | { |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 4632 | btrfs_leak_debug_del(&eb->leak_list); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4633 | kmem_cache_free(extent_buffer_cache, eb); |
| 4634 | } |
| 4635 | |
Josef Bacik | a26e8c9 | 2014-03-28 17:07:27 -0400 | [diff] [blame] | 4636 | int extent_buffer_under_io(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4637 | { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4638 | return (atomic_read(&eb->io_pages) || |
| 4639 | test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) || |
| 4640 | test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4641 | } |
| 4642 | |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4643 | /* |
| 4644 | * Helper for releasing extent buffer page. |
| 4645 | */ |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4646 | static void btrfs_release_extent_buffer_page(struct extent_buffer *eb) |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4647 | { |
| 4648 | unsigned long index; |
| 4649 | struct page *page; |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 4650 | int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4651 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4652 | BUG_ON(extent_buffer_under_io(eb)); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4653 | |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4654 | index = num_extent_pages(eb->start, eb->len); |
| 4655 | if (index == 0) |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4656 | return; |
| 4657 | |
| 4658 | do { |
| 4659 | index--; |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 4660 | page = eb->pages[index]; |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4661 | if (!page) |
| 4662 | continue; |
| 4663 | if (mapped) |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4664 | spin_lock(&page->mapping->private_lock); |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4665 | /* |
| 4666 | * We do this since we'll remove the pages after we've |
| 4667 | * removed the eb from the radix tree, so we could race |
| 4668 | * and have this page now attached to the new eb. So |
| 4669 | * only clear page_private if it's still connected to |
| 4670 | * this eb. |
| 4671 | */ |
| 4672 | if (PagePrivate(page) && |
| 4673 | page->private == (unsigned long)eb) { |
| 4674 | BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
| 4675 | BUG_ON(PageDirty(page)); |
| 4676 | BUG_ON(PageWriteback(page)); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4677 | /* |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4678 | * We need to make sure we haven't be attached |
| 4679 | * to a new eb. |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4680 | */ |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4681 | ClearPagePrivate(page); |
| 4682 | set_page_private(page, 0); |
| 4683 | /* One for the page private */ |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4684 | page_cache_release(page); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4685 | } |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4686 | |
| 4687 | if (mapped) |
| 4688 | spin_unlock(&page->mapping->private_lock); |
| 4689 | |
| 4690 | /* One for when we alloced the page */ |
| 4691 | page_cache_release(page); |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4692 | } while (index != 0); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4693 | } |
| 4694 | |
| 4695 | /* |
| 4696 | * Helper for releasing the extent buffer. |
| 4697 | */ |
| 4698 | static inline void btrfs_release_extent_buffer(struct extent_buffer *eb) |
| 4699 | { |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 4700 | btrfs_release_extent_buffer_page(eb); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4701 | __free_extent_buffer(eb); |
| 4702 | } |
| 4703 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4704 | static struct extent_buffer * |
| 4705 | __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, |
David Sterba | 23d79d8 | 2014-06-15 02:55:29 +0200 | [diff] [blame] | 4706 | unsigned long len) |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4707 | { |
| 4708 | struct extent_buffer *eb = NULL; |
| 4709 | |
Michal Hocko | d1b5c56 | 2015-08-19 14:17:40 +0200 | [diff] [blame] | 4710 | eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4711 | eb->start = start; |
| 4712 | eb->len = len; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4713 | eb->fs_info = fs_info; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4714 | eb->bflags = 0; |
| 4715 | rwlock_init(&eb->lock); |
| 4716 | atomic_set(&eb->write_locks, 0); |
| 4717 | atomic_set(&eb->read_locks, 0); |
| 4718 | atomic_set(&eb->blocking_readers, 0); |
| 4719 | atomic_set(&eb->blocking_writers, 0); |
| 4720 | atomic_set(&eb->spinning_readers, 0); |
| 4721 | atomic_set(&eb->spinning_writers, 0); |
| 4722 | eb->lock_nested = 0; |
| 4723 | init_waitqueue_head(&eb->write_lock_wq); |
| 4724 | init_waitqueue_head(&eb->read_lock_wq); |
| 4725 | |
| 4726 | btrfs_leak_debug_add(&eb->leak_list, &buffers); |
| 4727 | |
| 4728 | spin_lock_init(&eb->refs_lock); |
| 4729 | atomic_set(&eb->refs, 1); |
| 4730 | atomic_set(&eb->io_pages, 0); |
| 4731 | |
| 4732 | /* |
| 4733 | * Sanity checks, currently the maximum is 64k covered by 16x 4k pages |
| 4734 | */ |
| 4735 | BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE |
| 4736 | > MAX_INLINE_EXTENT_BUFFER_SIZE); |
| 4737 | BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE); |
| 4738 | |
| 4739 | return eb; |
| 4740 | } |
| 4741 | |
| 4742 | struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src) |
| 4743 | { |
| 4744 | unsigned long i; |
| 4745 | struct page *p; |
| 4746 | struct extent_buffer *new; |
| 4747 | unsigned long num_pages = num_extent_pages(src->start, src->len); |
| 4748 | |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4749 | new = __alloc_extent_buffer(src->fs_info, src->start, src->len); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4750 | if (new == NULL) |
| 4751 | return NULL; |
| 4752 | |
| 4753 | for (i = 0; i < num_pages; i++) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 4754 | p = alloc_page(GFP_NOFS); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4755 | if (!p) { |
| 4756 | btrfs_release_extent_buffer(new); |
| 4757 | return NULL; |
| 4758 | } |
| 4759 | attach_extent_buffer_page(new, p); |
| 4760 | WARN_ON(PageDirty(p)); |
| 4761 | SetPageUptodate(p); |
| 4762 | new->pages[i] = p; |
| 4763 | } |
| 4764 | |
| 4765 | copy_extent_buffer(new, src, 0, 0, src->len); |
| 4766 | set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags); |
| 4767 | set_bit(EXTENT_BUFFER_DUMMY, &new->bflags); |
| 4768 | |
| 4769 | return new; |
| 4770 | } |
| 4771 | |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4772 | struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, |
| 4773 | u64 start) |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4774 | { |
| 4775 | struct extent_buffer *eb; |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4776 | unsigned long len; |
| 4777 | unsigned long num_pages; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4778 | unsigned long i; |
| 4779 | |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4780 | if (!fs_info) { |
| 4781 | /* |
| 4782 | * Called only from tests that don't always have a fs_info |
| 4783 | * available, but we know that nodesize is 4096 |
| 4784 | */ |
| 4785 | len = 4096; |
| 4786 | } else { |
| 4787 | len = fs_info->tree_root->nodesize; |
| 4788 | } |
| 4789 | num_pages = num_extent_pages(0, len); |
| 4790 | |
| 4791 | eb = __alloc_extent_buffer(fs_info, start, len); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4792 | if (!eb) |
| 4793 | return NULL; |
| 4794 | |
| 4795 | for (i = 0; i < num_pages; i++) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 4796 | eb->pages[i] = alloc_page(GFP_NOFS); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4797 | if (!eb->pages[i]) |
| 4798 | goto err; |
| 4799 | } |
| 4800 | set_extent_buffer_uptodate(eb); |
| 4801 | btrfs_set_header_nritems(eb, 0); |
| 4802 | set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags); |
| 4803 | |
| 4804 | return eb; |
| 4805 | err: |
| 4806 | for (; i > 0; i--) |
| 4807 | __free_page(eb->pages[i - 1]); |
| 4808 | __free_extent_buffer(eb); |
| 4809 | return NULL; |
| 4810 | } |
| 4811 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4812 | static void check_buffer_tree_ref(struct extent_buffer *eb) |
| 4813 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4814 | int refs; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4815 | /* the ref bit is tricky. We have to make sure it is set |
| 4816 | * if we have the buffer dirty. Otherwise the |
| 4817 | * code to free a buffer can end up dropping a dirty |
| 4818 | * page |
| 4819 | * |
| 4820 | * Once the ref bit is set, it won't go away while the |
| 4821 | * buffer is dirty or in writeback, and it also won't |
| 4822 | * go away while we have the reference count on the |
| 4823 | * eb bumped. |
| 4824 | * |
| 4825 | * We can't just set the ref bit without bumping the |
| 4826 | * ref on the eb because free_extent_buffer might |
| 4827 | * see the ref bit and try to clear it. If this happens |
| 4828 | * free_extent_buffer might end up dropping our original |
| 4829 | * ref by mistake and freeing the page before we are able |
| 4830 | * to add one more ref. |
| 4831 | * |
| 4832 | * So bump the ref count first, then set the bit. If someone |
| 4833 | * beat us to it, drop the ref we added. |
| 4834 | */ |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4835 | refs = atomic_read(&eb->refs); |
| 4836 | if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 4837 | return; |
| 4838 | |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4839 | spin_lock(&eb->refs_lock); |
| 4840 | if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4841 | atomic_inc(&eb->refs); |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4842 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4843 | } |
| 4844 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4845 | static void mark_extent_buffer_accessed(struct extent_buffer *eb, |
| 4846 | struct page *accessed) |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4847 | { |
| 4848 | unsigned long num_pages, i; |
| 4849 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4850 | check_buffer_tree_ref(eb); |
| 4851 | |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4852 | num_pages = num_extent_pages(eb->start, eb->len); |
| 4853 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 4854 | struct page *p = eb->pages[i]; |
| 4855 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4856 | if (p != accessed) |
| 4857 | mark_page_accessed(p); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4858 | } |
| 4859 | } |
| 4860 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4861 | struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info, |
| 4862 | u64 start) |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4863 | { |
| 4864 | struct extent_buffer *eb; |
| 4865 | |
| 4866 | rcu_read_lock(); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4867 | eb = radix_tree_lookup(&fs_info->buffer_radix, |
| 4868 | start >> PAGE_CACHE_SHIFT); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4869 | if (eb && atomic_inc_not_zero(&eb->refs)) { |
| 4870 | rcu_read_unlock(); |
Filipe Manana | 062c19e | 2015-04-23 11:28:48 +0100 | [diff] [blame] | 4871 | /* |
| 4872 | * Lock our eb's refs_lock to avoid races with |
| 4873 | * free_extent_buffer. When we get our eb it might be flagged |
| 4874 | * with EXTENT_BUFFER_STALE and another task running |
| 4875 | * free_extent_buffer might have seen that flag set, |
| 4876 | * eb->refs == 2, that the buffer isn't under IO (dirty and |
| 4877 | * writeback flags not set) and it's still in the tree (flag |
| 4878 | * EXTENT_BUFFER_TREE_REF set), therefore being in the process |
| 4879 | * of decrementing the extent buffer's reference count twice. |
| 4880 | * So here we could race and increment the eb's reference count, |
| 4881 | * clear its stale flag, mark it as dirty and drop our reference |
| 4882 | * before the other task finishes executing free_extent_buffer, |
| 4883 | * which would later result in an attempt to free an extent |
| 4884 | * buffer that is dirty. |
| 4885 | */ |
| 4886 | if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) { |
| 4887 | spin_lock(&eb->refs_lock); |
| 4888 | spin_unlock(&eb->refs_lock); |
| 4889 | } |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4890 | mark_extent_buffer_accessed(eb, NULL); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4891 | return eb; |
| 4892 | } |
| 4893 | rcu_read_unlock(); |
| 4894 | |
| 4895 | return NULL; |
| 4896 | } |
| 4897 | |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4898 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 4899 | struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info, |
David Sterba | ce3e698 | 2014-06-15 03:00:04 +0200 | [diff] [blame] | 4900 | u64 start) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4901 | { |
| 4902 | struct extent_buffer *eb, *exists = NULL; |
| 4903 | int ret; |
| 4904 | |
| 4905 | eb = find_extent_buffer(fs_info, start); |
| 4906 | if (eb) |
| 4907 | return eb; |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4908 | eb = alloc_dummy_extent_buffer(fs_info, start); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4909 | if (!eb) |
| 4910 | return NULL; |
| 4911 | eb->fs_info = fs_info; |
| 4912 | again: |
| 4913 | ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); |
| 4914 | if (ret) |
| 4915 | goto free_eb; |
| 4916 | spin_lock(&fs_info->buffer_lock); |
| 4917 | ret = radix_tree_insert(&fs_info->buffer_radix, |
| 4918 | start >> PAGE_CACHE_SHIFT, eb); |
| 4919 | spin_unlock(&fs_info->buffer_lock); |
| 4920 | radix_tree_preload_end(); |
| 4921 | if (ret == -EEXIST) { |
| 4922 | exists = find_extent_buffer(fs_info, start); |
| 4923 | if (exists) |
| 4924 | goto free_eb; |
| 4925 | else |
| 4926 | goto again; |
| 4927 | } |
| 4928 | check_buffer_tree_ref(eb); |
| 4929 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
| 4930 | |
| 4931 | /* |
| 4932 | * We will free dummy extent buffer's if they come into |
| 4933 | * free_extent_buffer with a ref count of 2, but if we are using this we |
| 4934 | * want the buffers to stay in memory until we're done with them, so |
| 4935 | * bump the ref count again. |
| 4936 | */ |
| 4937 | atomic_inc(&eb->refs); |
| 4938 | return eb; |
| 4939 | free_eb: |
| 4940 | btrfs_release_extent_buffer(eb); |
| 4941 | return exists; |
| 4942 | } |
| 4943 | #endif |
| 4944 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4945 | struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, |
David Sterba | ce3e698 | 2014-06-15 03:00:04 +0200 | [diff] [blame] | 4946 | u64 start) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4947 | { |
David Sterba | ce3e698 | 2014-06-15 03:00:04 +0200 | [diff] [blame] | 4948 | unsigned long len = fs_info->tree_root->nodesize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4949 | unsigned long num_pages = num_extent_pages(start, len); |
| 4950 | unsigned long i; |
| 4951 | unsigned long index = start >> PAGE_CACHE_SHIFT; |
| 4952 | struct extent_buffer *eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4953 | struct extent_buffer *exists = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4954 | struct page *p; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4955 | struct address_space *mapping = fs_info->btree_inode->i_mapping; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4956 | int uptodate = 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4957 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4958 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4959 | eb = find_extent_buffer(fs_info, start); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4960 | if (eb) |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4961 | return eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4962 | |
David Sterba | 23d79d8 | 2014-06-15 02:55:29 +0200 | [diff] [blame] | 4963 | eb = __alloc_extent_buffer(fs_info, start, len); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 4964 | if (!eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4965 | return NULL; |
| 4966 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4967 | for (i = 0; i < num_pages; i++, index++) { |
Michal Hocko | d1b5c56 | 2015-08-19 14:17:40 +0200 | [diff] [blame] | 4968 | p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL); |
Josef Bacik | 4804b38 | 2012-10-05 16:43:45 -0400 | [diff] [blame] | 4969 | if (!p) |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4970 | goto free_eb; |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4971 | |
| 4972 | spin_lock(&mapping->private_lock); |
| 4973 | if (PagePrivate(p)) { |
| 4974 | /* |
| 4975 | * We could have already allocated an eb for this page |
| 4976 | * and attached one so lets see if we can get a ref on |
| 4977 | * the existing eb, and if we can we know it's good and |
| 4978 | * we can just return that one, else we know we can just |
| 4979 | * overwrite page->private. |
| 4980 | */ |
| 4981 | exists = (struct extent_buffer *)p->private; |
| 4982 | if (atomic_inc_not_zero(&exists->refs)) { |
| 4983 | spin_unlock(&mapping->private_lock); |
| 4984 | unlock_page(p); |
Josef Bacik | 17de39a | 2012-05-04 15:16:06 -0400 | [diff] [blame] | 4985 | page_cache_release(p); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4986 | mark_extent_buffer_accessed(exists, p); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4987 | goto free_eb; |
| 4988 | } |
Omar Sandoval | 5ca64f4 | 2015-02-24 02:47:05 -0800 | [diff] [blame] | 4989 | exists = NULL; |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4990 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4991 | /* |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4992 | * Do this so attach doesn't complain and we need to |
| 4993 | * drop the ref the old guy had. |
| 4994 | */ |
| 4995 | ClearPagePrivate(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4996 | WARN_ON(PageDirty(p)); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4997 | page_cache_release(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4998 | } |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4999 | attach_extent_buffer_page(eb, p); |
| 5000 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5001 | WARN_ON(PageDirty(p)); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5002 | eb->pages[i] = p; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5003 | if (!PageUptodate(p)) |
| 5004 | uptodate = 0; |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5005 | |
| 5006 | /* |
| 5007 | * see below about how we avoid a nasty race with release page |
| 5008 | * and why we unlock later |
| 5009 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5010 | } |
| 5011 | if (uptodate) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5012 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 5013 | again: |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5014 | ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); |
| 5015 | if (ret) |
| 5016 | goto free_eb; |
| 5017 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5018 | spin_lock(&fs_info->buffer_lock); |
| 5019 | ret = radix_tree_insert(&fs_info->buffer_radix, |
| 5020 | start >> PAGE_CACHE_SHIFT, eb); |
| 5021 | spin_unlock(&fs_info->buffer_lock); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 5022 | radix_tree_preload_end(); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5023 | if (ret == -EEXIST) { |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5024 | exists = find_extent_buffer(fs_info, start); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 5025 | if (exists) |
| 5026 | goto free_eb; |
| 5027 | else |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 5028 | goto again; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5029 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5030 | /* add one reference for the tree */ |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5031 | check_buffer_tree_ref(eb); |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 5032 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5033 | |
| 5034 | /* |
| 5035 | * there is a race where release page may have |
| 5036 | * tried to find this extent buffer in the radix |
| 5037 | * but failed. It will tell the VM it is safe to |
| 5038 | * reclaim the, and it will clear the page private bit. |
| 5039 | * We must make sure to set the page private bit properly |
| 5040 | * after the extent buffer is in the radix tree so |
| 5041 | * it doesn't get lost |
| 5042 | */ |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5043 | SetPageChecked(eb->pages[0]); |
| 5044 | for (i = 1; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5045 | p = eb->pages[i]; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5046 | ClearPageChecked(p); |
| 5047 | unlock_page(p); |
| 5048 | } |
| 5049 | unlock_page(eb->pages[0]); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5050 | return eb; |
| 5051 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5052 | free_eb: |
Omar Sandoval | 5ca64f4 | 2015-02-24 02:47:05 -0800 | [diff] [blame] | 5053 | WARN_ON(!atomic_dec_and_test(&eb->refs)); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5054 | for (i = 0; i < num_pages; i++) { |
| 5055 | if (eb->pages[i]) |
| 5056 | unlock_page(eb->pages[i]); |
| 5057 | } |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5058 | |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5059 | btrfs_release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5060 | return exists; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5061 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5062 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5063 | static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head) |
| 5064 | { |
| 5065 | struct extent_buffer *eb = |
| 5066 | container_of(head, struct extent_buffer, rcu_head); |
| 5067 | |
| 5068 | __free_extent_buffer(eb); |
| 5069 | } |
| 5070 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5071 | /* Expects to have eb->eb_lock already held */ |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5072 | static int release_extent_buffer(struct extent_buffer *eb) |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5073 | { |
| 5074 | WARN_ON(atomic_read(&eb->refs) == 0); |
| 5075 | if (atomic_dec_and_test(&eb->refs)) { |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 5076 | if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) { |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5077 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5078 | |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 5079 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5080 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5081 | spin_lock(&fs_info->buffer_lock); |
| 5082 | radix_tree_delete(&fs_info->buffer_radix, |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 5083 | eb->start >> PAGE_CACHE_SHIFT); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5084 | spin_unlock(&fs_info->buffer_lock); |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 5085 | } else { |
| 5086 | spin_unlock(&eb->refs_lock); |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 5087 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5088 | |
| 5089 | /* Should be safe to release our pages at this point */ |
David Sterba | a50924e | 2014-07-31 00:51:36 +0200 | [diff] [blame] | 5090 | btrfs_release_extent_buffer_page(eb); |
Josef Bacik | bcb7e44 | 2015-03-16 17:38:02 -0400 | [diff] [blame] | 5091 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 5092 | if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) { |
| 5093 | __free_extent_buffer(eb); |
| 5094 | return 1; |
| 5095 | } |
| 5096 | #endif |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5097 | call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 5098 | return 1; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5099 | } |
| 5100 | spin_unlock(&eb->refs_lock); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 5101 | |
| 5102 | return 0; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5103 | } |
| 5104 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5105 | void free_extent_buffer(struct extent_buffer *eb) |
| 5106 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 5107 | int refs; |
| 5108 | int old; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5109 | if (!eb) |
| 5110 | return; |
| 5111 | |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 5112 | while (1) { |
| 5113 | refs = atomic_read(&eb->refs); |
| 5114 | if (refs <= 3) |
| 5115 | break; |
| 5116 | old = atomic_cmpxchg(&eb->refs, refs, refs - 1); |
| 5117 | if (old == refs) |
| 5118 | return; |
| 5119 | } |
| 5120 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5121 | spin_lock(&eb->refs_lock); |
| 5122 | if (atomic_read(&eb->refs) == 2 && |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 5123 | test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) |
| 5124 | atomic_dec(&eb->refs); |
| 5125 | |
| 5126 | if (atomic_read(&eb->refs) == 2 && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5127 | test_bit(EXTENT_BUFFER_STALE, &eb->bflags) && |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5128 | !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5129 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 5130 | atomic_dec(&eb->refs); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5131 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5132 | /* |
| 5133 | * I know this is terrible, but it's temporary until we stop tracking |
| 5134 | * the uptodate bits and such for the extent buffers. |
| 5135 | */ |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5136 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5137 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5138 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5139 | void free_extent_buffer_stale(struct extent_buffer *eb) |
| 5140 | { |
| 5141 | if (!eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5142 | return; |
| 5143 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5144 | spin_lock(&eb->refs_lock); |
| 5145 | set_bit(EXTENT_BUFFER_STALE, &eb->bflags); |
| 5146 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5147 | if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5148 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 5149 | atomic_dec(&eb->refs); |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5150 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5151 | } |
| 5152 | |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 5153 | void clear_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5154 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5155 | unsigned long i; |
| 5156 | unsigned long num_pages; |
| 5157 | struct page *page; |
| 5158 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5159 | num_pages = num_extent_pages(eb->start, eb->len); |
| 5160 | |
| 5161 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5162 | page = eb->pages[i]; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5163 | if (!PageDirty(page)) |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 5164 | continue; |
| 5165 | |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 5166 | lock_page(page); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5167 | WARN_ON(!PagePrivate(page)); |
| 5168 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5169 | clear_page_dirty_for_io(page); |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 5170 | spin_lock_irq(&page->mapping->tree_lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5171 | if (!PageDirty(page)) { |
| 5172 | radix_tree_tag_clear(&page->mapping->page_tree, |
| 5173 | page_index(page), |
| 5174 | PAGECACHE_TAG_DIRTY); |
| 5175 | } |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 5176 | spin_unlock_irq(&page->mapping->tree_lock); |
Chris Mason | bf0da8c | 2011-11-04 12:29:37 -0400 | [diff] [blame] | 5177 | ClearPageError(page); |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 5178 | unlock_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5179 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5180 | WARN_ON(atomic_read(&eb->refs) == 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5181 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5182 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5183 | int set_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5184 | { |
| 5185 | unsigned long i; |
| 5186 | unsigned long num_pages; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5187 | int was_dirty = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5188 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5189 | check_buffer_tree_ref(eb); |
| 5190 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5191 | was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5192 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5193 | num_pages = num_extent_pages(eb->start, eb->len); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5194 | WARN_ON(atomic_read(&eb->refs) == 0); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5195 | WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)); |
| 5196 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5197 | for (i = 0; i < num_pages; i++) |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5198 | set_page_dirty(eb->pages[i]); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5199 | return was_dirty; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5200 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5201 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5202 | int clear_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5203 | { |
| 5204 | unsigned long i; |
| 5205 | struct page *page; |
| 5206 | unsigned long num_pages; |
| 5207 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5208 | clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5209 | num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5210 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5211 | page = eb->pages[i]; |
Chris Mason | 33958dc | 2008-07-30 10:29:12 -0400 | [diff] [blame] | 5212 | if (page) |
| 5213 | ClearPageUptodate(page); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5214 | } |
| 5215 | return 0; |
| 5216 | } |
| 5217 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5218 | int set_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5219 | { |
| 5220 | unsigned long i; |
| 5221 | struct page *page; |
| 5222 | unsigned long num_pages; |
| 5223 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5224 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5225 | num_pages = num_extent_pages(eb->start, eb->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5226 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5227 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5228 | SetPageUptodate(page); |
| 5229 | } |
| 5230 | return 0; |
| 5231 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5232 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5233 | int extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5234 | { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5235 | return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5236 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5237 | |
| 5238 | int read_extent_buffer_pages(struct extent_io_tree *tree, |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 5239 | struct extent_buffer *eb, u64 start, int wait, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5240 | get_extent_t *get_extent, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5241 | { |
| 5242 | unsigned long i; |
| 5243 | unsigned long start_i; |
| 5244 | struct page *page; |
| 5245 | int err; |
| 5246 | int ret = 0; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5247 | int locked_pages = 0; |
| 5248 | int all_uptodate = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5249 | unsigned long num_pages; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5250 | unsigned long num_reads = 0; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5251 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5252 | unsigned long bio_flags = 0; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5253 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5254 | if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5255 | return 0; |
| 5256 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5257 | if (start) { |
| 5258 | WARN_ON(start < eb->start); |
| 5259 | start_i = (start >> PAGE_CACHE_SHIFT) - |
| 5260 | (eb->start >> PAGE_CACHE_SHIFT); |
| 5261 | } else { |
| 5262 | start_i = 0; |
| 5263 | } |
| 5264 | |
| 5265 | num_pages = num_extent_pages(eb->start, eb->len); |
| 5266 | for (i = start_i; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5267 | page = eb->pages[i]; |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 5268 | if (wait == WAIT_NONE) { |
David Woodhouse | 2db0496 | 2008-08-07 11:19:43 -0400 | [diff] [blame] | 5269 | if (!trylock_page(page)) |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5270 | goto unlock_exit; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5271 | } else { |
| 5272 | lock_page(page); |
| 5273 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5274 | locked_pages++; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5275 | if (!PageUptodate(page)) { |
| 5276 | num_reads++; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5277 | all_uptodate = 0; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5278 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5279 | } |
| 5280 | if (all_uptodate) { |
| 5281 | if (start_i == 0) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5282 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5283 | goto unlock_exit; |
| 5284 | } |
| 5285 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 5286 | clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 5287 | eb->read_mirror = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5288 | atomic_set(&eb->io_pages, num_reads); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5289 | for (i = start_i; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5290 | page = eb->pages[i]; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5291 | if (!PageUptodate(page)) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5292 | ClearPageError(page); |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5293 | err = __extent_read_full_page(tree, page, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5294 | get_extent, &bio, |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 5295 | mirror_num, &bio_flags, |
| 5296 | READ | REQ_META); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5297 | if (err) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5298 | ret = err; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5299 | } else { |
| 5300 | unlock_page(page); |
| 5301 | } |
| 5302 | } |
| 5303 | |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 5304 | if (bio) { |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 5305 | err = submit_one_bio(READ | REQ_META, bio, mirror_num, |
| 5306 | bio_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5307 | if (err) |
| 5308 | return err; |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 5309 | } |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5310 | |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 5311 | if (ret || wait != WAIT_COMPLETE) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5312 | return ret; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5313 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5314 | for (i = start_i; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5315 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5316 | wait_on_page_locked(page); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5317 | if (!PageUptodate(page)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5318 | ret = -EIO; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5319 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5320 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5321 | return ret; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5322 | |
| 5323 | unlock_exit: |
| 5324 | i = start_i; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5325 | while (locked_pages > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5326 | page = eb->pages[i]; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5327 | i++; |
| 5328 | unlock_page(page); |
| 5329 | locked_pages--; |
| 5330 | } |
| 5331 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5332 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5333 | |
| 5334 | void read_extent_buffer(struct extent_buffer *eb, void *dstv, |
| 5335 | unsigned long start, |
| 5336 | unsigned long len) |
| 5337 | { |
| 5338 | size_t cur; |
| 5339 | size_t offset; |
| 5340 | struct page *page; |
| 5341 | char *kaddr; |
| 5342 | char *dst = (char *)dstv; |
| 5343 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 5344 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5345 | |
| 5346 | WARN_ON(start > eb->len); |
| 5347 | WARN_ON(start + len > eb->start + eb->len); |
| 5348 | |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 5349 | offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5350 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5351 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5352 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5353 | |
| 5354 | cur = min(len, (PAGE_CACHE_SIZE - offset)); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5355 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5356 | memcpy(dst, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5357 | |
| 5358 | dst += cur; |
| 5359 | len -= cur; |
| 5360 | offset = 0; |
| 5361 | i++; |
| 5362 | } |
| 5363 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5364 | |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5365 | int read_extent_buffer_to_user(struct extent_buffer *eb, void __user *dstv, |
| 5366 | unsigned long start, |
| 5367 | unsigned long len) |
| 5368 | { |
| 5369 | size_t cur; |
| 5370 | size_t offset; |
| 5371 | struct page *page; |
| 5372 | char *kaddr; |
| 5373 | char __user *dst = (char __user *)dstv; |
| 5374 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 5375 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 5376 | int ret = 0; |
| 5377 | |
| 5378 | WARN_ON(start > eb->len); |
| 5379 | WARN_ON(start + len > eb->start + eb->len); |
| 5380 | |
| 5381 | offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1); |
| 5382 | |
| 5383 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5384 | page = eb->pages[i]; |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5385 | |
| 5386 | cur = min(len, (PAGE_CACHE_SIZE - offset)); |
| 5387 | kaddr = page_address(page); |
| 5388 | if (copy_to_user(dst, kaddr + offset, cur)) { |
| 5389 | ret = -EFAULT; |
| 5390 | break; |
| 5391 | } |
| 5392 | |
| 5393 | dst += cur; |
| 5394 | len -= cur; |
| 5395 | offset = 0; |
| 5396 | i++; |
| 5397 | } |
| 5398 | |
| 5399 | return ret; |
| 5400 | } |
| 5401 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5402 | int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start, |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5403 | unsigned long min_len, char **map, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5404 | unsigned long *map_start, |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5405 | unsigned long *map_len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5406 | { |
| 5407 | size_t offset = start & (PAGE_CACHE_SIZE - 1); |
| 5408 | char *kaddr; |
| 5409 | struct page *p; |
| 5410 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 5411 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 5412 | unsigned long end_i = (start_offset + start + min_len - 1) >> |
| 5413 | PAGE_CACHE_SHIFT; |
| 5414 | |
| 5415 | if (i != end_i) |
| 5416 | return -EINVAL; |
| 5417 | |
| 5418 | if (i == 0) { |
| 5419 | offset = start_offset; |
| 5420 | *map_start = 0; |
| 5421 | } else { |
| 5422 | offset = 0; |
| 5423 | *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset; |
| 5424 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5425 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5426 | if (start + min_len > eb->len) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 5427 | WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, " |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 5428 | "wanted %lu %lu\n", |
| 5429 | eb->start, eb->len, start, min_len); |
Josef Bacik | 85026533 | 2011-03-15 14:52:12 -0400 | [diff] [blame] | 5430 | return -EINVAL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5431 | } |
| 5432 | |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5433 | p = eb->pages[i]; |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5434 | kaddr = page_address(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5435 | *map = kaddr + offset; |
| 5436 | *map_len = PAGE_CACHE_SIZE - offset; |
| 5437 | return 0; |
| 5438 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5439 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5440 | int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv, |
| 5441 | unsigned long start, |
| 5442 | unsigned long len) |
| 5443 | { |
| 5444 | size_t cur; |
| 5445 | size_t offset; |
| 5446 | struct page *page; |
| 5447 | char *kaddr; |
| 5448 | char *ptr = (char *)ptrv; |
| 5449 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 5450 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 5451 | int ret = 0; |
| 5452 | |
| 5453 | WARN_ON(start > eb->len); |
| 5454 | WARN_ON(start + len > eb->start + eb->len); |
| 5455 | |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 5456 | offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5457 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5458 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5459 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5460 | |
| 5461 | cur = min(len, (PAGE_CACHE_SIZE - offset)); |
| 5462 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5463 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5464 | ret = memcmp(ptr, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5465 | if (ret) |
| 5466 | break; |
| 5467 | |
| 5468 | ptr += cur; |
| 5469 | len -= cur; |
| 5470 | offset = 0; |
| 5471 | i++; |
| 5472 | } |
| 5473 | return ret; |
| 5474 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5475 | |
| 5476 | void write_extent_buffer(struct extent_buffer *eb, const void *srcv, |
| 5477 | unsigned long start, unsigned long len) |
| 5478 | { |
| 5479 | size_t cur; |
| 5480 | size_t offset; |
| 5481 | struct page *page; |
| 5482 | char *kaddr; |
| 5483 | char *src = (char *)srcv; |
| 5484 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 5485 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 5486 | |
| 5487 | WARN_ON(start > eb->len); |
| 5488 | WARN_ON(start + len > eb->start + eb->len); |
| 5489 | |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 5490 | offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5491 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5492 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5493 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5494 | WARN_ON(!PageUptodate(page)); |
| 5495 | |
| 5496 | cur = min(len, PAGE_CACHE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5497 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5498 | memcpy(kaddr + offset, src, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5499 | |
| 5500 | src += cur; |
| 5501 | len -= cur; |
| 5502 | offset = 0; |
| 5503 | i++; |
| 5504 | } |
| 5505 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5506 | |
| 5507 | void memset_extent_buffer(struct extent_buffer *eb, char c, |
| 5508 | unsigned long start, unsigned long len) |
| 5509 | { |
| 5510 | size_t cur; |
| 5511 | size_t offset; |
| 5512 | struct page *page; |
| 5513 | char *kaddr; |
| 5514 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 5515 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; |
| 5516 | |
| 5517 | WARN_ON(start > eb->len); |
| 5518 | WARN_ON(start + len > eb->start + eb->len); |
| 5519 | |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 5520 | offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5521 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5522 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5523 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5524 | WARN_ON(!PageUptodate(page)); |
| 5525 | |
| 5526 | cur = min(len, PAGE_CACHE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5527 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5528 | memset(kaddr + offset, c, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5529 | |
| 5530 | len -= cur; |
| 5531 | offset = 0; |
| 5532 | i++; |
| 5533 | } |
| 5534 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5535 | |
| 5536 | void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src, |
| 5537 | unsigned long dst_offset, unsigned long src_offset, |
| 5538 | unsigned long len) |
| 5539 | { |
| 5540 | u64 dst_len = dst->len; |
| 5541 | size_t cur; |
| 5542 | size_t offset; |
| 5543 | struct page *page; |
| 5544 | char *kaddr; |
| 5545 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 5546 | unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT; |
| 5547 | |
| 5548 | WARN_ON(src->len != dst_len); |
| 5549 | |
| 5550 | offset = (start_offset + dst_offset) & |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 5551 | (PAGE_CACHE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5552 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5553 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5554 | page = dst->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5555 | WARN_ON(!PageUptodate(page)); |
| 5556 | |
| 5557 | cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset)); |
| 5558 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5559 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5560 | read_extent_buffer(src, kaddr + offset, src_offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5561 | |
| 5562 | src_offset += cur; |
| 5563 | len -= cur; |
| 5564 | offset = 0; |
| 5565 | i++; |
| 5566 | } |
| 5567 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5568 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5569 | static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len) |
| 5570 | { |
| 5571 | unsigned long distance = (src > dst) ? src - dst : dst - src; |
| 5572 | return distance < len; |
| 5573 | } |
| 5574 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5575 | static void copy_pages(struct page *dst_page, struct page *src_page, |
| 5576 | unsigned long dst_off, unsigned long src_off, |
| 5577 | unsigned long len) |
| 5578 | { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5579 | char *dst_kaddr = page_address(dst_page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5580 | char *src_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5581 | int must_memmove = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5582 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5583 | if (dst_page != src_page) { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5584 | src_kaddr = page_address(src_page); |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5585 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5586 | src_kaddr = dst_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5587 | if (areas_overlap(src_off, dst_off, len)) |
| 5588 | must_memmove = 1; |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5589 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5590 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5591 | if (must_memmove) |
| 5592 | memmove(dst_kaddr + dst_off, src_kaddr + src_off, len); |
| 5593 | else |
| 5594 | memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5595 | } |
| 5596 | |
| 5597 | void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 5598 | unsigned long src_offset, unsigned long len) |
| 5599 | { |
| 5600 | size_t cur; |
| 5601 | size_t dst_off_in_page; |
| 5602 | size_t src_off_in_page; |
| 5603 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 5604 | unsigned long dst_i; |
| 5605 | unsigned long src_i; |
| 5606 | |
| 5607 | if (src_offset + len > dst->len) { |
David Sterba | f14d104 | 2015-10-08 11:37:06 +0200 | [diff] [blame] | 5608 | btrfs_err(dst->fs_info, |
| 5609 | "memmove bogus src_offset %lu move " |
| 5610 | "len %lu dst len %lu", src_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5611 | BUG_ON(1); |
| 5612 | } |
| 5613 | if (dst_offset + len > dst->len) { |
David Sterba | f14d104 | 2015-10-08 11:37:06 +0200 | [diff] [blame] | 5614 | btrfs_err(dst->fs_info, |
| 5615 | "memmove bogus dst_offset %lu move " |
| 5616 | "len %lu dst len %lu", dst_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5617 | BUG_ON(1); |
| 5618 | } |
| 5619 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5620 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5621 | dst_off_in_page = (start_offset + dst_offset) & |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 5622 | (PAGE_CACHE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5623 | src_off_in_page = (start_offset + src_offset) & |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 5624 | (PAGE_CACHE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5625 | |
| 5626 | dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT; |
| 5627 | src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT; |
| 5628 | |
| 5629 | cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - |
| 5630 | src_off_in_page)); |
| 5631 | cur = min_t(unsigned long, cur, |
| 5632 | (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page)); |
| 5633 | |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5634 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5635 | dst_off_in_page, src_off_in_page, cur); |
| 5636 | |
| 5637 | src_offset += cur; |
| 5638 | dst_offset += cur; |
| 5639 | len -= cur; |
| 5640 | } |
| 5641 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5642 | |
| 5643 | void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 5644 | unsigned long src_offset, unsigned long len) |
| 5645 | { |
| 5646 | size_t cur; |
| 5647 | size_t dst_off_in_page; |
| 5648 | size_t src_off_in_page; |
| 5649 | unsigned long dst_end = dst_offset + len - 1; |
| 5650 | unsigned long src_end = src_offset + len - 1; |
| 5651 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 5652 | unsigned long dst_i; |
| 5653 | unsigned long src_i; |
| 5654 | |
| 5655 | if (src_offset + len > dst->len) { |
David Sterba | f14d104 | 2015-10-08 11:37:06 +0200 | [diff] [blame] | 5656 | btrfs_err(dst->fs_info, "memmove bogus src_offset %lu move " |
| 5657 | "len %lu len %lu", src_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5658 | BUG_ON(1); |
| 5659 | } |
| 5660 | if (dst_offset + len > dst->len) { |
David Sterba | f14d104 | 2015-10-08 11:37:06 +0200 | [diff] [blame] | 5661 | btrfs_err(dst->fs_info, "memmove bogus dst_offset %lu move " |
| 5662 | "len %lu len %lu", dst_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5663 | BUG_ON(1); |
| 5664 | } |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5665 | if (dst_offset < src_offset) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5666 | memcpy_extent_buffer(dst, dst_offset, src_offset, len); |
| 5667 | return; |
| 5668 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5669 | while (len > 0) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5670 | dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT; |
| 5671 | src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT; |
| 5672 | |
| 5673 | dst_off_in_page = (start_offset + dst_end) & |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 5674 | (PAGE_CACHE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5675 | src_off_in_page = (start_offset + src_end) & |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 5676 | (PAGE_CACHE_SIZE - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5677 | |
| 5678 | cur = min_t(unsigned long, len, src_off_in_page + 1); |
| 5679 | cur = min(cur, dst_off_in_page + 1); |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5680 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5681 | dst_off_in_page - cur + 1, |
| 5682 | src_off_in_page - cur + 1, cur); |
| 5683 | |
| 5684 | dst_end -= cur; |
| 5685 | src_end -= cur; |
| 5686 | len -= cur; |
| 5687 | } |
| 5688 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5689 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5690 | int try_release_extent_buffer(struct page *page) |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5691 | { |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5692 | struct extent_buffer *eb; |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5693 | |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5694 | /* |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5695 | * We need to make sure noboody is attaching this page to an eb right |
| 5696 | * now. |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5697 | */ |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5698 | spin_lock(&page->mapping->private_lock); |
| 5699 | if (!PagePrivate(page)) { |
| 5700 | spin_unlock(&page->mapping->private_lock); |
| 5701 | return 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5702 | } |
| 5703 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5704 | eb = (struct extent_buffer *)page->private; |
| 5705 | BUG_ON(!eb); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5706 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5707 | /* |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5708 | * This is a little awful but should be ok, we need to make sure that |
| 5709 | * the eb doesn't disappear out from under us while we're looking at |
| 5710 | * this page. |
| 5711 | */ |
| 5712 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5713 | if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5714 | spin_unlock(&eb->refs_lock); |
| 5715 | spin_unlock(&page->mapping->private_lock); |
| 5716 | return 0; |
| 5717 | } |
| 5718 | spin_unlock(&page->mapping->private_lock); |
| 5719 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5720 | /* |
| 5721 | * If tree ref isn't set then we know the ref on this eb is a real ref, |
| 5722 | * so just return, this page will likely be freed soon anyway. |
| 5723 | */ |
| 5724 | if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { |
| 5725 | spin_unlock(&eb->refs_lock); |
| 5726 | return 0; |
| 5727 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5728 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5729 | return release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5730 | } |