Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 2 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3 | #include <linux/bitops.h> |
| 4 | #include <linux/slab.h> |
| 5 | #include <linux/bio.h> |
| 6 | #include <linux/mm.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7 | #include <linux/pagemap.h> |
| 8 | #include <linux/page-flags.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 9 | #include <linux/spinlock.h> |
| 10 | #include <linux/blkdev.h> |
| 11 | #include <linux/swap.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 12 | #include <linux/writeback.h> |
| 13 | #include <linux/pagevec.h> |
Linus Torvalds | 268bb0c | 2011-05-20 12:50:29 -0700 | [diff] [blame] | 14 | #include <linux/prefetch.h> |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 15 | #include <linux/cleancache.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 16 | #include "extent_io.h" |
| 17 | #include "extent_map.h" |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 18 | #include "ctree.h" |
| 19 | #include "btrfs_inode.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 20 | #include "volumes.h" |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 21 | #include "check-integrity.h" |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 22 | #include "locking.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 23 | #include "rcu-string.h" |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 24 | #include "backref.h" |
David Sterba | 6af49db | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 25 | #include "disk-io.h" |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 26 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 27 | static struct kmem_cache *extent_state_cache; |
| 28 | static struct kmem_cache *extent_buffer_cache; |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 29 | static struct bio_set btrfs_bioset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 30 | |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 31 | static inline bool extent_state_in_tree(const struct extent_state *state) |
| 32 | { |
| 33 | return !RB_EMPTY_NODE(&state->rb_node); |
| 34 | } |
| 35 | |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 36 | #ifdef CONFIG_BTRFS_DEBUG |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 37 | static LIST_HEAD(buffers); |
| 38 | static LIST_HEAD(states); |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 39 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 40 | static DEFINE_SPINLOCK(leak_lock); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 41 | |
| 42 | static inline |
| 43 | void btrfs_leak_debug_add(struct list_head *new, struct list_head *head) |
| 44 | { |
| 45 | unsigned long flags; |
| 46 | |
| 47 | spin_lock_irqsave(&leak_lock, flags); |
| 48 | list_add(new, head); |
| 49 | spin_unlock_irqrestore(&leak_lock, flags); |
| 50 | } |
| 51 | |
| 52 | static inline |
| 53 | void btrfs_leak_debug_del(struct list_head *entry) |
| 54 | { |
| 55 | unsigned long flags; |
| 56 | |
| 57 | spin_lock_irqsave(&leak_lock, flags); |
| 58 | list_del(entry); |
| 59 | spin_unlock_irqrestore(&leak_lock, flags); |
| 60 | } |
| 61 | |
| 62 | static inline |
| 63 | void btrfs_leak_debug_check(void) |
| 64 | { |
| 65 | struct extent_state *state; |
| 66 | struct extent_buffer *eb; |
| 67 | |
| 68 | while (!list_empty(&states)) { |
| 69 | state = list_entry(states.next, struct extent_state, leak_list); |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 70 | 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] | 71 | state->start, state->end, state->state, |
| 72 | extent_state_in_tree(state), |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 73 | refcount_read(&state->refs)); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 74 | list_del(&state->leak_list); |
| 75 | kmem_cache_free(extent_state_cache, state); |
| 76 | } |
| 77 | |
| 78 | while (!list_empty(&buffers)) { |
| 79 | eb = list_entry(buffers.next, struct extent_buffer, leak_list); |
Liu Bo | af2679e | 2018-01-25 11:02:48 -0700 | [diff] [blame] | 80 | pr_err("BTRFS: buffer leak start %llu len %lu refs %d bflags %lu\n", |
| 81 | eb->start, eb->len, atomic_read(&eb->refs), eb->bflags); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 82 | list_del(&eb->leak_list); |
| 83 | kmem_cache_free(extent_buffer_cache, eb); |
| 84 | } |
| 85 | } |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 86 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 87 | #define btrfs_debug_check_extent_io_range(tree, start, end) \ |
| 88 | __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end)) |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 89 | static inline void __btrfs_debug_check_extent_io_range(const char *caller, |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 90 | struct extent_io_tree *tree, u64 start, u64 end) |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 91 | { |
Nikolay Borisov | 65a680f | 2018-11-01 14:09:49 +0200 | [diff] [blame] | 92 | struct inode *inode = tree->private_data; |
| 93 | u64 isize; |
| 94 | |
| 95 | if (!inode || !is_data_inode(inode)) |
| 96 | return; |
| 97 | |
| 98 | isize = i_size_read(inode); |
| 99 | if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) { |
| 100 | btrfs_debug_rl(BTRFS_I(inode)->root->fs_info, |
| 101 | "%s: ino %llu isize %llu odd range [%llu,%llu]", |
| 102 | caller, btrfs_ino(BTRFS_I(inode)), isize, start, end); |
| 103 | } |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 104 | } |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 105 | #else |
| 106 | #define btrfs_leak_debug_add(new, head) do {} while (0) |
| 107 | #define btrfs_leak_debug_del(entry) do {} while (0) |
| 108 | #define btrfs_leak_debug_check() do {} while (0) |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 109 | #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] | 110 | #endif |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 111 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 112 | #define BUFFER_LRU_MAX 64 |
| 113 | |
| 114 | struct tree_entry { |
| 115 | u64 start; |
| 116 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 117 | struct rb_node rb_node; |
| 118 | }; |
| 119 | |
| 120 | struct extent_page_data { |
| 121 | struct bio *bio; |
| 122 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 123 | /* tells writepage not to lock the state bits for this range |
| 124 | * it still does the unlocking |
| 125 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 126 | unsigned int extent_locked:1; |
| 127 | |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 128 | /* tells the submit_bio code to use REQ_SYNC */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 129 | unsigned int sync_io:1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 130 | }; |
| 131 | |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 132 | static int add_extent_changeset(struct extent_state *state, unsigned bits, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 133 | struct extent_changeset *changeset, |
| 134 | int set) |
| 135 | { |
| 136 | int ret; |
| 137 | |
| 138 | if (!changeset) |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 139 | return 0; |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 140 | if (set && (state->state & bits) == bits) |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 141 | return 0; |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 142 | if (!set && (state->state & bits) == 0) |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 143 | return 0; |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 144 | changeset->bytes_changed += state->end - state->start + 1; |
David Sterba | 53d3235 | 2017-02-13 13:42:29 +0100 | [diff] [blame] | 145 | ret = ulist_add(&changeset->range_changed, state->start, state->end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 146 | GFP_ATOMIC); |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 147 | return ret; |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 148 | } |
| 149 | |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 150 | static int __must_check submit_one_bio(struct bio *bio, int mirror_num, |
| 151 | unsigned long bio_flags) |
| 152 | { |
| 153 | blk_status_t ret = 0; |
| 154 | struct bio_vec *bvec = bio_last_bvec_all(bio); |
Linus Torvalds | 80201fe | 2019-03-08 14:12:17 -0800 | [diff] [blame] | 155 | struct bio_vec bv; |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 156 | struct extent_io_tree *tree = bio->bi_private; |
| 157 | u64 start; |
| 158 | |
Linus Torvalds | 80201fe | 2019-03-08 14:12:17 -0800 | [diff] [blame] | 159 | mp_bvec_last_segment(bvec, &bv); |
| 160 | start = page_offset(bv.bv_page) + bv.bv_offset; |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 161 | |
| 162 | bio->bi_private = NULL; |
| 163 | |
| 164 | if (tree->ops) |
| 165 | ret = tree->ops->submit_bio_hook(tree->private_data, bio, |
| 166 | mirror_num, bio_flags, start); |
| 167 | else |
| 168 | btrfsic_submit_bio(bio); |
| 169 | |
| 170 | return blk_status_to_errno(ret); |
| 171 | } |
| 172 | |
| 173 | static void flush_write_bio(struct extent_page_data *epd) |
| 174 | { |
| 175 | if (epd->bio) { |
| 176 | int ret; |
| 177 | |
| 178 | ret = submit_one_bio(epd->bio, 0, 0); |
| 179 | BUG_ON(ret < 0); /* -ENOMEM */ |
| 180 | epd->bio = NULL; |
| 181 | } |
| 182 | } |
David Sterba | e2932ee | 2017-06-23 04:16:17 +0200 | [diff] [blame] | 183 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 184 | int __init extent_io_init(void) |
| 185 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 186 | extent_state_cache = kmem_cache_create("btrfs_extent_state", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 187 | sizeof(struct extent_state), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 188 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 189 | if (!extent_state_cache) |
| 190 | return -ENOMEM; |
| 191 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 192 | extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 193 | sizeof(struct extent_buffer), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 194 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 195 | if (!extent_buffer_cache) |
| 196 | goto free_state_cache; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 197 | |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 198 | if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE, |
| 199 | offsetof(struct btrfs_io_bio, bio), |
| 200 | BIOSET_NEED_BVECS)) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 201 | goto free_buffer_cache; |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 202 | |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 203 | if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE)) |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 204 | goto free_bioset; |
| 205 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 206 | return 0; |
| 207 | |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 208 | free_bioset: |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 209 | bioset_exit(&btrfs_bioset); |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 210 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 211 | free_buffer_cache: |
| 212 | kmem_cache_destroy(extent_buffer_cache); |
| 213 | extent_buffer_cache = NULL; |
| 214 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 215 | free_state_cache: |
| 216 | kmem_cache_destroy(extent_state_cache); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 217 | extent_state_cache = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 218 | return -ENOMEM; |
| 219 | } |
| 220 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 221 | void __cold extent_io_exit(void) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 222 | { |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 223 | btrfs_leak_debug_check(); |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 224 | |
| 225 | /* |
| 226 | * Make sure all delayed rcu free are flushed before we |
| 227 | * destroy caches. |
| 228 | */ |
| 229 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 230 | kmem_cache_destroy(extent_state_cache); |
| 231 | kmem_cache_destroy(extent_buffer_cache); |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 232 | bioset_exit(&btrfs_bioset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 233 | } |
| 234 | |
Qu Wenruo | c258d6e | 2019-03-01 10:47:58 +0800 | [diff] [blame] | 235 | void extent_io_tree_init(struct btrfs_fs_info *fs_info, |
Qu Wenruo | 43eb5f2 | 2019-03-01 10:47:59 +0800 | [diff] [blame] | 236 | struct extent_io_tree *tree, unsigned int owner, |
| 237 | void *private_data) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 238 | { |
Qu Wenruo | c258d6e | 2019-03-01 10:47:58 +0800 | [diff] [blame] | 239 | tree->fs_info = fs_info; |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 240 | tree->state = RB_ROOT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 241 | tree->ops = NULL; |
| 242 | tree->dirty_bytes = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 243 | spin_lock_init(&tree->lock); |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 244 | tree->private_data = private_data; |
Qu Wenruo | 43eb5f2 | 2019-03-01 10:47:59 +0800 | [diff] [blame] | 245 | tree->owner = owner; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 246 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 247 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 248 | static struct extent_state *alloc_extent_state(gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 249 | { |
| 250 | struct extent_state *state; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 251 | |
Michal Hocko | 3ba7ab2 | 2017-01-09 15:39:02 +0100 | [diff] [blame] | 252 | /* |
| 253 | * The given mask might be not appropriate for the slab allocator, |
| 254 | * drop the unsupported bits |
| 255 | */ |
| 256 | mask &= ~(__GFP_DMA32|__GFP_HIGHMEM); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 257 | state = kmem_cache_alloc(extent_state_cache, mask); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 258 | if (!state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 259 | return state; |
| 260 | state->state = 0; |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 261 | state->failrec = NULL; |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 262 | RB_CLEAR_NODE(&state->rb_node); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 263 | btrfs_leak_debug_add(&state->leak_list, &states); |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 264 | refcount_set(&state->refs, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 265 | init_waitqueue_head(&state->wq); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 266 | trace_alloc_extent_state(state, mask, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 267 | return state; |
| 268 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 269 | |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 270 | void free_extent_state(struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 271 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 272 | if (!state) |
| 273 | return; |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 274 | if (refcount_dec_and_test(&state->refs)) { |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 275 | WARN_ON(extent_state_in_tree(state)); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 276 | btrfs_leak_debug_del(&state->leak_list); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 277 | trace_free_extent_state(state, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 278 | kmem_cache_free(extent_state_cache, state); |
| 279 | } |
| 280 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 281 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 282 | static struct rb_node *tree_insert(struct rb_root *root, |
| 283 | struct rb_node *search_start, |
| 284 | u64 offset, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 285 | struct rb_node *node, |
| 286 | struct rb_node ***p_in, |
| 287 | struct rb_node **parent_in) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 288 | { |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 289 | struct rb_node **p; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 290 | struct rb_node *parent = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 291 | struct tree_entry *entry; |
| 292 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 293 | if (p_in && parent_in) { |
| 294 | p = *p_in; |
| 295 | parent = *parent_in; |
| 296 | goto do_insert; |
| 297 | } |
| 298 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 299 | p = search_start ? &search_start : &root->rb_node; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 300 | while (*p) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 301 | parent = *p; |
| 302 | entry = rb_entry(parent, struct tree_entry, rb_node); |
| 303 | |
| 304 | if (offset < entry->start) |
| 305 | p = &(*p)->rb_left; |
| 306 | else if (offset > entry->end) |
| 307 | p = &(*p)->rb_right; |
| 308 | else |
| 309 | return parent; |
| 310 | } |
| 311 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 312 | do_insert: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 313 | rb_link_node(node, parent, p); |
| 314 | rb_insert_color(node, root); |
| 315 | return NULL; |
| 316 | } |
| 317 | |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 318 | 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] | 319 | struct rb_node **next_ret, |
Nikolay Borisov | 352646c | 2019-01-30 16:51:00 +0200 | [diff] [blame] | 320 | struct rb_node **prev_ret, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 321 | struct rb_node ***p_ret, |
| 322 | struct rb_node **parent_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 323 | { |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 324 | struct rb_root *root = &tree->state; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 325 | struct rb_node **n = &root->rb_node; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 326 | struct rb_node *prev = NULL; |
| 327 | struct rb_node *orig_prev = NULL; |
| 328 | struct tree_entry *entry; |
| 329 | struct tree_entry *prev_entry = NULL; |
| 330 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 331 | while (*n) { |
| 332 | prev = *n; |
| 333 | entry = rb_entry(prev, struct tree_entry, rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 334 | prev_entry = entry; |
| 335 | |
| 336 | if (offset < entry->start) |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 337 | n = &(*n)->rb_left; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 338 | else if (offset > entry->end) |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 339 | n = &(*n)->rb_right; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 340 | else |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 341 | return *n; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 342 | } |
| 343 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 344 | if (p_ret) |
| 345 | *p_ret = n; |
| 346 | if (parent_ret) |
| 347 | *parent_ret = prev; |
| 348 | |
Nikolay Borisov | 352646c | 2019-01-30 16:51:00 +0200 | [diff] [blame] | 349 | if (next_ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 350 | orig_prev = prev; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 351 | while (prev && offset > prev_entry->end) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 352 | prev = rb_next(prev); |
| 353 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| 354 | } |
Nikolay Borisov | 352646c | 2019-01-30 16:51:00 +0200 | [diff] [blame] | 355 | *next_ret = prev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 356 | prev = orig_prev; |
| 357 | } |
| 358 | |
Nikolay Borisov | 352646c | 2019-01-30 16:51:00 +0200 | [diff] [blame] | 359 | if (prev_ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 360 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 361 | while (prev && offset < prev_entry->start) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 362 | prev = rb_prev(prev); |
| 363 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| 364 | } |
Nikolay Borisov | 352646c | 2019-01-30 16:51:00 +0200 | [diff] [blame] | 365 | *prev_ret = prev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 366 | } |
| 367 | return NULL; |
| 368 | } |
| 369 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 370 | static inline struct rb_node * |
| 371 | tree_search_for_insert(struct extent_io_tree *tree, |
| 372 | u64 offset, |
| 373 | struct rb_node ***p_ret, |
| 374 | struct rb_node **parent_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 375 | { |
Nikolay Borisov | 352646c | 2019-01-30 16:51:00 +0200 | [diff] [blame] | 376 | struct rb_node *next= NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 377 | struct rb_node *ret; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 378 | |
Nikolay Borisov | 352646c | 2019-01-30 16:51:00 +0200 | [diff] [blame] | 379 | ret = __etree_search(tree, offset, &next, NULL, p_ret, parent_ret); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 380 | if (!ret) |
Nikolay Borisov | 352646c | 2019-01-30 16:51:00 +0200 | [diff] [blame] | 381 | return next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 382 | return ret; |
| 383 | } |
| 384 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 385 | static inline struct rb_node *tree_search(struct extent_io_tree *tree, |
| 386 | u64 offset) |
| 387 | { |
| 388 | return tree_search_for_insert(tree, offset, NULL, NULL); |
| 389 | } |
| 390 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 391 | /* |
| 392 | * utility function to look for merge candidates inside a given range. |
| 393 | * Any extents with matching state are merged together into a single |
| 394 | * extent in the tree. Extents with EXTENT_IO in their state field |
| 395 | * are not merged because the end_io handlers need to be able to do |
| 396 | * operations on them without sleeping (or doing allocations/splits). |
| 397 | * |
| 398 | * This should be called with the tree lock held. |
| 399 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 400 | static void merge_state(struct extent_io_tree *tree, |
| 401 | struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 402 | { |
| 403 | struct extent_state *other; |
| 404 | struct rb_node *other_node; |
| 405 | |
Nikolay Borisov | 8882679 | 2019-03-14 15:28:31 +0200 | [diff] [blame] | 406 | if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 407 | return; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 408 | |
| 409 | other_node = rb_prev(&state->rb_node); |
| 410 | if (other_node) { |
| 411 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 412 | if (other->end == state->start - 1 && |
| 413 | other->state == state->state) { |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 414 | if (tree->private_data && |
| 415 | is_data_inode(tree->private_data)) |
| 416 | btrfs_merge_delalloc_extent(tree->private_data, |
| 417 | state, other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 418 | state->start = other->start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 419 | rb_erase(&other->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 420 | RB_CLEAR_NODE(&other->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 421 | free_extent_state(other); |
| 422 | } |
| 423 | } |
| 424 | other_node = rb_next(&state->rb_node); |
| 425 | if (other_node) { |
| 426 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 427 | if (other->start == state->end + 1 && |
| 428 | other->state == state->state) { |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 429 | if (tree->private_data && |
| 430 | is_data_inode(tree->private_data)) |
| 431 | btrfs_merge_delalloc_extent(tree->private_data, |
| 432 | state, other); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 433 | state->end = other->end; |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 434 | rb_erase(&other->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 435 | RB_CLEAR_NODE(&other->rb_node); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 436 | free_extent_state(other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 437 | } |
| 438 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 439 | } |
| 440 | |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 441 | static void set_state_bits(struct extent_io_tree *tree, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 442 | struct extent_state *state, unsigned *bits, |
| 443 | struct extent_changeset *changeset); |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 444 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 445 | /* |
| 446 | * insert an extent_state struct into the tree. 'bits' are set on the |
| 447 | * struct before it is inserted. |
| 448 | * |
| 449 | * This may return -EEXIST if the extent is already there, in which case the |
| 450 | * state struct is freed. |
| 451 | * |
| 452 | * The tree lock is not taken internally. This is a utility function and |
| 453 | * probably isn't what you want to call (see set/clear_extent_bit). |
| 454 | */ |
| 455 | static int insert_state(struct extent_io_tree *tree, |
| 456 | struct extent_state *state, u64 start, u64 end, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 457 | struct rb_node ***p, |
| 458 | struct rb_node **parent, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 459 | unsigned *bits, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 460 | { |
| 461 | struct rb_node *node; |
| 462 | |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 463 | if (end < start) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 464 | WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 465 | end, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 466 | state->start = start; |
| 467 | state->end = end; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 468 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 469 | set_state_bits(tree, state, bits, changeset); |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 470 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 471 | node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 472 | if (node) { |
| 473 | struct extent_state *found; |
| 474 | found = rb_entry(node, struct extent_state, rb_node); |
Jeff Mahoney | 62e8557 | 2016-09-20 10:05:01 -0400 | [diff] [blame] | 475 | pr_err("BTRFS: found node %llu %llu on insert of %llu %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 476 | found->start, found->end, start, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 477 | return -EEXIST; |
| 478 | } |
| 479 | merge_state(tree, state); |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * split a given extent state struct in two, inserting the preallocated |
| 485 | * struct 'prealloc' as the newly created second half. 'split' indicates an |
| 486 | * offset inside 'orig' where it should be split. |
| 487 | * |
| 488 | * Before calling, |
| 489 | * the tree has 'orig' at [orig->start, orig->end]. After calling, there |
| 490 | * are two extent state structs in the tree: |
| 491 | * prealloc: [orig->start, split - 1] |
| 492 | * orig: [ split, orig->end ] |
| 493 | * |
| 494 | * The tree locks are not taken by this function. They need to be held |
| 495 | * by the caller. |
| 496 | */ |
| 497 | static int split_state(struct extent_io_tree *tree, struct extent_state *orig, |
| 498 | struct extent_state *prealloc, u64 split) |
| 499 | { |
| 500 | struct rb_node *node; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 501 | |
Nikolay Borisov | abbb55f | 2018-11-01 14:09:53 +0200 | [diff] [blame] | 502 | if (tree->private_data && is_data_inode(tree->private_data)) |
| 503 | btrfs_split_delalloc_extent(tree->private_data, orig, split); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 504 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 505 | prealloc->start = orig->start; |
| 506 | prealloc->end = split - 1; |
| 507 | prealloc->state = orig->state; |
| 508 | orig->start = split; |
| 509 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 510 | node = tree_insert(&tree->state, &orig->rb_node, prealloc->end, |
| 511 | &prealloc->rb_node, NULL, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 512 | if (node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 513 | free_extent_state(prealloc); |
| 514 | return -EEXIST; |
| 515 | } |
| 516 | return 0; |
| 517 | } |
| 518 | |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 519 | static struct extent_state *next_state(struct extent_state *state) |
| 520 | { |
| 521 | struct rb_node *next = rb_next(&state->rb_node); |
| 522 | if (next) |
| 523 | return rb_entry(next, struct extent_state, rb_node); |
| 524 | else |
| 525 | return NULL; |
| 526 | } |
| 527 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 528 | /* |
| 529 | * utility function to clear some bits in an extent state struct. |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 530 | * it will optionally wake up anyone waiting on this state (wake == 1). |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 531 | * |
| 532 | * If no bits are set on the state struct after clearing things, the |
| 533 | * struct is freed and removed from the tree |
| 534 | */ |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 535 | static struct extent_state *clear_state_bit(struct extent_io_tree *tree, |
| 536 | struct extent_state *state, |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 537 | unsigned *bits, int wake, |
| 538 | struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 539 | { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 540 | struct extent_state *next; |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 541 | unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS; |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 542 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 543 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 544 | if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 545 | u64 range = state->end - state->start + 1; |
| 546 | WARN_ON(range > tree->dirty_bytes); |
| 547 | tree->dirty_bytes -= range; |
| 548 | } |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 549 | |
| 550 | if (tree->private_data && is_data_inode(tree->private_data)) |
| 551 | btrfs_clear_delalloc_extent(tree->private_data, state, bits); |
| 552 | |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 553 | ret = add_extent_changeset(state, bits_to_clear, changeset, 0); |
| 554 | BUG_ON(ret < 0); |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 555 | state->state &= ~bits_to_clear; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 556 | if (wake) |
| 557 | wake_up(&state->wq); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 558 | if (state->state == 0) { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 559 | next = next_state(state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 560 | if (extent_state_in_tree(state)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 561 | rb_erase(&state->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 562 | RB_CLEAR_NODE(&state->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 563 | free_extent_state(state); |
| 564 | } else { |
| 565 | WARN_ON(1); |
| 566 | } |
| 567 | } else { |
| 568 | merge_state(tree, state); |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 569 | next = next_state(state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 570 | } |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 571 | return next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 572 | } |
| 573 | |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 574 | static struct extent_state * |
| 575 | alloc_extent_state_atomic(struct extent_state *prealloc) |
| 576 | { |
| 577 | if (!prealloc) |
| 578 | prealloc = alloc_extent_state(GFP_ATOMIC); |
| 579 | |
| 580 | return prealloc; |
| 581 | } |
| 582 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 583 | 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] | 584 | { |
David Sterba | 05912a3 | 2018-07-18 19:23:45 +0200 | [diff] [blame] | 585 | struct inode *inode = tree->private_data; |
| 586 | |
| 587 | btrfs_panic(btrfs_sb(inode->i_sb), err, |
| 588 | "locking error: extent tree was modified by another thread while locked"); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 589 | } |
| 590 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 591 | /* |
| 592 | * clear some bits on a range in the tree. This may require splitting |
| 593 | * or inserting elements in the tree, so the gfp mask is used to |
| 594 | * indicate which allocations or sleeping are allowed. |
| 595 | * |
| 596 | * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove |
| 597 | * the given range from the tree regardless of state (ie for truncate). |
| 598 | * |
| 599 | * the range [start, end] is inclusive. |
| 600 | * |
Jeff Mahoney | 6763af8 | 2012-03-01 14:56:29 +0100 | [diff] [blame] | 601 | * 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] | 602 | */ |
David Sterba | 66b0c88 | 2017-10-31 16:30:47 +0100 | [diff] [blame] | 603 | int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 604 | unsigned bits, int wake, int delete, |
| 605 | struct extent_state **cached_state, |
| 606 | gfp_t mask, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 607 | { |
| 608 | struct extent_state *state; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 609 | struct extent_state *cached; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 610 | struct extent_state *prealloc = NULL; |
| 611 | struct rb_node *node; |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 612 | u64 last_end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 613 | int err; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 614 | int clear = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 615 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 616 | btrfs_debug_check_extent_io_range(tree, start, end); |
Qu Wenruo | a1d1984 | 2019-03-01 10:48:00 +0800 | [diff] [blame] | 617 | trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 618 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 619 | if (bits & EXTENT_DELALLOC) |
| 620 | bits |= EXTENT_NORESERVE; |
| 621 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 622 | if (delete) |
| 623 | bits |= ~EXTENT_CTLBITS; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 624 | |
Nikolay Borisov | 8882679 | 2019-03-14 15:28:31 +0200 | [diff] [blame] | 625 | if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY)) |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 626 | clear = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 627 | again: |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 628 | if (!prealloc && gfpflags_allow_blocking(mask)) { |
Filipe Manana | c7bc631 | 2014-11-03 14:12:57 +0000 | [diff] [blame] | 629 | /* |
| 630 | * Don't care for allocation failure here because we might end |
| 631 | * up not needing the pre-allocated extent state at all, which |
| 632 | * is the case if we only have in the tree extent states that |
| 633 | * cover our input range and don't cover too any other range. |
| 634 | * If we end up needing a new extent state we allocate it later. |
| 635 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 636 | prealloc = alloc_extent_state(mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 637 | } |
| 638 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 639 | spin_lock(&tree->lock); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 640 | if (cached_state) { |
| 641 | cached = *cached_state; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 642 | |
| 643 | if (clear) { |
| 644 | *cached_state = NULL; |
| 645 | cached_state = NULL; |
| 646 | } |
| 647 | |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 648 | if (cached && extent_state_in_tree(cached) && |
| 649 | cached->start <= start && cached->end > start) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 650 | if (clear) |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 651 | refcount_dec(&cached->refs); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 652 | state = cached; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 653 | goto hit_next; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 654 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 655 | if (clear) |
| 656 | free_extent_state(cached); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 657 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 658 | /* |
| 659 | * this search will find the extents that end after |
| 660 | * our range starts |
| 661 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 662 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 663 | if (!node) |
| 664 | goto out; |
| 665 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 666 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 667 | if (state->start > end) |
| 668 | goto out; |
| 669 | WARN_ON(state->end < start); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 670 | last_end = state->end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 671 | |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 672 | /* the state doesn't have the wanted bits, go ahead */ |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 673 | if (!(state->state & bits)) { |
| 674 | state = next_state(state); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 675 | goto next; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 676 | } |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 677 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 678 | /* |
| 679 | * | ---- desired range ---- | |
| 680 | * | state | or |
| 681 | * | ------------- state -------------- | |
| 682 | * |
| 683 | * We need to split the extent we found, and may flip |
| 684 | * bits on second half. |
| 685 | * |
| 686 | * If the extent we found extends past our range, we |
| 687 | * just split and search again. It'll get split again |
| 688 | * the next time though. |
| 689 | * |
| 690 | * If the extent we found is inside our range, we clear |
| 691 | * the desired bit on it. |
| 692 | */ |
| 693 | |
| 694 | if (state->start < start) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 695 | prealloc = alloc_extent_state_atomic(prealloc); |
| 696 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 697 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 698 | if (err) |
| 699 | extent_io_tree_panic(tree, err); |
| 700 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 701 | prealloc = NULL; |
| 702 | if (err) |
| 703 | goto out; |
| 704 | if (state->end <= end) { |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 705 | state = clear_state_bit(tree, state, &bits, wake, |
| 706 | changeset); |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 707 | goto next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 708 | } |
| 709 | goto search_again; |
| 710 | } |
| 711 | /* |
| 712 | * | ---- desired range ---- | |
| 713 | * | state | |
| 714 | * We need to split the extent, and clear the bit |
| 715 | * on the first half |
| 716 | */ |
| 717 | if (state->start <= end && state->end > end) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 718 | prealloc = alloc_extent_state_atomic(prealloc); |
| 719 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 720 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 721 | if (err) |
| 722 | extent_io_tree_panic(tree, err); |
| 723 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 724 | if (wake) |
| 725 | wake_up(&state->wq); |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 726 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 727 | clear_state_bit(tree, prealloc, &bits, wake, changeset); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 728 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 729 | prealloc = NULL; |
| 730 | goto out; |
| 731 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 732 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 733 | state = clear_state_bit(tree, state, &bits, wake, changeset); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 734 | next: |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 735 | if (last_end == (u64)-1) |
| 736 | goto out; |
| 737 | start = last_end + 1; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 738 | if (start <= end && state && !need_resched()) |
Liu Bo | 692e575 | 2012-02-16 18:34:36 +0800 | [diff] [blame] | 739 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 740 | |
| 741 | search_again: |
| 742 | if (start > end) |
| 743 | goto out; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 744 | spin_unlock(&tree->lock); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 745 | if (gfpflags_allow_blocking(mask)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 746 | cond_resched(); |
| 747 | goto again; |
David Sterba | 7ab5cb2 | 2016-04-27 01:02:15 +0200 | [diff] [blame] | 748 | |
| 749 | out: |
| 750 | spin_unlock(&tree->lock); |
| 751 | if (prealloc) |
| 752 | free_extent_state(prealloc); |
| 753 | |
| 754 | return 0; |
| 755 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 756 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 757 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 758 | static void wait_on_state(struct extent_io_tree *tree, |
| 759 | struct extent_state *state) |
Christoph Hellwig | 641f521 | 2008-12-02 06:36:10 -0500 | [diff] [blame] | 760 | __releases(tree->lock) |
| 761 | __acquires(tree->lock) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 762 | { |
| 763 | DEFINE_WAIT(wait); |
| 764 | prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 765 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 766 | schedule(); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 767 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 768 | finish_wait(&state->wq, &wait); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | /* |
| 772 | * waits for one or more bits to clear on a range in the state tree. |
| 773 | * The range [start, end] is inclusive. |
| 774 | * The tree lock is taken by this function |
| 775 | */ |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 776 | static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 777 | unsigned long bits) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 778 | { |
| 779 | struct extent_state *state; |
| 780 | struct rb_node *node; |
| 781 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 782 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 783 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 784 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 785 | again: |
| 786 | while (1) { |
| 787 | /* |
| 788 | * this search will find all the extents that end after |
| 789 | * our range starts |
| 790 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 791 | node = tree_search(tree, start); |
Filipe Manana | c50d3e7 | 2014-03-31 14:53:25 +0100 | [diff] [blame] | 792 | process_node: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 793 | if (!node) |
| 794 | break; |
| 795 | |
| 796 | state = rb_entry(node, struct extent_state, rb_node); |
| 797 | |
| 798 | if (state->start > end) |
| 799 | goto out; |
| 800 | |
| 801 | if (state->state & bits) { |
| 802 | start = state->start; |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 803 | refcount_inc(&state->refs); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 804 | wait_on_state(tree, state); |
| 805 | free_extent_state(state); |
| 806 | goto again; |
| 807 | } |
| 808 | start = state->end + 1; |
| 809 | |
| 810 | if (start > end) |
| 811 | break; |
| 812 | |
Filipe Manana | c50d3e7 | 2014-03-31 14:53:25 +0100 | [diff] [blame] | 813 | if (!cond_resched_lock(&tree->lock)) { |
| 814 | node = rb_next(node); |
| 815 | goto process_node; |
| 816 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 817 | } |
| 818 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 819 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 820 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 821 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 822 | static void set_state_bits(struct extent_io_tree *tree, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 823 | struct extent_state *state, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 824 | unsigned *bits, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 825 | { |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 826 | unsigned bits_to_set = *bits & ~EXTENT_CTLBITS; |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 827 | int ret; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 828 | |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 829 | if (tree->private_data && is_data_inode(tree->private_data)) |
| 830 | btrfs_set_delalloc_extent(tree->private_data, state, bits); |
| 831 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 832 | if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 833 | u64 range = state->end - state->start + 1; |
| 834 | tree->dirty_bytes += range; |
| 835 | } |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 836 | ret = add_extent_changeset(state, bits_to_set, changeset, 1); |
| 837 | BUG_ON(ret < 0); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 838 | state->state |= bits_to_set; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 839 | } |
| 840 | |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 841 | static void cache_state_if_flags(struct extent_state *state, |
| 842 | struct extent_state **cached_ptr, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 843 | unsigned flags) |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 844 | { |
| 845 | if (cached_ptr && !(*cached_ptr)) { |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 846 | if (!flags || (state->state & flags)) { |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 847 | *cached_ptr = state; |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 848 | refcount_inc(&state->refs); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | } |
| 852 | |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 853 | static void cache_state(struct extent_state *state, |
| 854 | struct extent_state **cached_ptr) |
| 855 | { |
| 856 | return cache_state_if_flags(state, cached_ptr, |
Nikolay Borisov | 8882679 | 2019-03-14 15:28:31 +0200 | [diff] [blame] | 857 | EXTENT_LOCKED | EXTENT_BOUNDARY); |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 858 | } |
| 859 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 860 | /* |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 861 | * set some bits on a range in the tree. This may require allocations or |
| 862 | * sleeping, so the gfp mask is used to indicate what is allowed. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 863 | * |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 864 | * If any of the exclusive bits are set, this will fail with -EEXIST if some |
| 865 | * part of the range already has the desired bits set. The start of the |
| 866 | * existing range is returned in failed_start in this case. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 867 | * |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 868 | * [start, end] is inclusive This takes the tree lock. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 869 | */ |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 870 | |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 871 | static int __must_check |
| 872 | __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 873 | unsigned bits, unsigned exclusive_bits, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 874 | u64 *failed_start, struct extent_state **cached_state, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 875 | gfp_t mask, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 876 | { |
| 877 | struct extent_state *state; |
| 878 | struct extent_state *prealloc = NULL; |
| 879 | struct rb_node *node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 880 | struct rb_node **p; |
| 881 | struct rb_node *parent; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 882 | int err = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 883 | u64 last_start; |
| 884 | u64 last_end; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 885 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 886 | btrfs_debug_check_extent_io_range(tree, start, end); |
Qu Wenruo | a1d1984 | 2019-03-01 10:48:00 +0800 | [diff] [blame] | 887 | trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 888 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 889 | again: |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 890 | if (!prealloc && gfpflags_allow_blocking(mask)) { |
David Sterba | 059f791 | 2016-04-27 01:03:45 +0200 | [diff] [blame] | 891 | /* |
| 892 | * Don't care for allocation failure here because we might end |
| 893 | * up not needing the pre-allocated extent state at all, which |
| 894 | * is the case if we only have in the tree extent states that |
| 895 | * cover our input range and don't cover too any other range. |
| 896 | * If we end up needing a new extent state we allocate it later. |
| 897 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 898 | prealloc = alloc_extent_state(mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 899 | } |
| 900 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 901 | spin_lock(&tree->lock); |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 902 | if (cached_state && *cached_state) { |
| 903 | state = *cached_state; |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 904 | if (state->start <= start && state->end > start && |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 905 | extent_state_in_tree(state)) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 906 | node = &state->rb_node; |
| 907 | goto hit_next; |
| 908 | } |
| 909 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 910 | /* |
| 911 | * this search will find all the extents that end after |
| 912 | * our range starts. |
| 913 | */ |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 914 | node = tree_search_for_insert(tree, start, &p, &parent); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 915 | if (!node) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 916 | prealloc = alloc_extent_state_atomic(prealloc); |
| 917 | BUG_ON(!prealloc); |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 918 | err = insert_state(tree, prealloc, start, end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 919 | &p, &parent, &bits, changeset); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 920 | if (err) |
| 921 | extent_io_tree_panic(tree, err); |
| 922 | |
Filipe David Borba Manana | c42ac0b | 2013-11-26 15:01:34 +0000 | [diff] [blame] | 923 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 924 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 925 | goto out; |
| 926 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 927 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 40431d6 | 2009-08-05 12:57:59 -0400 | [diff] [blame] | 928 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 929 | last_start = state->start; |
| 930 | last_end = state->end; |
| 931 | |
| 932 | /* |
| 933 | * | ---- desired range ---- | |
| 934 | * | state | |
| 935 | * |
| 936 | * Just lock what we found and keep going |
| 937 | */ |
| 938 | if (state->start == start && state->end <= end) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 939 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 940 | *failed_start = state->start; |
| 941 | err = -EEXIST; |
| 942 | goto out; |
| 943 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 944 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 945 | set_state_bits(tree, state, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 946 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 947 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 948 | if (last_end == (u64)-1) |
| 949 | goto out; |
| 950 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 951 | state = next_state(state); |
| 952 | if (start < end && state && state->start == start && |
| 953 | !need_resched()) |
| 954 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 955 | goto search_again; |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * | ---- desired range ---- | |
| 960 | * | state | |
| 961 | * or |
| 962 | * | ------------- state -------------- | |
| 963 | * |
| 964 | * We need to split the extent we found, and may flip bits on |
| 965 | * second half. |
| 966 | * |
| 967 | * If the extent we found extends past our |
| 968 | * range, we just split and search again. It'll get split |
| 969 | * again the next time though. |
| 970 | * |
| 971 | * If the extent we found is inside our range, we set the |
| 972 | * desired bit on it. |
| 973 | */ |
| 974 | if (state->start < start) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 975 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 976 | *failed_start = start; |
| 977 | err = -EEXIST; |
| 978 | goto out; |
| 979 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 980 | |
| 981 | prealloc = alloc_extent_state_atomic(prealloc); |
| 982 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 983 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 984 | if (err) |
| 985 | extent_io_tree_panic(tree, err); |
| 986 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 987 | prealloc = NULL; |
| 988 | if (err) |
| 989 | goto out; |
| 990 | if (state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 991 | set_state_bits(tree, state, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 992 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 993 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 994 | if (last_end == (u64)-1) |
| 995 | goto out; |
| 996 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 997 | state = next_state(state); |
| 998 | if (start < end && state && state->start == start && |
| 999 | !need_resched()) |
| 1000 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1001 | } |
| 1002 | goto search_again; |
| 1003 | } |
| 1004 | /* |
| 1005 | * | ---- desired range ---- | |
| 1006 | * | state | or | state | |
| 1007 | * |
| 1008 | * There's a hole, we need to insert something in it and |
| 1009 | * ignore the extent we found. |
| 1010 | */ |
| 1011 | if (state->start > start) { |
| 1012 | u64 this_end; |
| 1013 | if (end < last_start) |
| 1014 | this_end = end; |
| 1015 | else |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1016 | this_end = last_start - 1; |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 1017 | |
| 1018 | prealloc = alloc_extent_state_atomic(prealloc); |
| 1019 | BUG_ON(!prealloc); |
Xiao Guangrong | c7f895a | 2011-04-20 06:45:49 +0000 | [diff] [blame] | 1020 | |
| 1021 | /* |
| 1022 | * Avoid to free 'prealloc' if it can be merged with |
| 1023 | * the later extent. |
| 1024 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1025 | err = insert_state(tree, prealloc, start, this_end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1026 | NULL, NULL, &bits, changeset); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1027 | if (err) |
| 1028 | extent_io_tree_panic(tree, err); |
| 1029 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1030 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1031 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1032 | start = this_end + 1; |
| 1033 | goto search_again; |
| 1034 | } |
| 1035 | /* |
| 1036 | * | ---- desired range ---- | |
| 1037 | * | state | |
| 1038 | * We need to split the extent, and set the bit |
| 1039 | * on the first half |
| 1040 | */ |
| 1041 | if (state->start <= end && state->end > end) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1042 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1043 | *failed_start = start; |
| 1044 | err = -EEXIST; |
| 1045 | goto out; |
| 1046 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 1047 | |
| 1048 | prealloc = alloc_extent_state_atomic(prealloc); |
| 1049 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1050 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1051 | if (err) |
| 1052 | extent_io_tree_panic(tree, err); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1053 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1054 | set_state_bits(tree, prealloc, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1055 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1056 | merge_state(tree, prealloc); |
| 1057 | prealloc = NULL; |
| 1058 | goto out; |
| 1059 | } |
| 1060 | |
David Sterba | b5a4ba14 | 2016-04-27 01:02:15 +0200 | [diff] [blame] | 1061 | search_again: |
| 1062 | if (start > end) |
| 1063 | goto out; |
| 1064 | spin_unlock(&tree->lock); |
| 1065 | if (gfpflags_allow_blocking(mask)) |
| 1066 | cond_resched(); |
| 1067 | goto again; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1068 | |
| 1069 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1070 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1071 | if (prealloc) |
| 1072 | free_extent_state(prealloc); |
| 1073 | |
| 1074 | return err; |
| 1075 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1076 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1077 | |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1078 | 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] | 1079 | unsigned bits, u64 * failed_start, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1080 | struct extent_state **cached_state, gfp_t mask) |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1081 | { |
| 1082 | return __set_extent_bit(tree, start, end, bits, 0, failed_start, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1083 | cached_state, mask, NULL); |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1087 | /** |
Liu Bo | 10983f2 | 2012-07-11 15:26:19 +0800 | [diff] [blame] | 1088 | * convert_extent_bit - convert all bits in a given range from one bit to |
| 1089 | * another |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1090 | * @tree: the io tree to search |
| 1091 | * @start: the start offset in bytes |
| 1092 | * @end: the end offset in bytes (inclusive) |
| 1093 | * @bits: the bits to set in this range |
| 1094 | * @clear_bits: the bits to clear in this range |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1095 | * @cached_state: state that we're going to cache |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1096 | * |
| 1097 | * This will go through and set bits for the given range. If any states exist |
| 1098 | * already in this range they are set with the given bit and cleared of the |
| 1099 | * clear_bits. This is only meant to be used by things that are mergeable, ie |
| 1100 | * converting from say DELALLOC to DIRTY. This is not meant to be used with |
| 1101 | * boundary bits like LOCK. |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1102 | * |
| 1103 | * All allocations are done with GFP_NOFS. |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1104 | */ |
| 1105 | 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] | 1106 | unsigned bits, unsigned clear_bits, |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1107 | struct extent_state **cached_state) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1108 | { |
| 1109 | struct extent_state *state; |
| 1110 | struct extent_state *prealloc = NULL; |
| 1111 | struct rb_node *node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1112 | struct rb_node **p; |
| 1113 | struct rb_node *parent; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1114 | int err = 0; |
| 1115 | u64 last_start; |
| 1116 | u64 last_end; |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1117 | bool first_iteration = true; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1118 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 1119 | btrfs_debug_check_extent_io_range(tree, start, end); |
Qu Wenruo | a1d1984 | 2019-03-01 10:48:00 +0800 | [diff] [blame] | 1120 | trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits, |
| 1121 | clear_bits); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 1122 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1123 | again: |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1124 | if (!prealloc) { |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1125 | /* |
| 1126 | * Best effort, don't worry if extent state allocation fails |
| 1127 | * here for the first iteration. We might have a cached state |
| 1128 | * that matches exactly the target range, in which case no |
| 1129 | * extent state allocations are needed. We'll only know this |
| 1130 | * after locking the tree. |
| 1131 | */ |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1132 | prealloc = alloc_extent_state(GFP_NOFS); |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1133 | if (!prealloc && !first_iteration) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1134 | return -ENOMEM; |
| 1135 | } |
| 1136 | |
| 1137 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1138 | if (cached_state && *cached_state) { |
| 1139 | state = *cached_state; |
| 1140 | if (state->start <= start && state->end > start && |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1141 | extent_state_in_tree(state)) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1142 | node = &state->rb_node; |
| 1143 | goto hit_next; |
| 1144 | } |
| 1145 | } |
| 1146 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1147 | /* |
| 1148 | * this search will find all the extents that end after |
| 1149 | * our range starts. |
| 1150 | */ |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1151 | node = tree_search_for_insert(tree, start, &p, &parent); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1152 | if (!node) { |
| 1153 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1154 | if (!prealloc) { |
| 1155 | err = -ENOMEM; |
| 1156 | goto out; |
| 1157 | } |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1158 | err = insert_state(tree, prealloc, start, end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1159 | &p, &parent, &bits, NULL); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1160 | if (err) |
| 1161 | extent_io_tree_panic(tree, err); |
Filipe David Borba Manana | c42ac0b | 2013-11-26 15:01:34 +0000 | [diff] [blame] | 1162 | cache_state(prealloc, cached_state); |
| 1163 | prealloc = NULL; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1164 | goto out; |
| 1165 | } |
| 1166 | state = rb_entry(node, struct extent_state, rb_node); |
| 1167 | hit_next: |
| 1168 | last_start = state->start; |
| 1169 | last_end = state->end; |
| 1170 | |
| 1171 | /* |
| 1172 | * | ---- desired range ---- | |
| 1173 | * | state | |
| 1174 | * |
| 1175 | * Just lock what we found and keep going |
| 1176 | */ |
| 1177 | if (state->start == start && state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1178 | set_state_bits(tree, state, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1179 | cache_state(state, cached_state); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1180 | state = clear_state_bit(tree, state, &clear_bits, 0, NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1181 | if (last_end == (u64)-1) |
| 1182 | goto out; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1183 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1184 | if (start < end && state && state->start == start && |
| 1185 | !need_resched()) |
| 1186 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1187 | goto search_again; |
| 1188 | } |
| 1189 | |
| 1190 | /* |
| 1191 | * | ---- desired range ---- | |
| 1192 | * | state | |
| 1193 | * or |
| 1194 | * | ------------- state -------------- | |
| 1195 | * |
| 1196 | * We need to split the extent we found, and may flip bits on |
| 1197 | * second half. |
| 1198 | * |
| 1199 | * If the extent we found extends past our |
| 1200 | * range, we just split and search again. It'll get split |
| 1201 | * again the next time though. |
| 1202 | * |
| 1203 | * If the extent we found is inside our range, we set the |
| 1204 | * desired bit on it. |
| 1205 | */ |
| 1206 | if (state->start < start) { |
| 1207 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1208 | if (!prealloc) { |
| 1209 | err = -ENOMEM; |
| 1210 | goto out; |
| 1211 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1212 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1213 | if (err) |
| 1214 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1215 | prealloc = NULL; |
| 1216 | if (err) |
| 1217 | goto out; |
| 1218 | if (state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1219 | set_state_bits(tree, state, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1220 | cache_state(state, cached_state); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1221 | state = clear_state_bit(tree, state, &clear_bits, 0, |
| 1222 | NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1223 | if (last_end == (u64)-1) |
| 1224 | goto out; |
| 1225 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1226 | if (start < end && state && state->start == start && |
| 1227 | !need_resched()) |
| 1228 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1229 | } |
| 1230 | goto search_again; |
| 1231 | } |
| 1232 | /* |
| 1233 | * | ---- desired range ---- | |
| 1234 | * | state | or | state | |
| 1235 | * |
| 1236 | * There's a hole, we need to insert something in it and |
| 1237 | * ignore the extent we found. |
| 1238 | */ |
| 1239 | if (state->start > start) { |
| 1240 | u64 this_end; |
| 1241 | if (end < last_start) |
| 1242 | this_end = end; |
| 1243 | else |
| 1244 | this_end = last_start - 1; |
| 1245 | |
| 1246 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1247 | if (!prealloc) { |
| 1248 | err = -ENOMEM; |
| 1249 | goto out; |
| 1250 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1251 | |
| 1252 | /* |
| 1253 | * Avoid to free 'prealloc' if it can be merged with |
| 1254 | * the later extent. |
| 1255 | */ |
| 1256 | err = insert_state(tree, prealloc, start, this_end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1257 | NULL, NULL, &bits, NULL); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1258 | if (err) |
| 1259 | extent_io_tree_panic(tree, err); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1260 | cache_state(prealloc, cached_state); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1261 | prealloc = NULL; |
| 1262 | start = this_end + 1; |
| 1263 | goto search_again; |
| 1264 | } |
| 1265 | /* |
| 1266 | * | ---- desired range ---- | |
| 1267 | * | state | |
| 1268 | * We need to split the extent, and set the bit |
| 1269 | * on the first half |
| 1270 | */ |
| 1271 | if (state->start <= end && state->end > end) { |
| 1272 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1273 | if (!prealloc) { |
| 1274 | err = -ENOMEM; |
| 1275 | goto out; |
| 1276 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1277 | |
| 1278 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1279 | if (err) |
| 1280 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1281 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1282 | set_state_bits(tree, prealloc, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1283 | cache_state(prealloc, cached_state); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1284 | clear_state_bit(tree, prealloc, &clear_bits, 0, NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1285 | prealloc = NULL; |
| 1286 | goto out; |
| 1287 | } |
| 1288 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1289 | search_again: |
| 1290 | if (start > end) |
| 1291 | goto out; |
| 1292 | spin_unlock(&tree->lock); |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1293 | cond_resched(); |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1294 | first_iteration = false; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1295 | goto again; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1296 | |
| 1297 | out: |
| 1298 | spin_unlock(&tree->lock); |
| 1299 | if (prealloc) |
| 1300 | free_extent_state(prealloc); |
| 1301 | |
| 1302 | return err; |
| 1303 | } |
| 1304 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1305 | /* wrappers around set/clear extent bit */ |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1306 | int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 2c53b91 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1307 | unsigned bits, struct extent_changeset *changeset) |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1308 | { |
| 1309 | /* |
| 1310 | * We don't support EXTENT_LOCKED yet, as current changeset will |
| 1311 | * record any bits changed, so for EXTENT_LOCKED case, it will |
| 1312 | * either fail with -EEXIST or changeset will record the whole |
| 1313 | * range. |
| 1314 | */ |
| 1315 | BUG_ON(bits & EXTENT_LOCKED); |
| 1316 | |
David Sterba | 2c53b91 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1317 | return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1318 | changeset); |
| 1319 | } |
| 1320 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1321 | int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 1322 | unsigned bits, int wake, int delete, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 1323 | struct extent_state **cached) |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1324 | { |
| 1325 | return __clear_extent_bit(tree, start, end, bits, wake, delete, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 1326 | cached, GFP_NOFS, NULL); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1327 | } |
| 1328 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1329 | int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | f734c44 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1330 | unsigned bits, struct extent_changeset *changeset) |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1331 | { |
| 1332 | /* |
| 1333 | * Don't support EXTENT_LOCKED case, same reason as |
| 1334 | * set_record_extent_bits(). |
| 1335 | */ |
| 1336 | BUG_ON(bits & EXTENT_LOCKED); |
| 1337 | |
David Sterba | f734c44 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1338 | return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS, |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1339 | changeset); |
| 1340 | } |
| 1341 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1342 | /* |
| 1343 | * either insert or lock state struct between start and end use mask to tell |
| 1344 | * us if waiting is desired. |
| 1345 | */ |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1346 | int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1347 | struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1348 | { |
| 1349 | int err; |
| 1350 | u64 failed_start; |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1351 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1352 | while (1) { |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1353 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1354 | EXTENT_LOCKED, &failed_start, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1355 | cached_state, GFP_NOFS, NULL); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1356 | if (err == -EEXIST) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1357 | wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED); |
| 1358 | start = failed_start; |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1359 | } else |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1360 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1361 | WARN_ON(start > end); |
| 1362 | } |
| 1363 | return err; |
| 1364 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1365 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1366 | 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] | 1367 | { |
| 1368 | int err; |
| 1369 | u64 failed_start; |
| 1370 | |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1371 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1372 | &failed_start, NULL, GFP_NOFS, NULL); |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1373 | if (err == -EEXIST) { |
| 1374 | if (failed_start > start) |
| 1375 | clear_extent_bit(tree, start, failed_start - 1, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 1376 | EXTENT_LOCKED, 1, 0, NULL); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1377 | return 0; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1378 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1379 | return 1; |
| 1380 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1381 | |
David Sterba | bd1fa4f | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 1382 | void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end) |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1383 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1384 | unsigned long index = start >> PAGE_SHIFT; |
| 1385 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1386 | struct page *page; |
| 1387 | |
| 1388 | while (index <= end_index) { |
| 1389 | page = find_get_page(inode->i_mapping, index); |
| 1390 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
| 1391 | clear_page_dirty_for_io(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1392 | put_page(page); |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1393 | index++; |
| 1394 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1395 | } |
| 1396 | |
David Sterba | f631157 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 1397 | void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end) |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1398 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1399 | unsigned long index = start >> PAGE_SHIFT; |
| 1400 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1401 | struct page *page; |
| 1402 | |
| 1403 | while (index <= end_index) { |
| 1404 | page = find_get_page(inode->i_mapping, index); |
| 1405 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1406 | __set_page_dirty_nobuffers(page); |
Konstantin Khebnikov | 8d38633 | 2015-02-11 15:26:55 -0800 | [diff] [blame] | 1407 | account_page_redirty(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1408 | put_page(page); |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1409 | index++; |
| 1410 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1411 | } |
| 1412 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1413 | /* find the first state struct with 'bits' set after 'start', and |
| 1414 | * return it. tree->lock must be held. NULL will returned if |
| 1415 | * nothing was found after 'start' |
| 1416 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1417 | static struct extent_state * |
| 1418 | find_first_extent_bit_state(struct extent_io_tree *tree, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1419 | u64 start, unsigned bits) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1420 | { |
| 1421 | struct rb_node *node; |
| 1422 | struct extent_state *state; |
| 1423 | |
| 1424 | /* |
| 1425 | * this search will find all the extents that end after |
| 1426 | * our range starts. |
| 1427 | */ |
| 1428 | node = tree_search(tree, start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1429 | if (!node) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1430 | goto out; |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1431 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1432 | while (1) { |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1433 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1434 | if (state->end >= start && (state->state & bits)) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1435 | return state; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1436 | |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1437 | node = rb_next(node); |
| 1438 | if (!node) |
| 1439 | break; |
| 1440 | } |
| 1441 | out: |
| 1442 | return NULL; |
| 1443 | } |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1444 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1445 | /* |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1446 | * find the first offset in the io tree with 'bits' set. zero is |
| 1447 | * returned if we find something, and *start_ret and *end_ret are |
| 1448 | * set to reflect the state struct that was found. |
| 1449 | * |
Wang Sheng-Hui | 477d7ea | 2012-04-06 14:35:47 +0800 | [diff] [blame] | 1450 | * If nothing was found, 1 is returned. If found something, return 0. |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1451 | */ |
| 1452 | int find_first_extent_bit(struct extent_io_tree *tree, u64 start, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1453 | u64 *start_ret, u64 *end_ret, unsigned bits, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1454 | struct extent_state **cached_state) |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1455 | { |
| 1456 | struct extent_state *state; |
| 1457 | int ret = 1; |
| 1458 | |
| 1459 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1460 | if (cached_state && *cached_state) { |
| 1461 | state = *cached_state; |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1462 | if (state->end == start - 1 && extent_state_in_tree(state)) { |
Liu Bo | 9688e9a | 2018-08-23 03:14:53 +0800 | [diff] [blame] | 1463 | while ((state = next_state(state)) != NULL) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1464 | if (state->state & bits) |
| 1465 | goto got_it; |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1466 | } |
| 1467 | free_extent_state(*cached_state); |
| 1468 | *cached_state = NULL; |
| 1469 | goto out; |
| 1470 | } |
| 1471 | free_extent_state(*cached_state); |
| 1472 | *cached_state = NULL; |
| 1473 | } |
| 1474 | |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1475 | state = find_first_extent_bit_state(tree, start, bits); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1476 | got_it: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1477 | if (state) { |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 1478 | cache_state_if_flags(state, cached_state, 0); |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1479 | *start_ret = state->start; |
| 1480 | *end_ret = state->end; |
| 1481 | ret = 0; |
| 1482 | } |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1483 | out: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1484 | spin_unlock(&tree->lock); |
| 1485 | return ret; |
| 1486 | } |
| 1487 | |
| 1488 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1489 | * find a contiguous range of bytes in the file marked as delalloc, not |
| 1490 | * more than 'max_bytes'. start and end are used to return the range, |
| 1491 | * |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1492 | * true is returned if we find something, false if nothing was in the tree |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1493 | */ |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1494 | static noinline bool find_delalloc_range(struct extent_io_tree *tree, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1495 | u64 *start, u64 *end, u64 max_bytes, |
| 1496 | struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1497 | { |
| 1498 | struct rb_node *node; |
| 1499 | struct extent_state *state; |
| 1500 | u64 cur_start = *start; |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1501 | bool found = false; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1502 | u64 total_bytes = 0; |
| 1503 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1504 | spin_lock(&tree->lock); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1505 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1506 | /* |
| 1507 | * this search will find all the extents that end after |
| 1508 | * our range starts. |
| 1509 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1510 | node = tree_search(tree, cur_start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1511 | if (!node) { |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1512 | *end = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1513 | goto out; |
| 1514 | } |
| 1515 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1516 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1517 | state = rb_entry(node, struct extent_state, rb_node); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1518 | if (found && (state->start != cur_start || |
| 1519 | (state->state & EXTENT_BOUNDARY))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1520 | goto out; |
| 1521 | } |
| 1522 | if (!(state->state & EXTENT_DELALLOC)) { |
| 1523 | if (!found) |
| 1524 | *end = state->end; |
| 1525 | goto out; |
| 1526 | } |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1527 | if (!found) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1528 | *start = state->start; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1529 | *cached_state = state; |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 1530 | refcount_inc(&state->refs); |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1531 | } |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1532 | found = true; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1533 | *end = state->end; |
| 1534 | cur_start = state->end + 1; |
| 1535 | node = rb_next(node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1536 | total_bytes += state->end - state->start + 1; |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 1537 | if (total_bytes >= max_bytes) |
Josef Bacik | 573aeca | 2013-08-30 14:38:49 -0400 | [diff] [blame] | 1538 | break; |
Josef Bacik | 573aeca | 2013-08-30 14:38:49 -0400 | [diff] [blame] | 1539 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1540 | break; |
| 1541 | } |
| 1542 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1543 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1544 | return found; |
| 1545 | } |
| 1546 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1547 | static int __process_pages_contig(struct address_space *mapping, |
| 1548 | struct page *locked_page, |
| 1549 | pgoff_t start_index, pgoff_t end_index, |
| 1550 | unsigned long page_ops, pgoff_t *index_ret); |
| 1551 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1552 | static noinline void __unlock_for_delalloc(struct inode *inode, |
| 1553 | struct page *locked_page, |
| 1554 | u64 start, u64 end) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1555 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1556 | unsigned long index = start >> PAGE_SHIFT; |
| 1557 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1558 | |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1559 | ASSERT(locked_page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1560 | if (index == locked_page->index && end_index == index) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1561 | return; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1562 | |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1563 | __process_pages_contig(inode->i_mapping, locked_page, index, end_index, |
| 1564 | PAGE_UNLOCK, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | static noinline int lock_delalloc_pages(struct inode *inode, |
| 1568 | struct page *locked_page, |
| 1569 | u64 delalloc_start, |
| 1570 | u64 delalloc_end) |
| 1571 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1572 | unsigned long index = delalloc_start >> PAGE_SHIFT; |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1573 | unsigned long index_ret = index; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1574 | unsigned long end_index = delalloc_end >> PAGE_SHIFT; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1575 | int ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1576 | |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1577 | ASSERT(locked_page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1578 | if (index == locked_page->index && index == end_index) |
| 1579 | return 0; |
| 1580 | |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1581 | ret = __process_pages_contig(inode->i_mapping, locked_page, index, |
| 1582 | end_index, PAGE_LOCK, &index_ret); |
| 1583 | if (ret == -EAGAIN) |
| 1584 | __unlock_for_delalloc(inode, locked_page, delalloc_start, |
| 1585 | (u64)index_ret << PAGE_SHIFT); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1586 | return ret; |
| 1587 | } |
| 1588 | |
| 1589 | /* |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1590 | * Find and lock a contiguous range of bytes in the file marked as delalloc, no |
| 1591 | * more than @max_bytes. @Start and @end are used to return the range, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1592 | * |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1593 | * Return: true if we find something |
| 1594 | * false if nothing was in the tree |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1595 | */ |
Johannes Thumshirn | ce9f967 | 2018-11-19 10:38:17 +0100 | [diff] [blame] | 1596 | EXPORT_FOR_TESTS |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1597 | noinline_for_stack bool find_lock_delalloc_range(struct inode *inode, |
Josef Bacik | 294e30f | 2013-10-09 12:00:56 -0400 | [diff] [blame] | 1598 | struct extent_io_tree *tree, |
| 1599 | struct page *locked_page, u64 *start, |
Nikolay Borisov | 917aace | 2018-10-26 14:43:20 +0300 | [diff] [blame] | 1600 | u64 *end) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1601 | { |
Nikolay Borisov | 917aace | 2018-10-26 14:43:20 +0300 | [diff] [blame] | 1602 | u64 max_bytes = BTRFS_MAX_EXTENT_SIZE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1603 | u64 delalloc_start; |
| 1604 | u64 delalloc_end; |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1605 | bool found; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1606 | struct extent_state *cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1607 | int ret; |
| 1608 | int loops = 0; |
| 1609 | |
| 1610 | again: |
| 1611 | /* step one, find a bunch of delalloc bytes starting at start */ |
| 1612 | delalloc_start = *start; |
| 1613 | delalloc_end = 0; |
| 1614 | found = find_delalloc_range(tree, &delalloc_start, &delalloc_end, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1615 | max_bytes, &cached_state); |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1616 | if (!found || delalloc_end <= *start) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1617 | *start = delalloc_start; |
| 1618 | *end = delalloc_end; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1619 | free_extent_state(cached_state); |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1620 | return false; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | /* |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1624 | * start comes from the offset of locked_page. We have to lock |
| 1625 | * pages in order, so we can't process delalloc bytes before |
| 1626 | * locked_page |
| 1627 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1628 | if (delalloc_start < *start) |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1629 | delalloc_start = *start; |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1630 | |
| 1631 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1632 | * 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] | 1633 | */ |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 1634 | if (delalloc_end + 1 - delalloc_start > max_bytes) |
| 1635 | delalloc_end = delalloc_start + max_bytes - 1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1636 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1637 | /* step two, lock all the pages after the page that has start */ |
| 1638 | ret = lock_delalloc_pages(inode, locked_page, |
| 1639 | delalloc_start, delalloc_end); |
Nikolay Borisov | 9bfd61d | 2018-10-26 14:43:21 +0300 | [diff] [blame] | 1640 | ASSERT(!ret || ret == -EAGAIN); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1641 | if (ret == -EAGAIN) { |
| 1642 | /* some of the pages are gone, lets avoid looping by |
| 1643 | * shortening the size of the delalloc range we're searching |
| 1644 | */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1645 | free_extent_state(cached_state); |
Chris Mason | 7d78874 | 2014-05-21 05:49:54 -0700 | [diff] [blame] | 1646 | cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1647 | if (!loops) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1648 | max_bytes = PAGE_SIZE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1649 | loops = 1; |
| 1650 | goto again; |
| 1651 | } else { |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1652 | found = false; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1653 | goto out_failed; |
| 1654 | } |
| 1655 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1656 | |
| 1657 | /* step three, lock the state bits for the whole range */ |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1658 | lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1659 | |
| 1660 | /* then test to make sure it is all still delalloc */ |
| 1661 | ret = test_range_bit(tree, delalloc_start, delalloc_end, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1662 | EXTENT_DELALLOC, 1, cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1663 | if (!ret) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1664 | unlock_extent_cached(tree, delalloc_start, delalloc_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 1665 | &cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1666 | __unlock_for_delalloc(inode, locked_page, |
| 1667 | delalloc_start, delalloc_end); |
| 1668 | cond_resched(); |
| 1669 | goto again; |
| 1670 | } |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1671 | free_extent_state(cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1672 | *start = delalloc_start; |
| 1673 | *end = delalloc_end; |
| 1674 | out_failed: |
| 1675 | return found; |
| 1676 | } |
| 1677 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1678 | static int __process_pages_contig(struct address_space *mapping, |
| 1679 | struct page *locked_page, |
| 1680 | pgoff_t start_index, pgoff_t end_index, |
| 1681 | unsigned long page_ops, pgoff_t *index_ret) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1682 | { |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1683 | unsigned long nr_pages = end_index - start_index + 1; |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1684 | unsigned long pages_locked = 0; |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1685 | pgoff_t index = start_index; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1686 | struct page *pages[16]; |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1687 | unsigned ret; |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1688 | int err = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1689 | int i; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1690 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1691 | if (page_ops & PAGE_LOCK) { |
| 1692 | ASSERT(page_ops == PAGE_LOCK); |
| 1693 | ASSERT(index_ret && *index_ret == start_index); |
| 1694 | } |
| 1695 | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1696 | if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0) |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1697 | mapping_set_error(mapping, -EIO); |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1698 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1699 | while (nr_pages > 0) { |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1700 | ret = find_get_pages_contig(mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1701 | min_t(unsigned long, |
| 1702 | nr_pages, ARRAY_SIZE(pages)), pages); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1703 | if (ret == 0) { |
| 1704 | /* |
| 1705 | * Only if we're going to lock these pages, |
| 1706 | * can we find nothing at @index. |
| 1707 | */ |
| 1708 | ASSERT(page_ops & PAGE_LOCK); |
Liu Bo | 49d4a33 | 2017-03-06 18:20:56 -0800 | [diff] [blame] | 1709 | err = -EAGAIN; |
| 1710 | goto out; |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1711 | } |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1712 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1713 | for (i = 0; i < ret; i++) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1714 | if (page_ops & PAGE_SET_PRIVATE2) |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1715 | SetPagePrivate2(pages[i]); |
| 1716 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1717 | if (pages[i] == locked_page) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1718 | put_page(pages[i]); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1719 | pages_locked++; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1720 | continue; |
| 1721 | } |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1722 | if (page_ops & PAGE_CLEAR_DIRTY) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1723 | clear_page_dirty_for_io(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1724 | if (page_ops & PAGE_SET_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1725 | set_page_writeback(pages[i]); |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1726 | if (page_ops & PAGE_SET_ERROR) |
| 1727 | SetPageError(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1728 | if (page_ops & PAGE_END_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1729 | end_page_writeback(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1730 | if (page_ops & PAGE_UNLOCK) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1731 | unlock_page(pages[i]); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1732 | if (page_ops & PAGE_LOCK) { |
| 1733 | lock_page(pages[i]); |
| 1734 | if (!PageDirty(pages[i]) || |
| 1735 | pages[i]->mapping != mapping) { |
| 1736 | unlock_page(pages[i]); |
| 1737 | put_page(pages[i]); |
| 1738 | err = -EAGAIN; |
| 1739 | goto out; |
| 1740 | } |
| 1741 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1742 | put_page(pages[i]); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1743 | pages_locked++; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1744 | } |
| 1745 | nr_pages -= ret; |
| 1746 | index += ret; |
| 1747 | cond_resched(); |
| 1748 | } |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1749 | out: |
| 1750 | if (err && index_ret) |
| 1751 | *index_ret = start_index + pages_locked - 1; |
| 1752 | return err; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1753 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1754 | |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1755 | void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end, |
| 1756 | u64 delalloc_end, struct page *locked_page, |
| 1757 | unsigned clear_bits, |
| 1758 | unsigned long page_ops) |
| 1759 | { |
| 1760 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 1761 | NULL); |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1762 | |
| 1763 | __process_pages_contig(inode->i_mapping, locked_page, |
| 1764 | start >> PAGE_SHIFT, end >> PAGE_SHIFT, |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1765 | page_ops, NULL); |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1766 | } |
| 1767 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1768 | /* |
| 1769 | * count the number of bytes in the tree that have a given bit(s) |
| 1770 | * set. This can be fairly slow, except for EXTENT_DIRTY which is |
| 1771 | * cached. The total number found is returned. |
| 1772 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1773 | u64 count_range_bits(struct extent_io_tree *tree, |
| 1774 | u64 *start, u64 search_end, u64 max_bytes, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1775 | unsigned bits, int contig) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1776 | { |
| 1777 | struct rb_node *node; |
| 1778 | struct extent_state *state; |
| 1779 | u64 cur_start = *start; |
| 1780 | u64 total_bytes = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1781 | u64 last = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1782 | int found = 0; |
| 1783 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 1784 | if (WARN_ON(search_end <= cur_start)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1785 | return 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1786 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1787 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1788 | if (cur_start == 0 && bits == EXTENT_DIRTY) { |
| 1789 | total_bytes = tree->dirty_bytes; |
| 1790 | goto out; |
| 1791 | } |
| 1792 | /* |
| 1793 | * this search will find all the extents that end after |
| 1794 | * our range starts. |
| 1795 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1796 | node = tree_search(tree, cur_start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1797 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1798 | goto out; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1799 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1800 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1801 | state = rb_entry(node, struct extent_state, rb_node); |
| 1802 | if (state->start > search_end) |
| 1803 | break; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1804 | if (contig && found && state->start > last + 1) |
| 1805 | break; |
| 1806 | if (state->end >= cur_start && (state->state & bits) == bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1807 | total_bytes += min(search_end, state->end) + 1 - |
| 1808 | max(cur_start, state->start); |
| 1809 | if (total_bytes >= max_bytes) |
| 1810 | break; |
| 1811 | if (!found) { |
Josef Bacik | af60bed | 2011-05-04 11:11:17 -0400 | [diff] [blame] | 1812 | *start = max(cur_start, state->start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1813 | found = 1; |
| 1814 | } |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1815 | last = state->end; |
| 1816 | } else if (contig && found) { |
| 1817 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1818 | } |
| 1819 | node = rb_next(node); |
| 1820 | if (!node) |
| 1821 | break; |
| 1822 | } |
| 1823 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1824 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1825 | return total_bytes; |
| 1826 | } |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1827 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1828 | /* |
| 1829 | * set the private field for a given byte offset in the tree. If there isn't |
| 1830 | * an extent_state there already, this does nothing. |
| 1831 | */ |
Arnd Bergmann | f827ba9 | 2016-02-22 22:53:20 +0100 | [diff] [blame] | 1832 | static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start, |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 1833 | struct io_failure_record *failrec) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1834 | { |
| 1835 | struct rb_node *node; |
| 1836 | struct extent_state *state; |
| 1837 | int ret = 0; |
| 1838 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1839 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1840 | /* |
| 1841 | * this search will find all the extents that end after |
| 1842 | * our range starts. |
| 1843 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1844 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1845 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1846 | ret = -ENOENT; |
| 1847 | goto out; |
| 1848 | } |
| 1849 | state = rb_entry(node, struct extent_state, rb_node); |
| 1850 | if (state->start != start) { |
| 1851 | ret = -ENOENT; |
| 1852 | goto out; |
| 1853 | } |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 1854 | state->failrec = failrec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1855 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1856 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1857 | return ret; |
| 1858 | } |
| 1859 | |
Arnd Bergmann | f827ba9 | 2016-02-22 22:53:20 +0100 | [diff] [blame] | 1860 | static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start, |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 1861 | struct io_failure_record **failrec) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1862 | { |
| 1863 | struct rb_node *node; |
| 1864 | struct extent_state *state; |
| 1865 | int ret = 0; |
| 1866 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1867 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1868 | /* |
| 1869 | * this search will find all the extents that end after |
| 1870 | * our range starts. |
| 1871 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1872 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1873 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1874 | ret = -ENOENT; |
| 1875 | goto out; |
| 1876 | } |
| 1877 | state = rb_entry(node, struct extent_state, rb_node); |
| 1878 | if (state->start != start) { |
| 1879 | ret = -ENOENT; |
| 1880 | goto out; |
| 1881 | } |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 1882 | *failrec = state->failrec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1883 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1884 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1885 | return ret; |
| 1886 | } |
| 1887 | |
| 1888 | /* |
| 1889 | * searches a range in the state tree for a given mask. |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1890 | * 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] | 1891 | * has the bits set. Otherwise, 1 is returned if any bit in the |
| 1892 | * range is found set. |
| 1893 | */ |
| 1894 | 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] | 1895 | unsigned bits, int filled, struct extent_state *cached) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1896 | { |
| 1897 | struct extent_state *state = NULL; |
| 1898 | struct rb_node *node; |
| 1899 | int bitset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1900 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1901 | spin_lock(&tree->lock); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1902 | if (cached && extent_state_in_tree(cached) && cached->start <= start && |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 1903 | cached->end > start) |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1904 | node = &cached->rb_node; |
| 1905 | else |
| 1906 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1907 | while (node && start <= end) { |
| 1908 | state = rb_entry(node, struct extent_state, rb_node); |
| 1909 | |
| 1910 | if (filled && state->start > start) { |
| 1911 | bitset = 0; |
| 1912 | break; |
| 1913 | } |
| 1914 | |
| 1915 | if (state->start > end) |
| 1916 | break; |
| 1917 | |
| 1918 | if (state->state & bits) { |
| 1919 | bitset = 1; |
| 1920 | if (!filled) |
| 1921 | break; |
| 1922 | } else if (filled) { |
| 1923 | bitset = 0; |
| 1924 | break; |
| 1925 | } |
Chris Mason | 46562ce | 2009-09-23 20:23:16 -0400 | [diff] [blame] | 1926 | |
| 1927 | if (state->end == (u64)-1) |
| 1928 | break; |
| 1929 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1930 | start = state->end + 1; |
| 1931 | if (start > end) |
| 1932 | break; |
| 1933 | node = rb_next(node); |
| 1934 | if (!node) { |
| 1935 | if (filled) |
| 1936 | bitset = 0; |
| 1937 | break; |
| 1938 | } |
| 1939 | } |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1940 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1941 | return bitset; |
| 1942 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1943 | |
| 1944 | /* |
| 1945 | * helper function to set a given page up to date if all the |
| 1946 | * extents in the tree for that page are up to date |
| 1947 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1948 | 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] | 1949 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 1950 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1951 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1952 | if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1953 | SetPageUptodate(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1954 | } |
| 1955 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 1956 | int free_io_failure(struct extent_io_tree *failure_tree, |
| 1957 | struct extent_io_tree *io_tree, |
| 1958 | struct io_failure_record *rec) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1959 | { |
| 1960 | int ret; |
| 1961 | int err = 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1962 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 1963 | set_state_failrec(failure_tree, rec->start, NULL); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1964 | ret = clear_extent_bits(failure_tree, rec->start, |
| 1965 | rec->start + rec->len - 1, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1966 | EXTENT_LOCKED | EXTENT_DIRTY); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1967 | if (ret) |
| 1968 | err = ret; |
| 1969 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 1970 | ret = clear_extent_bits(io_tree, rec->start, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1971 | rec->start + rec->len - 1, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1972 | EXTENT_DAMAGED); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1973 | if (ret && !err) |
| 1974 | err = ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1975 | |
| 1976 | kfree(rec); |
| 1977 | return err; |
| 1978 | } |
| 1979 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1980 | /* |
| 1981 | * this bypasses the standard btrfs submit functions deliberately, as |
| 1982 | * the standard behavior is to write all copies in a raid setup. here we only |
| 1983 | * want to write the one bad copy. so we do the mapping for ourselves and issue |
| 1984 | * submit_bio directly. |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1985 | * to avoid any synchronization issues, wait for the data after writing, which |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1986 | * actually prevents the read that triggered the error from finishing. |
| 1987 | * currently, there can be no more than two copies of every data bit. thus, |
| 1988 | * exactly one rewrite is required. |
| 1989 | */ |
Josef Bacik | 6ec656b | 2017-05-05 11:57:14 -0400 | [diff] [blame] | 1990 | int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start, |
| 1991 | u64 length, u64 logical, struct page *page, |
| 1992 | unsigned int pg_offset, int mirror_num) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1993 | { |
| 1994 | struct bio *bio; |
| 1995 | struct btrfs_device *dev; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1996 | u64 map_length = 0; |
| 1997 | u64 sector; |
| 1998 | struct btrfs_bio *bbio = NULL; |
| 1999 | int ret; |
| 2000 | |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2001 | ASSERT(!(fs_info->sb->s_flags & SB_RDONLY)); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2002 | BUG_ON(!mirror_num); |
| 2003 | |
David Sterba | c5e4c3d | 2017-06-12 17:29:41 +0200 | [diff] [blame] | 2004 | bio = btrfs_io_bio_alloc(1); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2005 | bio->bi_iter.bi_size = 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2006 | map_length = length; |
| 2007 | |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2008 | /* |
| 2009 | * Avoid races with device replace and make sure our bbio has devices |
| 2010 | * associated to its stripes that don't go away while we are doing the |
| 2011 | * read repair operation. |
| 2012 | */ |
| 2013 | btrfs_bio_counter_inc_blocked(fs_info); |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 2014 | if (btrfs_is_parity_mirror(fs_info, logical, length)) { |
Liu Bo | c725328 | 2017-03-29 10:53:58 -0700 | [diff] [blame] | 2015 | /* |
| 2016 | * Note that we don't use BTRFS_MAP_WRITE because it's supposed |
| 2017 | * to update all raid stripes, but here we just want to correct |
| 2018 | * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad |
| 2019 | * stripe's dev and sector. |
| 2020 | */ |
| 2021 | ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical, |
| 2022 | &map_length, &bbio, 0); |
| 2023 | if (ret) { |
| 2024 | btrfs_bio_counter_dec(fs_info); |
| 2025 | bio_put(bio); |
| 2026 | return -EIO; |
| 2027 | } |
| 2028 | ASSERT(bbio->mirror_num == 1); |
| 2029 | } else { |
| 2030 | ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical, |
| 2031 | &map_length, &bbio, mirror_num); |
| 2032 | if (ret) { |
| 2033 | btrfs_bio_counter_dec(fs_info); |
| 2034 | bio_put(bio); |
| 2035 | return -EIO; |
| 2036 | } |
| 2037 | BUG_ON(mirror_num != bbio->mirror_num); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2038 | } |
Liu Bo | c725328 | 2017-03-29 10:53:58 -0700 | [diff] [blame] | 2039 | |
| 2040 | sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2041 | bio->bi_iter.bi_sector = sector; |
Liu Bo | c725328 | 2017-03-29 10:53:58 -0700 | [diff] [blame] | 2042 | dev = bbio->stripes[bbio->mirror_num - 1].dev; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 2043 | btrfs_put_bbio(bbio); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2044 | if (!dev || !dev->bdev || |
| 2045 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) { |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2046 | btrfs_bio_counter_dec(fs_info); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2047 | bio_put(bio); |
| 2048 | return -EIO; |
| 2049 | } |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 2050 | bio_set_dev(bio, dev->bdev); |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 2051 | bio->bi_opf = REQ_OP_WRITE | REQ_SYNC; |
Miao Xie | ffdd201 | 2014-09-12 18:44:00 +0800 | [diff] [blame] | 2052 | bio_add_page(bio, page, length, pg_offset); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2053 | |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 2054 | if (btrfsic_submit_bio_wait(bio)) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2055 | /* try to remap that extent elsewhere? */ |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2056 | btrfs_bio_counter_dec(fs_info); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2057 | bio_put(bio); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2058 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2059 | return -EIO; |
| 2060 | } |
| 2061 | |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 2062 | btrfs_info_rl_in_rcu(fs_info, |
| 2063 | "read error corrected: ino %llu off %llu (dev %s sector %llu)", |
Josef Bacik | 6ec656b | 2017-05-05 11:57:14 -0400 | [diff] [blame] | 2064 | ino, start, |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2065 | rcu_str_deref(dev->name), sector); |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2066 | btrfs_bio_counter_dec(fs_info); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2067 | bio_put(bio); |
| 2068 | return 0; |
| 2069 | } |
| 2070 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2071 | int repair_eb_io_failure(struct btrfs_fs_info *fs_info, |
| 2072 | struct extent_buffer *eb, int mirror_num) |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2073 | { |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2074 | u64 start = eb->start; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 2075 | int i, num_pages = num_extent_pages(eb); |
Chris Mason | d95603b | 2012-04-12 15:55:15 -0400 | [diff] [blame] | 2076 | int ret = 0; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2077 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 2078 | if (sb_rdonly(fs_info->sb)) |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2079 | return -EROFS; |
| 2080 | |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2081 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 2082 | struct page *p = eb->pages[i]; |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2083 | |
Josef Bacik | 6ec656b | 2017-05-05 11:57:14 -0400 | [diff] [blame] | 2084 | ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p, |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2085 | start - page_offset(p), mirror_num); |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2086 | if (ret) |
| 2087 | break; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2088 | start += PAGE_SIZE; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | return ret; |
| 2092 | } |
| 2093 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2094 | /* |
| 2095 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 2096 | * to see if we need to process or clean up an io_failure_record |
| 2097 | */ |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2098 | int clean_io_failure(struct btrfs_fs_info *fs_info, |
| 2099 | struct extent_io_tree *failure_tree, |
| 2100 | struct extent_io_tree *io_tree, u64 start, |
| 2101 | struct page *page, u64 ino, unsigned int pg_offset) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2102 | { |
| 2103 | u64 private; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2104 | struct io_failure_record *failrec; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2105 | struct extent_state *state; |
| 2106 | int num_copies; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2107 | int ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2108 | |
| 2109 | private = 0; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2110 | ret = count_range_bits(failure_tree, &private, (u64)-1, 1, |
| 2111 | EXTENT_DIRTY, 0); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2112 | if (!ret) |
| 2113 | return 0; |
| 2114 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2115 | ret = get_state_failrec(failure_tree, start, &failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2116 | if (ret) |
| 2117 | return 0; |
| 2118 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2119 | BUG_ON(!failrec->this_mirror); |
| 2120 | |
| 2121 | if (failrec->in_validation) { |
| 2122 | /* there was no real error, just free the record */ |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2123 | btrfs_debug(fs_info, |
| 2124 | "clean_io_failure: freeing dummy error at %llu", |
| 2125 | failrec->start); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2126 | goto out; |
| 2127 | } |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 2128 | if (sb_rdonly(fs_info->sb)) |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2129 | goto out; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2130 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2131 | spin_lock(&io_tree->lock); |
| 2132 | state = find_first_extent_bit_state(io_tree, |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2133 | failrec->start, |
| 2134 | EXTENT_LOCKED); |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2135 | spin_unlock(&io_tree->lock); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2136 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2137 | if (state && state->start <= failrec->start && |
| 2138 | state->end >= failrec->start + failrec->len - 1) { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2139 | num_copies = btrfs_num_copies(fs_info, failrec->logical, |
| 2140 | failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2141 | if (num_copies > 1) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2142 | repair_io_failure(fs_info, ino, start, failrec->len, |
| 2143 | failrec->logical, page, pg_offset, |
| 2144 | failrec->failed_mirror); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | out: |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2149 | free_io_failure(failure_tree, io_tree, failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2150 | |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2151 | return 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2152 | } |
| 2153 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2154 | /* |
| 2155 | * Can be called when |
| 2156 | * - hold extent lock |
| 2157 | * - under ordered extent |
| 2158 | * - the inode is freeing |
| 2159 | */ |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 2160 | void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end) |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2161 | { |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 2162 | struct extent_io_tree *failure_tree = &inode->io_failure_tree; |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2163 | struct io_failure_record *failrec; |
| 2164 | struct extent_state *state, *next; |
| 2165 | |
| 2166 | if (RB_EMPTY_ROOT(&failure_tree->state)) |
| 2167 | return; |
| 2168 | |
| 2169 | spin_lock(&failure_tree->lock); |
| 2170 | state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY); |
| 2171 | while (state) { |
| 2172 | if (state->start > end) |
| 2173 | break; |
| 2174 | |
| 2175 | ASSERT(state->end <= end); |
| 2176 | |
| 2177 | next = next_state(state); |
| 2178 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2179 | failrec = state->failrec; |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2180 | free_extent_state(state); |
| 2181 | kfree(failrec); |
| 2182 | |
| 2183 | state = next; |
| 2184 | } |
| 2185 | spin_unlock(&failure_tree->lock); |
| 2186 | } |
| 2187 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2188 | int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end, |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2189 | struct io_failure_record **failrec_ret) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2190 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2191 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2192 | struct io_failure_record *failrec; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2193 | struct extent_map *em; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2194 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 2195 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 2196 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2197 | int ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2198 | u64 logical; |
| 2199 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2200 | ret = get_state_failrec(failure_tree, start, &failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2201 | if (ret) { |
| 2202 | failrec = kzalloc(sizeof(*failrec), GFP_NOFS); |
| 2203 | if (!failrec) |
| 2204 | return -ENOMEM; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2205 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2206 | failrec->start = start; |
| 2207 | failrec->len = end - start + 1; |
| 2208 | failrec->this_mirror = 0; |
| 2209 | failrec->bio_flags = 0; |
| 2210 | failrec->in_validation = 0; |
| 2211 | |
| 2212 | read_lock(&em_tree->lock); |
| 2213 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 2214 | if (!em) { |
| 2215 | read_unlock(&em_tree->lock); |
| 2216 | kfree(failrec); |
| 2217 | return -EIO; |
| 2218 | } |
| 2219 | |
Filipe David Borba Manana | 68ba990 | 2013-11-25 03:22:07 +0000 | [diff] [blame] | 2220 | if (em->start > start || em->start + em->len <= start) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2221 | free_extent_map(em); |
| 2222 | em = NULL; |
| 2223 | } |
| 2224 | read_unlock(&em_tree->lock); |
Tsutomu Itoh | 7a2d6a6 | 2012-10-01 03:07:15 -0600 | [diff] [blame] | 2225 | if (!em) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2226 | kfree(failrec); |
| 2227 | return -EIO; |
| 2228 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2229 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2230 | logical = start - em->start; |
| 2231 | logical = em->block_start + logical; |
| 2232 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 2233 | logical = em->block_start; |
| 2234 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
| 2235 | extent_set_compress_type(&failrec->bio_flags, |
| 2236 | em->compress_type); |
| 2237 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2238 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2239 | btrfs_debug(fs_info, |
| 2240 | "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu", |
| 2241 | logical, start, failrec->len); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2242 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2243 | failrec->logical = logical; |
| 2244 | free_extent_map(em); |
| 2245 | |
| 2246 | /* set the bits in the private failure tree */ |
| 2247 | ret = set_extent_bits(failure_tree, start, end, |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2248 | EXTENT_LOCKED | EXTENT_DIRTY); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2249 | if (ret >= 0) |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2250 | ret = set_state_failrec(failure_tree, start, failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2251 | /* set the bits in the inode's tree */ |
| 2252 | if (ret >= 0) |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2253 | ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2254 | if (ret < 0) { |
| 2255 | kfree(failrec); |
| 2256 | return ret; |
| 2257 | } |
| 2258 | } else { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2259 | btrfs_debug(fs_info, |
| 2260 | "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d", |
| 2261 | failrec->logical, failrec->start, failrec->len, |
| 2262 | failrec->in_validation); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2263 | /* |
| 2264 | * when data can be on disk more than twice, add to failrec here |
| 2265 | * (e.g. with a list for failed_mirror) to make |
| 2266 | * clean_io_failure() clean all those errors at once. |
| 2267 | */ |
| 2268 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2269 | |
| 2270 | *failrec_ret = failrec; |
| 2271 | |
| 2272 | return 0; |
| 2273 | } |
| 2274 | |
Ming Lei | a0b60d7 | 2017-12-18 20:22:11 +0800 | [diff] [blame] | 2275 | bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages, |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2276 | struct io_failure_record *failrec, int failed_mirror) |
| 2277 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2278 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2279 | int num_copies; |
| 2280 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2281 | num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2282 | if (num_copies == 1) { |
| 2283 | /* |
| 2284 | * we only have a single copy of the data, so don't bother with |
| 2285 | * all the retry and error correction code that follows. no |
| 2286 | * matter what the error is, it is very likely to persist. |
| 2287 | */ |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2288 | btrfs_debug(fs_info, |
| 2289 | "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", |
| 2290 | num_copies, failrec->this_mirror, failed_mirror); |
Liu Bo | c3cfb65 | 2017-07-13 15:00:50 -0700 | [diff] [blame] | 2291 | return false; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2292 | } |
| 2293 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2294 | /* |
| 2295 | * there are two premises: |
| 2296 | * a) deliver good data to the caller |
| 2297 | * b) correct the bad sectors on disk |
| 2298 | */ |
Ming Lei | a0b60d7 | 2017-12-18 20:22:11 +0800 | [diff] [blame] | 2299 | if (failed_bio_pages > 1) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2300 | /* |
| 2301 | * to fulfill b), we need to know the exact failing sectors, as |
| 2302 | * we don't want to rewrite any more than the failed ones. thus, |
| 2303 | * we need separate read requests for the failed bio |
| 2304 | * |
| 2305 | * if the following BUG_ON triggers, our validation request got |
| 2306 | * merged. we need separate requests for our algorithm to work. |
| 2307 | */ |
| 2308 | BUG_ON(failrec->in_validation); |
| 2309 | failrec->in_validation = 1; |
| 2310 | failrec->this_mirror = failed_mirror; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2311 | } else { |
| 2312 | /* |
| 2313 | * we're ready to fulfill a) and b) alongside. get a good copy |
| 2314 | * of the failed sector and if we succeed, we have setup |
| 2315 | * everything for repair_io_failure to do the rest for us. |
| 2316 | */ |
| 2317 | if (failrec->in_validation) { |
| 2318 | BUG_ON(failrec->this_mirror != failed_mirror); |
| 2319 | failrec->in_validation = 0; |
| 2320 | failrec->this_mirror = 0; |
| 2321 | } |
| 2322 | failrec->failed_mirror = failed_mirror; |
| 2323 | failrec->this_mirror++; |
| 2324 | if (failrec->this_mirror == failed_mirror) |
| 2325 | failrec->this_mirror++; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2326 | } |
| 2327 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2328 | if (failrec->this_mirror > num_copies) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2329 | btrfs_debug(fs_info, |
| 2330 | "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", |
| 2331 | num_copies, failrec->this_mirror, failed_mirror); |
Liu Bo | c3cfb65 | 2017-07-13 15:00:50 -0700 | [diff] [blame] | 2332 | return false; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2333 | } |
| 2334 | |
Liu Bo | c3cfb65 | 2017-07-13 15:00:50 -0700 | [diff] [blame] | 2335 | return true; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | |
| 2339 | struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio, |
| 2340 | struct io_failure_record *failrec, |
| 2341 | struct page *page, int pg_offset, int icsum, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2342 | bio_end_io_t *endio_func, void *data) |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2343 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2344 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2345 | struct bio *bio; |
| 2346 | struct btrfs_io_bio *btrfs_failed_bio; |
| 2347 | struct btrfs_io_bio *btrfs_bio; |
| 2348 | |
David Sterba | c5e4c3d | 2017-06-12 17:29:41 +0200 | [diff] [blame] | 2349 | bio = btrfs_io_bio_alloc(1); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2350 | bio->bi_end_io = endio_func; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2351 | bio->bi_iter.bi_sector = failrec->logical >> 9; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 2352 | bio_set_dev(bio, fs_info->fs_devices->latest_bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2353 | bio->bi_iter.bi_size = 0; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2354 | bio->bi_private = data; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2355 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2356 | btrfs_failed_bio = btrfs_io_bio(failed_bio); |
| 2357 | if (btrfs_failed_bio->csum) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2358 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
| 2359 | |
| 2360 | btrfs_bio = btrfs_io_bio(bio); |
| 2361 | btrfs_bio->csum = btrfs_bio->csum_inline; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2362 | icsum *= csum_size; |
| 2363 | memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum, |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2364 | csum_size); |
| 2365 | } |
| 2366 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2367 | bio_add_page(bio, page, failrec->len, pg_offset); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2368 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2369 | return bio; |
| 2370 | } |
| 2371 | |
| 2372 | /* |
Nikolay Borisov | 78e62c0 | 2018-11-22 10:17:49 +0200 | [diff] [blame] | 2373 | * This is a generic handler for readpage errors. If other copies exist, read |
| 2374 | * those and write back good data to the failed position. Does not investigate |
| 2375 | * in remapping the failed extent elsewhere, hoping the device will be smart |
| 2376 | * enough to do this as needed |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2377 | */ |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2378 | static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset, |
| 2379 | struct page *page, u64 start, u64 end, |
| 2380 | int failed_mirror) |
| 2381 | { |
| 2382 | struct io_failure_record *failrec; |
| 2383 | struct inode *inode = page->mapping->host; |
| 2384 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2385 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2386 | struct bio *bio; |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 2387 | int read_mode = 0; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2388 | blk_status_t status; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2389 | int ret; |
Christoph Hellwig | 8a2ee44 | 2019-02-15 19:13:07 +0800 | [diff] [blame] | 2390 | unsigned failed_bio_pages = failed_bio->bi_iter.bi_size >> PAGE_SHIFT; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2391 | |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2392 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2393 | |
| 2394 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 2395 | if (ret) |
| 2396 | return ret; |
| 2397 | |
Ming Lei | a0b60d7 | 2017-12-18 20:22:11 +0800 | [diff] [blame] | 2398 | if (!btrfs_check_repairable(inode, failed_bio_pages, failrec, |
Liu Bo | c3cfb65 | 2017-07-13 15:00:50 -0700 | [diff] [blame] | 2399 | failed_mirror)) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2400 | free_io_failure(failure_tree, tree, failrec); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2401 | return -EIO; |
| 2402 | } |
| 2403 | |
Ming Lei | a0b60d7 | 2017-12-18 20:22:11 +0800 | [diff] [blame] | 2404 | if (failed_bio_pages > 1) |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 2405 | read_mode |= REQ_FAILFAST_DEV; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2406 | |
| 2407 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
| 2408 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
| 2409 | start - page_offset(page), |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2410 | (int)phy_offset, failed_bio->bi_end_io, |
| 2411 | NULL); |
David Sterba | ebcc326 | 2018-06-29 10:56:53 +0200 | [diff] [blame] | 2412 | bio->bi_opf = REQ_OP_READ | read_mode; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2413 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2414 | btrfs_debug(btrfs_sb(inode->i_sb), |
| 2415 | "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d", |
| 2416 | read_mode, failrec->this_mirror, failrec->in_validation); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2417 | |
Linus Torvalds | 8c27cb3 | 2017-07-05 16:41:23 -0700 | [diff] [blame] | 2418 | status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror, |
Tsutomu Itoh | 013bd4c | 2012-02-16 10:11:40 +0900 | [diff] [blame] | 2419 | failrec->bio_flags, 0); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2420 | if (status) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2421 | free_io_failure(failure_tree, tree, failrec); |
Miao Xie | 6c387ab | 2014-09-12 18:43:57 +0800 | [diff] [blame] | 2422 | bio_put(bio); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2423 | ret = blk_status_to_errno(status); |
Miao Xie | 6c387ab | 2014-09-12 18:43:57 +0800 | [diff] [blame] | 2424 | } |
| 2425 | |
Tsutomu Itoh | 013bd4c | 2012-02-16 10:11:40 +0900 | [diff] [blame] | 2426 | return ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2427 | } |
| 2428 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2429 | /* lots and lots of room for performance fixes in the end_bio funcs */ |
| 2430 | |
David Sterba | b5227c0 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 2431 | void end_extent_writepage(struct page *page, int err, u64 start, u64 end) |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2432 | { |
| 2433 | int uptodate = (err == 0); |
Eric Sandeen | 3e2426b | 2014-06-12 00:39:58 -0500 | [diff] [blame] | 2434 | int ret = 0; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2435 | |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 2436 | btrfs_writepage_endio_finish_ordered(page, start, end, uptodate); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2437 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2438 | if (!uptodate) { |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2439 | ClearPageUptodate(page); |
| 2440 | SetPageError(page); |
Colin Ian King | bff5baf | 2017-05-09 18:14:01 +0100 | [diff] [blame] | 2441 | ret = err < 0 ? err : -EIO; |
Liu Bo | 5dca6ee | 2014-05-12 12:47:36 +0800 | [diff] [blame] | 2442 | mapping_set_error(page->mapping, ret); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2443 | } |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2444 | } |
| 2445 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2446 | /* |
| 2447 | * after a writepage IO is done, we need to: |
| 2448 | * clear the uptodate bits on error |
| 2449 | * clear the writeback bits in the extent tree for this IO |
| 2450 | * end_page_writeback if the page has no more pending IO |
| 2451 | * |
| 2452 | * Scheduling is not allowed, so the extent state tree is expected |
| 2453 | * to have one and only one object corresponding to this IO. |
| 2454 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2455 | static void end_bio_extent_writepage(struct bio *bio) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2456 | { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2457 | int error = blk_status_to_errno(bio->bi_status); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2458 | struct bio_vec *bvec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2459 | u64 start; |
| 2460 | u64 end; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2461 | int i; |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 2462 | struct bvec_iter_all iter_all; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2463 | |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 2464 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 2465 | bio_for_each_segment_all(bvec, bio, i, iter_all) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2466 | struct page *page = bvec->bv_page; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2467 | struct inode *inode = page->mapping->host; |
| 2468 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2469 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2470 | /* We always issue full-page reads, but if some block |
| 2471 | * in a page fails to read, blk_update_request() will |
| 2472 | * advance bv_offset and adjust bv_len to compensate. |
| 2473 | * Print a warning for nonzero offsets, and an error |
| 2474 | * if they don't add up to a full page. */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2475 | if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) { |
| 2476 | if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2477 | btrfs_err(fs_info, |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2478 | "partial page write in btrfs with offset %u and length %u", |
| 2479 | bvec->bv_offset, bvec->bv_len); |
| 2480 | else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2481 | btrfs_info(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2482 | "incomplete page write in btrfs with offset %u and length %u", |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2483 | bvec->bv_offset, bvec->bv_len); |
| 2484 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2485 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2486 | start = page_offset(page); |
| 2487 | end = start + bvec->bv_offset + bvec->bv_len - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2488 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2489 | end_extent_writepage(page, error, start, end); |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2490 | end_page_writeback(page); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2491 | } |
Chris Mason | 2b1f55b | 2008-09-24 11:48:04 -0400 | [diff] [blame] | 2492 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2493 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2494 | } |
| 2495 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2496 | static void |
| 2497 | endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len, |
| 2498 | int uptodate) |
| 2499 | { |
| 2500 | struct extent_state *cached = NULL; |
| 2501 | u64 end = start + len - 1; |
| 2502 | |
| 2503 | if (uptodate && tree->track_uptodate) |
| 2504 | set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC); |
David Sterba | d810a4b | 2017-12-07 18:52:54 +0100 | [diff] [blame] | 2505 | unlock_extent_cached_atomic(tree, start, end, &cached); |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2506 | } |
| 2507 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2508 | /* |
| 2509 | * after a readpage IO is done, we need to: |
| 2510 | * clear the uptodate bits on error |
| 2511 | * set the uptodate bits if things worked |
| 2512 | * set the page up to date if all extents in the tree are uptodate |
| 2513 | * clear the lock bit in the extent tree |
| 2514 | * unlock the page if there are no other extents locked for it |
| 2515 | * |
| 2516 | * Scheduling is not allowed, so the extent state tree is expected |
| 2517 | * to have one and only one object corresponding to this IO. |
| 2518 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2519 | static void end_bio_extent_readpage(struct bio *bio) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2520 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2521 | struct bio_vec *bvec; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2522 | int uptodate = !bio->bi_status; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2523 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2524 | struct extent_io_tree *tree, *failure_tree; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2525 | u64 offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2526 | u64 start; |
| 2527 | u64 end; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2528 | u64 len; |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2529 | u64 extent_start = 0; |
| 2530 | u64 extent_len = 0; |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2531 | int mirror; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2532 | int ret; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2533 | int i; |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 2534 | struct bvec_iter_all iter_all; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2535 | |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 2536 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 2537 | bio_for_each_segment_all(bvec, bio, i, iter_all) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2538 | struct page *page = bvec->bv_page; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2539 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2540 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Nikolay Borisov | 78e62c0 | 2018-11-22 10:17:49 +0200 | [diff] [blame] | 2541 | bool data_inode = btrfs_ino(BTRFS_I(inode)) |
| 2542 | != BTRFS_BTREE_INODE_OBJECTID; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2543 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2544 | btrfs_debug(fs_info, |
| 2545 | "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u", |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2546 | (u64)bio->bi_iter.bi_sector, bio->bi_status, |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2547 | io_bio->mirror_num); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2548 | tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2549 | failure_tree = &BTRFS_I(inode)->io_failure_tree; |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2550 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2551 | /* We always issue full-page reads, but if some block |
| 2552 | * in a page fails to read, blk_update_request() will |
| 2553 | * advance bv_offset and adjust bv_len to compensate. |
| 2554 | * Print a warning for nonzero offsets, and an error |
| 2555 | * if they don't add up to a full page. */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2556 | if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) { |
| 2557 | if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2558 | btrfs_err(fs_info, |
| 2559 | "partial page read in btrfs with offset %u and length %u", |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2560 | bvec->bv_offset, bvec->bv_len); |
| 2561 | else |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2562 | btrfs_info(fs_info, |
| 2563 | "incomplete page read in btrfs with offset %u and length %u", |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2564 | bvec->bv_offset, bvec->bv_len); |
| 2565 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2566 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2567 | start = page_offset(page); |
| 2568 | end = start + bvec->bv_offset + bvec->bv_len - 1; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2569 | len = bvec->bv_len; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2570 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2571 | mirror = io_bio->mirror_num; |
Nikolay Borisov | 78e62c0 | 2018-11-22 10:17:49 +0200 | [diff] [blame] | 2572 | if (likely(uptodate)) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2573 | ret = tree->ops->readpage_end_io_hook(io_bio, offset, |
| 2574 | page, start, end, |
| 2575 | mirror); |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2576 | if (ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2577 | uptodate = 0; |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2578 | else |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2579 | clean_io_failure(BTRFS_I(inode)->root->fs_info, |
| 2580 | failure_tree, tree, start, |
| 2581 | page, |
| 2582 | btrfs_ino(BTRFS_I(inode)), 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2583 | } |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2584 | |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2585 | if (likely(uptodate)) |
| 2586 | goto readpage_ok; |
| 2587 | |
Nikolay Borisov | 78e62c0 | 2018-11-22 10:17:49 +0200 | [diff] [blame] | 2588 | if (data_inode) { |
Liu Bo | 9d0d1c8 | 2017-03-24 15:04:50 -0700 | [diff] [blame] | 2589 | |
| 2590 | /* |
Nikolay Borisov | 78e62c0 | 2018-11-22 10:17:49 +0200 | [diff] [blame] | 2591 | * The generic bio_readpage_error handles errors the |
| 2592 | * following way: If possible, new read requests are |
| 2593 | * created and submitted and will end up in |
| 2594 | * end_bio_extent_readpage as well (if we're lucky, |
| 2595 | * not in the !uptodate case). In that case it returns |
| 2596 | * 0 and we just go on with the next page in our bio. |
| 2597 | * If it can't handle the error it will return -EIO and |
| 2598 | * we remain responsible for that page. |
Liu Bo | 9d0d1c8 | 2017-03-24 15:04:50 -0700 | [diff] [blame] | 2599 | */ |
Nikolay Borisov | 78e62c0 | 2018-11-22 10:17:49 +0200 | [diff] [blame] | 2600 | ret = bio_readpage_error(bio, offset, page, start, end, |
| 2601 | mirror); |
| 2602 | if (ret == 0) { |
| 2603 | uptodate = !bio->bi_status; |
| 2604 | offset += len; |
| 2605 | continue; |
| 2606 | } |
| 2607 | } else { |
| 2608 | struct extent_buffer *eb; |
| 2609 | |
| 2610 | eb = (struct extent_buffer *)page->private; |
| 2611 | set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); |
| 2612 | eb->read_mirror = mirror; |
| 2613 | atomic_dec(&eb->io_pages); |
| 2614 | if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, |
| 2615 | &eb->bflags)) |
| 2616 | btree_readahead_hook(eb, -EIO); |
| 2617 | |
| 2618 | ret = -EIO; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2619 | } |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2620 | readpage_ok: |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2621 | if (likely(uptodate)) { |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2622 | loff_t i_size = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2623 | pgoff_t end_index = i_size >> PAGE_SHIFT; |
Liu Bo | a583c02 | 2014-08-19 23:32:22 +0800 | [diff] [blame] | 2624 | unsigned off; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2625 | |
| 2626 | /* Zero out the end if this page straddles i_size */ |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 2627 | off = offset_in_page(i_size); |
Liu Bo | a583c02 | 2014-08-19 23:32:22 +0800 | [diff] [blame] | 2628 | if (page->index == end_index && off) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2629 | zero_user_segment(page, off, PAGE_SIZE); |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2630 | SetPageUptodate(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2631 | } else { |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2632 | ClearPageUptodate(page); |
| 2633 | SetPageError(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2634 | } |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2635 | unlock_page(page); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2636 | offset += len; |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2637 | |
| 2638 | if (unlikely(!uptodate)) { |
| 2639 | if (extent_len) { |
| 2640 | endio_readpage_release_extent(tree, |
| 2641 | extent_start, |
| 2642 | extent_len, 1); |
| 2643 | extent_start = 0; |
| 2644 | extent_len = 0; |
| 2645 | } |
| 2646 | endio_readpage_release_extent(tree, start, |
| 2647 | end - start + 1, 0); |
| 2648 | } else if (!extent_len) { |
| 2649 | extent_start = start; |
| 2650 | extent_len = end + 1 - start; |
| 2651 | } else if (extent_start + extent_len == start) { |
| 2652 | extent_len += end + 1 - start; |
| 2653 | } else { |
| 2654 | endio_readpage_release_extent(tree, extent_start, |
| 2655 | extent_len, uptodate); |
| 2656 | extent_start = start; |
| 2657 | extent_len = end + 1 - start; |
| 2658 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2659 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2660 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2661 | if (extent_len) |
| 2662 | endio_readpage_release_extent(tree, extent_start, extent_len, |
| 2663 | uptodate); |
David Sterba | b3a0dd5 | 2018-11-22 17:16:49 +0100 | [diff] [blame] | 2664 | btrfs_io_bio_free_csum(io_bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2665 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2666 | } |
| 2667 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2668 | /* |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2669 | * Initialize the members up to but not including 'bio'. Use after allocating a |
| 2670 | * new bio by bio_alloc_bioset as it does not initialize the bytes outside of |
| 2671 | * 'bio' because use of __GFP_ZERO is not supported. |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2672 | */ |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2673 | static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2674 | { |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2675 | memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio)); |
| 2676 | } |
| 2677 | |
| 2678 | /* |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2679 | * The following helpers allocate a bio. As it's backed by a bioset, it'll |
| 2680 | * never fail. We're returning a bio right now but you can call btrfs_io_bio |
| 2681 | * for the appropriate container_of magic |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2682 | */ |
David Sterba | c821e7f3 | 2017-06-02 18:35:36 +0200 | [diff] [blame] | 2683 | struct bio *btrfs_bio_alloc(struct block_device *bdev, u64 first_byte) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2684 | { |
| 2685 | struct bio *bio; |
| 2686 | |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 2687 | bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 2688 | bio_set_dev(bio, bdev); |
David Sterba | c821e7f3 | 2017-06-02 18:35:36 +0200 | [diff] [blame] | 2689 | bio->bi_iter.bi_sector = first_byte >> 9; |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2690 | btrfs_io_bio_init(btrfs_io_bio(bio)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2691 | return bio; |
| 2692 | } |
| 2693 | |
David Sterba | 8b6c1d5 | 2017-06-02 17:48:13 +0200 | [diff] [blame] | 2694 | struct bio *btrfs_bio_clone(struct bio *bio) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2695 | { |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 2696 | struct btrfs_io_bio *btrfs_bio; |
| 2697 | struct bio *new; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2698 | |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2699 | /* Bio allocation backed by a bioset does not fail */ |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 2700 | new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset); |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2701 | btrfs_bio = btrfs_io_bio(new); |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2702 | btrfs_io_bio_init(btrfs_bio); |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2703 | btrfs_bio->iter = bio->bi_iter; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 2704 | return new; |
| 2705 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2706 | |
David Sterba | c5e4c3d | 2017-06-12 17:29:41 +0200 | [diff] [blame] | 2707 | struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2708 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2709 | struct bio *bio; |
| 2710 | |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2711 | /* Bio allocation backed by a bioset does not fail */ |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 2712 | bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset); |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2713 | btrfs_io_bio_init(btrfs_io_bio(bio)); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2714 | return bio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2715 | } |
| 2716 | |
Liu Bo | e477094 | 2017-05-16 10:57:14 -0700 | [diff] [blame] | 2717 | struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size) |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 2718 | { |
| 2719 | struct bio *bio; |
| 2720 | struct btrfs_io_bio *btrfs_bio; |
| 2721 | |
| 2722 | /* this will never fail when it's backed by a bioset */ |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 2723 | bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset); |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 2724 | ASSERT(bio); |
| 2725 | |
| 2726 | btrfs_bio = btrfs_io_bio(bio); |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2727 | btrfs_io_bio_init(btrfs_bio); |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 2728 | |
| 2729 | bio_trim(bio, offset >> 9, size >> 9); |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 2730 | btrfs_bio->iter = bio->bi_iter; |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 2731 | return bio; |
| 2732 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2733 | |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 2734 | /* |
| 2735 | * @opf: bio REQ_OP_* and REQ_* flags as one value |
David Sterba | b8b3d62 | 2017-06-12 19:50:41 +0200 | [diff] [blame] | 2736 | * @tree: tree so we can call our merge_bio hook |
| 2737 | * @wbc: optional writeback control for io accounting |
| 2738 | * @page: page to add to the bio |
| 2739 | * @pg_offset: offset of the new bio or to check whether we are adding |
| 2740 | * a contiguous page to the previous one |
| 2741 | * @size: portion of page that we want to write |
| 2742 | * @offset: starting offset in the page |
| 2743 | * @bdev: attach newly created bios to this bdev |
David Sterba | 5c2b1fd | 2017-06-06 19:22:55 +0200 | [diff] [blame] | 2744 | * @bio_ret: must be valid pointer, newly allocated bio will be stored there |
David Sterba | b8b3d62 | 2017-06-12 19:50:41 +0200 | [diff] [blame] | 2745 | * @end_io_func: end_io callback for new bio |
| 2746 | * @mirror_num: desired mirror to read/write |
| 2747 | * @prev_bio_flags: flags of previous bio to see if we can merge the current one |
| 2748 | * @bio_flags: flags of the current bio to see if we can merge them |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 2749 | */ |
| 2750 | static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree, |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2751 | struct writeback_control *wbc, |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 2752 | struct page *page, u64 offset, |
David Sterba | 6c5a4e2 | 2017-10-04 17:10:34 +0200 | [diff] [blame] | 2753 | size_t size, unsigned long pg_offset, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2754 | struct block_device *bdev, |
| 2755 | struct bio **bio_ret, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2756 | bio_end_io_t end_io_func, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2757 | int mirror_num, |
| 2758 | unsigned long prev_bio_flags, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2759 | unsigned long bio_flags, |
| 2760 | bool force_bio_submit) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2761 | { |
| 2762 | int ret = 0; |
| 2763 | struct bio *bio; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2764 | size_t page_size = min_t(size_t, size, PAGE_SIZE); |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 2765 | sector_t sector = offset >> 9; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2766 | |
David Sterba | 5c2b1fd | 2017-06-06 19:22:55 +0200 | [diff] [blame] | 2767 | ASSERT(bio_ret); |
| 2768 | |
| 2769 | if (*bio_ret) { |
David Sterba | 0c8508a | 2017-06-12 20:00:43 +0200 | [diff] [blame] | 2770 | bool contig; |
| 2771 | bool can_merge = true; |
| 2772 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2773 | bio = *bio_ret; |
David Sterba | 0c8508a | 2017-06-12 20:00:43 +0200 | [diff] [blame] | 2774 | if (prev_bio_flags & EXTENT_BIO_COMPRESSED) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2775 | contig = bio->bi_iter.bi_sector == sector; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2776 | else |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 2777 | contig = bio_end_sector(bio) == sector; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2778 | |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2779 | ASSERT(tree->ops); |
| 2780 | if (btrfs_bio_fits_in_stripe(page, page_size, bio, bio_flags)) |
David Sterba | 0c8508a | 2017-06-12 20:00:43 +0200 | [diff] [blame] | 2781 | can_merge = false; |
| 2782 | |
| 2783 | if (prev_bio_flags != bio_flags || !contig || !can_merge || |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2784 | force_bio_submit || |
David Sterba | 6c5a4e2 | 2017-10-04 17:10:34 +0200 | [diff] [blame] | 2785 | bio_add_page(bio, page, page_size, pg_offset) < page_size) { |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2786 | ret = submit_one_bio(bio, mirror_num, prev_bio_flags); |
Naohiro Aota | 289454a | 2015-01-06 01:01:03 +0900 | [diff] [blame] | 2787 | if (ret < 0) { |
| 2788 | *bio_ret = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2789 | return ret; |
Naohiro Aota | 289454a | 2015-01-06 01:01:03 +0900 | [diff] [blame] | 2790 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2791 | bio = NULL; |
| 2792 | } else { |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2793 | if (wbc) |
| 2794 | wbc_account_io(wbc, page, page_size); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2795 | return 0; |
| 2796 | } |
| 2797 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2798 | |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 2799 | bio = btrfs_bio_alloc(bdev, offset); |
David Sterba | 6c5a4e2 | 2017-10-04 17:10:34 +0200 | [diff] [blame] | 2800 | bio_add_page(bio, page, page_size, pg_offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2801 | bio->bi_end_io = end_io_func; |
| 2802 | bio->bi_private = tree; |
Jens Axboe | e6959b9 | 2017-06-27 11:51:28 -0600 | [diff] [blame] | 2803 | bio->bi_write_hint = page->mapping->host->i_write_hint; |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 2804 | bio->bi_opf = opf; |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2805 | if (wbc) { |
| 2806 | wbc_init_bio(wbc, bio); |
| 2807 | wbc_account_io(wbc, page, page_size); |
| 2808 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2809 | |
David Sterba | 5c2b1fd | 2017-06-06 19:22:55 +0200 | [diff] [blame] | 2810 | *bio_ret = bio; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2811 | |
| 2812 | return ret; |
| 2813 | } |
| 2814 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 2815 | static void attach_extent_buffer_page(struct extent_buffer *eb, |
| 2816 | struct page *page) |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 2817 | { |
| 2818 | if (!PagePrivate(page)) { |
| 2819 | SetPagePrivate(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2820 | get_page(page); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 2821 | set_page_private(page, (unsigned long)eb); |
| 2822 | } else { |
| 2823 | WARN_ON(page->private != (unsigned long)eb); |
| 2824 | } |
| 2825 | } |
| 2826 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2827 | void set_page_extent_mapped(struct page *page) |
| 2828 | { |
| 2829 | if (!PagePrivate(page)) { |
| 2830 | SetPagePrivate(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2831 | get_page(page); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 2832 | set_page_private(page, EXTENT_PAGE_PRIVATE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2833 | } |
| 2834 | } |
| 2835 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2836 | static struct extent_map * |
| 2837 | __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset, |
| 2838 | u64 start, u64 len, get_extent_t *get_extent, |
| 2839 | struct extent_map **em_cached) |
| 2840 | { |
| 2841 | struct extent_map *em; |
| 2842 | |
| 2843 | if (em_cached && *em_cached) { |
| 2844 | em = *em_cached; |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 2845 | if (extent_map_in_tree(em) && start >= em->start && |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2846 | start < extent_map_end(em)) { |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 2847 | refcount_inc(&em->refs); |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2848 | return em; |
| 2849 | } |
| 2850 | |
| 2851 | free_extent_map(em); |
| 2852 | *em_cached = NULL; |
| 2853 | } |
| 2854 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 2855 | em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0); |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2856 | if (em_cached && !IS_ERR_OR_NULL(em)) { |
| 2857 | BUG_ON(*em_cached); |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 2858 | refcount_inc(&em->refs); |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2859 | *em_cached = em; |
| 2860 | } |
| 2861 | return em; |
| 2862 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2863 | /* |
| 2864 | * basic readpage implementation. Locked extent state structs are inserted |
| 2865 | * into the tree that are removed when the IO is done (by the end_io |
| 2866 | * handlers) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2867 | * XXX JDM: This needs looking at to ensure proper page locking |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 2868 | * return 0 on success, otherwise return error |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2869 | */ |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2870 | static int __do_readpage(struct extent_io_tree *tree, |
| 2871 | struct page *page, |
| 2872 | get_extent_t *get_extent, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2873 | struct extent_map **em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2874 | struct bio **bio, int mirror_num, |
David Sterba | f1c77c5 | 2017-06-06 19:03:49 +0200 | [diff] [blame] | 2875 | unsigned long *bio_flags, unsigned int read_flags, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2876 | u64 *prev_em_start) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2877 | { |
| 2878 | struct inode *inode = page->mapping->host; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2879 | u64 start = page_offset(page); |
David Sterba | 8eec829 | 2017-06-06 19:50:13 +0200 | [diff] [blame] | 2880 | const u64 end = start + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2881 | u64 cur = start; |
| 2882 | u64 extent_offset; |
| 2883 | u64 last_byte = i_size_read(inode); |
| 2884 | u64 block_start; |
| 2885 | u64 cur_end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2886 | struct extent_map *em; |
| 2887 | struct block_device *bdev; |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 2888 | int ret = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2889 | int nr = 0; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2890 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2891 | size_t iosize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2892 | size_t disk_io_size; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2893 | size_t blocksize = inode->i_sb->s_blocksize; |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 2894 | unsigned long this_bio_flag = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2895 | |
| 2896 | set_page_extent_mapped(page); |
| 2897 | |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 2898 | if (!PageUptodate(page)) { |
| 2899 | if (cleancache_get_page(page) == 0) { |
| 2900 | BUG_ON(blocksize != PAGE_SIZE); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 2901 | unlock_extent(tree, start, end); |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 2902 | goto out; |
| 2903 | } |
| 2904 | } |
| 2905 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2906 | if (page->index == last_byte >> PAGE_SHIFT) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2907 | char *userpage; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 2908 | size_t zero_offset = offset_in_page(last_byte); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2909 | |
| 2910 | if (zero_offset) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2911 | iosize = PAGE_SIZE - zero_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2912 | userpage = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2913 | memset(userpage + zero_offset, 0, iosize); |
| 2914 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2915 | kunmap_atomic(userpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2916 | } |
| 2917 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2918 | while (cur <= end) { |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2919 | bool force_bio_submit = false; |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 2920 | u64 offset; |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 2921 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2922 | if (cur >= last_byte) { |
| 2923 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2924 | struct extent_state *cached = NULL; |
| 2925 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2926 | iosize = PAGE_SIZE - pg_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2927 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 2928 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2929 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2930 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2931 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2932 | &cached, GFP_NOFS); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 2933 | unlock_extent_cached(tree, cur, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 2934 | cur + iosize - 1, &cached); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2935 | break; |
| 2936 | } |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 2937 | em = __get_extent_map(inode, page, pg_offset, cur, |
| 2938 | end - cur + 1, get_extent, em_cached); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 2939 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2940 | SetPageError(page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 2941 | unlock_extent(tree, cur, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2942 | break; |
| 2943 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2944 | extent_offset = cur - em->start; |
| 2945 | BUG_ON(extent_map_end(em) <= cur); |
| 2946 | BUG_ON(end < cur); |
| 2947 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2948 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 2949 | this_bio_flag |= EXTENT_BIO_COMPRESSED; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2950 | extent_set_compress_type(&this_bio_flag, |
| 2951 | em->compress_type); |
| 2952 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2953 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2954 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
| 2955 | cur_end = min(extent_map_end(em) - 1, end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 2956 | iosize = ALIGN(iosize, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2957 | if (this_bio_flag & EXTENT_BIO_COMPRESSED) { |
| 2958 | disk_io_size = em->block_len; |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 2959 | offset = em->block_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2960 | } else { |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 2961 | offset = em->block_start + extent_offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2962 | disk_io_size = iosize; |
| 2963 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2964 | bdev = em->bdev; |
| 2965 | block_start = em->block_start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2966 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 2967 | block_start = EXTENT_MAP_HOLE; |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2968 | |
| 2969 | /* |
| 2970 | * If we have a file range that points to a compressed extent |
| 2971 | * and it's followed by a consecutive file range that points to |
| 2972 | * to the same compressed extent (possibly with a different |
| 2973 | * offset and/or length, so it either points to the whole extent |
| 2974 | * or only part of it), we must make sure we do not submit a |
| 2975 | * single bio to populate the pages for the 2 ranges because |
| 2976 | * this makes the compressed extent read zero out the pages |
| 2977 | * belonging to the 2nd range. Imagine the following scenario: |
| 2978 | * |
| 2979 | * File layout |
| 2980 | * [0 - 8K] [8K - 24K] |
| 2981 | * | | |
| 2982 | * | | |
| 2983 | * points to extent X, points to extent X, |
| 2984 | * offset 4K, length of 8K offset 0, length 16K |
| 2985 | * |
| 2986 | * [extent X, compressed length = 4K uncompressed length = 16K] |
| 2987 | * |
| 2988 | * If the bio to read the compressed extent covers both ranges, |
| 2989 | * it will decompress extent X into the pages belonging to the |
| 2990 | * first range and then it will stop, zeroing out the remaining |
| 2991 | * pages that belong to the other range that points to extent X. |
| 2992 | * So here we make sure we submit 2 bios, one for the first |
| 2993 | * range and another one for the third range. Both will target |
| 2994 | * the same physical extent from disk, but we can't currently |
| 2995 | * make the compressed bio endio callback populate the pages |
| 2996 | * for both ranges because each compressed bio is tightly |
| 2997 | * coupled with a single extent map, and each range can have |
| 2998 | * an extent map with a different offset value relative to the |
| 2999 | * uncompressed data of our extent and different lengths. This |
| 3000 | * is a corner case so we prioritize correctness over |
| 3001 | * non-optimal behavior (submitting 2 bios for the same extent). |
| 3002 | */ |
| 3003 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) && |
| 3004 | prev_em_start && *prev_em_start != (u64)-1 && |
Filipe Manana | 8e92821 | 2019-02-14 15:17:20 +0000 | [diff] [blame] | 3005 | *prev_em_start != em->start) |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3006 | force_bio_submit = true; |
| 3007 | |
| 3008 | if (prev_em_start) |
Filipe Manana | 8e92821 | 2019-02-14 15:17:20 +0000 | [diff] [blame] | 3009 | *prev_em_start = em->start; |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3010 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3011 | free_extent_map(em); |
| 3012 | em = NULL; |
| 3013 | |
| 3014 | /* we've found a hole, just zero and go on */ |
| 3015 | if (block_start == EXTENT_MAP_HOLE) { |
| 3016 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3017 | struct extent_state *cached = NULL; |
| 3018 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3019 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3020 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3021 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3022 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3023 | |
| 3024 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3025 | &cached, GFP_NOFS); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3026 | unlock_extent_cached(tree, cur, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 3027 | cur + iosize - 1, &cached); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3028 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3029 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3030 | continue; |
| 3031 | } |
| 3032 | /* the get_extent function already copied into the page */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3033 | if (test_range_bit(tree, cur, cur_end, |
| 3034 | EXTENT_UPTODATE, 1, NULL)) { |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 3035 | check_page_uptodate(tree, page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3036 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3037 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3038 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3039 | continue; |
| 3040 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3041 | /* we have an inline extent but it didn't get marked up |
| 3042 | * to date. Error out |
| 3043 | */ |
| 3044 | if (block_start == EXTENT_MAP_INLINE) { |
| 3045 | SetPageError(page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3046 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3047 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3048 | pg_offset += iosize; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3049 | continue; |
| 3050 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3051 | |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 3052 | ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL, |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 3053 | page, offset, disk_io_size, |
| 3054 | pg_offset, bdev, bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3055 | end_bio_extent_readpage, mirror_num, |
| 3056 | *bio_flags, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3057 | this_bio_flag, |
| 3058 | force_bio_submit); |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3059 | if (!ret) { |
| 3060 | nr++; |
| 3061 | *bio_flags = this_bio_flag; |
| 3062 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3063 | SetPageError(page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3064 | unlock_extent(tree, cur, cur + iosize - 1); |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 3065 | goto out; |
Josef Bacik | edd33c9 | 2012-10-05 16:40:32 -0400 | [diff] [blame] | 3066 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3067 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3068 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3069 | } |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 3070 | out: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3071 | if (!nr) { |
| 3072 | if (!PageError(page)) |
| 3073 | SetPageUptodate(page); |
| 3074 | unlock_page(page); |
| 3075 | } |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 3076 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3077 | } |
| 3078 | |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3079 | static inline void __do_contiguous_readpages(struct extent_io_tree *tree, |
| 3080 | struct page *pages[], int nr_pages, |
| 3081 | u64 start, u64 end, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3082 | struct extent_map **em_cached, |
Nikolay Borisov | d3fac6b | 2017-10-24 11:50:39 +0300 | [diff] [blame] | 3083 | struct bio **bio, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3084 | unsigned long *bio_flags, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3085 | u64 *prev_em_start) |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3086 | { |
| 3087 | struct inode *inode; |
| 3088 | struct btrfs_ordered_extent *ordered; |
| 3089 | int index; |
| 3090 | |
| 3091 | inode = pages[0]->mapping->host; |
| 3092 | while (1) { |
| 3093 | lock_extent(tree, start, end); |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 3094 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3095 | end - start + 1); |
| 3096 | if (!ordered) |
| 3097 | break; |
| 3098 | unlock_extent(tree, start, end); |
| 3099 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3100 | btrfs_put_ordered_extent(ordered); |
| 3101 | } |
| 3102 | |
| 3103 | for (index = 0; index < nr_pages; index++) { |
David Sterba | 4ef7769 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 3104 | __do_readpage(tree, pages[index], btrfs_get_extent, em_cached, |
Jens Axboe | 5e9d398 | 2018-08-17 15:45:39 -0700 | [diff] [blame] | 3105 | bio, 0, bio_flags, REQ_RAHEAD, prev_em_start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3106 | put_page(pages[index]); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3107 | } |
| 3108 | } |
| 3109 | |
| 3110 | static void __extent_readpages(struct extent_io_tree *tree, |
| 3111 | struct page *pages[], |
David Sterba | e4d17ef | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 3112 | int nr_pages, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3113 | struct extent_map **em_cached, |
Nikolay Borisov | d3fac6b | 2017-10-24 11:50:39 +0300 | [diff] [blame] | 3114 | struct bio **bio, unsigned long *bio_flags, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3115 | u64 *prev_em_start) |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3116 | { |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 3117 | u64 start = 0; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3118 | u64 end = 0; |
| 3119 | u64 page_start; |
| 3120 | int index; |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 3121 | int first_index = 0; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3122 | |
| 3123 | for (index = 0; index < nr_pages; index++) { |
| 3124 | page_start = page_offset(pages[index]); |
| 3125 | if (!end) { |
| 3126 | start = page_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3127 | end = start + PAGE_SIZE - 1; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3128 | first_index = index; |
| 3129 | } else if (end + 1 == page_start) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3130 | end += PAGE_SIZE; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3131 | } else { |
| 3132 | __do_contiguous_readpages(tree, &pages[first_index], |
| 3133 | index - first_index, start, |
David Sterba | 4ef7769 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 3134 | end, em_cached, |
Nikolay Borisov | d3fac6b | 2017-10-24 11:50:39 +0300 | [diff] [blame] | 3135 | bio, bio_flags, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3136 | prev_em_start); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3137 | start = page_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3138 | end = start + PAGE_SIZE - 1; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3139 | first_index = index; |
| 3140 | } |
| 3141 | } |
| 3142 | |
| 3143 | if (end) |
| 3144 | __do_contiguous_readpages(tree, &pages[first_index], |
| 3145 | index - first_index, start, |
David Sterba | 4ef7769 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 3146 | end, em_cached, bio, |
Nikolay Borisov | d3fac6b | 2017-10-24 11:50:39 +0300 | [diff] [blame] | 3147 | bio_flags, prev_em_start); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | static int __extent_read_full_page(struct extent_io_tree *tree, |
| 3151 | struct page *page, |
| 3152 | get_extent_t *get_extent, |
| 3153 | struct bio **bio, int mirror_num, |
David Sterba | f1c77c5 | 2017-06-06 19:03:49 +0200 | [diff] [blame] | 3154 | unsigned long *bio_flags, |
| 3155 | unsigned int read_flags) |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3156 | { |
| 3157 | struct inode *inode = page->mapping->host; |
| 3158 | struct btrfs_ordered_extent *ordered; |
| 3159 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3160 | u64 end = start + PAGE_SIZE - 1; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3161 | int ret; |
| 3162 | |
| 3163 | while (1) { |
| 3164 | lock_extent(tree, start, end); |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 3165 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3166 | PAGE_SIZE); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3167 | if (!ordered) |
| 3168 | break; |
| 3169 | unlock_extent(tree, start, end); |
| 3170 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3171 | btrfs_put_ordered_extent(ordered); |
| 3172 | } |
| 3173 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3174 | ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3175 | bio_flags, read_flags, NULL); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3176 | return ret; |
| 3177 | } |
| 3178 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3179 | 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] | 3180 | get_extent_t *get_extent, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3181 | { |
| 3182 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3183 | unsigned long bio_flags = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3184 | int ret; |
| 3185 | |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 3186 | ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3187 | &bio_flags, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3188 | if (bio) |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3189 | ret = submit_one_bio(bio, mirror_num, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3190 | return ret; |
| 3191 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3192 | |
David Sterba | 3d4b949 | 2017-02-10 19:33:41 +0100 | [diff] [blame] | 3193 | static void update_nr_written(struct writeback_control *wbc, |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3194 | unsigned long nr_written) |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3195 | { |
| 3196 | wbc->nr_to_write -= nr_written; |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3197 | } |
| 3198 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3199 | /* |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3200 | * helper for __extent_writepage, doing all of the delayed allocation setup. |
| 3201 | * |
Nikolay Borisov | 5eaad97 | 2018-11-01 14:09:46 +0200 | [diff] [blame] | 3202 | * This returns 1 if btrfs_run_delalloc_range function did all the work required |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3203 | * to write the page (copy into inline extent). In this case the IO has |
| 3204 | * been started and the page is already unlocked. |
| 3205 | * |
| 3206 | * This returns 0 if all went well (page still locked) |
| 3207 | * This returns < 0 if there were errors (page still locked) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3208 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3209 | static noinline_for_stack int writepage_delalloc(struct inode *inode, |
Nikolay Borisov | 8cc0237 | 2018-11-08 10:18:07 +0200 | [diff] [blame] | 3210 | struct page *page, struct writeback_control *wbc, |
| 3211 | u64 delalloc_start, unsigned long *nr_written) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3212 | { |
Nikolay Borisov | 8cc0237 | 2018-11-08 10:18:07 +0200 | [diff] [blame] | 3213 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3214 | u64 page_end = delalloc_start + PAGE_SIZE - 1; |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 3215 | bool found; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3216 | u64 delalloc_to_write = 0; |
| 3217 | u64 delalloc_end = 0; |
| 3218 | int ret; |
| 3219 | int page_started = 0; |
| 3220 | |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3221 | |
| 3222 | while (delalloc_end < page_end) { |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 3223 | found = find_lock_delalloc_range(inode, tree, |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3224 | page, |
| 3225 | &delalloc_start, |
Nikolay Borisov | 917aace | 2018-10-26 14:43:20 +0300 | [diff] [blame] | 3226 | &delalloc_end); |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 3227 | if (!found) { |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3228 | delalloc_start = delalloc_end + 1; |
| 3229 | continue; |
| 3230 | } |
Nikolay Borisov | 5eaad97 | 2018-11-01 14:09:46 +0200 | [diff] [blame] | 3231 | ret = btrfs_run_delalloc_range(inode, page, delalloc_start, |
| 3232 | delalloc_end, &page_started, nr_written, wbc); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3233 | /* File system has been set read-only */ |
| 3234 | if (ret) { |
| 3235 | SetPageError(page); |
Nikolay Borisov | 5eaad97 | 2018-11-01 14:09:46 +0200 | [diff] [blame] | 3236 | /* |
| 3237 | * btrfs_run_delalloc_range should return < 0 for error |
| 3238 | * but just in case, we use > 0 here meaning the IO is |
| 3239 | * started, so we don't want to return > 0 unless |
| 3240 | * things are going well. |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3241 | */ |
| 3242 | ret = ret < 0 ? ret : -EIO; |
| 3243 | goto done; |
| 3244 | } |
| 3245 | /* |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 3246 | * delalloc_end is already one less than the total length, so |
| 3247 | * we don't subtract one from PAGE_SIZE |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3248 | */ |
| 3249 | delalloc_to_write += (delalloc_end - delalloc_start + |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 3250 | PAGE_SIZE) >> PAGE_SHIFT; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3251 | delalloc_start = delalloc_end + 1; |
| 3252 | } |
| 3253 | if (wbc->nr_to_write < delalloc_to_write) { |
| 3254 | int thresh = 8192; |
| 3255 | |
| 3256 | if (delalloc_to_write < thresh * 2) |
| 3257 | thresh = delalloc_to_write; |
| 3258 | wbc->nr_to_write = min_t(u64, delalloc_to_write, |
| 3259 | thresh); |
| 3260 | } |
| 3261 | |
| 3262 | /* did the fill delalloc function already unlock and start |
| 3263 | * the IO? |
| 3264 | */ |
| 3265 | if (page_started) { |
| 3266 | /* |
| 3267 | * we've unlocked the page, so we can't update |
| 3268 | * the mapping's writeback index, just update |
| 3269 | * nr_to_write. |
| 3270 | */ |
| 3271 | wbc->nr_to_write -= *nr_written; |
| 3272 | return 1; |
| 3273 | } |
| 3274 | |
| 3275 | ret = 0; |
| 3276 | |
| 3277 | done: |
| 3278 | return ret; |
| 3279 | } |
| 3280 | |
| 3281 | /* |
| 3282 | * helper for __extent_writepage. This calls the writepage start hooks, |
| 3283 | * and does the loop to map the page into extents and bios. |
| 3284 | * |
| 3285 | * We return 1 if the IO is started and the page is unlocked, |
| 3286 | * 0 if all went well (page still locked) |
| 3287 | * < 0 if there were errors (page still locked) |
| 3288 | */ |
| 3289 | static noinline_for_stack int __extent_writepage_io(struct inode *inode, |
| 3290 | struct page *page, |
| 3291 | struct writeback_control *wbc, |
| 3292 | struct extent_page_data *epd, |
| 3293 | loff_t i_size, |
| 3294 | unsigned long nr_written, |
David Sterba | f1c77c5 | 2017-06-06 19:03:49 +0200 | [diff] [blame] | 3295 | unsigned int write_flags, int *nr_ret) |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3296 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3297 | struct extent_io_tree *tree = epd->tree; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3298 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3299 | u64 page_end = start + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3300 | u64 end; |
| 3301 | u64 cur = start; |
| 3302 | u64 extent_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3303 | u64 block_start; |
| 3304 | u64 iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3305 | struct extent_map *em; |
| 3306 | struct block_device *bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3307 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3308 | size_t blocksize; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3309 | int ret = 0; |
| 3310 | int nr = 0; |
| 3311 | bool compressed; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3312 | |
Nikolay Borisov | d75855b | 2018-11-01 14:09:47 +0200 | [diff] [blame] | 3313 | ret = btrfs_writepage_cow_fixup(page, start, page_end); |
| 3314 | if (ret) { |
| 3315 | /* Fixup worker will requeue */ |
| 3316 | if (ret == -EBUSY) |
| 3317 | wbc->pages_skipped++; |
| 3318 | else |
| 3319 | redirty_page_for_writepage(wbc, page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3320 | |
Nikolay Borisov | d75855b | 2018-11-01 14:09:47 +0200 | [diff] [blame] | 3321 | update_nr_written(wbc, nr_written); |
| 3322 | unlock_page(page); |
| 3323 | return 1; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3324 | } |
| 3325 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3326 | /* |
| 3327 | * we don't want to touch the inode after unlocking the page, |
| 3328 | * so we update the mapping writeback index now |
| 3329 | */ |
David Sterba | 3d4b949 | 2017-02-10 19:33:41 +0100 | [diff] [blame] | 3330 | update_nr_written(wbc, nr_written + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3331 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3332 | end = page_end; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3333 | if (i_size <= start) { |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 3334 | btrfs_writepage_endio_finish_ordered(page, start, page_end, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3335 | goto done; |
| 3336 | } |
| 3337 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3338 | blocksize = inode->i_sb->s_blocksize; |
| 3339 | |
| 3340 | while (cur <= end) { |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3341 | u64 em_end; |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 3342 | u64 offset; |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3343 | |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3344 | if (cur >= i_size) { |
Nikolay Borisov | 7087a9d | 2018-11-01 14:09:48 +0200 | [diff] [blame] | 3345 | btrfs_writepage_endio_finish_ordered(page, cur, |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 3346 | page_end, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3347 | break; |
| 3348 | } |
David Sterba | 3c98c62 | 2017-06-23 04:01:08 +0200 | [diff] [blame] | 3349 | em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, cur, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3350 | end - cur + 1, 1); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3351 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3352 | SetPageError(page); |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 3353 | ret = PTR_ERR_OR_ZERO(em); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3354 | break; |
| 3355 | } |
| 3356 | |
| 3357 | extent_offset = cur - em->start; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3358 | em_end = extent_map_end(em); |
| 3359 | BUG_ON(em_end <= cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3360 | BUG_ON(end < cur); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3361 | iosize = min(em_end - cur, end - cur + 1); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3362 | iosize = ALIGN(iosize, blocksize); |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 3363 | offset = em->block_start + extent_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3364 | bdev = em->bdev; |
| 3365 | block_start = em->block_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3366 | compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3367 | free_extent_map(em); |
| 3368 | em = NULL; |
| 3369 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3370 | /* |
| 3371 | * compressed and inline extents are written through other |
| 3372 | * paths in the FS |
| 3373 | */ |
| 3374 | if (compressed || block_start == EXTENT_MAP_HOLE || |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3375 | block_start == EXTENT_MAP_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3376 | /* |
| 3377 | * end_io notification does not happen here for |
| 3378 | * compressed extents |
| 3379 | */ |
Nikolay Borisov | 7087a9d | 2018-11-01 14:09:48 +0200 | [diff] [blame] | 3380 | if (!compressed) |
| 3381 | btrfs_writepage_endio_finish_ordered(page, cur, |
| 3382 | cur + iosize - 1, |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 3383 | 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3384 | else if (compressed) { |
| 3385 | /* we don't want to end_page_writeback on |
| 3386 | * a compressed extent. this happens |
| 3387 | * elsewhere |
| 3388 | */ |
| 3389 | nr++; |
| 3390 | } |
| 3391 | |
| 3392 | cur += iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3393 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3394 | continue; |
| 3395 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3396 | |
David Sterba | 5cdc84b | 2018-07-18 20:32:52 +0200 | [diff] [blame] | 3397 | btrfs_set_range_writeback(tree, cur, cur + iosize - 1); |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3398 | if (!PageWriteback(page)) { |
| 3399 | btrfs_err(BTRFS_I(inode)->root->fs_info, |
| 3400 | "page %lu not writeback, cur %llu end %llu", |
| 3401 | page->index, cur, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3402 | } |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3403 | |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 3404 | ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc, |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 3405 | page, offset, iosize, pg_offset, |
David Sterba | c2df8bb | 2017-02-10 19:29:38 +0100 | [diff] [blame] | 3406 | bdev, &epd->bio, |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3407 | end_bio_extent_writepage, |
| 3408 | 0, 0, 0, false); |
Takafumi Kubota | fe01aa6 | 2017-02-09 17:24:33 +0900 | [diff] [blame] | 3409 | if (ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3410 | SetPageError(page); |
Takafumi Kubota | fe01aa6 | 2017-02-09 17:24:33 +0900 | [diff] [blame] | 3411 | if (PageWriteback(page)) |
| 3412 | end_page_writeback(page); |
| 3413 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3414 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3415 | cur = cur + iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3416 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3417 | nr++; |
| 3418 | } |
| 3419 | done: |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3420 | *nr_ret = nr; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3421 | return ret; |
| 3422 | } |
| 3423 | |
| 3424 | /* |
| 3425 | * the writepage semantics are similar to regular writepage. extent |
| 3426 | * records are inserted to lock ranges in the tree, and as dirty areas |
| 3427 | * are found, they are marked writeback. Then the lock bits are removed |
| 3428 | * and the end_io handler clears the writeback ranges |
| 3429 | */ |
| 3430 | static int __extent_writepage(struct page *page, struct writeback_control *wbc, |
David Sterba | aab6e9e | 2017-11-30 18:00:02 +0100 | [diff] [blame] | 3431 | struct extent_page_data *epd) |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3432 | { |
| 3433 | struct inode *inode = page->mapping->host; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3434 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3435 | u64 page_end = start + PAGE_SIZE - 1; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3436 | int ret; |
| 3437 | int nr = 0; |
| 3438 | size_t pg_offset = 0; |
| 3439 | loff_t i_size = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3440 | unsigned long end_index = i_size >> PAGE_SHIFT; |
David Sterba | f1c77c5 | 2017-06-06 19:03:49 +0200 | [diff] [blame] | 3441 | unsigned int write_flags = 0; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3442 | unsigned long nr_written = 0; |
| 3443 | |
Liu Bo | ff40adf | 2017-08-24 18:19:48 -0600 | [diff] [blame] | 3444 | write_flags = wbc_to_write_flags(wbc); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3445 | |
| 3446 | trace___extent_writepage(page, inode, wbc); |
| 3447 | |
| 3448 | WARN_ON(!PageLocked(page)); |
| 3449 | |
| 3450 | ClearPageError(page); |
| 3451 | |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 3452 | pg_offset = offset_in_page(i_size); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3453 | if (page->index > end_index || |
| 3454 | (page->index == end_index && !pg_offset)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3455 | page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3456 | unlock_page(page); |
| 3457 | return 0; |
| 3458 | } |
| 3459 | |
| 3460 | if (page->index == end_index) { |
| 3461 | char *userpage; |
| 3462 | |
| 3463 | userpage = kmap_atomic(page); |
| 3464 | memset(userpage + pg_offset, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3465 | PAGE_SIZE - pg_offset); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3466 | kunmap_atomic(userpage); |
| 3467 | flush_dcache_page(page); |
| 3468 | } |
| 3469 | |
| 3470 | pg_offset = 0; |
| 3471 | |
| 3472 | set_page_extent_mapped(page); |
| 3473 | |
Nikolay Borisov | 7789a55 | 2018-11-08 10:18:06 +0200 | [diff] [blame] | 3474 | if (!epd->extent_locked) { |
Nikolay Borisov | 8cc0237 | 2018-11-08 10:18:07 +0200 | [diff] [blame] | 3475 | ret = writepage_delalloc(inode, page, wbc, start, &nr_written); |
Nikolay Borisov | 7789a55 | 2018-11-08 10:18:06 +0200 | [diff] [blame] | 3476 | if (ret == 1) |
| 3477 | goto done_unlocked; |
| 3478 | if (ret) |
| 3479 | goto done; |
| 3480 | } |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3481 | |
| 3482 | ret = __extent_writepage_io(inode, page, wbc, epd, |
| 3483 | i_size, nr_written, write_flags, &nr); |
| 3484 | if (ret == 1) |
| 3485 | goto done_unlocked; |
| 3486 | |
| 3487 | done: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3488 | if (nr == 0) { |
| 3489 | /* make sure the mapping tag for page dirty gets cleared */ |
| 3490 | set_page_writeback(page); |
| 3491 | end_page_writeback(page); |
| 3492 | } |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 3493 | if (PageError(page)) { |
| 3494 | ret = ret < 0 ? ret : -EIO; |
| 3495 | end_extent_writepage(page, ret, start, page_end); |
| 3496 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3497 | unlock_page(page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3498 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3499 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3500 | done_unlocked: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3501 | return 0; |
| 3502 | } |
| 3503 | |
Josef Bacik | fd8b2b6 | 2013-04-24 16:41:19 -0400 | [diff] [blame] | 3504 | void wait_on_extent_buffer_writeback(struct extent_buffer *eb) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3505 | { |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 3506 | wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK, |
| 3507 | TASK_UNINTERRUPTIBLE); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3508 | } |
| 3509 | |
Chris Mason | 0e378df | 2014-05-19 20:55:27 -0700 | [diff] [blame] | 3510 | static noinline_for_stack int |
| 3511 | lock_extent_buffer_for_io(struct extent_buffer *eb, |
| 3512 | struct btrfs_fs_info *fs_info, |
| 3513 | struct extent_page_data *epd) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3514 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 3515 | int i, num_pages; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3516 | int flush = 0; |
| 3517 | int ret = 0; |
| 3518 | |
| 3519 | if (!btrfs_try_tree_write_lock(eb)) { |
| 3520 | flush = 1; |
| 3521 | flush_write_bio(epd); |
| 3522 | btrfs_tree_lock(eb); |
| 3523 | } |
| 3524 | |
| 3525 | if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) { |
| 3526 | btrfs_tree_unlock(eb); |
| 3527 | if (!epd->sync_io) |
| 3528 | return 0; |
| 3529 | if (!flush) { |
| 3530 | flush_write_bio(epd); |
| 3531 | flush = 1; |
| 3532 | } |
Chris Mason | a098d8e8 | 2012-03-21 12:09:56 -0400 | [diff] [blame] | 3533 | while (1) { |
| 3534 | wait_on_extent_buffer_writeback(eb); |
| 3535 | btrfs_tree_lock(eb); |
| 3536 | if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) |
| 3537 | break; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3538 | btrfs_tree_unlock(eb); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3539 | } |
| 3540 | } |
| 3541 | |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3542 | /* |
| 3543 | * We need to do this to prevent races in people who check if the eb is |
| 3544 | * under IO since we can end up having no IO bits set for a short period |
| 3545 | * of time. |
| 3546 | */ |
| 3547 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3548 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) { |
| 3549 | set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3550 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3551 | btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 3552 | percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, |
| 3553 | -eb->len, |
| 3554 | fs_info->dirty_metadata_batch); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3555 | ret = 1; |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3556 | } else { |
| 3557 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3558 | } |
| 3559 | |
| 3560 | btrfs_tree_unlock(eb); |
| 3561 | |
| 3562 | if (!ret) |
| 3563 | return ret; |
| 3564 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 3565 | num_pages = num_extent_pages(eb); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3566 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 3567 | struct page *p = eb->pages[i]; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3568 | |
| 3569 | if (!trylock_page(p)) { |
| 3570 | if (!flush) { |
| 3571 | flush_write_bio(epd); |
| 3572 | flush = 1; |
| 3573 | } |
| 3574 | lock_page(p); |
| 3575 | } |
| 3576 | } |
| 3577 | |
| 3578 | return ret; |
| 3579 | } |
| 3580 | |
| 3581 | static void end_extent_buffer_writeback(struct extent_buffer *eb) |
| 3582 | { |
| 3583 | clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 3584 | smp_mb__after_atomic(); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3585 | wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK); |
| 3586 | } |
| 3587 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3588 | static void set_btree_ioerr(struct page *page) |
| 3589 | { |
| 3590 | struct extent_buffer *eb = (struct extent_buffer *)page->private; |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3591 | |
| 3592 | SetPageError(page); |
| 3593 | if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) |
| 3594 | return; |
| 3595 | |
| 3596 | /* |
| 3597 | * If writeback for a btree extent that doesn't belong to a log tree |
| 3598 | * failed, increment the counter transaction->eb_write_errors. |
| 3599 | * We do this because while the transaction is running and before it's |
| 3600 | * committing (when we call filemap_fdata[write|wait]_range against |
| 3601 | * the btree inode), we might have |
| 3602 | * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it |
| 3603 | * returns an error or an error happens during writeback, when we're |
| 3604 | * committing the transaction we wouldn't know about it, since the pages |
| 3605 | * can be no longer dirty nor marked anymore for writeback (if a |
| 3606 | * subsequent modification to the extent buffer didn't happen before the |
| 3607 | * transaction commit), which makes filemap_fdata[write|wait]_range not |
| 3608 | * able to find the pages tagged with SetPageError at transaction |
| 3609 | * commit time. So if this happens we must abort the transaction, |
| 3610 | * otherwise we commit a super block with btree roots that point to |
| 3611 | * btree nodes/leafs whose content on disk is invalid - either garbage |
| 3612 | * or the content of some node/leaf from a past generation that got |
| 3613 | * cowed or deleted and is no longer valid. |
| 3614 | * |
| 3615 | * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would |
| 3616 | * not be enough - we need to distinguish between log tree extents vs |
| 3617 | * non-log tree extents, and the next filemap_fdatawait_range() call |
| 3618 | * will catch and clear such errors in the mapping - and that call might |
| 3619 | * be from a log sync and not from a transaction commit. Also, checking |
| 3620 | * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is |
| 3621 | * not done and would not be reliable - the eb might have been released |
| 3622 | * from memory and reading it back again means that flag would not be |
| 3623 | * set (since it's a runtime flag, not persisted on disk). |
| 3624 | * |
| 3625 | * Using the flags below in the btree inode also makes us achieve the |
| 3626 | * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started |
| 3627 | * writeback for all dirty pages and before filemap_fdatawait_range() |
| 3628 | * is called, the writeback for all dirty pages had already finished |
| 3629 | * with errors - because we were not using AS_EIO/AS_ENOSPC, |
| 3630 | * filemap_fdatawait_range() would return success, as it could not know |
| 3631 | * that writeback errors happened (the pages were no longer tagged for |
| 3632 | * writeback). |
| 3633 | */ |
| 3634 | switch (eb->log_index) { |
| 3635 | case -1: |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 3636 | set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3637 | break; |
| 3638 | case 0: |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 3639 | set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3640 | break; |
| 3641 | case 1: |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 3642 | set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3643 | break; |
| 3644 | default: |
| 3645 | BUG(); /* unexpected, logic error */ |
| 3646 | } |
| 3647 | } |
| 3648 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3649 | static void end_bio_extent_buffer_writepage(struct bio *bio) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3650 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3651 | struct bio_vec *bvec; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3652 | struct extent_buffer *eb; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3653 | int i, done; |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 3654 | struct bvec_iter_all iter_all; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3655 | |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 3656 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 3657 | bio_for_each_segment_all(bvec, bio, i, iter_all) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3658 | struct page *page = bvec->bv_page; |
| 3659 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3660 | eb = (struct extent_buffer *)page->private; |
| 3661 | BUG_ON(!eb); |
| 3662 | done = atomic_dec_and_test(&eb->io_pages); |
| 3663 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 3664 | if (bio->bi_status || |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3665 | test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3666 | ClearPageUptodate(page); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3667 | set_btree_ioerr(page); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | end_page_writeback(page); |
| 3671 | |
| 3672 | if (!done) |
| 3673 | continue; |
| 3674 | |
| 3675 | end_extent_buffer_writeback(eb); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3676 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3677 | |
| 3678 | bio_put(bio); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3679 | } |
| 3680 | |
Chris Mason | 0e378df | 2014-05-19 20:55:27 -0700 | [diff] [blame] | 3681 | static noinline_for_stack int write_one_eb(struct extent_buffer *eb, |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3682 | struct btrfs_fs_info *fs_info, |
| 3683 | struct writeback_control *wbc, |
| 3684 | struct extent_page_data *epd) |
| 3685 | { |
| 3686 | struct block_device *bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 3687 | 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] | 3688 | u64 offset = eb->start; |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3689 | u32 nritems; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 3690 | int i, num_pages; |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3691 | unsigned long start, end; |
Liu Bo | ff40adf | 2017-08-24 18:19:48 -0600 | [diff] [blame] | 3692 | unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META; |
Josef Bacik | d7dbe9e | 2012-04-23 14:00:51 -0400 | [diff] [blame] | 3693 | int ret = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3694 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3695 | clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags); |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 3696 | num_pages = num_extent_pages(eb); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3697 | atomic_set(&eb->io_pages, num_pages); |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3698 | |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3699 | /* set btree blocks beyond nritems with 0 to avoid stale content. */ |
| 3700 | nritems = btrfs_header_nritems(eb); |
Liu Bo | 3eb548e | 2016-09-14 17:22:57 -0700 | [diff] [blame] | 3701 | if (btrfs_header_level(eb) > 0) { |
Liu Bo | 3eb548e | 2016-09-14 17:22:57 -0700 | [diff] [blame] | 3702 | end = btrfs_node_key_ptr_offset(nritems); |
| 3703 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3704 | memzero_extent_buffer(eb, end, eb->len - end); |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3705 | } else { |
| 3706 | /* |
| 3707 | * leaf: |
| 3708 | * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0 |
| 3709 | */ |
| 3710 | start = btrfs_item_nr_offset(nritems); |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3711 | end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, eb); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3712 | memzero_extent_buffer(eb, start, end - start); |
Liu Bo | 3eb548e | 2016-09-14 17:22:57 -0700 | [diff] [blame] | 3713 | } |
| 3714 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3715 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 3716 | struct page *p = eb->pages[i]; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3717 | |
| 3718 | clear_page_dirty_for_io(p); |
| 3719 | set_page_writeback(p); |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 3720 | ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc, |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 3721 | p, offset, PAGE_SIZE, 0, bdev, |
David Sterba | c2df8bb | 2017-02-10 19:29:38 +0100 | [diff] [blame] | 3722 | &epd->bio, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3723 | end_bio_extent_buffer_writepage, |
Liu Bo | 18fdc67 | 2017-09-13 12:18:22 -0600 | [diff] [blame] | 3724 | 0, 0, 0, false); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3725 | if (ret) { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3726 | set_btree_ioerr(p); |
Takafumi Kubota | fe01aa6 | 2017-02-09 17:24:33 +0900 | [diff] [blame] | 3727 | if (PageWriteback(p)) |
| 3728 | end_page_writeback(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3729 | if (atomic_sub_and_test(num_pages - i, &eb->io_pages)) |
| 3730 | end_extent_buffer_writeback(eb); |
| 3731 | ret = -EIO; |
| 3732 | break; |
| 3733 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3734 | offset += PAGE_SIZE; |
David Sterba | 3d4b949 | 2017-02-10 19:33:41 +0100 | [diff] [blame] | 3735 | update_nr_written(wbc, 1); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3736 | unlock_page(p); |
| 3737 | } |
| 3738 | |
| 3739 | if (unlikely(ret)) { |
| 3740 | for (; i < num_pages; i++) { |
Chris Mason | bbf65cf | 2014-10-04 09:56:45 -0700 | [diff] [blame] | 3741 | struct page *p = eb->pages[i]; |
Liu Bo | 8146502 | 2014-09-23 22:22:33 +0800 | [diff] [blame] | 3742 | clear_page_dirty_for_io(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3743 | unlock_page(p); |
| 3744 | } |
| 3745 | } |
| 3746 | |
| 3747 | return ret; |
| 3748 | } |
| 3749 | |
| 3750 | int btree_write_cache_pages(struct address_space *mapping, |
| 3751 | struct writeback_control *wbc) |
| 3752 | { |
| 3753 | struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree; |
| 3754 | struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info; |
| 3755 | struct extent_buffer *eb, *prev_eb = NULL; |
| 3756 | struct extent_page_data epd = { |
| 3757 | .bio = NULL, |
| 3758 | .tree = tree, |
| 3759 | .extent_locked = 0, |
| 3760 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
| 3761 | }; |
| 3762 | int ret = 0; |
| 3763 | int done = 0; |
| 3764 | int nr_to_write_done = 0; |
| 3765 | struct pagevec pvec; |
| 3766 | int nr_pages; |
| 3767 | pgoff_t index; |
| 3768 | pgoff_t end; /* Inclusive */ |
| 3769 | int scanned = 0; |
Matthew Wilcox | 10bbd23 | 2017-12-05 17:30:38 -0500 | [diff] [blame] | 3770 | xa_mark_t tag; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3771 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 3772 | pagevec_init(&pvec); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3773 | if (wbc->range_cyclic) { |
| 3774 | index = mapping->writeback_index; /* Start from prev offset */ |
| 3775 | end = -1; |
| 3776 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3777 | index = wbc->range_start >> PAGE_SHIFT; |
| 3778 | end = wbc->range_end >> PAGE_SHIFT; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3779 | scanned = 1; |
| 3780 | } |
| 3781 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3782 | tag = PAGECACHE_TAG_TOWRITE; |
| 3783 | else |
| 3784 | tag = PAGECACHE_TAG_DIRTY; |
| 3785 | retry: |
| 3786 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3787 | tag_pages_for_writeback(mapping, index, end); |
| 3788 | while (!done && !nr_to_write_done && (index <= end) && |
Jan Kara | 4006f43 | 2017-11-15 17:34:37 -0800 | [diff] [blame] | 3789 | (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end, |
Jan Kara | 67fd707 | 2017-11-15 17:35:19 -0800 | [diff] [blame] | 3790 | tag))) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3791 | unsigned i; |
| 3792 | |
| 3793 | scanned = 1; |
| 3794 | for (i = 0; i < nr_pages; i++) { |
| 3795 | struct page *page = pvec.pages[i]; |
| 3796 | |
| 3797 | if (!PagePrivate(page)) |
| 3798 | continue; |
| 3799 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3800 | spin_lock(&mapping->private_lock); |
| 3801 | if (!PagePrivate(page)) { |
| 3802 | spin_unlock(&mapping->private_lock); |
| 3803 | continue; |
| 3804 | } |
| 3805 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3806 | eb = (struct extent_buffer *)page->private; |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3807 | |
| 3808 | /* |
| 3809 | * Shouldn't happen and normally this would be a BUG_ON |
| 3810 | * but no sense in crashing the users box for something |
| 3811 | * we can survive anyway. |
| 3812 | */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3813 | if (WARN_ON(!eb)) { |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3814 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3815 | continue; |
| 3816 | } |
| 3817 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3818 | if (eb == prev_eb) { |
| 3819 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3820 | continue; |
| 3821 | } |
| 3822 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3823 | ret = atomic_inc_not_zero(&eb->refs); |
| 3824 | spin_unlock(&mapping->private_lock); |
| 3825 | if (!ret) |
| 3826 | continue; |
| 3827 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3828 | prev_eb = eb; |
| 3829 | ret = lock_extent_buffer_for_io(eb, fs_info, &epd); |
| 3830 | if (!ret) { |
| 3831 | free_extent_buffer(eb); |
| 3832 | continue; |
| 3833 | } |
| 3834 | |
| 3835 | ret = write_one_eb(eb, fs_info, wbc, &epd); |
| 3836 | if (ret) { |
| 3837 | done = 1; |
| 3838 | free_extent_buffer(eb); |
| 3839 | break; |
| 3840 | } |
| 3841 | free_extent_buffer(eb); |
| 3842 | |
| 3843 | /* |
| 3844 | * the filesystem may choose to bump up nr_to_write. |
| 3845 | * We have to make sure to honor the new nr_to_write |
| 3846 | * at any time |
| 3847 | */ |
| 3848 | nr_to_write_done = wbc->nr_to_write <= 0; |
| 3849 | } |
| 3850 | pagevec_release(&pvec); |
| 3851 | cond_resched(); |
| 3852 | } |
| 3853 | if (!scanned && !done) { |
| 3854 | /* |
| 3855 | * We hit the last page and there is more work to be done: wrap |
| 3856 | * back to the start of the file |
| 3857 | */ |
| 3858 | scanned = 1; |
| 3859 | index = 0; |
| 3860 | goto retry; |
| 3861 | } |
| 3862 | flush_write_bio(&epd); |
| 3863 | return ret; |
| 3864 | } |
| 3865 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3866 | /** |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3867 | * write_cache_pages - walk the list of dirty pages of the given address space and write all of them. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3868 | * @mapping: address space structure to write |
| 3869 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
David Sterba | 935db85 | 2017-06-23 04:30:28 +0200 | [diff] [blame] | 3870 | * @data: data passed to __extent_writepage function |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3871 | * |
| 3872 | * If a page is already under I/O, write_cache_pages() skips it, even |
| 3873 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, |
| 3874 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() |
| 3875 | * and msync() need to guarantee that all the data which was dirty at the time |
| 3876 | * the call was made get new I/O started against them. If wbc->sync_mode is |
| 3877 | * WB_SYNC_ALL then we were called for data integrity and we must wait for |
| 3878 | * existing IO to complete. |
| 3879 | */ |
David Sterba | 4242b64 | 2017-02-10 19:38:24 +0100 | [diff] [blame] | 3880 | static int extent_write_cache_pages(struct address_space *mapping, |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3881 | struct writeback_control *wbc, |
David Sterba | aab6e9e | 2017-11-30 18:00:02 +0100 | [diff] [blame] | 3882 | struct extent_page_data *epd) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3883 | { |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 3884 | struct inode *inode = mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3885 | int ret = 0; |
| 3886 | int done = 0; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3887 | int nr_to_write_done = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3888 | struct pagevec pvec; |
| 3889 | int nr_pages; |
| 3890 | pgoff_t index; |
| 3891 | pgoff_t end; /* Inclusive */ |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3892 | pgoff_t done_index; |
| 3893 | int range_whole = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3894 | int scanned = 0; |
Matthew Wilcox | 10bbd23 | 2017-12-05 17:30:38 -0500 | [diff] [blame] | 3895 | xa_mark_t tag; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3896 | |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 3897 | /* |
| 3898 | * We have to hold onto the inode so that ordered extents can do their |
| 3899 | * work when the IO finishes. The alternative to this is failing to add |
| 3900 | * an ordered extent if the igrab() fails there and that is a huge pain |
| 3901 | * to deal with, so instead just hold onto the inode throughout the |
| 3902 | * writepages operation. If it fails here we are freeing up the inode |
| 3903 | * anyway and we'd rather not waste our time writing out stuff that is |
| 3904 | * going to be truncated anyway. |
| 3905 | */ |
| 3906 | if (!igrab(inode)) |
| 3907 | return 0; |
| 3908 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 3909 | pagevec_init(&pvec); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3910 | if (wbc->range_cyclic) { |
| 3911 | index = mapping->writeback_index; /* Start from prev offset */ |
| 3912 | end = -1; |
| 3913 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3914 | index = wbc->range_start >> PAGE_SHIFT; |
| 3915 | end = wbc->range_end >> PAGE_SHIFT; |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3916 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 3917 | range_whole = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3918 | scanned = 1; |
| 3919 | } |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 3920 | |
| 3921 | /* |
| 3922 | * We do the tagged writepage as long as the snapshot flush bit is set |
| 3923 | * and we are the first one who do the filemap_flush() on this inode. |
| 3924 | * |
| 3925 | * The nr_to_write == LONG_MAX is needed to make sure other flushers do |
| 3926 | * not race in and drop the bit. |
| 3927 | */ |
| 3928 | if (range_whole && wbc->nr_to_write == LONG_MAX && |
| 3929 | test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH, |
| 3930 | &BTRFS_I(inode)->runtime_flags)) |
| 3931 | wbc->tagged_writepages = 1; |
| 3932 | |
| 3933 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3934 | tag = PAGECACHE_TAG_TOWRITE; |
| 3935 | else |
| 3936 | tag = PAGECACHE_TAG_DIRTY; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3937 | retry: |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 3938 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 3939 | tag_pages_for_writeback(mapping, index, end); |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3940 | done_index = index; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 3941 | while (!done && !nr_to_write_done && (index <= end) && |
Jan Kara | 67fd707 | 2017-11-15 17:35:19 -0800 | [diff] [blame] | 3942 | (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, |
| 3943 | &index, end, tag))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3944 | unsigned i; |
| 3945 | |
| 3946 | scanned = 1; |
| 3947 | for (i = 0; i < nr_pages; i++) { |
| 3948 | struct page *page = pvec.pages[i]; |
| 3949 | |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3950 | done_index = page->index; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3951 | /* |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 3952 | * At this point we hold neither the i_pages lock nor |
| 3953 | * the page lock: the page may be truncated or |
| 3954 | * invalidated (changing page->mapping to NULL), |
| 3955 | * or even swizzled back from swapper_space to |
| 3956 | * tmpfs file mapping |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3957 | */ |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3958 | if (!trylock_page(page)) { |
David Sterba | aab6e9e | 2017-11-30 18:00:02 +0100 | [diff] [blame] | 3959 | flush_write_bio(epd); |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3960 | lock_page(page); |
Chris Mason | 01d658f | 2011-11-01 10:08:06 -0400 | [diff] [blame] | 3961 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3962 | |
| 3963 | if (unlikely(page->mapping != mapping)) { |
| 3964 | unlock_page(page); |
| 3965 | continue; |
| 3966 | } |
| 3967 | |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3968 | if (wbc->sync_mode != WB_SYNC_NONE) { |
Chris Mason | 0e6bd95 | 2008-11-20 10:46:35 -0500 | [diff] [blame] | 3969 | if (PageWriteback(page)) |
David Sterba | aab6e9e | 2017-11-30 18:00:02 +0100 | [diff] [blame] | 3970 | flush_write_bio(epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3971 | wait_on_page_writeback(page); |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 3972 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3973 | |
| 3974 | if (PageWriteback(page) || |
| 3975 | !clear_page_dirty_for_io(page)) { |
| 3976 | unlock_page(page); |
| 3977 | continue; |
| 3978 | } |
| 3979 | |
David Sterba | aab6e9e | 2017-11-30 18:00:02 +0100 | [diff] [blame] | 3980 | ret = __extent_writepage(page, wbc, epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3981 | |
| 3982 | if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) { |
| 3983 | unlock_page(page); |
| 3984 | ret = 0; |
| 3985 | } |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3986 | if (ret < 0) { |
| 3987 | /* |
| 3988 | * done_index is set past this page, |
| 3989 | * so media errors will not choke |
| 3990 | * background writeout for the entire |
| 3991 | * file. This has consequences for |
| 3992 | * range_cyclic semantics (ie. it may |
| 3993 | * not be suitable for data integrity |
| 3994 | * writeout). |
| 3995 | */ |
| 3996 | done_index = page->index + 1; |
| 3997 | done = 1; |
| 3998 | break; |
| 3999 | } |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 4000 | |
| 4001 | /* |
| 4002 | * the filesystem may choose to bump up nr_to_write. |
| 4003 | * We have to make sure to honor the new nr_to_write |
| 4004 | * at any time |
| 4005 | */ |
| 4006 | nr_to_write_done = wbc->nr_to_write <= 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4007 | } |
| 4008 | pagevec_release(&pvec); |
| 4009 | cond_resched(); |
| 4010 | } |
Liu Bo | 894b36e | 2016-03-07 16:56:22 -0800 | [diff] [blame] | 4011 | if (!scanned && !done) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4012 | /* |
| 4013 | * We hit the last page and there is more work to be done: wrap |
| 4014 | * back to the start of the file |
| 4015 | */ |
| 4016 | scanned = 1; |
| 4017 | index = 0; |
| 4018 | goto retry; |
| 4019 | } |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 4020 | |
| 4021 | if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole)) |
| 4022 | mapping->writeback_index = done_index; |
| 4023 | |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 4024 | btrfs_add_delayed_iput(inode); |
Liu Bo | 894b36e | 2016-03-07 16:56:22 -0800 | [diff] [blame] | 4025 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4026 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4027 | |
Nikolay Borisov | 0a9b0e5 | 2017-12-08 15:55:59 +0200 | [diff] [blame] | 4028 | int extent_write_full_page(struct page *page, struct writeback_control *wbc) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4029 | { |
| 4030 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4031 | struct extent_page_data epd = { |
| 4032 | .bio = NULL, |
Nikolay Borisov | 0a9b0e5 | 2017-12-08 15:55:59 +0200 | [diff] [blame] | 4033 | .tree = &BTRFS_I(page->mapping->host)->io_tree, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4034 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4035 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4036 | }; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4037 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4038 | ret = __extent_writepage(page, wbc, &epd); |
| 4039 | |
David Sterba | e2932ee | 2017-06-23 04:16:17 +0200 | [diff] [blame] | 4040 | flush_write_bio(&epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4041 | return ret; |
| 4042 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4043 | |
Nikolay Borisov | 5e3ee23 | 2017-12-08 15:55:58 +0200 | [diff] [blame] | 4044 | int extent_write_locked_range(struct inode *inode, u64 start, u64 end, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4045 | int mode) |
| 4046 | { |
| 4047 | int ret = 0; |
| 4048 | struct address_space *mapping = inode->i_mapping; |
Nikolay Borisov | 5e3ee23 | 2017-12-08 15:55:58 +0200 | [diff] [blame] | 4049 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4050 | struct page *page; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4051 | unsigned long nr_pages = (end - start + PAGE_SIZE) >> |
| 4052 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4053 | |
| 4054 | struct extent_page_data epd = { |
| 4055 | .bio = NULL, |
| 4056 | .tree = tree, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4057 | .extent_locked = 1, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4058 | .sync_io = mode == WB_SYNC_ALL, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4059 | }; |
| 4060 | struct writeback_control wbc_writepages = { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4061 | .sync_mode = mode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4062 | .nr_to_write = nr_pages * 2, |
| 4063 | .range_start = start, |
| 4064 | .range_end = end + 1, |
| 4065 | }; |
| 4066 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4067 | while (start <= end) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4068 | page = find_get_page(mapping, start >> PAGE_SHIFT); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4069 | if (clear_page_dirty_for_io(page)) |
| 4070 | ret = __extent_writepage(page, &wbc_writepages, &epd); |
| 4071 | else { |
Nikolay Borisov | 7087a9d | 2018-11-01 14:09:48 +0200 | [diff] [blame] | 4072 | btrfs_writepage_endio_finish_ordered(page, start, |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 4073 | start + PAGE_SIZE - 1, 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4074 | unlock_page(page); |
| 4075 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4076 | put_page(page); |
| 4077 | start += PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4078 | } |
| 4079 | |
David Sterba | e2932ee | 2017-06-23 04:16:17 +0200 | [diff] [blame] | 4080 | flush_write_bio(&epd); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4081 | return ret; |
| 4082 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4083 | |
Nikolay Borisov | 8ae225a | 2018-04-19 10:46:38 +0300 | [diff] [blame] | 4084 | int extent_writepages(struct address_space *mapping, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4085 | struct writeback_control *wbc) |
| 4086 | { |
| 4087 | int ret = 0; |
| 4088 | struct extent_page_data epd = { |
| 4089 | .bio = NULL, |
Nikolay Borisov | 8ae225a | 2018-04-19 10:46:38 +0300 | [diff] [blame] | 4090 | .tree = &BTRFS_I(mapping->host)->io_tree, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4091 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4092 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4093 | }; |
| 4094 | |
David Sterba | 935db85 | 2017-06-23 04:30:28 +0200 | [diff] [blame] | 4095 | ret = extent_write_cache_pages(mapping, wbc, &epd); |
David Sterba | e2932ee | 2017-06-23 04:16:17 +0200 | [diff] [blame] | 4096 | flush_write_bio(&epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4097 | return ret; |
| 4098 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4099 | |
Nikolay Borisov | 2a3ff0a | 2018-04-19 10:46:36 +0300 | [diff] [blame] | 4100 | int extent_readpages(struct address_space *mapping, struct list_head *pages, |
| 4101 | unsigned nr_pages) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4102 | { |
| 4103 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4104 | unsigned long bio_flags = 0; |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4105 | struct page *pagepool[16]; |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4106 | struct extent_map *em_cached = NULL; |
Nikolay Borisov | 2a3ff0a | 2018-04-19 10:46:36 +0300 | [diff] [blame] | 4107 | struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree; |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4108 | int nr = 0; |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 4109 | u64 prev_em_start = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4110 | |
Nikolay Borisov | 61ed3a1 | 2018-11-29 18:41:31 +0200 | [diff] [blame] | 4111 | while (!list_empty(pages)) { |
| 4112 | for (nr = 0; nr < ARRAY_SIZE(pagepool) && !list_empty(pages);) { |
Nikolay Borisov | f86196e | 2019-01-03 15:29:02 -0800 | [diff] [blame] | 4113 | struct page *page = lru_to_page(pages); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4114 | |
Nikolay Borisov | 61ed3a1 | 2018-11-29 18:41:31 +0200 | [diff] [blame] | 4115 | prefetchw(&page->flags); |
| 4116 | list_del(&page->lru); |
| 4117 | if (add_to_page_cache_lru(page, mapping, page->index, |
| 4118 | readahead_gfp_mask(mapping))) { |
| 4119 | put_page(page); |
| 4120 | continue; |
| 4121 | } |
| 4122 | |
| 4123 | pagepool[nr++] = page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4124 | } |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4125 | |
David Sterba | e4d17ef | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 4126 | __extent_readpages(tree, pagepool, nr, &em_cached, &bio, |
Nikolay Borisov | 61ed3a1 | 2018-11-29 18:41:31 +0200 | [diff] [blame] | 4127 | &bio_flags, &prev_em_start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4128 | } |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4129 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4130 | if (em_cached) |
| 4131 | free_extent_map(em_cached); |
| 4132 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4133 | if (bio) |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 4134 | return submit_one_bio(bio, 0, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4135 | return 0; |
| 4136 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4137 | |
| 4138 | /* |
| 4139 | * basic invalidatepage code, this waits on any locked or writeback |
| 4140 | * ranges corresponding to the page, and then deletes any extent state |
| 4141 | * records from the tree |
| 4142 | */ |
| 4143 | int extent_invalidatepage(struct extent_io_tree *tree, |
| 4144 | struct page *page, unsigned long offset) |
| 4145 | { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4146 | struct extent_state *cached_state = NULL; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4147 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4148 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4149 | size_t blocksize = page->mapping->host->i_sb->s_blocksize; |
| 4150 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4151 | start += ALIGN(offset, blocksize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4152 | if (start > end) |
| 4153 | return 0; |
| 4154 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 4155 | lock_extent_bits(tree, start, end, &cached_state); |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 4156 | wait_on_page_writeback(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4157 | clear_extent_bit(tree, start, end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 4158 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4159 | EXTENT_DO_ACCOUNTING, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 4160 | 1, 1, &cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4161 | return 0; |
| 4162 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4163 | |
| 4164 | /* |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4165 | * a helper for releasepage, this tests for areas of the page that |
| 4166 | * are locked or under IO and drops the related state bits if it is safe |
| 4167 | * to drop the page. |
| 4168 | */ |
Nikolay Borisov | 29c68b2d | 2018-04-19 10:46:35 +0300 | [diff] [blame] | 4169 | static int try_release_extent_state(struct extent_io_tree *tree, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 4170 | struct page *page, gfp_t mask) |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4171 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4172 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4173 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4174 | int ret = 1; |
| 4175 | |
Nikolay Borisov | 8882679 | 2019-03-14 15:28:31 +0200 | [diff] [blame] | 4176 | if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) { |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4177 | ret = 0; |
Nikolay Borisov | 8882679 | 2019-03-14 15:28:31 +0200 | [diff] [blame] | 4178 | } else { |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 4179 | /* |
| 4180 | * at this point we can safely clear everything except the |
| 4181 | * locked bit and the nodatasum bit |
| 4182 | */ |
David Sterba | 66b0c88 | 2017-10-31 16:30:47 +0100 | [diff] [blame] | 4183 | ret = __clear_extent_bit(tree, start, end, |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 4184 | ~(EXTENT_LOCKED | EXTENT_NODATASUM), |
David Sterba | 66b0c88 | 2017-10-31 16:30:47 +0100 | [diff] [blame] | 4185 | 0, 0, NULL, mask, NULL); |
Chris Mason | e3f24cc | 2011-02-14 12:52:08 -0500 | [diff] [blame] | 4186 | |
| 4187 | /* if clear_extent_bit failed for enomem reasons, |
| 4188 | * we can't allow the release to continue. |
| 4189 | */ |
| 4190 | if (ret < 0) |
| 4191 | ret = 0; |
| 4192 | else |
| 4193 | ret = 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4194 | } |
| 4195 | return ret; |
| 4196 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4197 | |
| 4198 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4199 | * a helper for releasepage. As long as there are no locked extents |
| 4200 | * in the range corresponding to the page, both state records and extent |
| 4201 | * map records are removed |
| 4202 | */ |
Nikolay Borisov | 477a30b | 2018-04-19 10:46:34 +0300 | [diff] [blame] | 4203 | int try_release_extent_mapping(struct page *page, gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4204 | { |
| 4205 | struct extent_map *em; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4206 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4207 | u64 end = start + PAGE_SIZE - 1; |
Filipe Manana | bd3599a | 2018-07-12 01:36:43 +0100 | [diff] [blame] | 4208 | struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host); |
| 4209 | struct extent_io_tree *tree = &btrfs_inode->io_tree; |
| 4210 | struct extent_map_tree *map = &btrfs_inode->extent_tree; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4211 | |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 4212 | if (gfpflags_allow_blocking(mask) && |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4213 | page->mapping->host->i_size > SZ_16M) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4214 | u64 len; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4215 | while (start <= end) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4216 | len = end - start + 1; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4217 | write_lock(&map->lock); |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4218 | em = lookup_extent_mapping(map, start, len); |
Tsutomu Itoh | 285190d | 2012-02-16 16:23:58 +0900 | [diff] [blame] | 4219 | if (!em) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4220 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4221 | break; |
| 4222 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 4223 | if (test_bit(EXTENT_FLAG_PINNED, &em->flags) || |
| 4224 | em->start != start) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4225 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4226 | free_extent_map(em); |
| 4227 | break; |
| 4228 | } |
| 4229 | if (!test_range_bit(tree, em->start, |
| 4230 | extent_map_end(em) - 1, |
Nikolay Borisov | 4e586ca | 2019-03-14 15:28:30 +0200 | [diff] [blame] | 4231 | EXTENT_LOCKED, 0, NULL)) { |
Filipe Manana | bd3599a | 2018-07-12 01:36:43 +0100 | [diff] [blame] | 4232 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4233 | &btrfs_inode->runtime_flags); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4234 | remove_extent_mapping(map, em); |
| 4235 | /* once for the rb tree */ |
| 4236 | free_extent_map(em); |
| 4237 | } |
| 4238 | start = extent_map_end(em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4239 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4240 | |
| 4241 | /* once for us */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4242 | free_extent_map(em); |
| 4243 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4244 | } |
Nikolay Borisov | 29c68b2d | 2018-04-19 10:46:35 +0300 | [diff] [blame] | 4245 | return try_release_extent_state(tree, page, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4246 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4247 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4248 | /* |
| 4249 | * helper function for fiemap, which doesn't want to see any holes. |
| 4250 | * This maps until we find something past 'last' |
| 4251 | */ |
| 4252 | static struct extent_map *get_extent_skip_holes(struct inode *inode, |
David Sterba | e3350e1 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 4253 | u64 offset, u64 last) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4254 | { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4255 | u64 sectorsize = btrfs_inode_sectorsize(inode); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4256 | struct extent_map *em; |
| 4257 | u64 len; |
| 4258 | |
| 4259 | if (offset >= last) |
| 4260 | return NULL; |
| 4261 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 4262 | while (1) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4263 | len = last - offset; |
| 4264 | if (len == 0) |
| 4265 | break; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4266 | len = ALIGN(len, sectorsize); |
Nikolay Borisov | 4ab47a8 | 2018-12-12 09:42:32 +0200 | [diff] [blame] | 4267 | em = btrfs_get_extent_fiemap(BTRFS_I(inode), offset, len); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 4268 | if (IS_ERR_OR_NULL(em)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4269 | return em; |
| 4270 | |
| 4271 | /* if this isn't a hole return it */ |
Nikolay Borisov | 4a2d25c | 2017-11-23 10:51:43 +0200 | [diff] [blame] | 4272 | if (em->block_start != EXTENT_MAP_HOLE) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4273 | return em; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4274 | |
| 4275 | /* this is a hole, advance to the next extent */ |
| 4276 | offset = extent_map_end(em); |
| 4277 | free_extent_map(em); |
| 4278 | if (offset >= last) |
| 4279 | break; |
| 4280 | } |
| 4281 | return NULL; |
| 4282 | } |
| 4283 | |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4284 | /* |
| 4285 | * To cache previous fiemap extent |
| 4286 | * |
| 4287 | * Will be used for merging fiemap extent |
| 4288 | */ |
| 4289 | struct fiemap_cache { |
| 4290 | u64 offset; |
| 4291 | u64 phys; |
| 4292 | u64 len; |
| 4293 | u32 flags; |
| 4294 | bool cached; |
| 4295 | }; |
| 4296 | |
| 4297 | /* |
| 4298 | * Helper to submit fiemap extent. |
| 4299 | * |
| 4300 | * Will try to merge current fiemap extent specified by @offset, @phys, |
| 4301 | * @len and @flags with cached one. |
| 4302 | * And only when we fails to merge, cached one will be submitted as |
| 4303 | * fiemap extent. |
| 4304 | * |
| 4305 | * Return value is the same as fiemap_fill_next_extent(). |
| 4306 | */ |
| 4307 | static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo, |
| 4308 | struct fiemap_cache *cache, |
| 4309 | u64 offset, u64 phys, u64 len, u32 flags) |
| 4310 | { |
| 4311 | int ret = 0; |
| 4312 | |
| 4313 | if (!cache->cached) |
| 4314 | goto assign; |
| 4315 | |
| 4316 | /* |
| 4317 | * Sanity check, extent_fiemap() should have ensured that new |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 4318 | * fiemap extent won't overlap with cached one. |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4319 | * Not recoverable. |
| 4320 | * |
| 4321 | * NOTE: Physical address can overlap, due to compression |
| 4322 | */ |
| 4323 | if (cache->offset + cache->len > offset) { |
| 4324 | WARN_ON(1); |
| 4325 | return -EINVAL; |
| 4326 | } |
| 4327 | |
| 4328 | /* |
| 4329 | * Only merges fiemap extents if |
| 4330 | * 1) Their logical addresses are continuous |
| 4331 | * |
| 4332 | * 2) Their physical addresses are continuous |
| 4333 | * So truly compressed (physical size smaller than logical size) |
| 4334 | * extents won't get merged with each other |
| 4335 | * |
| 4336 | * 3) Share same flags except FIEMAP_EXTENT_LAST |
| 4337 | * So regular extent won't get merged with prealloc extent |
| 4338 | */ |
| 4339 | if (cache->offset + cache->len == offset && |
| 4340 | cache->phys + cache->len == phys && |
| 4341 | (cache->flags & ~FIEMAP_EXTENT_LAST) == |
| 4342 | (flags & ~FIEMAP_EXTENT_LAST)) { |
| 4343 | cache->len += len; |
| 4344 | cache->flags |= flags; |
| 4345 | goto try_submit_last; |
| 4346 | } |
| 4347 | |
| 4348 | /* Not mergeable, need to submit cached one */ |
| 4349 | ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys, |
| 4350 | cache->len, cache->flags); |
| 4351 | cache->cached = false; |
| 4352 | if (ret) |
| 4353 | return ret; |
| 4354 | assign: |
| 4355 | cache->cached = true; |
| 4356 | cache->offset = offset; |
| 4357 | cache->phys = phys; |
| 4358 | cache->len = len; |
| 4359 | cache->flags = flags; |
| 4360 | try_submit_last: |
| 4361 | if (cache->flags & FIEMAP_EXTENT_LAST) { |
| 4362 | ret = fiemap_fill_next_extent(fieinfo, cache->offset, |
| 4363 | cache->phys, cache->len, cache->flags); |
| 4364 | cache->cached = false; |
| 4365 | } |
| 4366 | return ret; |
| 4367 | } |
| 4368 | |
| 4369 | /* |
Qu Wenruo | 848c23b | 2017-06-22 10:01:21 +0800 | [diff] [blame] | 4370 | * Emit last fiemap cache |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4371 | * |
Qu Wenruo | 848c23b | 2017-06-22 10:01:21 +0800 | [diff] [blame] | 4372 | * The last fiemap cache may still be cached in the following case: |
| 4373 | * 0 4k 8k |
| 4374 | * |<- Fiemap range ->| |
| 4375 | * |<------------ First extent ----------->| |
| 4376 | * |
| 4377 | * In this case, the first extent range will be cached but not emitted. |
| 4378 | * So we must emit it before ending extent_fiemap(). |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4379 | */ |
Qu Wenruo | 848c23b | 2017-06-22 10:01:21 +0800 | [diff] [blame] | 4380 | static int emit_last_fiemap_cache(struct btrfs_fs_info *fs_info, |
| 4381 | struct fiemap_extent_info *fieinfo, |
| 4382 | struct fiemap_cache *cache) |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4383 | { |
| 4384 | int ret; |
| 4385 | |
| 4386 | if (!cache->cached) |
| 4387 | return 0; |
| 4388 | |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4389 | ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys, |
| 4390 | cache->len, cache->flags); |
| 4391 | cache->cached = false; |
| 4392 | if (ret > 0) |
| 4393 | ret = 0; |
| 4394 | return ret; |
| 4395 | } |
| 4396 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4397 | int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
David Sterba | 2135fb9 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 4398 | __u64 start, __u64 len) |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4399 | { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4400 | int ret = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4401 | u64 off = start; |
| 4402 | u64 max = start + len; |
| 4403 | u32 flags = 0; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4404 | u32 found_type; |
| 4405 | u64 last; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4406 | u64 last_for_get_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4407 | u64 disko = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4408 | u64 isize = i_size_read(inode); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4409 | struct btrfs_key found_key; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4410 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4411 | struct extent_state *cached_state = NULL; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4412 | struct btrfs_path *path; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4413 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4414 | struct fiemap_cache cache = { 0 }; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4415 | int end = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4416 | u64 em_start = 0; |
| 4417 | u64 em_len = 0; |
| 4418 | u64 em_end = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4419 | |
| 4420 | if (len == 0) |
| 4421 | return -EINVAL; |
| 4422 | |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4423 | path = btrfs_alloc_path(); |
| 4424 | if (!path) |
| 4425 | return -ENOMEM; |
| 4426 | path->leave_spinning = 1; |
| 4427 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4428 | start = round_down(start, btrfs_inode_sectorsize(inode)); |
| 4429 | len = round_up(max, btrfs_inode_sectorsize(inode)) - start; |
Josef Bacik | 4d479cf | 2011-11-17 11:34:31 -0500 | [diff] [blame] | 4430 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4431 | /* |
| 4432 | * lookup the last file extent. We're not using i_size here |
| 4433 | * because there might be preallocation past i_size |
| 4434 | */ |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4435 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 4436 | btrfs_ino(BTRFS_I(inode)), -1, 0); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4437 | if (ret < 0) { |
| 4438 | btrfs_free_path(path); |
| 4439 | return ret; |
Liu Bo | 2d324f5 | 2016-05-17 17:21:48 -0700 | [diff] [blame] | 4440 | } else { |
| 4441 | WARN_ON(!ret); |
| 4442 | if (ret == 1) |
| 4443 | ret = 0; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4444 | } |
Liu Bo | 2d324f5 | 2016-05-17 17:21:48 -0700 | [diff] [blame] | 4445 | |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4446 | path->slots[0]--; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4447 | 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] | 4448 | found_type = found_key.type; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4449 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4450 | /* No extents, but there might be delalloc bits */ |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4451 | if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) || |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4452 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4453 | /* have to trust i_size as the end */ |
| 4454 | last = (u64)-1; |
| 4455 | last_for_get_extent = isize; |
| 4456 | } else { |
| 4457 | /* |
| 4458 | * remember the start of the last extent. There are a |
| 4459 | * bunch of different factors that go into the length of the |
| 4460 | * extent, so its much less complex to remember where it started |
| 4461 | */ |
| 4462 | last = found_key.offset; |
| 4463 | last_for_get_extent = last + 1; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4464 | } |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4465 | btrfs_release_path(path); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4466 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4467 | /* |
| 4468 | * we might have some extents allocated but more delalloc past those |
| 4469 | * extents. so, we trust isize unless the start of the last extent is |
| 4470 | * beyond isize |
| 4471 | */ |
| 4472 | if (last < isize) { |
| 4473 | last = (u64)-1; |
| 4474 | last_for_get_extent = isize; |
| 4475 | } |
| 4476 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 4477 | lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4478 | &cached_state); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4479 | |
David Sterba | e3350e1 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 4480 | em = get_extent_skip_holes(inode, start, last_for_get_extent); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4481 | if (!em) |
| 4482 | goto out; |
| 4483 | if (IS_ERR(em)) { |
| 4484 | ret = PTR_ERR(em); |
| 4485 | goto out; |
| 4486 | } |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4487 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4488 | while (!end) { |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4489 | u64 offset_in_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4490 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4491 | /* break if the extent we found is outside the range */ |
| 4492 | if (em->start >= max || extent_map_end(em) < off) |
| 4493 | break; |
| 4494 | |
| 4495 | /* |
| 4496 | * get_extent may return an extent that starts before our |
| 4497 | * requested range. We have to make sure the ranges |
| 4498 | * we return to fiemap always move forward and don't |
| 4499 | * overlap, so adjust the offsets here |
| 4500 | */ |
| 4501 | em_start = max(em->start, off); |
| 4502 | |
| 4503 | /* |
| 4504 | * record the offset from the start of the extent |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4505 | * for adjusting the disk offset below. Only do this if the |
| 4506 | * extent isn't compressed since our in ram offset may be past |
| 4507 | * what we have actually allocated on disk. |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4508 | */ |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4509 | if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 4510 | offset_in_extent = em_start - em->start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4511 | em_end = extent_map_end(em); |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4512 | em_len = em_end - em_start; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4513 | flags = 0; |
Filipe Manana | f098631 | 2018-06-20 10:02:30 +0100 | [diff] [blame] | 4514 | if (em->block_start < EXTENT_MAP_LAST_BYTE) |
| 4515 | disko = em->block_start + offset_in_extent; |
| 4516 | else |
| 4517 | disko = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4518 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4519 | /* |
| 4520 | * bump off for our next call to get_extent |
| 4521 | */ |
| 4522 | off = extent_map_end(em); |
| 4523 | if (off >= max) |
| 4524 | end = 1; |
| 4525 | |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4526 | if (em->block_start == EXTENT_MAP_LAST_BYTE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4527 | end = 1; |
| 4528 | flags |= FIEMAP_EXTENT_LAST; |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4529 | } else if (em->block_start == EXTENT_MAP_INLINE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4530 | flags |= (FIEMAP_EXTENT_DATA_INLINE | |
| 4531 | FIEMAP_EXTENT_NOT_ALIGNED); |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4532 | } else if (em->block_start == EXTENT_MAP_DELALLOC) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4533 | flags |= (FIEMAP_EXTENT_DELALLOC | |
| 4534 | FIEMAP_EXTENT_UNKNOWN); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4535 | } else if (fieinfo->fi_extents_max) { |
| 4536 | u64 bytenr = em->block_start - |
| 4537 | (em->start - em->orig_start); |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4538 | |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4539 | /* |
| 4540 | * As btrfs supports shared space, this information |
| 4541 | * can be exported to userspace tools via |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4542 | * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0 |
| 4543 | * then we're just getting a count and we can skip the |
| 4544 | * lookup stuff. |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4545 | */ |
Edmund Nadolski | bb739cf | 2017-06-28 21:56:58 -0600 | [diff] [blame] | 4546 | ret = btrfs_check_shared(root, |
| 4547 | btrfs_ino(BTRFS_I(inode)), |
| 4548 | bytenr); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4549 | if (ret < 0) |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4550 | goto out_free; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4551 | if (ret) |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4552 | flags |= FIEMAP_EXTENT_SHARED; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4553 | ret = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4554 | } |
| 4555 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 4556 | flags |= FIEMAP_EXTENT_ENCODED; |
Josef Bacik | 0d2b237 | 2015-05-19 10:44:04 -0400 | [diff] [blame] | 4557 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 4558 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4559 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4560 | free_extent_map(em); |
| 4561 | em = NULL; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4562 | if ((em_start >= last) || em_len == (u64)-1 || |
| 4563 | (last == (u64)-1 && isize <= em_end)) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4564 | flags |= FIEMAP_EXTENT_LAST; |
| 4565 | end = 1; |
| 4566 | } |
| 4567 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4568 | /* now scan forward to see if this is really the last extent. */ |
David Sterba | e3350e1 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 4569 | em = get_extent_skip_holes(inode, off, last_for_get_extent); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4570 | if (IS_ERR(em)) { |
| 4571 | ret = PTR_ERR(em); |
| 4572 | goto out; |
| 4573 | } |
| 4574 | if (!em) { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4575 | flags |= FIEMAP_EXTENT_LAST; |
| 4576 | end = 1; |
| 4577 | } |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4578 | ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko, |
| 4579 | em_len, flags); |
Chengyu Song | 26e726a | 2015-03-24 18:12:56 -0400 | [diff] [blame] | 4580 | if (ret) { |
| 4581 | if (ret == 1) |
| 4582 | ret = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4583 | goto out_free; |
Chengyu Song | 26e726a | 2015-03-24 18:12:56 -0400 | [diff] [blame] | 4584 | } |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4585 | } |
| 4586 | out_free: |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4587 | if (!ret) |
Qu Wenruo | 848c23b | 2017-06-22 10:01:21 +0800 | [diff] [blame] | 4588 | ret = emit_last_fiemap_cache(root->fs_info, fieinfo, &cache); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4589 | free_extent_map(em); |
| 4590 | out: |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4591 | btrfs_free_path(path); |
Liu Bo | a52f4cd | 2013-05-01 16:23:41 +0000 | [diff] [blame] | 4592 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4593 | &cached_state); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4594 | return ret; |
| 4595 | } |
| 4596 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4597 | static void __free_extent_buffer(struct extent_buffer *eb) |
| 4598 | { |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 4599 | btrfs_leak_debug_del(&eb->leak_list); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4600 | kmem_cache_free(extent_buffer_cache, eb); |
| 4601 | } |
| 4602 | |
Josef Bacik | a26e8c9 | 2014-03-28 17:07:27 -0400 | [diff] [blame] | 4603 | int extent_buffer_under_io(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4604 | { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4605 | return (atomic_read(&eb->io_pages) || |
| 4606 | test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) || |
| 4607 | test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4608 | } |
| 4609 | |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4610 | /* |
David Sterba | 55ac013 | 2018-07-19 17:24:32 +0200 | [diff] [blame] | 4611 | * Release all pages attached to the extent buffer. |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4612 | */ |
David Sterba | 55ac013 | 2018-07-19 17:24:32 +0200 | [diff] [blame] | 4613 | static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb) |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4614 | { |
Nikolay Borisov | d64766f | 2018-06-27 16:38:22 +0300 | [diff] [blame] | 4615 | int i; |
| 4616 | int num_pages; |
Nikolay Borisov | b0132a3 | 2018-06-27 16:38:24 +0300 | [diff] [blame] | 4617 | int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4618 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4619 | BUG_ON(extent_buffer_under_io(eb)); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4620 | |
Nikolay Borisov | d64766f | 2018-06-27 16:38:22 +0300 | [diff] [blame] | 4621 | num_pages = num_extent_pages(eb); |
| 4622 | for (i = 0; i < num_pages; i++) { |
| 4623 | struct page *page = eb->pages[i]; |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4624 | |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4625 | if (!page) |
| 4626 | continue; |
| 4627 | if (mapped) |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4628 | spin_lock(&page->mapping->private_lock); |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4629 | /* |
| 4630 | * We do this since we'll remove the pages after we've |
| 4631 | * removed the eb from the radix tree, so we could race |
| 4632 | * and have this page now attached to the new eb. So |
| 4633 | * only clear page_private if it's still connected to |
| 4634 | * this eb. |
| 4635 | */ |
| 4636 | if (PagePrivate(page) && |
| 4637 | page->private == (unsigned long)eb) { |
| 4638 | BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
| 4639 | BUG_ON(PageDirty(page)); |
| 4640 | BUG_ON(PageWriteback(page)); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4641 | /* |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4642 | * We need to make sure we haven't be attached |
| 4643 | * to a new eb. |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4644 | */ |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4645 | ClearPagePrivate(page); |
| 4646 | set_page_private(page, 0); |
| 4647 | /* One for the page private */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4648 | put_page(page); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4649 | } |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4650 | |
| 4651 | if (mapped) |
| 4652 | spin_unlock(&page->mapping->private_lock); |
| 4653 | |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 4654 | /* One for when we allocated the page */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4655 | put_page(page); |
Nikolay Borisov | d64766f | 2018-06-27 16:38:22 +0300 | [diff] [blame] | 4656 | } |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4657 | } |
| 4658 | |
| 4659 | /* |
| 4660 | * Helper for releasing the extent buffer. |
| 4661 | */ |
| 4662 | static inline void btrfs_release_extent_buffer(struct extent_buffer *eb) |
| 4663 | { |
David Sterba | 55ac013 | 2018-07-19 17:24:32 +0200 | [diff] [blame] | 4664 | btrfs_release_extent_buffer_pages(eb); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4665 | __free_extent_buffer(eb); |
| 4666 | } |
| 4667 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4668 | static struct extent_buffer * |
| 4669 | __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, |
David Sterba | 23d79d8 | 2014-06-15 02:55:29 +0200 | [diff] [blame] | 4670 | unsigned long len) |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4671 | { |
| 4672 | struct extent_buffer *eb = NULL; |
| 4673 | |
Michal Hocko | d1b5c56 | 2015-08-19 14:17:40 +0200 | [diff] [blame] | 4674 | eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4675 | eb->start = start; |
| 4676 | eb->len = len; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4677 | eb->fs_info = fs_info; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4678 | eb->bflags = 0; |
| 4679 | rwlock_init(&eb->lock); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4680 | atomic_set(&eb->blocking_readers, 0); |
| 4681 | atomic_set(&eb->blocking_writers, 0); |
David Sterba | ed1b4ed | 2018-08-24 16:31:17 +0200 | [diff] [blame^] | 4682 | eb->lock_nested = false; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4683 | init_waitqueue_head(&eb->write_lock_wq); |
| 4684 | init_waitqueue_head(&eb->read_lock_wq); |
| 4685 | |
| 4686 | btrfs_leak_debug_add(&eb->leak_list, &buffers); |
| 4687 | |
| 4688 | spin_lock_init(&eb->refs_lock); |
| 4689 | atomic_set(&eb->refs, 1); |
| 4690 | atomic_set(&eb->io_pages, 0); |
| 4691 | |
| 4692 | /* |
| 4693 | * Sanity checks, currently the maximum is 64k covered by 16x 4k pages |
| 4694 | */ |
| 4695 | BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE |
| 4696 | > MAX_INLINE_EXTENT_BUFFER_SIZE); |
| 4697 | BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE); |
| 4698 | |
David Sterba | 843ccf9 | 2018-08-24 14:56:28 +0200 | [diff] [blame] | 4699 | #ifdef CONFIG_BTRFS_DEBUG |
| 4700 | atomic_set(&eb->spinning_writers, 0); |
David Sterba | afd495a | 2018-08-24 15:57:38 +0200 | [diff] [blame] | 4701 | atomic_set(&eb->spinning_readers, 0); |
David Sterba | 5c9c799 | 2018-08-24 16:15:51 +0200 | [diff] [blame] | 4702 | atomic_set(&eb->read_locks, 0); |
David Sterba | c79adfc | 2018-08-24 16:24:26 +0200 | [diff] [blame] | 4703 | atomic_set(&eb->write_locks, 0); |
David Sterba | 843ccf9 | 2018-08-24 14:56:28 +0200 | [diff] [blame] | 4704 | #endif |
| 4705 | |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4706 | return eb; |
| 4707 | } |
| 4708 | |
| 4709 | struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src) |
| 4710 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 4711 | int i; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4712 | struct page *p; |
| 4713 | struct extent_buffer *new; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 4714 | int num_pages = num_extent_pages(src); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4715 | |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4716 | new = __alloc_extent_buffer(src->fs_info, src->start, src->len); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4717 | if (new == NULL) |
| 4718 | return NULL; |
| 4719 | |
| 4720 | for (i = 0; i < num_pages; i++) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 4721 | p = alloc_page(GFP_NOFS); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4722 | if (!p) { |
| 4723 | btrfs_release_extent_buffer(new); |
| 4724 | return NULL; |
| 4725 | } |
| 4726 | attach_extent_buffer_page(new, p); |
| 4727 | WARN_ON(PageDirty(p)); |
| 4728 | SetPageUptodate(p); |
| 4729 | new->pages[i] = p; |
David Sterba | fba1acf | 2016-11-08 17:56:24 +0100 | [diff] [blame] | 4730 | copy_page(page_address(p), page_address(src->pages[i])); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4731 | } |
| 4732 | |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4733 | set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags); |
Nikolay Borisov | b0132a3 | 2018-06-27 16:38:24 +0300 | [diff] [blame] | 4734 | set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4735 | |
| 4736 | return new; |
| 4737 | } |
| 4738 | |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4739 | struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, |
| 4740 | u64 start, unsigned long len) |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4741 | { |
| 4742 | struct extent_buffer *eb; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 4743 | int num_pages; |
| 4744 | int i; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4745 | |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4746 | eb = __alloc_extent_buffer(fs_info, start, len); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4747 | if (!eb) |
| 4748 | return NULL; |
| 4749 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 4750 | num_pages = num_extent_pages(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4751 | for (i = 0; i < num_pages; i++) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 4752 | eb->pages[i] = alloc_page(GFP_NOFS); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4753 | if (!eb->pages[i]) |
| 4754 | goto err; |
| 4755 | } |
| 4756 | set_extent_buffer_uptodate(eb); |
| 4757 | btrfs_set_header_nritems(eb, 0); |
Nikolay Borisov | b0132a3 | 2018-06-27 16:38:24 +0300 | [diff] [blame] | 4758 | set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4759 | |
| 4760 | return eb; |
| 4761 | err: |
| 4762 | for (; i > 0; i--) |
| 4763 | __free_page(eb->pages[i - 1]); |
| 4764 | __free_extent_buffer(eb); |
| 4765 | return NULL; |
| 4766 | } |
| 4767 | |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4768 | struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4769 | u64 start) |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4770 | { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4771 | return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize); |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4772 | } |
| 4773 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4774 | static void check_buffer_tree_ref(struct extent_buffer *eb) |
| 4775 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4776 | int refs; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4777 | /* the ref bit is tricky. We have to make sure it is set |
| 4778 | * if we have the buffer dirty. Otherwise the |
| 4779 | * code to free a buffer can end up dropping a dirty |
| 4780 | * page |
| 4781 | * |
| 4782 | * Once the ref bit is set, it won't go away while the |
| 4783 | * buffer is dirty or in writeback, and it also won't |
| 4784 | * go away while we have the reference count on the |
| 4785 | * eb bumped. |
| 4786 | * |
| 4787 | * We can't just set the ref bit without bumping the |
| 4788 | * ref on the eb because free_extent_buffer might |
| 4789 | * see the ref bit and try to clear it. If this happens |
| 4790 | * free_extent_buffer might end up dropping our original |
| 4791 | * ref by mistake and freeing the page before we are able |
| 4792 | * to add one more ref. |
| 4793 | * |
| 4794 | * So bump the ref count first, then set the bit. If someone |
| 4795 | * beat us to it, drop the ref we added. |
| 4796 | */ |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4797 | refs = atomic_read(&eb->refs); |
| 4798 | if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 4799 | return; |
| 4800 | |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4801 | spin_lock(&eb->refs_lock); |
| 4802 | if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4803 | atomic_inc(&eb->refs); |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4804 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4805 | } |
| 4806 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4807 | static void mark_extent_buffer_accessed(struct extent_buffer *eb, |
| 4808 | struct page *accessed) |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4809 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 4810 | int num_pages, i; |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4811 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4812 | check_buffer_tree_ref(eb); |
| 4813 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 4814 | num_pages = num_extent_pages(eb); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4815 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 4816 | struct page *p = eb->pages[i]; |
| 4817 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4818 | if (p != accessed) |
| 4819 | mark_page_accessed(p); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4820 | } |
| 4821 | } |
| 4822 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4823 | struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info, |
| 4824 | u64 start) |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4825 | { |
| 4826 | struct extent_buffer *eb; |
| 4827 | |
| 4828 | rcu_read_lock(); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4829 | eb = radix_tree_lookup(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4830 | start >> PAGE_SHIFT); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4831 | if (eb && atomic_inc_not_zero(&eb->refs)) { |
| 4832 | rcu_read_unlock(); |
Filipe Manana | 062c19e | 2015-04-23 11:28:48 +0100 | [diff] [blame] | 4833 | /* |
| 4834 | * Lock our eb's refs_lock to avoid races with |
| 4835 | * free_extent_buffer. When we get our eb it might be flagged |
| 4836 | * with EXTENT_BUFFER_STALE and another task running |
| 4837 | * free_extent_buffer might have seen that flag set, |
| 4838 | * eb->refs == 2, that the buffer isn't under IO (dirty and |
| 4839 | * writeback flags not set) and it's still in the tree (flag |
| 4840 | * EXTENT_BUFFER_TREE_REF set), therefore being in the process |
| 4841 | * of decrementing the extent buffer's reference count twice. |
| 4842 | * So here we could race and increment the eb's reference count, |
| 4843 | * clear its stale flag, mark it as dirty and drop our reference |
| 4844 | * before the other task finishes executing free_extent_buffer, |
| 4845 | * which would later result in an attempt to free an extent |
| 4846 | * buffer that is dirty. |
| 4847 | */ |
| 4848 | if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) { |
| 4849 | spin_lock(&eb->refs_lock); |
| 4850 | spin_unlock(&eb->refs_lock); |
| 4851 | } |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4852 | mark_extent_buffer_accessed(eb, NULL); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4853 | return eb; |
| 4854 | } |
| 4855 | rcu_read_unlock(); |
| 4856 | |
| 4857 | return NULL; |
| 4858 | } |
| 4859 | |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4860 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 4861 | struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4862 | u64 start) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4863 | { |
| 4864 | struct extent_buffer *eb, *exists = NULL; |
| 4865 | int ret; |
| 4866 | |
| 4867 | eb = find_extent_buffer(fs_info, start); |
| 4868 | if (eb) |
| 4869 | return eb; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4870 | eb = alloc_dummy_extent_buffer(fs_info, start); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4871 | if (!eb) |
| 4872 | return NULL; |
| 4873 | eb->fs_info = fs_info; |
| 4874 | again: |
David Sterba | e1860a7 | 2016-05-09 14:11:38 +0200 | [diff] [blame] | 4875 | ret = radix_tree_preload(GFP_NOFS); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4876 | if (ret) |
| 4877 | goto free_eb; |
| 4878 | spin_lock(&fs_info->buffer_lock); |
| 4879 | ret = radix_tree_insert(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4880 | start >> PAGE_SHIFT, eb); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4881 | spin_unlock(&fs_info->buffer_lock); |
| 4882 | radix_tree_preload_end(); |
| 4883 | if (ret == -EEXIST) { |
| 4884 | exists = find_extent_buffer(fs_info, start); |
| 4885 | if (exists) |
| 4886 | goto free_eb; |
| 4887 | else |
| 4888 | goto again; |
| 4889 | } |
| 4890 | check_buffer_tree_ref(eb); |
| 4891 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
| 4892 | |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4893 | return eb; |
| 4894 | free_eb: |
| 4895 | btrfs_release_extent_buffer(eb); |
| 4896 | return exists; |
| 4897 | } |
| 4898 | #endif |
| 4899 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4900 | struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, |
David Sterba | ce3e698 | 2014-06-15 03:00:04 +0200 | [diff] [blame] | 4901 | u64 start) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4902 | { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4903 | unsigned long len = fs_info->nodesize; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 4904 | int num_pages; |
| 4905 | int i; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4906 | unsigned long index = start >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4907 | struct extent_buffer *eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4908 | struct extent_buffer *exists = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4909 | struct page *p; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4910 | struct address_space *mapping = fs_info->btree_inode->i_mapping; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4911 | int uptodate = 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4912 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4913 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4914 | if (!IS_ALIGNED(start, fs_info->sectorsize)) { |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4915 | btrfs_err(fs_info, "bad tree block start %llu", start); |
| 4916 | return ERR_PTR(-EINVAL); |
| 4917 | } |
| 4918 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4919 | eb = find_extent_buffer(fs_info, start); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4920 | if (eb) |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4921 | return eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4922 | |
David Sterba | 23d79d8 | 2014-06-15 02:55:29 +0200 | [diff] [blame] | 4923 | eb = __alloc_extent_buffer(fs_info, start, len); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 4924 | if (!eb) |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4925 | return ERR_PTR(-ENOMEM); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4926 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 4927 | num_pages = num_extent_pages(eb); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4928 | for (i = 0; i < num_pages; i++, index++) { |
Michal Hocko | d1b5c56 | 2015-08-19 14:17:40 +0200 | [diff] [blame] | 4929 | p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4930 | if (!p) { |
| 4931 | exists = ERR_PTR(-ENOMEM); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4932 | goto free_eb; |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4933 | } |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4934 | |
| 4935 | spin_lock(&mapping->private_lock); |
| 4936 | if (PagePrivate(p)) { |
| 4937 | /* |
| 4938 | * We could have already allocated an eb for this page |
| 4939 | * and attached one so lets see if we can get a ref on |
| 4940 | * the existing eb, and if we can we know it's good and |
| 4941 | * we can just return that one, else we know we can just |
| 4942 | * overwrite page->private. |
| 4943 | */ |
| 4944 | exists = (struct extent_buffer *)p->private; |
| 4945 | if (atomic_inc_not_zero(&exists->refs)) { |
| 4946 | spin_unlock(&mapping->private_lock); |
| 4947 | unlock_page(p); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4948 | put_page(p); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4949 | mark_extent_buffer_accessed(exists, p); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4950 | goto free_eb; |
| 4951 | } |
Omar Sandoval | 5ca64f4 | 2015-02-24 02:47:05 -0800 | [diff] [blame] | 4952 | exists = NULL; |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4953 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4954 | /* |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4955 | * Do this so attach doesn't complain and we need to |
| 4956 | * drop the ref the old guy had. |
| 4957 | */ |
| 4958 | ClearPagePrivate(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4959 | WARN_ON(PageDirty(p)); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4960 | put_page(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4961 | } |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4962 | attach_extent_buffer_page(eb, p); |
| 4963 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4964 | WARN_ON(PageDirty(p)); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4965 | eb->pages[i] = p; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4966 | if (!PageUptodate(p)) |
| 4967 | uptodate = 0; |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 4968 | |
| 4969 | /* |
Nikolay Borisov | b16d011 | 2018-07-04 10:24:52 +0300 | [diff] [blame] | 4970 | * We can't unlock the pages just yet since the extent buffer |
| 4971 | * hasn't been properly inserted in the radix tree, this |
| 4972 | * opens a race with btree_releasepage which can free a page |
| 4973 | * while we are still filling in all pages for the buffer and |
| 4974 | * we could crash. |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 4975 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4976 | } |
| 4977 | if (uptodate) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4978 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 4979 | again: |
David Sterba | e1860a7 | 2016-05-09 14:11:38 +0200 | [diff] [blame] | 4980 | ret = radix_tree_preload(GFP_NOFS); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4981 | if (ret) { |
| 4982 | exists = ERR_PTR(ret); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4983 | goto free_eb; |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4984 | } |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4985 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4986 | spin_lock(&fs_info->buffer_lock); |
| 4987 | ret = radix_tree_insert(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4988 | start >> PAGE_SHIFT, eb); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4989 | spin_unlock(&fs_info->buffer_lock); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4990 | radix_tree_preload_end(); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4991 | if (ret == -EEXIST) { |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4992 | exists = find_extent_buffer(fs_info, start); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 4993 | if (exists) |
| 4994 | goto free_eb; |
| 4995 | else |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 4996 | goto again; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4997 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 4998 | /* add one reference for the tree */ |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4999 | check_buffer_tree_ref(eb); |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 5000 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5001 | |
| 5002 | /* |
Nikolay Borisov | b16d011 | 2018-07-04 10:24:52 +0300 | [diff] [blame] | 5003 | * Now it's safe to unlock the pages because any calls to |
| 5004 | * btree_releasepage will correctly detect that a page belongs to a |
| 5005 | * live buffer and won't free them prematurely. |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5006 | */ |
Nikolay Borisov | 28187ae | 2018-07-04 10:24:51 +0300 | [diff] [blame] | 5007 | for (i = 0; i < num_pages; i++) |
| 5008 | unlock_page(eb->pages[i]); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5009 | return eb; |
| 5010 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5011 | free_eb: |
Omar Sandoval | 5ca64f4 | 2015-02-24 02:47:05 -0800 | [diff] [blame] | 5012 | WARN_ON(!atomic_dec_and_test(&eb->refs)); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5013 | for (i = 0; i < num_pages; i++) { |
| 5014 | if (eb->pages[i]) |
| 5015 | unlock_page(eb->pages[i]); |
| 5016 | } |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5017 | |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5018 | btrfs_release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5019 | return exists; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5020 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5021 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5022 | static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head) |
| 5023 | { |
| 5024 | struct extent_buffer *eb = |
| 5025 | container_of(head, struct extent_buffer, rcu_head); |
| 5026 | |
| 5027 | __free_extent_buffer(eb); |
| 5028 | } |
| 5029 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5030 | static int release_extent_buffer(struct extent_buffer *eb) |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5031 | { |
Nikolay Borisov | 07e21c4 | 2018-06-27 16:38:23 +0300 | [diff] [blame] | 5032 | lockdep_assert_held(&eb->refs_lock); |
| 5033 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5034 | WARN_ON(atomic_read(&eb->refs) == 0); |
| 5035 | if (atomic_dec_and_test(&eb->refs)) { |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 5036 | if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) { |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5037 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5038 | |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 5039 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5040 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5041 | spin_lock(&fs_info->buffer_lock); |
| 5042 | radix_tree_delete(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5043 | eb->start >> PAGE_SHIFT); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5044 | spin_unlock(&fs_info->buffer_lock); |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 5045 | } else { |
| 5046 | spin_unlock(&eb->refs_lock); |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 5047 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5048 | |
| 5049 | /* Should be safe to release our pages at this point */ |
David Sterba | 55ac013 | 2018-07-19 17:24:32 +0200 | [diff] [blame] | 5050 | btrfs_release_extent_buffer_pages(eb); |
Josef Bacik | bcb7e44 | 2015-03-16 17:38:02 -0400 | [diff] [blame] | 5051 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
Nikolay Borisov | b0132a3 | 2018-06-27 16:38:24 +0300 | [diff] [blame] | 5052 | if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) { |
Josef Bacik | bcb7e44 | 2015-03-16 17:38:02 -0400 | [diff] [blame] | 5053 | __free_extent_buffer(eb); |
| 5054 | return 1; |
| 5055 | } |
| 5056 | #endif |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5057 | call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 5058 | return 1; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5059 | } |
| 5060 | spin_unlock(&eb->refs_lock); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 5061 | |
| 5062 | return 0; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5063 | } |
| 5064 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5065 | void free_extent_buffer(struct extent_buffer *eb) |
| 5066 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 5067 | int refs; |
| 5068 | int old; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5069 | if (!eb) |
| 5070 | return; |
| 5071 | |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 5072 | while (1) { |
| 5073 | refs = atomic_read(&eb->refs); |
Nikolay Borisov | 46cc775 | 2018-10-15 17:04:01 +0300 | [diff] [blame] | 5074 | if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3) |
| 5075 | || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && |
| 5076 | refs == 1)) |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 5077 | break; |
| 5078 | old = atomic_cmpxchg(&eb->refs, refs, refs - 1); |
| 5079 | if (old == refs) |
| 5080 | return; |
| 5081 | } |
| 5082 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5083 | spin_lock(&eb->refs_lock); |
| 5084 | if (atomic_read(&eb->refs) == 2 && |
| 5085 | test_bit(EXTENT_BUFFER_STALE, &eb->bflags) && |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5086 | !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5087 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 5088 | atomic_dec(&eb->refs); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5089 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5090 | /* |
| 5091 | * I know this is terrible, but it's temporary until we stop tracking |
| 5092 | * the uptodate bits and such for the extent buffers. |
| 5093 | */ |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5094 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5095 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5096 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5097 | void free_extent_buffer_stale(struct extent_buffer *eb) |
| 5098 | { |
| 5099 | if (!eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5100 | return; |
| 5101 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5102 | spin_lock(&eb->refs_lock); |
| 5103 | set_bit(EXTENT_BUFFER_STALE, &eb->bflags); |
| 5104 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5105 | if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5106 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 5107 | atomic_dec(&eb->refs); |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5108 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5109 | } |
| 5110 | |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 5111 | void clear_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5112 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5113 | int i; |
| 5114 | int num_pages; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5115 | struct page *page; |
| 5116 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5117 | num_pages = num_extent_pages(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5118 | |
| 5119 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5120 | page = eb->pages[i]; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5121 | if (!PageDirty(page)) |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 5122 | continue; |
| 5123 | |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 5124 | lock_page(page); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5125 | WARN_ON(!PagePrivate(page)); |
| 5126 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5127 | clear_page_dirty_for_io(page); |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 5128 | xa_lock_irq(&page->mapping->i_pages); |
Matthew Wilcox | 0a943c6 | 2017-12-04 10:37:22 -0500 | [diff] [blame] | 5129 | if (!PageDirty(page)) |
| 5130 | __xa_clear_mark(&page->mapping->i_pages, |
| 5131 | page_index(page), PAGECACHE_TAG_DIRTY); |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 5132 | xa_unlock_irq(&page->mapping->i_pages); |
Chris Mason | bf0da8c | 2011-11-04 12:29:37 -0400 | [diff] [blame] | 5133 | ClearPageError(page); |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 5134 | unlock_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5135 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5136 | WARN_ON(atomic_read(&eb->refs) == 0); |
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 | |
Liu Bo | abb57ef | 2018-09-14 01:44:42 +0800 | [diff] [blame] | 5139 | bool set_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5140 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5141 | int i; |
| 5142 | int num_pages; |
Liu Bo | abb57ef | 2018-09-14 01:44:42 +0800 | [diff] [blame] | 5143 | bool was_dirty; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5144 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5145 | check_buffer_tree_ref(eb); |
| 5146 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5147 | was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5148 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5149 | num_pages = num_extent_pages(eb); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5150 | WARN_ON(atomic_read(&eb->refs) == 0); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5151 | WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)); |
| 5152 | |
Liu Bo | abb57ef | 2018-09-14 01:44:42 +0800 | [diff] [blame] | 5153 | if (!was_dirty) |
| 5154 | for (i = 0; i < num_pages; i++) |
| 5155 | set_page_dirty(eb->pages[i]); |
Liu Bo | 51995c3 | 2018-09-14 01:46:08 +0800 | [diff] [blame] | 5156 | |
| 5157 | #ifdef CONFIG_BTRFS_DEBUG |
| 5158 | for (i = 0; i < num_pages; i++) |
| 5159 | ASSERT(PageDirty(eb->pages[i])); |
| 5160 | #endif |
| 5161 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5162 | return was_dirty; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5163 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5164 | |
David Sterba | 69ba392 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 5165 | void clear_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5166 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5167 | int i; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5168 | struct page *page; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5169 | int num_pages; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5170 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5171 | clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5172 | num_pages = num_extent_pages(eb); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5173 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5174 | page = eb->pages[i]; |
Chris Mason | 33958dc | 2008-07-30 10:29:12 -0400 | [diff] [blame] | 5175 | if (page) |
| 5176 | ClearPageUptodate(page); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5177 | } |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5178 | } |
| 5179 | |
David Sterba | 09c25a8 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 5180 | void set_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5181 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5182 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5183 | struct page *page; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5184 | int num_pages; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5185 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5186 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5187 | num_pages = num_extent_pages(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5188 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5189 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5190 | SetPageUptodate(page); |
| 5191 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5192 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5193 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5194 | int read_extent_buffer_pages(struct extent_io_tree *tree, |
David Sterba | 6af49db | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 5195 | struct extent_buffer *eb, int wait, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5196 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5197 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5198 | struct page *page; |
| 5199 | int err; |
| 5200 | int ret = 0; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5201 | int locked_pages = 0; |
| 5202 | int all_uptodate = 1; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5203 | int num_pages; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5204 | unsigned long num_reads = 0; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5205 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5206 | unsigned long bio_flags = 0; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5207 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5208 | if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5209 | return 0; |
| 5210 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5211 | num_pages = num_extent_pages(eb); |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5212 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5213 | page = eb->pages[i]; |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 5214 | if (wait == WAIT_NONE) { |
David Woodhouse | 2db0496 | 2008-08-07 11:19:43 -0400 | [diff] [blame] | 5215 | if (!trylock_page(page)) |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5216 | goto unlock_exit; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5217 | } else { |
| 5218 | lock_page(page); |
| 5219 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5220 | locked_pages++; |
Liu Bo | 2571e73 | 2016-08-03 12:33:01 -0700 | [diff] [blame] | 5221 | } |
| 5222 | /* |
| 5223 | * We need to firstly lock all pages to make sure that |
| 5224 | * the uptodate bit of our pages won't be affected by |
| 5225 | * clear_extent_buffer_uptodate(). |
| 5226 | */ |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5227 | for (i = 0; i < num_pages; i++) { |
Liu Bo | 2571e73 | 2016-08-03 12:33:01 -0700 | [diff] [blame] | 5228 | page = eb->pages[i]; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5229 | if (!PageUptodate(page)) { |
| 5230 | num_reads++; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5231 | all_uptodate = 0; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5232 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5233 | } |
Liu Bo | 2571e73 | 2016-08-03 12:33:01 -0700 | [diff] [blame] | 5234 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5235 | if (all_uptodate) { |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5236 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5237 | goto unlock_exit; |
| 5238 | } |
| 5239 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 5240 | clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 5241 | eb->read_mirror = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5242 | atomic_set(&eb->io_pages, num_reads); |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5243 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5244 | page = eb->pages[i]; |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5245 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5246 | if (!PageUptodate(page)) { |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5247 | if (ret) { |
| 5248 | atomic_dec(&eb->io_pages); |
| 5249 | unlock_page(page); |
| 5250 | continue; |
| 5251 | } |
| 5252 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5253 | ClearPageError(page); |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5254 | err = __extent_read_full_page(tree, page, |
David Sterba | 6af49db | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 5255 | btree_get_extent, &bio, |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 5256 | mirror_num, &bio_flags, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 5257 | REQ_META); |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5258 | if (err) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5259 | ret = err; |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5260 | /* |
| 5261 | * We use &bio in above __extent_read_full_page, |
| 5262 | * so we ensure that if it returns error, the |
| 5263 | * current page fails to add itself to bio and |
| 5264 | * it's been unlocked. |
| 5265 | * |
| 5266 | * We must dec io_pages by ourselves. |
| 5267 | */ |
| 5268 | atomic_dec(&eb->io_pages); |
| 5269 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5270 | } else { |
| 5271 | unlock_page(page); |
| 5272 | } |
| 5273 | } |
| 5274 | |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 5275 | if (bio) { |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 5276 | err = submit_one_bio(bio, mirror_num, bio_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5277 | if (err) |
| 5278 | return err; |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 5279 | } |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5280 | |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 5281 | if (ret || wait != WAIT_COMPLETE) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5282 | return ret; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5283 | |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5284 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5285 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5286 | wait_on_page_locked(page); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5287 | if (!PageUptodate(page)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5288 | ret = -EIO; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5289 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5290 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5291 | return ret; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5292 | |
| 5293 | unlock_exit: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5294 | while (locked_pages > 0) { |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5295 | locked_pages--; |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5296 | page = eb->pages[locked_pages]; |
| 5297 | unlock_page(page); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5298 | } |
| 5299 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5300 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5301 | |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 5302 | void read_extent_buffer(const struct extent_buffer *eb, void *dstv, |
| 5303 | unsigned long start, unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5304 | { |
| 5305 | size_t cur; |
| 5306 | size_t offset; |
| 5307 | struct page *page; |
| 5308 | char *kaddr; |
| 5309 | char *dst = (char *)dstv; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5310 | size_t start_offset = offset_in_page(eb->start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5311 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5312 | |
Liu Bo | f716abd | 2017-08-09 11:10:16 -0600 | [diff] [blame] | 5313 | if (start + len > eb->len) { |
| 5314 | WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n", |
| 5315 | eb->start, eb->len, start, len); |
| 5316 | memset(dst, 0, len); |
| 5317 | return; |
| 5318 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5319 | |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5320 | offset = offset_in_page(start_offset + start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5321 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5322 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5323 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5324 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5325 | cur = min(len, (PAGE_SIZE - offset)); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5326 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5327 | memcpy(dst, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5328 | |
| 5329 | dst += cur; |
| 5330 | len -= cur; |
| 5331 | offset = 0; |
| 5332 | i++; |
| 5333 | } |
| 5334 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5335 | |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 5336 | int read_extent_buffer_to_user(const struct extent_buffer *eb, |
| 5337 | void __user *dstv, |
| 5338 | unsigned long start, unsigned long len) |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5339 | { |
| 5340 | size_t cur; |
| 5341 | size_t offset; |
| 5342 | struct page *page; |
| 5343 | char *kaddr; |
| 5344 | char __user *dst = (char __user *)dstv; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5345 | size_t start_offset = offset_in_page(eb->start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5346 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5347 | int ret = 0; |
| 5348 | |
| 5349 | WARN_ON(start > eb->len); |
| 5350 | WARN_ON(start + len > eb->start + eb->len); |
| 5351 | |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5352 | offset = offset_in_page(start_offset + start); |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5353 | |
| 5354 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5355 | page = eb->pages[i]; |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5356 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5357 | cur = min(len, (PAGE_SIZE - offset)); |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5358 | kaddr = page_address(page); |
| 5359 | if (copy_to_user(dst, kaddr + offset, cur)) { |
| 5360 | ret = -EFAULT; |
| 5361 | break; |
| 5362 | } |
| 5363 | |
| 5364 | dst += cur; |
| 5365 | len -= cur; |
| 5366 | offset = 0; |
| 5367 | i++; |
| 5368 | } |
| 5369 | |
| 5370 | return ret; |
| 5371 | } |
| 5372 | |
Liu Bo | 415b35a | 2016-06-17 19:16:21 -0700 | [diff] [blame] | 5373 | /* |
| 5374 | * return 0 if the item is found within a page. |
| 5375 | * return 1 if the item spans two pages. |
| 5376 | * return -EINVAL otherwise. |
| 5377 | */ |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 5378 | int map_private_extent_buffer(const struct extent_buffer *eb, |
| 5379 | unsigned long start, unsigned long min_len, |
| 5380 | char **map, unsigned long *map_start, |
| 5381 | unsigned long *map_len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5382 | { |
Johannes Thumshirn | cc2c39d | 2018-11-28 09:54:54 +0100 | [diff] [blame] | 5383 | size_t offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5384 | char *kaddr; |
| 5385 | struct page *p; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5386 | size_t start_offset = offset_in_page(eb->start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5387 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5388 | unsigned long end_i = (start_offset + start + min_len - 1) >> |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5389 | PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5390 | |
Liu Bo | f716abd | 2017-08-09 11:10:16 -0600 | [diff] [blame] | 5391 | if (start + min_len > eb->len) { |
| 5392 | WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n", |
| 5393 | eb->start, eb->len, start, min_len); |
| 5394 | return -EINVAL; |
| 5395 | } |
| 5396 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5397 | if (i != end_i) |
Liu Bo | 415b35a | 2016-06-17 19:16:21 -0700 | [diff] [blame] | 5398 | return 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5399 | |
| 5400 | if (i == 0) { |
| 5401 | offset = start_offset; |
| 5402 | *map_start = 0; |
| 5403 | } else { |
| 5404 | offset = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5405 | *map_start = ((u64)i << PAGE_SHIFT) - start_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5406 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5407 | |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5408 | p = eb->pages[i]; |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5409 | kaddr = page_address(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5410 | *map = kaddr + offset; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5411 | *map_len = PAGE_SIZE - offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5412 | return 0; |
| 5413 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5414 | |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 5415 | int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv, |
| 5416 | unsigned long start, unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5417 | { |
| 5418 | size_t cur; |
| 5419 | size_t offset; |
| 5420 | struct page *page; |
| 5421 | char *kaddr; |
| 5422 | char *ptr = (char *)ptrv; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5423 | size_t start_offset = offset_in_page(eb->start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5424 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5425 | int ret = 0; |
| 5426 | |
| 5427 | WARN_ON(start > eb->len); |
| 5428 | WARN_ON(start + len > eb->start + eb->len); |
| 5429 | |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5430 | offset = offset_in_page(start_offset + start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5431 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5432 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5433 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5434 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5435 | cur = min(len, (PAGE_SIZE - offset)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5436 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5437 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5438 | ret = memcmp(ptr, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5439 | if (ret) |
| 5440 | break; |
| 5441 | |
| 5442 | ptr += cur; |
| 5443 | len -= cur; |
| 5444 | offset = 0; |
| 5445 | i++; |
| 5446 | } |
| 5447 | return ret; |
| 5448 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5449 | |
David Sterba | f157bf7 | 2016-11-09 17:43:38 +0100 | [diff] [blame] | 5450 | void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb, |
| 5451 | const void *srcv) |
| 5452 | { |
| 5453 | char *kaddr; |
| 5454 | |
| 5455 | WARN_ON(!PageUptodate(eb->pages[0])); |
| 5456 | kaddr = page_address(eb->pages[0]); |
| 5457 | memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv, |
| 5458 | BTRFS_FSID_SIZE); |
| 5459 | } |
| 5460 | |
| 5461 | void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv) |
| 5462 | { |
| 5463 | char *kaddr; |
| 5464 | |
| 5465 | WARN_ON(!PageUptodate(eb->pages[0])); |
| 5466 | kaddr = page_address(eb->pages[0]); |
| 5467 | memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv, |
| 5468 | BTRFS_FSID_SIZE); |
| 5469 | } |
| 5470 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5471 | void write_extent_buffer(struct extent_buffer *eb, const void *srcv, |
| 5472 | unsigned long start, unsigned long len) |
| 5473 | { |
| 5474 | size_t cur; |
| 5475 | size_t offset; |
| 5476 | struct page *page; |
| 5477 | char *kaddr; |
| 5478 | char *src = (char *)srcv; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5479 | size_t start_offset = offset_in_page(eb->start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5480 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5481 | |
| 5482 | WARN_ON(start > eb->len); |
| 5483 | WARN_ON(start + len > eb->start + eb->len); |
| 5484 | |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5485 | offset = offset_in_page(start_offset + start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5486 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5487 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5488 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5489 | WARN_ON(!PageUptodate(page)); |
| 5490 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5491 | cur = min(len, PAGE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5492 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5493 | memcpy(kaddr + offset, src, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5494 | |
| 5495 | src += cur; |
| 5496 | len -= cur; |
| 5497 | offset = 0; |
| 5498 | i++; |
| 5499 | } |
| 5500 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5501 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 5502 | void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start, |
| 5503 | unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5504 | { |
| 5505 | size_t cur; |
| 5506 | size_t offset; |
| 5507 | struct page *page; |
| 5508 | char *kaddr; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5509 | size_t start_offset = offset_in_page(eb->start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5510 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5511 | |
| 5512 | WARN_ON(start > eb->len); |
| 5513 | WARN_ON(start + len > eb->start + eb->len); |
| 5514 | |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5515 | offset = offset_in_page(start_offset + start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5516 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5517 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5518 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5519 | WARN_ON(!PageUptodate(page)); |
| 5520 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5521 | cur = min(len, PAGE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5522 | kaddr = page_address(page); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 5523 | memset(kaddr + offset, 0, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5524 | |
| 5525 | len -= cur; |
| 5526 | offset = 0; |
| 5527 | i++; |
| 5528 | } |
| 5529 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5530 | |
David Sterba | 58e8012 | 2016-11-08 18:30:31 +0100 | [diff] [blame] | 5531 | void copy_extent_buffer_full(struct extent_buffer *dst, |
| 5532 | struct extent_buffer *src) |
| 5533 | { |
| 5534 | int i; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5535 | int num_pages; |
David Sterba | 58e8012 | 2016-11-08 18:30:31 +0100 | [diff] [blame] | 5536 | |
| 5537 | ASSERT(dst->len == src->len); |
| 5538 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5539 | num_pages = num_extent_pages(dst); |
David Sterba | 58e8012 | 2016-11-08 18:30:31 +0100 | [diff] [blame] | 5540 | for (i = 0; i < num_pages; i++) |
| 5541 | copy_page(page_address(dst->pages[i]), |
| 5542 | page_address(src->pages[i])); |
| 5543 | } |
| 5544 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5545 | void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src, |
| 5546 | unsigned long dst_offset, unsigned long src_offset, |
| 5547 | unsigned long len) |
| 5548 | { |
| 5549 | u64 dst_len = dst->len; |
| 5550 | size_t cur; |
| 5551 | size_t offset; |
| 5552 | struct page *page; |
| 5553 | char *kaddr; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5554 | size_t start_offset = offset_in_page(dst->start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5555 | unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5556 | |
| 5557 | WARN_ON(src->len != dst_len); |
| 5558 | |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5559 | offset = offset_in_page(start_offset + dst_offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5560 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5561 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5562 | page = dst->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5563 | WARN_ON(!PageUptodate(page)); |
| 5564 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5565 | cur = min(len, (unsigned long)(PAGE_SIZE - offset)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5566 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5567 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5568 | read_extent_buffer(src, kaddr + offset, src_offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5569 | |
| 5570 | src_offset += cur; |
| 5571 | len -= cur; |
| 5572 | offset = 0; |
| 5573 | i++; |
| 5574 | } |
| 5575 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5576 | |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5577 | /* |
| 5578 | * eb_bitmap_offset() - calculate the page and offset of the byte containing the |
| 5579 | * given bit number |
| 5580 | * @eb: the extent buffer |
| 5581 | * @start: offset of the bitmap item in the extent buffer |
| 5582 | * @nr: bit number |
| 5583 | * @page_index: return index of the page in the extent buffer that contains the |
| 5584 | * given bit number |
| 5585 | * @page_offset: return offset into the page given by page_index |
| 5586 | * |
| 5587 | * This helper hides the ugliness of finding the byte in an extent buffer which |
| 5588 | * contains a given bit. |
| 5589 | */ |
| 5590 | static inline void eb_bitmap_offset(struct extent_buffer *eb, |
| 5591 | unsigned long start, unsigned long nr, |
| 5592 | unsigned long *page_index, |
| 5593 | size_t *page_offset) |
| 5594 | { |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5595 | size_t start_offset = offset_in_page(eb->start); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5596 | size_t byte_offset = BIT_BYTE(nr); |
| 5597 | size_t offset; |
| 5598 | |
| 5599 | /* |
| 5600 | * The byte we want is the offset of the extent buffer + the offset of |
| 5601 | * the bitmap item in the extent buffer + the offset of the byte in the |
| 5602 | * bitmap item. |
| 5603 | */ |
| 5604 | offset = start_offset + start + byte_offset; |
| 5605 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5606 | *page_index = offset >> PAGE_SHIFT; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5607 | *page_offset = offset_in_page(offset); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5608 | } |
| 5609 | |
| 5610 | /** |
| 5611 | * extent_buffer_test_bit - determine whether a bit in a bitmap item is set |
| 5612 | * @eb: the extent buffer |
| 5613 | * @start: offset of the bitmap item in the extent buffer |
| 5614 | * @nr: bit number to test |
| 5615 | */ |
| 5616 | int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start, |
| 5617 | unsigned long nr) |
| 5618 | { |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5619 | u8 *kaddr; |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5620 | struct page *page; |
| 5621 | unsigned long i; |
| 5622 | size_t offset; |
| 5623 | |
| 5624 | eb_bitmap_offset(eb, start, nr, &i, &offset); |
| 5625 | page = eb->pages[i]; |
| 5626 | WARN_ON(!PageUptodate(page)); |
| 5627 | kaddr = page_address(page); |
| 5628 | return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1))); |
| 5629 | } |
| 5630 | |
| 5631 | /** |
| 5632 | * extent_buffer_bitmap_set - set an area of a bitmap |
| 5633 | * @eb: the extent buffer |
| 5634 | * @start: offset of the bitmap item in the extent buffer |
| 5635 | * @pos: bit number of the first bit |
| 5636 | * @len: number of bits to set |
| 5637 | */ |
| 5638 | void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start, |
| 5639 | unsigned long pos, unsigned long len) |
| 5640 | { |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5641 | u8 *kaddr; |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5642 | struct page *page; |
| 5643 | unsigned long i; |
| 5644 | size_t offset; |
| 5645 | const unsigned int size = pos + len; |
| 5646 | int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE); |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5647 | u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5648 | |
| 5649 | eb_bitmap_offset(eb, start, pos, &i, &offset); |
| 5650 | page = eb->pages[i]; |
| 5651 | WARN_ON(!PageUptodate(page)); |
| 5652 | kaddr = page_address(page); |
| 5653 | |
| 5654 | while (len >= bits_to_set) { |
| 5655 | kaddr[offset] |= mask_to_set; |
| 5656 | len -= bits_to_set; |
| 5657 | bits_to_set = BITS_PER_BYTE; |
Dan Carpenter | 9c89469 | 2016-10-12 11:33:21 +0300 | [diff] [blame] | 5658 | mask_to_set = ~0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5659 | if (++offset >= PAGE_SIZE && len > 0) { |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5660 | offset = 0; |
| 5661 | page = eb->pages[++i]; |
| 5662 | WARN_ON(!PageUptodate(page)); |
| 5663 | kaddr = page_address(page); |
| 5664 | } |
| 5665 | } |
| 5666 | if (len) { |
| 5667 | mask_to_set &= BITMAP_LAST_BYTE_MASK(size); |
| 5668 | kaddr[offset] |= mask_to_set; |
| 5669 | } |
| 5670 | } |
| 5671 | |
| 5672 | |
| 5673 | /** |
| 5674 | * extent_buffer_bitmap_clear - clear an area of a bitmap |
| 5675 | * @eb: the extent buffer |
| 5676 | * @start: offset of the bitmap item in the extent buffer |
| 5677 | * @pos: bit number of the first bit |
| 5678 | * @len: number of bits to clear |
| 5679 | */ |
| 5680 | void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start, |
| 5681 | unsigned long pos, unsigned long len) |
| 5682 | { |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5683 | u8 *kaddr; |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5684 | struct page *page; |
| 5685 | unsigned long i; |
| 5686 | size_t offset; |
| 5687 | const unsigned int size = pos + len; |
| 5688 | int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE); |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5689 | u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5690 | |
| 5691 | eb_bitmap_offset(eb, start, pos, &i, &offset); |
| 5692 | page = eb->pages[i]; |
| 5693 | WARN_ON(!PageUptodate(page)); |
| 5694 | kaddr = page_address(page); |
| 5695 | |
| 5696 | while (len >= bits_to_clear) { |
| 5697 | kaddr[offset] &= ~mask_to_clear; |
| 5698 | len -= bits_to_clear; |
| 5699 | bits_to_clear = BITS_PER_BYTE; |
Dan Carpenter | 9c89469 | 2016-10-12 11:33:21 +0300 | [diff] [blame] | 5700 | mask_to_clear = ~0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5701 | if (++offset >= PAGE_SIZE && len > 0) { |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5702 | offset = 0; |
| 5703 | page = eb->pages[++i]; |
| 5704 | WARN_ON(!PageUptodate(page)); |
| 5705 | kaddr = page_address(page); |
| 5706 | } |
| 5707 | } |
| 5708 | if (len) { |
| 5709 | mask_to_clear &= BITMAP_LAST_BYTE_MASK(size); |
| 5710 | kaddr[offset] &= ~mask_to_clear; |
| 5711 | } |
| 5712 | } |
| 5713 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5714 | static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len) |
| 5715 | { |
| 5716 | unsigned long distance = (src > dst) ? src - dst : dst - src; |
| 5717 | return distance < len; |
| 5718 | } |
| 5719 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5720 | static void copy_pages(struct page *dst_page, struct page *src_page, |
| 5721 | unsigned long dst_off, unsigned long src_off, |
| 5722 | unsigned long len) |
| 5723 | { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5724 | char *dst_kaddr = page_address(dst_page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5725 | char *src_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5726 | int must_memmove = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5727 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5728 | if (dst_page != src_page) { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5729 | src_kaddr = page_address(src_page); |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5730 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5731 | src_kaddr = dst_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5732 | if (areas_overlap(src_off, dst_off, len)) |
| 5733 | must_memmove = 1; |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5734 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5735 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5736 | if (must_memmove) |
| 5737 | memmove(dst_kaddr + dst_off, src_kaddr + src_off, len); |
| 5738 | else |
| 5739 | memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5740 | } |
| 5741 | |
| 5742 | void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 5743 | unsigned long src_offset, unsigned long len) |
| 5744 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5745 | struct btrfs_fs_info *fs_info = dst->fs_info; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5746 | size_t cur; |
| 5747 | size_t dst_off_in_page; |
| 5748 | size_t src_off_in_page; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5749 | size_t start_offset = offset_in_page(dst->start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5750 | unsigned long dst_i; |
| 5751 | unsigned long src_i; |
| 5752 | |
| 5753 | if (src_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5754 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5755 | "memmove bogus src_offset %lu move len %lu dst len %lu", |
| 5756 | src_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5757 | BUG_ON(1); |
| 5758 | } |
| 5759 | if (dst_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5760 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5761 | "memmove bogus dst_offset %lu move len %lu dst len %lu", |
| 5762 | dst_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5763 | BUG_ON(1); |
| 5764 | } |
| 5765 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5766 | while (len > 0) { |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5767 | dst_off_in_page = offset_in_page(start_offset + dst_offset); |
| 5768 | src_off_in_page = offset_in_page(start_offset + src_offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5769 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5770 | dst_i = (start_offset + dst_offset) >> PAGE_SHIFT; |
| 5771 | src_i = (start_offset + src_offset) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5772 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5773 | cur = min(len, (unsigned long)(PAGE_SIZE - |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5774 | src_off_in_page)); |
| 5775 | cur = min_t(unsigned long, cur, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5776 | (unsigned long)(PAGE_SIZE - dst_off_in_page)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5777 | |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5778 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5779 | dst_off_in_page, src_off_in_page, cur); |
| 5780 | |
| 5781 | src_offset += cur; |
| 5782 | dst_offset += cur; |
| 5783 | len -= cur; |
| 5784 | } |
| 5785 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5786 | |
| 5787 | void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 5788 | unsigned long src_offset, unsigned long len) |
| 5789 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5790 | struct btrfs_fs_info *fs_info = dst->fs_info; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5791 | size_t cur; |
| 5792 | size_t dst_off_in_page; |
| 5793 | size_t src_off_in_page; |
| 5794 | unsigned long dst_end = dst_offset + len - 1; |
| 5795 | unsigned long src_end = src_offset + len - 1; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5796 | size_t start_offset = offset_in_page(dst->start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5797 | unsigned long dst_i; |
| 5798 | unsigned long src_i; |
| 5799 | |
| 5800 | if (src_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5801 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5802 | "memmove bogus src_offset %lu move len %lu len %lu", |
| 5803 | src_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5804 | BUG_ON(1); |
| 5805 | } |
| 5806 | if (dst_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5807 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5808 | "memmove bogus dst_offset %lu move len %lu len %lu", |
| 5809 | dst_offset, len, dst->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5810 | BUG_ON(1); |
| 5811 | } |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5812 | if (dst_offset < src_offset) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5813 | memcpy_extent_buffer(dst, dst_offset, src_offset, len); |
| 5814 | return; |
| 5815 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5816 | while (len > 0) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5817 | dst_i = (start_offset + dst_end) >> PAGE_SHIFT; |
| 5818 | src_i = (start_offset + src_end) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5819 | |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5820 | dst_off_in_page = offset_in_page(start_offset + dst_end); |
| 5821 | src_off_in_page = offset_in_page(start_offset + src_end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5822 | |
| 5823 | cur = min_t(unsigned long, len, src_off_in_page + 1); |
| 5824 | cur = min(cur, dst_off_in_page + 1); |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5825 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5826 | dst_off_in_page - cur + 1, |
| 5827 | src_off_in_page - cur + 1, cur); |
| 5828 | |
| 5829 | dst_end -= cur; |
| 5830 | src_end -= cur; |
| 5831 | len -= cur; |
| 5832 | } |
| 5833 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5834 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5835 | int try_release_extent_buffer(struct page *page) |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5836 | { |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5837 | struct extent_buffer *eb; |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5838 | |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5839 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5840 | * We need to make sure nobody is attaching this page to an eb right |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5841 | * now. |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5842 | */ |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5843 | spin_lock(&page->mapping->private_lock); |
| 5844 | if (!PagePrivate(page)) { |
| 5845 | spin_unlock(&page->mapping->private_lock); |
| 5846 | return 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5847 | } |
| 5848 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5849 | eb = (struct extent_buffer *)page->private; |
| 5850 | BUG_ON(!eb); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5851 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5852 | /* |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5853 | * This is a little awful but should be ok, we need to make sure that |
| 5854 | * the eb doesn't disappear out from under us while we're looking at |
| 5855 | * this page. |
| 5856 | */ |
| 5857 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5858 | if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5859 | spin_unlock(&eb->refs_lock); |
| 5860 | spin_unlock(&page->mapping->private_lock); |
| 5861 | return 0; |
| 5862 | } |
| 5863 | spin_unlock(&page->mapping->private_lock); |
| 5864 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5865 | /* |
| 5866 | * If tree ref isn't set then we know the ref on this eb is a real ref, |
| 5867 | * so just return, this page will likely be freed soon anyway. |
| 5868 | */ |
| 5869 | if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { |
| 5870 | spin_unlock(&eb->refs_lock); |
| 5871 | return 0; |
| 5872 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5873 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5874 | return release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5875 | } |